knife-digital_ocean 0.7.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,9 @@
1
1
  # Licensed under the Apache License, Version 2.0 (the "License");
2
2
  # you may not use this file except in compliance with the License.
3
3
  # You may obtain a copy of the License at
4
- #
4
+ #
5
5
  # http://www.apache.org/licenses/LICENSE-2.0
6
- #
6
+ #
7
7
  # Unless required by applicable law or agreed to in writing, software
8
8
  # distributed under the License is distributed on an "AS IS" BASIS,
9
9
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -30,120 +30,120 @@ class Chef
30
30
  banner 'knife digital_ocean droplet create (options)'
31
31
 
32
32
  option :server_name,
33
- :short => '-N NAME',
34
- :long => '--server-name NAME',
35
- :description => 'The server name',
36
- :proc => Proc.new { |server_name| Chef::Config[:knife][:server_name] = server_name }
33
+ short: '-N NAME',
34
+ long: '--server-name NAME',
35
+ description: 'The server name',
36
+ proc: proc { |server_name| Chef::Config[:knife][:server_name] = server_name }
37
37
 
38
38
  option :image,
39
- :short => '-I IMAGE',
40
- :long => '--image IMAGE',
41
- :description => 'Your DigitalOcean Image',
42
- :proc => Proc.new { |image| Chef::Config[:knife][:image] = image }
39
+ short: '-I IMAGE',
40
+ long: '--image IMAGE',
41
+ description: 'Your DigitalOcean Image',
42
+ proc: proc { |image| Chef::Config[:knife][:image] = image }
43
43
 
44
44
  option :size,
45
- :short => '-S SIZE',
46
- :long => '--size SIZE',
47
- :description => 'Your DigitalOcean Size',
48
- :proc => Proc.new { |size| Chef::Config[:knife][:size] = size }
45
+ short: '-S SIZE',
46
+ long: '--size SIZE',
47
+ description: 'Your DigitalOcean Size',
48
+ proc: proc { |size| Chef::Config[:knife][:size] = size }
49
49
 
50
50
  option :location,
51
- :short => '-L REGION',
52
- :long => '--location REGION',
53
- :description => 'DigitalOcean Location (Region)',
54
- :proc => Proc.new { |location| Chef::Config[:knife][:location] = location }
51
+ short: '-L REGION',
52
+ long: '--location REGION',
53
+ description: 'DigitalOcean Location (Region)',
54
+ proc: proc { |location| Chef::Config[:knife][:location] = location }
55
55
 
56
56
  option :ssh_key_ids,
57
- :short => '-K KEYID',
58
- :long => '--ssh-keys KEY_ID',
59
- :description => 'Comma spearated list of your SSH key ids',
60
- :proc => lambda { |o| o.split(/[\s,]+/) }
57
+ short: '-K KEYID',
58
+ long: '--ssh-keys KEY_ID',
59
+ description: 'Comma spearated list of your SSH key ids',
60
+ proc: lambda { |o| o.split(/[\s,]+/) }
61
61
 
62
62
  option :identity_file,
63
- :short => '-i IDENTITY_FILE',
64
- :long => '--identity-file IDENTITY_FILE',
65
- :description => 'The SSH identity file used for authentication',
66
- :proc => Proc.new { |identity| Chef::Config[:knife][:identity_file] = identity }
63
+ short: '-i IDENTITY_FILE',
64
+ long: '--identity-file IDENTITY_FILE',
65
+ description: 'The SSH identity file used for authentication',
66
+ proc: proc { |identity| Chef::Config[:knife][:identity_file] = identity }
67
67
 
68
68
  option :bootstrap,
69
- :short => '-B',
70
- :long => '--bootstrap',
71
- :description => 'Do a chef-client bootstrap on the created droplet (for use with chef-server)'
69
+ short: '-B',
70
+ long: '--bootstrap',
71
+ description: 'Do a chef-client bootstrap on the created droplet (for use with chef-server)'
72
72
 
73
73
  option :solo,
