linecook-gem 0.7.28 → 0.7.29
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/linecook-gem/baker.rb +4 -21
- data/lib/linecook-gem/cli.rb +2 -1
- data/lib/linecook-gem/packager.rb +2 -2
- data/lib/linecook-gem/packager/packer.rb +53 -90
- data/lib/linecook-gem/util/common.rb +20 -0
- data/lib/linecook-gem/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 617b8b12b9c62eef6319df894542e2d3289e3fa7
|
4
|
+
data.tar.gz: d13c1fb336d86b22d6178b65e4debc747d3809e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79e827a474f67b01a23dd31c6d641021c68eb6bc251a1d453e92aa0dbbefd009fe0d912841280c979714b78147b1a103dfa0cbf3aa5007f420102b57633963f5
|
7
|
+
data.tar.gz: 86232a32f22ecb61c9fee1f7b04984dd82dbf67f4e339d2aa79640823c0d40305d090a5abd2e12c49318debdfbf27e75da78359a5060aa75789fc1d4f6f8ee77
|
data/lib/linecook-gem/baker.rb
CHANGED
@@ -4,6 +4,7 @@ require 'kitchen'
|
|
4
4
|
require 'kitchen/command/test'
|
5
5
|
|
6
6
|
require 'linecook-gem/image'
|
7
|
+
require 'linecook-gem/util/common'
|
7
8
|
require 'linecook-gem/baker/docker'
|
8
9
|
|
9
10
|
module Linecook
|
@@ -13,12 +14,9 @@ module Linecook
|
|
13
14
|
def initialize(image, directory: nil)
|
14
15
|
@directory = File.expand_path(directory || Dir.pwd)
|
15
16
|
@image = image
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
munge_config
|
20
|
-
@driver = driver
|
21
|
-
end
|
17
|
+
@config = load_config
|
18
|
+
munge_config
|
19
|
+
@driver = driver
|
22
20
|
|
23
21
|
end
|
24
22
|
|
@@ -81,21 +79,6 @@ module Linecook
|
|
81
79
|
end
|
82
80
|
end
|
83
81
|
|
84
|
-
def load_config
|
85
|
-
@config ||= begin
|
86
|
-
|
87
|
-
Kitchen::Config.new(
|
88
|
-
kitchen_root: @directory,
|
89
|
-
loader: Kitchen::Loader::YAML.new(
|
90
|
-
project_config: ENV['KITCHEN_YAML'] || File.join(@directory, '.kitchen.yml'),
|
91
|
-
local_config: ENV['KITCHEN_LOCAL_YAML'],
|
92
|
-
global_config: ENV['KITCHEN_GLOBAL_YAML']
|
93
|
-
)
|
94
|
-
)
|
95
|
-
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
82
|
def munge_config
|
100
83
|
data = @config.send(:data).instance_variable_get(:@data)
|
101
84
|
@is_chef = data[:provisioner][:name] =~ /chef/
|
data/lib/linecook-gem/cli.rb
CHANGED
@@ -50,13 +50,14 @@ class Image < Thor
|
|
50
50
|
|
51
51
|
desc 'package', 'Package image'
|
52
52
|
method_option :name, type: :string, required: true, banner: 'NAME', desc: 'Name of the image to package.', aliases: '-n'
|
53
|
+
method_option :directory, type: :string, required: false, banner: 'DIR', desc: 'Directory containing kitchen files', aliases: '-d'
|
53
54
|
method_option :tag, type: :string, default: 'latest', banner: 'NAME', desc: 'Tag of the image to package.', aliases: '-t'
|
54
55
|
method_option :group, type: :string, required: false, banner: 'ID', desc: 'Group of image to package', aliases: '-g'
|
55
56
|
method_option :strategy, type: :string, default: 'packer', banner: 'STRATEGY', enum: ['packer', 'squashfs'], desc: 'Packaging strategy', aliases: '-s'
|
56
57
|
def package
|
57
58
|
opts = options.symbolize_keys
|
58
59
|
image = Linecook::Image.new(opts[:name], opts[:group], opts[:tag])
|
59
|
-
Linecook::Packager.package(image, name: opts[:strategy])
|
60
|
+
Linecook::Packager.package(image, name: opts[:strategy], directory: opts[:directory])
|
60
61
|
end
|
61
62
|
|
62
63
|
desc 'save', 'Save running build'
|
@@ -9,9 +9,9 @@ module Linecook
|
|
9
9
|
module Packager
|
10
10
|
extend self
|
11
11
|
|
12
|
-
def package(image, name: 'packer')
|
12
|
+
def package(image, name: 'packer', directory: nil)
|
13
13
|
image.fetch
|
14
|
-
provider(name.to_sym).package(image)
|
14
|
+
provider(name.to_sym).package(image, directory)
|
15
15
|
end
|
16
16
|
|
17
17
|
private
|
@@ -9,6 +9,7 @@ require 'pty'
|
|
9
9
|
require 'linecook-gem/image'
|
10
10
|
require 'linecook-gem/util/downloader'
|
11
11
|
require 'linecook-gem/util/locking'
|
12
|
+
require 'linecook-gem/util/common'
|
12
13
|
require 'linecook-gem/packager/route53'
|
13
14
|
|
14
15
|
module Linecook
|
@@ -21,44 +22,44 @@ module Linecook
|
|
21
22
|
PACKER_VERSION = '0.12.0'
|
22
23
|
PACKER_PATH = File.join(Linecook::Config::LINECOOK_HOME, 'bin', 'packer')
|
23
24
|
|
25
|
+
PRE_MOUNT_COMMANDS = [
|
26
|
+
'parted -s {{.Device}} mklabel msdos',
|
27
|
+
'parted -s {{.Device}} mkpart primary ext2 0% 100%',
|
28
|
+
'mkfs.ext4 {{.Device}}1',
|
29
|
+
'tune2fs -L cloudimg-rootfs {{.Device}}1',
|
30
|
+
]
|
31
|
+
|
32
|
+
POST_MOUNT_COMMANDS = [
|
33
|
+
'tar -C {{.MountPath}} -xpf {{ user `source_image_path` }}',
|
34
|
+
'cp /etc/resolv.conf {{.MountPath}}/etc',
|
35
|
+
'echo "LABEL=cloudimg-rootfs / ext4 defaults,discard 0 0" > {{.MountPath}}/etc/fstab',
|
36
|
+
'grub-install --root-directory={{.MountPath}} {{.Device}}',
|
37
|
+
'rm -rf {{.MountPath}}/etc/network',
|
38
|
+
'cp -r /etc/network {{.MountPath}}/etc/',
|
39
|
+
]
|
40
|
+
|
41
|
+
ROOT_DEVICE_MAP = {
|
42
|
+
device_name: 'xvda',
|
43
|
+
delete_on_termination: true
|
44
|
+
}
|
45
|
+
|
24
46
|
BUILDER_CONFIG = {
|
25
47
|
type: 'amazon-chroot',
|
26
48
|
access_key: '{{ user `aws_access_key` }}',
|
27
49
|
secret_key: '{{ user `aws_secret_key` }}',
|
28
|
-
source_ami: "{{user `source_ami`}}",
|
29
50
|
ami_name: 'packer-image.{{ user `image_name` }} {{timestamp}}',
|
30
|
-
|
51
|
+
from_scratch: true,
|
52
|
+
root_device_name: ROOT_DEVICE_MAP[:device_name],
|
53
|
+
ami_block_device_mappings: [ ROOT_DEVICE_MAP ],
|
54
|
+
pre_mount_commands: PRE_MOUNT_COMMANDS,
|
55
|
+
post_mount_commands: POST_MOUNT_COMMANDS,
|
31
56
|
}.freeze
|
32
57
|
|
33
|
-
|
34
|
-
'
|
35
|
-
'
|
36
|
-
'
|
37
|
-
'
|
38
|
-
'umount /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }}/dev',
|
39
|
-
'mv /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }}/etc/network /tmp/{{ user `ebs_device`}}-network',
|
40
|
-
'umount /dev/{{ user `ebs_device` }}1',
|
41
|
-
'mkfs.ext4 /dev/{{ user `ebs_device` }}1',
|
42
|
-
'tune2fs -L cloudimg-rootfs /dev/{{ user `ebs_device` }}1',
|
43
|
-
'mkdir -p /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }}',
|
44
|
-
'mount /dev/{{ user `ebs_device` }}1 /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }}',
|
45
|
-
'tar -C /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }} -xpf {{ user `source_image_path` }}',
|
46
|
-
'cp /etc/resolv.conf /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }}/etc',
|
47
|
-
'echo "LABEL=cloudimg-rootfs / ext4 defaults,discard 0 0" > /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }}/etc/fstab',
|
48
|
-
'mount -t proc none /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }}/proc',
|
49
|
-
'mount -o bind /sys /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }}/sys',
|
50
|
-
'mount -o bind /dev /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }}/dev',
|
51
|
-
# Sadly we need to install grub inside the image, and this implementation is Ubunut specific. This can be patched eventually when needed
|
52
|
-
'chroot /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }} apt-get update',
|
53
|
-
'chroot /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }} apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y --force-yes --no-upgrade install grub-pc grub-legacy-ec2',
|
54
|
-
'chroot /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }} update-grub',
|
55
|
-
'grub-install --root-directory=/mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }} /dev/{{ user `ebs_device` }}',
|
56
|
-
'rm -rf /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }}/etc/network',
|
57
|
-
'mv /tmp/{{ user `ebs_device`}}-network /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }}/etc/network',
|
58
|
-
'rm -f /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }}/etc/init/fake-container-events.conf', # HACK
|
59
|
-
'umount /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }}/proc',
|
60
|
-
'umount /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }}/sys',
|
61
|
-
'umount /mnt/packer-amazon-chroot-volumes/{{ user `ebs_device` }}/dev'
|
58
|
+
CHROOT_COMMANDS = [
|
59
|
+
'apt-get update',
|
60
|
+
'apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y --force-yes --no-upgrade install grub-pc grub-legacy-ec2',
|
61
|
+
'update-grub',
|
62
|
+
'rm -f /etc/init/fake-container-events.conf', # HACK
|
62
63
|
]
|
63
64
|
|
64
65
|
def initialize(config)
|
@@ -68,14 +69,18 @@ module Linecook
|
|
68
69
|
@region = config[:region] || 'us-east-1'
|
69
70
|
@copy_regions = config[:copy_regions] || []
|
70
71
|
@accounts = config[:account_ids] || []
|
71
|
-
@source_ami = config[:source_ami] || find_ami
|
72
72
|
@write_txt = Linecook.config[:packager] && Linecook.config[:packager][:ami] && Linecook.config[:packager][:ami][:update_txt]
|
73
73
|
end
|
74
74
|
|
75
|
-
def package(image)
|
75
|
+
def package(image, directory)
|
76
76
|
@image = image
|
77
|
+
kitchen_config = load_config(directory).send(:data).instance_variable_get(:@data)
|
78
|
+
image_config = kitchen_config[:suites].find{ |x| x[:name] == image.name }
|
79
|
+
if image_config && image_config[:packager]
|
80
|
+
packager = image_config[:packager] || {}
|
81
|
+
end
|
77
82
|
conf_file = Tempfile.new("#{@image.id}-packer.json")
|
78
|
-
config = generate_config
|
83
|
+
config = generate_config(packager)
|
79
84
|
conf_file.write(config)
|
80
85
|
conf_file.close
|
81
86
|
output = []
|
@@ -111,13 +116,12 @@ module Linecook
|
|
111
116
|
# amis = `grep "amazon-ebs,artifact,0,id" packer.log`.chomp.split(',')[5].split('%!(PACKER_COMMA)')
|
112
117
|
# route53 TXT record integration
|
113
118
|
|
114
|
-
def generate_config
|
119
|
+
def generate_config(packager)
|
120
|
+
packager ||= {}
|
115
121
|
config = {
|
116
122
|
variables: {
|
117
123
|
aws_access_key: Linecook.config[:aws][:access_key],
|
118
124
|
aws_secret_key: Linecook.config[:aws][:secret_key],
|
119
|
-
ebs_device: free_device,
|
120
|
-
source_ami: @source_ami,
|
121
125
|
image_name: "linecook-#{@image.id}",
|
122
126
|
source_image_path: @image.path
|
123
127
|
},
|
@@ -127,22 +131,24 @@ module Linecook
|
|
127
131
|
ami_regions: @copy_regions,
|
128
132
|
ami_virtualization_type: virt_type,
|
129
133
|
root_volume_size: @root_size
|
130
|
-
)
|
134
|
+
).deep_merge(packager[:builder] || {})
|
131
135
|
],
|
132
|
-
provisioners: build_provisioner(
|
136
|
+
provisioners: build_provisioner(CHROOT_COMMANDS)
|
133
137
|
}
|
138
|
+
|
139
|
+
unless config[:builders].first[:ami_block_device_mappings].find { |x| x[:device_name] == ROOT_DEVICE_MAP[:device_name] }
|
140
|
+
config[:builders].first[:ami_block_device_mappings].prepend ROOT_DEVICE_MAP
|
141
|
+
end
|
134
142
|
JSON.pretty_generate(config)
|
135
143
|
end
|
136
144
|
|
137
|
-
def build_provisioner(
|
138
|
-
provisioner = [
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
command: cmd
|
145
|
+
def build_provisioner(chroot_commands)
|
146
|
+
provisioner = [
|
147
|
+
{
|
148
|
+
type: 'shell',
|
149
|
+
inline: chroot_commands
|
143
150
|
}
|
144
|
-
|
145
|
-
provisioner
|
151
|
+
]
|
146
152
|
end
|
147
153
|
|
148
154
|
def packer_path
|
@@ -168,39 +174,6 @@ module Linecook
|
|
168
174
|
PACKER_PATH
|
169
175
|
end
|
170
176
|
|
171
|
-
def free_device
|
172
|
-
lock('device_scan')
|
173
|
-
free = nil
|
174
|
-
prefix = device_prefix
|
175
|
-
('f'..'zzz').to_a.each do |suffix|
|
176
|
-
device = "#{prefix}#{suffix}"
|
177
|
-
if free_device?(device)
|
178
|
-
lock(device)
|
179
|
-
at_exit do
|
180
|
-
clear_lock(device)
|
181
|
-
end
|
182
|
-
free = device
|
183
|
-
break
|
184
|
-
end
|
185
|
-
end
|
186
|
-
unlock('device_scan')
|
187
|
-
return free
|
188
|
-
end
|
189
|
-
|
190
|
-
def free_device?(device)
|
191
|
-
!File.exists?("/dev/#{device}") && !File.exists?(lock_path(device))
|
192
|
-
end
|
193
|
-
|
194
|
-
def device_prefix
|
195
|
-
prefixes = ['xvd']
|
196
|
-
`sudo ls -1 /sys/block`.lines.each do |dev| # FIXME
|
197
|
-
prefixes.each do |prefix|
|
198
|
-
return prefix if dev =~ /^#{prefix}/
|
199
|
-
end
|
200
|
-
end
|
201
|
-
return prefixes.first
|
202
|
-
end
|
203
|
-
|
204
177
|
def extract_amis_from_output(output)
|
205
178
|
amis = output.grep(/amazon-chroot,artifact,0,id/).first.chomp.split(',')[5].split('%!(PACKER_COMMA)')
|
206
179
|
amis.each do |info_str|
|
@@ -216,15 +189,5 @@ module Linecook
|
|
216
189
|
@hvm ? 'hvm' : 'paravirtual'
|
217
190
|
end
|
218
191
|
|
219
|
-
def find_ami
|
220
|
-
url = "http://uec-images.ubuntu.com/query/trusty/server/released.current.txt"
|
221
|
-
data = open(url).read.split("\n").map{|l| l.split}.detect do |ary|
|
222
|
-
ary[4] == 'ebs' &&
|
223
|
-
ary[5] == 'amd64' &&
|
224
|
-
ary[6] == @region &&
|
225
|
-
ary.last == virt_type
|
226
|
-
end
|
227
|
-
data[7]
|
228
|
-
end
|
229
192
|
end
|
230
193
|
end
|
@@ -1,3 +1,23 @@
|
|
1
|
+
require 'kitchen'
|
2
|
+
|
3
|
+
def load_config(directory)
|
4
|
+
@config ||= begin
|
5
|
+
Dir.chdir(directory) do
|
6
|
+
Kitchen::Config.new(
|
7
|
+
kitchen_root: Dir.pwd,
|
8
|
+
loader: Kitchen::Loader::YAML.new(
|
9
|
+
project_config: ENV['KITCHEN_YAML'] || File.join(Dir.pwd, '.kitchen.yml'),
|
10
|
+
local_config: ENV['KITCHEN_LOCAL_YAML'],
|
11
|
+
global_config: ENV['KITCHEN_GLOBAL_YAML']
|
12
|
+
)
|
13
|
+
)
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
|
1
21
|
def with_retries(retries, sleep_duration: 5, &block)
|
2
22
|
attempts = 0
|
3
23
|
while attempts < retries
|
data/lib/linecook-gem/version.rb
CHANGED