Class: GPUEater::APIv1
- Inherits:
-
Object
- Object
- GPUEater::APIv1
- Defined in:
- lib/gpueater.rb
Defined Under Namespace
Classes: ProductsResnpose
Instance Method Summary collapse
-
#___________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
@.
- #_get(u, q = {}) ⇒ Object
- #_post(u, form) ⇒ Object
-
#change_instance_tag(form) ⇒ Object
@.
-
#create_image ⇒ Object
@.
-
#delete_image ⇒ Object
@.
-
#delete_snapshot ⇒ Object
@.
-
#delete_ssh_key(form) ⇒ Object
@.
-
#emergency_restart_instance(form) ⇒ 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
@.
-
#launch_ondemand_instance(form) ⇒ Object
@.
-
#launch_subcription_instance(form) ⇒ Object
@.
-
#ondemand_list ⇒ Object
@.
-
#register_image ⇒ Object
@.
-
#register_ssh_key(form) ⇒ Object
@.
- #relogin ⇒ Object
-
#restart_instance(form) ⇒ Object
@.
-
#snapshot_instance ⇒ Object
@.
-
#ssh_key_list ⇒ Object
@.
- #ssh_key_test ⇒ Object
-
#start_instance(form) ⇒ Object
@.
-
#stop_instance(form) ⇒ Object
@.
-
#subscription_list ⇒ Object
@.
-
#terminate_instance(form) ⇒ Object
@.
- #test ⇒ Object
Constructor Details
#initialize ⇒ APIv1
Returns a new instance of 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 @cookie_path = 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(@cookie_path).read rescue => e end end |
Instance Method Details
#___________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 |
#_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 |
#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 |
#create_image ⇒ Object
@
182 |
# File 'lib/gpueater.rb', line 182 def create_image; 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 |
#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 |
#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 form.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 |
#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 |
#ondemand_list ⇒ Object
@
194 |
# File 'lib/gpueater.rb', line 194 def ondemand_list; ProductsResnpose.new(func_get('/console/servers/ondemand_launch_list')); 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(@cookie_path,"w") f.write(@g_header['cookie']) f.close end 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 |
#subscription_list ⇒ Object
@
195 |
# File 'lib/gpueater.rb', line 195 def subscription_list; 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 |