knife-esx 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +201 -0
- data/README.rdoc +75 -0
- data/features/esx.feature +68 -0
- data/features/foo +0 -0
- data/features/step_definitions/steps.rb +0 -0
- data/features/support/env.rb +11 -0
- data/knife-esx.gemspec +24 -0
- data/lib/chef/knife/esx_base.rb +81 -0
- data/lib/chef/knife/esx_vm_create.rb +222 -0
- data/lib/chef/knife/esx_vm_delete.rb +57 -0
- data/lib/chef/knife/esx_vm_list.rb +41 -0
- data/lib/knife-esx/version.rb +6 -0
- metadata +97 -0
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.rdoc
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
= Knife ESX
|
2
|
+
|
3
|
+
= DESCRIPTION:
|
4
|
+
|
5
|
+
This is the unofficial Opscode Knife plugin for ESX. This plugin gives knife the ability to create, bootstrap, and manage virtual machines in a VMWare ESX/ESXi host.
|
6
|
+
|
7
|
+
You'll need an ESX(i)4/5 host with SSH enabled and a privileged user account to connect to it via SSH:
|
8
|
+
|
9
|
+
You'll also need a base VM template (a VMDK in fact) with CentOS/Ubuntu/Fedora with vmware-tools installed.
|
10
|
+
|
11
|
+
http://github.com/rubiojr/knife-esx
|
12
|
+
|
13
|
+
= INSTALLATION:
|
14
|
+
|
15
|
+
Be sure you are running the latest version Chef. Versions earlier than 0.10.0 don't support plugins:
|
16
|
+
|
17
|
+
gem install chef
|
18
|
+
|
19
|
+
This plugin is distributed as a Ruby Gem. To install it, run:
|
20
|
+
|
21
|
+
gem install knife-esx
|
22
|
+
|
23
|
+
Depending on your system's configuration, you may need to run this command with root privileges.
|
24
|
+
|
25
|
+
= CONFIGURATION:
|
26
|
+
|
27
|
+
In order to communicate with the ESX Cloud API you will have to tell Knife about your Username and API Key. The easiest way to accomplish this is to create some entries in your <tt>knife.rb</tt> file:
|
28
|
+
|
29
|
+
knife[:esx_host] = "Your ESX host address"
|
30
|
+
knife[:esx_username] = "root"
|
31
|
+
knife[:esx_password] = "secret"
|
32
|
+
|
33
|
+
You also have the option of passing your ESX Host/Username/Password into the individual knife subcommands using the <tt>--esx-host</tt> <tt>--esx-username</tt> and <tt>--esx-password</tt> command options
|
34
|
+
|
35
|
+
# provision a new Ubuntu 11.10 VM
|
36
|
+
knife esx vm create --template-file ~/.chef/bootstrap/ubuntu11.10-gems.erb \
|
37
|
+
--vm-disk /path-to/ubuntu1110-x64-vmware-tools.vmdk \
|
38
|
+
--vm-name knife-esx-test-ubuntu \
|
39
|
+
--datastore datastore1
|
40
|
+
--esx-host my-test-host \
|
41
|
+
--esx-password secret
|
42
|
+
|
43
|
+
= SUBCOMMANDS:
|
44
|
+
|
45
|
+
This plugin provides the following Knife subcommands. Specific command options can be found by invoking the subcommand with a <tt>--help</tt> flag
|
46
|
+
|
47
|
+
== knife esx vm create
|
48
|
+
|
49
|
+
Provisions a new virtual machine in the ESX host and then perform a Chef bootstrap (using the SSH protocol). The goal of the bootstrap is to get Chef installed on the target system so it can run Chef Client with a Chef Server. The main assumption is a baseline OS installation exists (provided by the provisioning). It is primarily intended for Chef Client systems that talk to a Chef server. By default the virtual machine is bootstrapped using the {ubuntu10.04-gems}[https://github.com/opscode/chef/blob/master/chef/lib/chef/knife/bootstrap/ubuntu10.04-gems.erb] template. This can be overridden using the <tt>-d</tt> or <tt>--template-file</tt> command options.
|
50
|
+
|
51
|
+
== knife esx vm delete
|
52
|
+
|
53
|
+
Deletes an existing virtual machine in the currently configured ESX host by the virtual machine name. You can find the instance id by entering 'knife esx vm list'. Please note - this does not delete the associated node and client objects from the Chef server.
|
54
|
+
|
55
|
+
== knife esx vm list
|
56
|
+
|
57
|
+
Outputs a list of all virtual machines in the currently configured ESX host. Please note - this shows all the virtual machines available in the ESX host, some of which may not be currently managed by the Chef server.
|
58
|
+
|
59
|
+
= LICENSE:
|
60
|
+
|
61
|
+
Author:: Sergio Rubio (<rubiojr@frameos.org>)
|
62
|
+
Copyright:: Copyright (c) 2011 Sergio Rubio
|
63
|
+
License:: Apache License, Version 2.0
|
64
|
+
|
65
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
66
|
+
you may not use this file except in compliance with the License.
|
67
|
+
You may obtain a copy of the License at
|
68
|
+
|
69
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
70
|
+
|
71
|
+
Unless required by applicable law or agreed to in writing, software
|
72
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
73
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
74
|
+
See the License for the specific language governing permissions and
|
75
|
+
limitations under the License.
|
@@ -0,0 +1,68 @@
|
|
1
|
+
Feature: knife esx
|
2
|
+
|
3
|
+
Scenario: Run esx create vm without arguments
|
4
|
+
When I run `knife esx vm create`
|
5
|
+
Then the output should contain:
|
6
|
+
"""
|
7
|
+
ERROR: You have not provided a valid VMDK file. (--vm-disk)
|
8
|
+
"""
|
9
|
+
Scenario: Print help
|
10
|
+
When I run `knife esx --help`
|
11
|
+
Then the output should contain "Available esx subcommands:"
|
12
|
+
|
13
|
+
Scenario: Unknown esx create sub-command
|
14
|
+
When I run `knife esx create foobar`
|
15
|
+
Then the output should contain "FATAL: Cannot find sub command for:"
|
16
|
+
|
17
|
+
Scenario: Invalid VMDK file
|
18
|
+
When I run `knife esx vm create --vm-disk /tmp/foo9098jfj`
|
19
|
+
Then the output should contain "Invalid VMDK disk file (--vm-disk)"
|
20
|
+
|
21
|
+
Scenario: Missing VM name
|
22
|
+
When I run `knife esx vm create --vm-disk /tmp/test.vmdk`
|
23
|
+
Then the output should contain "ERROR: Invalid Virtual Machine name (--vm-name)"
|
24
|
+
|
25
|
+
Scenario: Missing Host option
|
26
|
+
When I run `knife esx vm create --vm-disk /tmp/test.vmdk --vm-name knife-esx-test`
|
27
|
+
Then the output should contain "ERROR: RuntimeError: host option required"
|
28
|
+
|
29
|
+
Scenario: Invalid ESX host
|
30
|
+
When I run `knife esx vm create --vm-disk /tmp/m0n0wall-stream.vmdk --vm-name knife-esx-test --esx-host localhost --esx-password temporal --datastore datastore1`
|
31
|
+
Then the output should contain "ERROR: Network Error: Connection refused"
|
32
|
+
|
33
|
+
Scenario: Invalid ESX password
|
34
|
+
When I run `knife esx vm create --vm-disk /tmp/m0n0wall-stream.vmdk --vm-name knife-esx-test --esx-host esx-test-host --esx-password oiusdf`
|
35
|
+
Then the output should contain "ERROR: RbVmomi::Fault: InvalidLogin: Cannot complete login due to an incorrect user name or password"
|
36
|
+
|
37
|
+
Scenario: Unknown host
|
38
|
+
When I run `knife esx vm create --vm-disk /tmp/m0n0wall-stream.vmdk --vm-name knife-esx-test --esx-host lllocalhost --esx-password temporal --datastore datastore1`
|
39
|
+
Then the output should contain "ERROR: Network Error: getaddrinfo: Name or service not known"
|
40
|
+
|
41
|
+
Scenario: Missing VMWare Tools in server
|
42
|
+
When I run `knife esx vm create --vm-disk /tmp/m0n0wall-stream.vmdk --vm-name knife-esx-test --esx-host esx-test-host --esx-password temporal --datastore datastore1`
|
43
|
+
Then the output should contain "Timeout trying to reach the VM. Does it have vmware-tools installed?"
|
44
|
+
|
45
|
+
Scenario: Existing VM disk in host
|
46
|
+
When I run `knife esx vm create --vm-disk /tmp/m0n0wall-stream.vmdk --vm-name knife-esx-test --esx-host esx-test-host --esx-password temporal --datastore datastore1`
|
47
|
+
Then the output should contain "ERROR: Exception: Destination file"
|
48
|
+
|
49
|
+
Scenario: Delete Existing VM
|
50
|
+
When I run `knife esx vm delete --force-delete yes knife-esx-test --esx-host esx-test-host --esx-password temporal`
|
51
|
+
Then the output should contain "WARNING: Deleted virtual machine knife-esx-test"
|
52
|
+
|
53
|
+
Scenario: Delete non-existent VM
|
54
|
+
When I run `knife esx vm delete --force-delete yes knife-esx-test --esx-host esx-test-host --esx-password temporal`
|
55
|
+
Then the output should contain "WARNING: Virtual Machine knife-esx-test not found"
|
56
|
+
|
57
|
+
@announce
|
58
|
+
Scenario: Success bootstraping VM
|
59
|
+
When I run `knife esx vm create --template-file /home/rubiojr/.chef/bootstrap/ubuntu11.10-gems.erb --vm-disk /home/rubiojr/tmp/ubuntu1110-x64-vmware-tools.vmdk --vm-name knife-esx-test-ubuntu --esx-host esx-test-host --esx-password temporal --datastore datastore1 --no-host-key-verify`
|
60
|
+
Then the output should contain "Done!"
|
61
|
+
|
62
|
+
Scenario: List virtual machine
|
63
|
+
When I run `knife esx vm list --esx-host esx-test-host --esx-password temporal`
|
64
|
+
Then the output should contain "knife-esx-test-ubuntu"
|
65
|
+
|
66
|
+
Scenario: Delete Existing VM
|
67
|
+
When I run `knife esx vm delete --force-delete yes knife-esx-test-ubuntu --esx-host esx-test-host --esx-password temporal`
|
68
|
+
Then the output should contain "WARNING: Deleted virtual machine knife-esx-test-ubuntu"
|
data/features/foo
ADDED
File without changes
|
File without changes
|
data/knife-esx.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "knife-esx/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "knife-esx"
|
7
|
+
s.version = Knife::ESX::VERSION
|
8
|
+
s.has_rdoc = true
|
9
|
+
s.authors = ["Sergio Rubio"]
|
10
|
+
s.email = ["rubiojr@frameos.org","rubiojr@frameos.org"]
|
11
|
+
s.homepage = "http://github.com/rubiojr/knife-esx"
|
12
|
+
s.summary = "ESX Support for Chef's Knife Command"
|
13
|
+
s.description = s.summary
|
14
|
+
s.extra_rdoc_files = ["README.rdoc", "LICENSE" ]
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.add_dependency "esx", ">= 0.2"
|
20
|
+
s.add_dependency "terminal-table"
|
21
|
+
s.add_dependency "chef", ">= 0.10"
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Seth Chisamore (<schisamo@opscode.com>)
|
3
|
+
# Author:: Sergio Rubio (<rubiojr@frameos.org>)
|
4
|
+
# Copyright:: Copyright (c) 2011 Opscode, Inc.
|
5
|
+
# Copyright:: Sergio Rubio (c) 2011
|
6
|
+
# License:: Apache License, Version 2.0
|
7
|
+
#
|
8
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
+
# you may not use this file except in compliance with the License.
|
10
|
+
# You may obtain a copy of the License at
|
11
|
+
#
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
13
|
+
#
|
14
|
+
# Unless required by applicable law or agreed to in writing, software
|
15
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
17
|
+
# See the License for the specific language governing permissions and
|
18
|
+
# limitations under the License.
|
19
|
+
#
|
20
|
+
|
21
|
+
require 'chef/knife'
|
22
|
+
|
23
|
+
class Chef
|
24
|
+
class Knife
|
25
|
+
module ESXBase
|
26
|
+
|
27
|
+
# :nodoc:
|
28
|
+
# Would prefer to do this in a rational way, but can't be done b/c of
|
29
|
+
# Mixlib::CLI's design :(
|
30
|
+
def self.included(includer)
|
31
|
+
includer.class_eval do
|
32
|
+
|
33
|
+
deps do
|
34
|
+
require 'net/ssh/multi'
|
35
|
+
require 'esx'
|
36
|
+
require 'readline'
|
37
|
+
require 'chef/json_compat'
|
38
|
+
require 'terminal-table/import'
|
39
|
+
end
|
40
|
+
|
41
|
+
option :esx_password,
|
42
|
+
:long => "--esx-password PASSWORD",
|
43
|
+
:description => "Your ESX password",
|
44
|
+
:proc => Proc.new { |key| Chef::Config[:knife][:esx_password] = key }
|
45
|
+
|
46
|
+
option :esx_username,
|
47
|
+
:long => "--esx-username USERNAME",
|
48
|
+
:default => "root",
|
49
|
+
:description => "Your ESX username (default 'root')",
|
50
|
+
:proc => Proc.new { |username| Chef::Config[:knife][:esx_username] = (username || 'root') }
|
51
|
+
|
52
|
+
option :esx_host,
|
53
|
+
:long => "--esx-host ADDRESS",
|
54
|
+
:description => "Your ESX host address",
|
55
|
+
:default => "127.0.0.1",
|
56
|
+
:proc => Proc.new { |host| Chef::Config[:knife][:esx_host] = host }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def connection
|
61
|
+
Chef::Config[:knife][:esx_username] = 'root' if not Chef::Config[:knife][:esx_username]
|
62
|
+
if not @connection
|
63
|
+
ui.info "#{ui.color("Connecting to ESX host #{config[:esx_host]}... ", :magenta)}"
|
64
|
+
@connection = ESX::Host.connect(Chef::Config[:knife][:esx_host],
|
65
|
+
Chef::Config[:knife][:esx_username],
|
66
|
+
Chef::Config[:knife][:esx_password])
|
67
|
+
else
|
68
|
+
@connection
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def locate_config_value(key)
|
73
|
+
key = key.to_sym
|
74
|
+
Chef::Config[:knife][key] || config[key]
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
|
@@ -0,0 +1,222 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Adam Jacob (<adam@opscode.com>)
|
3
|
+
# Copyright:: Copyright (c) 2009 Opscode, 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/brightbox_base'
|
20
|
+
|
21
|
+
class Chef
|
22
|
+
class Knife
|
23
|
+
class EsxVmCreate < Knife
|
24
|
+
|
25
|
+
include Knife::ESXBase
|
26
|
+
|
27
|
+
deps do
|
28
|
+
require 'readline'
|
29
|
+
require 'esx'
|
30
|
+
require 'chef/json_compat'
|
31
|
+
require 'chef/knife/bootstrap'
|
32
|
+
Chef::Knife::Bootstrap.load_deps
|
33
|
+
end
|
34
|
+
|
35
|
+
banner "knife esx vm create (options)"
|
36
|
+
|
37
|
+
option :vm_disk,
|
38
|
+
:long => "--vm-disk FILE",
|
39
|
+
:description => "The path to the VMDK disk file"
|
40
|
+
|
41
|
+
option :vm_name,
|
42
|
+
:long => "--vm-name NAME",
|
43
|
+
:description => "The Virtual Machine name"
|
44
|
+
|
45
|
+
option :datastore,
|
46
|
+
:long => "--datastore NAME",
|
47
|
+
:default => 'datastore1',
|
48
|
+
:description => "The Datastore to use for the VM files (default: datastore1)"
|
49
|
+
|
50
|
+
option :guest_id,
|
51
|
+
:long => "--guest-id NAME",
|
52
|
+
:default => "otherGuest",
|
53
|
+
:description => "The VM GuestID (default: otherGuest)"
|
54
|
+
|
55
|
+
option :memory,
|
56
|
+
:long => "--vm-memory MEM",
|
57
|
+
:default => "512",
|
58
|
+
:description => "The VM memory in MB (default: 512)"
|
59
|
+
|
60
|
+
option :chef_node_name,
|
61
|
+
:short => "-N NAME",
|
62
|
+
:long => "--node-name NAME",
|
63
|
+
:description => "The Chef node name for your new node"
|
64
|
+
|
65
|
+
option :prerelease,
|
66
|
+
:long => "--prerelease",
|
67
|
+
:description => "Install the pre-release chef gems"
|
68
|
+
|
69
|
+
option :bootstrap_version,
|
70
|
+
:long => "--bootstrap-version VERSION",
|
71
|
+
:description => "The version of Chef to install",
|
72
|
+
:proc => Proc.new { |v| Chef::Config[:knife][:bootstrap_version] = v }
|
73
|
+
|
74
|
+
option :distro,
|
75
|
+
:short => "-d DISTRO",
|
76
|
+
:long => "--distro DISTRO",
|
77
|
+
:description => "Bootstrap a distro using a template; default is 'ubuntu10.04-gems'",
|
78
|
+
:proc => Proc.new { |d| Chef::Config[:knife][:distro] = d },
|
79
|
+
:default => "ubuntu10.04-gems"
|
80
|
+
|
81
|
+
option :template_file,
|
82
|
+
:long => "--template-file TEMPLATE",
|
83
|
+
:description => "Full path to location of template to use",
|
84
|
+
:proc => Proc.new { |t| Chef::Config[:knife][:template_file] = t },
|
85
|
+
:default => false
|
86
|
+
|
87
|
+
option :run_list,
|
88
|
+
:short => "-r RUN_LIST",
|
89
|
+
:long => "--run-list RUN_LIST",
|
90
|
+
:description => "Comma separated list of roles/recipes to apply",
|
91
|
+
:proc => lambda { |o| o.split(/[\s,]+/) },
|
92
|
+
:default => []
|
93
|
+
|
94
|
+
option :ssh_user,
|
95
|
+
:short => "-x USERNAME",
|
96
|
+
:long => "--ssh-user USERNAME",
|
97
|
+
:description => "The ssh username; default is 'root'",
|
98
|
+
:default => "root"
|
99
|
+
|
100
|
+
option :ssh_password,
|
101
|
+
:short => "-P PASSWORD",
|
102
|
+
:long => "--ssh-password PASSWORD",
|
103
|
+
:description => "The ssh password"
|
104
|
+
|
105
|
+
option :identity_file,
|
106
|
+
:short => "-i IDENTITY_FILE",
|
107
|
+
:long => "--identity-file IDENTITY_FILE",
|
108
|
+
:description => "The SSH identity file used for authentication"
|
109
|
+
|
110
|
+
option :no_host_key_verify,
|
111
|
+
:long => "--no-host-key-verify",
|
112
|
+
:description => "Disable host key verification",
|
113
|
+
:boolean => true,
|
114
|
+
:default => false
|
115
|
+
|
116
|
+
def tcp_test_ssh(hostname)
|
117
|
+
tcp_socket = TCPSocket.new(hostname, 22)
|
118
|
+
readable = IO.select([tcp_socket], nil, nil, 5)
|
119
|
+
if readable
|
120
|
+
Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
|
121
|
+
yield
|
122
|
+
true
|
123
|
+
else
|
124
|
+
false
|
125
|
+
end
|
126
|
+
rescue Errno::ETIMEDOUT, Errno::EPERM
|
127
|
+
false
|
128
|
+
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH
|
129
|
+
sleep 2
|
130
|
+
false
|
131
|
+
ensure
|
132
|
+
tcp_socket && tcp_socket.close
|
133
|
+
end
|
134
|
+
|
135
|
+
def run
|
136
|
+
$stdout.sync = true
|
137
|
+
|
138
|
+
unless config[:vm_disk]
|
139
|
+
ui.error("You have not provided a valid VMDK file. (--vm-disk)")
|
140
|
+
exit 1
|
141
|
+
end
|
142
|
+
|
143
|
+
if not File.exist?(config[:vm_disk])
|
144
|
+
ui.error("Invalid VMDK disk file (--vm-disk)")
|
145
|
+
exit 1
|
146
|
+
end
|
147
|
+
|
148
|
+
vm_name = config[:vm_name]
|
149
|
+
if not vm_name
|
150
|
+
ui.error("Invalid Virtual Machine name (--vm-name)")
|
151
|
+
exit 1
|
152
|
+
end
|
153
|
+
|
154
|
+
datastore = config[:datastore]
|
155
|
+
memory = config[:memory]
|
156
|
+
vm_disk = config[:vm_disk]
|
157
|
+
guest_id =config[:guest_id]
|
158
|
+
destination_path = "/vmfs/volumes/#{datastore}/#{vm_name}"
|
159
|
+
|
160
|
+
connection.remote_command "mkdir #{destination_path}"
|
161
|
+
puts "#{ui.color("Creating VM... ", :magenta)}"
|
162
|
+
puts "#{ui.color("Importing VM disk... ", :magenta)}"
|
163
|
+
connection.import_disk vm_disk, destination_path + "/#{vm_name}.vmdk"
|
164
|
+
vm = connection.create_vm :vm_name => vm_name,
|
165
|
+
:datastore => datastore,
|
166
|
+
:disk_file => "#{vm_name}/#{vm_name}.vmdk",
|
167
|
+
:memory => memory,
|
168
|
+
:guest_id => guest_id
|
169
|
+
vm.power_on
|
170
|
+
puts " done \n"
|
171
|
+
|
172
|
+
puts "#{ui.color("VM Name", :cyan)}: #{vm.name}"
|
173
|
+
puts "#{ui.color("VM Memory", :cyan)}: #{(vm.memory_size.to_f/1024/1024).round} MB"
|
174
|
+
|
175
|
+
# wait for it to be ready to do stuff
|
176
|
+
print "\n#{ui.color("Waiting server... ", :magenta)}"
|
177
|
+
timeout = 100
|
178
|
+
found = connection.virtual_machines.find { |v| v.name == vm.name }
|
179
|
+
loop do
|
180
|
+
break if found.guest_info.vmware_tools_installed?
|
181
|
+
timeout -= 1
|
182
|
+
if timeout == 0
|
183
|
+
ui.error "Timeout trying to reach the VM. Does it have vmware-tools installed?"
|
184
|
+
exit 1
|
185
|
+
end
|
186
|
+
sleep 1
|
187
|
+
found = connection.virtual_machines.find { |v| v.name == vm.name }
|
188
|
+
end
|
189
|
+
|
190
|
+
print "\n#{ui.color("Waiting for sshd... ", :magenta)}"
|
191
|
+
print(".") until tcp_test_ssh(vm.ip_address) { sleep @initial_sleep_delay ||= 10; puts(" done") }
|
192
|
+
bootstrap_for_node(vm).run
|
193
|
+
|
194
|
+
puts "\n"
|
195
|
+
puts "#{ui.color("Name", :cyan)}: #{vm.name}"
|
196
|
+
puts "#{ui.color("IP Address", :cyan)}: #{vm.ip_address}"
|
197
|
+
puts "#{ui.color("Environment", :cyan)}: #{config[:environment] || '_default'}"
|
198
|
+
puts "#{ui.color("Run List", :cyan)}: #{config[:run_list].join(', ')}"
|
199
|
+
puts "#{ui.color("Done!", :green)}"
|
200
|
+
end
|
201
|
+
|
202
|
+
def bootstrap_for_node(vm)
|
203
|
+
bootstrap = Chef::Knife::Bootstrap.new
|
204
|
+
bootstrap.name_args = [vm.ip_address]
|
205
|
+
bootstrap.config[:run_list] = config[:run_list]
|
206
|
+
bootstrap.config[:ssh_user] = config[:ssh_user]
|
207
|
+
bootstrap.config[:identity_file] = config[:identity_file]
|
208
|
+
bootstrap.config[:chef_node_name] = config[:chef_node_name] || vm.name
|
209
|
+
bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
|
210
|
+
bootstrap.config[:distro] = locate_config_value(:distro)
|
211
|
+
# bootstrap will run as root...sudo (by default) also messes up Ohai on CentOS boxes
|
212
|
+
bootstrap.config[:use_sudo] = true unless config[:ssh_user] == 'root'
|
213
|
+
bootstrap.config[:template_file] = locate_config_value(:template_file)
|
214
|
+
bootstrap.config[:environment] = config[:environment]
|
215
|
+
bootstrap.config[:no_host_key_verify] = config[:no_host_key_verify]
|
216
|
+
bootstrap.config[:ssh_password] = config[:ssh_password]
|
217
|
+
bootstrap
|
218
|
+
end
|
219
|
+
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Adam Jacob (<adam@opscode.com>)
|
3
|
+
# Copyright:: Copyright (c) 2009 Opscode, 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/esx_base'
|
20
|
+
|
21
|
+
class Chef
|
22
|
+
class Knife
|
23
|
+
class EsxVmDelete < Knife
|
24
|
+
|
25
|
+
include Knife::ESXBase
|
26
|
+
|
27
|
+
banner "knife esx vm delete VM_NAME [VM_NAME] (options)"
|
28
|
+
|
29
|
+
option :force_delete,
|
30
|
+
:long => "--force-delete NO",
|
31
|
+
:default => 'no',
|
32
|
+
:description => "Do not confirm VM deletion when yes"
|
33
|
+
|
34
|
+
def run
|
35
|
+
deleted = []
|
36
|
+
connection.virtual_machines.each do |vm|
|
37
|
+
@name_args.each do |vm_name|
|
38
|
+
if vm_name == vm.name
|
39
|
+
if config[:force_delete] != 'yes'
|
40
|
+
confirm("Do you really want to delete this virtual machine '#{vm.name}'")
|
41
|
+
end
|
42
|
+
|
43
|
+
vm.power_off if (vm.name =~ /#{vm.name}/ and vm.power_state == 'poweredOn')
|
44
|
+
vm.destroy
|
45
|
+
deleted << vm_name
|
46
|
+
ui.warn("Deleted virtual machine #{vm.name}")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
@name_args.each do |vm_name|
|
51
|
+
ui.warn "Virtual Machine #{vm_name} not found" if not deleted.include?(vm_name)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Adam Jacob (<adam@opscode.com>)
|
3
|
+
# Copyright:: Copyright (c) 2009 Opscode, 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/esx_base'
|
20
|
+
|
21
|
+
class Chef
|
22
|
+
class Knife
|
23
|
+
class EsxVmList < Knife
|
24
|
+
|
25
|
+
include Knife::ESXBase
|
26
|
+
|
27
|
+
banner "knife esx vm list (options)"
|
28
|
+
|
29
|
+
def run
|
30
|
+
$stdout.sync = true
|
31
|
+
vm_table = table do |t|
|
32
|
+
t.headings = %w{NAME IPADDR POWER_STATE VMW_TOOLS}
|
33
|
+
connection.virtual_machines.each do |vm|
|
34
|
+
t << [vm.name, vm.ip_address, vm.power_state, vm.guest_info.vmware_tools_installed?]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
puts vm_table
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: knife-esx
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sergio Rubio
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: esx
|
16
|
+
requirement: &12269740 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.2'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *12269740
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: terminal-table
|
27
|
+
requirement: &12265640 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *12265640
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: chef
|
38
|
+
requirement: &11559560 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0.10'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *11559560
|
47
|
+
description: ESX Support for Chef's Knife Command
|
48
|
+
email:
|
49
|
+
- rubiojr@frameos.org
|
50
|
+
- rubiojr@frameos.org
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files:
|
54
|
+
- README.rdoc
|
55
|
+
- LICENSE
|
56
|
+
files:
|
57
|
+
- LICENSE
|
58
|
+
- README.rdoc
|
59
|
+
- features/esx.feature
|
60
|
+
- features/foo
|
61
|
+
- features/step_definitions/steps.rb
|
62
|
+
- features/support/env.rb
|
63
|
+
- knife-esx.gemspec
|
64
|
+
- lib/chef/knife/esx_base.rb
|
65
|
+
- lib/chef/knife/esx_vm_create.rb
|
66
|
+
- lib/chef/knife/esx_vm_delete.rb
|
67
|
+
- lib/chef/knife/esx_vm_list.rb
|
68
|
+
- lib/knife-esx/version.rb
|
69
|
+
homepage: http://github.com/rubiojr/knife-esx
|
70
|
+
licenses: []
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.8.10
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: ESX Support for Chef's Knife Command
|
93
|
+
test_files:
|
94
|
+
- features/esx.feature
|
95
|
+
- features/foo
|
96
|
+
- features/step_definitions/steps.rb
|
97
|
+
- features/support/env.rb
|