knife-joyent 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/lib/chef/knife/joyent_base.rb +41 -5
  3. data/lib/chef/knife/joyent_flavor_list.rb +2 -0
  4. data/lib/chef/knife/joyent_fw_create.rb +3 -1
  5. data/lib/chef/knife/joyent_fw_delete.rb +3 -1
  6. data/lib/chef/knife/joyent_fw_get.rb +1 -2
  7. data/lib/chef/knife/joyent_fw_list.rb +2 -1
  8. data/lib/chef/knife/joyent_fw_update.rb +1 -1
  9. data/lib/chef/knife/joyent_image_create.rb +74 -0
  10. data/lib/chef/knife/joyent_image_list.rb +41 -7
  11. data/lib/chef/knife/joyent_key_add.rb +2 -1
  12. data/lib/chef/knife/joyent_key_delete.rb +3 -0
  13. data/lib/chef/knife/joyent_key_list.rb +2 -0
  14. data/lib/chef/knife/joyent_network_list.rb +2 -0
  15. data/lib/chef/knife/joyent_server_create.rb +1 -16
  16. data/lib/chef/knife/joyent_server_delete.rb +12 -9
  17. data/lib/chef/knife/joyent_server_fw_disable.rb +1 -1
  18. data/lib/chef/knife/joyent_server_fw_enable.rb +1 -1
  19. data/lib/chef/knife/joyent_server_list.rb +2 -0
  20. data/lib/chef/knife/joyent_server_metadata_delete.rb +3 -0
  21. data/lib/chef/knife/joyent_server_metadata_update.rb +2 -0
  22. data/lib/chef/knife/joyent_server_reboot.rb +3 -0
  23. data/lib/chef/knife/joyent_server_resize.rb +2 -0
  24. data/lib/chef/knife/joyent_server_start.rb +3 -1
  25. data/lib/chef/knife/joyent_server_stop.rb +2 -0
  26. data/lib/chef/knife/joyent_snapshot_create.rb +20 -29
  27. data/lib/chef/knife/joyent_snapshot_delete.rb +1 -13
  28. data/lib/chef/knife/joyent_snapshot_list.rb +2 -2
  29. data/lib/chef/knife/joyent_tag_create.rb +2 -2
  30. data/lib/chef/knife/joyent_tag_delete.rb +2 -0
  31. data/lib/chef/knife/joyent_tag_list.rb +3 -0
  32. data/lib/knife-joyent/version.rb +1 -1
  33. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 45293468ec017cf7ebb0f58f4a0d3a95531dad56
4
- data.tar.gz: bfe1d3c0b2da13606dc83038fb815ca2e7dd0532
3
+ metadata.gz: d785e10606c59d43b4031b90d2b0d0e752db4593
4
+ data.tar.gz: e703e79ffa5b7e7bc260e38e8fafd316ff9e69ce
5
5
  SHA512:
6
- metadata.gz: 97084d24162cf1f927515cd3b64a097fed154c04b9c4937a1df969aa2f329e5fbdfd69c78f8e07320409a0ced22d26585ab2a41f44c896e55ac199d5216e9784
7
- data.tar.gz: 58c05b85366ac511235eb3bd10847694333f0e17ca3bb595254505bcf743c578b1dd515dea310181aa92afe0b6fc46ee247b6ee11a43380354996f8d782d1ef7
6
+ metadata.gz: 364b38a35715a43c2cc528b53137d61fa7fbcd361fef0cc653a21a6d857aeaacbd0e639dda9a44cd6f15fada9879c007da62a80a016c0911b22a2898a0b4b38c
7
+ data.tar.gz: 201c979f3decc2d4c65cb19a4ff02d74fe72c96f8a94c0b0bd255623fb54e0f9e85e116f98f3c4fa968ef03681b3d7df3b543b1c1552b1c7119546f204de7eaf
@@ -82,13 +82,32 @@ class Chef
82
82
  config[key] || Chef::Config[:knife][key]
83
83
  end
84
84
 
