Skip to content

Commit

Permalink
add mounting of source AMI
Browse files Browse the repository at this point in the history
  • Loading branch information
stonith committed Apr 21, 2016
1 parent d431adf commit f1a919b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
6 changes: 6 additions & 0 deletions lib/linecook/builder/lxc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'linecook/util/executor'
require 'tempfile'
require 'ipaddress'
require 'linecook/packager/ebs'

module Linecook
module Lxc
Expand All @@ -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

Expand Down Expand Up @@ -133,6 +135,10 @@ def write_config
file.close
file.path
end
if @ebs_config[:src_ami] && @remote

This comment has been minimized.

Copy link
@stonith

stonith Apr 22, 2016

Author

write_config method doesn't get called if container is running, need to move this somewhere else.

@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

Expand Down
25 changes: 21 additions & 4 deletions lib/linecook/packager/ebs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f1a919b

Please sign in to comment.