smartdc 0.4.0 → 1.0.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 (100) hide show
  1. data/.document +0 -1
  2. data/.travis.yml +5 -0
  3. data/Gemfile +7 -10
  4. data/README.md +42 -69
  5. data/Rakefile +47 -0
  6. data/VERSION +1 -1
  7. data/bin/sdc +327 -0
  8. data/lib/cli_helper.rb +131 -8
  9. data/lib/configure.rb +73 -0
  10. data/lib/smartdc.rb +16 -1
  11. data/lib/smartdc/api/analytics.rb +29 -7
  12. data/lib/smartdc/api/analytics/heatmap.rb +20 -0
  13. data/lib/smartdc/api/datacenters.rb +6 -7
  14. data/lib/smartdc/api/datasets.rb +6 -7
  15. data/lib/smartdc/api/keys.rb +10 -11
  16. data/lib/smartdc/api/machine/metadata.rb +9 -15
  17. data/lib/smartdc/api/machine/snapshots.rb +16 -16
  18. data/lib/smartdc/api/machine/tags.rb +11 -12
  19. data/lib/smartdc/api/machines.rb +23 -23
  20. data/lib/smartdc/api/packages.rb +7 -8
  21. data/lib/smartdc/client.rb +14 -18
  22. data/lib/smartdc/request.rb +41 -51
  23. data/lib/smartdc/response.rb +24 -0
  24. data/lib/smartdc/response/raise_error.rb +1 -1
  25. data/smartdc.gemspec +51 -90
  26. data/spec/fixtures/analytics.json +26 -0
  27. data/spec/fixtures/datacenter.json +4 -0
  28. data/spec/fixtures/datacenters.json +3 -0
  29. data/spec/fixtures/datasets.json +38 -0
  30. data/spec/fixtures/keys.json +14 -0
  31. data/spec/fixtures/machines.json +36 -0
  32. data/spec/fixtures/packages.json +18 -0
  33. data/spec/fixtures/snapshots.json +8 -0
  34. data/spec/fixtures/tag.json +3 -0
  35. data/spec/spec_helper.rb +3 -12
  36. data/spec/unit/smartdc/api/analytics_spec.rb +53 -0
  37. data/spec/unit/smartdc/api/datacenters_spec.rb +31 -0
  38. data/spec/unit/smartdc/api/datasets_spec.rb +31 -0
  39. data/spec/unit/smartdc/api/keys_spec.rb +53 -0
  40. data/spec/unit/smartdc/api/machine/metadata_spec.rb +42 -0
  41. data/spec/unit/smartdc/api/machine/snapshots_spec.rb +132 -0
  42. data/spec/unit/smartdc/api/machine/tags_spec.rb +53 -0
  43. data/spec/unit/smartdc/api/machines_spec.rb +53 -0
  44. data/spec/unit/smartdc/api/packages_spec.rb +31 -0
  45. data/spec/{smartdc → unit/smartdc}/client_spec.rb +0 -6
  46. data/spec/{smartdc_spec.rb → unit/smartdc_spec.rb} +1 -1
  47. metadata +72 -140
  48. data/LICENSE.md +0 -20
  49. data/bin/sdc-addmachinemetadata +0 -29
  50. data/bin/sdc-addmachinetag +0 -29
  51. data/bin/sdc-createinstrumentation +0 -27
  52. data/bin/sdc-createkey +0 -32
  53. data/bin/sdc-createmachine +0 -39
  54. data/bin/sdc-createmachinesnapshot +0 -27
  55. data/bin/sdc-deleteinstrumentation +0 -12
  56. data/bin/sdc-deletekey +0 -12
  57. data/bin/sdc-deletemachine +0 -12
  58. data/bin/sdc-deletemachinemetadata +0 -29
  59. data/bin/sdc-deletemachinesnapshot +0 -29
  60. data/bin/sdc-deletemachinetag +0 -29
  61. data/bin/sdc-describeanalytics +0 -10
  62. data/bin/sdc-getdatacenter +0 -10
  63. data/bin/sdc-getdataset +0 -10
  64. data/bin/sdc-getinstrumentation +0 -31
  65. data/bin/sdc-getkey +0 -10
  66. data/bin/sdc-getmachine +0 -12
  67. data/bin/sdc-getmachinemetadata +0 -12
  68. data/bin/sdc-getmachinesnapshot +0 -27
  69. data/bin/sdc-getmachinetag +0 -27
  70. data/bin/sdc-getpackage +0 -10
  71. data/bin/sdc-listdatacenters +0 -10
  72. data/bin/sdc-listdatasets +0 -10
  73. data/bin/sdc-listinstrumentations +0 -10
  74. data/bin/sdc-listkeys +0 -10
  75. data/bin/sdc-listmachines +0 -10
  76. data/bin/sdc-listmachinesnapshots +0 -10
  77. data/bin/sdc-listmachinetags +0 -10
  78. data/bin/sdc-listpackages +0 -10
  79. data/bin/sdc-rebootmachine +0 -12
  80. data/bin/sdc-resizemachine +0 -34
  81. data/bin/sdc-setup +0 -67
  82. data/bin/sdc-startmachine +0 -12
  83. data/bin/sdc-startmachinefromsnapshot +0 -29
  84. data/bin/sdc-stopmachine +0 -12
  85. data/config/fixtures/instrumentation.json +0 -4
  86. data/config/fixtures/key.json +0 -4
  87. data/config/fixtures/tag.json +0 -3
  88. data/lib/smartdc/api/analytics/instrumentations.rb +0 -44
  89. data/lib/smartdc/response/mashify.rb +0 -29
  90. data/lib/smartdc/response/parse_json.rb +0 -26
  91. data/spec/smartdc/api/analytics_spec.rb +0 -32
  92. data/spec/smartdc/api/datacenters_spec.rb +0 -16
  93. data/spec/smartdc/api/datasets_spec.rb +0 -16
  94. data/spec/smartdc/api/keys_spec.rb +0 -32
  95. data/spec/smartdc/api/machine/metadata_spec.rb +0 -59
  96. data/spec/smartdc/api/machine/snapshots_spec.rb +0 -70
  97. data/spec/smartdc/api/machine/tags_spec.rb +0 -65
  98. data/spec/smartdc/api/machines_spec.rb +0 -92
  99. data/spec/smartdc/api/packages_spec.rb +0 -16
  100. data/spec/smartdc/request_spec.rb +0 -12