85
- def output_error_response(res)
86
- if (res.body['message'])
87
- ui.error ui.color(res.body["message"], :white)
85
+ def output_error(e)
86
+ res = case e
87
+ when Excon::Response
88
+ e
89
+ when Excon::Errors::Error
90
+ e.response
91
+ else
92
+ raise
88
93
  end
89
94
 
90
- if (res.body["errors"])
91
- errors = res.body["errors"]
95
+ if res.headers["Content-Type"] == 'application/json' && res.body.kind_of?(String)
96
+ body = json_decode(res.body)
97
+ else
98
+ body = res.body
99
+ end
100
+
101
+ if body['code']
102
+ ui.error ui.color("*#{body["code"]}*", :white)
103
+ end
104
+
105
+ if body['message']
106
+ ui.error ui.color(body["message"], :white)
107
+ end
108
+
109
+ if body["errors"]
110
+ errors = body["errors"]
92
111
  errors.each do |e|
93
112
  ui.error("[#{e["field"]}] #{e["message"]}")
94
113
  end
@@ -100,6 +119,23 @@ class Chef
100
119
  puts "#{ui.color(label, :cyan)}: #{value}"
101
120
  end
102
121
  end
122
+
123
+ def json_decode(body)
124
+ parsed = Fog::JSON.decode(body)
125
+ decode_time_attrs(parsed)
126
+ end
127
+
128
+ def decode_time_attrs(obj)
129
+ if obj.kind_of?(Hash)
130
+ obj["created"] = Time.parse(obj["created"]) unless obj["created"].nil? or obj["created"] == ''
131
+ obj["updated"] = Time.parse(obj["updated"]) unless obj["updated"].nil? or obj["updated"] == ''
132
+ elsif obj.kind_of?(Array)
133
+ obj.map do |o|
134
+ decode_time_attrs(o)
135
+ end
136
+ end
137
+ obj
138
+ end
103
139
  end
104
140
  end
105
141
  end
@@ -26,6 +26,8 @@ class Chef
26
26
  end
27
27
 
28
28
  puts ui.list(flavor_list, :uneven_columns_across, 5)
29
+ rescue => e
30
+ output_error(e)
29
31
  end
30
32
  end
31
33
  end
@@ -34,7 +34,7 @@ class Chef
34
34
  )
35
35
 
36
36
  unless res.status == 201
37
- output_error_response(res)
37
+ output_error(res)
38
38
  else
39
39
  r = res.body
40
40
 
@@ -42,6 +42,8 @@ class Chef
42
42
  msg_pair "RULE", r["rule"]
43
43
  msg_pair "ENABLED", (r["enabled"] ? ui.color("✓ YES", :cyan) : "✗ NO")
44
44
  end
45
+ rescue => e
46
+ output_error(e)
45
47
  end
46
48
  end
47
49
  end
@@ -30,9 +30,11 @@ class Chef
30
30
  if res.status == 204
31
31
  ui.info "Rule #{id} Deleted."
32
32
  else
33
- self.output_error_response(res)
33
+ self.output_error(res)
34
34
  end
35
35
 
36
+ rescue => e
37
+ output_error(e)
36
38
  end
37
39
  end
38
40
  end
@@ -28,7 +28,7 @@ class Chef
28
28
  ]
29
29
 
30
30
  if (res.status == 422)
31
- output_error_response(res)
31
+ output_error(res)
32
32
  else
33
33
  r = res.body
34
34
  rules << r["id"]
@@ -36,7 +36,6 @@ class Chef
36
36
  rules << r["rule"]
37
37
  ui.list(rules, :uneven_columns_across, 3)
38
38
  end
39
-
40
39
  end
41
40
  end
42
41
  end
@@ -34,8 +34,9 @@ class Chef
34
34
  rules << (r["enabled"] ? ui.color("✓", :cyan) : "✗")
35
35
  rules << r["rule"]
36
36
  end
37
+ ui.output ui.list(rules, :uneven_columns_across, 3)
37
38
  else
