knife-bmcs 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: be2e5406c2c26806829a393a11214ab8d49a8ff3
4
+ data.tar.gz: 6a562eda67b9f8d5261f60921e4d7de5b5d8e310
5
+ SHA512:
6
+ metadata.gz: 362935ec20291a749046113c309cf0e3156d52be47cd6b022fae2c994790125613b5ca26c6880bf5c6a30d9b8769d4d15eca205eadbe62e414c9cb2de0527c58
7
+ data.tar.gz: fad73265958e040f3e802903f2ef9d78782c1066ce9203f7b119f5a9dbcab27710f6e8064090d55a3a8428f6cff6cca7e5bf718a94518a5d816817bc10c00d75
data/LICENSE.txt ADDED
@@ -0,0 +1,82 @@
1
+ Copyright (c) 2017, Oracle and/or its affiliates.  All rights reserved.
2
+
3
+ This software is dual-licensed to you under the Universal Permissive License (UPL) and Apache License 2.0.  See below for license terms.  You may choose either license, or both.
4
+  ____________________________
5
+ The Universal Permissive License (UPL), Version 1.0
6
+ Copyright (c) 2017, Oracle and/or its affiliates.  All rights reserved.
7
+
8
+ Subject to the condition set forth below, permission is hereby granted to any person obtaining a copy of this software, associated documentation and/or data (collectively the "Software"), free of charge and under any and all copyright rights in the Software, and any and all patent rights owned or freely licensable by each licensor hereunder covering either (i) the unmodified Software as contributed to or provided by such licensor, or (ii) the Larger Works (as defined below), to deal in both
9
+
10
+ (a) the Software, and
11
+ (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if one is included with the Software (each a "Larger Work" to which the Software is contributed by such licensors),
12
+
13
+ without restriction, including without limitation the rights to copy, create derivative works of, display, perform, and distribute the Software and make, use, sell, offer for sale, import, export, have made, and have sold the Software and the Larger Work(s), and to sublicense the foregoing rights on either these or other terms.
14
+
15
+ This license is subject to the following condition:
16
+
17
+ The above copyright notice and either this complete permission notice or at a minimum a reference to the UPL must be included in all copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+
21
+ The Apache Software License, Version 2.0
22
+ Copyright (c) 2017, Oracle and/or its affiliates.  All rights reserved.
23
+
24
+ Licensed under the Apache License, Version 2.0 (the "License"); You may not use this product except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. A copy of the license is also reproduced below. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
25
+
26
+ Apache License
27
+
28
+ Version 2.0, January 2004
29
+
30
+ http://www.apache.org/licenses/
31
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
32
+ 1. Definitions.
33
+ "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
34
+ "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
35
+ "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
36
+ "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
37
+ "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
38
+ "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
39
+ "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
40
+ "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
41
+ "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
42
+ "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
43
+ 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
44
+ 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
45
+ 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
46
+ You must give any other recipients of the Work or Derivative Works a copy of this License; and
47
+ You must cause any modified files to carry prominent notices stating that You changed the files; and
48
+ You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
49
+ If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
50
+
51
+ You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
52
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
53
+ 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
54
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
55
+ 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
56
+ 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
57
+ END OF TERMS AND CONDITIONS
58
+
59
+ APPENDIX: How to apply the Apache License to your work.
60
+
61
+ To apply the Apache License to your work, attach the following
62
+ boilerplate notice, with the fields enclosed by brackets "[]"
63
+ replaced with your own identifying information. (Don't include
64
+ the brackets!) The text should be enclosed in the appropriate
65
+ comment syntax for the file format. We also recommend that a
66
+ file or class name and description of purpose be included on the
67
+ same "printed page" as the copyright notice for easier
68
+ identification within third-party archives.
69
+
70
+ Copyright [yyyy] [name of copyright owner]
71
+
72
+ Licensed under the Apache License, Version 2.0 (the "License");
73
+ you may not use this file except in compliance with the License.
74
+ You may obtain a copy of the License at
75
+
76
+ http://www.apache.org/licenses/LICENSE-2.0
77
+
78
+ Unless required by applicable law or agreed to in writing, software
79
+ distributed under the License is distributed on an "AS IS" BASIS,
80
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
81
+ See the License for the specific language governing permissions and
82
+ limitations under the License.
@@ -0,0 +1,29 @@
1
+ # Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
2
+
3
+ require 'chef/knife'
4
+ require 'chef/knife/bmcs_common_options'
5
+ require 'chef/knife/bmcs_helper'
6
+
7
+ class Chef
8
+ class Knife
9
+ # List availability domains
10
+ class BmcsAdList < Knife
11
+ banner 'knife bmcs ad list (options)'
12
+
13
+ include BmcsHelper
14
+ include BmcsCommonOptions
15
+
16
+ deps do
17
+ require 'oraclebmc'
18
+ end
19
+
20
+ def run
21
+ response = identity_client.list_availability_domains(compartment_id)
22
+
23
+ display_list(response, []) do |item|
24
+ [item.name]
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ # Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
2
+
3
+ require 'chef/knife'
4
+ require 'knife-bmcs/version'
5
+
6
+ class Chef
7
+ class Knife
8
+ # Options that should be included in all BMCS commands
9
+ module BmcsCommonOptions
10
+ def self.included(includer)
11
+ includer.class_eval do
12
+ option :bmcs_config_file,
13
+ long: '--bmcs-config-file FILE',
14
+ description: 'The path to the Oracle BMCS config file. Default: ~/.oraclebmc/config'
15
+
16
+ option :bmcs_profile,
17
+ long: '--bmcs-profile PROFILE',
18
+ description: 'The profile to load from the Oracle BMCS config file. Default: DEFAULT'
19
+
20
+ option :compartment_id,
21
+ long: '--compartment-id COMPARTMENT',
22
+ description: 'The OCID of the compartment.'
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,94 @@
1
+ # Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
2
+
3
+ require 'chef/knife'
4
+ require 'knife-bmcs/version'
5
+
6
+ class Chef
7
+ class Knife
8
+ # BMCS helper module
9
+ module BmcsHelper
10
+ def bmcs_config
11
+ unless @bmcs_config
12
+ # Load config and profile first from command line args if available, then from knife.rb, then use the default.
13
+ config_file = config[:bmcs_config_file] || Chef::Config[:knife][:bmcs_config_file] || OracleBMC::ConfigFileLoader::DEFAULT_CONFIG_FILE
14
+ profile = config[:bmcs_profile] || Chef::Config[:knife][:bmcs_profile] || OracleBMC::ConfigFileLoader::DEFAULT_PROFILE
15
+ @bmcs_config = OracleBMC::ConfigFileLoader.load_config(config_file_location: config_file, profile_name: profile)
16
+
17
+ @bmcs_config.additional_user_agent = "Oracle-ChefKnifeBMCS/#{::Knife::BMCS::VERSION}"
18
+ end
19
+
20
+ @bmcs_config
21
+ end
22
+
23
+ def compute_client
24
+ @compute_client ||= OracleBMC::Core::ComputeClient.new(config: bmcs_config)
25
+ end
26
+
27
+ def network_client
28
+ @network_client ||= OracleBMC::Core::VirtualNetworkClient.new(config: bmcs_config)
29
+ end
30
+
31
+ def identity_client
32
+ @identity_client ||= OracleBMC::Identity::IdentityClient.new(config: bmcs_config)
33
+ end
34
+
35
+ # Get the compartment ID first from the command line args if available, then from the knife.rb
36
+ # file, and if neither of those is specified then use the tenancy.
37
+ def compartment_id
38
+ @compartment_id ||= config[:compartment_id] || Chef::Config[:knife][:compartment_id] || bmcs_config.tenancy
39
+ end
40
+
41
+ def error_and_exit(message)
42
+ ui.error message
43
+ exit(1)
44
+ end
45
+
46
+ def validate_required_params(required_params, params)
47
+ missing_params = required_params.select do |param|
48
+ params[param].nil?
49
+ end
50
+
51
+ error_and_exit("Missing the following required parameters: #{missing_params.join(', ').tr!('_', '-')}") unless missing_params.empty?
52
+ end
53
+
54
+ def warn_if_page_is_truncated(response)
55
+ ui.warn('This list has been truncated. To view more items, increase the limit.') if response.headers.include? 'opc-next-page'
56
+ end
57
+
58
+ def display_list(response, columns)
59
+ list = response.data
60
+ list_for_display = []
61
+
62
+ if config[:format] == 'summary'
63
+ width = 1
64
+
65
+ unless columns.empty?
66
+ columns.each do |column|
67
+ list_for_display += [ui.color(column, :bold)]
68
+ end
69
+
70
+ list_for_display = list_for_display.flatten.compact
71
+ width = columns.length
72
+ end
73
+
74
+ if list
75
+ list.each do |item|
76
+ display_item = yield(item, list_for_display)
77
+ list_for_display += display_item if display_item
78
+ end
79
+ end
80
+
81
+ puts ui.list(list_for_display, :uneven_columns_across, width)
82
+ else
83
+ list.each do |item|
84
+ list_for_display += [item.to_hash]
85
+ end
86
+
87
+ ui.output(list_for_display)
88
+ end
89
+
90
+ warn_if_page_is_truncated(response)
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,36 @@
1
+ # Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
2
+
3
+ require 'chef/knife'
4
+ require 'chef/knife/bmcs_common_options'
5
+ require 'chef/knife/bmcs_helper'
6
+
7
+ class Chef
8
+ class Knife
9
+ # List available images
10
+ class BmcsImageList < Knife
11
+ banner 'knife bmcs image list (options)'
12
+
13
+ include BmcsHelper
14
+ include BmcsCommonOptions
15
+
16
+ deps do
17
+ require 'oraclebmc'
18
+ end
19
+
20
+ option :limit,
21
+ long: '--limit LIMIT',
22
+ description: 'The maximum number of items to return.'
23
+
24
+ def run
25
+ options = {}
26
+ options[:limit] = config[:limit] if config[:limit]
27
+
28
+ response = compute_client.list_images(compartment_id, options)
29
+
30
+ display_list(response, ['Display Name', 'ID', 'OS', 'OS Version']) do |image|
31
+ [image.display_name, image.id, image.operating_system, image.operating_system_version]
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,319 @@
1
+ # Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
2
+
3
+ require 'chef/knife'
4
+ require 'chef/knife/bmcs_helper'
5
+ require 'chef/knife/bmcs_common_options'
6
+
7
+ # Port for SSH - might want to parameterize this in the future.
8
+ SSH_PORT = 22
9
+
10
+ WAIT_FOR_SSH_INTERVAL_SECONDS = 2
11
+ WAIT_FOR_SSH_MAX_SECONDS = 180
12
+
13
+ class Chef
14
+ class Knife
15
+ # Server Create Command: Launch an instance and bootstrap it.
16
+ class BmcsServerCreate < Knife
17
+ banner 'knife bmcs server create (options)'
18
+
19
+ include BmcsHelper
20
+ include BmcsCommonOptions
21
+
22
+ deps do
23
+ require 'oraclebmc'
24
+ require 'chef/knife/bootstrap'
25
+ Chef::Knife::Bootstrap.load_deps
26
+ end
27
+
28
+ option :bmcs_config_file,
29
+ long: '--bmcs-config-file FILE',
30
+ description: 'The path to the Oracle BMCS config file. Default: ~/.oraclebmc/config'
31
+
32
+ option :bmcs_profile,
33
+ long: '--bmcs-profile PROFILE',
34
+ description: 'The profile to load from the Oracle BMCS config file. Default: DEFAULT'
35
+
36
+ option :availability_domain,
37
+ long: '--availability-domain AD',
38
+ description: 'The Availability Domain of the instance.'
39
+
40
+ option :display_name,
41
+ long: '--display-name NAME',
42
+ description: "A user-friendly name for the instance. Does not have to be unique, and it's changeable."
43
+
44
+ option :hostname_label,
45
+ long: '--hostname-label HOSTNAME',
46
+ description: 'The hostname for the VNIC that is created during instance launch. Used for DNS. The value is the hostname '\
47
+ "portion of the instance's fully qualified domain name (FQDN). Must be unique across all VNICs in the subnet "\
48
+ 'and comply with RFC 952 and RFC 1123. The value cannot be changed, and it can be retrieved from the Vnic object.'
49
+
50
+ option :image_id,
51
+ long: '--image-id IMAGE',
52
+ description: 'The OCID of the image used to boot the instance.'
53
+
54
+ option :metadata,
55
+ long: '--metadata METADATA',
56
+ description: 'Custom metadata key/value pairs in JSON format.'
57
+
58
+ option :shape,
59
+ long: '--shape SHAPE',
60
+ description: 'The shape of an instance. The shape determines the number of CPUs, amount of memory, and other resources allocated to the instance.'
61
+
62
+ option :ssh_authorized_keys_file,
63
+ long: '--ssh-authorized-keys-file FILE',
64
+ description: 'A file containing one or more public SSH keys to be included in the ~/.ssh/authorized_keys file for the default user on the instance. '\
65
+ 'Use a newline character to separate multiple keys. The SSH keys must be in the format necessary for the authorized_keys file. This parameter '\
66
+ "is a convenience wrapper around the 'ssh_authorized_keys' field of the --metadata parameter. Populating both values in the same call will result "\
67
+ 'in an error. For more info see documentation: https://docs.us-phoenix-1.oraclecloud.com/api/#/en/iaas/20160918/requests/LaunchInstanceDetails.'
68
+
69
+ option :subnet_id,
70
+ long: '--subnet-id SUBNET',
71
+ description: 'The OCID of the subnet.'
72
+
73
+ option :user_data_file,
74
+ long: '--user-data-file FILE',
75
+ description: 'A file containing data that Cloud-Init can use to run custom scripts or provide custom Cloud-Init configuration. This parameter is a convenience '\
76
+ "wrapper around the 'user_data' field of the --metadata parameter. Populating both values in the same call will result in an error. For more info "\
77
+ 'see Cloud-Init documentation: https://cloudinit.readthedocs.org/en/latest/topics/format.html.'
78
+
79
+ option :ssh_user,
80
+ short: '-x USERNAME',
81
+ long: '--ssh-user USERNAME',
82
+ description: 'The SSH username. Defaults to opc.',
83
+ default: 'opc'
84
+
85
+ option :ssh_password,
86
+ short: '-P PASSWORD',
87
+ long: '--ssh-password PASSWORD',
88
+ description: 'The SSH password'
89
+
90
+ option :identity_file,
91
+ short: '-i FILE',
92
+ long: '--identity-file IDENTITY_FILE',
93
+ description: 'The SSH identity file used for authentication. This must correspond to a public SSH key provided by --ssh-authorized-keys-file.'
94
+
95
+ option :chef_node_name,
96
+ short: '-N NAME',
97
+ long: '--node-name NAME',
98
+ description: 'The Chef node name for the new node. If not specified, the instance display name will be used.'
99
+
100
+ option :run_list,
101
+ short: '-r RUN_LIST',
102
+ long: '--run-list RUN_LIST',
103
+ description: 'A comma-separated list of roles or recipes.',
104
+ proc: ->(o) { o.split(/[\s,]+/) },
105
+ default: []
106
+
107
+ def run
108
+ $stdout.sync = true
109
+ validate_required_params(%i[availability_domain image_id shape subnet_id identity_file], config)
110
+
111
+ metadata = merge_metadata
112
+ error_and_exit 'SSH authorized keys must be specified.' unless metadata['ssh_authorized_keys']
113
+
114
+ request = OracleBMC::Core::Models::LaunchInstanceDetails.new
115
+ request.availability_domain = config[:availability_domain]
116
+ request.compartment_id = compartment_id
117
+ request.display_name = config[:display_name]
118
+ request.hostname_label = config[:hostname_label]
119
+ request.image_id = config[:image_id]
120
+ request.metadata = metadata
121
+ request.shape = config[:shape]
122
+ request.subnet_id = config[:subnet_id]
123
+
124
+ response = compute_client.launch_instance(request)
125
+ instance = response.data
126
+
127
+ ui.msg "Launched instance '#{instance.display_name}' [#{instance.id}]"
128
+ show_value('Display Name', instance.display_name)
129
+ show_value('Instance ID', instance.id)
130
+ show_value('Availability Domain', instance.availability_domain)
131
+ show_value('Compartment ID', instance.compartment_id)
132
+ show_value('Region', instance.region)
133
+ show_value('Image ID', instance.image_id)
134
+ show_value('Shape', instance.shape)
135
+
136
+ instance = wait_for_instance_running(instance.id)
137
+
138
+ ui.msg "Instance '#{instance.display_name}' is now running."
139
+
140
+ vnic = get_vnic(instance.id, instance.compartment_id)
141
+ show_value('Public IP Address', vnic.public_ip)
142
+ show_value('Private IP Address', vnic.private_ip)
143
+
144
+ unless wait_for_ssh(vnic.public_ip, SSH_PORT, WAIT_FOR_SSH_INTERVAL_SECONDS, WAIT_FOR_SSH_MAX_SECONDS)
145
+ error_and_exit 'Timed out while waiting for SSH access.'
146
+ end
147
+
148
+ wait_to_stabilize
149
+
150
+ config[:chef_node_name] = instance.display_name unless config[:chef_node_name]
151
+
152
+ ui.msg "Bootstrapping with node name '#{config[:chef_node_name]}'."
153
+
154
+ # TODO: Consider adding a use_private_ip option.
155
+ bootstrap(vnic.public_ip)
156
+
157
+ ui.msg "Created and bootstrapped node '#{config[:chef_node_name]}'."
158
+ ui.msg "\n"
159
+
160
+ show_value('Display Name', instance.display_name)
161
+ show_value('Instance ID', instance.id)
162
+ show_value('Availability Domain', instance.availability_domain)
163
+ show_value('Compartment ID', instance.compartment_id)
164
+ show_value('Region', instance.region)
165
+ show_value('Image ID', instance.image_id)
166
+ show_value('Shape', instance.shape)
167
+ show_value('Public IP Address', vnic.public_ip)
168
+ show_value('Private IP Address', vnic.private_ip)
169
+ show_value('Hostname', vnic.hostname_label)
170
+ show_value('Node Name', config[:chef_node_name])
171
+ end
172
+
173
+ def bootstrap(name)
174
+ bootstrap = Chef::Knife::Bootstrap.new
175
+
176
+ bootstrap.name_args = [name]
177
+ bootstrap.config[:chef_node_name] = config[:chef_node_name]
178
+ bootstrap.config[:ssh_user] = config[:ssh_user]
179
+ bootstrap.config[:ssh_password] = config[:ssh_password]
180
+ bootstrap.config[:identity_file] = config[:identity_file]
181
+ bootstrap.config[:use_sudo] = true
182
+ bootstrap.config[:ssh_gateway] = config[:ssh_user] + '@' + name
183
+ bootstrap.config[:run_list] = config[:run_list]
184
+
185
+ bootstrap.config[:yes] = true if config[:yes]
186
+
187
+ bootstrap.run
188
+ end
189
+
190
+ def wait_to_stabilize
191
+ # This extra sleep even after getting SSH access is necessary. It's not clear why, but without it we often get
192
+ # errors about missing a password for ssh, or sometimes errors during bootstrapping. (Note that plugins for other
193
+ # cloud providers have similar sleeps.)
194
+ sleep(40)
195
+ end
196
+
197
+ def wait_for_ssh(hostname, ssh_port, interval_seconds, max_time_seconds)
198
+ print ui.color('Waiting for ssh access...', :magenta)
199
+
200
+ end_time = Time.now + max_time_seconds
201
+
202
+ begin
203
+ while Time.now < end_time
204
+ return true if can_ssh(hostname, ssh_port)
205
+
206
+ show_progress
207
+ sleep interval_seconds
208
+ end
209
+ ensure
210
+ end_progress_indicator
211
+ end
212
+
213
+ false
214
+ end
215
+
216
+ def can_ssh(hostname, ssh_port)
217
+ socket = TCPSocket.new(hostname, ssh_port)
218
+ # Wait up to 5 seconds.
219
+ readable = IO.select([socket], nil, nil, 5)
220
+ if readable
221
+ content = socket.gets
222
+ # Make sure some content was actually returned.
223
+ return true unless content.nil? || content.empty?
224
+ else
225
+ false
226
+ end
227
+ rescue SocketError, IOError, Errno::ETIMEDOUT, Errno::EPERM, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::ECONNRESET, Errno::ENOTCONN
228
+ false
229
+ ensure
230
+ socket && socket.close
231
+ end
232
+
233
+ def wait_for_instance_running(instance_id)
234
+ print ui.color('Waiting for instance to reach running state...', :magenta)
235
+
236
+ begin
237
+ response = compute_client.get_instance(instance_id).wait_until(:lifecycle_state,
238
+ OracleBMC::Core::Models::Instance::LIFECYCLE_STATE_RUNNING,
239
+ max_interval_seconds: 3) do |poll_response|
240
+ if poll_response.data.lifecycle_state == OracleBMC::Core::Models::Instance::LIFECYCLE_STATE_TERMINATED ||
241
+ poll_response.data.lifecycle_state == OracleBMC::Core::Models::Instance::LIFECYCLE_STATE_TERMINATING
242
+ throw :stop_succeed
243
+ end
244
+
245
+ show_progress
246
+ end
247
+ ensure
248
+ end_progress_indicator
249
+ end
250
+
251
+ if response.data.lifecycle_state != OracleBMC::Core::Models::Instance::LIFECYCLE_STATE_RUNNING
252
+ error_and_exit 'Instance failed to provision.'
253
+ end
254
+
255
+ response.data
256
+ end
257
+
258
+ # Return the first VNIC found (which should be the only VNIC).
259
+ def get_vnic(instance_id, compartment)
260
+ compute_client.list_vnic_attachments(compartment, instance_id: instance_id).each do |response|
261
+ response.data.each do |vnic_attachment|
262
+ return network_client.get_vnic(vnic_attachment.vnic_id).data
263
+ end
264
+ end
265
+ end
266
+
267
+ def merge_metadata
268
+ metadata = config[:metadata]
269
+
270
+ if metadata
271
+ begin
272
+ metadata = JSON.parse(metadata)
273
+ rescue JSON::ParserError
274
+ error_and_exit('Metadata value must be in JSON format. Example: \'{"key1":"value1", "key2":"value2"}\'')
275
+ end
276
+ else
277
+ metadata = {}
278
+ end
279
+
280
+ ssh_authorized_keys = get_file_content(:ssh_authorized_keys_file)
281
+ user_data = get_file_content(:user_data_file)
282
+ user_data = Base64.strict_encode64(user_data) if user_data
283
+
284
+ if ssh_authorized_keys
285
+ error_and_exit('Cannot specify ssh-authorized-keys as part of both --ssh-authorized-keys-file and --metadata.') if metadata.key? 'ssh_authorized_keys'
286
+ metadata['ssh_authorized_keys'] = ssh_authorized_keys
287
+ end
288
+
289
+ if user_data
290
+ error_and_exit('Cannot specify CloudInit user-data as part of both --user-data-file and --metadata.') if metadata.key? 'user_data'
291
+ metadata['user_data'] = user_data
292
+ end
293
+
294
+ metadata
295
+ end
296
+
297
+ def show_progress
298
+ print ui.color('.', :magenta)
299
+ $stdout.flush
300
+ end
301
+
302
+ def end_progress_indicator
303
+ print ui.color("done\n", :magenta)
304
+ end
305
+
306
+ def show_value(key, value, color = :cyan)
307
+ ui.msg "#{ui.color(key, color)}: #{value}" if value && !value.to_s.empty?
308
+ end
309
+
310
+ def get_file_content(file_name_param)
311
+ file_name = config[file_name_param]
312
+ return if file_name.nil?
313
+
314
+ file_name = File.expand_path(file_name)
315
+ File.open(file_name, 'r').read
316
+ end
317
+ end
318
+ end
319
+ end
@@ -0,0 +1,36 @@
1
+ # Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
2
+
3
+ require 'chef/knife'
4
+ require 'chef/knife/bmcs_common_options'
5
+ require 'chef/knife/bmcs_helper'
6
+
7
+ class Chef
8
+ class Knife
9
+ # List BMCS instances. Note that this list all instances in a compartment, not just those that are set up as Chef nodes.
10
+ class BmcsServerList < Knife
11
+ banner 'knife bmcs server list (options)'
12
+
13
+ include BmcsHelper
14
+ include BmcsCommonOptions
15
+
16
+ deps do
17
+ require 'oraclebmc'
18
+ end
19
+
20
+ option :limit,
21
+ long: '--limit LIMIT',
22
+ description: 'The maximum number of items to return.'
23
+
24
+ def run
25
+ options = {}
26
+ options[:limit] = config[:limit] if config[:limit]
27
+
28
+ response = compute_client.list_instances(compartment_id, options)
29
+
30
+ display_list(response, ['Display Name', 'ID', 'State']) do |item|
31
+ [item.display_name, item.id, item.lifecycle_state]
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,46 @@
1
+ # Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
2
+
3
+ require 'chef/knife'
4
+ require 'chef/knife/bmcs_common_options'
5
+ require 'chef/knife/bmcs_helper'
6
+
7
+ class Chef
8
+ class Knife
9
+ # List available shapes
10
+ class BmcsShapeList < Knife
11
+ banner 'knife bmcs shape list (options)'
12
+
13
+ include BmcsHelper
14
+ include BmcsCommonOptions
15
+
16
+ deps do
17
+ require 'oraclebmc'
18
+ end
19
+
20
+ option :availability_domain,
21
+ long: '--availability-domain AD',
22
+ description: 'The Availability Domain of the instance.'
23
+
24
+ option :image_id,
25
+ long: '--image-id IMAGE',
26
+ description: 'The OCID of the image used to boot the instance.'
27
+
28
+ option :limit,
29
+ long: '--limit LIMIT',
30
+ description: 'The maximum number of items to return.'
31
+
32
+ def run
33
+ options = {}
34
+ options[:availability_domain] = config[:availability_domain] if config[:availability_domain]
35
+ options[:image_id] = config[:image_id] if config[:image_id]
36
+ options[:limit] = config[:limit] if config[:limit]
37
+
38
+ response = compute_client.list_shapes(compartment_id, options)
39
+
40
+ display_list(response, []) do |item, list|
41
+ [item.shape] unless list.include? item.shape
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,7 @@
1
+ # Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
2
+
3
+ module Knife
4
+ module BMCS
5
+ VERSION = '1.0.0'.freeze
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-bmcs
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Oracle
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oraclebmc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ - - '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 1.2.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 1.2.1
33
+ description: ''
34
+ email:
35
+ - brian.gustafson@oracle.com
36
+ - joe.levy@oracle.com
37
+ executables: []
38
+ extensions: []
39
+ extra_rdoc_files: []
40
+ files:
41
+ - ./lib/chef/knife/bmcs_ad_list.rb
42
+ - ./lib/chef/knife/bmcs_common_options.rb
43
+ - ./lib/chef/knife/bmcs_helper.rb
44
+ - ./lib/chef/knife/bmcs_image_list.rb
45
+ - ./lib/chef/knife/bmcs_server_create.rb
46
+ - ./lib/chef/knife/bmcs_server_list.rb
47
+ - ./lib/chef/knife/bmcs_shape_list.rb
48
+ - ./lib/knife-bmcs/version.rb
49
+ - LICENSE.txt
50
+ homepage: https://github.com/oracle/knife-bmcs
51
+ licenses:
52
+ - UPL-1.0
53
+ - Apache-2.0
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - '>='
62
+ - !ruby/object:Gem::Version
63
+ version: 2.2.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 2.0.14
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Chef Knife Plugin for Oracle Bare Metal Cloud Services
75
+ test_files: []