linecook-gem 0.1.9 → 0.2.0

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: 807017706c5a506cfb424b1ec39cd94b7c404c68
4
- data.tar.gz: fa3b10d58e469b0b90c45bcc9ff83609e7a2302c
3
+ metadata.gz: 3c2ebac88ea5856749e3c19115a06aee40f9c258
4
+ data.tar.gz: 21c9e7a2166694a3793813c82d0131963e3f7b78
5
5
  SHA512:
6
- metadata.gz: a8b27d7bf3e905e3560f3febdac266db5304df8e951dd395f2cef086a4637e3ed2e4e5b17de9b7236458b7b0de798689b4ef10eec2fe81a51a4d35fff98ed60d
7
- data.tar.gz: edac426ed6c88470c88621a6c4c5a7ee2b1da7a303e586f4d04d9c8d00603d68fa61e25f165d3c431253a11e996cf9ca6e3dad752a59851dfa60d8fe33ff23f6
6
+ metadata.gz: 73f4b086e94dcea9fe1bb9106becf8dcb6bee7293c16b07ed467301335dd3409996808e86fe2bd8abf0cbadfc0e0aa9330bd24e1abba274ea0e4da00c3429631
7
+ data.tar.gz: 88b2c87c88f17445e74da4ad4c57ea6bd92b2ae3f41cd2e9989ffacebaee9f45172fa121f43ca72ca98d87ba49fc987139068675cb5784f176a984070658a621
@@ -6,10 +6,12 @@ module Linecook
6
6
  extend Forwardable
7
7
 
8
8
  def_instance_delegators :@container, :stop, :start, :ip, :info
9
+ attr_reader :type
9
10
 
10
- def initialize(name, tag: nil, image: nil)
11
+ def initialize(name, tag: nil, image: nil, id: nil)
11
12
  Linecook::Builder.start
12
- @id = tag ? "#{name}-#{tag}" : name
13
+ @type = tag ? "#{name}-#{tag}" : name
14
+ @id = id ? "#{@type}-#{id}" : @type
13
15
  @image = image || Linecook.config[:provisioner][:default_image]
14
16
  @container = Linecook::Lxc::Container.new(name: @id, image: @image, remote: Linecook::Builder.ssh)
15
17
  end
data/lib/linecook/cli.rb CHANGED
@@ -23,7 +23,18 @@ class Image < Thor
23
23
  subcommand 'crypto', Crypto
24
24
 
25
25
  desc 'list', 'List images' # add remote flag
26
+ method_option :type, type: :string, required: false, banner: 'ID', desc: 'Type of image to list', aliases: '-t'
27
+ method_option :profile, type: :string, default: 'private', banner: 'PROFILE', desc: 'Profile (public/private) of image to list', enum: %w(public private), aliases: '-p'
26
28
  def list
29
+ opts = options.symbolize_keys
30
+ puts Linecook::ImageManager.list(**opts)
31
+ end
32
+
33
+ desc 'latest ID', 'Get the latest image by id' # add remote flag
34
+ method_option :profile, type: :string, default: 'private', banner: 'PROFILE', desc: 'Profile (public/private) of image to list', enum: %w(public private), aliases: '-p'
35
+ def latest(id)
36
+ opts = options.symbolize_keys
37
+ puts Linecook::ImageManager.latest(id, **opts)
27
38
  end
28
39
 
29
40
  desc 'fetch IMAGE_NAME', 'Fetch an image by name'
@@ -39,9 +50,10 @@ class Image < Thor
39
50
  end
40
51
 
41
52
  desc 'url IMAGE', 'Get URL for image'
42
- method_options image: :string
53
+ method_option :type, type: :string, required: false, banner: 'ID', desc: 'Type of image to list', aliases: '-t'
43
54
  def url(image)
44
- puts Linecook::ImageManager.url(image)
55
+ opts = options.symbolize_keys
56
+ puts Linecook::ImageManager.url(image, **opts)
45
57
  end
46
58
 
47
59
  desc 'package IMAGE', 'Package image'
@@ -109,7 +121,7 @@ class Linecook::CLI < Thor
109
121
  desc 'bake', 'Bake a new image.'
110
122
  method_option :name, type: :string, required: true, banner: 'ROLE_NAME', desc: 'Name of the role to build', aliases: '-n'