74
- :long => '--[no-]solo',
75
- :description => 'Do a chef-solo bootstrap on the droplet using knife-solo',
76
- :proc => Proc.new { |s| Chef::Config[:knife][:solo] = s }
74
+ long: '--[no-]solo',
75
+ description: 'Do a chef-solo bootstrap on the droplet using knife-solo',
76
+ proc: proc { |s| Chef::Config[:knife][:solo] = s }
77
77
 
78
78
  option :ssh_user,
79
- :short => '-x USERNAME',
80
- :long => '--ssh-user USERNAME',
81
- :description => 'The ssh username; default is "root"',
82
- :default => 'root'
79
+ short: '-x USERNAME',
80
+ long: '--ssh-user USERNAME',
81
+ description: 'The ssh username; default is "root"',
82
+ default: 'root'
83
83
 
84
84
  option :distro,
85
- :short => '-d DISTRO',
86
- :long => '--distro DISTRO',
87
- :description => 'Chef-Bootstrap a distro using a template; default is "chef-full"',
88
- :proc => Proc.new { |d| Chef::Config[:knife][:distro] = d },
89
- :default => 'chef-full'
85
+ short: '-d DISTRO',
86
+ long: '--distro DISTRO',
87
+ description: 'Chef-Bootstrap a distro using a template; default is "chef-full"',
88
+ proc: proc { |d| Chef::Config[:knife][:distro] = d },
89
+ default: 'chef-full'
90
90
 
91
91
  option :run_list,
92
- :short => '-r RUN_LIST',
93
- :long => '--run-list RUN_LIST',
94
- :description => 'Comma separated list of roles/recipes to apply',
95
- :proc => lambda { |o| o.split(/[\s,]+/) },
96
- :default => []
92
+ short: '-r RUN_LIST',
93
+ long: '--run-list RUN_LIST',
94
+ description: 'Comma separated list of roles/recipes to apply',
95
+ proc: lambda { |o| o.split(/[\s,]+/) },
96
+ default: []
97
97
 
98
98
  option :template_file,
99
- :long => "--template-file TEMPLATE",
100
- :description => "Full path to location of template to use",
101
- :proc => Proc.new { |t| Chef::Config[:knife][:template_file] = t },
102
- :default => false
99
+ long: '--template-file TEMPLATE',
100
+ description: 'Full path to location of template to use',
101
+ proc: proc { |t| Chef::Config[:knife][:template_file] = t },
102
+ default: false
103
103
 
104
104
  option :host_key_verify,
105
- :long => "--[no-]host-key-verify",
106
- :description => "Verify host key, enabled by default",
107
- :default => true
105
+ long: '--[no-]host-key-verify',
106
+ description: 'Verify host key, enabled by default',
107
+ default: true
108
108
 
109
109
  option :prerelease,
110
- :long => "--prerelease",
111
- :description => "Install the pre-release chef gems"
110
+ long: '--prerelease',
111
+ description: 'Install the pre-release chef gems'
112
112
 
113
113
  option :bootstrap_version,
114
- :long => "--bootstrap-version VERSION",
115
- :description => "The version of Chef to install",
116
- :proc => Proc.new { |v| Chef::Config[:knife][:bootstrap_version] = v }
114
+ long: '--bootstrap-version VERSION',
115
+ description: 'The version of Chef to install',
116
+ proc: proc { |v| Chef::Config[:knife][:bootstrap_version] = v }
117
117
 
118
118
  option :environment,
119
- :short => "-E ENVIRONMENT",
120
- :long => "--environment ENVIRONMENT",
121
- :description => "The name of the chef environment to use",
122
- :proc => Proc.new { |e| Chef::Config[:knife][:environment] = e },
123
- :default => '_default'
119
+ short: '-E ENVIRONMENT',
120
+ long: '--environment ENVIRONMENT',
121
+ description: 'The name of the chef environment to use',
122
+ proc: proc { |e| Chef::Config[:knife][:environment] = e },
123
+ default: '_default'
124
124
 
