knife-oktawave 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.rdoc ADDED
@@ -0,0 +1,86 @@
1
+ = Knife Oktawave
2
+
3
+ = DESCRIPTION:
4
+
5
+ This plugin extends Knife with the ability to manage Oktawave Cloud Instances (OCI).
6
+
7
+ = INSTALLATION:
8
+
9
+ This plugin is distributed as Ruby Gem. You can install it by running:
10
+
11
+ gem install knife-oktawave
12
+
13
+ = CONFIGURATION:
14
+
15
+ This plugin uses the Oktawave API to manage OCI. Knife will need a valid Oktawave
16
+ login and password before it can talk to the API. You can put these in your
17
+ <tt>knife.rb</tt> file:
18
+
19
+ knife[:oktawave_login] = 'YOUR_OKTAWAVE_LOGIN'
20
+ knife[:oktawave_password] = 'YOUR_OKTAWAVE_PASSWORD'
21
+
22
+ Alternatively, you can supply your credentials to each knife command using the
23
+ <tt>-A</tt> and <tt>-K</tt> (or <tt>--oktawave-login</tt> and
24
+ <tt>--oktawave-password</tt>) options:
25
+
26
+ knife oktawave oci list -A YOUR_OKTAWAVE_LOGIN -K YOUR_OKTAWAVE_PASSWORD
27
+
28
+ = SUBCOMMANDS:
29
+
30
+ This plugin provides the following Knife subcommands:
31
+
32
+ == knife oktawave oci list
33
+
34
+ Shows a list of all instances for your Oktawave account. Note that some of these
35
+ may not be currently managed by Chef server.
36
+
37
+ == knife oktawave oci show ID
38
+
39
+ Displays extended information about a selected OCI. Requires an OCI ID (returned
40
+ by "knife oktawave oci list")
41
+
42
+ == knife oktawave template list
43
+
44
+ Prints a list of all OCI templates available for tour account. A template's ID is
45
+ required to create an instance. Not all templates support an automatic Chef
46
+ bootstrap, but you can provision any instance with Knife (and perhaps install
47
+ chef-client manually). Refer to the Oktawave documentation for details.
48
+
49
+ == knife oktawave oci create -T TEMPLATE_ID -N NAME
50
+
51
+ Creates a new OCI from the selected template and bootstraps Chef on it.
52
+
53
+ == knife oktawave oci delete ID
54
+
55
+ Deletes an OCI. Use the <tt>--purge</tt> flag to delete the corresponding Chef
56
+ node and client as well.
57
+
58
+ == knife oktawave oci power off ID
59
+
60
+ Shuts down an instance.
61
+
62
+ == knife oktawave oci power on ID
63
+
64
+ Starts an instance.
65
+
66
+ == knife oktawave oci restart ID
67
+
68
+ Restarts an instance.
69
+
70
+ = LICENSE:
71
+
72
+ Author:: Marek Siemdaj (<marek.siemdaj@gmail.com>)
73
+ Copyright:: Copyright (C) 2013 Oktawave
74
+ License:: Apache License, Version 2.0
75
+
76
+ Licensed under the Apache License, Version 2.0 (the "License");
77
+ you may not use this file except in compliance with the License.
78
+ You may obtain a copy of the License at
79
+
80
+ http://www.apache.org/licenses/LICENSE-2.0
81
+
82
+ Unless required by applicable law or agreed to in writing, software
83
+ distributed under the License is distributed on an "AS IS" BASIS,
84
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
85
+ See the License for the specific language governing permissions and
86
+ limitations under the License.
@@ -0,0 +1,95 @@
1
+ #
2
+ # Base class for knife oktawave subcommands
3
+ #
4
+
5
+ require 'chef/knife'
6
+ require 'oktawave/client'
7
+
8
+ class Chef
9
+ class Knife
10
+ module OktawaveBase
11
+
12
+ def self.included(includer)
13
+ includer.class_eval do
14
+ deps do
15
+ end
16
+
17
+ option :oktawave_login,
18
+ :short => "-A LOGIN",
19
+ :long => "--oktawave-login LOGIN",
20
+ :description => "Your Oktawave login",
21
+ :proc => Proc.new { |l| Chef::Config[:knife][:oktawave_login] = l }
22
+
23
+ option :oktawave_password,
24
+ :short => "-K PASSWORD",
25
+ :long => "--oktawave-password PASSWORD",
26
+ :description => "Your Oktawave password",
27
+ :proc => Proc.new { |key| Chef::Config[:knife][:oktawave_password] = key }
28
+
29
+ option :debug,
30
+ :long => "--debug",
31
+ :description => "Enable debug mode (including a log of SOAP API requests)",
32
+ :proc => Proc.new { |d| Chef::Config[:knife][:debug] = d }
33
+
34
+ end
35
+ end
36
+
37
+ def oci_not_found
38
+ ui.fatal("OCI not found")
39
+ exit 1
40
+ end
41
+
42
+ # A wrapper around oci_get
43
+ def get_oci(id)
44
+ res = api.oci_get(id)
45
+ if !res[:virtual_machine_id]
46
+ oci_not_found
47
+ end
48
+ res
49
+ end
50
+
51
+ # Returns the underlying Oktawave::OktawaveClient instance
52
+ def api
53
+ @api ||= begin
54
+ api = Oktawave::OktawaveClient.new(
55
+ config[:oktawave_login],
56
+ config[:oktawave_password],
57
+ Chef::Config[:knife][:debug] ? true : false
58
+ )
59
+ end
60
+ end
61
+
62
+ def msg_pair(label, value, color=:cyan)
63
+ if value && !value.to_s.empty?
64
+ puts "#{ui.color(label, color)}: #{value}"
65
+ end
66
+ end
67
+
68
+ # Prints basic information about an OCI. Expects the oci argument to be an
69
+ # instance returned by get_oci.
70
+ def print_oci_summary(oci)
71
+ if !oci[:virtual_machine_id]
72
+ oci_not_found
73
+ end
74
+ msg_pair("OCI ID", oci[:virtual_machine_id])
75
+ msg_pair("Name", oci[:virtual_machine_name])
76
+ msg_pair("Class", api.dive2name(oci[:vm_class])[:item_name])
77
+ msg_pair("Status", api.dive2name(oci[:status])[:item_name])
78
+ end
79
+
80
+ def validate!(keys=[:oktawave_login, :oktawave_password])
81
+ errors = []
82
+ keys.each do |k|
83
+ unless Chef::Config[:knife][k]
84
+ errors << "You did not provide a valid '#{k}' value."
85
+ end
86
+ end
87
+ if errors.each{|e| ui.error(e)}.any?
88
+ exit 1
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+
@@ -0,0 +1,253 @@
1
+ require 'chef/knife/oktawave_base'
2
+
3
+ class Chef
4
+ class Knife
5
+ class OktawaveOciCreate < Knife
6
+ include OktawaveBase
7
+
8
+ banner 'knife oktawave oci create (options)'
9
+
10
+ option :node_name,
11
+ :short => "-N NAME",
12
+ :long => "--node-name NAME",
13
+ :description => "The Chef node name for your new node",
14
+ :proc => Proc.new { |key| Chef::Config[:knife][:node_name] = key }
15
+
16
+ option :bootstrap_version,
17
+ :long => "--bootstrap-version VERSION",
18
+ :description => "The version of Chef to install",
19
+ :proc => Proc.new { |v| Chef::Config[:knife][:bootstrap_version] = v }
20
+
21
+ option :run_list,
22
+ :short => "-r RUN_LIST",
23
+ :long => "--run-list RUN_LIST",
24
+ :description => "Comma separated list of roles/recipes to apply",
25
+ :proc => lambda { |o| o.split(/[\s,]+/) }
26
+
27
+ option :json_attributes,
28
+ :short => "-j JSON",
29
+ :long => "--json-attributes JSON",
30
+ :description => "A JSON string to be added to the first run of chef-client",
31
+ :proc => lambda { |o| JSON.parse(o) }
32
+
33
+ option :distro,
34
+ :short => "-d DISTRO",
35
+ :long => "--distro DISTRO",
36
+ :description => "Bootstrap a distro using a template; default is 'chef-full'",
37
+ :proc => Proc.new { |d| Chef::Config[:knife][:distro] = d }
38
+
39
+ option :skip_bootstrap,
40
+ :long => "--skip-bootstrap",
41
+ :boolean => true,
42
+ :default => false,
43
+ :description => "Only create the OCI, do not perform Chef bootstrap.",
44
+ :proc => Proc.new { Chef::Config[:knife][:skip_bootstrap] = true }
45
+
46
+ option :template,
47
+ :short => '-T TEMPLATE_ID',
48
+ :long => '--oci-template TEMPLATE_ID',
49
+ :description => 'The OCI template ID to use. Run "knife oktawave template list" for a list of templates',
50
+ :proc => Proc.new { |t| Chef::Config[:knife][:template] = t }
51
+
52
+ option :oci_name,
53
+ :long => '--oci-name NAME',
54
+ :description => 'The OCI name. Default is the same as node_name.',
55
+ :proc => Proc.new { |n| Chef::Config[:knife][:oci_name] = n }
56
+
57
+ option :oci_id,
58
+ :short => '-B ID',
59
+ :long => '--bootstrap-oci ID',
60
+ :description => 'ID of an existing OCI. Setting this will cause knife to skip creating a new OCI and just perform the bootstrap.',
61
+ :proc => Proc.new { |id| Chef::Config[:knife][:oci_id] = id }
62
+
63
+ option :oci_class,
64
+ :short => '-C CLASS',
65
+ :long => '--oci-class CLASS',
66
+ :description => 'The OCI class to use ("Small", "Extreme" etc.). Defaults to the minimal class for the template.',
67
+ :proc => Proc.new { |c| Chef::Config[:knife][:oci_class] = c }
68
+
69
+ option :oci_autoscaler,
70
+ :short => '-a TYPE',
71
+ :long => '--oci-autoscaler TYPE',
72
+ :description => 'Autoscaler type ("on", "off" or "notify", default is "on").',
73
+ :proc => Proc.new { |a| Chef::Config[:knife][:oci_autoscaler] = a }
74
+
75
+ option :prerelease,
76
+ :long => "--prerelease",
77
+ :description => "Install the pre-release chef gems"
78
+
79
+ deps do
80
+ require 'chef/knife/bootstrap'
81
+ Chef::Knife::Bootstrap.load_deps
82
+ end
83
+
84
+ # Get a config value
85
+ def conf(key)
86
+ key = key.to_sym
87
+ config[key] || Chef::Config[:knife][key]
88
+ end
89
+
90
+ # wait for sshd to accept connections
91
+ def tcp_test_ssh(hostname, ssh_port = 22, tries = 8, timeout = 16)
92
+ print "\n#{ui.color("Waiting for sshd ", :magenta)}" + '.'
93
+ tries.times {|i|
94
+ begin
95
+ print "."
96
+ tcp_socket = TCPSocket.new(hostname, ssh_port)
97
+ readable = IO.select([tcp_socket], nil, nil, timeout)
98
+ if readable
99
+ puts " done"
100
+ Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
101
+ return true
102
+ end
103
+ rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, IOError
104
+ sleep timeout
105
+ false
106
+ rescue Errno::EPERM, Errno::ETIMEDOUT
107
+ false
108
+ ensure
109
+ tcp_socket && tcp_socket.close
110
+ end
111
+ }
112
+ end
113
+
114
+ def bootstrap_common_params(bootstrap)
115
+ bootstrap.config[:run_list] = config[:run_list]
116
+ bootstrap.config[:bootstrap_version] = conf(:bootstrap_version)
117
+ bootstrap.config[:environment] = conf(:environment)
118
+ bootstrap.config[:prerelease] = conf(:prerelease)
119
+ bootstrap.config[:first_boot_attributes] = conf(:json_attributes) || {}
120
+ bootstrap.config[:encrypted_data_bag_secret] = conf(:encrypted_data_bag_secret)
121
+ bootstrap.config[:encrypted_data_bag_secret_file] = conf(:encrypted_data_bag_secret_file)
122
+ bootstrap
123
+ end
124
+
125
+ def bootstrap_for_linux_node(oci)
126
+ bootstrap = Chef::Knife::Bootstrap.new
127
+ bootstrap.name_args = [@ip]
128
+ bootstrap.config[:ssh_user] = 'root'
129
+ bootstrap.config[:ssh_password] = conf(:ssh_password)
130
+ bootstrap.config[:chef_node_name] = conf(:node_name) || oci[:virtual_machine_name] || self.fqdn || @ip || 'oci_unknown'
131
+ bootstrap.config[:distro] = conf(:distro) || "chef-full"
132
+ bootstrap.config[:use_sudo] = false
133
+ bootstrap_common_params(bootstrap)
134
+ end
135
+
136
+ def fqdn
137
+ require 'resolv'
138
+ @fqdn ||= Resolv.getname(@ip)
139
+ end
140
+
141
+ def bootstrap(id)
142
+ if conf(:skip_bootstrap)
143
+ puts "skip_bootstrap option set, skipping bootstrap."
144
+ return
145
+ end
146
+ unless id
147
+ show_usage
148
+ ui.fatal('You must specify the OCI ID (try "knife oktawave oci list")')
149
+ exit 1
150
+ end
151
+ @oci = get_oci(id)
152
+ @ip = api.oci_ip(@oci)
153
+ puts "\nBootstrapping Chef on Oktawave Cloud Instance #{self.fqdn} (#{@ip}):"
154
+ puts "(if this fails for some reason, you can retry by running \"knife oktawave oci create --bootstrap-oci #{id}\")"
155
+ self.print_oci_summary(@oci)
156
+ unless conf(:oci_id)
157
+ print "\n#{ui.color("Waiting for the OCI ", :magenta)}" + '.'
158
+ sleep 24
159
+ print "done\n"
160
+ end
161
+ unless conf(:ssh_password)
162
+ pass = api.oci_password(id)
163
+ if pass != nil
164
+ Chef::Config[:knife][:ssh_password] = pass
165
+ end
166
+ end
167
+ if !tcp_test_ssh(@ip)
168
+ ui.fatal('Failed to estabilish SSH connection for bootstrap')
169
+ exit 1
170
+ end
171
+ sleep 1
172
+ bootstrap_for_linux_node(@oci).run
173
+ end
174
+
175
+ def run
176
+ $stdout.sync = true
177
+ validate!
178
+ if conf(:oci_id)
179
+ bootstrap(conf(:oci_id))
180
+ return
181
+ end
182
+ validate!([:node_name, :template])
183
+ period = 60 # minutes
184
+ interval = 5 # seconds
185
+ timeout = 30 # iterations, multiply by interval for seconds approximation
186
+ object_type_id = 139 # Machine
187
+ name = conf(:oci_name) || conf(:node_name)
188
+ template_id = conf(:template)
189
+ oci_class = conf(:oci_class) || nil
190
+ oci_class = api.oci_class_id(oci_class) if oci_class
191
+ puts "Creating a new OCI instance \"#{name}\" (template #{template_id})"
192
+ rj = api.running_jobs(period)
193
+ old_oci_ids = Hash[
194
+ api.oci_list.map {|o| [o[:virtual_machine_id], true]} +
195
+ rj.select {|j| j[:object_type_id].to_i == object_type_id}.map {|j| [j[:object_id], true]}
196
+ ]
197
+ old_job_ids = Hash[rj.map {|j| [j[:asynchronous_operation_id], true]}]
198
+ api.oci_create(template_id, name, oci_class, conf(:oci_autoscaler))
199
+ oci_id = -1
200
+ it = 0
201
+
202
+ while true
203
+ it += 1
204
+ sleep interval
205
+ all_jobs = api.running_jobs(period)
206
+ jobs = all_jobs.select {|o| # select operations on the new machine only
207
+ (not old_job_ids[o[:asynchronous_operation_id]])\
208
+ and
209
+ (o[:object_type_id].to_i == object_type_id)\
210
+ and
211
+ o[:status_id].to_i == 135\
212
+ and
213
+ o[:object_name] == name\
214
+ and
215
+ (
216
+ (
217
+ oci_id != -1\
218
+ and
219
+ o[:object_id].to_i == oci_id
220
+ )\
221
+ or
222
+ oci_id == -1
223
+ )\
224
+ and
225
+ (
226
+ not old_oci_ids[o[:object_id]]\
227
+ or
228
+ o[:object_id].to_i == 0
229
+ )
230
+ }
231
+ for j in jobs
232
+ if oci_id == -1 and j[:object_id].to_i != 0
233
+ puts "Created new OCI with ID: #{j[:object_id]}"
234
+ oci_id = j[:object_id].to_i # we know the ID now, so we remember it
235
+ end
236
+ if oci_id != -1 and j[:object_id].to_i != oci_id # different OCI id
237
+ next
238
+ end
239
+ puts "#{j[:operation_type_name]}: #{j[:progress]}%"
240
+ end
241
+ if (jobs.length == 0 and oci_id != -1) or (it > timeout) # timeout or finished
242
+ break
243
+ end
244
+ end
245
+ if (oci_id == -1)
246
+ ui.fatal("Failed to deploy instance. Please check it at admin.oktawave.com and bootstrap manually.")
247
+ exit 1
248
+ end
249
+ bootstrap(oci_id)
250
+ end
251
+ end
252
+ end
253
+ end
@@ -0,0 +1,57 @@
1
+ require 'chef/knife/oktawave_base'
2
+ require 'chef/node'
3
+ require 'chef/api_client'
4
+
5
+ class Chef
6
+ class Knife
7
+ class OktawaveOciDelete < Knife
8
+
9
+ include Knife::OktawaveBase
10
+
11
+ banner "knife oktawave oci delete ID [ID] (options)"
12
+
13
+ option :purge,
14
+ :long => "--purge",
15
+ :short => "-P",
16
+ :boolean => true,
17
+ :default => false,
18
+ :description => "Destroy corresponding node and client on the Chef Server, in addition to destroying the OCI instance. Assumes node and client have the same name as the server (if not, add the '--node-name' option)."
19
+
20
+ option :chef_node_name,
21
+ :short => "-N NAME",
22
+ :long => "--node-name NAME",
23
+ :description => "The name of the node and client to delete, if it differs from the server name. Only has meaning when used with the '--purge' option."
24
+
25
+ def destroy_item(klass, name, type_name)
26
+ begin
27
+ object = klass.load(name)
28
+ object.destroy
29
+ ui.warn("Deleted #{type_name} #{name}")
30
+ rescue Net::HTTPServerException
31
+ ui.warn("Could not find a #{type_name} named #{name} to delete!")
32
+ end
33
+ end
34
+
35
+ def run
36
+ validate!
37
+ @name_args.each do |oci_id|
38
+ oci = get_oci(oci_id)
39
+ print_oci_summary(oci)
40
+ oci_name = oci[:virtual_machine_name]
41
+ puts "\n"
42
+ confirm("Do you really want to delete this OCI instance")
43
+ api.oci_delete(oci_id)
44
+ ui.warn("Deleted OCI instance \##{oci_id} (#{oci_name})")
45
+ if config[:purge]
46
+ thing_to_delete = config[:chef_node_name] || oci_name
47
+ destroy_item(Chef::Node, thing_to_delete, "node")
48
+ destroy_item(Chef::ApiClient, thing_to_delete, "client")
49
+ else
50
+ ui.warn("Corresponding node and client for the #{oci_id} instance (#{oci_name}) were not deleted and remain registered with the Chef Server")
51
+ end
52
+ end
53
+ end
54
+
55
+ end
56
+ end
57
+ end