111
123
  method_option :tag, type: :string, required: false, banner: 'TAG', desc: 'Optional tag for a build', aliases: '-t'
112
- method_option :image, type: :string, banner: 'SOURCE_IMAGE', desc: 'Source image to seed the build.', aliases: '-i'
124
+ method_option :id, type: :string, required: false, banner: 'ID', desc: 'Optional id for a build', aliases: '-i'
113
125
  method_option :keep, type: :boolean, default: true, desc: 'Keep the build running when done', aliases: '-k'
114
126
  method_option :clean, type: :boolean, default: false, desc: 'Clean up all build artifacts', aliases: '-c'
115
127
  method_option :build, type: :boolean, default: true, desc: 'Build the image', aliases: '-b'
@@ -1,5 +1,6 @@
1
1
  require 'base64'
2
2
  require 'encryptor'
3
+ require 'tempfile'
3
4
 
4
5
  require 'linecook/image/manager'
5
6
  require 'linecook/util/executor'
@@ -24,13 +25,21 @@ module Linecook
24
25
 
25
26
  def encrypt_file(source, dest: nil, keypath: nil)
26
27
  dest ||= "/tmp/#{File.basename(source)}"
27
- capture("openssl enc -#{CIPHER} -out #{dest} -in #{source} -pass 'pass:#{@secret_key}'", sudo: false)
28
+ Tempfile.open('key') do |key|
29
+ key.write(@secret_key)
30
+ key.flush
31
+ capture("openssl enc -#{CIPHER} -out #{dest} -in #{source} -kfile #{key.path}", sudo: false)
32
+ end
28
33
  dest
29
34
  end
30
35
 
31
36
  def decrypt_file(source, dest: nil, keypath: nil)
32
37
  dest ||= "/tmp/#{File.basename(source)}-decrypted"
33
- capture("openssl enc -#{CIPHER} -out #{dest} -in #{source} -pass pass:#{@secret_key}' -d", sudo: false)
38
+ Tempfile.open('key') do |key|
39
+ @remote.upload(@secret_key, key.path) if @remote
40
+ capture("openssl enc -#{CIPHER} -out #{dest} -in #{source} -kfile #{key.path} -d", sudo: false)
41
+ @remote.run("rm #{key.path}") if @remote
42
+ end
34
43
  dest
35
44
  end
36
45
 
@@ -18,16 +18,26 @@ module Linecook
18
18
  path
19
19
  end
20
20
 
21
- def upload(image, profile: :private)
21
+ def upload(image, profile: :private, type: nil)
22
22
  path = File.join(IMAGE_PATH, File.basename(image))
23
23
  puts "Encrypting and uploading image #{path}"
24
24
  encrypted = Linecook::Crypto.new.encrypt_file(path)
25
- provider(profile).upload(encrypted)
25
+ provider(profile).upload(encrypted, type: type)
26
26
  FileUtils.rm_f(encrypted)
27
27
  end
28
28
 
29
- def url(image, profile: :private)
30
- provider(profile).url("builds/#{image}")
29
+ def url(image, profile: :private, type: nil)
30
+ provider(profile).url(image, type: type)
31
+ end
32
+
33
+ def list(type: nil, profile: :private)
34
+ profile = profile.to_sym
35
+ provider(profile).list(type: type)
36
+ end
37
+
38
+ def latest(type, profile: :private)
39
+ profile = profile.to_sym
40
+ provider(profile).latest(type)
31
41
  end
32
42
 
33
43
  private
@@ -5,19 +5,29 @@ module Linecook
5
5
  module S3Manager
6
6
  extend self
7
7
  EXPIRY = 20
8
+ PREFIX = 'builds'
8
9
 
9
- def url(name)
10
+ def url(name, type: nil)
10
11
  client
11
12
  s3 = Aws::S3::Resource.new
12
- obj = s3.bucket(Linecook.config[:aws][:s3][:bucket]).object(name)
13
+ obj = s3.bucket(Linecook.config[:aws][:s3][:bucket]).object(File.join([PREFIX, type, name].compact))
13
14
  obj.presigned_url(:get, expires_in: EXPIRY * 60)
14
15
  end
15
16
 
