knife-vcair 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,59 @@
1
+ #
2
+ # Author:: Matt Ray (<matt@getchef.com>)
3
+ # Copyright:: Copyright (c) 2014 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/list_resource_command'
20
+ require 'chef/knife/vcair_helpers'
21
+ require 'chef/knife/cloud/vcair_service_options'
22
+
23
+ class Chef
24
+ class Knife
25
+ class Cloud
26
+ class VcairIpList < ResourceListCommand
27
+ include VcairHelpers
28
+ include VcairServiceOptions
29
+
30
+ banner "knife vcair ip list (options)"
31
+
32
+ def query_resource
33
+ # TODO: this doesn't work yet, this in inside the routers
34
+ org.networks
35
+ end
36
+
37
+ def before_exec_command
38
+ @columns_with_info = [
39
+ {:label => 'Name', :key => 'name'},
40
+ {:label => 'Gateway', :key => 'gateway'},
41
+ {:label => 'IP Range Start', :key => 'ip_ranges', :value_callback => method(:start_address) },
42
+ {:label => 'End', :key => 'ip_ranges', :value_callback => method(:end_address) },
43
+ {:label => 'Description', :key => 'description'}
44
+ ]
45
+ @sort_by_field = "name"
46
+ end
47
+
48
+ def start_address(ranges)
49
+ ranges[0][:start_address]
50
+ end
51
+
52
+ def end_address(ranges)
53
+ ranges[0][:end_address]
54
+ end
55
+
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,57 @@
1
+ #
2
+ # Author:: Matt Ray (<matt@getchef.com>)
3
+ # Copyright:: Copyright (c) 2014 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/list_resource_command'
20
+ require 'chef/knife/vcair_helpers'
21
+ require 'chef/knife/cloud/vcair_service_options'
22
+
23
+ class Chef
24
+ class Knife
25
+ class Cloud
26
+ class VcairNetworkList < ResourceListCommand
27
+ include VcairHelpers
28
+ include VcairServiceOptions
29
+
30
+ banner "knife vcair network list (options)"
31
+
32
+ def query_resource
33
+ org.networks
34
+ end
35
+
36
+ def before_exec_command
37
+ @columns_with_info = [
38
+ {:label => 'Name', :key => 'name'},
39
+ {:label => 'Gateway', :key => 'gateway'},
40
+ {:label => 'IP Range Start', :key => 'ip_ranges', :value_callback => method(:start_address) },
41
+ {:label => 'End', :key => 'ip_ranges', :value_callback => method(:end_address) },
42
+ {:label => 'Description', :key => 'description'}
43
+ ]
44
+ @sort_by_field = "name"
45
+ end
46
+
47
+ def start_address(ranges)
48
+ ranges[0][:start_address]
49
+ end
50
+
51
+ def end_address(ranges)
52
+ ranges[0][:end_address]
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,231 @@
1
+ #
2
+ # Author::
3
+ # Copyright::
4
+ #
5
+
6
+ require 'chef/knife/cloud/server/create_command'
7
+ require 'chef/knife/vcair_helpers'
8
+ require 'chef/knife/cloud/vcair_server_create_options'
9
+ require 'chef/knife/cloud/vcair_service'
10
+ require 'chef/knife/cloud/vcair_service_options'
11
+ require 'chef/knife/cloud/exceptions'
12
+
13
+ class Chef
14
+ class Knife
15
+ class Cloud
16
+ class VcairServerCreate < ServerCreateCommand
17
+ include VcairHelpers
18
+ include VcairServiceOptions
19
+ include VcairServerCreateOptions
20
+
21
+ deps do
22
+ require 'chef/knife/winrm_base'
23
+ require 'winrm'
24
+ require 'em-winrm'
25
+ require 'chef/json_compat'
26
+ require 'chef/knife/bootstrap'
27
+ require 'chef/knife/bootstrap_windows_winrm'
28
+ require 'chef/knife/core/windows_bootstrap_context'
29
+ require 'chef/knife/winrm'
30
+ Chef::Knife::Bootstrap.load_deps
31
+ end
32
+
33
+ banner "knife vcair server create (options)"
34
+
35
+ def execute_command
36
+ instantiate
37
+ update_customization
38
+ if config_value(:vcair_cpus)
39
+ vm.cpu = config_value(:vcair_cpus)
40
+ end
41
+ if config_value(:vcair_memory)
42
+ vm.memory = config_value(:vcair_memory)
43
+ end
44
+ update_network
45
+ vm.power_on
46
+ end
47
+
48
+ def set_image_os_type
49
+ # TODO: Pull Template VM OS Type
50
+ # config[:image_os_type] = (ami.platform == 'windows') ? 'windows' : 'linux'
51
+ end
52
+
53
+ # def before_exec_command
54
+ # @create_options = {
55
+ # :server_def => {
56
+ # :name => config[:chef_node_name],
57
+ # :image_ref => config_value(:image),
58
+
59
+ # },
60
+ # :server_create_timeout => config_value(:server_create_timeout)
61
+ # }
62
+
63
+ # if config_value(:vcair_custom_script)
64
+ # @create_options[:server_def].merge!({
65
+ # customization_script: config_value(:vcair_custom_script)})
66
+ # end
67
+ # if config_value(:vcair_cpus)
68
+ # @create_options[:server_def].merge!({:cpus => config_value(:vcair_cpus)})
69
+ # end
70
+ # if config_value(:vcair_memory)
71
+ # @create_options[:server_def].merge!({:memory => config_value(:vcair_memory)})
72
+ # end
73
+
74
+ # Chef::Log.debug("Create server params - server_def = #{@create_options[:server_def]}")
75
+ # @columns_with_info = []
76
+ # super
77
+ # end
78
+
79
+ # Setup the floating ip after server creation.
80
+ # def after_exec_command
81
+ # TODO: Add NAT rules optionally?
82
+
83
+ # Any action you want to perform post VM creation in your cloud.
84
+ # Example say assigning floating IP to the newly created VM.
85
+ # Make calls to "service" object if you need any information for cloud,
86
+ # example service.connection.addresses
87
+ # Make call to "server" object if you want set properties on newly created VM,
88
+ # example server.associate_address(floating_address)
89
+ # super
90
+ #end
91
+
92
+ def before_bootstrap
93
+ super
94
+ vm.reload
95
+ Chef::Log.debug("Bootstrap IP Address: #{bootstrap_ip_address}")
96
+ # If we start using auto_generated passwords, we might need this
97
+ # Chef::Config[:ssh_password] = vm.customization.admin_password
98
+ if bootstrap_ip_address.nil?
99
+ error_message = "No IP address available for bootstrapping."
100
+ ui.error(error_message)
101
+ raise CloudExceptions::BootstrapError, error_message
102
+ end
103
+ config[:bootstrap_ip_address] = bootstrap_ip_address
104
+ end
105
+
106
+ def validate_params!
107
+ # TODO: validate required params
108
+ super
109
+ errors = []
110
+ case config_value(:bootstrap_protocol)
111
+ when 'winrm'
112
+ password = config_value(:winrm_password)
113
+ errors << "WinRM requires a password on Vcair" unless password
114
+ batch_file = config_value(:vcair_customization_script)
115
+ if ::File.exists? batch_file
116
+ batch_contents = open(batch_file).read
117
+ if not batch_contents.match /#{password}/
118
+ errors << "WinRM customization script must set password"
119
+ end
120
+ else
121
+ errors << """
122
+ WinRM requires a customization_script on Vcair
123
+ The batch file should setup winrm and set the password
124
+ An example is available at:
125
+ https://raw.githubusercontent.com/vulk/knife-vchs/server-create/install-winrm-vcair-example.bat
126
+ """
127
+ end
128
+ when 'ssh'
129
+ errors << "SSH requires a password on Vcair" unless config_value(:ssh_password)
130
+ end
131
+
132
+ # errors << "your error message" if some_param_undefined
133
+ # TODO: Error out if windows users don't provide a password
134
+ # AND maybe even force a batch file and check that the password
135
+ # is in it
136
+ error_message = "We are very sorry that you will be unable to continue"
137
+ raise CloudExceptions::ValidationError, error_message if errors.each{|e| ui.error(e); error_message = "#{error_message} #{e}."}.any?
138
+ end
139
+
140
+ private
141
+
142
+ def instantiate
143
+ begin
144
+ node_name = config_value(:chef_node_name)
145
+ template.instantiate(
146
+ node_name,
147
+ vdc_id: vdc.id,
148
+ network_id: net.id,
149
+ description: "id:#{node_name}")
150
+ rescue CloudExceptions::ServerCreateError => e
151
+ raise e
152
+ end
153
+ end
154
+
155
+ def update_customization
156
+ ## Initialization before first power on.
157
+ c=vm.customization
158
+
159
+ if config_value(:vcair_customization_script)
160
+ c.script = open(config_value(:vcair_customization_script)).read
161
+ end
162
+
163
+ password = case config_value(:bootstrap_protocol)
164
+ when 'winrm'
165
+ config_value(:winrm_password)
166
+ when 'ssh'
167
+ config_value(:ssh_password)
168
+ end
169
+ if password
170
+ c.admin_password = password
171
+ c.admin_password_auto = false
172
+ c.reset_password_required = false
173
+ else
174
+ # Password will be autogenerated
175
+ c.admin_password_auto=true
176
+ # API will force password resets when auto is enabled
177
+ c.reset_password_required = true
178
+ end
179
+
180
+ # TODO: Add support for admin_auto_logon to fog
181
+ # c.admin_auto_logon_count = 100
182
+ # c.admin_auto_logon_enabled = true
183
+
184
+ # DNS and Windows want AlphaNumeric and dashes for hostnames
185
+ # Windows can only handle 15 character hostnames
186
+ c.computer_name = config_value(:chef_node_name).gsub(/\W/,"-").slice(0..14)
187
+ c.enabled = true
188
+ c.save
189
+ end
190
+
191
+ def update_network
192
+ ## TODO: allow user to specify network to connect to (see above net used)
193
+ # Define network connection for vm based on existing routed network
194
+ nc = vapp.network_config.find { |n| n if n[:networkName].match(net.name) }
195
+ networks_config = [nc]
196
+ section = {PrimaryNetworkConnectionIndex: 0}
197
+ section[:NetworkConnection] = networks_config.compact.each_with_index.map do |network, i|
198
+ connection = {
199
+ network: network[:networkName],
200
+ needsCustomization: true,
201
+ NetworkConnectionIndex: i,
202
+ IsConnected: true
203
+ }
204
+ ip_address = network[:ip_address]
205
+ ## TODO: support config options for allocation mode
206
+ #allocation_mode = network[:allocation_mode]
207
+ #allocation_mode = 'manual' if ip_address
208
+ #allocation_mode = 'dhcp' unless %w{dhcp manual pool}.include?(allocation_mode)
209
+ #allocation_mode = 'POOL'
210
+ #connection[:Dns1] = dns1 if dns1
211
+ allocation_mode = 'pool'
212
+ connection[:IpAddressAllocationMode] = allocation_mode.upcase
213
+ connection[:IpAddress] = ip_address if ip_address
214
+ connection
215
+ end
216
+
217
+ ## attach the network to the vm
218
+ nc_task = @service.connection.put_network_connection_system_section_vapp(
219
+ vm.id,section).body
220
+ @service.connection.process_task(nc_task)
221
+ end
222
+
223
+ def bootstrap_ip_address
224
+ vm.reload
225
+ vm.ip_address
226
+ end
227
+
228
+ end
229
+ end
230
+ end
231
+ end
@@ -0,0 +1,34 @@
1
+ #
2
+ # Author:: Matt Ray (<matt@getchef.com>)
3
+ # Copyright:: Copyright (c) 2014 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/vcair_vm_delete'
20
+ require 'chef/knife/cloud/vcair_service_options'
21
+
22
+ class Chef
23
+ class Knife
24
+ class Cloud
25
+ class VcairServerDelete < VcairVmDelete
26
+ include VcairServiceOptions
27
+ include ServerDeleteOptions
28
+
29
+ banner "knife vcair server delete INSTANCEID [INSTANCEID] (options)"
30
+
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,33 @@
1
+ #
2
+ # Author:: Seth Thomas (<sthomas@getchef.com>)
3
+ # Copyright:: Copyright (c) 2014 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/vcair_vm_list'
20
+ require 'chef/knife/cloud/vcair_service_options'
21
+
22
+ class Chef
23
+ class Knife
24
+ class Cloud
25
+ class VcairServerList < VcairVmList
26
+ include VcairServiceOptions
27
+
28
+ banner "knife vcair server list (options)"
29
+
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,37 @@
1
+ #
2
+ # Author::
3
+ # Copyright::
4
+ #
5
+
6
+ require 'chef/knife/cloud/server/show_command'
7
+ require 'chef/knife/vcair_helpers'
8
+ require 'chef/knife/cloud/server/show_options'
9
+ require 'chef/knife/cloud/vcair_service'
10
+ require 'chef/knife/cloud/vcair_service_options'
11
+ require 'chef/knife/cloud/exceptions'
12
+
13
+ class Chef
14
+ class Knife
15
+ class Cloud
16
+ class VcairServerShow < ServerShowCommand
17
+ include VcairHelpers
18
+ include VcairServiceOptions
19
+ include ServerShowOptions
20
+
21
+ banner "knife vcair server show (options)"
22
+
23
+ def before_exec_command
24
+ # TODO - Update the columns info with the keys and callbacks required as per fog object returned for your cloud. Framework looks for 'key' on your server object hash returned by fog. If you need the values to be formatted or if your value is another object that needs to be looked up use value_callback.
25
+
26
+ @columns_with_info = [
27
+ {:label => 'Instance ID', :key => 'id'},
28
+ {:label => 'Name', :key => 'name'},
29
+ #...
30
+ ]
31
+ super
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+ end