Class: GPUEater::APIv1
- Inherits:
-
Object
- Object
- GPUEater::APIv1
- Defined in:
- lib/gpueater.rb
Defined Under Namespace
Classes: ProductsResnpose
Instance Method Summary collapse
-
#__________________________ ⇒ Object
@.
-
#___________image___________ ⇒ Object
def func_post_launch(api,required_fields=[],form={}, e=nil, try=2) raise e if try <= 0 tag = form unless tag form = @alist[((rand()*100) % @alist.length).to_i] “-”@blist[((rand()*100) % @blist.length).to_i] end required_fields.each{|v| raise “Required field => #{v}” unless form.include?(v) } image = form ssh_key_id = form product_id = form unless image.kind_of?(String) end.
-
#___________instance___________ ⇒ Object
@.
-
#___________ssh_key___________ ⇒ Object
@.
-
#__________network__________ ⇒ Object
@.
-
#__________storage__________ ⇒ Object
@.
-
#_________extensions__________ ⇒ Object
@.
-
#_________payment__________ ⇒ Object
@.
-
#_________special__________ ⇒ Object
@.
-
#_________subscription__________ ⇒ Object
@.
- #_get(u, q = {}) ⇒ Object
- #_post(u, form) ⇒ Object
-
#cancel_transaction ⇒ Object
@.
-
#change_instance_tag(form) ⇒ Object
@.
-
#close_port(form) ⇒ Object
@.
-
#copy_file ⇒ Object
@.
-
#create_image ⇒ Object
@.
-
#create_volume ⇒ Object
@.
-
#delete_file ⇒ Object
@.
-
#delete_image ⇒ Object
@.
-
#delete_snapshot ⇒ Object
@.
-
#delete_ssh_key(form) ⇒ Object
@.
-
#delete_volume ⇒ Object
@.
-
#emergency_restart_instance(form) ⇒ Object
@.
-
#file_list ⇒ Object
@.
- #func_get(api, required_fields = [], query = {}, e = nil, try = 2) ⇒ Object
- #func_post(api, required_fields = [], form = {}, e = nil, try = 2) ⇒ Object
- #func_post_inss(api, required_fields = [], form = {}, e = nil, try = 2) ⇒ Object
-
#generate_ssh_key ⇒ Object
@.
-
#image_list ⇒ Object
@.
-
#initialize ⇒ APIv1
constructor
A new instance of APIv1.
-
#instance_list ⇒ Object
@.
-
#invoice_list ⇒ Object
@.
-
#launch_ondemand_instance(form) ⇒ Object
@.
-
#launch_subcription_instance(form) ⇒ Object
@.
-
#live_migration ⇒ Object
@.
-
#login_instance ⇒ Object
@.
-
#make_directory ⇒ Object
@.
-
#make_invoice ⇒ Object
@.
-
#move_file ⇒ Object
@.
-
#network_description(form) ⇒ Object
@.
-
#ondemand_list ⇒ Object
@.
-
#open_port(form) ⇒ Object
@.
-
#port_list ⇒ Object
@.
-
#refresh_ipv4(form) ⇒ Object
@.
-
#register_image ⇒ Object
@.
-
#register_ssh_key(form) ⇒ Object
@.
- #relogin ⇒ Object
-
#renew_ipv4(form) ⇒ Object
@.
-
#restart_instance(form) ⇒ Object
@.
-
#snapshot_instance ⇒ Object
@.
-
#ssh_key_list ⇒ Object
@.
- #ssh_key_test ⇒ Object
-
#start_instance(form) ⇒ Object
@.
-
#stop_instance(form) ⇒ Object
@.
-
#subscribe_instance ⇒ Object
@.
-
#subscribe_network ⇒ Object
@.
-
#subscribe_storage ⇒ Object
@.
-
#subscription_instance_list ⇒ Object
@.
-
#subscription_invoice_list ⇒ Object
@.
-
#subscription_list ⇒ Object
@.
-
#subscription_network_list ⇒ Object
@.
-
#subscription_storage_list ⇒ Object
@.
-
#synchronize_files ⇒ Object
@.
-
#terminate_instance(form) ⇒ Object
@.
- #test ⇒ Object
-
#transfer_volume ⇒ Object
@.
-
#tunnel ⇒ Object
@.
-
#unsubscribe_instance ⇒ Object
@.
-
#unsubscribe_network ⇒ Object
@.
-
#unsubscribe_storage ⇒ Object
@.
Constructor Details
#initialize ⇒ APIv1
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/gpueater.rb', line 10 def initialize @debug = false @base = 'https://www.gpueater.com' if ENV['GPUEATER_URL'] @base = ENV['GPUEATER_URL'] end @homedir = File.('~') @tmpdir = Dir.tmpdir = File.join(@tmpdir,"gpueater_cookie.txt") @g_config = {} @g_header = {'User-Agent':'RubyAPI'} @conn = Faraday::Connection.new(:url => @base) do |builder| builder.use Faraday::Request::UrlEncoded # builder.use Faraday::Response::Logger builder.use Faraday::Adapter::NetHttpPersistent # gem install net-http-persistent end @alist=["raccoon", "dog", "wild boar", "rabbit", "cow", "horse", "wolf", "hippopotamus", "kangaroo", "fox", "giraffe", "bear", "koala", "bat", "gorilla", "rhinoceros", "monkey", "deer", "zebra", "jaguar", "polar bear", "skunk", "elephant", "raccoon dog", "animal", "reindeer", "rat", "tiger", "cat", "mouse", "buffalo", "hamster", "panda", "sheep", "leopard", "pig", "mole", "goat", "lion", "camel", "squirrel", "donkey"] @blist=["happy", "glad", "comfortable", "pleased", "delighted", "relieved", "calm", "surprised", "exciting"] begin @g_config = JSON.load(open(".eater").read) rescue begin @g_config = JSON.load(open(File.join(@homedir,".eater")).read) rescue puts "You have to define to ~/.eater" exit(9) end end begin @g_header['cookie'] = open().read rescue => e end end |
Instance Method Details
#__________________________ ⇒ Object
@
281 |
# File 'lib/gpueater.rb', line 281 def __________________________;end |
#___________image___________ ⇒ Object
def func_post_launch(api,required_fields=[],form={}, e=nil, try=2)
raise e if try <= 0
tag = form['tag']
unless tag
form['tag'] = @alist[((rand()*100) % @alist.length).to_i] +"-"+@blist[((rand()*100) % @blist.length).to_i]
end
required_fields.each{|v| raise "Required field => #{v}" unless form.include?(v) }
image = form['image']
ssh_key_id = form['ssh_key_id']
product_id = form['product_id']
unless image.kind_of?(String)
end
j = nil
begin
j = JSON.load(_post(api,form).body)
rescue => e
relogin
return func_post(api, required_fields, form, e, try-1)
end
raise j['error'] if j['error']
j['data']
end
178 |
# File 'lib/gpueater.rb', line 178 def ___________image___________;end |
#___________instance___________ ⇒ Object
@
193 |
# File 'lib/gpueater.rb', line 193 def ___________instance___________;end |
#___________ssh_key___________ ⇒ Object
@
187 |
# File 'lib/gpueater.rb', line 187 def ___________ssh_key___________;end |
#__________network__________ ⇒ Object
@
237 |
# File 'lib/gpueater.rb', line 237 def __________network__________;end |
#__________storage__________ ⇒ Object
@
246 |
# File 'lib/gpueater.rb', line 246 def __________storage__________;end |
#_________extensions__________ ⇒ Object
@
271 |
# File 'lib/gpueater.rb', line 271 def _________extensions__________;end |
#_________payment__________ ⇒ Object
@
266 |
# File 'lib/gpueater.rb', line 266 def _________payment__________;end |
#_________special__________ ⇒ Object
@
262 |
# File 'lib/gpueater.rb', line 262 def _________special__________;end |
#_________subscription__________ ⇒ Object
@
251 |
# File 'lib/gpueater.rb', line 251 def _________subscription__________;end |
#_get(u, q = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/gpueater.rb', line 49 def _get(u,q={}) puts u if @debug response = @conn.get do |req| req.url u @g_header.each{|k,v| if v req.headers[k] = v end } end return response end |
#_post(u, form) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/gpueater.rb', line 62 def _post(u,form) puts u if @debug response = @conn.post do |req| req.url u @g_header.each{|k,v| if v req.headers[k] = v end } req.body = form end return response end |
#cancel_transaction ⇒ Object
@
264 |
# File 'lib/gpueater.rb', line 264 def cancel_transaction; raise "Not implemented yet"; end |
#change_instance_tag(form) ⇒ Object
@
199 |
# File 'lib/gpueater.rb', line 199 def change_instance_tag(form); func_post('/console/servers/change_instance_tag',['instance_id','tag'],form); end |
#close_port(form) ⇒ Object
@
240 |
# File 'lib/gpueater.rb', line 240 def close_port(form); func_post('/console/servers/delete_port',['instance_id','connection_id','port'],form); end |
#copy_file ⇒ Object
@
272 |
# File 'lib/gpueater.rb', line 272 def copy_file; raise "Not implemented yet"; end |
#create_image ⇒ Object
@
182 |
# File 'lib/gpueater.rb', line 182 def create_image; raise "Not implemented yet"; end |
#create_volume ⇒ Object
@
247 |
# File 'lib/gpueater.rb', line 247 def create_volume; raise "Not implemented yet"; end |
#delete_file ⇒ Object
@
273 |
# File 'lib/gpueater.rb', line 273 def delete_file; raise "Not implemented yet"; end |
#delete_image ⇒ Object
@
184 |
# File 'lib/gpueater.rb', line 184 def delete_image; raise "Not implemented yet"; end |
#delete_snapshot ⇒ Object
@
181 |
# File 'lib/gpueater.rb', line 181 def delete_snapshot; raise "Not implemented yet"; end |
#delete_ssh_key(form) ⇒ Object
@
191 |
# File 'lib/gpueater.rb', line 191 def delete_ssh_key(form); func_post('/console/servers/delete_ssh_key',['id'],form); end |
#delete_volume ⇒ Object
@
248 |
# File 'lib/gpueater.rb', line 248 def delete_volume; raise "Not implemented yet"; end |
#emergency_restart_instance(form) ⇒ Object
@
204 |
# File 'lib/gpueater.rb', line 204 def emergency_restart_instance(form); func_post_inss('/console/servers/emergency_restart',['instance_id','machine_resource_id'],form); end |
#file_list ⇒ Object
@
276 |
# File 'lib/gpueater.rb', line 276 def file_list; raise "Not implemented yet"; end |
#func_get(api, required_fields = [], query = {}, e = nil, try = 2) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/gpueater.rb', line 111 def func_get(api,required_fields=[],query={}, e=nil, try=2) raise e if try <= 0 required_fields.each{|v| raise "Required field => #{v}" unless query.include?(v) } j = nil begin j = JSON.load(_get(api).body) rescue => e relogin return func_get(api, required_fields, query, e, try-1) end raise j['error'] if j['error'] j['data'] end |
#func_post(api, required_fields = [], form = {}, e = nil, try = 2) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/gpueater.rb', line 125 def func_post(api,required_fields=[],form={}, e=nil, try=2) raise e if try <= 0 required_fields.each{|v| raise "Required field => #{v}" unless form.include?(v) } j = nil begin j = JSON.load(_post(api,form).body) rescue => e relogin return func_post(api, required_fields, form, e, try-1) end raise j['error'] if j['error'] j['data'] end |
#func_post_inss(api, required_fields = [], form = {}, e = nil, try = 2) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/gpueater.rb', line 139 def func_post_inss(api,required_fields=[],form={}, e=nil, try=2) raise e if try <= 0 required_fields.each{|v| raise "Required field => #{v}" unless form.include?(v) } form["instances"] = [{"instance_id"=>form["instance_id"],"machine_resource_id"=>form["machine_resource_id"]}].to_json; j = nil begin j = JSON.load(_post(api,form).body) rescue => e relogin return func_post(api, required_fields, form, e, try-1) end raise j['error'] if j['error'] j['data'] end |
#generate_ssh_key ⇒ Object
@
189 |
# File 'lib/gpueater.rb', line 189 def generate_ssh_key; func_get('/console/servers/ssh_key_gen'); end |
#image_list ⇒ Object
@
179 |
# File 'lib/gpueater.rb', line 179 def image_list; func_get('/console/servers/images'); end |
#instance_list ⇒ Object
@
198 |
# File 'lib/gpueater.rb', line 198 def instance_list; func_get('/console/servers/instance_list'); end |
#invoice_list ⇒ Object
@
267 |
# File 'lib/gpueater.rb', line 267 def invoice_list; raise "Not implemented yet"; end |
#launch_ondemand_instance(form) ⇒ Object
@
196 |
# File 'lib/gpueater.rb', line 196 def launch_ondemand_instance(form); func_post('/console/servers/launch_ondemand_instance',['product_id','image','ssh_key_id','tag'],form); end |
#launch_subcription_instance(form) ⇒ Object
@
197 |
# File 'lib/gpueater.rb', line 197 def launch_subcription_instance(form); raise "Not implemented yet"; end |
#live_migration ⇒ Object
@
263 |
# File 'lib/gpueater.rb', line 263 def live_migration; raise "Not implemented yet"; end |
#login_instance ⇒ Object
@
278 |
# File 'lib/gpueater.rb', line 278 def login_instance; raise "Not implemented yet"; end |
#make_directory ⇒ Object
@
275 |
# File 'lib/gpueater.rb', line 275 def make_directory; raise "Not implemented yet"; end |
#make_invoice ⇒ Object
@
269 |
# File 'lib/gpueater.rb', line 269 def make_invoice; raise "Not implemented yet"; end |
#move_file ⇒ Object
@
274 |
# File 'lib/gpueater.rb', line 274 def move_file; raise "Not implemented yet"; end |
#network_description(form) ⇒ Object
@
243 |
# File 'lib/gpueater.rb', line 243 def network_description(form); func_get('/console/servers/instance_info',['instance_id'],form); end |
#ondemand_list ⇒ Object
@
194 |
# File 'lib/gpueater.rb', line 194 def ondemand_list; ProductsResnpose.new(func_get('/console/servers/ondemand_launch_list')); end |
#open_port(form) ⇒ Object
@
239 |
# File 'lib/gpueater.rb', line 239 def open_port(form); func_post('/console/servers/add_port',['instance_id','connection_id','port'],form); end |
#port_list ⇒ Object
@
238 |
# File 'lib/gpueater.rb', line 238 def port_list; func_get('/console/servers/port_list'); end |
#refresh_ipv4(form) ⇒ Object
@
242 |
# File 'lib/gpueater.rb', line 242 def refresh_ipv4(form); func_post('/console/servers/refresh_ipv4',['instance_id'],form); end |
#register_image ⇒ Object
@
183 |
# File 'lib/gpueater.rb', line 183 def register_image; raise "Not implemented yet"; end |
#register_ssh_key(form) ⇒ Object
@
190 |
# File 'lib/gpueater.rb', line 190 def register_ssh_key(form); func_post('/console/servers/register_ssh_key',['name','public_key'],form); end |
#relogin ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/gpueater.rb', line 76 def relogin res = _post('/api_login',{'email':@g_config['gpueater']['email'],'password':@g_config['gpueater']['password']}) if res.headers['set-cookie'] @g_header['cookie'] = res.headers['set-cookie'] f = open(,"w") f.write(@g_header['cookie']) f.close end end |
#renew_ipv4(form) ⇒ Object
@
241 |
# File 'lib/gpueater.rb', line 241 def renew_ipv4(form); func_post('/console/servers/renew_ipv4',['instance_id'],form); end |
#restart_instance(form) ⇒ Object
@
202 |
# File 'lib/gpueater.rb', line 202 def restart_instance(form); func_post_inss('/console/servers/stop',['instance_id','machine_resource_id'],form); func_post_inss('/console/servers/start',['instance_id','machine_resource_id'],form); end |
#snapshot_instance ⇒ Object
@
180 |
# File 'lib/gpueater.rb', line 180 def snapshot_instance; raise "Not implemented yet"; end |
#ssh_key_list ⇒ Object
@
188 |
# File 'lib/gpueater.rb', line 188 def ssh_key_list; func_get('/console/servers/ssh_keys'); end |
#ssh_key_test ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/gpueater.rb', line 222 def ssh_key_test key = generate_ssh_key keyname = 'my_ssh_key2' ssh_key_list().select{|e| delete_ssh_key(e) if e["name"] == keyname } register_ssh_key({"name"=>keyname,"public_key"=>key["public_key"]}) pem = File.join(@homedir,'.ssh',keyname+".pem") fp = open(pem,"w") fp.write(key["private_key"]) fp.close FileUtils.chmod(0600,pem) puts ssh_key_list end |
#start_instance(form) ⇒ Object
@
200 |
# File 'lib/gpueater.rb', line 200 def start_instance(form); func_post_inss('/console/servers/start',['instance_id','machine_resource_id'],form); end |
#stop_instance(form) ⇒ Object
@
201 |
# File 'lib/gpueater.rb', line 201 def stop_instance(form); func_post_inss('/console/servers/stop',['instance_id','machine_resource_id'],form); end |
#subscribe_instance ⇒ Object
@
255 |
# File 'lib/gpueater.rb', line 255 def subscribe_instance; raise "Not implemented yet"; end |
#subscribe_network ⇒ Object
@
259 |
# File 'lib/gpueater.rb', line 259 def subscribe_network; raise "Not implemented yet"; end |
#subscribe_storage ⇒ Object
@
257 |
# File 'lib/gpueater.rb', line 257 def subscribe_storage; raise "Not implemented yet"; end |
#subscription_instance_list ⇒ Object
@
252 |
# File 'lib/gpueater.rb', line 252 def subscription_instance_list; raise "Not implemented yet"; end |
#subscription_invoice_list ⇒ Object
@
268 |
# File 'lib/gpueater.rb', line 268 def subscription_invoice_list; raise "Not implemented yet"; end |
#subscription_list ⇒ Object
@
195 |
# File 'lib/gpueater.rb', line 195 def subscription_list; raise "Not implemented yet"; end |
#subscription_network_list ⇒ Object
@
254 |
# File 'lib/gpueater.rb', line 254 def subscription_network_list; raise "Not implemented yet"; end |
#subscription_storage_list ⇒ Object
@
253 |
# File 'lib/gpueater.rb', line 253 def subscription_storage_list; raise "Not implemented yet"; end |
#synchronize_files ⇒ Object
@
277 |
# File 'lib/gpueater.rb', line 277 def synchronize_files; raise "Not implemented yet"; end |
#terminate_instance(form) ⇒ Object
@
203 |
# File 'lib/gpueater.rb', line 203 def terminate_instance(form); func_post_inss('/console/servers/force_terminate',['instance_id','machine_resource_id'],form); end |
#test ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/gpueater.rb', line 207 def test pd = ondemand_list image = pd.find_image "Ubuntu16.04 x64" ssh_key = pd.find_ssh_key "my_ssh_key2" product = pd.find_product "n1.p400" emergency_restart_instance(instance_list[0]); p image p ssh_key["id"] p product["id"] #launch_ondemand_instance({"tag"=>"ponkoponko","product_id"=>product["id"], "ssh_key_id"=>ssh_key["id"], "image" => image["alias"]}); def ssh_key_test key = generate_ssh_key keyname = 'my_ssh_key2' ssh_key_list().select{|e| delete_ssh_key(e) if e["name"] == keyname } register_ssh_key({"name"=>keyname,"public_key"=>key["public_key"]}) pem = File.join(@homedir,'.ssh',keyname+".pem") fp = open(pem,"w") fp.write(key["private_key"]) fp.close FileUtils.chmod(0600,pem) puts ssh_key_list end end |
#transfer_volume ⇒ Object
@
249 |
# File 'lib/gpueater.rb', line 249 def transfer_volume; raise "Not implemented yet"; end |
#tunnel ⇒ Object
@
279 |
# File 'lib/gpueater.rb', line 279 def tunnel; raise "Not implemented yet"; end |
#unsubscribe_instance ⇒ Object
@
256 |
# File 'lib/gpueater.rb', line 256 def unsubscribe_instance; raise "Not implemented yet"; end |
#unsubscribe_network ⇒ Object
@
260 |
# File 'lib/gpueater.rb', line 260 def unsubscribe_network; raise "Not implemented yet"; end |
#unsubscribe_storage ⇒ Object
@
258 |
# File 'lib/gpueater.rb', line 258 def unsubscribe_storage; raise "Not implemented yet"; end |