-
Notifications
You must be signed in to change notification settings - Fork 9
/
kitchen-instance-ctl
executable file
·123 lines (114 loc) · 4.33 KB
/
kitchen-instance-ctl
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/opt/chefdk/embedded/bin/ruby
require 'open3'
require 'yaml'
require 'aws-sdk'
require 'thor'
class KitchenInstanceCTL < Thor
no_commands do
def get_instances(instance_regex = nil)
stdout, stderr, status = Open3.capture3('kitchen', 'diagnose', '--instances', instance_regex.to_s)
unless status.success?
puts "Error: No instances for regex '#{instance_regex}', try running 'kitchen list' to see what instances are available."
exit 1
end
output = YAML.load(stdout)
output['instances']
end
def ctl_instance(task = nil, name = nil, data = nil)
if data['state_file'].empty?
puts "Kitchen instance #{name} is not created"
else
kitchen_root = data['driver']['kitchen_root']
kitchen_instance_file = File.join(kitchen_root, '.kitchen', "#{name}.yml")
case data['driver']['name']
when 'ec2'
region = data['driver']['region']
Aws.config.update({ region: region }) if region
i = Aws::EC2::Instance.new(data['state_file']['server_id'])
if i.exists?
case task
when 'status'
puts "EC2 instance #{i.id} in region #{region} is #{i.state.name}"
when 'start'
puts "Starting EC2 instance #{i.id}"
i.start
i.wait_until_running
update_kitchen_instance_file_hostname(kitchen_instance_file, i)
when 'stop'
puts "Stopping EC2 instance #{i.id}"
i.stop
i.wait_until_stopped
end
else
puts "EC2 instance #{i.id} does not exist in the #{region} region."
end
when 'vagrant'
p = File.join(kitchen_root, %w(.kitchen kitchen-vagrant), "kitchen-#{File.basename(kitchen_root)}-#{name}")
puts "Getting status of Vagrant instance at path #{p}"
case task
when 'status'
Dir.chdir(p) do
system('vagrant status')
end
when 'start'
puts "Starting Vagrant instance at path #{p}"
port = ''
Dir.chdir(p) do
system('vagrant up')
port = `vagrant port --guest 22`
end
update_kitchen_instance_file_port(kitchen_instance_file, port.to_i)
when 'stop'
puts "Stopping Vagrant instance at path #{p}"
Dir.chdir(p) do
system('vagrant halt --force')
end
end
end
end
end
def update_kitchen_instance_file_hostname(kitchen_instance_file = nil, i = nil)
if i.public_dns_name.empty?
puts "ERROR: Public DNS Name is not set for EC2 instance #{i.id}. Make sure the instance exists and is running."
puts "Instance #{i.id} is #{i.state.name}"
exit 1
end
puts "Updating the hostname in #{kitchen_instance_file} to #{i.public_dns_name}"
kitchen_instance = YAML.load(IO.read(kitchen_instance_file))
kitchen_instance['hostname'] = i.public_dns_name
IO.write(kitchen_instance_file, kitchen_instance.to_yaml)
end
def update_kitchen_instance_file_port(kitchen_instance_file, port)
if port == 0
puts 'ERROR: Port is not set for the Vagrant instance. Make sure the instance exists and is running.'
exit 1
end
puts "Updating the port in #{kitchen_instance_file} to '#{port}'"
kitchen_instance = YAML.load(IO.read(kitchen_instance_file))
kitchen_instance['port'] = port.to_s
IO.write(kitchen_instance_file, kitchen_instance.to_yaml)
end
end
desc 'status [INSTANCE|REGEXP]', 'Print instance state'
def status(instance_regex = nil)
instances = get_instances(instance_regex)
instances.each do |instance_name, instance_data|
ctl_instance('status', instance_name, instance_data)
end
end
desc 'start [INSTANCE|REGEXP]', 'Start instance'
def start(instance_regex = nil)
instances = get_instances(instance_regex)
instances.each do |instance_name, instance_data|
ctl_instance('start', instance_name, instance_data)
end
end
desc 'stop [INSTANCE|REGEXP]', 'Stop instance'
def stop(instance_regex = nil)
instances = get_instances(instance_regex)
instances.each do |instance_name, instance_data|
ctl_instance('stop', instance_name, instance_data)
end
end
end
KitchenInstanceCTL.start(ARGV)