125
125
  option :json_attributes,
126
- :short => '-j JSON',
127
- :long => '--json-attributes JSON',
128
- :description => 'A JSON string to be added to the first run of chef-client',
129
- :proc => lambda { |o| JSON.parse(o) }
126
+ short: '-j JSON',
127
+ long: '--json-attributes JSON',
128
+ description: 'A JSON string to be added to the first run of chef-client',
129
+ proc: lambda { |o| JSON.parse(o) }
130
130
 
131
131
  option :private_networking,
132
- :long => "--private_networking",
133
- :description => "Enables private networking if the selected region supports it",
134
- :default => false
132
+ long: '--private_networking',
133
+ description: 'Enables private networking if the selected region supports it',
134
+ default: false
135
135
 
136
136
  option :secret_file,
137
- :long => "--secret-file SECRET_FILE",
138
- :description => "A file containing the secret key to use to encrypt data bag item values",
139
- :proc => Proc.new { |sf| Chef::Config[:knife][:secret_file] = sf }
137
+ long: '--secret-file SECRET_FILE',
138
+ description: 'A file containing the secret key to use to encrypt data bag item values',
139
+ proc: proc { |sf| Chef::Config[:knife][:secret_file] = sf }
140
140
 
141
141
  option :ssh_port,
142
- :short => "-p PORT",
143
- :long => "--ssh-port PORT",
144
- :description => "The ssh port",
145
- :default => "22",
146
- :proc => Proc.new { |port| Chef::Config[:knife][:ssh_port] = port }
142
+ short: '-p PORT',
143
+ long: '--ssh-port PORT',
144
+ description: 'The ssh port',
145
+ default: '22',
146
+ proc: proc { |port| Chef::Config[:knife][:ssh_port] = port }
147
147
 
148
148
  def run
149
149
  $stdout.sync = true
@@ -180,40 +180,42 @@ class Chef
180
180
  'Knife plugin knife-solo was not found.',
181
181
  'Please add the knife-solo gem to your Gemfile or',
182
182
  'install it manually with `gem install knife-solo`.'
183
- ].join(" ")
183
+ ].join(' ')
184
184
  exit 1
185
185
  end
186
186
 
187
- response = client.droplets.create(:name => locate_config_value(:server_name),
188
- :size_id => locate_config_value(:size),
189
- :image_id => locate_config_value(:image),
190
- :region_id => locate_config_value(:location),
191
- :ssh_key_ids => locate_config_value(:ssh_key_ids).join(','),
192
- :private_networking => locate_config_value(:private_networking))
187
+ droplet = DropletKit::Droplet.new(name: locate_config_value(:server_name),
188
+ size: locate_config_value(:size),
189
+ image: locate_config_value(:image),
190
+ region: locate_config_value(:location),
191
+ ssh_keys: locate_config_value(:ssh_key_ids).split(/, ?/),
192
+ private_networking: locate_config_value(:private_networking))
193
193
 
194
- if response.status != 'OK'
195
- ui.error("Droplet could not be started #{response.inspect}")
196
- exit 1
197
- end
194
+ server = client.droplets.create(droplet)
198
195
 
199
- puts "Droplet creation for #{locate_config_value(:server_name)} started. Droplet-ID is #{response.droplet.id}"
196
+ # if client.droplets.find(id: server.id).status != 'in-progress'
197
+ # ui.error("Droplet could not be started #{server.inspect}")
198
+ # exit 1
199
+ # end
200
200
 
201
- unless !config.has_key?(:json_attributes) || config[:json_attributes].empty?
201
+ puts "Droplet creation for #{locate_config_value(:server_name)} started. Droplet-ID is #{server.id}"
202
+
203
+ unless !config.key?(:json_attributes) || config[:json_attributes].empty?
202
204
  puts ui.color("JSON Attributes: #{config[:json_attributes]}", :magenta)
203
205
  end
204
206
 
