Class: Prepd::Machine
- Includes:
- Component
- Defined in:
- lib/prepd/models/machine.rb
Constant Summary collapse
- WORK_DIR =
'machines'
- USER_CONFIG_FILE =
'build.yml'
- VALID_BUMP_NAMES =
%w(major minor patch)
Instance Attribute Summary collapse
-
#bump ⇒ Object
Returns the value of attribute bump.
Instance Method Summary collapse
- #action_file ⇒ Object
-
#base_dir ⇒ Object
Derive values for image and box.
- #box_json_file ⇒ Object
-
#box_version ⇒ Object
Calculate the next box version.
- #build ⇒ Object
-
#build_action ⇒ Object
Caclulate the packer builder to run: :iso, :build, :rebuild or :push.
-
#build_box_dir ⇒ Object
def build_box_dir; “#Dir.pwd/#'name'/boxes” end.
-
#build_env ⇒ Object
Setup env vars for the builder.
-
#build_image_dir ⇒ Object
def build_image_dir; “#'name'/images/#name” end.
- #build_image_name ⇒ Object
- #inc(version, type: 'patch') ⇒ Object
- #name_included_in_yaml ⇒ Object
-
#os_build ⇒ Object
Get references to current build, the os build and the os env.
- #os_env ⇒ Object
-
#perform ⇒ Object
Execute the builder.
- #set_defaults ⇒ Object
- #valid_build_names ⇒ Object
- #yml ⇒ Object
Methods included from Component
#component_dir, #component_directory_does_not_exist, #component_root, #files_dir, #in_component_dir, #in_component_root, #in_workspace_root, #klass_name, #workspace_root
Methods inherited from Base
Instance Attribute Details
#bump ⇒ Object
Returns the value of attribute bump.
13 14 15 |
# File 'lib/prepd/models/machine.rb', line 13 def bump @bump end |
Instance Method Details
#action_file ⇒ Object
57 58 59 60 |
# 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 ⇒ Object
Derive values for image and box
83 |
# File 'lib/prepd/models/machine.rb', line 83 def base_dir; os_env['base_dir'] end |
#box_json_file ⇒ Object
89 |
# File 'lib/prepd/models/machine.rb', line 89 def box_json_file; "#{build_box_dir}/#{build_image_name}.json" end |
#box_version ⇒ Object
Calculate the next box version
129 130 131 132 133 134 135 |
# 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 ⇒ Object
62 63 64 |
# File 'lib/prepd/models/machine.rb', line 62 def build yml['images'][name] end |
#build_action ⇒ Object
Caclulate the packer builder to run: :iso, :build, :rebuild or :push
94 95 96 97 98 |
# 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 ⇒ Object
def build_box_dir; “#Dir.pwd/#'name'/boxes” end
88 |
# File 'lib/prepd/models/machine.rb', line 88 def build_box_dir; "#{Dir.pwd}/boxes" end |
#build_env ⇒ Object
Setup env vars for the builder
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# 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 ⇒ Object
def build_image_dir; “#'name'/images/#name” end
85 |
# File 'lib/prepd/models/machine.rb', line 85 def build_image_dir; "images/#{name}" end |
#build_image_name ⇒ Object
86 |
# File 'lib/prepd/models/machine.rb', line 86 def build_image_name; "#{os_env['base_name']}-#{name}" end |
#inc(version, type: 'patch') ⇒ Object
137 138 139 140 141 142 143 144 |
# 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 ⇒ Object
26 27 28 29 |
# 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 ⇒ Object
Get references to current build, the os build and the os env
67 68 69 70 71 72 73 74 |
# 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 ⇒ Object
76 77 78 |
# File 'lib/prepd/models/machine.rb', line 76 def os_env yml['os_images'][os_build['source']['os_image']] end |
#perform ⇒ Object
Execute the builder
46 47 48 49 50 51 52 53 54 55 |
# 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 ⇒ Object
22 23 24 |
# File 'lib/prepd/models/machine.rb', line 22 def set_defaults self.bump ||= Prepd.config.bump || 'patch' end |
#valid_build_names ⇒ Object
31 32 33 |
# File 'lib/prepd/models/machine.rb', line 31 def valid_build_names yml['images'].keys end |
#yml ⇒ Object
35 36 37 38 39 40 41 |
# 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 |