38
- output_error_response(res)
39
+ output_error(res)
39
40
  end
40
41
  end
41
42
  end
@@ -35,7 +35,7 @@ class Chef
35
35
  )
36
36
 
37
37
  unless res.status == 200
38
- output_error_response(res)
38
+ output_error(res)
39
39
  else
40
40
  r = res.body
41
41
 
@@ -0,0 +1,74 @@
1
+ require 'chef/knife/joyent_base'
2
+
3
+ class Chef
4
+ class Knife
5
+ class JoyentImageCreate < Knife
6
+
7
+ include Knife::JoyentBase
8
+
9
+ banner "knife joyent image create <options>"
10
+
11
+ option :server,
12
+ :long => "--server <server_uuid>",
13
+ :description => "server uuid to create machine from",
14
+ :required => true
15
+
16
+ option :name,
17
+ :long => "--name <image_name>",
18
+ :description => "name of custom image",
19
+ :required => true
20
+
21
+ option :version,
22
+ :long => "--version <image_version>",
23
+ :description => "version of custom image",
24
+ :required => true
25
+
26
+ option :description,
27
+ :long => "--description <image description>",
28
+ :description => "description of custom image (optional)"
29
+
30
+ option :homepage,
31
+ :long => "--homepage <homepage>",
32
+ :description => "homepage of custom image (optional)"
33
+
34
+ option :eula,
35
+ :long => "--eula <eula>",
36
+ :description => "EULA of custom image"
37
+
38
+ option :acl,
39
+ :long => "--acl <eula>",
40
+ :description => "ACL (json) of custom image see: https://images.joyent.com/docs/#manifest-acl"
41
+
42
+ option :tags,
43
+ :long => "--tags <tags>",
44
+ :description => "tags (json) of custom image"
45
+
46
+ def image_create_options
47
+ opts = {}
48
+ opts["machine"] = config[:server] if config[:server]
49
+ opts["name"] = config[:name] if config[:name]
50
+ opts["version"] = config[:version] if config[:version]
51
+ opts["description"] = config[:description] if config[:description]
52
+ opts["eula"] = config[:eula] if config[:eula]
53
+ opts["acl"] = config[:acl] if config[:acl]
54
+ opts["tags"] = config[:tags] if config[:tags]
55
+ opts
56
+ end
57
+
58
+ def run
59
+ # puts image_create_options.inspect
60
+ res = self.connection.request(
61
+ :method => "POST",
62
+ :path => "/my/images",
63
+ :body => image_create_options
64
+ )
65
+ if (res.status == 201)
66
+ ui.info ui.color("Creating Image from server #{config[:server]}...", :cyan)
67
+ ui.output(res.body)
68
+ else
69
+ output_error(res)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -8,6 +8,16 @@ class Chef
8
8
 
9
9
  banner "knife joyent image list <options>"
10
10
 
11
+ option :public,
12
+ :boolean => true,
13
+ :long => "--public <true/false>",
14
+ :description => "filter public/private images"
15
+
16
+ option :state,
17
+ :long => "--state <all/active/unactivated/disabled>",
18
+ :default => "active",
19
+ :description => "filter images by state (default: active"
20
+
11
21
  def run
12
22
  images = [
13
23
  ui.color('ID', :bold),
@@ -15,17 +25,41 @@ class Chef
15
25
  ui.color('Version', :bold),
16
26
  ui.color('OS', :bold),
17
27
  ui.color('Type', :bold),
28
+ ui.color('State', :bold),
18
29
  ]
19
30
 
20
- self.connection.images.sort_by(&:name).each do |i|
21
- images << i.id.to_s
22
- images << i.name
23
- images << i.version
24
- images << i.os
25
- images << i.type
31
+ query = {}
32
+ query[:public] = config[:public] if config[:public]
33
+ query[:state] = config[:state] if config[:state]
34
+
35
+ res = self.connection.request(
36
+ :method => "GET",
37
+ :query => query,
38
+ :path => "/my/images",
39
+ )
40
+
41
+ if res.status == 200
42
+ data = res.body
43
+ else
44
+ output_error(res)
45
+ exit 1
26
46
  end
