knife-digital_ocean 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .chef
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-19mode
7
+ - ruby-head
8
+ - jruby-head
9
+ script: bundle exec rake test
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in knife-digital_ocean.gemspec
4
+ gemspec
data/LICENSE.txt 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.md ADDED
@@ -0,0 +1,104 @@
1
+ # Knife::DigitalOcean
2
+
3
+ This is a plugin for [Chef's](http://www.opscode.com/chef/) [knife](http://wiki.opscode.com/display/chef/Knife) tool. It allows you to bootstrap virtual machines with [DigitalOcean.com](https://www.digitalocean.com/) including the initial bootstrapping of chef on that system.
4
+ You can also skip the chef bootstrapping if you prefer using [knife-solo](https://github.com/matschaffer/knife-solo) or some other solutions.
5
+ ## Installation
6
+ $ # install chef
7
+ $ gem install knife-digital_ocean
8
+
9
+ ## Overview
10
+
11
+ This plugin provides the following sub-commands:
12
+
13
+ * knife digital_ocean droplet create (options)
14
+ **Creates a virtual machine with or without bootstrapping chef-client**
15
+
16
+ * knife digital_ocean droplet destroy (options)
17
+ **Destroys the virtual machine and its data**
18
+
19
+ * knife digital_ocean droplet list (options)
20
+ **Lists currently running virtual machines**
21
+
22
+ * knife digital_ocean image list (options)
23
+ **Lists available images (snapshots, backups, OS-images)**
24
+
25
+ * knife digital_ocean region list (options)
26
+ **Lists the server regions/locations/data-center**
27
+
28
+ * knife digital_ocean size list (options)
29
+ **Lists the available server sizes**
30
+
31
+ * knife digital_ocean sshkey list
32
+ **Lists name + id of the uploaded known ssh keys**
33
+
34
+ ## Configuration
35
+
36
+ The best way is to put your API-credentials of DigitalOcean in your knife.rb file of choice:
37
+
38
+ ```ruby
39
+ knife[:digital_ocean_client_id] = 'XXXXXXXXXXXX'
40
+ knife[:digital_ocean_api_key] = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'
41
+ ```
42
+
43
+ ## Usage
44
+
45
+ ### Create a Droplet
46
+
47
+ #### With bootstrapping in an chef-server environment:
48
+
49
+ __Example__
50
+
51
+ ```bash
52
+ knife digital_ocean droplet create --server-name awesome-vm1.chef.io \
53
+ --image 25306 \
54
+ --location 2 \
55
+ --size 66 \
56
+ --ssh-keys 1234,4567 \
57
+ --bootstrap \
58
+ --run-list "role[base],role[webserver]"
59
+ ```
60
+
61
+ __Syntax__
62
+ ```bash
63
+ knife digital_ocean droplet create --server-name <FQDN> \
64
+ --image <IMAGE ID> \
65
+ --location <REGION ID> \
66
+ --size <SIZE ID> \
67
+ --ssh-keys <SSH KEY-ID(s), comma-separated> \
68
+ --bootstrap \
69
+ --run-list "<RUNLIST>"
70
+ ```
71
+
72
+ __Short Syntax__
73
+ ```bash
74
+ knife digital_ocean droplet create --N <FQDN> \
75
+ --I <IMAGE ID> \
76
+ --L <REGION ID> \
77
+ --S <SIZE ID> \
78
+ --K <SSH KEY-ID(s), comma-separated> \
79
+ --B \
80
+ --r "<RUNLIST>"
81
+ ```
82
+
83
+
84
+ #### With knife-solo, your custom external bootstrapping script or without chef at all
85
+
86
+ This will just create a droplet and returns its IP-address no other actions are done. You can now run your custom solution e.g. ```knife solo bootstrap <IP> ```
87
+ __Example__
88
+
89
+ ```bash
90
+ knife digital_ocean droplet create --server-name awesome-vm1.chef.io \
91
+ --image 25306 \
92
+ --location 2 \
93
+ --size 66 \
94
+ --ssh-keys 1234,4567
95
+ ```
96
+
97
+
98
+ ## Contributing
99
+
100
+ 1. Fork it
101
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
102
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
103
+ 4. Push to the branch (`git push origin my-new-feature`)
104
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ #encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ Bundler.setup
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ require 'rspec/core/rake_task'
8
+ desc 'Run RSpec'
9
+ RSpec::Core::RakeTask.new do |spec|
10
+ spec.pattern = 'spec/**/*_spec.rb'
11
+ spec.rspec_opts = ['--color', '--format nested']
12
+ end
13
+
14
+ desc 'Run rspec tests'
15
+ task :test => [:spec]
16
+
17
+ task :default => :test
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'knife-digital_ocean/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'knife-digital_ocean'
8
+ gem.version = Knife::DigitalOcean::VERSION
9
+ gem.authors = ['Roland Moriz']
10
+ gem.email = ['roland@moriz.de']
11
+ gem.description = %q{A plugin for chef's knife to manage instances of DigitalOcean servers}
12
+ gem.summary = %q{A plugin for chef's knife to manage instances of DigitalOcean servers}
13
+ gem.homepage = 'http://github.com/rmoriz/knife-digital_ocean'
14
+ gem.license = 'Apache 2.0'
15
+
16
+ gem.add_dependency 'chef', '~> 10.18'
17
+ gem.add_dependency 'digital_ocean'
18
+ gem.add_dependency 'highline'
19
+ gem.add_dependency 'net-ssh-multi', '~> 1.1'
20
+
21
+
22
+ gem.add_development_dependency 'rspec', '~> 2.0'
23
+ gem.add_development_dependency 'guard'
24
+ gem.add_development_dependency 'guard-rspec'
25
+ gem.add_development_dependency 'rb-fsevent'
26
+ gem.add_development_dependency 'rake'
27
+
28
+ gem.files = `git ls-files`.split($/)
29
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
30
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
31
+ gem.require_paths = ['lib']
32
+ end
@@ -0,0 +1,75 @@
1
+ # lots of awesome stoff stolen from opscode/knife-azure ;-)
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ class Chef
16
+ class Knife
17
+ module DigitalOceanBase
18
+
19
+ def self.included(includer)
20
+ includer.class_eval do
21
+
22
+ deps do
23
+ require 'digital_ocean'
24
+ require 'highline'
25
+ require 'net/ssh/multi'
26
+ require 'readline'
27
+ require 'chef/json_compat'
28
+ require 'chef/knife/bootstrap'
29
+ Chef::Knife::Bootstrap.load_deps
30
+ end
31
+
32
+ option :digital_ocean_client_id,
33
+ :short => '-K CLIENT_ID',
34
+ :long => '--digital_ocean_client_id CLIENT_ID',
35
+ :description => 'Your DigitalOcean client_id',
36
+ :proc => Proc.new { |client_id| Chef::Config[:knife][:digital_ocean_client_id] = client_id }
37
+
38
+ option :digital_ocean_api_key,
39
+ :short => '-A API_KEY',
40
+ :long => '--digital_ocean_api_key API_KEY',
41
+ :description => 'Your DigitalOcean API_KEY',
42
+ :proc => Proc.new { |api_key| Chef::Config[:knife][:digital_ocean_api_key] = api_key }
43
+ end
44
+ end
45
+
46
+ def h
47
+ @highline ||= HighLine.new
48
+ end
49
+
50
+ def client
51
+ DigitalOcean::API.new(:client_id => Chef::Config[:knife][:digital_ocean_client_id],
52
+ :api_key => Chef::Config[:knife][:digital_ocean_api_key])
53
+ end
54
+
55
+ def validate!(keys=[:digital_ocean_client_id, :digital_ocean_api_key])
56
+ errors = []
57
+
58
+ keys.each do |k|
59
+ if locate_config_value(k).nil?
60
+ errors << "You did not provide a valid '#{k}' value. Please set knife[:#{k}] in your knife.rb or pass as an option."
61
+ end
62
+ end
63
+
64
+ if errors.each{|e| ui.error(e)}.any?
65
+ exit 1
66
+ end
67
+ end
68
+
69
+ def locate_config_value(key)
70
+ key = key.to_sym
71
+ config[key] || Chef::Config[:knife][key]
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,204 @@
1
+ # Licensed under the Apache License, Version 2.0 (the "License");
2
+ # you may not use this file except in compliance with the License.
3
+ # You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+ #
13
+ require File.expand_path('../digital_ocean_base', __FILE__)
14
+
15
+ class Chef
16
+ class Knife
17
+ class DigitalOceanDropletCreate < Knife
18
+ include Knife::DigitalOceanBase
19
+
20
+ banner 'knife digital_ocean droplet create (options)'
21
+
22
+ option :server_name,
23
+ :short => '-N NAME',
24
+ :long => '--server-name NAME',
25
+ :description => 'The server name',
26
+ :proc => Proc.new { |server_name| Chef::Config[:knife][:server_name] = server_name }
27
+
28
+ option :image,
29
+ :short => '-I IMAGE',
30
+ :long => '--image IMAGE',
31
+ :description => 'Your DigitalOcean Image',
32
+ :proc => Proc.new { |image| Chef::Config[:knife][:image] = image }
33
+
34
+ option :size,
35
+ :short => '-S SIZE',
36
+ :long => '--size SIZE',
37
+ :description => 'Your DigitalOcean Size',
38
+ :proc => Proc.new { |size| Chef::Config[:knife][:size] = size }
39
+
40
+ option :location,
41
+ :short => '-L REGION',
42
+ :long => '--location REGION',
43
+ :description => 'DigitalOcean Location (Region)',
44
+ :proc => Proc.new { |location| Chef::Config[:knife][:location] = location }
45
+
46
+ # FIXME splitting + kniferb
47
+ option :ssh_key_ids,
48
+ :short => '-K KEYID',
49
+ :long => '--ssh-keys KEY_ID',
50
+ :description => 'Comma spearated list of your SSH key ids',
51
+ :proc => lambda { |o| o.split(/[\s,]+/) }
52
+
53
+ option :identity_file,
54
+ :short => '-i IDENTITY_FILE',
55
+ :long => '--identity-file IDENTITY_FILE',
56
+ :description => 'The SSH identity file used for authentication',
57
+ :proc => Proc.new { |identity| Chef::Config[:knife][:identity_file] = identity }
58
+
59
+ option :bootstrap,
60
+ :short => '-B',
61
+ :long => '--bootstraps',
62
+ :boolean => false,
63
+ :description => 'Do a chef-client bootstrap on the create droplet (for use with chef-server)'
64
+
65
+ option :ssh_user,
66
+ :short => '-x USERNAME',
67
+ :long => '--ssh-user USERNAME',
68
+ :description => 'The ssh username; default is "root"',
69
+ :default => 'root'
70
+
71
+ option :distro,
72
+ :short => '-d DISTRO',
73
+ :long => '--distro DISTRO',
74
+ :description => 'Chef-Bootstrap a distro using a template; default is "ubuntu10.04-gems"',
75
+ :proc => Proc.new { |d| Chef::Config[:knife][:distro] = d },
76
+ :default => 'chef-full'
77
+
78
+ option :run_list,
79
+ :short => '-r RUN_LIST',
80
+ :long => '--run-list RUN_LIST',
81
+ :description => 'Comma separated list of roles/recipes to apply',
82
+ :proc => lambda { |o| o.split(/[\s,]+/) },
83
+ :default => []
84
+
85
+ def run
86
+ $stdout.sync = true
87
+
88
+ validate!
89
+
90
+ unless locate_config_value(:server_name)
91
+ ui.error("Server Name cannot be empty: -N <servername>")
92
+ exit 1
93
+ end
94
+
95
+ unless locate_config_value(:image)
96
+ ui.error("Image cannot be empty: -I <image>")
97
+ exit 1
98
+ end
99
+
100
+ unless locate_config_value(:size)
101
+ ui.error("Size cannot be empty: -S <size>")
102
+ exit 1
103
+ end
104
+
105
+ unless locate_config_value(:location)
106
+ ui.error("Location cannot be empty: -L <region>")
107
+ exit 1
108
+ end
109
+
110
+ unless locate_config_value(:ssh_key_ids)
111
+ ui.error("One or more DigitalOcean SSH key ids missing: -K <KEY1>, <KEY2> ...")
112
+ exit 1
113
+ end
114
+
115
+ response = client.droplets.create(:name => locate_config_value(:server_name),
116
+ :size_id => locate_config_value(:size),
117
+ :image_id => locate_config_value(:image),
118
+ :region_id => locate_config_value(:location),
119
+ :ssh_key_ids => locate_config_value(:ssh_key_ids).join(','))
120
+
121
+ if response.status != "OK"
122
+ ui.error("Droplet could not be started #{response.inspect}")
123
+ exit 1
124
+ end
125
+
126
+ puts "Droplet creation for #{locate_config_value(:server_name)} started. Droplet-ID is #{response.droplet.id}"
127
+
128
+ print "\n#{ui.color("Waiting for IPv4-Address", :magenta)}"
129
+ print(".") until ip_address = ip_address_available(response.droplet.id) {
130
+ puts 'done'
131
+ }
132
+
133
+ puts ui.color("IPv4 address is: #{ip_address}", :green)
134
+
135
+ print ui.color("Waiting for sshd:", :magenta)
136
+ print('.') until tcp_test_ssh(ip_address, 22) {
137
+ sleep 2
138
+ puts 'done'
139
+ }
140
+
141
+ if locate_config_value(:bootstrap)
142
+ bootstrap_for_node(ip_address).run
143
+ else
144
+ puts ip_address
145
+ exit 0
146
+ end
147
+
148
+ end
149
+
150
+ def ip_address_available(droplet_id)
151
+ response = client.droplets.show(droplet_id)
152
+ if response.droplet.ip_address
153
+ yield
154
+ response.droplet.ip_address
155
+ else
156
+ sleep @initial_sleep_delay ||= 10
157
+ false
158
+ end
159
+ end
160
+
161
+ def tcp_test_ssh(hostname, port)
162
+ tcp_socket = TCPSocket.new(hostname, port)
163
+ readable = IO.select([tcp_socket], nil, nil, 5)
164
+ if readable
165
+ Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
166
+ yield
167
+ true
168
+ else
169
+ false
170
+ end
171
+ rescue Errno::ETIMEDOUT
172
+ false
173
+ rescue Errno::EPERM
174
+ false
175
+ rescue Errno::ECONNREFUSED
176
+ sleep 2
177
+ false
178
+ rescue Errno::EHOSTUNREACH
179
+ sleep 2
180
+ false
181
+ ensure
182
+ tcp_socket && tcp_socket.close
183
+ end
184
+
185
+ def bootstrap_for_node(ip_address)
186
+ bootstrap = Chef::Knife::Bootstrap.new
187
+ bootstrap.name_args = [ ip_address ]
188
+ bootstrap.config[:run_list] = config[:run_list]
189
+ bootstrap.config[:ssh_user] = config[:ssh_user]
190
+ bootstrap.config[:identity_file] = config[:identity_file]
191
+ bootstrap.config[:chef_node_name] = locate_config_value(:server_name)
192
+ bootstrap.config[:prerelease] = config[:prerelease]
193
+ bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
194
+ bootstrap.config[:distro] = locate_config_value(:distro)
195
+ bootstrap.config[:use_sudo] = true unless config[:ssh_user] == 'root'
196
+ bootstrap.config[:template_file] = locate_config_value(:template_file)
197
+ bootstrap.config[:environment] = config[:environment]
198
+ bootstrap.config[:host_key_verify] = config[:host_key_verify]
199
+ bootstrap
200
+ end
201
+
202
+ end
203
+ end
204
+ end
@@ -0,0 +1,43 @@
1
+ # Licensed under the Apache License, Version 2.0 (the "License");
2
+ # you may not use this file except in compliance with the License.
3
+ # You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+ #
13
+ require File.expand_path('../digital_ocean_base', __FILE__)
14
+
15
+ class Chef
16
+ class Knife
17
+ class DigitalOceanDropletDestroy < Knife
18
+ include Knife::DigitalOceanBase
19
+
20
+ banner 'knife digital_ocean droplet destroy (options)'
21
+
22
+ option :server,
23
+ :short => '-S ID',
24
+ :long => '--server ID',
25
+ :description => 'The server id'
26
+
27
+ def run
28
+ $stdout.sync = true
29
+
30
+ validate!
31
+
32
+ unless locate_config_value(:server)
33
+ ui.error("Server cannot be empty. ALL DATA WILL BE LOST! => -S <server-id>")
34
+ exit 1
35
+ end
36
+
37
+ result = client.droplets.delete locate_config_value(:server)
38
+ puts result.status
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,62 @@
1
+ # Licensed under the Apache License, Version 2.0 (the "License");
2
+ # you may not use this file except in compliance with the License.
3
+ # You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+ #
13
+ require File.expand_path('../digital_ocean_base', __FILE__)
14
+
15
+ class Chef
16
+ class Knife
17
+ class DigitalOceanDropletList < Knife
18
+ include Knife::DigitalOceanBase
19
+
20
+ banner 'knife digital_ocean droplet list (options)'
21
+
22
+ def run
23
+ $stdout.sync = true
24
+
25
+ validate!
26
+
27
+ droplet_list = [
28
+ h.color('ID', :bold),
29
+ h.color('Name', :bold),
30
+ h.color('Size', :bold),
31
+ h.color('Region', :bold),
32
+ h.color('IPv4', :bold),
33
+ h.color('Image', :bold),
34
+ h.color('Status', :bold)
35
+ ]
36
+
37
+ regions = client.regions.list.regions.inject({}) do |h, region|
38
+ h[region.id] = region.name
39
+ h
40
+ end
41
+
42
+ sizes = client.sizes.list.sizes.inject({}) do |h, size|
43
+ h[size.id] = size.name
44
+ h
45
+ end
46
+
47
+ client.droplets.list.droplets.each do |droplet|
48
+ droplet_list << droplet.id.to_s
49
+ droplet_list << droplet.name.to_s
50
+ droplet_list << sizes[droplet.size_id]
51
+ droplet_list << regions[droplet.region_id]
52
+ droplet_list << droplet.ip_address.to_s
53
+ droplet_list << droplet.image_id.to_s + ' (' + client.images.show(droplet.image_id).image.name + ')'
54
+ droplet_list << droplet.status.to_s
55
+ end
56
+
57
+ puts h.list(droplet_list, :uneven_columns_across, 7)
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,71 @@
1
+ # Licensed under the Apache License, Version 2.0 (the "License");
2
+ # you may not use this file except in compliance with the License.
3
+ # You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+ #
13
+ require File.expand_path('../digital_ocean_base', __FILE__)
14
+
15
+ class Chef
16
+ class Knife
17
+ class DigitalOceanImageList < Knife
18
+ include Knife::DigitalOceanBase
19
+
20
+ banner 'knife digital_ocean image list (options)'
21
+
22
+ option :global_images,
23
+ :short => '-G',
24
+ :long => '--global',
25
+ :description => 'Show global images'
26
+
27
+ def run
28
+ $stdout.sync = true
29
+
30
+ validate!
31
+
32
+ image_list = [
33
+ h.color('ID', :bold),
34
+ h.color('Distribution', :bold),
35
+ h.color('Name', :bold),
36
+ h.color('Global', :bold)
37
+ ]
38
+
39
+ if config[:global_images]
40
+ filter = 'global'
41
+ else
42
+ filter = 'my_images'
43
+ end
44
+
45
+ images = client.images.list(:filter => filter).images
46
+
47
+ images.sort! do |a, b|
48
+ [ a.distribution, a.name ] <=> [ b.distribution, b.name ]
49
+ end
50
+
51
+ images.each do |image|
52
+ image_list << image.id.to_s
53
+ image_list << image.distribution.to_s
54
+ image_list << image.name.to_s
55
+ image_list << truefalse(config[:global_images])
56
+ end
57
+
58
+ puts h.list(image_list, :uneven_columns_across, 4)
59
+ end
60
+
61
+ def truefalse(boolean)
62
+ if boolean
63
+ '+'
64
+ else
65
+ '-'
66
+ end
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,47 @@
1
+ # Licensed under the Apache License, Version 2.0 (the "License");
2
+ # you may not use this file except in compliance with the License.
3
+ # You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+ #
13
+ require File.expand_path('../digital_ocean_base', __FILE__)
14
+
15
+ class Chef
16
+ class Knife
17
+ class DigitalOceanRegionList < Knife
18
+ include Knife::DigitalOceanBase
19
+
20
+ banner 'knife digital_ocean region list (options)'
21
+
22
+ def run
23
+ $stdout.sync = true
24
+
25
+ validate!
26
+
27
+ region_list = [
28
+ h.color('ID', :bold),
29
+ h.color('Name', :bold)
30
+ ]
31
+
32
+ regions = client.regions.list.regions
33
+
34
+ regions.sort! do |a, b|
35
+ a.name.to_i <=> b.name.to_i
36
+ end
37
+
38
+ regions.each do |region|
39
+ region_list << region.id.to_s
40
+ region_list << region.name.to_s
41
+ end
42
+
43
+ puts h.list(region_list, :uneven_columns_across, 2)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,47 @@
1
+ # Licensed under the Apache License, Version 2.0 (the "License");
2
+ # you may not use this file except in compliance with the License.
3
+ # You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+ #
13
+ require File.expand_path('../digital_ocean_base', __FILE__)
14
+
15
+ class Chef
16
+ class Knife
17
+ class DigitalOceanSizeList < Knife
18
+ include Knife::DigitalOceanBase
19
+
20
+ banner 'knife digital_ocean size list (options)'
21
+
22
+ def run
23
+ $stdout.sync = true
24
+
25
+ validate!
26
+
27
+ size_list = [
28
+ h.color('ID', :bold),
29
+ h.color('Name', :bold)
30
+ ]
31
+
32
+ sizes = client.sizes.list.sizes
33
+
34
+ sizes.sort! do |a, b|
35
+ a.name.to_i <=> b.name.to_i
36
+ end
37
+
38
+ sizes.each do |size|
39
+ size_list << size.id.to_s
40
+ size_list << size.name.to_s
41
+ end
42
+
43
+ puts h.list(size_list, :uneven_columns_across, 2)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,48 @@
1
+ # Licensed under the Apache License, Version 2.0 (the "License");
2
+ # you may not use this file except in compliance with the License.
3
+ # You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+ #
13
+ require File.expand_path('../digital_ocean_base', __FILE__)
14
+
15
+ class Chef
16
+ class Knife
17
+ class DigitalOceanSshkeyList < Knife
18
+ include Knife::DigitalOceanBase
19
+
20
+ banner 'knife digital_ocean sshkey list'
21
+
22
+ def run
23
+ $stdout.sync = true
24
+
25
+ validate!
26
+
27
+ sshkey_list = [
28
+ h.color('ID', :bold),
29
+ h.color('Name', :bold),
30
+ ]
31
+
32
+ sshkeys = client.ssh_keys.list.ssh_keys
33
+
34
+ sshkeys.sort! do |a, b|
35
+ a.name <=> b.name
36
+ end
37
+
38
+ sshkeys.each do |sshkey|
39
+ sshkey_list << sshkey.id.to_s
40
+ sshkey_list << sshkey.name.to_s
41
+ end
42
+
43
+ puts h.list(sshkey_list, :uneven_columns_across, 2)
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,7 @@
1
+ require "knife-digital_ocean/version"
2
+
3
+ module Knife
4
+ module DigitalOcean
5
+ # Your code goes here...
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Knife
2
+ module DigitalOcean
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'rspec/autorun'
3
+ require 'knife-digital_ocean'
4
+
5
+
6
+
7
+ Dir['./spec/support/**/*.rb'].sort.each {|f| require f}
8
+
9
+
metadata ADDED
@@ -0,0 +1,215 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-digital_ocean
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Roland Moriz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chef
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '10.18'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '10.18'
30
+ - !ruby/object:Gem::Dependency
31
+ name: digital_ocean
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: highline
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: net-ssh-multi
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '1.1'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '1.1'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '2.0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '2.0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: guard
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: guard-rspec
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rb-fsevent
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: rake
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ description: A plugin for chef's knife to manage instances of DigitalOcean servers
159
+ email:
160
+ - roland@moriz.de
161
+ executables: []
162
+ extensions: []
163
+ extra_rdoc_files: []
164
+ files:
165
+ - .gitignore
166
+ - .travis.yml
167
+ - Gemfile
168
+ - LICENSE.txt
169
+ - README.md
170
+ - Rakefile
171
+ - knife-digital_ocean.gemspec
172
+ - lib/chef/knife/digital_ocean_base.rb
173
+ - lib/chef/knife/digital_ocean_droplet_create.rb
174
+ - lib/chef/knife/digital_ocean_droplet_destroy.rb
175
+ - lib/chef/knife/digital_ocean_droplet_list.rb
176
+ - lib/chef/knife/digital_ocean_image_list.rb
177
+ - lib/chef/knife/digital_ocean_region_list.rb
178
+ - lib/chef/knife/digital_ocean_size_list.rb
179
+ - lib/chef/knife/digital_ocean_sshkey_list.rb
180
+ - lib/knife-digital_ocean.rb
181
+ - lib/knife-digital_ocean/version.rb
182
+ - spec/spec_helper.rb
183
+ homepage: http://github.com/rmoriz/knife-digital_ocean
184
+ licenses:
185
+ - Apache 2.0
186
+ post_install_message:
187
+ rdoc_options: []
188
+ require_paths:
189
+ - lib
190
+ required_ruby_version: !ruby/object:Gem::Requirement
191
+ none: false
192
+ requirements:
193
+ - - ! '>='
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ segments:
197
+ - 0
198
+ hash: 3713788869412067883
199
+ required_rubygems_version: !ruby/object:Gem::Requirement
200
+ none: false
201
+ requirements:
202
+ - - ! '>='
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ segments:
206
+ - 0
207
+ hash: 3713788869412067883
208
+ requirements: []
209
+ rubyforge_project:
210
+ rubygems_version: 1.8.23
211
+ signing_key:
212
+ specification_version: 3
213
+ summary: A plugin for chef's knife to manage instances of DigitalOcean servers
214
+ test_files:
215
+ - spec/spec_helper.rb