knife-gandi 0.2.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 +82 -0
- data/lib/chef/knife/gandi_datacenter_list.rb +70 -0
- data/lib/chef/knife/gandi_image_list.rb +100 -0
- data/lib/chef/knife/gandi_server_create.rb +327 -0
- data/lib/chef/knife/gandi_server_delete.rb +94 -0
- data/lib/chef/knife/gandi_server_list.rb +89 -0
- data/lib/knife-gandi.rb +6 -0
- data/lib/knife-gandi/plugin_helper.rb +63 -0
- data/lib/knife-gandi/version.rb +3 -0
- metadata +87 -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,82 @@
|
|
1
|
+
= Knife Gandi
|
2
|
+
|
3
|
+
= DESCRIPTION:
|
4
|
+
|
5
|
+
This plugin gives knife the ability to create, bootstrap, and manage servers on the Gandi Hosting platform.
|
6
|
+
|
7
|
+
= INSTALLATION:
|
8
|
+
|
9
|
+
This plugin is distributed as a Ruby Gem. To install it, run:
|
10
|
+
|
11
|
+
gem install knife-gandi
|
12
|
+
|
13
|
+
Depending on your system's configuration, you may need to run this command with root privileges.
|
14
|
+
|
15
|
+
= CONFIGURATION:
|
16
|
+
|
17
|
+
In order to communicate with the Gandi Hosting API you will have to tell Knife about your API Key. The easiest way to accomplish this is to create some entries in your <tt>knife.rb</tt> file:
|
18
|
+
|
19
|
+
knife[:gandi_api_key] = "Your Gandi API Key"
|
20
|
+
|
21
|
+
If your knife.rb file will be checked into a SCM system (ie readable by others) you may want to read the values from environment variables:
|
22
|
+
|
23
|
+
knife[:gandi_api_key] = "#{ENV['GANDI_API_KEY']}"
|
24
|
+
|
25
|
+
You also have the option of passing your Gandi API Key into the individual knife subcommands using <tt>-K</tt> (or <tt>--gandi-api-key</tt>) command option
|
26
|
+
|
27
|
+
# provision a new 1GB Ubuntu 10.04 webserver
|
28
|
+
knife gandi server create 'role[webserver]' -I 23 -M 1024 -K "Your Gandi API Key"
|
29
|
+
|
30
|
+
|
31
|
+
Additionally the following options may be set in your `knife.rb`:
|
32
|
+
|
33
|
+
* template_file
|
34
|
+
|
35
|
+
= SUBCOMMANDS:
|
36
|
+
|
37
|
+
This plugin provides the following Knife subcommands. Specific command options can be found by invoking the subcommand with a <tt>--help</tt> flag
|
38
|
+
|
39
|
+
== knife gandi server create
|
40
|
+
|
41
|
+
Provisions a new server in the Gandi Hosting platform 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 server 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.
|
42
|
+
|
43
|
+
Gandi server images have the root account enabled by default. When a server instance is created, a user with the credentials you provide is created. This account does not have sudo enabled, therefore the root account will alway be used to bootstrap Chef. For security reasons, it is recommended that you enable sudo for some user account then disable the root account once the create command returns.
|
44
|
+
|
45
|
+
Make sure your Gandi account has sufficient resources in order to create a server.
|
46
|
+
|
47
|
+
The Gandi Hosting platform will always try to recycle any IP address previously released from a server created using your Gandi account. Note that if you do not remove that IP address' fingerprint from ~/.ssh/known_hosts, you will get a 'could not connect'/Net::SSH::HostKeyMismatch Warning during the bootstrap phase and the bootstrap phase will be skipped. You can disable the host key check in your ssh settings, know the security implications before doing that.
|
48
|
+
|
49
|
+
== knife gandi server delete
|
50
|
+
|
51
|
+
Deletes an existing server in the currently configured Gandi account. Please note - this does not delete the associated node and client objects from the Chef server.
|
52
|
+
|
53
|
+
== knife gandi server list
|
54
|
+
|
55
|
+
Outputs a list of all servers in the currently configured Gandi account. Please note - this shows all instances associated with the account, some of which may not be currently managed by the Chef server.
|
56
|
+
|
57
|
+
== knife gandi datacenter list
|
58
|
+
|
59
|
+
Outputs a list of all available datacenters. This data can be useful when choosing a datacenter id to pass to the <tt>knife gandi server create</tt> subcommand.
|
60
|
+
|
61
|
+
== knife gandi image list
|
62
|
+
|
63
|
+
Outputs a list of all available images available to the currently configured Gandi account. An image is a collection of files used to create or rebuild a server. Gandi provides a number of pre-built OS images by default. This data can be useful when choosing an image id to pass to the <tt>knife gandi server create</tt> subcommand.
|
64
|
+
|
65
|
+
|
66
|
+
= LICENSE:
|
67
|
+
|
68
|
+
Author:: Maxime Liron (<maximeliron@gmail.com>)
|
69
|
+
Copyright:: Copyright (c) 2009-2011 Maxime Liron
|
70
|
+
License:: Apache License, Version 2.0
|
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,70 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Maxime Liron (<maximeliron@gmail.com>)
|
3
|
+
# Copyright:: Copyright (c) 2011 Maxime Liron
|
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
|
+
|
21
|
+
class Chef
|
22
|
+
class Knife
|
23
|
+
class GandiDatacenterList < Knife
|
24
|
+
|
25
|
+
deps do
|
26
|
+
require 'knife-gandi'
|
27
|
+
require 'knife-gandi/plugin_helper'
|
28
|
+
require 'xmlrpc/client'
|
29
|
+
end
|
30
|
+
|
31
|
+
banner "knife gandi datacenter list (options)"
|
32
|
+
|
33
|
+
option :gandi_api_key,
|
34
|
+
:short => "-K KEY",
|
35
|
+
:long => "--gandi-api-key KEY",
|
36
|
+
:description => "Your Gandi API key",
|
37
|
+
:proc => Proc.new { |key| Chef::Config[:knife][:gandi_api_key] = key }
|
38
|
+
|
39
|
+
attr_reader :connection, :api_key
|
40
|
+
|
41
|
+
def run
|
42
|
+
# Unsual to extend here but enables 'plugin_helper' to be require'd lazily
|
43
|
+
extend KnifeGandi::PluginHelper
|
44
|
+
$stdout.sync = true
|
45
|
+
|
46
|
+
# Necessary changes to xmlrpc's defaults, prevent warnings from showing up in prompt
|
47
|
+
suppress_warnings do
|
48
|
+
XMLRPC::Config.const_set(:ENABLE_NIL_PARSER, true)
|
49
|
+
end
|
50
|
+
|
51
|
+
@connection = XMLRPC::Client.new2(KnifeGandi::API_ENDPOINT_URL)
|
52
|
+
@api_key = Chef::Config[:knife][:gandi_api_key] || config[:gandi_api_key]
|
53
|
+
|
54
|
+
server_list = [
|
55
|
+
ui.color('ID', :bold),
|
56
|
+
ui.color('Name', :bold),
|
57
|
+
ui.color('Country', :bold)
|
58
|
+
]
|
59
|
+
|
60
|
+
connection.call('datacenter.list', api_key).each do |server|
|
61
|
+
server_list << server['id'].to_s
|
62
|
+
server_list << server['name']
|
63
|
+
server_list << server['country']
|
64
|
+
end
|
65
|
+
|
66
|
+
puts ui.list(server_list, :columns_across, 3)
|
67
|
+
end
|
68
|
+
end # GandiServerList
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Maxime Liron (<maximeliron@gmail.com>)
|
3
|
+
# Copyright:: Copyright (c) 2011 Maxime Liron
|
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
|
+
|
21
|
+
class Chef
|
22
|
+
class Knife
|
23
|
+
class GandiImageList < Knife
|
24
|
+
|
25
|
+
deps do
|
26
|
+
require 'knife-gandi'
|
27
|
+
require 'knife-gandi/plugin_helper'
|
28
|
+
require 'xmlrpc/client'
|
29
|
+
end
|
30
|
+
|
31
|
+
banner "knife gandi image list (options)"
|
32
|
+
|
33
|
+
option :gandi_api_key,
|
34
|
+
:short => "-K KEY",
|
35
|
+
:long => "--gandi-api-key KEY",
|
36
|
+
:description => "Your Gandi API key",
|
37
|
+
:proc => Proc.new { |key| Chef::Config[:knife][:gandi_api_key] = key }
|
38
|
+
|
39
|
+
option :gandi_datacenter_id,
|
40
|
+
:short => "-D ID",
|
41
|
+
:long => "--gandi-datacenter-id ID",
|
42
|
+
:description => "Filter using a specific datacenter ID",
|
43
|
+
:proc => Proc.new { |d| Chef::Config[:knife][:gandi_datacenter_id] = d.to_i }
|
44
|
+
|
45
|
+
option :gandi_os_arch_32,
|
46
|
+
:short => "--32",
|
47
|
+
:description => "List only 32 bit images",
|
48
|
+
:boolean => true,
|
49
|
+
:proc => Proc.new { |d| Chef::Config[:knife][:gandi_os_arch] = 'x86-32' }
|
50
|
+
|
51
|
+
option :gandi_os_arch_64,
|
52
|
+
:short => "--64",
|
53
|
+
:description => "List only 64 bit images",
|
54
|
+
:boolean => true,
|
55
|
+
:proc => Proc.new { |d| Chef::Config[:knife][:gandi_os_arch] = 'x86-64' }
|
56
|
+
|
57
|
+
attr_reader :connection, :api_key
|
58
|
+
|
59
|
+
def run
|
60
|
+
# Unsual to extend here but enables 'plugin_helper' to be require'd lazily
|
61
|
+
extend KnifeGandi::PluginHelper
|
62
|
+
$stdout.sync = true
|
63
|
+
|
64
|
+
# Necessary changes to xmlrpc's defaults, prevent warnings from showing up in prompt
|
65
|
+
suppress_warnings do
|
66
|
+
XMLRPC::Config.const_set(:ENABLE_NIL_PARSER, true)
|
67
|
+
XMLRPC::Config.const_set(:ENABLE_NIL_CREATE, true)
|
68
|
+
end
|
69
|
+
|
70
|
+
@connection = XMLRPC::Client.new2(KnifeGandi::API_ENDPOINT_URL)
|
71
|
+
@api_key = Chef::Config[:knife][:gandi_api_key] || config[:gandi_api_key]
|
72
|
+
|
73
|
+
raise "No Gandi API key was specified" unless api_key
|
74
|
+
|
75
|
+
image_list = [
|
76
|
+
ui.color('ID', :bold),
|
77
|
+
ui.color('Name', :bold),
|
78
|
+
ui.color('Arch', :bold),
|
79
|
+
ui.color('Datacenter_id', :bold)
|
80
|
+
]
|
81
|
+
|
82
|
+
# Filter the result set
|
83
|
+
filters = {
|
84
|
+
:sort_by => 'label',
|
85
|
+
:datacenter_id => Chef::Config[:knife][:gandi_datacenter_id] || nil,
|
86
|
+
:os_arch => Chef::Config[:knife][:gandi_os_arch] || nil
|
87
|
+
}
|
88
|
+
|
89
|
+
connection.call('image.list', api_key, filters).each do |image|
|
90
|
+
image_list << image['id'].to_s
|
91
|
+
image_list << ui.color(image['label'], :bold)
|
92
|
+
image_list << image['os_arch']
|
93
|
+
image_list << image['datacenter_id'].to_s
|
94
|
+
end
|
95
|
+
|
96
|
+
puts ui.list(image_list, :columns_across, 4)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,327 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Maxime Liron (<maximeliron@gmail.com>)
|
3
|
+
# Copyright:: Copyright (c) 2011 Maxime Liron
|
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
|
+
|
21
|
+
class Chef
|
22
|
+
class Knife
|
23
|
+
class GandiServerCreate < Knife
|
24
|
+
|
25
|
+
deps do
|
26
|
+
require 'knife-gandi'
|
27
|
+
require 'knife-gandi/plugin_helper'
|
28
|
+
require 'xmlrpc/client'
|
29
|
+
require 'chef/knife/bootstrap'
|
30
|
+
require 'resolv'
|
31
|
+
Chef::Knife::Bootstrap.load_deps
|
32
|
+
end
|
33
|
+
|
34
|
+
banner "knife gandi server create (options)"
|
35
|
+
|
36
|
+
option :memory,
|
37
|
+
:short => "-M MEMORY",
|
38
|
+
:long => "--memory MEMORY",
|
39
|
+
:description => "The amount of memory (ram) of server",
|
40
|
+
:proc => Proc.new { |f| Chef::Config[:knife][:memory] = f.to_i }
|
41
|
+
|
42
|
+
option :cores,
|
43
|
+
:short => "-C CORES",
|
44
|
+
:long => "--cores CORES",
|
45
|
+
:description => "The number of cores (cpu) of server",
|
46
|
+
:proc => Proc.new { |f| Chef::Config[:knife][:cores] = f.to_i }
|
47
|
+
|
48
|
+
option :bandwidth,
|
49
|
+
:short => "-B BANDWIDTH",
|
50
|
+
:long => "--bandwidth BANDWIDTH",
|
51
|
+
:description => "The amount of bandwidth (network) of server (optional)",
|
52
|
+
:proc => Proc.new { |f| Chef::Config[:knife][:bandwidth] = f.to_i }
|
53
|
+
|
54
|
+
|
55
|
+
option :ip_version,
|
56
|
+
:short => "-W VERSION",
|
57
|
+
:long => "--ip-version VERSION",
|
58
|
+
:description => "The version of the IP protocol",
|
59
|
+
:proc => Proc.new { |f| Chef::Config[:knife][:ip_version] = f.to_i }
|
60
|
+
|
61
|
+
option :image_id,
|
62
|
+
:short => "-I IMAGE_ID",
|
63
|
+
:long => "--image-id IMAGE_ID",
|
64
|
+
:description => "The image id of the desired server image",
|
65
|
+
:proc => Proc.new { |i| Chef::Config[:knife][:image_id] = i.to_i }
|
66
|
+
|
67
|
+
option :disk_name,
|
68
|
+
:short => "-H NAME",
|
69
|
+
:long => "--disk-name NAME",
|
70
|
+
:description => "The name of the server's system disk. default: {server name}_disk",
|
71
|
+
:proc => Proc.new { |i| Chef::Config[:knife][:disk_name] = i }
|
72
|
+
|
73
|
+
option :datacenter_id,
|
74
|
+
:short => "-D DATACENTER_ID",
|
75
|
+
:long => "--datacenter-id DATACENTER_ID",
|
76
|
+
:description => "The datacenter's id in which the server will be created",
|
77
|
+
:proc => Proc.new { |i| Chef::Config[:knife][:datacenter_id] = i.to_i }
|
78
|
+
|
79
|
+
option :server_name,
|
80
|
+
:short => "-S NAME",
|
81
|
+
:long => "--server-name NAME",
|
82
|
+
:description => "The server name"
|
83
|
+
|
84
|
+
option :chef_node_name,
|
85
|
+
:short => "-N NAME",
|
86
|
+
:long => "--node-name NAME",
|
87
|
+
:description => "The Chef node name for your new node, defaults to server name"
|
88
|
+
|
89
|
+
option :login,
|
90
|
+
:short => "-x USERNAME",
|
91
|
+
:long => "--login USERNAME",
|
92
|
+
:description => "The login for the server"
|
93
|
+
|
94
|
+
option :password,
|
95
|
+
:short => "-P PASSWORD",
|
96
|
+
:long => "--password PASSWORD",
|
97
|
+
:description => "The password for the server"
|
98
|
+
|
99
|
+
option :gandi_api_key,
|
100
|
+
:short => "-K KEY",
|
101
|
+
:long => "--gandi-api-key KEY",
|
102
|
+
:description => "Your Gandi API key",
|
103
|
+
:proc => Proc.new { |key| Chef::Config[:knife][:gandi_api_key] = key }
|
104
|
+
|
105
|
+
option :prerelease,
|
106
|
+
:long => "--prerelease",
|
107
|
+
:description => "Install the pre-release chef gems"
|
108
|
+
|
109
|
+
option :bootstrap_version,
|
110
|
+
:long => "--bootstrap-version VERSION",
|
111
|
+
:description => "The version of Chef to install",
|
112
|
+
:proc => Proc.new { |v| Chef::Config[:knife][:bootstrap_version] = v }
|
113
|
+
|
114
|
+
option :distro,
|
115
|
+
:short => "-d DISTRO",
|
116
|
+
:long => "--distro DISTRO",
|
117
|
+
:description => "Bootstrap a distro using a template",
|
118
|
+
:proc => Proc.new { |d| Chef::Config[:knife][:distro] = d },
|
119
|
+
:default => "ubuntu10.04-gems"
|
120
|
+
|
121
|
+
option :use_sudo,
|
122
|
+
:long => "--sudo",
|
123
|
+
:description => "Execute the bootstrap via sudo",
|
124
|
+
:boolean => true,
|
125
|
+
:default => true
|
126
|
+
|
127
|
+
option :template_file,
|
128
|
+
:long => "--template-file TEMPLATE",
|
129
|
+
:description => "Full path to location of template to use",
|
130
|
+
:proc => Proc.new { |t| Chef::Config[:knife][:template_file] = t },
|
131
|
+
:default => false
|
132
|
+
|
133
|
+
option :run_list,
|
134
|
+
:short => "-r RUN_LIST",
|
135
|
+
:long => "--run-list RUN_LIST",
|
136
|
+
:description => "Comma separated list of roles/recipes to apply",
|
137
|
+
:proc => lambda { |o| o.split(/[\s,]+/) },
|
138
|
+
:default => []
|
139
|
+
|
140
|
+
attr_reader :connection, :api_key
|
141
|
+
|
142
|
+
def run
|
143
|
+
# Unsual to extend here but enables 'plugin_helper' to be require'd lazily (in deps)
|
144
|
+
extend KnifeGandi::PluginHelper
|
145
|
+
$stdout.sync = true
|
146
|
+
|
147
|
+
# Necessary changes to xmlrpc's defaults, prevent warnings from showing up in prompt
|
148
|
+
suppress_warnings do
|
149
|
+
XMLRPC::Config.const_set(:ENABLE_NIL_PARSER, true)
|
150
|
+
XMLRPC::Config.const_set(:ENABLE_NIL_CREATE, true)
|
151
|
+
end
|
152
|
+
|
153
|
+
# The connection used to communicate with the Gandi API throught this command invokation
|
154
|
+
@connection = XMLRPC::Client.new2(KnifeGandi::API_ENDPOINT_URL)
|
155
|
+
@api_key = Chef::Config[:knife][:gandi_api_key] || config[:gandi_api_key] || raise("Please provide an API key")
|
156
|
+
|
157
|
+
print "\n"
|
158
|
+
|
159
|
+
# Server spec
|
160
|
+
# Sent as a param to the Gandi API
|
161
|
+
# For mandatory params that were not passed in through the command line, ask for them interactively
|
162
|
+
# The code may try to validate given values but we expect the API will throw errors.
|
163
|
+
server_spec = Hash.new
|
164
|
+
|
165
|
+
# Server Name
|
166
|
+
server_spec[:hostname] = locate_config_value(:server_name)
|
167
|
+
server_spec[:hostname] ||= ui.ask('Name of the server: ') do |question|
|
168
|
+
question.answer_type = String
|
169
|
+
end
|
170
|
+
|
171
|
+
# CPU Cores
|
172
|
+
server_spec[:cores] = locate_config_value(:cores)
|
173
|
+
server_spec[:cores] ||= ui.ask('Number of CPU cores: ') do |question|
|
174
|
+
question.answer_type = Integer
|
175
|
+
question.default = 1
|
176
|
+
question.in = 1..6
|
177
|
+
end
|
178
|
+
|
179
|
+
# Memory
|
180
|
+
server_spec[:memory] = locate_config_value(:memory)
|
181
|
+
server_spec[:memory] ||= ui.ask('Amount of memory (MB): ') do |question|
|
182
|
+
question.answer_type = Integer
|
183
|
+
question.default = 256
|
184
|
+
# Make sure this is a valid memory value
|
185
|
+
question.validate = lambda { |ans| ans.to_i.modulo(64) == 0 }
|
186
|
+
question.in = 256..12288
|
187
|
+
question.responses[:not_valid] = "Your answer isn't valid (must be a valid memory value ex: 256, 512, 2048, ...)"
|
188
|
+
end
|
189
|
+
|
190
|
+
# Network Bandwidth
|
191
|
+
bandwidth = locate_config_value(:bandwidth)
|
192
|
+
bandwidth ||= ui.ask('Bandwidth (MB): ') do |question|
|
193
|
+
question.answer_type = Integer
|
194
|
+
question.default = 5
|
195
|
+
end
|
196
|
+
server_spec[:bandwidth] = bandwidth * 1024
|
197
|
+
|
198
|
+
# IP Version
|
199
|
+
server_spec[:ip_version] = locate_config_value(:ip_version)
|
200
|
+
server_spec[:ip_version] ||= ui.ask('IP protocol version: ') do |question|
|
201
|
+
question.answer_type = Integer
|
202
|
+
question.default = 4
|
203
|
+
question.in = [4, 6]
|
204
|
+
end
|
205
|
+
|
206
|
+
# Datacenter
|
207
|
+
# TODO: Query the API and list the datacenters inline, there is only two of them currently.
|
208
|
+
server_spec[:datacenter_id] = locate_config_value(:datacenter_id)
|
209
|
+
server_spec[:datacenter_id] ||= ui.ask('Datacenter id: ') do |question|
|
210
|
+
question.answer_type = Integer
|
211
|
+
end
|
212
|
+
|
213
|
+
# User Name
|
214
|
+
# WARN: The Gandi hosting platform will not allow use of 'root'
|
215
|
+
server_spec[:login] = locate_config_value(:login)
|
216
|
+
server_spec[:login] ||= ui.ask('Username: ') do |question|
|
217
|
+
question.answer_type = String
|
218
|
+
question.default = 'admin'
|
219
|
+
end
|
220
|
+
|
221
|
+
# User Password
|
222
|
+
server_spec[:password] = locate_config_value(:password)
|
223
|
+
server_spec[:password] ||= ui.ask('Password: ') do |question|
|
224
|
+
question.answer_type = String
|
225
|
+
question.validate = /^[ -~]{8,64}$/
|
226
|
+
end
|
227
|
+
|
228
|
+
# Server Image
|
229
|
+
# To create a server instance, Gandi requires a disk spec of the server's system disk
|
230
|
+
disk_spec = Hash.new
|
231
|
+
disk_spec[:datacenter_id] = server_spec[:datacenter_id]
|
232
|
+
disk_spec[:name] = locate_config_value(:disk_name) || "disk_#{server_spec[:hostname]}"
|
233
|
+
|
234
|
+
server_image_id = locate_config_value(:image_id)
|
235
|
+
server_image_id ||= ui.ask('Image id (see `knife gandi image list` command output): ') do |question|
|
236
|
+
question.answer_type = Integer
|
237
|
+
end
|
238
|
+
|
239
|
+
# Lookup the server image
|
240
|
+
server_image = connection.call('image.info', api_key, server_image_id)
|
241
|
+
|
242
|
+
# Create the server
|
243
|
+
# 'vm.create_from' is a shortcut method that creates the vm, its system disk and network interface
|
244
|
+
# in one method call. It returns three operations, one describing each create operation.
|
245
|
+
create_operations = connection.call('vm.create_from', api_key, server_spec, disk_spec, server_image['disk_id'])
|
246
|
+
vm_create_operation = create_operations.find { |op| op['type'] == 'vm_create' }
|
247
|
+
|
248
|
+
print "\n"
|
249
|
+
print ui.color("Creating the server.", :magenta)
|
250
|
+
|
251
|
+
# Wait for it to be created to do stuff...
|
252
|
+
until_done(vm_create_operation) { print '.' }
|
253
|
+
|
254
|
+
# Server is created, now get its specs
|
255
|
+
server = connection.call('vm.info', api_key, vm_create_operation['vm_id'])
|
256
|
+
# Server's public IP resource representation (IP address, reverse dns, ...). See Gandi API docs
|
257
|
+
public_ip_info = ip_objects_of(server, :type => 'public', :version => server_spec[:ip_version]).first
|
258
|
+
|
259
|
+
|
260
|
+
puts "\n"
|
261
|
+
puts "#{ui.color("Public DNS Name", :cyan)}: #{public_ip_info['reverse']}"
|
262
|
+
puts "#{ui.color("Public IP Address", :cyan)}: #{public_ip_info['ip']}"
|
263
|
+
puts "#{ui.color("Password", :cyan)}: #{server_spec[:password]}"
|
264
|
+
|
265
|
+
print "\n"
|
266
|
+
print ui.color("Waiting for sshd.", :magenta)
|
267
|
+
|
268
|
+
print(".") until tcp_test_ssh(public_ip_info['reverse']) { sleep @initial_sleep_delay ||= 10; puts("done") }
|
269
|
+
|
270
|
+
# Now that we have SSH access, bootstrap Chef on the server
|
271
|
+
# and hand over control to the Chef infrastructure
|
272
|
+
bootstrap_for_node(server_spec, server, public_ip_info['reverse']).run
|
273
|
+
|
274
|
+
puts "\n"
|
275
|
+
puts "#{ui.color("Instance ID", :cyan)}: #{server['id']}"
|
276
|
+
puts "#{ui.color("Name", :cyan)}: #{server['hostname']}"
|
277
|
+
puts "#{ui.color("Memory", :cyan)}: #{server['memory']}"
|
278
|
+
puts "#{ui.color("Cores", :cyan)}: #{server['cores']}"
|
279
|
+
puts "#{ui.color("Image", :cyan)}: #{server_image['label']}"
|
280
|
+
puts "#{ui.color("Public DNS Name", :cyan)}: #{public_ip_info['reverse']}"
|
281
|
+
puts "#{ui.color("Public IP Address", :cyan)}: #{public_ip_info['ip']}"
|
282
|
+
puts "#{ui.color("Password", :cyan)}: #{server_spec[:password]}"
|
283
|
+
puts "#{ui.color("Environment", :cyan)}: #{config[:environment] || '_default'}"
|
284
|
+
puts "#{ui.color("Run List", :cyan)}: #{config[:run_list].join(', ')}"
|
285
|
+
end
|
286
|
+
|
287
|
+
# Test if an SSH deamon is listening.
|
288
|
+
# AKA: is my server up and running?
|
289
|
+
def tcp_test_ssh(hostname)
|
290
|
+
tcp_socket = TCPSocket.new(hostname, 22)
|
291
|
+
readable = IO.select([tcp_socket], nil, nil, 5)
|
292
|
+
if readable
|
293
|
+
Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
|
294
|
+
yield
|
295
|
+
true
|
296
|
+
else
|
297
|
+
false
|
298
|
+
end
|
299
|
+
rescue Errno::ETIMEDOUT
|
300
|
+
false
|
301
|
+
rescue Errno::ECONNREFUSED
|
302
|
+
sleep 2
|
303
|
+
false
|
304
|
+
ensure
|
305
|
+
tcp_socket && tcp_socket.close
|
306
|
+
end
|
307
|
+
|
308
|
+
def bootstrap_for_node(server_spec, server, fqdn)
|
309
|
+
bootstrap = Chef::Knife::Bootstrap.new
|
310
|
+
bootstrap.name_args = [fqdn]
|
311
|
+
bootstrap.config[:run_list] = config[:run_list]
|
312
|
+
bootstrap.config[:ssh_user] = 'root'
|
313
|
+
bootstrap.config[:ssh_password] = server_spec[:password]
|
314
|
+
bootstrap.config[:identity_file] = config[:identity_file]
|
315
|
+
bootstrap.config[:chef_node_name] = server[:chef_node_name] || "#{server['hostname']}_gandi-#{server['id']}"
|
316
|
+
bootstrap.config[:prerelease] = config[:prerelease]
|
317
|
+
bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
|
318
|
+
bootstrap.config[:distro] = locate_config_value(:distro)
|
319
|
+
# bootstrap will run as root...sudo (by default) also messes up Ohai on CentOS boxes
|
320
|
+
bootstrap.config[:use_sudo] = config[:use_sudo]
|
321
|
+
bootstrap.config[:template_file] = locate_config_value(:template_file)
|
322
|
+
bootstrap.config[:environment] = config[:environment]
|
323
|
+
bootstrap
|
324
|
+
end
|
325
|
+
end
|
326
|
+
end
|
327
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Maxime Liron (<maximeliron@gmail.com>)
|
3
|
+
# Copyright:: Copyright (c) 2011 Maxime Liron
|
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
|
+
|
21
|
+
class Chef
|
22
|
+
class Knife
|
23
|
+
class GandiServerDelete < Knife
|
24
|
+
|
25
|
+
deps do
|
26
|
+
require 'knife-gandi'
|
27
|
+
require 'knife-gandi/plugin_helper'
|
28
|
+
require 'xmlrpc/client'
|
29
|
+
end
|
30
|
+
|
31
|
+
banner "knife gandi server delete SERVER_ID (options)"
|
32
|
+
|
33
|
+
option :gandi_api_key,
|
34
|
+
:short => "-K KEY",
|
35
|
+
:long => "--gandi-api-key KEY",
|
36
|
+
:description => "Your Gandi API key",
|
37
|
+
:proc => Proc.new { |key| Chef::Config[:knife][:gandi_api_key] = key }
|
38
|
+
|
39
|
+
attr_reader :connection, :api_key
|
40
|
+
|
41
|
+
def run
|
42
|
+
# Unsual to extend here but enables 'plugin_helper' to be require'd lazily
|
43
|
+
extend KnifeGandi::PluginHelper
|
44
|
+
$stdout.sync = true
|
45
|
+
|
46
|
+
# Necessary changes to xmlrpc's defaults, prevent warnings from showing up in prompt
|
47
|
+
suppress_warnings do
|
48
|
+
XMLRPC::Config.const_set(:ENABLE_NIL_PARSER, true)
|
49
|
+
XMLRPC::Config.const_set(:ENABLE_NIL_CREATE, true)
|
50
|
+
end
|
51
|
+
|
52
|
+
@connection = XMLRPC::Client.new2(KnifeGandi::API_ENDPOINT_URL)
|
53
|
+
@api_key = Chef::Config[:knife][:gandi_api_key] || config[:gandi_api_key] || raise("Please provide an API key")
|
54
|
+
|
55
|
+
server = connection.call('vm.info', api_key, @name_args[0].to_i)
|
56
|
+
|
57
|
+
puts "\n"
|
58
|
+
puts "#{ui.color("Server ID", :cyan)}: #{server['id']}"
|
59
|
+
puts "#{ui.color("Name", :cyan)}: #{ui.color(server['hostname'], :bold)}"
|
60
|
+
puts "#{ui.color("Description", :cyan)}: #{server['description']}" if server['description']
|
61
|
+
puts "#{ui.color("Memory", :cyan)}: #{server['memory']}"
|
62
|
+
puts "#{ui.color("Cores", :cyan)}: #{server['cores']}"
|
63
|
+
puts "#{ui.color("Datacenter ID", :cyan)}: #{server['datacenter_id']}"
|
64
|
+
puts "#{ui.color("Image", :cyan)}: #{server['disks'].find{ |disk| disk['is_boot_disk'] }['label']}"
|
65
|
+
puts "#{ui.color("State", :cyan)}: #{server['state']}"
|
66
|
+
|
67
|
+
raise "This server is being created, wait for this to end and try again" if server['state'] == 'being_created'
|
68
|
+
|
69
|
+
puts "\n"
|
70
|
+
puts "==============================================="
|
71
|
+
confirm("Do you really want to delete this server?")
|
72
|
+
|
73
|
+
# Stop the server, the API requires a server to be halted before it can be deleted
|
74
|
+
unless (server['state'] == 'halted')
|
75
|
+
puts "\n"
|
76
|
+
print ui.color("Stopping server.", :magenta)
|
77
|
+
halt_operation = connection.call('vm.stop', api_key, server['id'])
|
78
|
+
until_done(halt_operation) { print '.' }
|
79
|
+
end
|
80
|
+
|
81
|
+
vm_delete_operation = connection.call('vm.delete', api_key, server['id'])
|
82
|
+
|
83
|
+
puts "\n\n"
|
84
|
+
print ui.color("Deleting server.", :magenta)
|
85
|
+
|
86
|
+
until_done(vm_delete_operation) { print '.' }
|
87
|
+
|
88
|
+
puts "\n\n"
|
89
|
+
ui.warn("Deleted server #{ui.color(server['id'], :bold)} named #{ui.color(server['hostname'], :bold)}")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Maxime Liron (<maximeliron@gmail.com>)
|
3
|
+
# Copyright:: Copyright (c) 2011 Maxime Liron
|
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
|
+
|
21
|
+
class Chef
|
22
|
+
class Knife
|
23
|
+
class GandiServerList < Knife
|
24
|
+
|
25
|
+
deps do
|
26
|
+
require 'knife-gandi'
|
27
|
+
require 'knife-gandi/plugin_helper'
|
28
|
+
require 'xmlrpc/client'
|
29
|
+
end
|
30
|
+
|
31
|
+
banner "knife gandi server list (options)"
|
32
|
+
|
33
|
+
option :gandi_api_key,
|
34
|
+
:short => "-K KEY",
|
35
|
+
:long => "--gandi-api-key KEY",
|
36
|
+
:description => "Your Gandi API key",
|
37
|
+
:proc => Proc.new { |key| Chef::Config[:knife][:gandi_api_key] = key }
|
38
|
+
|
39
|
+
option :ip_version,
|
40
|
+
:short => "-W VERSION",
|
41
|
+
:long => "--ip-version VERSION",
|
42
|
+
:description => "The version of the IP protocol (4|6) displayed in the listings",
|
43
|
+
:proc => Proc.new { |f| Chef::Config[:knife][:ip_version] = f.to_i },
|
44
|
+
:default => 4
|
45
|
+
|
46
|
+
attr_reader :connection, :api_key
|
47
|
+
|
48
|
+
def run
|
49
|
+
# Unsual to extend here but enables 'plugin_helper' to be require'd lazily
|
50
|
+
extend KnifeGandi::PluginHelper
|
51
|
+
$stdout.sync = true
|
52
|
+
|
53
|
+
# Necessary changes to xmlrpc's defaults, prevent warnings from showing up in prompt
|
54
|
+
suppress_warnings do
|
55
|
+
XMLRPC::Config.const_set(:ENABLE_NIL_PARSER, true)
|
56
|
+
end
|
57
|
+
|
58
|
+
@connection = XMLRPC::Client.new2(KnifeGandi::API_ENDPOINT_URL)
|
59
|
+
@api_key = Chef::Config[:knife][:gandi_api_key] || config[:gandi_api_key] || raise("Please provide an API key")
|
60
|
+
|
61
|
+
server_list = [
|
62
|
+
ui.color('ID', :bold),
|
63
|
+
ui.color('Hostname', :bold),
|
64
|
+
ui.color('Date Created', :bold),
|
65
|
+
ui.color('Cores', :bold),
|
66
|
+
ui.color('Memory', :bold),
|
67
|
+
ui.color('Image', :bold),
|
68
|
+
ui.color('State', :bold),
|
69
|
+
ui.color('Public IPs', :bold)
|
70
|
+
]
|
71
|
+
|
72
|
+
connection.call('vm.list', api_key).each do |server|
|
73
|
+
# Adds a some overhead but returns much more vm attributes including IPs and Image
|
74
|
+
server = connection.call('vm.info', api_key, server['id'])
|
75
|
+
server_list << server['id'].to_s
|
76
|
+
server_list << server['hostname']
|
77
|
+
server_list << server['date_created'].to_date.to_s
|
78
|
+
server_list << server['cores'].to_s
|
79
|
+
server_list << server['memory'].to_s + 'mb'
|
80
|
+
server_list << server['disks'].find{ |disk| disk['is_boot_disk'] }['label']
|
81
|
+
server_list << server['state']
|
82
|
+
server_list << public_ips_of(server, :version => locate_config_value(:ip_version)).join(', ')
|
83
|
+
end
|
84
|
+
|
85
|
+
puts ui.list(server_list, :columns_across, 8)
|
86
|
+
end
|
87
|
+
end # GandiServerList
|
88
|
+
end
|
89
|
+
end
|
data/lib/knife-gandi.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
module KnifeGandi
|
2
|
+
module PluginHelper
|
3
|
+
|
4
|
+
# Keeps the vm's warnings out of $stdout.
|
5
|
+
# Helpful when changing the value of a constant.
|
6
|
+
def suppress_warnings
|
7
|
+
original_verbosity = $VERBOSE
|
8
|
+
$VERBOSE = nil
|
9
|
+
result = yield
|
10
|
+
$VERBOSE = original_verbosity
|
11
|
+
return result
|
12
|
+
end
|
13
|
+
|
14
|
+
def locate_config_value(key)
|
15
|
+
key = key.to_sym
|
16
|
+
Chef::Config[:knife][key] || config[key]
|
17
|
+
end
|
18
|
+
|
19
|
+
def config_has_value?(key)
|
20
|
+
key = key.to_sym
|
21
|
+
Chef::Config[:knife].has_key?(key) || config.has_key?(key)
|
22
|
+
end
|
23
|
+
|
24
|
+
def until_done(operation)
|
25
|
+
while operation['step'] != 'DONE'
|
26
|
+
operation = connection.call('operation.info', api_key, operation['id'])
|
27
|
+
yield
|
28
|
+
sleep 2
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def public_ips_of(server, opts={})
|
33
|
+
options = opts.merge({:type => 'public'})
|
34
|
+
public_ips = ip_objects_of(server, options)
|
35
|
+
public_ips.collect { |ip| ip['ip'] }
|
36
|
+
end
|
37
|
+
|
38
|
+
def public_reverses_of(server, opts={})
|
39
|
+
options = opts.merge({:type => 'public'})
|
40
|
+
public_ips = ip_objects_of(server, options)
|
41
|
+
public_ips.collect { |ip| ip['reverse'] }
|
42
|
+
end
|
43
|
+
|
44
|
+
# Returns the public IP Resource objects sent from the server.
|
45
|
+
# Look up their representation in the Gandi API docs.
|
46
|
+
def ip_objects_of(server, opts={})
|
47
|
+
# Server has n ifaces which hold 2 IPs (IPv4 and IPv6)
|
48
|
+
public_ifaces = server['ifaces']
|
49
|
+
if opts.has_key?(:type)
|
50
|
+
public_ifaces = public_ifaces.find_all { |iface| iface['type'] == 'public' }
|
51
|
+
end
|
52
|
+
|
53
|
+
public_ips = public_ifaces.collect_concat { |iface| iface['ips'] }
|
54
|
+
if opts.has_key?(:version)
|
55
|
+
# Filter IP version (4|6)
|
56
|
+
public_ips = public_ips.find_all { |ip| ip['version'] == opts[:version] }
|
57
|
+
end
|
58
|
+
|
59
|
+
public_ips
|
60
|
+
end
|
61
|
+
|
62
|
+
end # EOF PluginHelper
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: knife-gandi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.2.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Maxime Liron
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-05-11 00:00:00 +02:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: net-ssh
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 2.1.3
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: net-ssh-multi
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ~>
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 1.0.1
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
38
|
+
description: Gandi Hosting Support for Chef's Knife Command
|
39
|
+
email: maximeliron@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files:
|
45
|
+
- README.rdoc
|
46
|
+
- LICENSE
|
47
|
+
files:
|
48
|
+
- LICENSE
|
49
|
+
- README.rdoc
|
50
|
+
- lib/chef/knife/gandi_datacenter_list.rb
|
51
|
+
- lib/chef/knife/gandi_image_list.rb
|
52
|
+
- lib/chef/knife/gandi_server_create.rb
|
53
|
+
- lib/chef/knife/gandi_server_delete.rb
|
54
|
+
- lib/chef/knife/gandi_server_list.rb
|
55
|
+
- lib/knife-gandi/plugin_helper.rb
|
56
|
+
- lib/knife-gandi/version.rb
|
57
|
+
- lib/knife-gandi.rb
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: http://wiki.opscode.com/display/chef
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.6.1
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Gandi Hosting Support for Chef's Knife Command
|
86
|
+
test_files: []
|
87
|
+
|