This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm_driver.rb
74 lines (62 loc) · 2.01 KB
/
vm_driver.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require 'tempfile'
class VmDriver
attr_accessor :ssh_opts
def format_chef_ssh cmd
"ssh #{@ssh_opts} #{CHEF_USER}@#{ip} \"#{cmd}\""
end
def run cmd
begin
raise "ssh #{CHEF_USER} : #{cmd} failed. Aborting..." unless system format_chef_ssh(cmd)
rescue
raise "ssh #{CHEF_USER} : #{cmd} failed. Aborting..."
end
end
def run_fail cmd
begin
system format_chef_ssh(cmd)
raise "ssh #{CHEF_USER} : #{cmd} should fail. Aborting..."
rescue
end
end
def capture cmd
result = `#{format_chef_ssh(cmd)}`
raise "ssh capture #{CHEF_USER} : #{cmd} failed. Aborting..." unless $?.exitstatus == 0
result
end
def run_chef
if ENV["CHEF_LOCAL"]
exec_local "../../master-chef/runtime/chef_local.rb #{ip}"
else
chef_cmd = "/opt/master-chef/bin/master-chef.sh"
prefix = ""
prefix = "http_proxy=http://#{ENV["PROXY_IP"]}:3128 https_proxy=http://#{ENV["PROXY_IP"]}:3128" if ENV["PROXY_IP"]
prefix = "http_proxy=#{ENV["PROXY"]} https_proxy=#{ENV["PROXY"]}" if ENV["PROXY"]
run "#{prefix} #{chef_cmd}"
end
check_last_chef_run
end
def check_last_chef_run
last_chef_run = "/opt/master-chef/var/last/log"
log = capture "sudo cat #{last_chef_run}"
raise "Not a chef log" unless log.match /INFO: \*\*\* Chef (.*) \*\*\*/
[
/WARN:/,
/Overriding duplicate/
].each do |x|
raise "Error : pattern #{x} found in log" if log.match(x)
end
end
def upload_file from, to
exec_local "scp #{@ssh_opts} #{from} #{CHEF_USER}@#{ip}:#{to}"
end
def upload_json json
content = File.read(File.join(File.dirname(__FILE__), "json", json))
content = content.gsub(/http:\/\/github.com\/kitchenware\/master-chef.git/, ENV["MASTER_CHEF_URL"]) if ENV["MASTER_CHEF_URL"]
tempfile = Tempfile.new 'json'
tempfile.write content
tempfile.close
upload_file tempfile.path, "/tmp/local.json"
json_path = "/opt/master-chef/etc"
self.run "sudo mv /tmp/local.json #{json_path}/local.json"
end
end