knife-softlayer 0.4.5 → 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +19 -19
  3. data/.travis.yml +20 -20
  4. data/CHANGELOG.md +19 -19
  5. data/CONTRIBUTING.md +31 -31
  6. data/CONTRIBUTORS.md +2 -2
  7. data/Gemfile +4 -4
  8. data/LICENSE.txt +72 -72
  9. data/README.md +83 -83
  10. data/Rakefile +37 -37
  11. data/docs/cla-corporate.md +133 -133
  12. data/docs/cla-individual.md +84 -84
  13. data/examples/datacenter.md +94 -94
  14. data/examples/flavor.md +58 -58
  15. data/examples/global_ip.md +22 -22
  16. data/examples/image.md +34 -34
  17. data/examples/key_pair.md +56 -56
  18. data/examples/server.md +82 -82
  19. data/examples/vlan.md +85 -85
  20. data/knife-softlayer.gemspec +39 -39
  21. data/lib/chef/knife/flavor/base.rb +52 -52
  22. data/lib/chef/knife/softlayer.rb +14 -14
  23. data/lib/chef/knife/softlayer_base.rb +112 -112
  24. data/lib/chef/knife/softlayer_datacenter_list.rb +28 -28
  25. data/lib/chef/knife/softlayer_datacenter_show.rb +45 -45
  26. data/lib/chef/knife/softlayer_delete.rb +6 -6
  27. data/lib/chef/knife/softlayer_flavor_list.rb +53 -53
  28. data/lib/chef/knife/softlayer_global_ip_list.rb +34 -34
  29. data/lib/chef/knife/softlayer_image_list.rb +26 -26
  30. data/lib/chef/knife/softlayer_key_pair_create.rb +37 -37
  31. data/lib/chef/knife/softlayer_key_pair_list.rb +28 -28
  32. data/lib/chef/knife/softlayer_list.rb +6 -6
  33. data/lib/chef/knife/softlayer_server_create.rb +462 -458
  34. data/lib/chef/knife/softlayer_server_destroy.rb +146 -146
  35. data/lib/chef/knife/softlayer_server_list.rb +27 -27
  36. data/lib/chef/knife/softlayer_server_relaunch.rb +208 -208
  37. data/lib/chef/knife/softlayer_vlan_create.rb +37 -37
  38. data/lib/chef/knife/softlayer_vlan_list.rb +28 -28
  39. data/lib/chef/knife/softlayer_vlan_show.rb +40 -40
  40. data/lib/knife-softlayer/version.rb +12 -12
  41. data/spec/spec_helper.rb +21 -21
  42. data/spec/unit/softlayer_base_spec.rb +26 -26
  43. data/spec/unit/softlayer_server_create_spec.rb +105 -105
  44. data/spec/unit/softlayer_server_destroy_spec.rb +47 -47
  45. metadata +27 -27