data/.document CHANGED
@@ -2,4 +2,3 @@ lib/**/*.rb
2
2
  bin/*
3
3
  -
4
4
  features/**/*.feature
5
- LICENSE.md
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ gemfile:
5
+ - Gemfile
data/Gemfile CHANGED
@@ -1,16 +1,13 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  group :development do
4
- gem "rspec", "~> 2.8.0"
5
- gem "bundler", "~> 1.0.0"
6
- gem "jeweler", "~> 1.8.3"
7
- gem "rcov", "~> 0.9.11"
8
- gem "uuid", "~> 2.3.4"
4
+ gem "bundler", "~> 1.1"
5
+ gem "jeweler", "~> 1.8"
6
+ gem "rspec", "~> 2.12"
9
7
  end
10
8
 
11
9
  group :runtime do
12
- gem "faraday", "~> 0.7.6"
13
- gem "hashie", "~> 1.2.0"
14
- gem "multi_json", "~> 1.1.0"
15
- gem "multipart-post", "~> 1.1.4"
16
- end
10
+ gem "faraday", "~> 0.8"
11
+ gem "thor", "~> 0.16"
12
+ gem "terminal-table", "~> 1.4"
13
+ end
data/README.md CHANGED
@@ -1,87 +1,60 @@
1
- # smartdc
1
+ smartdc
2
+ =======
2
3
 