16
- def upload(path)
17
+ def list(type: nil)
18
+ list_objects(type: type).map{ |x| x.key if x.key =~ /squashfs$/ }.compact
19
+ end
20
+
21
+ def latest(type)
22
+ objects = list_objects(type: type).sort! { |a,b| a.last_modified <=> b.last_modified }
23
+ key = objects.last ? objects.last.key : nil
24
+ end
25
+
26
+ def upload(path, type: nil)
17
27
  File.open(path, 'rb') do |file|
18
28
  fname = File.basename(path)
19
29
  pbar = ProgressBar.create(title: fname, total: file.size)
20
- common_opts = { bucket: Linecook.config[:aws][:s3][:bucket], key: File.join('builds', fname) }
30
+ common_opts = { bucket: Linecook.config[:aws][:s3][:bucket], key: File.join([PREFIX, type, fname].compact) }
21
31
  resp = client.create_multipart_upload(storage_class: 'REDUCED_REDUNDANCY', server_side_encryption: 'AES256', **common_opts)
22
32
  id = resp.upload_id
23
33
  part = 0
@@ -36,6 +46,11 @@ module Linecook
36
46
  end
37
47
 
38
48
  private
49
+
50
+ def list_objects(type: nil)
51
+ client.list_objects(bucket: Linecook.config[:aws][:s3][:bucket], prefix: File.join([PREFIX, type].compact)).contents
52
+ end
53
+
39
54
  def client
40
55
  @client ||= begin
41
56
  Aws.config[:credentials] = Aws::Credentials.new(Linecook.config[:aws][:access_key], Linecook.config[:aws][:secret_key])
@@ -20,8 +20,9 @@ module Linecook
20
20
  @region = region
21
21
  end
22
22
 
23
- def package(image)
23
+ def package(image, type: nil)
24
24
  @image = image
25
+ @type = type
25
26
  setup_remote unless instance_id
26
27
  prepare
27
28
  execute("tar -C #{@mountpoint} -cpf - . | sudo tar -C #{@root} -xpf -")
@@ -166,7 +167,7 @@ module Linecook
166
167
  def setup_remote
167
168
  start_node
168
169
  path = "/tmp/#{File.basename(@image)}"
169
- @remote.run("wget '#{Linecook::ImageManager.url(File.basename(@image))}' -nv -O #{path}")
170
+ @remote.run("wget '#{Linecook::ImageManager.url(File.basename(@image), type: @type)}' -nv -O #{path}")
170
171
  @image = Linecook::Crypto.new(remote: @remote).decrypt_file(path)
171
172
  end
172
173
 
@@ -4,8 +4,8 @@ module Linecook
4
4
  module Packager
5
5
  extend self
6
6
 
7
- def package(image)
8
- provider.package(image)
7
+ def package(image, type: type)
8
+ provider.package(image, type: type)
9
9
  end
10
10
 
11
11
  private
@@ -9,12 +9,12 @@ module Linecook
9
9
  module Baker
10
10
  extend self
11
11
 
12
- def bake(name: nil, tag: nil, image: nil, snapshot: nil, upload: nil, package: nil, build: nil, keep: nil, clean: nil)
13
- build_agent = Linecook::Build.new(name, tag: tag, image: image)
12
+ def bake(name: nil, tag: nil, id: nil, snapshot: nil, upload: nil, package: nil, build: nil, keep: nil, clean: nil)
13
+ build_agent = Linecook::Build.new(name, tag: tag, id: id)
14
14
  provider(name).provision(build_agent, name) if build
15
15
  snapshot = build_agent.snapshot(save: true) if snapshot || upload || package
16
- Linecook::ImageManager.upload(snapshot) if upload || package
17
- Linecook::Packager.package(snapshot) if package
16
+ Linecook::ImageManager.upload(snapshot, type: build_agent.type) if upload || package
17
+ Linecook::Packager.package(snapshot, type: build_agent.type) if package
18
18
  ensure
19
19
  build_agent.stop unless keep
20
20
  FileUtils.rm_f(snapshot) if clean
@@ -1,3 +1,3 @@
1
1
  module Linecook
2
- VERSION = '0.1.9'
2
+ VERSION = '0.2.0'
3
3
  end
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.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Hamel