knife-vcenter 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
4
+ # Copyright:: Copyright (c) 2017 Chef Software, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require 'chef/knife'
21
+ require 'chef/knife/cloud/list_resource_command'
22
+ require 'chef/knife/cloud/vcenter_service'
23
+ require 'chef/knife/cloud/vcenter_service_helpers'
24
+ require 'chef/knife/cloud/vcenter_service_options'
25
+
26
+ class Chef
27
+ class Knife
28
+ class Cloud
29
+ class VcenterClusterList < ResourceListCommand
30
+ include VcenterServiceHelpers
31
+ include VcenterServiceOptions
32
+
33
+ banner 'knife vcenter cluster list'
34
+
35
+ def before_exec_command
36
+ @columns_with_info = [
37
+ { label: 'ID', key: 'cluster' },
38
+ { label: 'Name', key: 'name' },
39
+ { label: 'DRS?', key: 'drs_enabled', value_callback: method(:format_boolean) },
40
+ { label: 'HA?', key: 'ha_enabled', value_callback: method(:format_boolean)},
41
+ ]
42
+
43
+ @sort_by_field = 'name'
44
+ end
45
+
46
+ def query_resource
47
+ # Call service to get the list of hosts from vcenter
48
+ service.list_clusters
49
+ end
50
+
51
+ def format_boolean(status)
52
+ status_color = status ? :green : :red
53
+ ui.color(status ? "True" : "False", status_color)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
4
+ # Copyright:: Copyright (c) 2017 Chef Software, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require 'chef/knife'
21
+ require 'chef/knife/cloud/list_resource_command'
22
+ require 'chef/knife/cloud/vcenter_service'
23
+ require 'chef/knife/cloud/vcenter_service_helpers'
24
+ require 'chef/knife/cloud/vcenter_service_options'
25
+
26
+ class Chef
27
+ class Knife
28
+ class Cloud
29
+ class VcenterDatacenterList < ResourceListCommand
30
+ include VcenterServiceHelpers
31
+ include VcenterServiceOptions
32
+
33
+ banner 'knife vcenter datacenter list'
34
+
35
+ def before_exec_command
36
+ @columns_with_info = [
37
+ { label: 'ID', key: 'datacenter' },
38
+ { label: 'Name', key: 'name' },
39
+ ]
40
+
41
+ @sort_by_field = 'name'
42
+ end
43
+
44
+ def query_resource
45
+ # Call service to get the list of hosts from vcenter
46
+ service.list_datacenters
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
4
+ # Copyright:: Copyright (c) 2017 Chef Software, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require 'chef/knife'
21
+ require 'chef/knife/cloud/list_resource_command'
22
+ require 'chef/knife/cloud/vcenter_service'
23
+ require 'chef/knife/cloud/vcenter_service_helpers'
24
+ require 'chef/knife/cloud/vcenter_service_options'
25
+
26
+ class Chef
27
+ class Knife
28
+ class Cloud
29
+ class VcenterHostList < ResourceListCommand
30
+ include VcenterServiceHelpers
31
+ include VcenterServiceOptions
32
+
33
+ banner 'knife vcenter host list'
34
+
35
+ def before_exec_command
36
+ @columns_with_info = [
37
+ { label: 'ID', key: 'host' },
38
+ { label: 'Name', key: 'name' },
39
+ { label: 'Power State', key: 'power_state', value_callback: method(:format_power_status) },
40
+ { label: 'Connection State', key: 'connection_state'},
41
+ ]
42
+
43
+ @sort_by_field = 'name'
44
+ end
45
+
46
+ def query_resource
47
+ # Call service to get the list of hosts from vcenter
48
+ service.list_hosts
49
+ end
50
+
51
+ def format_power_status(status)
52
+ status_check = status.value
53
+ status_color = case status_check
54
+ when 'POWERED_OFF'
55
+ :red
56
+ when 'POWERED_ON'
57
+ :green
58
+ when 'SUSPENDED'
59
+ :yellow
60
+ end
61
+
62
+ ui.color(status.value, status_color)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
4
+ # Copyright:: Copyright (c) 2017 Chef Software, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require 'chef/knife'
21
+ require 'chef/knife/cloud/server/create_command'
22
+ require 'chef/knife/cloud/server/create_options'
23
+ require 'chef/knife/cloud/vcenter_service'
24
+ require 'chef/knife/cloud/vcenter_service_helpers'
25
+ require 'chef/knife/cloud/vcenter_service_options'
26
+
27
+ class Chef
28
+ class Knife
29
+ class Cloud
30
+ class VcenterVmClone < Chef::Knife::Cloud::ServerCreateCommand
31
+ include VcenterServiceHelpers
32
+ include VcenterServiceOptions
33
+ include ServerCreateOptions
34
+
35
+ banner 'knife vcenter vm clone NAME'
36
+
37
+ option :template,
38
+ long: "--template NAME",
39
+ description: "Name of VM or template to use to clone machine from"
40
+
41
+ option :targethost,
42
+ long: "--targethost HOST",
43
+ description: "Host that the machine should be created on"
44
+
45
+ option :datacenter,
46
+ long: "--datacenter NAME",
47
+ description: "The datacenter for vSphere"
48
+
49
+ option :disable_power_on,
50
+ long: "--disable-power-on",
51
+ boolean: true,
52
+ default: false
53
+
54
+ option :folder,
55
+ long: "--folder NAME",
56
+ description: "Folder to deploy the new machine into"
57
+
58
+ def validate_params!
59
+ super
60
+
61
+ if @name_args.empty?
62
+ ui.error('You must provide the name of the new machine')
63
+ end
64
+
65
+ check_for_missing_config_values!(:template, :targethost, :datacenter)
66
+ end
67
+
68
+ def before_exec_command
69
+ super
70
+
71
+ @create_options = {
72
+ name: @name_args[0],
73
+ type: "clone",
74
+ template: locate_config_value(:template),
75
+ targethost: locate_config_value(:targethost),
76
+ datacenter: locate_config_value(:datacenter),
77
+ poweron: !locate_config_value(:disable_power_on),
78
+ folder: locate_config_value(:folder)
79
+ }
80
+
81
+ end
82
+
83
+ def before_bootstrap
84
+ super
85
+
86
+ config[:chef_node_name] = locate_config_value(:chef_node_name) ? locate_config_value(:chef_node_name) : server.name
87
+
88
+ # determine the IP address to use to bootstrap the machine with chef
89
+ config[:bootstrap_ip_address] = hostname_for_server
90
+ end
91
+
92
+ def hostname_for_server
93
+ ipaddress = service.ipaddress
94
+
95
+ ipaddress.nil? ? server.name : ipaddress
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
4
+ # Copyright:: Copyright (c) 2017 Chef Software, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require 'chef/knife'
21
+ require 'chef/knife/cloud/server/create_command'
22
+ require 'chef/knife/cloud/server/create_options'
23
+ require 'chef/knife/cloud/vcenter_service'
24
+ require 'chef/knife/cloud/vcenter_service_helpers'
25
+ require 'chef/knife/cloud/vcenter_service_options'
26
+
27
+ class Chef
28
+ class Knife
29
+ class Cloud
30
+ class VcenterVmCreate < Chef::Knife::Cloud::ServerCreateCommand
31
+ include VcenterServiceHelpers
32
+ include VcenterServiceOptions
33
+ include ServerCreateOptions
34
+
35
+ banner 'knife vcenter vm create NAME'
36
+
37
+ option :targethost,
38
+ long: "--targethost HOST",
39
+ description: "vCenter host on which the new VM should be created"
40
+
41
+ option :folder,
42
+ long: "--folder FOLDER",
43
+ description: "Folder in which the machine will reside"
44
+
45
+ option :datastore,
46
+ long: "--datastore DATASTORE",
47
+ description: "Datastore to be used for the disks etc"
48
+
49
+ option :resource_pool,
50
+ long: "--resource_pool RESOURCEPOOOL",
51
+ description: "Resource Pool to create the machine"
52
+
53
+
54
+ def validate_params!
55
+ super
56
+
57
+ if @name_args.empty?
58
+ ui.error('You must provide the name of the new machine')
59
+ end
60
+
61
+ check_for_missing_config_values!(:targethost, :datastore, :folder)
62
+ end
63
+
64
+ def before_exec_command
65
+ super
66
+
67
+ @create_options = {
68
+ name: @name_args[0],
69
+ type: "create",
70
+ targethost: locate_config_value(:targethost),
71
+ folder: locate_config_value(:folder),
72
+ datastore: locate_config_value(:datastore),
73
+ resource_pool: locate_config_value(:resource_pool)
74
+ }
75
+
76
+ puts @create_options
77
+ end
78
+
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
4
+ # Copyright:: Copyright (c) 2017 Chef Software, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require 'chef/knife'
21
+ require 'chef/knife/cloud/server/delete_options'
22
+ require 'chef/knife/cloud/server/delete_command'
23
+ require 'chef/knife/cloud/vcenter_service'
24
+ require 'chef/knife/cloud/vcenter_service_helpers'
25
+ require 'chef/knife/cloud/vcenter_service_options'
26
+
27
+ class Chef
28
+ class Knife
29
+ class Cloud
30
+ class VcenterVmDelete < ServerDeleteCommand
31
+ include ServerDeleteOptions
32
+ include VcenterServiceOptions
33
+ include VcenterServiceHelpers
34
+
35
+ banner 'knife vcenter vm delete NAME [NAME] (options)'
36
+
37
+ # rubocop:disable Style/GuardClause
38
+ def validate_params!
39
+ if @name_args.empty?
40
+ ui.error('You must supply the name of the virtual machine to delete.')
41
+ exit(1) if @name_args.empty?
42
+ end
43
+ end
44
+
45
+ def execute_command
46
+ @name_args.each do |name|
47
+ service.delete_vm(name)
48
+ delete_from_chef(name)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
4
+ # Copyright:: Copyright (c) 2017 Chef Software, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require 'chef/knife'
21
+ require 'chef/knife/cloud/server/list_command'
22
+ require 'chef/knife/cloud/server/list_options'
23
+ require 'chef/knife/cloud/vcenter_service'
24
+ require 'chef/knife/cloud/vcenter_service_helpers'
25
+ require 'chef/knife/cloud/vcenter_service_options'
26
+
27
+ class Chef
28
+ class Knife
29
+ class Cloud
30
+ class VcenterVmList < Chef::Knife::Cloud::ServerListCommand
31
+ include VcenterServiceHelpers
32
+ include VcenterServiceOptions
33
+
34
+ banner 'knife vcenter vm list'
35
+
36
+ def before_exec_command
37
+ @columns_with_info = [
38
+ { label: 'ID', key: 'vm' },
39
+ { label: 'Name', key: 'name' },
40
+ { label: 'Power State', key: 'power_state', value_callback: method(:format_power_status) },
41
+ { label: 'CPU Count', key: 'cpu_count'},
42
+ { label: 'RAM Size (MB)', key: 'memory_size_MiB', value_callback: method(:format_memory_value) },
43
+ ]
44
+
45
+ @sort_by_field = 'name'
46
+ end
47
+
48
+ def format_power_status(status)
49
+ status_check = status.value
50
+ status_color = case status_check
51
+ when 'POWERED_OFF'
52
+ :red
53
+ when 'POWERED_ON'
54
+ :green
55
+ when 'SUSPENDED'
56
+ :yellow
57
+ end
58
+
59
+ ui.color(status.value, status_color)
60
+ end
61
+
62
+ def format_memory_value(value)
63
+ value.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end