diff --git a/lib/linecook/builder/lxc.rb b/lib/linecook/builder/lxc.rb index 10c2265..d86efa5 100644 --- a/lib/linecook/builder/lxc.rb +++ b/lib/linecook/builder/lxc.rb @@ -4,6 +4,7 @@ require 'linecook/util/executor' require 'tempfile' require 'ipaddress' +require 'linecook/packager/ebs' module Linecook module Lxc @@ -20,6 +21,7 @@ def initialize(name: 'linecook', home: '/u/lxc', image: nil, remote: :local, bri @root = File.join(@home, @name, 'rootfs') config = { utsname: name, rootfs: @root } @config = Linecook::Lxc::Config.generate(config) # FIXME read link from config + @ebs_config = Linecook.config[:packager][:ebs] @source_image = image || :base_image end @@ -133,6 +135,10 @@ def write_config file.close file.path end + if @ebs_config[:src_ami] && @remote + @src_ami_attached_volume = Linecook::Packager::EBS.new(@ebs_config).mount_src_ami if @ebs_config[:src_ami] + execute("lxc-device add #{container_str} #{@src_ami_attached_volume}1") if @ebs_config[:src_ami] + end execute("mv #{path} #{File.join(@home, @name, 'config')}") end diff --git a/lib/linecook/packager/ebs.rb b/lib/linecook/packager/ebs.rb index fd6c3de..d70f5e8 100644 --- a/lib/linecook/packager/ebs.rb +++ b/lib/linecook/packager/ebs.rb @@ -15,12 +15,13 @@ module Packager class EBS include Executor - def initialize(hvm: true, size: 10, region: nil, copy_regions: [], account_ids: []) + def initialize(hvm: true, size: 10, region: nil, copy_regions: [], account_ids: [], src_ami: nil) @hvm = hvm @size = size @region = region @copy_regions = copy_regions @account_ids = account_ids + @src_ami = src_ami end def package(image, type: nil, ami: nil) @@ -37,6 +38,10 @@ def package(image, type: nil, ami: nil) create_ami if ami end + def mount_src_ami + create_volume(volume_type: 'gp2') + end + private def prepare @@ -100,16 +105,28 @@ def client(region: nil) @client ||= begin region ||= @region puts "Using AWS region #{region} for following request" - credentials = Aws::Credentials.new(Linecook.config[:aws][:access_key], Linecook.config[:aws][:secret_key]) + if Linecook.config[:aws][:access_key] + credentials = Aws::Credentials.new(Linecook.config[:aws][:access_key], Linecook.config[:aws][:secret_key]) + else + credentials = Aws::InstanceProfileCredentials.new + end Aws::EC2::Client.new(region: region, credentials: credentials) end end - def create_volume + def get_snapshot_id + @snapshot_id = client.describe_images({ + image_ids: [@src_ami] + }).images[0].block_device_mappings[0].ebs.snapshot_id + end + + def create_volume(snapshot_id: nil, volume_type: 'standard') + snapshot_id = get_snapshot_id if @src_ami resp = client.create_volume({ size: @size, availability_zone: availability_zone, # required - volume_type: "standard", # accepts standard, io1, gp2 + volume_type: volume_type, # accepts standard, io1, gp2 + snapshot_id: snapshot_id }) @volume_id = resp.volume_id