vagabond 0.2.8 → 0.2.10
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +16 -0
- data/DEVELOP.md +38 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +33 -0
- data/README.md +55 -21
- data/USAGE.md +28 -3
- data/Vagabondfile.sample +47 -0
- data/Vagrantfile +17 -5
- data/bin/vagabond +5 -0
- data/lib/vagabond/Cheffile +7 -0
- data/lib/vagabond/Cheffile.dev +7 -0
- data/lib/vagabond/actions/cluster.rb +22 -8
- data/lib/vagabond/actions/create.rb +7 -4
- data/lib/vagabond/actions/destroy.rb +24 -14
- data/lib/vagabond/actions/freeze.rb +1 -0
- data/lib/vagabond/actions/init.rb +8 -11
- data/lib/vagabond/actions/provision.rb +16 -8
- data/lib/vagabond/actions/rebuild.rb +4 -9
- data/lib/vagabond/actions/ssh.rb +9 -1
- data/lib/vagabond/actions/start.rb +1 -0
- data/lib/vagabond/actions/status.rb +1 -0
- data/lib/vagabond/actions/thaw.rb +1 -0
- data/lib/vagabond/actions/up.rb +22 -7
- data/lib/vagabond/bootstraps/chef_10_compat_config.erb +62 -0
- data/lib/vagabond/bootstraps/no_lazy_load.erb +63 -0
- data/lib/vagabond/bootstraps/server-zero.erb +1 -8
- data/lib/vagabond/bootstraps/server.erb +9 -6
- data/lib/vagabond/constants.rb +1 -6
- data/lib/vagabond/core.rb +209 -0
- data/lib/vagabond/errors.rb +3 -0
- data/lib/vagabond/helpers.rb +11 -99
- data/lib/vagabond/helpers/base.rb +134 -0
- data/lib/vagabond/helpers/callbacks.rb +47 -0
- data/lib/vagabond/helpers/chains.rb +23 -0
- data/lib/vagabond/helpers/commands.rb +49 -0
- data/lib/vagabond/helpers/knife.rb +47 -0
- data/lib/vagabond/helpers/naming.rb +39 -0
- data/lib/vagabond/helpers/server.rb +30 -0
- data/lib/vagabond/internal_configuration.rb +186 -32
- data/lib/vagabond/kitchen.rb +110 -60
- data/lib/vagabond/knife.rb +5 -1
- data/lib/vagabond/layout.rb +1 -0
- data/lib/vagabond/monkey/kitchen_config.rb +1 -0
- data/lib/vagabond/notify_mash.rb +25 -0
- data/lib/vagabond/server.rb +81 -49
- data/lib/vagabond/settings.rb +17 -0
- data/lib/vagabond/spec.rb +125 -99
- data/lib/vagabond/uploader.rb +4 -1
- data/lib/vagabond/uploader/berkshelf.rb +2 -1
- data/lib/vagabond/uploader/knife.rb +3 -9
- data/lib/vagabond/uploader/librarian.rb +2 -5
- data/lib/vagabond/vagabond.rb +77 -93
- data/lib/vagabond/vagabondfile.rb +73 -9
- data/lib/vagabond/version.rb +2 -1
- data/vagabond.gemspec +7 -5
- metadata +58 -81
- data/lib/vagabond/cookbooks/apt/Berksfile +0 -8
- data/lib/vagabond/cookbooks/apt/CHANGELOG.md +0 -97
- data/lib/vagabond/cookbooks/apt/CONTRIBUTING +0 -29
- data/lib/vagabond/cookbooks/apt/LICENSE +0 -201
- data/lib/vagabond/cookbooks/apt/README.md +0 -243
- data/lib/vagabond/cookbooks/apt/TESTING.md +0 -25
- data/lib/vagabond/cookbooks/apt/attributes/default.rb +0 -4
- data/lib/vagabond/cookbooks/apt/files/default/apt-proxy-v2.conf +0 -50
- data/lib/vagabond/cookbooks/apt/metadata.rb +0 -30
- data/lib/vagabond/cookbooks/apt/providers/preference.rb +0 -61
- data/lib/vagabond/cookbooks/apt/providers/repository.rb +0 -132
- data/lib/vagabond/cookbooks/apt/recipes/cacher-client.rb +0 -59
- data/lib/vagabond/cookbooks/apt/recipes/cacher-ng.rb +0 -40
- data/lib/vagabond/cookbooks/apt/recipes/default.rb +0 -68
- data/lib/vagabond/cookbooks/apt/resources/preference.rb +0 -30
- data/lib/vagabond/cookbooks/apt/resources/repository.rb +0 -40
- data/lib/vagabond/cookbooks/apt/templates/debian-6.0/acng.conf.erb +0 -174
- data/lib/vagabond/cookbooks/apt/templates/default/01proxy.erb +0 -2
- data/lib/vagabond/cookbooks/apt/templates/default/acng.conf.erb +0 -276
- data/lib/vagabond/cookbooks/apt/templates/ubuntu-10.04/acng.conf.erb +0 -270
- data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/README.md +0 -1
- data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/files/default/tests/minitest/cacher-ng_test.rb +0 -28
- data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/files/default/tests/minitest/default_test.rb +0 -28
- data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/files/default/tests/minitest/lwrps_test.rb +0 -48
- data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/files/default/tests/minitest/support/helpers.rb +0 -29
- data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/metadata.rb +0 -6
- data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/recipes/cacher-ng.rb +0 -20
- data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/recipes/default.rb +0 -20
- data/lib/vagabond/cookbooks/apt/test/cookbooks/apt_test/recipes/lwrps.rb +0 -66
- data/lib/vagabond/cookbooks/lxc/CHANGELOG.md +0 -37
- data/lib/vagabond/cookbooks/lxc/Gemfile +0 -4
- data/lib/vagabond/cookbooks/lxc/Gemfile.lock +0 -41
- data/lib/vagabond/cookbooks/lxc/README.md +0 -112
- data/lib/vagabond/cookbooks/lxc/attributes/default.rb +0 -26
- data/lib/vagabond/cookbooks/lxc/files/default/knife_lxc +0 -228
- data/lib/vagabond/cookbooks/lxc/files/default/lxc-awesome-ephemeral +0 -499
- data/lib/vagabond/cookbooks/lxc/libraries/lxc.rb +0 -477
- data/lib/vagabond/cookbooks/lxc/libraries/lxc_expanded_resources.rb +0 -40
- data/lib/vagabond/cookbooks/lxc/libraries/lxc_file_config.rb +0 -84
- data/lib/vagabond/cookbooks/lxc/libraries/monkey.rb +0 -51
- data/lib/vagabond/cookbooks/lxc/metadata.rb +0 -12
- data/lib/vagabond/cookbooks/lxc/providers/config.rb +0 -75
- data/lib/vagabond/cookbooks/lxc/providers/container.rb +0 -354
- data/lib/vagabond/cookbooks/lxc/providers/default.rb +0 -57
- data/lib/vagabond/cookbooks/lxc/providers/ephemeral.rb +0 -40
- data/lib/vagabond/cookbooks/lxc/providers/fstab.rb +0 -30
- data/lib/vagabond/cookbooks/lxc/providers/interface.rb +0 -45
- data/lib/vagabond/cookbooks/lxc/providers/service.rb +0 -53
- data/lib/vagabond/cookbooks/lxc/recipes/containers.rb +0 -13
- data/lib/vagabond/cookbooks/lxc/recipes/default.rb +0 -58
- data/lib/vagabond/cookbooks/lxc/recipes/install_dependencies.rb +0 -15
- data/lib/vagabond/cookbooks/lxc/recipes/knife.rb +0 -37
- data/lib/vagabond/cookbooks/lxc/resources/config.rb +0 -19
- data/lib/vagabond/cookbooks/lxc/resources/container.rb +0 -54
- data/lib/vagabond/cookbooks/lxc/resources/default.rb +0 -12
- data/lib/vagabond/cookbooks/lxc/resources/ephemeral.rb +0 -13
- data/lib/vagabond/cookbooks/lxc/resources/fstab.rb +0 -12
- data/lib/vagabond/cookbooks/lxc/resources/interface.rb +0 -13
- data/lib/vagabond/cookbooks/lxc/resources/service.rb +0 -5
- data/lib/vagabond/cookbooks/lxc/templates/default/client.rb.erb +0 -13
- data/lib/vagabond/cookbooks/lxc/templates/default/default-lxc.erb +0 -3
- data/lib/vagabond/cookbooks/lxc/templates/default/file_content.erb +0 -2
- data/lib/vagabond/cookbooks/lxc/templates/default/fstab.erb +0 -5
- data/lib/vagabond/cookbooks/lxc/templates/default/interface.erb +0 -27
- data/lib/vagabond/cookbooks/vagabond/README.md +0 -10
- data/lib/vagabond/cookbooks/vagabond/attributes/default.rb +0 -18
- data/lib/vagabond/cookbooks/vagabond/files/default/lxc-centos +0 -460
- data/lib/vagabond/cookbooks/vagabond/libraries/vagabond.rb +0 -10
- data/lib/vagabond/cookbooks/vagabond/metadata.rb +0 -8
- data/lib/vagabond/cookbooks/vagabond/recipes/default.rb +0 -132
- data/lib/vagabond/cookbooks/vagabond/recipes/zero.rb +0 -9
- data/lib/vagabond/helpers/cheffile_loader.rb +0 -20
- data/vagabond-0.2.6.gem +0 -0
@@ -1,477 +0,0 @@
|
|
1
|
-
require 'mixlib/shellout'
|
2
|
-
require 'pathname'
|
3
|
-
require 'tmpdir'
|
4
|
-
|
5
|
-
class Lxc
|
6
|
-
class CommandFailed < StandardError
|
7
|
-
end
|
8
|
-
|
9
|
-
module Helpers
|
10
|
-
|
11
|
-
# Simple helper to shell out
|
12
|
-
def run_command(cmd, args={})
|
13
|
-
retries = args[:allow_failure_retry].to_i
|
14
|
-
begin
|
15
|
-
shlout = Mixlib::ShellOut.new(cmd,
|
16
|
-
:logger => defined?(Chef::Log) ? Chef::Log.logger : log,
|
17
|
-
:live_stream => args[:livestream] ? STDOUT : nil,
|
18
|
-
:timeout => args[:timeout] || 1200,
|
19
|
-
:environment => {'HOME' => detect_home}
|
20
|
-
)
|
21
|
-
shlout.run_command
|
22
|
-
shlout.error!
|
23
|
-
shlout
|
24
|
-
rescue Mixlib::ShellOut::ShellCommandFailed, CommandFailed, Mixlib::ShellOut::CommandTimeout
|
25
|
-
if(retries > 0)
|
26
|
-
log.warn "LXC run command failed: #{cmd}"
|
27
|
-
log.warn "Retrying command. #{args[:allow_failure_retry].to_i - retries} of #{args[:allow_failure_retry].to_i} retries remain"
|
28
|
-
sleep(0.3)
|
29
|
-
retries -= 1
|
30
|
-
retry
|
31
|
-
elsif(args[:allow_failure])
|
32
|
-
true
|
33
|
-
else
|
34
|
-
raise
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def command(*args)
|
40
|
-
run_command(*args)
|
41
|
-
end
|
42
|
-
|
43
|
-
def log
|
44
|
-
if(defined?(Chef::Log))
|
45
|
-
Chef::Log
|
46
|
-
else
|
47
|
-
unless(@logger)
|
48
|
-
require 'logger'
|
49
|
-
@logger = Logger.new('/dev/null')
|
50
|
-
end
|
51
|
-
@logger
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
# Detect HOME environment variable. If not an acceptable
|
56
|
-
# value, set to /root or /tmp
|
57
|
-
def detect_home(set_if_missing=false)
|
58
|
-
if(ENV['HOME'] && Pathname.new(ENV['HOME']).absolute?)
|
59
|
-
ENV['HOME']
|
60
|
-
else
|
61
|
-
home = File.directory?('/root') && File.writable?('/root') ? '/root' : '/tmp'
|
62
|
-
if(set_if_missing)
|
63
|
-
ENV['HOME'] = home
|
64
|
-
end
|
65
|
-
home
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
# Pathname#join does not act like File#join when joining paths that
|
71
|
-
# begin with '/', and that's dumb. So we'll make our own Pathname,
|
72
|
-
# with a #join that uses File
|
73
|
-
class Pathname < ::Pathname
|
74
|
-
def join(*args)
|
75
|
-
self.class.new(::File.join(self.to_path, *args))
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
include Helpers
|
80
|
-
|
81
|
-
attr_reader :name, :base_path, :lease_file, :preferred_device
|
82
|
-
|
83
|
-
class << self
|
84
|
-
|
85
|
-
include Helpers
|
86
|
-
|
87
|
-
attr_accessor :use_sudo
|
88
|
-
attr_accessor :base_path
|
89
|
-
|
90
|
-
def base_path
|
91
|
-
@base_path || '/var/lib/lxc'
|
92
|
-
end
|
93
|
-
|
94
|
-
def sudo
|
95
|
-
case use_sudo
|
96
|
-
when TrueClass
|
97
|
-
'sudo '
|
98
|
-
when String
|
99
|
-
"#{use_sudo} "
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
# List running containers
|
104
|
-
def running
|
105
|
-
full_list[:running]
|
106
|
-
end
|
107
|
-
|
108
|
-
# List stopped containers
|
109
|
-
def stopped
|
110
|
-
full_list[:stopped]
|
111
|
-
end
|
112
|
-
|
113
|
-
# List frozen containers
|
114
|
-
def frozen
|
115
|
-
full_list[:frozen]
|
116
|
-
end
|
117
|
-
|
118
|
-
# name:: name of container
|
119
|
-
# Returns if container exists
|
120
|
-
def exists?(name)
|
121
|
-
list.include?(name)
|
122
|
-
end
|
123
|
-
|
124
|
-
# List of containers
|
125
|
-
def list
|
126
|
-
Dir.glob(File.join(base_path, '*')).map do |item|
|
127
|
-
if(File.directory?(item) && File.exists?(File.join(item, 'config')))
|
128
|
-
File.basename(item)
|
129
|
-
end
|
130
|
-
end.compact
|
131
|
-
end
|
132
|
-
|
133
|
-
# name:: Name of container
|
134
|
-
# Returns information about given container
|
135
|
-
def info(name)
|
136
|
-
res = {:state => nil, :pid => nil}
|
137
|
-
info = run_command("#{sudo}lxc-info -n #{name}").stdout.split("\n")
|
138
|
-
if(info.first)
|
139
|
-
parts = info.first.split(' ')
|
140
|
-
res[:state] = parts.last.downcase.to_sym
|
141
|
-
parts = info.last.split(' ')
|
142
|
-
res[:pid] = parts.last.to_i
|
143
|
-
res
|
144
|
-
else
|
145
|
-
res[:state] = :unknown
|
146
|
-
res[:pid] = -1
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
# Return full container information list
|
151
|
-
def full_list
|
152
|
-
res = {}
|
153
|
-
list.each do |item|
|
154
|
-
item_info = info(item)
|
155
|
-
res[item_info[:state]] ||= []
|
156
|
-
res[item_info[:state]] << item
|
157
|
-
end
|
158
|
-
res
|
159
|
-
end
|
160
|
-
|
161
|
-
# ip:: IP address
|
162
|
-
# Returns if IP address is alive
|
163
|
-
def connection_alive?(ip)
|
164
|
-
%x{ping -c 1 -W 1 #{ip}}
|
165
|
-
$?.exitstatus == 0
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
# name:: name of container
|
170
|
-
# args:: Argument hash
|
171
|
-
# - :base_path -> path to container directory
|
172
|
-
# - :dnsmasq_lease_file -> path to lease file
|
173
|
-
# - :net_device -> network device to use within container for ssh connection
|
174
|
-
def initialize(name, args={})
|
175
|
-
@name = name
|
176
|
-
@base_path = args[:base_path] || self.class.base_path
|
177
|
-
@lease_file = args[:dnsmasq_lease_file] || '/var/lib/misc/dnsmasq.leases'
|
178
|
-
@preferred_device = args[:net_device]
|
179
|
-
end
|
180
|
-
|
181
|
-
# Returns if container exists
|
182
|
-
def exists?
|
183
|
-
self.class.exists?(name)
|
184
|
-
end
|
185
|
-
|
186
|
-
# Returns if container is running
|
187
|
-
def running?
|
188
|
-
self.class.info(name)[:state] == :running
|
189
|
-
end
|
190
|
-
|
191
|
-
# Returns if container is stopped
|
192
|
-
def stopped?
|
193
|
-
self.class.info(name)[:state] == :stopped
|
194
|
-
end
|
195
|
-
|
196
|
-
# Returns if container is frozen
|
197
|
-
def frozen?
|
198
|
-
self.class.info(name)[:state] == :frozen
|
199
|
-
end
|
200
|
-
|
201
|
-
# retries:: Number of discovery attempt (3 second sleep intervals)
|
202
|
-
# Returns container IP
|
203
|
-
def container_ip(retries=0, raise_on_fail=false)
|
204
|
-
(retries.to_i + 1).times do
|
205
|
-
ip = proc_detected_address || hw_detected_address || leased_address || lxc_stored_address
|
206
|
-
if(ip.is_a?(Array))
|
207
|
-
# Filter any found loopbacks
|
208
|
-
ip.delete_if{|info| info[:device].start_with?('lo') }
|
209
|
-
ip = ip.detect do |info|
|
210
|
-
if(@preferred_device)
|
211
|
-
info[:device] == @preferred_device
|
212
|
-
else
|
213
|
-
true
|
214
|
-
end
|
215
|
-
end
|
216
|
-
ip = ip[:address] if ip
|
217
|
-
end
|
218
|
-
return ip if ip && self.class.connection_alive?(ip)
|
219
|
-
log.warn "LXC IP discovery: Failed to detect live IP"
|
220
|
-
sleep(3) if retries > 0
|
221
|
-
end
|
222
|
-
raise "Failed to detect live IP address for container: #{name}" if raise_on_fail
|
223
|
-
end
|
224
|
-
|
225
|
-
# Container address via lxc config file
|
226
|
-
def lxc_stored_address
|
227
|
-
if(File.exists?(container_config))
|
228
|
-
ip = File.readlines(container_config).detect{|line|
|
229
|
-
line.include?('ipv4')
|
230
|
-
}.to_s.split('=').last.to_s.strip
|
231
|
-
if(ip.to_s.empty?)
|
232
|
-
nil
|
233
|
-
else
|
234
|
-
log.info "LXC Discovery: Found container address via storage: #{ip}"
|
235
|
-
ip
|
236
|
-
end
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
# Container address via dnsmasq lease
|
241
|
-
def leased_address
|
242
|
-
ip = nil
|
243
|
-
if(File.exists?(@lease_file))
|
244
|
-
leases = File.readlines(@lease_file).map{|line| line.split(' ')}
|
245
|
-
leases.each do |lease|
|
246
|
-
if(lease.include?(name))
|
247
|
-
ip = lease[2]
|
248
|
-
end
|
249
|
-
end
|
250
|
-
end
|
251
|
-
if(ip.to_s.empty?)
|
252
|
-
nil
|
253
|
-
else
|
254
|
-
log.info "LXC Discovery: Found container address via DHCP lease: #{ip}"
|
255
|
-
ip
|
256
|
-
end
|
257
|
-
end
|
258
|
-
|
259
|
-
def hw_detected_address
|
260
|
-
if(container_config.readable?)
|
261
|
-
hw = File.readlines(container_config).detect{|line|
|
262
|
-
line.include?('hwaddr')
|
263
|
-
}.to_s.split('=').last.to_s.downcase
|
264
|
-
if(File.exists?(container_config) && !hw.empty?)
|
265
|
-
running? # need to do a list!
|
266
|
-
ip = File.readlines('/proc/net/arp').detect{|line|
|
267
|
-
line.downcase.include?(hw)
|
268
|
-
}.to_s.split(' ').first.to_s.strip
|
269
|
-
if(ip.to_s.empty?)
|
270
|
-
nil
|
271
|
-
else
|
272
|
-
log.info "LXC Discovery: Found container address via HW addr: #{ip}"
|
273
|
-
ip
|
274
|
-
end
|
275
|
-
end
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
def proc_detected_address(base='/run/netns')
|
280
|
-
if(pid != -1)
|
281
|
-
Dir.mktmpdir do |t_dir|
|
282
|
-
name = File.basename(t_dir)
|
283
|
-
path = File.join(base, name)
|
284
|
-
system("#{sudo}mkdir -p #{base}")
|
285
|
-
system("#{sudo}ln -s /proc/#{pid}/ns/net #{path}")
|
286
|
-
res = %x{#{sudo}ip netns exec #{name} ip -4 addr show scope global | grep inet}
|
287
|
-
system("#{sudo}rm -f #{path}")
|
288
|
-
ips = res.split("\n").map do |line|
|
289
|
-
parts = line.split(' ')
|
290
|
-
{:address => parts[1].to_s.sub(%r{/.+$}, ''), :device => parts.last}
|
291
|
-
end
|
292
|
-
ips.empty? ? nil : ips
|
293
|
-
end
|
294
|
-
end
|
295
|
-
end
|
296
|
-
|
297
|
-
def sudo
|
298
|
-
self.class.sudo
|
299
|
-
end
|
300
|
-
|
301
|
-
# Full path to container
|
302
|
-
def container_path
|
303
|
-
Pathname.new(@base_path).join(name)
|
304
|
-
end
|
305
|
-
alias_method :path, :container_path
|
306
|
-
|
307
|
-
# Full path to container configuration file
|
308
|
-
def container_config
|
309
|
-
container_path.join('config')
|
310
|
-
end
|
311
|
-
alias_method :config, :container_config
|
312
|
-
|
313
|
-
def container_rootfs
|
314
|
-
container_path.join('rootfs')
|
315
|
-
end
|
316
|
-
alias_method :rootfs, :container_rootfs
|
317
|
-
|
318
|
-
def expand_path(path)
|
319
|
-
container_rootfs.join(path)
|
320
|
-
end
|
321
|
-
|
322
|
-
def state
|
323
|
-
self.class.info(name)[:state]
|
324
|
-
end
|
325
|
-
|
326
|
-
def pid
|
327
|
-
self.class.info(name)[:pid]
|
328
|
-
end
|
329
|
-
|
330
|
-
# Start the container
|
331
|
-
def start(*args)
|
332
|
-
if(args.include?(:no_daemon))
|
333
|
-
run_command("#{sudo}lxc-start -n #{name}")
|
334
|
-
else
|
335
|
-
run_command("#{sudo}lxc-start -n #{name} -d")
|
336
|
-
wait_for_state(:running)
|
337
|
-
end
|
338
|
-
end
|
339
|
-
|
340
|
-
# Stop the container
|
341
|
-
def stop
|
342
|
-
run_command("#{sudo}lxc-stop -n #{name}", :allow_failure_retry => 3)
|
343
|
-
wait_for_state(:stopped)
|
344
|
-
end
|
345
|
-
|
346
|
-
# Freeze the container
|
347
|
-
def freeze
|
348
|
-
run_command("#{sudo}lxc-freeze -n #{name}")
|
349
|
-
wait_for_state(:frozen)
|
350
|
-
end
|
351
|
-
|
352
|
-
# Unfreeze the container
|
353
|
-
def unfreeze
|
354
|
-
run_command("#{sudo}lxc-unfreeze -n #{name}")
|
355
|
-
wait_for_state(:running)
|
356
|
-
end
|
357
|
-
|
358
|
-
# Shutdown the container
|
359
|
-
def shutdown
|
360
|
-
run_command("#{sudo}lxc-shutdown -n #{name}")
|
361
|
-
wait_for_state(:stopped, :timeout => 120)
|
362
|
-
# This block is for fedora/centos/anyone else that does not like lxc-shutdown
|
363
|
-
if(running?)
|
364
|
-
container_command('shutdown -h now')
|
365
|
-
wait_for_state(:stopped, :timeout => 120)
|
366
|
-
# If still running here, something is wrong
|
367
|
-
if(running?)
|
368
|
-
raise "Failed to shutdown container: #{name}"
|
369
|
-
end
|
370
|
-
end
|
371
|
-
end
|
372
|
-
|
373
|
-
def direct_container_command(command, args={})
|
374
|
-
com = "#{sudo}ssh root@#{args[:ip] || container_ip} -i /opt/hw-lxc-config/id_rsa -oStrictHostKeyChecking=no '#{command}'"
|
375
|
-
begin
|
376
|
-
cmd = Mixlib::ShellOut.new(com,
|
377
|
-
:live_stream => args[:live_stream],
|
378
|
-
:timeout => args[:timeout] || 1200
|
379
|
-
)
|
380
|
-
cmd.run_command
|
381
|
-
cmd.error!
|
382
|
-
true
|
383
|
-
rescue
|
384
|
-
raise if args[:raise_on_failure]
|
385
|
-
false
|
386
|
-
end
|
387
|
-
end
|
388
|
-
alias_method :knife_container, :direct_container_command
|
389
|
-
|
390
|
-
# Simple helper to shell out
|
391
|
-
def run_command(cmd, args={})
|
392
|
-
retries = args[:allow_failure_retry].to_i
|
393
|
-
begin
|
394
|
-
shlout = Mixlib::ShellOut.new(cmd,
|
395
|
-
:logger => defined?(Chef::Log) ? Chef::Log.logger : log,
|
396
|
-
:live_stream => args[:livestream] ? nil : STDOUT,
|
397
|
-
:timeout => args[:timeout] || 1200,
|
398
|
-
:environment => {'HOME' => detect_home}
|
399
|
-
)
|
400
|
-
shlout.run_command
|
401
|
-
shlout.error!
|
402
|
-
shlout
|
403
|
-
rescue Mixlib::ShellOut::ShellCommandFailed, CommandFailed, Mixlib::ShellOut::CommandTimeout
|
404
|
-
if(retries > 0)
|
405
|
-
log.warn "LXC run command failed: #{cmd}"
|
406
|
-
log.warn "Retrying command. #{args[:allow_failure_retry].to_i - retries} of #{args[:allow_failure_retry].to_i} retries remain"
|
407
|
-
sleep(0.3)
|
408
|
-
retries -= 1
|
409
|
-
retry
|
410
|
-
elsif(args[:allow_failure])
|
411
|
-
true
|
412
|
-
else
|
413
|
-
raise
|
414
|
-
end
|
415
|
-
end
|
416
|
-
end
|
417
|
-
|
418
|
-
def wait_for_state(desired_state, args={})
|
419
|
-
args[:sleep_interval] ||= 1.0
|
420
|
-
wait_total = 0.0
|
421
|
-
until(state == desired_state.to_sym || (args[:timeout].to_i > 0 && wait_total.to_i > args[:timeout].to_i))
|
422
|
-
sleep(args[:sleep_interval])
|
423
|
-
wait_total += args[:sleep_interval]
|
424
|
-
end
|
425
|
-
end
|
426
|
-
|
427
|
-
# Detect HOME environment variable. If not an acceptable
|
428
|
-
# value, set to /root or /tmp
|
429
|
-
def detect_home(set_if_missing=false)
|
430
|
-
if(ENV['HOME'] && Pathname.new(ENV['HOME']).absolute?)
|
431
|
-
ENV['HOME']
|
432
|
-
else
|
433
|
-
home = File.directory?('/root') && File.writable?('/root') ? '/root' : '/tmp'
|
434
|
-
if(set_if_missing)
|
435
|
-
ENV['HOME'] = home
|
436
|
-
end
|
437
|
-
home
|
438
|
-
end
|
439
|
-
end
|
440
|
-
|
441
|
-
# cmd:: Shell command string
|
442
|
-
# retries:: Number of retry attempts (1 second sleep interval)
|
443
|
-
# Runs command in container via ssh
|
444
|
-
def container_command(cmd, retries=1)
|
445
|
-
begin
|
446
|
-
detect_home(true)
|
447
|
-
direct_container_command(cmd,
|
448
|
-
:ip => container_ip(5),
|
449
|
-
:live_stream => STDOUT,
|
450
|
-
:raise_on_failure => true
|
451
|
-
)
|
452
|
-
rescue => e
|
453
|
-
if(retries.to_i > 0)
|
454
|
-
log.info "Encountered error running container command (#{cmd}): #{e}"
|
455
|
-
log.info "Retrying command..."
|
456
|
-
retries = retries.to_i - 1
|
457
|
-
sleep(1)
|
458
|
-
retry
|
459
|
-
else
|
460
|
-
raise e
|
461
|
-
end
|
462
|
-
end
|
463
|
-
end
|
464
|
-
|
465
|
-
def log
|
466
|
-
if(defined?(Chef::Log))
|
467
|
-
Chef::Log
|
468
|
-
else
|
469
|
-
unless(@logger)
|
470
|
-
require 'logger'
|
471
|
-
@logger = Logger.new('/dev/null')
|
472
|
-
end
|
473
|
-
@logger
|
474
|
-
end
|
475
|
-
end
|
476
|
-
|
477
|
-
end
|