27
47
 
28
- puts ui.list(images, :uneven_columns_across, 5)
48
+ data.sort_by do |v|
49
+ v["name"]
50
+ end.each do |i|
51
+ images << i["id"]
52
+ images << i["name"]
53
+ images << i["version"]
54
+ images << i["os"]
55
+ images << i["type"]
56
+ images << i["state"]
57
+ end
58
+
59
+ ui.output ui.list(images, :uneven_columns_across, 6)
60
+ rescue => e
61
+ output_error(e)
62
+
29
63
  end
30
64
  end
31
65
  end
@@ -48,7 +48,8 @@ class Chef
48
48
  end
49
49
 
50
50
  puts ui.color('Created key: '+keyname, :cyan)
51
- exit 0
51
+ rescue => e
52
+ output_error(e)
52
53
  end
53
54
  end
54
55
  end
@@ -28,6 +28,9 @@ class Chef
28
28
 
29
29
  puts ui.color('Deleted key: '+keyname, :cyan)
30
30
  exit 0
31
+ rescue => e
32
+ output_error(e)
33
+ exit 1
31
34
  end
32
35
  end
33
36
  end
@@ -20,6 +20,8 @@ class Chef
20
20
  end
21
21
 
22
22
  puts ui.list(keys, :uneven_columns_across, 2)
23
+ rescue => e
24
+ output_error(e)
23
25
  end
24
26
  end
25
27
  end
@@ -22,6 +22,8 @@ class Chef
22
22
 
23
23
  puts ui.list(networks, :uneven_columns_across, 3)
24
24
  exit 0
25
+ rescue => e
26
+ output_error(e)
25
27
  end
26
28
  end
27
29
  end
@@ -213,23 +213,8 @@ class Chef
213
213
 
214
214
  bootstrap_for_node(server, bootstrap_ip).run
215
215
 
216
- rescue Excon::Errors::Conflict => e
217
- if e.response && e.response.body.kind_of?(String)
218
- error = ::Fog::JSON.decode(e.response.body)
219
- print ui.error(error['message'])
220
- if error.key?('errors') && error['errors'].kind_of?(Array)
221
- error['errors'].each do |err|
222
- print ui.error " * [#{err['field']}] #{err['message']}"
223
- end
224
- end
225
- exit 1
226
- else
227
- puts ui.error(e.message)
228
- exit 1
229
- end
230
-
231
216
  rescue => e
232
- puts ui.error('Unexpected Error Occured:' + e.inspect)
217
+ output_error(e)
233
218
  exit 1
234
219
  end
235
220
 
@@ -19,22 +19,22 @@ class Chef
19
19
  id = name_args.first
20
20
 
21
21
  server = self.connection.servers.get(id)
22
-
22
+
23
23
  msg("ID", server.id.to_s)
24
24
  msg("Name", server.name)
25
25
  msg("State", server.state)
26
26
  msg("Type", server.type)
27
27
  msg("Dataset", server.dataset)
28
28
  msg("IPs", server.ips.join(" "))
29
-
29
+
30
30
  unless server
31
31
  puts ui.error("Unable to locate server: #{id}")
32
32
  exit 1
33
33
  end
34
-
34
+
35
35
  puts "\n"
36
36
  confirm("Do you really want to delete this server")
37
-
37
+
38
38
  puts ui.color("Stopping server...", :cyan)
39
39
 
40
40
  if server.stopped?
@@ -47,24 +47,27 @@ class Chef
47
47
  exit 1
48
48
  end
49
49
  end
50
-
50
+
51
51
  server.destroy
52
52
  puts ui.color("Deleted server: #{id}", :cyan)
53
-
53
+
54
54
  puts "\n"
55
55
  confirm("Delete client and node for #{server.name}?")
56
-
56
+
57
57
  node = Chef::Node.load(server.name)
58
58
  puts "deleting node #{node.name}"
