oc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8260cf7e63a4e1a86fdf1909883e2eec0fe98942
4
+ data.tar.gz: e77297fc3910231db147624e1c47cae43307de1e
5
+ SHA512:
6
+ metadata.gz: 47b7cb9711308d4ba1d38faf0fd9c1ddd3f5015aebfa4d815eddebe44607de69ecf34e6e41ead98c64cee3028e37a6a8bad8e663a3733729fbb9223bd674314c
7
+ data.tar.gz: 04e6a12679c7a4d3870e076381f8546bc7509fb143f01e596e1db5e16962e1d8f69beb03524d5578bdf2862f3116e58a03133baa760581723668793b0ed2a706
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in oc.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Sedat ÇİFTÇİ
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Oc
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'oc'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install oc
18
+
19
+ ## Usage
20
+
21
+ $ oc --help
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/oc/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/oc ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require "pathname"
5
+ require "netrc"
6
+ require "colorize"
7
+ require "terminal-table"
8
+
9
+ bin_file = Pathname.new(__FILE__).realpath
10
+ $:.unshift File.expand_path("../../lib", bin_file)
11
+
12
+ config = Netrc.read
13
+
14
+ if config["api.digitalocean.com"].nil?
15
+ puts "\n"
16
+ client_id = [(print 'Digital Ocean Client ID: '), STDIN.gets.rstrip][1]
17
+ api_key = [(print 'Digital Ocean API Key: '), STDIN.gets.rstrip][1]
18
+ config["api.digitalocean.com"] = client_id, api_key
19
+ config.save
20
+ else
21
+ require 'oc'
22
+ Oc::Runner.start
23
+ end
@@ -0,0 +1,6 @@
1
+ require "oc/version"
2
+ require "system/config"
3
+ require "system/get"
4
+ require "system/client/client"
5
+ require "system/run/client"
6
+ require "system/run/run"
@@ -0,0 +1,3 @@
1
+ module Oc
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,15 @@
1
+ module Oc
2
+ class Config
3
+
4
+ def self.client_id
5
+ config = Netrc.read
6
+ config["api.digitalocean.com"][0]
7
+ end
8
+
9
+ def self.api_key
10
+ config = Netrc.read
11
+ config["api.digitalocean.com"][1]
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,63 @@
1
+ require "net/http"
2
+ require "json"
3
+ module Oc
4
+ class Get
5
+ def self.get_json path,filter=nil
6
+ source = "https://api.digitalocean.com/#{path}/?client_id=#{Oc::Config.client_id}&api_key=#{Oc::Config.api_key}&filter=#{filter}"
7
+ puts "Requesting #{source}".blue
8
+ response = Net::HTTP.get_response(URI.parse(source))
9
+ data = response.body
10
+ result = JSON.parse(data)
11
+ end
12
+
13
+
14
+ def self.get_json_parameter path, sub_path = nil ,parameters=[]
15
+ url = "https://api.digitalocean.com/#{path}"
16
+
17
+ unless sub_path.nil?
18
+ url += "/#{sub_path}"
19
+ end
20
+
21
+ url += "?client_id=#{Oc::Config.client_id}&api_key=#{Oc::Config.api_key}"
22
+
23
+ parameter = ""
24
+ if parameters.length > 0
25
+ parameters.each do |p,v|
26
+ parameter += "&#{p}=#{v}"
27
+ end
28
+ end
29
+
30
+ parameter = URI::escape(parameter)
31
+
32
+ source = url + parameter
33
+
34
+ puts "Requesting #{source}".blue
35
+ response = Net::HTTP.get_response(URI.parse(source))
36
+ data = response.body
37
+ result = JSON.parse(data)
38
+ end
39
+
40
+
41
+ def self.get_json_url url ,parameters=[]
42
+
43
+ url += "?client_id=#{Oc::Config.client_id}&api_key=#{Oc::Config.api_key}"
44
+
45
+ parameter = ""
46
+ if parameters.length > 0
47
+ parameters.each do |p,v|
48
+ parameter += "&#{p}=#{v}"
49
+ end
50
+ end
51
+
52
+ parameter = URI::escape(parameter)
53
+
54
+ source = url + parameter
55
+
56
+ puts "Requesting #{source}".blue
57
+ response = Net::HTTP.get_response(URI.parse(source))
58
+ data = response.body
59
+ result = JSON.parse(data)
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,63 @@
1
+ class Oc::Run::Base
2
+ attr_writer :options, :config
3
+
4
+
5
+ def self.method_added(method)
6
+ return if self == Oc::Run::Base
7
+ return if private_method_defined? method
8
+ return if protected_method_defined? method
9
+
10
+ prefix = self.get_object_name
11
+ method_name = method.to_s == 'run' ? nil : method.to_s.gsub('_','-')
12
+ name = [prefix, method_name].compact
13
+
14
+ Oc::Run.add({
15
+ :name => name,
16
+ :class => self,
17
+ :method => method,
18
+ :options => options
19
+ })
20
+ @options = nil
21
+ end
22
+
23
+ def self.get_object_name(value=nil)
24
+ @object_name ||= begin
25
+ value ||= if self.name && !self.name.empty?
26
+ self.name.split('::').last
27
+ end
28
+ value.to_s.split(/(?=[A-Z])/).join('-').downcase if value
29
+ end
30
+ end
31
+
32
+ def self.syntax(value)
33
+ options[:syntax] = value
34
+ end
35
+
36
+ def self.description(value)
37
+ options[:description] = value
38
+ end
39
+
40
+ def self.summary(value)
41
+ options[:summary] = value
42
+ end
43
+
44
+ def self.example(value)
45
+ options[:example] = value
46
+ end
47
+
48
+ def self.option(switches, description=nil, options={})
49
+ meta << {
50
+ :switches => switches,
51
+ :description => description,
52
+ :type => String
53
+ }
54
+ end
55
+
56
+ def self.options
57
+ @options ||= {}
58
+ end
59
+
60
+ def self.meta
61
+ options[:meta] ||=[]
62
+ end
63
+ end
@@ -0,0 +1,14 @@
1
+ require "commander"
2
+ module Oc
3
+ class Runner
4
+ def self.start
5
+
6
+ program :name, 'Digital Ocean Cliean'
7
+ program :version, Oc::VERSION
8
+ program :description, 'TCP-IP File Manager'
9
+
10
+ Oc::Run.load_commands.parse_command
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,316 @@
1
+ module Oc::Run
2
+ class Droplets < Base
3
+
4
+ def initialize
5
+ config(Netrc.read)
6
+ end
7
+
8
+ description "Show Droplets"
9
+ syntax "oc show"
10
+ def show
11
+ result = Oc::Get.get_json("droplets")
12
+ if result["status"] == "ERROR"
13
+ puts "Error: Please check your information".red
14
+ else
15
+ puts "Your Droplets".yellow
16
+ droplets = []
17
+
18
+ droplets << [
19
+ 'ID',
20
+ 'Name',
21
+ 'IP Address',
22
+ 'Private Ip Address',
23
+ 'Status',
24
+ 'Created At'
25
+ ]
26
+
27
+ result["droplets"].each do |droplet|
28
+ droplets << [
29
+ droplet["id"],
30
+ droplet["name"].red,
31
+ droplet["ip_address"].red,
32
+ droplet["private_ip_address"].nil? ? "Null".red : droplet["private_ip_address"],
33
+ droplet["status"] == "active" ? "Active".green : "Deactive".red,
34
+ droplet["created_at"]
35
+ ]
36
+ end
37
+ table = Terminal::Table.new :rows => droplets
38
+ puts table
39
+ end
40
+ end
41
+
42
+
43
+ description "Create new droplet"
44
+ syntax "oc droplets new [DROPLET_NAME] [SIZE_ID] [IMAGE_ID] [REGION_ID]"
45
+
46
+ def new(*args)
47
+ name = args[0]
48
+ size = args[1]
49
+ image_id = args[2]
50
+ region_id = args[3]
51
+ if name.nil? or size.nil? or image_id.nil? or region_id.nil?
52
+ puts "Argument Error".red
53
+ puts "Usage".yellow
54
+ puts "$ oc droplets new [DROPLET_NAME] [SIZE_ID] [IMAGE_ID] [REGION_ID]".yellow
55
+ else
56
+ raise ArgumentError, "Argument Error - #{size}" unless size =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
57
+ raise ArgumentError, "Argument Error - #{image_id}" unless image_id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
58
+
59
+ url = "https://api.digitalocean.com/droplets/new"
60
+ result = Oc::Get.get_json_url(url,{"name" => name, "size_id" => size, "image_id" => image_id, "region_id" => region_id})
61
+ if result["status"] == "ERROR"
62
+ puts "#{result["message"]}".red
63
+ else
64
+ puts "Droplet created".green
65
+ end
66
+ end
67
+ end
68
+
69
+
70
+ description "Reboot droplet"
71
+ syntax "oc droplets reboot [DROPLET_ID]"
72
+ def reboot(*args)
73
+ id = args[0]
74
+ if id.nil?
75
+ puts "Argument Error".red
76
+ puts "Usage".yellow
77
+ puts "$ oc droplets reboot [DROPLET_ID]".yellow
78
+ else
79
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
80
+ url = "https://api.digitalocean.com/droplets/#{id}/reboot"
81
+ result = Oc::Get.get_json_url(url)
82
+ if result["status"] == "ERROR"
83
+ puts "#{result["message"]}".red
84
+ else
85
+ puts "Droplet rebooted".green
86
+ end
87
+ end
88
+ end
89
+
90
+ description "This method allows you to power cycle a droplet. This will turn off the droplet and then turn it back on."
91
+ syntax "oc droplets cycle [DROPLET_ID]"
92
+ def cycle(*args)
93
+ id = args[0]
94
+ if id.nil?
95
+ puts "Argument Error".red
96
+ puts "Usage".yellow
97
+ puts "$ oc droplets cycle [DROPLET_ID]".yellow
98
+ else
99
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
100
+ url = "https://api.digitalocean.com/droplets/#{id}/power_cycle"
101
+ result = Oc::Get.get_json_url(url)
102
+ if result["status"] == "ERROR"
103
+ puts "#{result["message"]}".red
104
+ else
105
+ puts "Power cycle has been successful".green
106
+ end
107
+ end
108
+ end
109
+
110
+
111
+ description "This method allows you to shutdown a running droplet. The droplet will remain in your account."
112
+ syntax "oc droplets down [DROPLET_ID]"
113
+ def down(*args)
114
+ id = args[0]
115
+ if id.nil?
116
+ puts "Argument Error".red
117
+ puts "Usage".yellow
118
+ puts "$ oc droplets down [DROPLET_ID]".yellow
119
+ else
120
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
121
+ url = "https://api.digitalocean.com/droplets/#{id}/shutdown"
122
+ result = Oc::Get.get_json_url(url)
123
+ if result["status"] == "ERROR"
124
+ puts "#{result["message"]}".red
125
+ else
126
+ puts "Shut down has been successful".green
127
+ end
128
+ end
129
+ end
130
+
131
+
132
+ description "This method allows you to poweroff a running droplet. The droplet will remain in your account."
133
+ syntax "oc droplets off [DROPLET_ID]"
134
+ def off(*args)
135
+ id = args[0]
136
+ if id.nil?
137
+ puts "Argument Error".red
138
+ puts "Usage".yellow
139
+ puts "$ oc droplets off [DROPLET_ID]".yellow
140
+ else
141
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
142
+ url = "https://api.digitalocean.com/droplets/#{id}/power_off"
143
+ result = Oc::Get.get_json_url(url)
144
+ if result["status"] == "ERROR"
145
+ puts "#{result["message"]}".red
146
+ else
147
+ puts "Power off has been successful".green
148
+ end
149
+ end
150
+ end
151
+
152
+
153
+ description "This method allows you to poweron a powered off droplet."
154
+ syntax "oc droplets on [DROPLET_ID]"
155
+ def on(*args)
156
+ id = args[0]
157
+ if id.nil?
158
+ puts "Argument Error".red
159
+ puts "Usage".yellow
160
+ puts "$ oc droplets on [DROPLET_ID]".yellow
161
+ else
162
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
163
+ url = "https://api.digitalocean.com/droplets/#{id}/power_on"
164
+ result = Oc::Get.get_json_url(url)
165
+ if result["status"] == "ERROR"
166
+ puts "#{result["message"]}".red
167
+ else
168
+ puts "Power on has been successful".green
169
+ end
170
+ end
171
+ end
172
+
173
+
174
+ description "This method will reset the root password for a droplet. Please be aware that this will reboot the droplet to allow resetting the password."
175
+ syntax "oc droplets reset_password [DROPLET_ID]"
176
+
177
+ def reset_password(*args)
178
+ id = args[0]
179
+ if id.nil?
180
+ puts "Argument Error".red
181
+ puts "Usage".yellow
182
+ puts "$ oc droplets reset_password [DROPLET_ID]".yellow
183
+ else
184
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
185
+ url = "https://api.digitalocean.com/droplets/#{id}/password_reset"
186
+ result = Oc::Get.get_json_url(url)
187
+ if result["status"] == "ERROR"
188
+ puts "#{result["message"]}".red
189
+ else
190
+ puts "Password restored. Please check your email".green
191
+ end
192
+ end
193
+ end
194
+
195
+ description "This method allows you to resize a specific droplet to a different size. This will affect the number of processors and memory allocated to the droplet."
196
+ syntax "oc droplets resize [DROPLET_ID] [SIZE_ID]"
197
+ def resize(*args)
198
+ id = args[0]
199
+ size_id = args[1]
200
+
201
+ if id.nil? or size_id.nil?
202
+ puts "Argument Error".red
203
+ puts "Usage".yellow
204
+ puts "$ oc droplets resize [DROPLET_ID] [SIZE_ID]".yellow
205
+ else
206
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
207
+ raise ArgumentError, "Argument Error - #{id}" unless size_id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
208
+ url = "https://api.digitalocean.com/droplets/#{id}/resize/"
209
+ result = Oc::Get.get_json_url(url,{ "size" => size_id })
210
+ if result["status"] == "ERROR"
211
+ puts "#{result["error_message"]}".red
212
+ else
213
+ puts "Droplet resized".green
214
+ end
215
+ end
216
+ end
217
+
218
+ description "This method allows you to take a snapshot of the droplet once it has been powered off, which can later be restored or used to create a new droplet from the same image. Please be aware this may cause a reboot."
219
+ syntax "oc droplets snaphot [DROPLET_ID] [SNAPSHOT_NAME]"
220
+ def snapshot(*args)
221
+ id = args[0]
222
+ name = args[1]
223
+ if id.nil? or name.nil?
224
+ puts "Argument Error".red
225
+ puts "Usage".yellow
226
+ puts "$ oc droplets snaphot [DROPLET_ID] [SNAPSHOT_NAME]".yellow
227
+ else
228
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
229
+ url = "https://api.digitalocean.com/droplets/#{id}/snapshot"
230
+ result = Oc::Get.get_json_url(url, {"name" => name})
231
+ if result["status"] == "ERROR"
232
+ puts "#{result["message"]}".red
233
+ else
234
+ puts "Snapshot generated.".green
235
+ end
236
+ end
237
+ end
238
+
239
+ description "This method allows you to restore a droplet with a previous image or snapshot. This will be a mirror copy of the image or snapshot to your droplet. Be sure you have backed up any necessary information prior to restore."
240
+ syntax "oc droplets restore [DROPLET_ID] [IMAGE_ID]"
241
+
242
+ def restore(*args)
243
+ id = args[0]
244
+ image_id = args[1]
245
+ if id.nil? or image_id.nil?
246
+ puts "Argument Error".red
247
+ puts "Usage".yellow
248
+ puts "$ oc droplets restore [DROPLET_ID] [IMAGE_ID]".yellow
249
+ else
250
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
251
+ raise ArgumentError, "Argument Error - #{image_id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
252
+
253
+ url = "https://api.digitalocean.com/droplets/#{id}/restore"
254
+ result = Oc::Get.get_json_url(url, {"image_id" => image_id})
255
+ if result["status"] == "ERROR"
256
+ puts "#{result["message"]}".red
257
+ else
258
+ puts "Droplets restored.".green
259
+ end
260
+ end
261
+ end
262
+
263
+ description "This method allows you to reinstall a droplet with a default image. This is useful if you want to start again but retain the same IP address for your droplet."
264
+ syntax "oc droplets rebuild [DROPLET_ID] [IMAGE_ID]"
265
+
266
+ def rebuild(*args)
267
+ id = args[0]
268
+ image_id = args[1]
269
+ if id.nil? or image_id.nil?
270
+ puts "Argument Error".red
271
+ puts "Usage".yellow
272
+ puts "$ oc droplets rebuild [DROPLET_ID] [IMAGE_ID]".yellow
273
+ else
274
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
275
+ raise ArgumentError, "Argument Error - #{image_id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
276
+
277
+ url = "https://api.digitalocean.com/droplets/#{id}/rebuild"
278
+ result = Oc::Get.get_json_url(url, {"image_id" => image_id})
279
+ if result["status"] == "ERROR"
280
+ puts "#{result["message"]}".red
281
+ else
282
+ puts "Droplets rebuilded.".green
283
+ end
284
+ end
285
+ end
286
+
287
+ description "This method renames the droplet to the specified name."
288
+ syntax "oc droplets rename [DROPLET_ID] [NEW_NAME]"
289
+ def rename(*args)
290
+ id = args[0]
291
+ name = args[1]
292
+ if id.nil? or name.nil?
293
+ puts "Argument Error".red
294
+ puts "Usage".yellow
295
+ puts "$ oc droplets rename [DROPLET_ID] [NEW_NAME]".yellow
296
+ else
297
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
298
+
299
+ url = "https://api.digitalocean.com/droplets/#{id}/rename"
300
+ result = Oc::Get.get_json_url(url, {"name" => name})
301
+ if result["status"] == "ERROR"
302
+ puts "#{result["message"]}".red
303
+ else
304
+ puts "Droplets renamed.".green
305
+ end
306
+ end
307
+ end
308
+
309
+
310
+ private
311
+ def config(value)
312
+ @config ||= value
313
+ end
314
+
315
+ end
316
+ end
@@ -0,0 +1,157 @@
1
+ module Oc::Run
2
+ class Images < Base
3
+
4
+ description "This method returns all the available images that can be accessed by your client ID. You will have access to all public images by default, and any snapshots or backups that you have created in your own account."
5
+ syntax "co images [FLAG true -> Show my images]"
6
+ def run(*args)
7
+ filter = args[0]
8
+ if filter == "true"
9
+ result = Oc::Get.get_json("images","my_images")
10
+ if result["status"] == "ERROR"
11
+ puts "Error: Please check your information".red
12
+ else
13
+ puts "Images".yellow
14
+ rows = []
15
+
16
+ rows << [
17
+ 'ID',
18
+ 'Name',
19
+ 'Distribution',
20
+ 'Public',
21
+ 'Regions'
22
+ ]
23
+
24
+ result["images"].each do |images|
25
+ rows << [
26
+ images["id"],
27
+ images["name"].red,
28
+ images["distribution"].red,
29
+ images["public"] == true ? "True".green : "False".red,
30
+ images["regions"].join(",").yellow
31
+ ]
32
+ end
33
+ table = Terminal::Table.new :rows => rows
34
+ puts table
35
+ end
36
+ else
37
+ result = Oc::Get.get_json("images")
38
+ if result["status"] == "ERROR"
39
+ puts "Error: Please check your information".red
40
+ else
41
+ puts "Images".yellow
42
+ rows = []
43
+
44
+ rows << [
45
+ 'ID',
46
+ 'Name',
47
+ 'Distribution',
48
+ 'Public',
49
+ 'Regions'
50
+ ]
51
+
52
+ result["images"].each do |images|
53
+ rows << [
54
+ images["id"],
55
+ images["name"].red,
56
+ images["distribution"].red,
57
+ images["public"] == true ? "True".green : "False".red,
58
+ images["regions"].join(",").yellow
59
+ ]
60
+ end
61
+ table = Terminal::Table.new :rows => rows
62
+ puts table
63
+ end
64
+ end
65
+ end
66
+
67
+ description "This method displays the attributes of an image."
68
+ syntax "oc images show [IMAGE_ID]"
69
+ def show(*args)
70
+ id = args[0]
71
+ if id.nil?
72
+ puts "Argument Error".red
73
+ puts "Usage".yellow
74
+ puts "$ oc images show [IMAGE_ID]".yellow
75
+ else
76
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
77
+
78
+ url = "https://api.digitalocean.com/images/#{id}/"
79
+ result = Oc::Get.get_json_url(url)
80
+ if result["status"] == "ERROR"
81
+ puts "#{result["message"]}".red
82
+ else
83
+ puts "Images".yellow
84
+ rows = []
85
+
86
+ rows << [
87
+ 'ID',
88
+ 'Name',
89
+ 'Distribution',
90
+ 'Public',
91
+ 'Regions'
92
+ ]
93
+
94
+ images = result["image"]
95
+
96
+ rows << [
97
+ images["id"],
98
+ images["name"].red,
99
+ images["distribution"].red,
100
+ images["public"] == true ? "True".green : "False".red,
101
+ images["regions"].join(",").yellow
102
+ ]
103
+
104
+ table = Terminal::Table.new :rows => rows
105
+ puts table
106
+
107
+ end
108
+ end
109
+ end
110
+
111
+
112
+ description "This method allows you to destroy an image. There is no way to restore a deleted image so be careful and ensure your data is properly backed up."
113
+ syntax "oc images destroy [IMAGE_ID]"
114
+
115
+ def destroy(*args)
116
+ id = args[0]
117
+ if id.nil?
118
+ puts "Argument Error".red
119
+ puts "Usage".yellow
120
+ puts "$ oc images destroy [IMAGE_ID]".yellow
121
+ else
122
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
123
+ url = "https://api.digitalocean.com/images/#{id}/destroy/"
124
+ result = Oc::Get.get_json_url(url)
125
+ if result["status"] == "ERROR"
126
+ puts "#{result["message"]}".red
127
+ else
128
+ puts "Image destroyed".green
129
+ end
130
+ end
131
+ end
132
+
133
+ description "This method allows you to transfer an image to a specified region."
134
+ syntax "oc images transfer [IMAGE_ID] [REGION_ID]"
135
+ def transfer(*args)
136
+ id = args[0]
137
+ region_id = args[1]
138
+ if id.nil? or region_id.nil?
139
+ puts "Argument Error".red
140
+ puts "Usage".yellow
141
+ puts "$ oc images transfer [IMAGE_ID] [REGION_ID]".yellow
142
+ else
143
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
144
+ raise ArgumentError, "Argument Error - #{id}" unless region_id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
145
+ url = "https://api.digitalocean.com/images/#{id}/transfer/"
146
+ result = Oc::Get.get_json_url(url,{ "region_id" => region_id })
147
+ if result["status"] == "ERROR"
148
+ puts "#{result["message"]}".red
149
+ else
150
+ puts "Image transfered".green
151
+ end
152
+ end
153
+ end
154
+
155
+
156
+ end
157
+ end
@@ -0,0 +1,29 @@
1
+ module Oc::Run
2
+ class Regions < Base
3
+ description "This method will return all the available regions within the DigitalOcean cloud."
4
+ syntax "oc regions"
5
+ def run
6
+ result = Oc::Get.get_json("regions")
7
+ if result["status"] == "ERROR"
8
+ puts "Error: Please check your information".red
9
+ else
10
+ puts "Regions".yellow
11
+ droplets = []
12
+
13
+ droplets << [
14
+ 'ID',
15
+ 'Name'
16
+ ]
17
+
18
+ result["regions"].each do |droplet|
19
+ droplets << [
20
+ droplet["id"],
21
+ droplet["name"].red,
22
+ ]
23
+ end
24
+ table = Terminal::Table.new :rows => droplets
25
+ puts table
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ module Oc::Run
2
+ class Sizes < Base
3
+
4
+ description "This method returns all the available sizes that can be used to create a droplet."
5
+ syntax "oc sizes"
6
+ def run
7
+ url = "https://api.digitalocean.com/sizes"
8
+ result = Oc::Get.get_json_url(url)
9
+ if result["status"] == "ERROR"
10
+ puts "Error: #{result["error_message"]}".red
11
+ else
12
+ puts "Sizes".yellow
13
+ rows = []
14
+
15
+ rows << [
16
+ 'ID',
17
+ 'Name'
18
+ ]
19
+
20
+ result["sizes"].each do |key|
21
+ rows << [
22
+ key["id"],
23
+ key["name"].red,
24
+ ]
25
+ end
26
+ table = Terminal::Table.new :rows => rows
27
+ puts table
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,148 @@
1
+ module Oc::Run
2
+ class Ssh < Base
3
+ def initialize
4
+ config(Netrc.read)
5
+ end
6
+
7
+
8
+ description "This method lists all the available public SSH keys in your account that can be added to a droplet."
9
+ syntax "oc ssh keys"
10
+ def keys
11
+ result = Oc::Get.get_json("ssh_keys")
12
+ if result["status"] == "ERROR"
13
+ puts "Error: Please check your information".red
14
+ else
15
+ puts "Your SSH Keys".yellow
16
+ rows = []
17
+
18
+ rows << [
19
+ 'ID',
20
+ 'Name'
21
+ ]
22
+
23
+ result["ssh_keys"].each do |key|
24
+ rows << [
25
+ key["id"],
26
+ key["name"].red,
27
+ ]
28
+ end
29
+ table = Terminal::Table.new :rows => rows
30
+ puts table
31
+ end
32
+ end
33
+
34
+ description "This method allows you to add a new public SSH key to your account."
35
+ syntax "oc ssh add [KEY_NAME] [KEY_EMAIL] [KEY_PUB]"
36
+
37
+ def add(*args)
38
+ name = args[0]
39
+ email = args[1]
40
+ pub_key = args[2]
41
+ if name.nil? or pub_key.nil?
42
+ puts "Argument Error".red
43
+ puts "Usage".yellow
44
+ puts "$ oc ssh add [KEY_NAME] [KEY_EMAIL] [KEY_PUB]".yellow
45
+ else
46
+ result = Oc::Get.get_json_parameter("ssh_keys","new",{"name" => name, "ssh_pub_key" => "ssh-rsa " + pub_key + " #{email}"})
47
+ if result["status"] == "ERROR"
48
+ puts "#{result["message"]}".red
49
+ else
50
+ puts "SSH Key Added".green
51
+ end
52
+ end
53
+ end
54
+
55
+ description "This method shows a specific public SSH key in your account that can be added to a droplet."
56
+ syntax "oc ssh show [KEY_ID]"
57
+ def show(*args)
58
+ id = args[0]
59
+ if id.nil?
60
+ puts "Argument Error".red
61
+ puts "Usage".yellow
62
+ puts "$ oc ssh show [KEY_ID]".yellow
63
+ else
64
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
65
+ result = Oc::Get.get_json_parameter("ssh_keys",id)
66
+ if result["status"] == "ERROR"
67
+ puts "#{result["error_message"]}".red
68
+ else
69
+ puts "SSH Keys".yellow
70
+ rows = []
71
+
72
+ rows << [
73
+ 'ID',
74
+ 'Name',
75
+ 'SSH Pub Key'
76
+ ]
77
+
78
+ rows << [
79
+ result["ssh_key"]["id"],
80
+ result["ssh_key"]["name"].red,
81
+ result["ssh_key"]["ssh_pub_key"].red
82
+ ]
83
+
84
+ table = Terminal::Table.new :rows => rows
85
+ puts table
86
+
87
+ end
88
+ end
89
+ end
90
+
91
+ description "This method allows you to modify an existing public SSH key in your account."
92
+ syntax "oc ssh edit [KEY_ID] [KEY_NAME] [KEY_EMAIL] [KEY_PUB]"
93
+
94
+ def edit(*args)
95
+ id = args[0]
96
+ name = args[1]
97
+ email = args[2]
98
+ pub_key = args[3]
99
+
100
+ if id.nil? or name.nil? or email.nil? or pub_key.nil?
101
+ puts "Argument Error".red
102
+ puts "Usage".yellow
103
+ puts "$ oc ssh edit [KEY_ID] [KEY_NAME] [KEY_EMAIL] [KEY_PUB]".yellow
104
+ else
105
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
106
+ url = "https://api.digitalocean.com/ssh_keys/#{id}/edit/"
107
+ result = Oc::Get.get_json_url(url,{"name" => name, "ssh_pub_key" => "ssh-rsa " + pub_key + " #{email}"})
108
+ if result["status"] == "ERROR"
109
+ puts "#{result["message"]}".red
110
+ else
111
+ puts "SSH Key Edited".green
112
+ end
113
+ end
114
+ end
115
+
116
+ description "This method will delete the SSH key from your account."
117
+ syntax "oc ssh destroy [KEY_ID]"
118
+
119
+ def destroy(*args)
120
+ id = args[0]
121
+ if id.nil?
122
+ puts "Argument Error".red
123
+ puts "Usage".yellow
124
+ puts "$ oc ssh destroy [KEY_ID]".yellow
125
+ else
126
+ raise ArgumentError, "Argument Error - #{id}" unless id =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
127
+ url = "https://api.digitalocean.com/ssh_keys/#{id}/destroy/"
128
+ result = Oc::Get.get_json_url(url)
129
+ if result["status"] == "ERROR"
130
+ puts "#{result["message"]}".red
131
+ else
132
+ puts "SSH Key Deleted".green
133
+ end
134
+ end
135
+ end
136
+
137
+ private
138
+ def config(value)
139
+ @config ||=value
140
+ end
141
+
142
+ private
143
+ def new_key_request name, pub_key
144
+
145
+ end
146
+
147
+ end
148
+ end
@@ -0,0 +1,70 @@
1
+ require 'commander'
2
+ require 'commander/command'
3
+ require 'commander/import'
4
+ module Oc
5
+ module Run
6
+ autoload :Base, "system/run/base"
7
+
8
+ attr_accessor :main
9
+
10
+ def self.load_commands
11
+ Dir[File.join(File.dirname(__FILE__), "commands", "*.rb")].each do |file|
12
+ require file
13
+ end
14
+ self
15
+ end
16
+
17
+ def self.add(opts)
18
+ commands[opts[:name]] = opts
19
+ end
20
+
21
+
22
+ def self.parse_command(ins = Commander::Runner::instance)
23
+
24
+
25
+
26
+ commands.each_pair do |name, opts|
27
+ name = Array(name)
28
+ names = [name.reverse.join('-'), name.join(' ')] if name.length > 1
29
+ name = name.join(" ")
30
+
31
+
32
+ ins.command name do |c|
33
+ c.description = opts[:options][:description]
34
+ c.summary = opts[:options][:summary]
35
+ c.syntax = opts[:options][:syntax]
36
+
37
+ (options_metadata = Array(opts[:options][:meta])).each do |o|
38
+ option_data = [o[:switches], o[:type], o[:description]].compact.flatten(1)
39
+ c.option *option_data
40
+ o[:arg] = Commander::Runner.switch_to_sym(Array(o[:switches]).last)
41
+
42
+ end
43
+
44
+
45
+
46
+ c.when_called do |args, options|
47
+ object = opts[:class].new
48
+ object.options = options
49
+
50
+
51
+ run(object,opts[:method],args)
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+
59
+ protected
60
+ def self.run(obj, method, args)
61
+ obj.send(method, *args)
62
+ end
63
+
64
+
65
+ def self.commands
66
+ @commands ||= {}
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'oc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "oc"
8
+ spec.version = Oc::VERSION
9
+ spec.authors = ["Sedat ÇİFTÇİ"]
10
+ spec.email = ["iamcodegrab@gmail.com"]
11
+ spec.summary = %q{DigitalOcean Command Line Tools}
12
+ spec.description = %q{DigitalOcean Command Line Tools}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'commander', '~> 4.2.0'
22
+ spec.add_runtime_dependency 'terminal-table', '~> 1.4.5'
23
+ spec.add_runtime_dependency 'netrc', '~> 0.7.7'
24
+ spec.add_runtime_dependency 'colorize', '~> 0.7.2'
25
+
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.5"
28
+ spec.add_development_dependency "rake"
29
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sedat ÇİFTÇİ
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: commander
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: terminal-table
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.4.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.4.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: netrc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.7.7
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.7.7
55
+ - !ruby/object:Gem::Dependency
56
+ name: colorize
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.7.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.7.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: DigitalOcean Command Line Tools
98
+ email:
99
+ - iamcodegrab@gmail.com
100
+ executables:
101
+ - oc
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/oc
111
+ - lib/oc.rb
112
+ - lib/oc/version.rb
113
+ - lib/system/config.rb
114
+ - lib/system/get.rb
115
+ - lib/system/run/base.rb
116
+ - lib/system/run/client.rb
117
+ - lib/system/run/commands/droplets.rb
118
+ - lib/system/run/commands/images.rb
119
+ - lib/system/run/commands/regions.rb
120
+ - lib/system/run/commands/sizes.rb
121
+ - lib/system/run/commands/ssh.rb
122
+ - lib/system/run/run.rb
123
+ - oc.gemspec
124
+ homepage: ''
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.2.2
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: DigitalOcean Command Line Tools
148
+ test_files: []