3
- ruby-smartdc is a ruby client library for interacting with the Joyent SmartDataCenter CloudApi.
4
+ smartdc is Joyent's [SmartDataCenter](http://www.joyent.com/software/smartdatacenter) client and SmartDataCenter Command Line Interface.
5
+ [Joyent CloudAPI Documentation](http://apidocs.joyent.com/sdcapidoc/cloudapi/).
4
6
 
7
+ ## Features
8
+ * Response content is Hash.
9
+ * Debug output Request and Response.
10
+ * Output style is Table or JSON.
11
+ * CLI is sub command style.
5
12
 
6
13
  ## Installation
7
-
8
- gem install smartdc
9
-
14
+ ```
15
+ gem install smartdc
16
+ ```
10
17
 
11
18
  ## Usage
12
19
 
13
- Get Started with SmartDataCenter [here](http://www.slideshare.net/ogom_/smartdc-by-ruby-10047222).
14
-
15
-
16
20
  ### CLI
17
-
18
- $ sdc-setup https://example.com
19
- version:[~6.5]
20
- Username (login): auth_user
21
- Password: auth_pass
22
- Successful configuration.
23
-
24
- $ sdc-listdatacenters
25
- {
26
- "example": "https://example.com"
27
- }
28
-
21
+ ```
22
+ $ sdc init
23
+ $ sdc key add key_name ~/.ssh/id_rsa.pub
24
+ $ sdc dataset ls
25
+ $ sdc package ls
26
+ $ sdc machine add -e DATASET_URN -p PACKAGE_NAME
27
+ $ sdc machine ls
28
+ $ sdc machine ls --raw
29
+ ```
29
30
 
30
31
  ### Program
31
32
 
32
- Connect and Machine find.
33
-
34
- require 'smartdc'
35
-
36
- config = {
37
- "url" => "https://example.com/",
38
- "version" => "~6.5",
39
- "username" => "auth_user",
40
- "password" => "auth_pass"
41
- }
42
-
43
- client = Smartdc.new(config)
44
- machines = client.machines.find
45
- machines.each do |machine|
46
- puts "machines id: #{machine.id}, state: #{machine.state}"
47
- machine = client.machines(machine.id)
48
-
49
- puts 'Stop the machine.'
50
- machine.stop
51
- 8.times do |i|
52
- puts "machine(#{i}) id: #{machine.read.id}, state: #{machine.read.state}"
53
- break if machine.read.state == 'stopped'
54
- sleep i
55
- end
56
-
57
- puts 'Start the machine.'
58
- machine.start
59
- 8.times do |i|
60
- puts "machine(#{i}) id: #{machine.read.id}, state: #{machine.read.state}"
61
- break if machine.read.state == 'running'
62
- sleep i
63
- end
64
- puts
65
- end
66
-
33
+ ```
34
+ require 'smartdc'
67
35
 
68
- Format of the return value.
36
+ client = Smartdc.new({
37
+ hostname: 'example.com',
38
+ username: 'auth_user',
39
+ password: 'auth_pass',
40
+ version: '~6.5'
41
+ })
69
42
 
70
- client = Smartdc.new(config)
71
-
72
- # Type of a variable to mash. (Default)
73
- client.format = 'mash'
74
- puts client.datacenters.find #=> #<Hashie::Mash example="example.com">
43
+ client.machines.all.content.each do |machine|
44
+ p "#{machine['name']} is state at #{machine['state']}."
75
45
 
76
- # Type of a variable to hash.
77
- client.format = 'hash'
78
- puts client.datacenters.find #=> {"example"=>"https://example.com"}
46
+ # Stop machine
47
+ p client.machines.stop(machine['id']).status
48
+ end
49
+ ```
79
50
 
80
- # Type of a variable to json.
81
- client.format = 'json'
82
- puts client.datacenters.find #=> {"example": "https://example.com"}
51
+ ## Tests
52
+ ```
53
+ $ rake spec
54
+ ```
83
55
 
56
+ [![Build Status](https://travis-ci.org/ogom/ruby-smartdc.png?branch=master)](https://travis-ci.org/ogom/ruby-smartdc)
84
57
 
85
- ## Copyright
86
58
 
87
- Copyright (c) 2012 ogom. See LICENSE.md for further details.
59
+ ## License
60
+ * MIT
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ gem.name = "smartdc"
17
+ gem.homepage = "http://github.com/ogom/ruby-smartdc"
18
+ gem.license = "MIT"
19
+ gem.summary = " Joyent SmartDataCenter CloudApi client by ruby."
20
+ gem.description = "SmartDataCenter client and SmartDataCenter Command Line Interface."
21
+ gem.email = "ogom@hotmail.co.jp"
22
+ gem.authors = ["ogom"]
23
+ end
24
+ Jeweler::RubygemsDotOrgTasks.new
25
+
26
+ require 'rspec/core'
27
+ require 'rspec/core/rake_task'
28
+ RSpec::Core::RakeTask.new(:spec) do |spec|
29
+ spec.pattern = FileList['spec/**/*_spec.rb']
30
+ end
31
+
32
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ end
36
+
37
+ task :default => :spec
38
+
39
+ require 'rdoc/task'
40
+ Rake::RDocTask.new do |rdoc|
41
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "smartdc #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 1.0.1
data/bin/sdc ADDED
@@ -0,0 +1,327 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # sdc
4
+ # Copyright(c) 2012 ogom
5
+ # MIT Licensed
6
+ #
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ require 'cli_helper'
9
+
10
+ module CLI
11
+ class Base < Thor
12
+ class_option :debug, type: :boolean, aliases: '-d', desc: 'Debug output'
13
+ class_option :raw, type: :boolean, aliases: '-r', desc: 'Raw response body'
14
+ end
15
+
16
+ class Key < CLI::Base
17
+ desc 'ls', 'Lists all public keys we have on record for the specified account.'
18
+ def ls
19
+ output sdc(config(options)).keys.all, {table: :h, exclude: [:key]}.merge(options)
20
+ end
21
+
22
+ desc 'get [NAME]', 'Retrieves an individual key record.'
23
+ def get(name)
24
+ output sdc(config(options)).keys.read(name), {only: :key}.merge(options)
25
+ end
26
+
27
+ desc 'add [NAME] [FILE]', 'Uploads a new OpenSSH key to SmartDataCenter.'
28
+ def add(name, file)
29
+ raw = {name: name}
30
+ raw[:key] = File.read(file)
31
+ output sdc(config(options)).keys.create(raw), {table: :v, exclude: [:key]}.merge(options)
32
+ end
33
+
34
+ desc 'del [NAME]', 'Deletes an SSH key by name.'
35
+ def del(name)
36
+ output sdc(config(options)).keys.destroy(name), {message: "Key #{name} deleted"}.merge(options)
37
+ end
38
+ end
39
+
40
+ class Datacenter < CLI::Base
41
+ desc 'ls', 'Provides a list of all datacenters this cloud is aware of.'
42
+ def ls
43
+ output sdc(config(options)).datacenters.all, {table: :v}.merge(options)
44
+ end
45
+
46
+ desc 'get [NAME]', 'Gets an individual datacenter by name.'
47
+ def get(name)
48
+ output sdc(config(options)).datacenters.read(name), {table: :v}.merge(options)
49
+ end
50
+ end
51
+
52
+ class Dataset < CLI::Base
53
+ desc 'ls', 'Provides a list of datasets available in this datacenter.'
54
+ def ls
55
+ include = [:id, :name, :urn, :version, :os]
56
+ output sdc(config(options)).datasets.all, {table: :h, include: include}.merge(options)
57
+ end
58
+
59
+ desc 'get [ID]', 'Gets an individual dataset by id.'
60
+ def get(id)
61
+ output sdc(config(options)).datasets.read(id), {table: :v}.merge(options)
62
+ end
63
+ end
64
+
65
+ class Package < CLI::Base
66
+ desc 'ls', 'Provides a list of packages available in this datacenter.'
67
+ def ls
68
+ output sdc(config(options)).packages.all, {table: :h}.merge(options)
69
+ end
70
+
71
+ desc 'get [NAME]', 'Gets a package by name.'
72
+ def get(name)
73
+ output sdc(config(options)).packages.read(name), {table: :v}.merge(options)
74
+ end
75
+ end
76
+
77
+ class Tag < CLI::Base
78
+ class_option :uuid, type: :string, aliases: '-u', desc: 'Machine uuid'
79
+
80
+ desc 'ls', 'Returns the complete set of tags associated with this machine.'
81
+ def ls
82
+ uuid = options.uuid.nil? ? config[:use_machine] : options.uuid
83
+ output sdc(config(opt(ARGV, options))).machines.tags(uuid).all, {table: :v}.merge(opt(ARGV, options))
84
+ end
85
+
86
+ desc 'get [KEY]', 'Returns the value for a single tag on this machine.'
87
+ def get(key)
88
+ uuid = options.uuid.nil? ? config[:use_machine] : options.uuid
89
+ output sdc(config(opt(ARGV, options))).machines.tags(uuid).read(key), {'raw' => true}.merge(opt(ARGV, options))
90
+ end
91
+
92
+ desc 'add [KEY] [VALUE]', 'Allows you to add additional tags, other than those set at provisioning time.'
93
+ def add(key, value)
94
+ uuid = options.uuid.nil? ? config[:use_machine] : options.uuid
95
+ output sdc(config(opt(ARGV, options))).machines.tags(uuid).create({key => value}), {table: :v}.merge(opt(ARGV, options))
96
+ end
97
+
98
+ desc 'del [KEY]', 'Deletes tag from this machine.'
99
+ method_option :all, type: :boolean, desc: 'All tags delete.'
100
+ def del(key=nil)
101
+ uuid = options.uuid.nil? ? config[:use_machine] : options.uuid
102
+ if key.nil?
103
+ if options.all
104
+ msg = "Machine #{uuid} all tags deleted"
105
+ output sdc(config(opt(ARGV, options))).machines.tags(uuid).destroy, {message: msg}.merge(opt(ARGV, options))
106
+ end
107
+ else
108
+ msg = "Machine #{uuid} tag #{key} deleted"
109
+ output sdc(config(opt(ARGV, options))).machines.tags(uuid).destroy(key), {message: msg}.merge(opt(ARGV, options))
110
+ end
111
+ end
112
+ end
113
+
114
+ class Metadata < CLI::Base
115
+ class_option :uuid, type: :string, aliases: '-u', desc: 'Machine uuid'
116
+
117
+ desc 'ls', 'Returns the complete set of metadata associated with this machine.'
118
+ def ls
119
+ uuid = options.uuid.nil? ? config[:use_machine] : options.uuid
120
+ output sdc(config(opt(ARGV, options))).machines.metadata(uuid).read, {table: :v}.merge(opt(ARGV, options))
121
+ end
122
+
123
+ desc 'add [KEY] [VALUE]', 'Allows you to update the metadata for a given machine.'
124
+ def add(key, value)
125
+ uuid = options.uuid.nil? ? config[:use_machine] : options.uuid
126
+ output sdc(config(opt(ARGV, options))).machines.metadata(uuid).create({key => value}), {table: :v}.merge(opt(ARGV, options))
127
+ end
128
+
129
+ desc 'del [KEY]', 'Deletes metadata key from this machine.'
130
+ method_option :all, type: :boolean, desc: 'All metadata delete.'
131
+ def del(key=nil)
132
+ uuid = options.uuid.nil? ? config[:use_machine] : options.uuid
133
+ if key.nil?
134
+ if options.all
135
+ msg = "Machine #{uuid} all metadata deleted"
136
+ output sdc(config(opt(ARGV, options))).machines.metadata(uuid).destroy, {message: msg}.merge(opt(ARGV, options))
137
+ end
138
+ else
139
+ msg = "Machine #{uuid} metadata #{key} deleted"
140
+ output sdc(config(opt(ARGV, options))).machines.metadata(uuid).destroy(key), {message: msg}.merge(opt(ARGV, options))
141
+ end
142
+ end
143
+ end
144
+
145
+ class Snapshot < CLI::Base
146
+ class_option :uuid, type: :string, aliases: '-u', desc: 'Machine uuid'
147
+
148
+ desc 'ls', 'Lists all snapshots taken for a given machine.'
149
+ def ls
150
+ uuid = options.uuid.nil? ? config[:use_machine] : options.uuid
151
+ output sdc(config(opt(ARGV, options))).machines.snapshots(uuid).all, {table: :h}.merge(opt(ARGV, options))
152
+ end
153
+
154
+ desc 'get [NAME]', 'Gets the state of the named snapshot.'
155
+ def get(name)
156
+ uuid = options.uuid.nil? ? config[:use_machine] : options.uuid
157
+ output sdc(config(opt(ARGV, options))).machines.snapshots(uuid).read(name), {table: :v}.merge(opt(ARGV, options))
158
+ end
159
+
160
+ desc 'add [NAME]', 'Allows you to take a snapshot of a machine.'
161
+ def add(name)
162
+ uuid = options.uuid.nil? ? config[:use_machine] : options.uuid
163
+ output sdc(config(opt(ARGV, options))).machines.snapshots(uuid).create({name: name}), {table: :v}.merge(opt(ARGV, options))
164
+ end
165
+
166
+ desc 'del [NAME]', 'Deletes the specified snapshot of a machine.'
167
+ def del(name)
168
+ uuid = options.uuid.nil? ? config[:use_machine] : options.uuid
169
+ msg = "Machine #{uuid} snapshots #{options[:name]} deleted"
170
+ output sdc(config(opt(ARGV, options))).machines.snapshots(uuid).destroy(name), {message: msg}.merge(opt(ARGV, options))
171
+ end
172
+
173
+ desc 'start [NAME]', 'Starts a stopped machine from the referenced snapshot.'
174
+ def start(name)
175
+ uuid = options.uuid.nil? ? config[:use_machine] : options.uuid
176
+ msg = "Machine #{uuid} snapshots #{options[:key]} started"
177
+ output sdc(config(opt(ARGV, options))).machines.snapshots(uuid).start(name), {message: msg}.merge(opt(ARGV, options))
178
+ end
179
+ end
180
+
181
+ class Machine < CLI::Base
182
+ desc 'ls', 'Lists all machines on record for an account.'
183
+ method_option :type, type: :string, aliases: '-t', desc: 'virtualmachine or smartmachine'
184
+ method_option :state, type: :string, aliases: '-s', desc: 'running or stopped'
185
+ def ls
186
+ include = [:id, :name, :dataset, :state]
187
+ output sdc(config(options)).machines.all(options), {table: :h, include: include}.merge(options)
188
+ end
189
+
190
+ desc 'get [UUID]', 'Gets the details for an individual machine.'
191
+ def get(uuid=nil)
192
+ uuid ||= config[:use_machine]
193
+ output sdc(config(options)).machines.read(uuid), {table: :v}.merge(options)
194
+ end
195
+
196
+ desc 'add [NAME]', 'Allows you to provision a machine.'
197
+ method_option :dataset, type: :string, aliases: '-e', desc: 'dataset URN; default is indicated in ListDatasets'
198
+ method_option :package, type: :string, aliases: '-p', desc: 'Name of the package to use on provisioning; default is indicated in ListPackages'
199
+ def add(name)
200
+ output sdc(config(options)).machines.create({name: name}.merge(options)), {table: :v}.merge(options)
201
+ end
202
+
203
+ desc 'del [UUID]', 'Allows you to completely destroy a machine.'
204
+ def del(uuid=nil)
205
+ uuid ||= config[:use_machine]
206
+ output sdc(config(options)).machines.destroy(uuid), {message: "Machine #{uuid} deleted"}.merge(options)
207
+ end
208
+
209
+ desc 'stop [UUID]', 'Allows you to shut down a machine.'
210
+ def stop(uuid=nil)
211
+ uuid ||= config[:use_machine]
212
+ output sdc(config(options)).machines.stop(uuid), {message: "Machine #{uuid} stoped"}.merge(options)
213
+ end
214
+
215
+ desc 'start [UUID]', 'Allows you to boot up a machine.'
216
+ def start(uuid=nil)
217
+ uuid ||= config[:use_machine]
218
+ output sdc(config(options)).machines.start(uuid), {message: "Machine #{uuid} started"}.merge(options)
219
+ end
220
+
221
+ desc 'reboot [UUID]', 'Allows you to reboot a machine.'
222
+ def reboot(uuid=nil)
223
+ uuid ||= config[:use_machine]
224
+ output sdc(config(options)).machines.reboot(uuid), {message: "Machine #{uuid} reboot"}.merge(options)
225
+ end
226
+
227
+ desc 'resize [UUID]', 'Allows you to resize a SmartMachine.'
228
+ method_option :package, type: :string, aliases: '-p', desc: 'Use a package name returned from ListPackages'
229
+ def resize(uuid=nil)
230
+ uuid ||= config[:use_machine]
231
+ output sdc(config(options)).machines.resize(uuid, {}.merge(options)), {message: "Machine #{uuid} resize"}.merge(options)
232
+ end
233
+
234
+ desc 'use [UUID]', 'Use machine.'
235
+ def use(uuid=nil)
236
+ config = Configure.read
237
+ if uuid.nil?
238
+ puts "use #{config[:use_machine]}"
239
+ else
240
+ config[:use_machine] = uuid
241
+ Configure.write config
242
+ end
243
+ end
244
+
245
+ register(Tag, 'tag', 'tag [COMMAND]', 'Machine tag')
246
+ register(Metadata, 'metadata', 'metadata [COMMAND]', 'Machine metadata')
247
+ register(Snapshot, 'snapshot', 'snapshot [COMMAND]', 'Machine snapshot')
248
+ end
249
+
250
+ class Analytic < CLI::Base
251
+ desc 'desc', 'Retrieves the schema for instrumentations.'
252
+ method_option :all, type: :boolean, desc: 'All describe.'
253
+ method_option :output, type: :string, :default => 'metrics', aliases: '-o', desc: 'Only output the modules or fields or types or metrics or transformations.'
254
+ def desc
255
+ if options['raw']
256
+ puts sdc(config(options)).analytics.describe.body
257
+ else
258
+ content = sdc(config(options)).analytics.describe.content
259
+ describe('modules', content, {cols: ['key', 'label']}.merge(options))
260
+ describe('fields', content, {cols: ['key', 'label', 'type']}.merge(options))
261
+ describe('types', content, {cols: ['key', 'name', 'arity', 'unit', 'abbr', 'base', 'power']}.merge(options))
262
+ describe('metrics', content, {cols: :all, exclude: [:fields]}.merge(options))
263
+ describe('transformations', content, {cols: ['key', 'label']}.merge(options))
264
+ end
265
+ end
266
+
267
+ desc 'ls', 'Retrieves all currently created instrumentations.'
268
+ def ls
269
+ include = ['id', 'module', 'stat', 'decomposition']
270
+ output sdc(config(options)).analytics.all, {table: :h, include: include}.merge(options)
271
+ end
272
+
273
+ desc 'get [ID]', 'Retrieves the configuration for an instrumentation.'
274
+ method_option :value, type: :boolean, aliases: '-v', desc: 'All metadata.'
275
+ def get(id)
276
+ if options[:value]
277
+ output sdc(config(options)).analytics.value(id), {table: :v}.merge(options)
278
+ else
279
+ output sdc(config(options)).analytics.read(id), {table: :v, exclude: ['uris']}.merge(options)
280
+ end
281
+ end
282
+
283
+ desc 'add [NAME]', 'Creates an instrumentation.'
284
+ method_option :module, type: :string, aliases: '-m', desc: 'The Cloud analytics module'
285
+ method_option :stat, type: :string, aliases: '-s', desc: 'The Cloud analytics stat'
286
+ def add
287
+ output sdc(config(options)).analytics.create(options), {table: :v, exclude: ['uris']}.merge(options)
288
+ end
289
+
290
+ desc 'del [ID]', 'Destroys an instrumentation.'
291
+ def del(id)
292
+ output sdc(config(opt(ARGV, options))).analytics.destroy(id), {message: "Analytic #{id} deleted"}.merge(opt(ARGV, options))
293
+ end
294
+
295
+ desc 'heatmap [ID]', 'Retrieves a particular instrumentation heatmap.'
296
+ method_option :image, type: :boolean, aliases: '-i', desc: 'Heatmap image file.'
297
+ method_option :x, type: :numeric, aliases: '-x', desc: 'X-Axis'
298
+ method_option :y, type: :numeric, aliases: '-y', desc: 'Y-Axis'
299
+ def heatmap(id)
300
+ if options[:image]
301
+ output sdc(config(options)).analytics.heatmap(id).image, {table: :v, exclude: ['image', 'present']}.merge(options)
302
+ else
303
+ client = sdc(config(options))
304
+ content = client.analytics.heatmap(id).image.content
305
+ query = {'ymin' => content['ymin'], 'ymax' => content['ymax']}
306
+ output client.analytics.heatmap(id).details(query.merge(options)), {table: :v}.merge(options)
307
+ end
308
+ end
309
+ end
310
+
311
+ class Main < Thor
312
+ desc 'init', 'Sets up an account on a datacenter for use with this CLI.'
313
+ def init
314
+ Configure.make
315
+ puts sdc(config).datacenters.all.status == 200 ? 'Successful configuration.' : 'Failed Configuration.'
316
+ end
317
+
318
+ register(Key, 'key', 'key [COMMAND]', 'SSH key')
319
+ register(Datacenter, 'datacenter', 'datacenter [COMMAND]', 'Smart Data Center')
320
+ register(Dataset, 'dataset', 'dataset [COMMAND]', 'Machine operating system')
321
+ register(Package, 'package', 'package [COMMAND]', 'Machine resources')
322
+ register(Machine, 'machine', 'machine [COMMAND]', 'Virtual machine')
323
+ register(Analytic, 'analytic', 'analytic [COMMAND]', 'DTrace analytics')
324
+ end
325
+ end
326
+
327
+ CLI::Main.start