59
59
  node.destroy
60
60
  ui.warn("Deleted node named #{node.name}")
61
-
61
+
62
62
  client = Chef::ApiClient.load(server.name)
63
63
  puts "deleting client #{client.name}"
64
64
  client.destroy
65
65
  ui.warn("Deleted client named #{client.name}")
66
+ rescue => e
67
+ output_error(e)
68
+ exit 1
66
69
  end
67
-
70
+
68
71
  def msg(label, value)
69
72
  if value && !value.empty?
70
73
  puts "#{ui.color(label, :cyan)}: #{value}"
@@ -26,7 +26,7 @@ class Chef
26
26
  if (res.status == 202)
27
27
  puts ui.color("Firewall Disabled for server #{id}", :cyan)
28
28
  else
29
- output_error_response(res)
29
+ output_error(res)
30
30
  end
31
31
  end
32
32
  end
@@ -25,7 +25,7 @@ class Chef
25
25
  if (res.status === 202)
26
26
  ui.info ui.color("Firewall Enabled for server #{id}", :cyan)
27
27
  else
28
- output_error_response(res)
28
+ output_error(res)
29
29
  end
30
30
  end
31
31
  end
@@ -70,6 +70,8 @@ class Chef
70
70
  price_column_width)
71
71
 
72
72
  puts ui.list(servers, :uneven_columns_across, columns)
73
+ rescue => e
74
+ output_error(e)
73
75
  end
74
76
 
75
77
  def show_tags?
@@ -52,6 +52,9 @@ class Chef
52
52
  puts ui.error("Metadata update failed")
53
53
  exit 1
54
54
  end
55
+
56
+ rescue => e
57
+ output_error(e)
55
58
  end
56
59
 
57
60
  private
@@ -41,6 +41,8 @@ class Chef
41
41
  puts ui.error("Metadata update failed")
42
42
  exit 1
43
43
  end
44
+ rescue => e
45
+ output_error(e)
44
46
  end
45
47
  end
46
48
  end
@@ -29,6 +29,9 @@ class Chef
29
29
  puts ui.error("Reboot server failed")
30
30
  exit 1
31
31
  end
32
+
33
+ rescue => e
34
+ output_error(e)
32
35
  end
33
36
  end
34
37
  end
@@ -39,6 +39,8 @@ class Chef
39
39
  puts ui.error("Resize server failed")
40
40
  exit 1
41
41
  end
42
+ rescue => e
43
+ output_error(e)
42
44
  end
43
45
  end
44
46
  end
@@ -36,6 +36,8 @@ class Chef
36
36
  exit 1
37
37
  end
38
38
  end
39
- end
39
+ rescue => e
40
+ output_error(e)
41
+ end
40
42
  end
41
43
  end
@@ -36,6 +36,8 @@ class Chef
36
36
  exit 1
37
37
  end
38
38
  end
39
+ rescue => e
40
+ output_error(e)
39
41
  end
40
42
  end
41
43
  end
@@ -2,41 +2,32 @@ require 'chef/knife/joyent_base'
2
2
 
3
3
  class Chef
4
4
  class Knife
5
- class JoyentSnapshotCreate < Knife
5
+ class JoyentSnapshotCreate < Knife
6
6
 
7
- include Knife::JoyentBase
7
+ include Knife::JoyentBase
8
8
 
9
- banner 'knife joyent snapshot create <server> <snapshot_name>'
9
+ banner 'knife joyent snapshot create <server> <snapshot_name>'
10
10
 
11
- def run
12
- unless name_args.size == 2
13
- show_usage
14
- exit 1
15
- end
16
-
17
- server = name_args[0]
18
- ssname = name_args[1]
19
-
20
- snapshot = self.connection.snapshots.create(server, ssname)
21
- puts ui.color("Created snapshot", :cyan)
22
- puts ui.output({
23
- :server => snapshot.machine_id,
24
- :name => snapshot.name,
25
- :state => snapshot.state,
26
- :created => snapshot.created
27
- })
28
- exit 0
29
- rescue Excon::Errors::Conflict => e
30
- if e.response && e.response.body.kind_of?(String)
31
- error = MultiJson.decode(e.response.body)
32
- puts ui.error(error['message'])
33
- exit 1
34
- else
35
- puts ui.error(e.message)
11
+ def run
12
+ unless name_args.size == 2
13
+ show_usage
36
14
  exit 1
37
15
  end
16
+
17
+ server = name_args[0]
18
+ ssname = name_args[1]
19
+
20
+ snapshot = self.connection.snapshots.create(server, ssname)
21
+ puts ui.color("Created snapshot", :cyan)
22
+ puts ui.output({
23
+ :server => snapshot.machine_id,
24
+ :name => snapshot.name,
25
+ :state => snapshot.state,
26
+ :created => snapshot.created
27
+ })
28
+ exit 0
38
29
  rescue => e
39
- puts ui.error('Unexpected Error Occured:' + e.message)
30
+ output_error(e)
40
31
  exit 1
41
32
  end
42
33
  end
@@ -21,20 +21,8 @@ class Chef
21
21
  snapshot.destroy
22
22
  puts ui.color("Deleted snapshot #{snapshot.name}", :cyan)
23
23
  exit 0
24
- rescue Excon::Errors::NotFound => e
25
- puts ui.error("Snapshot #{ssname} on server #{server} not found")
26
- exit 1
27
- rescue Excon::Errors::Conflict => e
28
- if e.response && e.response.body.kind_of?(String)
29
- error = MultiJson.decode(e.response.body)
30
- puts ui.error(error['message'])
31
- exit 1
32
- else
33
- puts ui.error(e.message)
34
- exit 1
35
- end
36
24
  rescue => e
37
- puts ui.error('Unexpected Error Occured:' + e.message)
25
+ output_error(e)
38
26
  exit 1
39
27
  end
40
28
  end
@@ -37,8 +37,8 @@ class Chef
37
37
  end
38
38
 
39
39
  puts ui.list(snapshots, :uneven_columns_across, 3)
40
- rescue Fog::Compute::Joyent::Errors::NotFound
41
- puts ui.error("Server #{server} not found.")
40
+ rescue e
41
+ output_error(e)
42
42
  end
43
43
  end
44
44
  end
@@ -31,8 +31,8 @@ class Chef
31
31
  puts ui.color("Updated tags for #{server}", :cyan)
32
32
  puts ui.list(tags, :uneven_columns_across, 2)
33
33
  exit 0
34
- rescue Excon::Errors::NotFound => e
35
- puts ui.error("Server #{server} not found")
34
+ rescue => e
35
+ output_error(e)
36
36
  exit 1
37
37
  end
38
38
  end
@@ -58,6 +58,8 @@ class Chef
58
58
  puts ui.list(tags, :uneven_columns_across, 2)
59
59
  end
60
60
  exit 0
61
+ rescue => e
62
+ output_error(e)
61
63
  end
62
64
  end
63
65
  end
@@ -31,6 +31,9 @@ class Chef
31
31
  rescue Excon::Errors::NotFound => e
32
32
  puts ui.error("Server #{server} not found")
33
33
  exit 1
34
+
35
+ rescue => e
36
+ output_error(e)
34
37
  end
35
38
  end
36
39
  end
@@ -1,3 +1,3 @@
1
1
  module KnifeJoyent
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-joyent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Chan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-14 00:00:00.000000000 Z
11
+ date: 2014-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fog
@@ -105,6 +105,7 @@ files:
105
105
  - lib/chef/knife/joyent_fw_get.rb
106
106
  - lib/chef/knife/joyent_fw_list.rb
107
107
  - lib/chef/knife/joyent_fw_update.rb
108
+ - lib/chef/knife/joyent_image_create.rb
108
109
  - lib/chef/knife/joyent_image_list.rb
109
110
  - lib/chef/knife/joyent_key_add.rb
110
111
  - lib/chef/knife/joyent_key_delete.rb