knife-joyent 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,11 +26,14 @@ Currently available commands:
26
26
  knife joyent key delete <name>
27
27
  knife joyent key list
28
28
  knife joyent server create (options)
29
+ knife joyent server delete <server_id>
29
30
  knife joyent server list <options>
30
31
  knife joyent server reboot <server_id>
31
32
  knife joyent server resize <server_id> -f <flavor>
32
33
  knife joyent server start <server_id>
33
34
  knife joyent server stop <server_id>
35
+ knife joyent server metadata update <server_id> -m <json>
36
+ knife joyent server metadata delete <server_id> <options>
34
37
  knife joyent snapshot create <server_id> <snapshot_name>
35
38
  knife joyent snapshot delete <server_id> <snapshot_name>
36
39
  knife joyent snapshot list <server_id>
@@ -70,6 +73,16 @@ provider powered by [SmartDataCenter](http://www.joyent.com/products/smartdatace
70
73
  # Defaults to https://us-west-1.api.joyentcloud.com/
71
74
  knife[:joyent_api_url] = "https://us-sw-1.api.joyentcloud.com/"
72
75
 
76
+ **joyent_metadata**
77
+
78
+ Metadata to apply to each provisioned machine via the Metadata API. This should take
79
+ the form of a hash with a single level of nesting. See the
80
+ [Metadata API](http://wiki.joyent.com/wiki/display/sdc/Using+the+Metadata+API) for more info.
81
+
82
+ knife[:joyent_metadata] = {
83
+ "some_data" => "value"
84
+ }
85
+
73
86
  ## Contributors
74
87
 
75
88
  - [Sean Omera](https://github.com/someara) - Opscode
@@ -50,7 +50,7 @@ class Chef
50
50
  :description => "A JSON string to be added to the first run of chef-client",
51
51
  :proc => lambda { |o| JSON.parse(o) },
52
52
  :default => {}
53
-
53
+
54
54
  option :private_network,
55
55
  :long => "--private-network",
56
56
  :description => "Use the private IP for bootstrapping rather than the public IP",
@@ -79,6 +79,12 @@ class Chef
79
79
  :proc => Proc.new { |d| Chef::Config[:knife][:distro] = d },
80
80
  :default => "chef-full"
81
81
 
82
+ option :joyent_metadata,
83
+ :long => '--metadata JSON',
84
+ :description => 'Metadata to apply to machine',
85
+ :proc => Proc.new { |m| JSON.parse(m) },
86
+ :default => {}
87
+
82
88
  option :no_host_key_verify,
83
89
  :long => "--no-host-key-verify",
84
90
  :description => "Disable host key verification",
@@ -89,12 +95,12 @@ class Chef
89
95
  linklocal = IPAddr.new "169.254.0.0/16"
90
96
  return linklocal.include?(ip)
91
97
  end
92
-
98
+
93
99
  def is_loopback(ip)
94
100
  loopback = IPAddr.new "127.0.0.0/8"
95
101
  return loopback.include?(ip)
96
102
  end
97
-
103
+
98
104
  def is_private(ip)
99
105
  block_a = IPAddr.new "10.0.0.0/8"
100
106
  block_b = IPAddr.new "172.16.0.0/12"
@@ -130,22 +136,17 @@ class Chef
130
136
  def run
131
137
  $stdout.sync = true
132
138
 
133
- # add some validation here ala knife-ec2
134
- unless config[:server_name] || config[:chef_node_name]
135
- ui.error("You have not provided a valid server or node name.")
136
- show_usage
137
- exit 1
138
- end
139
+ validate_server_name
139
140
 
140
141
  node_name = config[:chef_node_name] || config[:server_name]
141
142
 
142
143
  puts ui.color("Creating machine #{node_name}", :cyan)
143
144
 
144
- server = connection.servers.create(
145
+ server = connection.servers.create({
145
146
  :name => node_name,
146
147
  :dataset => config[:dataset],
147
148
  :package => config[:package]
148
- )
149
+ }.merge(joyent_metadata))
149
150
 
150
151
  print "\n#{ui.color("Waiting for server", :magenta)}"
151
152
  server.wait_for { print "."; ready? }
@@ -171,7 +172,19 @@ class Chef
171
172
 
172
173
  puts ui.color("attempting to bootstrap on #{bootstrap_ip_address}", :cyan)
173
174
 
174
- print(".") until tcp_test_ssh(bootstrap_ip_address) {
175
+ ssh_test_max = 10*60
176
+ ssh_test = 0
177
+
178
+ begin
179
+ if ssh_test < ssh_test_max
180
+ print(".")
181
+ ssh_test += 1
182
+ sleep 1
183
+ else
184
+ ui.error("Unable to ssh to node (#{bootstrap_ip_address}), exiting")
185
+ exit 1
186
+ end
187
+ end until tcp_test_ssh(bootstrap_ip_address) {
175
188
  sleep @initial_sleep_delay ||= 10
176
189
  puts("done")
177
190
  }
@@ -216,6 +229,25 @@ class Chef
216
229
 
217
230
  bootstrap
218
231
  end
232
+
233
+ private
234
+
235
+ def validate_server_name
236
+ # add some validation here ala knife-ec2
237
+ unless config[:server_name] || config[:chef_node_name]
238
+ ui.error("You have not provided a valid server or node name.")
239
+ show_usage
240
+ exit 1
241
+ end
242
+ end
243
+
244
+ def joyent_metadata
245
+ metadata = Chef::Config[:knife][:joyent_metadata] || {}
246
+ metadata.merge!(config[:joyent_metadata])
247
+
248
+ return {} if metadata.empty?
249
+ Hash[metadata.map { |k, v| ["metadata.#{k}", v] }]
250
+ end
219
251
  end
220
252
  end
221
253
  end
@@ -0,0 +1,89 @@
1
+ require 'chef/knife/joyent_base'
2
+
3
+ class Chef
4
+ class Knife
5
+ class JoyentServerMetadataDelete < Knife
6
+
7
+ include Knife::JoyentBase
8
+
9
+ attr_reader :server
10
+
11
+ banner 'knife joyent server metadata delete <server_id> <options>'
12
+
13
+ option :keys,
14
+ :short => "-k KEY",
15
+ :long => "--key KEY",
16
+ :description => "Key (or comma separated keys) to delete",
17
+ :proc => lambda { |o| o.split(/[\s,]+/) },
18
+ :default => []
19
+
20
+ option :all,
21
+ :long => "--all",
22
+ :description => "Delete all metadata",
23
+ :boolean => true,
24
+ :default => false
25
+
26
+ def run
27
+ unless name_args.size === 1
28
+ show_usage
29
+ exit 1
30
+ end
31
+
32
+ if config[:keys].empty? and not config[:all]
33
+ show_usage
34
+ exit 1
35
+ end
36
+
37
+ id = name_args.first
38
+
39
+ @server = self.connection.servers.get(id)
40
+ unless server
41
+ puts ui.error("Server with id: #{id} not found")
42
+ exit 1
43
+ end
44
+
45
+ delete_all_keys if config[:all]
46
+ delete_keys
47
+
48
+ if server.update_metadata(config[:metadata])
49
+ puts ui.color("Updated metadata on #{id}", :cyan)
50
+ exit 0
51
+ else
52
+ puts ui.error("Metadata update failed")
53
+ exit 1
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def delete_all_keys
60
+ if server.delete_all_metadata
61
+ msg("Deleted all metadata on #{server.id}")
62
+ exit 0
63
+ else
64
+ fail
65
+ end
66
+ end
67
+
68
+ def delete_keys
69
+ config[:keys].each do |key|
70
+ if server.delete_metadata(key)
71
+ msg("Deleted metadata key: #{key} on #{server.name}")
72
+ else
73
+ fail
74
+ end
75
+ end
76
+ exit 0
77
+ end
78
+
79
+ def msg(msg)
80
+ puts ui.color(msg, :cyan)
81
+ end
82
+
83
+ def fail
84
+ puts ui.error("Metadata delete failed")
85
+ exit 1
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,47 @@
1
+ require 'chef/knife/joyent_base'
2
+
3
+ class Chef
4
+ class Knife
5
+ class JoyentServerMetadataUpdate < Knife
6
+
7
+ include Knife::JoyentBase
8
+
9
+ banner 'knife joyent server metadata update <server_id> -m <json>'
10
+
11
+ option :metadata,
12
+ :short => "-m JSON",
13
+ :long => "--metadata JSON",
14
+ :description => "Metadata to be applied to server",
15
+ :proc => Proc.new { |m| JSON.parse(m) },
16
+ :default => {}
17
+
18
+ def run
19
+ if config[:metadata].empty?
20
+ show_usage
21
+ exit 1
22
+ end
23
+
24
+ unless name_args.size === 1
25
+ show_usage
26
+ exit 1
27
+ end
28
+
29
+ id = name_args.first
30
+
31
+ server = self.connection.servers.get(id)
32
+ unless server
33
+ puts ui.error("Server with id: #{id} not found")
34
+ exit 1
35
+ end
36
+
37
+ if server.update_metadata(config[:metadata])
38
+ puts ui.color("Updated metadata on #{id}", :cyan)
39
+ exit 0
40
+ else
41
+ puts ui.error("Metadata update failed")
42
+ exit 1
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,3 +1,3 @@
1
1
  module KnifeJoyent
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-joyent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-11 00:00:00.000000000 Z
12
+ date: 2013-04-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fog
@@ -67,6 +67,8 @@ files:
67
67
  - lib/chef/knife/joyent_server_create.rb
68
68
  - lib/chef/knife/joyent_server_delete.rb
69
69
  - lib/chef/knife/joyent_server_list.rb
70
+ - lib/chef/knife/joyent_server_metadata_delete.rb
71
+ - lib/chef/knife/joyent_server_metadata_update.rb
70
72
  - lib/chef/knife/joyent_server_reboot.rb
71
73
  - lib/chef/knife/joyent_server_resize.rb
72
74
  - lib/chef/knife/joyent_server_start.rb