Last active
January 6, 2020 10:29
-
-
Save krrr/8731b4de31aa4850ad807bdbdb5a0823 to your computer and use it in GitHub Desktop.
Create Conoha VPS by image, and get ip address; then ๐ฎ๐ฎ๐ฎ; finally delete the VPS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from requests.exceptions import * | |
import json | |
import requests | |
import sys | |
import time | |
# original author: https://takulog.info/python-script-create-conoha-server/ | |
''' === Setting Parameters ============================= ''' | |
APIUSER = 'CHANGEME' | |
APIPASS = 'CHANGEME' | |
TENANT = 'CHANGEME' | |
ROOTPASS = APIPASS | |
SECGRP = 'gncs-ipv4-all' | |
STAG = 'CHANGEME' | |
IMAGENAME = 'CHANGEME' | |
FLAVORNAME = "g-1gb" | |
SCRIPTPATH = '' | |
''' ==================================================== ''' | |
session = None | |
def get_conoha_token(tid, user, passwd): | |
''' Function of getting a text of conoha token''' | |
_api = 'https://identity.tyo2.conoha.io/v2.0/tokens' | |
_body = { | |
"auth": { | |
"passwordCredentials": { | |
"username": user, | |
"password": passwd | |
}, | |
"tenantId": tid | |
}} | |
try: | |
_res = session.post(_api, data=json.dumps(_body)) | |
return (json.loads(_res.text))["access"]["token"]["id"] | |
except Exception as e: | |
print('Error: Could not get ConoHa token:', e) | |
sys.exit() | |
def get_flavor_uuid(tid, flavorname): | |
''' Function of getting Conoha Server Plan ID from Server Plan Name ''' | |
_api = 'https://compute.tyo2.conoha.io/v2/' + tid + '/flavors/detail' | |
try: | |
_res = session.get(_api) | |
for server in json.loads(_res.content)['flavors']: | |
if server['name'] == flavorname: | |
return server['id'] | |
except (ValueError, NameError, ConnectionError, RequestException, HTTPError) as e: | |
print('Error: Could not get ConoHa flavor uuid.', e) | |
sys.exit() | |
def get_image_uuid(tid, imagename): | |
''' Function of getting Conoha Server Image ID from Server Image Name ''' | |
_api = 'https://compute.tyo2.conoha.io/v2/' + tid + '/images/detail' | |
try: | |
_res = session.get(_api) | |
for server in json.loads(_res.content)['images']: | |
if server['name'] == imagename: | |
return server['id'] | |
except (ValueError, NameError, ConnectionError, RequestException, HTTPError) as e: | |
print('Error: Could not get ConoHa image uuid.\n', e) | |
sys.exit() | |
def create_server(tid, sgroup, stag, admin_pass, fid, iid): | |
''' Function of creatting New Server ''' | |
_api = 'https://compute.tyo2.conoha.io/v2/' + tid + '/servers' | |
_body = {"server": { | |
"metadata": {"instance_name_tag": stag}, | |
"adminPass": admin_pass, | |
"flavorRef": fid, | |
"imageRef": iid, | |
}} | |
if sgroup: | |
_body["server"]["security_groups"] = [{"name": sgroup}] | |
print('creating server...') | |
try: | |
_res = session.post(_api, data=json.dumps(_body)) | |
if _res.status_code == 202: | |
return json.loads(_res.text)['server']['id'] | |
else: | |
raise ValueError('status code not 202') | |
except (ValueError, NameError, ConnectionError, RequestException, HTTPError) as e: | |
print('Error: Could not create server.', e) | |
sys.exit() | |
except KeyError: | |
print('Error Code: {code}\nError Message: {res}'.format( | |
code=_res.text['badRequest']['message'], | |
res=_res.text['badRequest']['code'])) | |
sys.exit() | |
def get_server_detail(tid, server_id): | |
_api = 'https://compute.tyo2.conoha.io/v2/' + tid + '/servers/' + server_id | |
try: | |
_res = session.get(_api) | |
return json.loads(_res.content) | |
except (ValueError, NameError, ConnectionError, RequestException, HTTPError) as e: | |
print('Error: Could not get ConoHa server detail.\n', e) | |
sys.exit() | |
def main(): | |
global session | |
session = requests.Session() | |
session.proxies = {'https': 'http://localhost:1080'} | |
session.headers = {'Accept': 'application/json'} | |
# Get API token | |
token = get_conoha_token(TENANT, APIUSER, APIPASS) | |
print('token: ' + token) | |
session.headers['X-Auth-Token'] = token | |
# Get Flavor UUID | |
Fuuid = get_flavor_uuid(TENANT, FLAVORNAME) | |
print('flavor uuid: ' + Fuuid) | |
# Get image UUID | |
Iuuid = get_image_uuid(TENANT, IMAGENAME) | |
print('Iuuid uuid: ' + Iuuid) | |
print() | |
# Create New Server | |
server_id = create_server(TENANT, SECGRP, STAG, ROOTPASS, Fuuid, Iuuid) | |
print('success: server created! try to get ip addr...') | |
addr = {} | |
while len(addr) == 0: | |
time.sleep(3) | |
detail = get_server_detail(TENANT, server_id) | |
addr = detail['server']['addresses'] | |
print(addr) | |
input() | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from requests.exceptions import * | |
import json | |
import requests | |
import sys | |
import time | |
# original author: https://takulog.info/python-script-create-conoha-server/ | |
''' === Setting Parameters ============================= ''' | |
APIUSER = 'CHANGEME' | |
APIPASS = 'CHANGEME' | |
TENANT = 'CHANGEME' | |
ROOTPASS = APIPASS | |
SECGRP = 'gncs-ipv4-all' | |
STAG = 'CHANGEME' | |
FLAVORNAME = "g-1gb" | |
SCRIPTPATH = '' | |
''' ==================================================== ''' | |
session = None | |
def get_conoha_token(tid, user, passwd): | |
''' Function of getting a text of conoha token''' | |
_api = 'https://identity.tyo2.conoha.io/v2.0/tokens' | |
_body = { | |
"auth": { | |
"passwordCredentials": { | |
"username": user, | |
"password": passwd | |
}, | |
"tenantId": tid | |
}} | |
try: | |
_res = session.post(_api, data=json.dumps(_body)) | |
return (json.loads(_res.text))["access"]["token"]["id"] | |
except Exception as e: | |
print('Error: Could not get ConoHa token:', e) | |
sys.exit() | |
def get_server_id(tid, server_name): | |
''' Function of getting Server ID from Server Name ''' | |
_api = 'https://compute.tyo2.conoha.io/v2/' + tid + '/servers/detail' | |
try: | |
_res = session.get(_api) | |
for server in json.loads(_res.content)['servers']: | |
if server['metadata']['instance_name_tag'] == server_name: | |
return server['id'] | |
except (ValueError, NameError, ConnectionError, RequestException, HTTPError) as e: | |
print('Error: Could not get server id.\n', e) | |
sys.exit() | |
def delete_server(tid, server_id): | |
''' Function of deleting New Server ''' | |
_api = 'https://compute.tyo2.conoha.io/v2/' + tid + '/servers/' + server_id | |
print('deleting server...') | |
try: | |
_res = session.delete(_api) | |
if _res.status != 204: | |
raise ValueError('not 204') | |
print('Success: server deleted!') | |
except (ValueError, NameError, ConnectionError, RequestException, HTTPError) as e: | |
print('Error: Could not delete server.', e) | |
sys.exit() | |
def main(): | |
global session | |
session = requests.Session() | |
session.proxies = {'https': 'http://localhost:1080'} | |
session.headers = {'Accept': 'application/json'} | |
# Get API token | |
token = get_conoha_token(TENANT, APIUSER, APIPASS) | |
print('token: ' + token) | |
session.headers['X-Auth-Token'] = token | |
server_id = get_server_id(TENANT, STAG) | |
if not server_id: | |
print('no action') | |
return | |
print('server id: ' + server_id) | |
delete_server(TENANT, server_id) | |
time.sleep(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment