linecook-gem 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1abf2321c6f050ebebaff2cd97736d181c3438d3
4
- data.tar.gz: e8482496017204409f513273352ac69b50d41295
3
+ metadata.gz: 6c4d3f814360279e6d37e60a436f6597ff6ac4b1
4
+ data.tar.gz: fefeaef64eff88fcb7c6610dcd26871aa322f5b6
5
5
  SHA512:
6
- metadata.gz: 310b56ecc9861ab86f2ed209fe8d77b64571e3b9d664747f2f785754388595be636ca1d3fcabaaf620b5c62fc2bdd3c877e11a40dee8d4ea09af4d7fe03d47f0
7
- data.tar.gz: b8a143904d6c6924cff355ebac678bae6abca2e0cac2de2ffd32698bfc58243c63b2fc73161b20d6601117ddfe977d9fd154cbb5ca5f9dc2ff2a8ff89f0e59aa
6
+ metadata.gz: a5705ef81dd3175c91ae2706974568ef0a3b771a175359aa35f7b35e103b6d15d0b5a8f2f4c65491b249bd4b46e01adbbf5989bb3cd365ee80502b695e43cf0a
7
+ data.tar.gz: 70214b40fd54f10a9046949a83f2726b9abf93c1462d70f77e2b962bababc56040ac62dcc7e0bd3a094ca6823e65cfae18f0b21a59a2859502088072d1c84a33
@@ -1,9 +1,8 @@
1
1
  require 'fileutils'
2
2
 
3
- require 'linecook/image/crypt'
4
- require 'linecook/image/github'
5
3
  require 'linecook/image/s3'
6
- require 'linecook/util/downloader'
4
+ require 'linecook/image/github'
5
+ require 'linecook/image/crypt'
7
6
 
8
7
  module Linecook
9
8
  module ImageManager
@@ -25,13 +25,15 @@ module Linecook
25
25
 
26
26
  def package(image, type: nil, ami: nil)
27
27
  @image = image
28
- @name = "#{File.basename(image).gsub('.squashfs', '').gsub('-decrypted', '')}-#{SecureRandom.hex(4)}"
28
+ @source = File.basename(@image)
29
+ @name = "#{@source}-#{SecureRandom.hex(4)}"
29
30
  @type = type
30
- setup_remote unless instance_id
31
+ setup_image
31
32
  prepare
32
33
  execute("tar -C #{@mountpoint} -cpf - . | sudo tar -C #{@root} -xpf -")
33
34
  finalize
34
35
  snapshot
36
+ cleanup
35
37
  create_ami if ami
36
38
  end
37
39
 
@@ -54,6 +56,11 @@ module Linecook
54
56
  execute("grub-install --root-directory=#{@root} $(echo #{@rootdev} | sed \"s/[0-9]*//g\")") if @hvm
55
57
  end
56
58
 
59
+ def cleanup
60
+ execute("umount #{@image}")
61
+ execute("rm -f #{@image}")
62
+ execute("rmdir #{@mountpoint}")
63
+ end
57
64
 
58
65
  def chroot_exec(command)
59
66
  execute("mount -o bind /dev #{@root}/dev")
@@ -135,7 +142,7 @@ module Linecook
135
142
  end
136
143
  resp = client.create_snapshot(volume_id: @volume_id, description: "Snapshot of #{@name}")
137
144
  @snapshot_id = resp.snapshot_id
138
- tag(@snapshot_id, Name: 'Linecook snapshot', image: @name, hvm: @hvm.to_s)
145
+ tag(@snapshot_id, Name: "Linecook snapshot for #{@source}", type: @type, image: @name, hvm: @hvm.to_s)
139
146
  client.delete_volume(volume_id: @volume_id)
140
147
  end
141
148
 
@@ -202,6 +209,11 @@ module Linecook
202
209
  })
203
210
  end
204
211
  end
212
+
213
+ amis.each do |region, ami|
214
+ tag(ami, region: region, source: @source, name: @name, type: @type, hvm: @hvm.to_s)
215
+ end
216
+ amis
205
217
  end
206
218
 
207
219
  def free_device
@@ -238,11 +250,21 @@ module Linecook
238
250
  return nil
239
251
  end
240
252
 
241
- def setup_remote
242
- start_node
253
+ def setup_image
243
254
  path = "/tmp/#{File.basename(@image)}"
244
- @remote.run("wget '#{Linecook::ImageManager.url(File.basename(@image), type: @type)}' -nv -O #{path}")
245
- @image = Linecook::Crypto.new(remote: @remote).decrypt_file(path)
255
+ if instance_id
256
+ return if File.exists?(@image)
257
+ Linecook::Downloader.download(image_url, path)
258
+ @image = path
259
+ else
260
+ start_node
261
+ @remote.run("wget '#{image_url}' -nv -O #{path}")
262
+ @image = Linecook::Crypto.new(remote: @remote).decrypt_file(path)
263
+ end
264
+ end
265
+
266
+ def image_url
267
+ @url ||= Linecook::ImageManager.url(File.basename(@image), type: @type)
246
268
  end
247
269
 
248
270
  def start_node
@@ -306,7 +328,8 @@ module Linecook
306
328
  end
307
329
 
308
330
  def tag(id, **kwargs)
309
- resp = client.create_tags(resources: [id], tags: kwargs.map{ |k,v| {key: k, value: v } })
331
+ puts "Will tag #{id} with #{kwargs}"
332
+ resp = client(region: kwargs[:region]).create_tags(resources: [id], tags: kwargs.map{ |k,v| {key: k.to_s, value: v.to_s } })
310
333
  end
311
334
 
312
335
  def key_pair
@@ -79,8 +79,15 @@ module Linecook
79
79
  def build
80
80
  if stale
81
81
  puts 'Regenerating cookbook cache'
82
- Chefdepartie.run(background: true, config: Linecook.config[:chef], cache: CACHE_PATH)
83
- Chefdepartie.stop
82
+ begin
83
+ Chefdepartie.run(background: true, config: Linecook.config[:chef], cache: CACHE_PATH)
84
+ rescue
85
+ puts 'Cache tainted, rebuilding completely'
86
+ FileUtils.rm_rf(CACHE_PATH)
87
+ Chefdepartie.run(background: true, config: Linecook.config[:chef], cache: CACHE_PATH)
88
+ ensure
89
+ Chefdepartie.stop
90
+ end
84
91
  write_stamp
85
92
  unlock
86
93
  end
@@ -69,9 +69,10 @@ module Linecook
69
69
 
70
70
  def secrets
71
71
  @secrets ||= begin
72
- if File.exists?(SECRETS_PATH)
72
+ secrets_path = ENV['LINECOOK_SECRETS_PATH'] || SECRETS_PATH
73
+ if File.exists?(secrets_path)
73
74
  ejson_path = File.join(Gem::Specification.find_by_name('ejson').gem_dir, 'build', "#{Linecook::Config.platform}-amd64", 'ejson' )
74
- command = "#{ejson_path} decrypt #{SECRETS_PATH}"
75
+ command = "#{ejson_path} decrypt #{secrets_path}"
75
76
  secrets = JSON.load(`sudo #{command}`)
76
77
  secrets.deep_symbolize_keys
77
78
  else
@@ -100,10 +101,9 @@ module Linecook
100
101
 
101
102
  def load_config
102
103
  @config ||= begin
104
+ config_path = ENV['LINECOOK_CONFIG_PATH'] || CONFIG_PATH
103
105
  config = YAML.load(File.read(DEFAULT_CONFIG_PATH)) if File.exist?(DEFAULT_CONFIG_PATH)
104
- config.deep_merge!(YAML.load(File.read(CONFIG_PATH))) if File.exist?(CONFIG_PATH)
105
- # fail "Cookbook path not provided or doesn't exist" unless (config[:chef][:cookbook_path] && Dir.exists?(config[:chef][:cookbook_path]))
106
- # fail "Databag secret not provided or doesn't exist" unless (config[:chef][:encrypted_data_bag_secret] && File.exists?(config[:chef][:encrypted_data_bag_secret]))
106
+ config.deep_merge!(YAML.load(File.read(config_path))) if File.exist?(config_path)
107
107
  (config || {}).deep_symbolize_keys!
108
108
  config.deep_merge!(secrets)
109
109
  end
@@ -1,3 +1,3 @@
1
1
  module Linecook
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
3
3
  end
data/lib/linecook.rb CHANGED
@@ -3,6 +3,7 @@ $LOAD_PATH.unshift(File.expand_path('../lib', __FILE__))
3
3
  require 'active_support/all'
4
4
  require 'linecook/version'
5
5
  require 'linecook/util/config'
6
+ require 'linecook/util/downloader'
6
7
  require 'linecook/image/manager'
7
8
  require 'linecook/builder/manager'
8
9
  require 'linecook/provisioner/manager'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linecook-gem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Hamel