@@ -1,146 +1,146 @@
1
- #
2
- # Author:: Matt Eldridge (<matt.eldridge@us.ibm.com>)
3
- # © Copyright IBM Corporation 2014.
4
- #
5
- # LICENSE: Apache 2.0 (http://www.apache.org/licenses/)
6
- #
7
-
8
- require 'chef/knife/softlayer_base'
9
- require 'chef/search/query'
10
- require 'chef/api_client'
11
-
12
- class Chef
13
- class Knife
14
- class SoftlayerServerDestroy < Knife
15
-
16
- attr_accessor :node
17
- attr_accessor :cci
18
-
19
- include Knife::SoftlayerBase
20
-
21
- banner 'knife softlayer server destroy (options)'
22
-
23
- option :chef_node_name,
24
- :short => "-N NAME",
25
- :long => "--node-name NAME",
26
- :description => "The name of the node to be destroyed."
27
-
28
- option :ip_address,
29
- :long => "--ip-address ADDRESS",
30
- :short => "-I",
31
- :description => "Find the VM and node to destroy by its public IP address."
32
-
33
- ##
34
- # Run the procedure to destroy a SoftLayer VM and clean up its Chef node and client.
35
- # @return [nil]
36
- def run
37
-
38
- $stdout.sync = true
39
-
40
- puts ui.color("Decommissioning SoftLayer VM, this may take a few minutes.", :green)
41
- connection.servers.each do |server|
42
- if config[:ip_address]
43
- if server.public_ip_address == config[:ip_address]
44
- @instance = server
45
- break
46
- end
47
- elsif config[:chef_node_name]
48
- if server.name == config[:chef_node_name]
49
- config[:ip_address] = server.public_ip_address
50
- @instance = server
51
- break
52
- end
53
- elsif arg = name_args[0]
54
- if arg =~ /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/ # ipv4
55
- if server.public_ip_address == arg
56
- @instance = server
57
- break
58
- end
59
- elsif arg =~ /^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$/ # ipv6
60
- if server.public_ip_address == arg
61
- @instance = server
62
- break
63
- end
64
- else
65
- if server.name == arg
66
- config[:ip_address] = server.public_ip_address
67
- @instance = server
68
- break
69
- end
70
- end
71
- end
72
- end
73
- @instance.nil? and raise "#{ui.color('VM instance with IP: ' + (config[:ip_address].to_s) +' not found!', :red)}"
74
- @chef = Chef::Search::Query.new
75
- @chef.search('node', "name:#{@instance.name}") do |node|
76
- begin
77
- @node = node
78
- rescue
79
- end
80
- end
81
-
82
- begin
83
- if @node
84
- begin
85
- destroy_item(Chef::Node, @node.name, "node")
86
- puts ui.color("Chef node successfully deleted.", :green)
87
- rescue Exception => e
88
- err_msg ui.color("ERROR DELETING CHEF NODE", :red)
89
- err_msg ui.color(e.message, :yellow)
90
- err_msg ui.color(e.backtrace.join("\n"), :yellow)
91
- end
92
-
93
- begin
94
- destroy_item(Chef::ApiClient, @node.name, "client")
95
- puts ui.color("Chef client successfully deleted.", :green)
96
- rescue Exception => e
97
- err_msg ui.color("ERROR DELETING CHEF CLIENT", :red)
98
- err_msg ui.color(e.message, :yellow)
99
- err_msg ui.color(e.backtrace.join("\n"), :yellow)
100
- end
101
- else
102
- "#{ui.color('Chef node: ' + config[:chef_node_name] +' not found! will destroy instance.', :red)}"
103
- end
104
-
105
- begin
106
- @instance.destroy
107
- puts ui.color("SoftLayer VM successfully deleted. You are no longer being billed for this instance.", :green)
108
- rescue Exception => e
109
- err_msg ui.color("ERROR DELETING SOFTLAYER VM. IT'S POSSIBLE YOU ARE STILL BEING BILLED FOR THIS INSTANCE. PLEASE CONTACT SUPPORT FOR FURTHER ASSISTANCE", :red)
110
- err_msg ui.color(e.message, :yellow)
111
- err_msg ui.color(e.backtrace.join("\n"), :yellow)
112
- end
113
- ensure
114
- unless err_msg.empty?
115
- err_msg.each do |msg|
116
- puts msg
117
- end
118
- end
119
- end
120
-
121
- end
122
-
123
- # @param [Chef::*] klass
124
- # @param [String] name
125
- # @param [String] type_name
126
- # @return [nil]
127
- def destroy_item(klass, name, type_name)
128
- begin
129
- object = klass.load(name)
130
- object.destroy
131
- ui.warn("Deleted #{type_name} #{name}")
132
- rescue Net::HTTPServerException
133
- ui.warn("Could not find a #{type_name} named #{name} to delete!")
134
- end
135
- end
136
-
137
- def err_msg(msg=nil)
138
- @msgs ||= []
139
- @msgs.push(msg).compact
140
- end
141
-
142
- end
143
- end
144
- end
145
-
146
-
1
+ #
2
+ # Author:: Matt Eldridge (<matt.eldridge@us.ibm.com>)
3
+ # © Copyright IBM Corporation 2014.
4
+ #
5
+ # LICENSE: Apache 2.0 (http://www.apache.org/licenses/)
6
+ #
7
+
8
+ require 'chef/knife/softlayer_base'
9
+ require 'chef/search/query'
10
+ require 'chef/api_client'
11
+
12
+ class Chef
13
+ class Knife
14
+ class SoftlayerServerDestroy < Knife
15
+
16
+ attr_accessor :node
17
+ attr_accessor :cci
18
+
19
+ include Knife::SoftlayerBase
20
+
21
+ banner 'knife softlayer server destroy (options)'
22
+
23
+ option :chef_node_name,
24
+ :short => "-N NAME",
25
+ :long => "--node-name NAME",
26
+ :description => "The name of the node to be destroyed."
27
+
28
+ option :ip_address,
29
+ :long => "--ip-address ADDRESS",
30
+ :short => "-I",
31
+ :description => "Find the VM and node to destroy by its public IP address."
32
+
33
+ ##
34
+ # Run the procedure to destroy a SoftLayer VM and clean up its Chef node and client.
35
+ # @return [nil]
36
+ def run
37
+
38
+ $stdout.sync = true
39
+
40
+ puts ui.color("Decommissioning SoftLayer VM, this may take a few minutes.", :green)
41
+ connection.servers.each do |server|
42
+ if config[:ip_address]
43
+ if server.public_ip_address == config[:ip_address]
44
+ @instance = server
45
+ break
46
+ end
47
+ elsif config[:chef_node_name]
48
+ if server.name == config[:chef_node_name]
49
+ config[:ip_address] = server.public_ip_address
50
+ @instance = server
51
+ break
52
+ end
53
+ elsif arg = name_args[0]
54
+ if arg =~ /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/ # ipv4
55
+ if server.public_ip_address == arg
56
+ @instance = server
57
+ break
58
+ end
59
+ elsif arg =~ /^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$/ # ipv6
60
+ if server.public_ip_address == arg
61
+ @instance = server
62
+ break
63
+ end
64
+ else
65
+ if server.name == arg
66
+ config[:ip_address] = server.public_ip_address
67
+ @instance = server
68
+ break
69
+ end
70
+ end
71
+ end
72
+ end
73
+ @instance.nil? and raise "#{ui.color('VM instance with IP: ' + (config[:ip_address].to_s) +' not found!', :red)}"
74
+ @chef = Chef::Search::Query.new
75
+ @chef.search('node', "name:#{@instance.name}") do |node|
76
+ begin
77
+ @node = node
78
+ rescue
79
+ end
80
+ end
81
+
82
+ begin
83
+ if @node
84
+ begin
85
+ destroy_item(Chef::Node, @node.name, "node")
86
+ puts ui.color("Chef node successfully deleted.", :green)
87
+ rescue Exception => e
88
+ err_msg ui.color("ERROR DELETING CHEF NODE", :red)
89
+ err_msg ui.color(e.message, :yellow)
90
+ err_msg ui.color(e.backtrace.join("\n"), :yellow)
91
+ end
92
+
93
+ begin
94
+ destroy_item(Chef::ApiClient, @node.name, "client")
95
+ puts ui.color("Chef client successfully deleted.", :green)
96
+ rescue Exception => e
97
+ err_msg ui.color("ERROR DELETING CHEF CLIENT", :red)
98
+ err_msg ui.color(e.message, :yellow)
99
+ err_msg ui.color(e.backtrace.join("\n"), :yellow)
100
+ end
101
+ else
102
+ "#{ui.color('Chef node: ' + config[:chef_node_name] +' not found! will destroy instance.', :red)}"
103
+ end
104
+
105
+ begin
106
+ @instance.destroy
107
+ puts ui.color("SoftLayer VM successfully deleted. You are no longer being billed for this instance.", :green)
108
+ rescue Exception => e
109
+ err_msg ui.color("ERROR DELETING SOFTLAYER VM. IT'S POSSIBLE YOU ARE STILL BEING BILLED FOR THIS INSTANCE. PLEASE CONTACT SUPPORT FOR FURTHER ASSISTANCE", :red)
110
+ err_msg ui.color(e.message, :yellow)
111
+ err_msg ui.color(e.backtrace.join("\n"), :yellow)
112
+ end
113
+ ensure
114
+ unless err_msg.empty?
115
+ err_msg.each do |msg|
116
+ puts msg
117
+ end
118
+ end
119
+ end
120
+
121
+ end
122
+
123
+ # @param [Chef::*] klass
124
+ # @param [String] name
125
+ # @param [String] type_name
126
+ # @return [nil]
127
+ def destroy_item(klass, name, type_name)
128
+ begin
129
+ object = klass.load(name)
130
+ object.destroy
131
+ ui.warn("Deleted #{type_name} #{name}")
132
+ rescue Net::HTTPServerException
133
+ ui.warn("Could not find a #{type_name} named #{name} to delete!")
134
+ end
135
+ end
136
+
137
+ def err_msg(msg=nil)
138
+ @msgs ||= []
139
+ @msgs.push(msg).compact
140
+ end
141
+
142
+ end
143
+ end
144
+ end
145
+
146
+
@@ -1,27 +1,27 @@
1
- require 'chef/knife/softlayer_base'
2
- require 'chef/search/query'
3
- require 'chef/api_client'
4
-
5
- class Chef
6
- class Knife
7
- class SoftlayerServerList < Knife
8
-
9
- include Knife::SoftlayerBase
10
-
11
- banner 'knife softlayer server list (options)'
12
-
13
- ##
14
- # Run the procedure to list all of the Softlayer VM's
15
- # @return [nil]
16
- def run
17
-
18
- $stdout.sync = true
19
- fmt = "%-20s %-8s %-15s %-15s %-10s"
20
- puts ui.color(sprintf(fmt, "Name", "Location", "Public IP", "Private IP", "Status"), :green)
21
- connection.servers.each do |server|
22
- puts sprintf fmt, server.name, server.datacenter, server.public_ip_address, server.private_ip_address, server.created_at ? 'Running' : 'Starting'
23
- end
24
- end
25
- end
26
- end
27
- end
1
+ require 'chef/knife/softlayer_base'
2
+ require 'chef/search/query'
3
+ require 'chef/api_client'
4
+
5
+ class Chef
6
+ class Knife
7
+ class SoftlayerServerList < Knife
8
+
9
+ include Knife::SoftlayerBase
10
+
11
+ banner 'knife softlayer server list (options)'
12
+
13
+ ##
14
+ # Run the procedure to list all of the Softlayer VM's
15
+ # @return [nil]
16
+ def run
17
+
18
+ $stdout.sync = true
19
+ fmt = "%-20s %-8s %-15s %-15s %-10s"
20
+ puts ui.color(sprintf(fmt, "Name", "Location", "Public IP", "Private IP", "Status"), :green)
21
+ connection.servers.each do |server|
22
+ puts sprintf fmt, server.name, server.datacenter, server.public_ip_address, server.private_ip_address, server.created_at ? 'Running' : 'Starting'
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,208 +1,208 @@
1
- #
2
- # Author:: Matt Eldridge (<matt.eldridge@us.ibm.com>)
3
- # © Copyright IBM Corporation 2014.
4
- #
5
- # LICENSE: Apache 2.0 (http://www.apache.org/licenses/)
6
- #
7
-
8
- require 'chef/knife/softlayer_base'
9
-
10
- class Chef
11
- class Knife
12
- class SoftlayerServerRelaunch < Knife
13
-
14
- include Knife::SoftlayerBase
15
-
16
- banner 'knife softlayer server relaunch <NODE NAME> [<NODE NAME>]'
17
-
18
- option :all,
19
- :short => "-a",
20
- :long => "--all",
21
- :description => "Display all available configuration options for launching an instance.",
22
- :default => false
23
-
24
- ##
25
- # Run the procedure to list softlayer VM flavors or display all available options.
26
- # @return [nil]
27
- def run
28
- $stdout.sync = true
29
- if name_args.count < 1
30
- ui.fatal("Server relaunch requires AT LEAST ONE node name.")
31
- exit 1;
32
- end
33
-
34
- ident_file = Chef::Config[:knife][:identity_file] || config[:identity_file]
35
- Fog.credentials[:private_key_path] = ident_file if ident_file
36
-
37
- Chef::Search::Query.new.search(:node, "name:#{name_args[0]}") do |object|
38
- @vm = connection.servers.select { |s| s.public_ip == object.ipaddress }.first
39
- end
40
- ui.fatal("Server not found on SoftLayer account.") and exit 1 unless @vm
41
-
42
- unless @vm.sshable?
43
- ui.fatal("Node with name #{name_args[0]} not sshable, relaunch canceled.")
44
- exit 1
45
- end
46
-
47
- # grab the contents of /etc/chef from the target node and stash a local copy
48
- begin
49
- puts ui.color("Capturing existing node configuration files.", :green)
50
- @vm.scp_download("/etc/chef", "/tmp/#{@vm.id}/", :recursive => true)
51
- rescue Exception => e
52
- puts ui.color(e.message, :red)
53
- ui.fatal('Relaunch canceled.')
54
- exit 1
55
- end
56
-
57
- begin
58
- puts ui.color("Relaunching SoftLayer server, this may take a few minutes.", :green)
59
- @vm.relaunch!
60
- @vm.wait_for { putc '.'; ready? && sshable? }
61
- puts ''
62
- rescue Exception => e
63
- puts ui.color(e.message, :red)
64
- ui.fatal('Relaunch FAILED. You may be missing a server.')
65
- exit 1
66
- end
67
-
68
- # push the locally stashed config items up to new machine
69
- begin
70
- puts ui.color("Installing node configuration on relaunched server.", :green)
71
- @vm.scp("/tmp/#{@vm.id}/chef/", "/etc/", :recursive => true)
72
- rescue Exception => e
73
- puts ui.color(e.message, :red)
74
- ui.fatal('Relaunch FAILED. You may be missing a chef node.')
75
- exit 1
76
- end
77
-
78
- begin
79
- puts ui.color("Installing chef-client executable on relaunched server.", :green)
80
- puts @vm.ssh('wget https://www.chef.io/chef/install.sh && sudo bash ./install.sh && rm install.sh').first.stdout
81
- rescue Exception => e
82
- puts ui.color(e.message, :red)
83
- ui.fatal('Relaunch FAILED. You may be missing a chef node.')
84
- exit 1
85
- end
86
-
87
- # run chef-client on the new machine with the existing config and attributes
88
- begin
89
- puts ui.color("Initial run of chef-client on relaunched server, this may take a few minutes.", :green)
90
- puts @vm.ssh('sudo chef-client').first.stdout
91
- rescue Exception => e
92
- puts ui.color(e.message, :red)
93
- ui.fatal('Relaunch FAILED on chef run. Your chef node may be misconfigured.')
94
- exit 1
95
- end
96
-
97
- end
98
-
99
- end
100
- end
101
- end
102
-
103
- #
104
- # Author:: Matt Eldridge (<matt.eldridge@us.ibm.com>)
105
- # © Copyright IBM Corporation 2014.
106
- #
107
- # LICENSE: Apache 2.0 (http://www.apache.org/licenses/)
108
- #
109
-
110
- require 'chef/knife/softlayer_base'
111
-
112
- class Chef
113
- class Knife
114
- class SoftlayerServerRelaunch < Knife
115
-
116
- include Knife::SoftlayerBase
117
-
118
- banner 'knife softlayer server relaunch <NODE NAME> [<NODE NAME>]'
119
-
120
- option :all,
121
- :short => "-a",
122
- :long => "--all",
123
- :description => "Display all available configuration options for launching an instance.",
124
- :default => false
125
-
126
- require 'chef/knife/bootstrap'
127
- # Make the base bootstrap options available on topo bootstrap
128
- self.options = (Chef::Knife::Bootstrap.options).merge(self.options)
129
-
130
- ##
131
- # Run the procedure to list softlayer VM flavors or display all available options.
132
- # @return [nil]
133
- def run
134
- $stdout.sync = true
135
- if name_args.count < 1
136
- ui.fatal("Server relaunch requires AT LEAST ONE node name.")
137
- exit 1;
138
- end
139
-
140
- ident_file = Chef::Config[:knife][:identity_file] || config[:identity_file]
141
- Fog.credentials[:private_key_path] = ident_file if ident_file
142
-
143
- Chef::Search::Query.new.search(:node, "name:#{name_args[0]}") do |object|
144
- @vm = connection.servers.select { |s| s.public_ip == object.ipaddress }.first
145
- end
146
- ui.fatal("Server not found on SoftLayer account.") and exit 1 unless @vm
147
-
148
- unless @vm.sshable?
149
- ui.fatal("Node with name #{name_args[0]} not sshable, relaunch canceled.")
150
- exit 1
151
- end
152
-
153
- # grab the contents of /etc/chef from the target node and stash a local copy
154
- begin
155
- puts ui.color("Capturing existing node configuration files.", :green)
156
- @vm.scp_download("/etc/chef", "/tmp/#{@vm.id}/", :recursive => true)
157
- rescue Exception => e
158
- puts ui.color(e.message, :red)
159
- ui.fatal('Relaunch canceled.')
160
- exit 1
161
- end
162
-
163
- begin
164
- puts ui.color("Relaunching SoftLayer server, this may take a few minutes.", :green)
165
- @vm.relaunch!
166
- @vm.wait_for { putc '.'; ready? && sshable? }
167
- puts ''
168
- rescue Exception => e
169
- puts ui.color(e.message, :red)
170
- ui.fatal('Relaunch FAILED. You may be missing a server.')
171
- exit 1
172
- end
173
-
174
- # push the locally stashed config items up to new machine
175
- begin
176
- puts ui.color("Installing node configuration on relaunched server.", :green)
177
- @vm.scp("/tmp/#{@vm.id}/chef/", "/etc/", :recursive => true)
178
- rescue Exception => e
179
- puts ui.color(e.message, :red)
180
- ui.fatal('Relaunch FAILED. You may be missing a chef node.')
181
- exit 1
182
- end
183
-
184
- begin
185
- puts ui.color("Installing chef-client executable on relaunched server.", :green)
186
- puts @vm.ssh('wget https://www.chef.io/chef/install.sh && sudo bash ./install.sh && rm install.sh').first.stdout
187
- rescue Exception => e
188
- puts ui.color(e.message, :red)
189
- ui.fatal('Relaunch FAILED. You may be missing a chef node.')
190
- exit 1
191
- end
192
-
193
- # run chef-client on the new machine with the existing config and attributes
194
- begin
195
- puts ui.color("Initial run of chef-client on relaunched server, this may take a few minutes.", :green)
196
- puts @vm.ssh('sudo chef-client -j /etc/chef/first-boot.json').first.stdout
197
- rescue Exception => e
198
- puts ui.color(e.message, :red)
199
- ui.fatal('Relaunch FAILED on chef run. Your chef node may be misconfigured.')
200
- exit 1
201
- end
202
-
203
- end
204
-
205
- end
206
- end
207
- end
208
-
1
+ #
2
+ # Author:: Matt Eldridge (<matt.eldridge@us.ibm.com>)
3
+ # © Copyright IBM Corporation 2014.
4
+ #
5
+ # LICENSE: Apache 2.0 (http://www.apache.org/licenses/)
6
+ #
7
+
8
+ require 'chef/knife/softlayer_base'
9
+
10
+ class Chef
11
+ class Knife
12
+ class SoftlayerServerRelaunch < Knife
13
+
14
+ include Knife::SoftlayerBase
15
+
16
+ banner 'knife softlayer server relaunch <NODE NAME> [<NODE NAME>]'
17
+
18
+ option :all,
19
+ :short => "-a",
20
+ :long => "--all",
21
+ :description => "Display all available configuration options for launching an instance.",
22
+ :default => false
23
+
24
+ ##
25
+ # Run the procedure to list softlayer VM flavors or display all available options.
26
+ # @return [nil]
27
+ def run
28
+ $stdout.sync = true
29
+ if name_args.count < 1
30
+ ui.fatal("Server relaunch requires AT LEAST ONE node name.")
31
+ exit 1;
32
+ end
33
+
34
+ ident_file = Chef::Config[:knife][:identity_file] || config[:identity_file]
35
+ Fog.credentials[:private_key_path] = ident_file if ident_file
36
+
37
+ Chef::Search::Query.new.search(:node, "name:#{name_args[0]}") do |object|
38
+ @vm = connection.servers.select { |s| s.public_ip == object.ipaddress }.first
39
+ end
40
+ ui.fatal("Server not found on SoftLayer account.") and exit 1 unless @vm
41
+
42
+ unless @vm.sshable?
43
+ ui.fatal("Node with name #{name_args[0]} not sshable, relaunch canceled.")
44
+ exit 1
45
+ end
46
+
47
+ # grab the contents of /etc/chef from the target node and stash a local copy
48
+ begin
49
+ puts ui.color("Capturing existing node configuration files.", :green)
50
+ @vm.scp_download("/etc/chef", "/tmp/#{@vm.id}/", :recursive => true)
51
+ rescue Exception => e
52
+ puts ui.color(e.message, :red)
53
+ ui.fatal('Relaunch canceled.')
54
+ exit 1
55
+ end
56
+
57
+ begin
58
+ puts ui.color("Relaunching SoftLayer server, this may take a few minutes.", :green)
59
+ @vm.relaunch!
60
+ @vm.wait_for { putc '.'; ready? && sshable? }
61
+ puts ''
62
+ rescue Exception => e
63
+ puts ui.color(e.message, :red)
64
+ ui.fatal('Relaunch FAILED. You may be missing a server.')
65
+ exit 1
66
+ end
67
+
68
+ # push the locally stashed config items up to new machine
69
+ begin
70
+ puts ui.color("Installing node configuration on relaunched server.", :green)
71
+ @vm.scp("/tmp/#{@vm.id}/chef/", "/etc/", :recursive => true)
72
+ rescue Exception => e
73
+ puts ui.color(e.message, :red)
74
+ ui.fatal('Relaunch FAILED. You may be missing a chef node.')
75
+ exit 1
76
+ end
77
+
78
+ begin
79
+ puts ui.color("Installing chef-client executable on relaunched server.", :green)
80
+ puts @vm.ssh('wget https://www.chef.io/chef/install.sh && sudo bash ./install.sh && rm install.sh').first.stdout
81
+ rescue Exception => e
82
+ puts ui.color(e.message, :red)
83
+ ui.fatal('Relaunch FAILED. You may be missing a chef node.')
84
+ exit 1
85
+ end
86
+
87
+ # run chef-client on the new machine with the existing config and attributes
88
+ begin
89
+ puts ui.color("Initial run of chef-client on relaunched server, this may take a few minutes.", :green)
90
+ puts @vm.ssh('sudo chef-client').first.stdout
91
+ rescue Exception => e
92
+ puts ui.color(e.message, :red)
93
+ ui.fatal('Relaunch FAILED on chef run. Your chef node may be misconfigured.')
94
+ exit 1
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+ end
101
+ end
102
+
103
+ #
104
+ # Author:: Matt Eldridge (<matt.eldridge@us.ibm.com>)
105
+ # © Copyright IBM Corporation 2014.
106
+ #
107
+ # LICENSE: Apache 2.0 (http://www.apache.org/licenses/)
108
+ #
109
+
110
+ require 'chef/knife/softlayer_base'
111
+
112
+ class Chef
113
+ class Knife
114
+ class SoftlayerServerRelaunch < Knife
115
+
116
+ include Knife::SoftlayerBase
117
+
118
+ banner 'knife softlayer server relaunch <NODE NAME> [<NODE NAME>]'
119
+
120
+ option :all,
121
+ :short => "-a",
122
+ :long => "--all",
123
+ :description => "Display all available configuration options for launching an instance.",
124
+ :default => false
125
+
126
+ require 'chef/knife/bootstrap'
127
+ # Make the base bootstrap options available on topo bootstrap
128
+ self.options = (Chef::Knife::Bootstrap.options).merge(self.options)
129
+
130
+ ##
131
+ # Run the procedure to list softlayer VM flavors or display all available options.
132
+ # @return [nil]
133
+ def run
134
+ $stdout.sync = true
135
+ if name_args.count < 1
136
+ ui.fatal("Server relaunch requires AT LEAST ONE node name.")
137
+ exit 1;
138
+ end
139
+
140
+ ident_file = Chef::Config[:knife][:identity_file] || config[:identity_file]
141
+ Fog.credentials[:private_key_path] = ident_file if ident_file
142
+
143
+ Chef::Search::Query.new.search(:node, "name:#{name_args[0]}") do |object|
144
+ @vm = connection.servers.select { |s| s.public_ip == object.ipaddress }.first
145
+ end
146
+ ui.fatal("Server not found on SoftLayer account.") and exit 1 unless @vm
147
+
148
+ unless @vm.sshable?
149
+ ui.fatal("Node with name #{name_args[0]} not sshable, relaunch canceled.")
150
+ exit 1
151
+ end
152
+
153
+ # grab the contents of /etc/chef from the target node and stash a local copy
154
+ begin
155
+ puts ui.color("Capturing existing node configuration files.", :green)
156
+ @vm.scp_download("/etc/chef", "/tmp/#{@vm.id}/", :recursive => true)
157
+ rescue Exception => e
158
+ puts ui.color(e.message, :red)
159
+ ui.fatal('Relaunch canceled.')
160
+ exit 1
161
+ end
162
+
163
+ begin
164
+ puts ui.color("Relaunching SoftLayer server, this may take a few minutes.", :green)
165
+ @vm.relaunch!
166
+ @vm.wait_for { putc '.'; ready? && sshable? }
167
+ puts ''
168
+ rescue Exception => e
169
+ puts ui.color(e.message, :red)
170
+ ui.fatal('Relaunch FAILED. You may be missing a server.')
171
+ exit 1
172
+ end
173
+
174
+ # push the locally stashed config items up to new machine
175
+ begin
176
+ puts ui.color("Installing node configuration on relaunched server.", :green)
177
+ @vm.scp("/tmp/#{@vm.id}/chef/", "/etc/", :recursive => true)
178
+ rescue Exception => e
179
+ puts ui.color(e.message, :red)
180
+ ui.fatal('Relaunch FAILED. You may be missing a chef node.')
181
+ exit 1
182
+ end
183
+
184
+ begin
185
+ puts ui.color("Installing chef-client executable on relaunched server.", :green)
186
+ puts @vm.ssh('wget https://www.chef.io/chef/install.sh && sudo bash ./install.sh && rm install.sh').first.stdout
187
+ rescue Exception => e
188
+ puts ui.color(e.message, :red)
189
+ ui.fatal('Relaunch FAILED. You may be missing a chef node.')
190
+ exit 1
191
+ end
192
+
193
+ # run chef-client on the new machine with the existing config and attributes
194
+ begin
195
+ puts ui.color("Initial run of chef-client on relaunched server, this may take a few minutes.", :green)
196
+ puts @vm.ssh('sudo chef-client -j /etc/chef/first-boot.json').first.stdout
197
+ rescue Exception => e
198
+ puts ui.color(e.message, :red)
199
+ ui.fatal('Relaunch FAILED on chef run. Your chef node may be misconfigured.')
200
+ exit 1
201
+ end
202
+
203
+ end
204
+
205
+ end
206
+ end
207
+ end
208
+