class Prepd::Machine

Constants

USER_CONFIG_FILE
VALID_BUMP_NAMES
WORK_DIR

Attributes

bump[RW]

Public Instance Methods

action_file() click to toggle source
# File lib/prepd/models/machine.rb, line 57
def action_file
  return "#{Prepd.files_dir}/machine/#{build_action}.json" unless build_action.eql?(:iso)
  "#{Prepd.files_dir}/machine/#{os_env['base_dir']}/iso.json"
end
base_dir() click to toggle source

Derive values for image and box

# File lib/prepd/models/machine.rb, line 83
def base_dir; os_env['base_dir'] end
box_json_file() click to toggle source
# File lib/prepd/models/machine.rb, line 89
def box_json_file; "#{build_box_dir}/#{build_image_name}.json" end
box_version() click to toggle source

Calculate the next box version

# File lib/prepd/models/machine.rb, line 129
def box_version
  return '0.0.1' unless File.exists?(box_json_file)
  json = JSON.parse(File.read(box_json_file))
  current_version = json['versions'].first['version']
  return current_version if build_action.eql?(:push)
  inc(current_version, type: bump)
end
build() click to toggle source
# File lib/prepd/models/machine.rb, line 62
def build
  yml['images'][name]
end
build_action() click to toggle source

Caclulate the packer builder to run: :iso, :build, :rebuild or :push

# File lib/prepd/models/machine.rb, line 94
def build_action
  return :push if Prepd.config.push
  return :rebuild if File.exists?("#{build_image_dir}/#{build_image_name}-disk1.vmdk")
  build['source'].has_key?('os_image') ? :iso : :build
end
build_box_dir() click to toggle source

def build_box_dir; “#{Dir.pwd}/#{yml}/boxes” end

# File lib/prepd/models/machine.rb, line 88
def build_box_dir; "#{Dir.pwd}/boxes" end
build_env() click to toggle source

Setup env vars for the builder

# File lib/prepd/models/machine.rb, line 103
def build_env
  xbuild_env = {
    'VM_BASE_NAME' => os_env['base_name'],
    'VM_INPUT' => build_action.eql?(:build) ? build['source']['image'] : name,
    'VM_OUTPUT' => name,
    'BOX_NAMESPACE' => yml['name'],
    'BOX_VERSION' => box_version,
    'PLAYBOOK_FILE' => build['provisioner'],
    'JSON_RB_FILE' => "#{Prepd.files_dir}/machine/json.rb"
  }

  xbuild_env.merge!({
    'ISO_CHECKSUM' => os_env['iso_checksum'],
    'ISO_URL' => os_env['iso_url']
  }) if build_action.eql?(:iso)

  xbuild_env.merge!({
    'AWS_PROFILE' => yml['aws']['profile'],
    'S3_BUCKET' => yml['aws']['s3_bucket'],
    'S3_REGION' => yml['aws']['s3_region'],
    'S3_BOX_DIR' => "#{yml['aws']['box_dir']}/#{yml['namesapce']}"
  }) if build_action.eql?(:push)
  xbuild_env
end
build_image_dir() click to toggle source

def build_image_dir; “#{yml}/images/#{name}” end

# File lib/prepd/models/machine.rb, line 85
def build_image_dir; "images/#{name}" end
build_image_name() click to toggle source
# File lib/prepd/models/machine.rb, line 86
def build_image_name; "#{os_env['base_name']}-#{name}" end
inc(version, type: 'patch') click to toggle source
# File lib/prepd/models/machine.rb, line 137
def inc(version, type: 'patch')
  idx = { 'major' => 0, 'minor' => 1, 'patch' => 2 }
  ver = version.split('.')
  ver[idx['patch']] = 0 if %w(major minor).include? type
  ver[idx['minor']] = 0 if %w(major).include? type
  ver[idx[type]] = ver[idx[type]].to_i + 1
  ver.join('.')
end
name_included_in_yaml() click to toggle source
# File lib/prepd/models/machine.rb, line 26
def name_included_in_yaml
  return if valid_build_names.include? name
  errors.add(:invalid_name, "valid names are #{valid_build_names.join(', ')}")
end
os_build() click to toggle source

Get references to current build, the os build and the os env

# File lib/prepd/models/machine.rb, line 67
def os_build
  os_build = build
  loop do
    break if os_build['source'].has_key?('os_image')
    os_build = yml['images'][os_build['source']['image']]
  end
  os_build
end
os_env() click to toggle source
# File lib/prepd/models/machine.rb, line 76
def os_env
  yml['os_images'][os_build['source']['os_image']]
end
perform() click to toggle source

Execute the builder

# File lib/prepd/models/machine.rb, line 46
def perform
  # return "#{build_action}\n#{build_env}" if Prepd.config.no_op
  in_component_root do
    # binding.pry
    # TODO: remove the preseed copy when running packer command from the gem directory
    FileUtils.cp("#{Prepd.files_dir}/machine/#{os_env['base_dir']}/preseed.cfg", '.')
    system(build_env, "packer build #{action_file}")
    FileUtils.rm('preseed.cfg')
  end
end
set_defaults() click to toggle source
# File lib/prepd/models/machine.rb, line 22
def set_defaults
  self.bump ||= Prepd.config.bump || 'patch'
end
valid_build_names() click to toggle source
# File lib/prepd/models/machine.rb, line 31
def valid_build_names
  yml['images'].keys
end
yml() click to toggle source
# File lib/prepd/models/machine.rb, line 35
def yml
  return @yml if @yml
  in_component_root do
    @yml = YAML.load(ERB.new(File.read("#{workspace_root}/prepd-workspace.yml")).result(binding)).merge(
      YAML.load(ERB.new(File.read("#{component_root}/#{USER_CONFIG_FILE}")).result(binding)))
  end
end