205
- print ui.color("Waiting for IPv4-Address", :magenta)
206
- print(".") until ip_address = ip_address_available(response.droplet.id) {
207
+ print ui.color('Waiting for IPv4-Address', :magenta)
208
+ print('.') until ip_address = ip_address_available(server.id) do
207
209
  puts 'done'
208
- }
210
+ end
209
211
 
210
212
  puts ui.color("IPv4 address is: #{ip_address}", :green)
211
213
 
212
214
  print ui.color('Waiting for sshd:', :magenta)
213
- print('.') until tcp_test_ssh(ip_address) {
215
+ print('.') until tcp_test_ssh(ip_address) do
214
216
  sleep 2
215
217
  puts 'done'
216
- }
218
+ end
217
219
 
218
220
  if locate_config_value(:bootstrap) || solo_bootstrap?
219
221
  bootstrap_for_node(ip_address).run
@@ -221,14 +223,13 @@ class Chef
221
223
  puts ip_address
222
224
  exit 0
223
225
  end
224
-
225
226
  end
226
227
 
227
228
  def ip_address_available(droplet_id)
228
- response = client.droplets.show(droplet_id)
229
- if response.droplet.ip_address
229
+ server = client.droplets.find(id: droplet_id)
230
+ if server.public_ip
230
231
  yield
231
- response.droplet.ip_address
232
+ server.public_ip
232
233
  else
233
234
  sleep @initial_sleep_delay ||= 10
234
235
  false
@@ -262,7 +263,7 @@ class Chef
262
263
 
263
264
  def bootstrap_for_node(ip_address)
264
265
  bootstrap = bootstrap_class.new
265
- bootstrap.name_args = [ ip_address ]
266
+ bootstrap.name_args = [ip_address]
266
267
  bootstrap.config.merge! config
267
268
  bootstrap.config[:chef_node_name] = locate_config_value(:server_name)
268
269
  bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
@@ -1,9 +1,9 @@
1
1
  # Licensed under the Apache License, Version 2.0 (the "License");
2
2
  # you may not use this file except in compliance with the License.
3
3
  # You may obtain a copy of the License at
4
- #
4
+ #
5
5
  # http://www.apache.org/licenses/LICENSE-2.0
6
- #
6
+ #
7
7
  # Unless required by applicable law or agreed to in writing, software
8
8
  # distributed under the License is distributed on an "AS IS" BASIS,
9
9
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,9 +20,9 @@ class Chef
20
20
  banner 'knife digital_ocean droplet destroy (options)'
21
21
 
22
22
  option :server,
23
- :short => '-S ID',
24
- :long => '--server ID',
25
- :description => 'The server id'
23
+ short: '-S ID',
24
+ long: '--server ID',
25
+ description: 'The server id'
26
26
 
27
27
  def run
28
28
  $stdout.sync = true
@@ -30,14 +30,13 @@ class Chef
30
30
  validate!
31
31
 
32
32
  unless locate_config_value(:server)
33
- ui.error("Server cannot be empty. ALL DATA WILL BE LOST! => -S <server-id>")
33
+ ui.error('Server cannot be empty. ALL DATA WILL BE LOST! => -S <server-id>')
34
34
  exit 1
35
35
  end
36
36
 
37
- result = client.droplets.delete locate_config_value(:server)
38
- puts result.status
37
+ result = client.droplets.delete(id: locate_config_value(:server))
38
+ puts JSON.parse(result)['message'] rescue 'OK'
39
39
  end
40
-
41
40
  end
42
41
  end
43
42
  end
@@ -1,9 +1,9 @@
1
1
  # Licensed under the Apache License, Version 2.0 (the "License");
2
2
  # you may not use this file except in compliance with the License.
3
3
  # You may obtain a copy of the License at
4
- #
4
+ #
5
5
  # http://www.apache.org/licenses/LICENSE-2.0
6
- #
6
+ #
7
7
  # Unless required by applicable law or agreed to in writing, software
8
8
  # distributed under the License is distributed on an "AS IS" BASIS,
9
9
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -25,46 +25,28 @@ class Chef
25
25
  validate!
26
26
 
27
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)
28
+ ui.color('ID', :bold),
29
+ ui.color('Name', :bold),
30
+ ui.color('Size', :bold),
31
+ ui.color('Region', :bold),
32
+ ui.color('IPv4', :bold),
33
+ ui.color('Image', :bold),
34
+ ui.color('Status', :bold)
35
35
  ]
36
+ droplets = client.droplets.all
36
37
 
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|
38
+ droplets.each do |droplet|
48
39
  droplet_list << droplet.id.to_s
49
40
  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
-
54
- image_details = client.images.show(droplet.image_id)
55
- if image_details.status != 'OK'
56
- image_os_info = 'N/A'
57
- else
58
- image_os_info = image_details.image.name
59
- end
60
-
61
- droplet_list << droplet.image_id.to_s + ' (' + image_os_info + ')'
41
+ droplet_list << droplet.size_slug.to_s
42
+ droplet_list << droplet.region.name.to_s
43
+ droplet_list << droplet.public_ip.to_s
44
+ droplet_list << droplet.image.name.to_s
62
45
  droplet_list << droplet.status.to_s
63
46
  end
64
47
 
65
- puts h.list(droplet_list, :uneven_columns_across, 7)
48
+ puts ui.list(droplet_list, :uneven_columns_across, 7)
66
49
  end
67
-
68
50
  end
69
51
  end
70
52
  end
@@ -1,9 +1,9 @@
1
1
  # Licensed under the Apache License, Version 2.0 (the "License");
2
2
  # you may not use this file except in compliance with the License.
3
3
  # You may obtain a copy of the License at
4
- #
4
+ #
5
5
  # http://www.apache.org/licenses/LICENSE-2.0
6
- #
6
+ #
7
7
  # Unless required by applicable law or agreed to in writing, software
8
8
  # distributed under the License is distributed on an "AS IS" BASIS,
9
9
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -19,10 +19,10 @@ class Chef
19
19
 
20
20
  banner 'knife digital_ocean image list (options)'
21
21
 
22
- option :global_images,
23
- :short => '-G',
24
- :long => '--global',
25
- :description => 'Show global images'
22
+ option :public_images,
23
+ short: '-P',
24
+ long: '--public',
25
+ description: 'Show public images'
26
26
 
27
27
  def run
28
28
  $stdout.sync = true
@@ -30,42 +30,29 @@ class Chef
30
30
  validate!
31
31
 
32
32
  image_list = [
33
- h.color('ID', :bold),
34
- h.color('Distribution', :bold),
35
- h.color('Name', :bold),
36
- h.color('Global', :bold)
33
+ ui.color('ID', :bold),
34
+ ui.color('Distribution', :bold),
35
+ ui.color('Name', :bold),
36
+ ui.color('Slug', :bold)
37
37
  ]
38
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
39
+ images = client.images.all
46
40
 
47
- images.sort! do |a, b|
48
- [ a.distribution, a.name ] <=> [ b.distribution, b.name ]
41
+ if config[:public_images]
42
+ found_images = images.find_all { |i| i.public == true }
43
+ else
44
+ found_images = images.find_all { |i| i.public == false }
49
45
  end
50
46
 
51
- images.each do |image|
47
+ found_images.each do |image|
52
48
  image_list << image.id.to_s
53
49
  image_list << image.distribution.to_s
54
50
  image_list << image.name.to_s
55
- image_list << truefalse(config[:global_images])
51
+ image_list << image.slug.to_s
56
52
  end
57
53
 
58
- puts h.list(image_list, :uneven_columns_across, 4)
54
+ puts ui.list(image_list, :uneven_columns_across, 4)
59
55
  end
60
-
61
- def truefalse(boolean)
62
- if boolean
63
- '+'
64
- else
65
- '-'
66
- end
67
- end
68
-
69
56
  end
70
57
  end
71
58
  end