knife-vrealize 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,88 @@
1
+ #
2
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/knife/cloud/helpers'
20
+
21
+ class Chef
22
+ class Knife
23
+ class Cloud
24
+ module VraServiceHelpers
25
+ include Chef::Knife::Cloud::Helpers
26
+
27
+ def create_service_instance
28
+ Chef::Knife::Cloud::VraService.new(username: locate_config_value(:vra_username),
29
+ password: locate_config_value(:vra_password),
30
+ base_url: locate_config_value(:vra_base_url),
31
+ tenant: locate_config_value(:vra_tenant),
32
+ verify_ssl: verify_ssl?)
33
+ end
34
+
35
+ def verify_ssl?
36
+ !locate_config_value(:vra_disable_ssl_verify)
37
+ end
38
+
39
+ def wait_for_request(request, wait_time=600, refresh_rate=2)
40
+ print 'Waiting for request to complete.'
41
+
42
+ last_status = ''
43
+
44
+ begin
45
+ Timeout.timeout(wait_time) do
46
+ loop do
47
+ request.refresh
48
+
49
+ if request.completed?
50
+ print "\n"
51
+ break
52
+ end
53
+
54
+ if last_status == request.status
55
+ print '.'
56
+ else
57
+ last_status = request.status
58
+ print "\n"
59
+ print "Current request status: #{request.status}."
60
+ end
61
+
62
+ sleep refresh_rate
63
+ end
64
+ end
65
+ rescue Timeout::Error
66
+ ui.msg('')
67
+ ui.error("Request did not complete in #{wait_time} seconds. Check the Requests tab in the vRA UI for more information.")
68
+ exit 1
69
+ end
70
+ end
71
+
72
+ def validate!
73
+ check_for_missing_config_values!(:vra_username, :vra_password, :vra_base_url, :vra_tenant)
74
+ end
75
+
76
+ # rubocop:disable Style/GuardClause
77
+ def check_for_missing_config_values!(*keys)
78
+ missing = keys.select { |x| locate_config_value(x).nil? }
79
+
80
+ unless missing.empty?
81
+ ui.error("The following required parameters are missing: #{missing.join(', ')}")
82
+ exit(1)
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,54 @@
1
+ #
2
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ class Chef
20
+ class Knife
21
+ class Cloud
22
+ # rubocop:disable Style/AlignParameters
23
+ module VraServiceOptions
24
+ def self.included(includer)
25
+ includer.class_eval do
26
+ option :vra_base_url,
27
+ long: '--vra-base-url API_URL',
28
+ description: 'URL for the vRA server'
29
+
30
+ option :vra_username,
31
+ long: '--vra-username USERNAME',
32
+ description: 'Username to use with the vRA API'
33
+
34
+ option :vra_password,
35
+ long: '--vra-password PASSWORD',
36
+ description: 'Password to use with the vRA API'
37
+
38
+ option :vra_disable_ssl_verify,
39
+ long: '--vra-disable-ssl-verify',
40
+ description: 'Skip any SSL verification for the vRA API',
41
+ boolean: true,
42
+ default: false
43
+
44
+ option :request_refresh_rate,
45
+ long: '--request-refresh-rate SECS',
46
+ description: 'Number of seconds to sleep between each check of the request status, defaults to 2',
47
+ default: 2,
48
+ proc: proc { |secs| secs.to_i }
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,69 @@
1
+ #
2
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/knife'
20
+ require 'chef/knife/cloud/list_resource_command'
21
+ require 'chef/knife/cloud/vra_service'
22
+ require 'chef/knife/cloud/vra_service_helpers'
23
+ require 'chef/knife/cloud/vra_service_options'
24
+
25
+ class Chef
26
+ class Knife
27
+ class Cloud
28
+ class VraCatalogList < ResourceListCommand
29
+ include VraServiceHelpers
30
+ include VraServiceOptions
31
+
32
+ banner 'knife vra catalog list'
33
+
34
+ option :entitled,
35
+ long: '--entitled-only',
36
+ description: 'only list entitled vRA catalog entries',
37
+ boolean: true,
38
+ default: false
39
+
40
+ def before_exec_command
41
+ @columns_with_info = [
42
+ { label: 'Catalog ID', key: 'id' },
43
+ { label: 'Name', key: 'name' },
44
+ { label: 'Description', key: 'description' },
45
+ { label: 'Status', key: 'status', value_callback: method(:format_status_value) },
46
+ { label: 'Subtenant', key: 'subtenant_name' }
47
+ ]
48
+
49
+ @sort_by_field = 'name'
50
+ end
51
+
52
+ def query_resource
53
+ @service.list_catalog_items(locate_config_value(:entitled))
54
+ end
55
+
56
+ def format_status_value(status)
57
+ status = status.downcase
58
+ if status == 'published'
59
+ color = :green
60
+ else
61
+ color = :red
62
+ end
63
+
64
+ ui.color(status, color)
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,134 @@
1
+ #
2
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/knife'
20
+ require 'chef/knife/cloud/server/create_command'
21
+ require 'chef/knife/cloud/server/create_options'
22
+ require 'chef/knife/cloud/vra_service'
23
+ require 'chef/knife/cloud/vra_service_helpers'
24
+ require 'chef/knife/cloud/vra_service_options'
25
+
26
+ class Chef
27
+ class Knife
28
+ class Cloud
29
+ # rubocop:disable Style/BlockDelimiters
30
+ class VraServerCreate < ServerCreateCommand
31
+ include VraServiceHelpers
32
+ include VraServiceOptions
33
+ include ServerCreateOptions
34
+
35
+ banner 'knife vra server create CATALOG_ID (options)'
36
+
37
+ option :cpus,
38
+ long: '--cpus NUM_CPUS',
39
+ description: 'Number of CPUs the server should have'
40
+
41
+ option :memory,
42
+ long: '--memory RAM_IN_MB',
43
+ description: 'Amount of RAM, in MB, the server should have'
44
+
45
+ option :requested_for,
46
+ long: '--requested-for LOGIN',
47
+ description: 'The login to list as the owner of this resource. Will default to the vra_username parameter'
48
+
49
+ option :subtenant_id,
50
+ long: '--subtenant-id ID',
51
+ description: 'The subtenant ID (a.k.a "business group") to list as the owner of this resource. ' \
52
+ 'Will default to the blueprint subtenant if it exists.'
53
+
54
+ option :lease_days,
55
+ long: '--lease-days NUM_DAYS',
56
+ description: 'Number of days requested for the server lease, provided the blueprint allows this to be specified'
57
+
58
+ option :notes,
59
+ long: '--notes NOTES',
60
+ description: 'String of text to be included in the request notes.'
61
+
62
+ option :extra_params,
63
+ long: '--extra-param KEY=TYPE:VALUE',
64
+ description: 'Additional parameters to pass to vRA for this catalog request. TYPE must be "string" or "integer". ' \
65
+ 'Can be used multiple times.',
66
+ default: {},
67
+ proc: proc { |param|
68
+ Chef::Config[:knife][:vra_extra_params] ||= {}
69
+ key, value_str = param.split('=')
70
+ Chef::Config[:knife][:vra_extra_params].merge!(key => value_str)
71
+ }
72
+
73
+ def validate_params!
74
+ super
75
+
76
+ if @name_args.empty?
77
+ ui.error('You must supply a Catalog ID to use for your new server.')
78
+ exit 1
79
+ end
80
+
81
+ check_for_missing_config_values!(:cpus, :memory, :requested_for)
82
+
83
+ validate_extra_params!
84
+ end
85
+
86
+ def before_exec_command
87
+ super
88
+
89
+ @create_options = {
90
+ catalog_id: @name_args.first,
91
+ cpus: locate_config_value(:cpus),
92
+ memory: locate_config_value(:memory),
93
+ requested_for: locate_config_value(:requested_for),
94
+ subtenant_id: locate_config_value(:subtenant_id),
95
+ lease_days: locate_config_value(:lease_days),
96
+ notes: locate_config_value(:notes),
97
+ extra_params: extra_params,
98
+ wait_time: locate_config_value(:server_create_timeout),
99
+ refresh_rate: locate_config_value(:request_refresh_rate)
100
+ }
101
+ end
102
+
103
+ def before_bootstrap
104
+ super
105
+
106
+ config[:chef_node_name] = locate_config_value(:chef_node_name) ? locate_config_value(:chef_node_name) : server.name
107
+ config[:bootstrap_ip_address] = server.ip_addresses.first
108
+ end
109
+
110
+ def extra_params
111
+ return unless Chef::Config[:knife][:vra_extra_params]
112
+
113
+ params = []
114
+ Chef::Config[:knife][:vra_extra_params].each do |key, value_str|
115
+ type, value = value_str.split(':')
116
+ params << { key: key, type: type, value: value }
117
+ end
118
+
119
+ params
120
+ end
121
+
122
+ def validate_extra_params!
123
+ return if extra_params.nil?
124
+
125
+ extra_params.each do |param|
126
+ raise ArgumentError, "No type and value set for extra parameter #{param[:key]}" if param[:type].nil? || param[:value].nil?
127
+ raise ArgumentError, "Invalid parameter type for #{param[:key]} - must be string or integer" unless
128
+ param[:type] == 'string' || param[:type] == 'integer'
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,56 @@
1
+ #
2
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/knife'
20
+ require 'chef/knife/cloud/server/delete_options'
21
+ require 'chef/knife/cloud/server/delete_command'
22
+ require 'chef/knife/cloud/vra_service'
23
+ require 'chef/knife/cloud/vra_service_helpers'
24
+ require 'chef/knife/cloud/vra_service_options'
25
+
26
+ class Chef
27
+ class Knife
28
+ class Cloud
29
+ class VraServerDelete < ServerDeleteCommand
30
+ include ServerDeleteOptions
31
+ include VraServiceHelpers
32
+ include VraServiceOptions
33
+
34
+ banner 'knife vra server delete RESOURCE_ID [RESOURCE_ID] (options)'
35
+
36
+ # rubocop:disable Style/GuardClause
37
+ def validate_params!
38
+ if @name_args.empty?
39
+ ui.error('You must supply a resource ID of a server to delete.')
40
+ exit(1) if @name_args.empty?
41
+ end
42
+ end
43
+
44
+ # overriding this method from knife-cloud so we can pull the machine name
45
+ # to pass to delete_from_chef rather than the resource ID
46
+ def execute_command
47
+ @name_args.each do |resource_id|
48
+ server = service.get_server(resource_id)
49
+ service.delete_server(resource_id)
50
+ delete_from_chef(server.name)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,61 @@
1
+ #
2
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/knife'
20
+ require 'chef/knife/cloud/server/list_command'
21
+ require 'chef/knife/cloud/server/list_options'
22
+ require 'chef/knife/cloud/vra_service'
23
+ require 'chef/knife/cloud/vra_service_helpers'
24
+ require 'chef/knife/cloud/vra_service_options'
25
+
26
+ class Chef
27
+ class Knife
28
+ class Cloud
29
+ class VraServerList < ServerListCommand
30
+ include VraServiceHelpers
31
+ include VraServiceOptions
32
+
33
+ banner 'knife vra server list'
34
+
35
+ def before_exec_command
36
+ @columns_with_info = [
37
+ { label: 'Resource ID', key: 'id' },
38
+ { label: 'Name', key: 'name' },
39
+ { label: 'Status', key: 'status', value_callback: method(:format_status_value) },
40
+ { label: 'Catalog Name', key: 'catalog_name' }
41
+ ]
42
+
43
+ @sort_by_field = 'name'
44
+ end
45
+
46
+ def format_status_value(status)
47
+ status = status.downcase
48
+ status_color = case status
49
+ when 'active'
50
+ :green
51
+ when 'deleted'
52
+ :red
53
+ else
54
+ :yellow
55
+ end
56
+ ui.color(status, status_color)
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end