def create_vm(profile, vmtype, storage, omi): gw = Gateway(**{'profile': profile}) with open('/Users/benjaminlaplane/.oapi_credentials') as creds: credentials = json.load(creds) region = credentials[profile]['region'] with open('config/omi.json') as omis: omi_list = json.load(omis) image = omi_list[region][omi] try: if 'db_manager_key' not in [key['KeypairName'] for key in gw.ReadKeypairs()['Keypairs']] : new_keypair = gw.CreateKeypair(KeypairName='db_manager_key') with open('../db_manager_key.rsa', 'w') as newkey: newkey.write(new_keypair['Keypair']['PrivateKey']) new_vm = gw.CreateVms(ImageId=image, VmType=vmtype, KeypairName='db_manager_key')['Vms'][0] new_vol = gw.CreateVolume(Size=storage['size'], VolumeType=storage['type'], SubregionName=region + 'a') gw.LinkVolume(VmId=new_vm['VmId'], VolumeId=new_vol['Volume']['VolumeId'], DeviceName='/dev/xvdb') except Exception as errorExcept: return False, None, errorExcept new_vm = gw.ReadVms(Filters={'VmIds': [new_vm['VmId']]})['Vms'][0] if waitforit(gw=gw, vms=[new_vm], state='running'): return True, new_vm, None return False, new_vm, None
def delete_vm(profile, vm_id): gw = Gateway(**{'profile': profile}) try: if vm_id in [vm['VmId'] for vm in gw.ReadVms()['Vms']]: gw.DeleteVms(VmIds=[vm_id]) return True except: return False
from osc_sdk_python import Gateway import json if __name__ == '__main__': gw = Gateway(**{'profile': 'default'}) #for vm in gw.ReadVms()['Vms']: # print(json.dumps(vm["SecurityGroups"])) vms = ((gw.ReadVms()['Vms'])) VMs = dict() for vm in vms: VMs.update({vm['VmId']: vm}) #print(json.dumps(VMs)) #print (VMs) #print(json.dumps(gw.ReadSecurityGroups())) #print("\nyour volumes:") #for volume in gw.ReadVolumes()["Volumes"]: #print(volume["VolumeId"]) #ssgw1 = Gateway() #print(gw1.ReadSecurityGroups( Filters = { "SecurityGroupIds": [ "sg-c387f0b7" ]})['SecurityGroups'][0]['InboundRules']) #imgs = gw.ReadImages()["Images"] #for img in imgs: # account = img["AccountAlias"] if "AccountAlias" in img else "Unknow User" # img_str = "creator: " + account + " id: " + img["ImageId"] + " name: " + img["ImageName"] # print(img_str) #print(json.dumps(gw.ReadSecurityGroups( # Filters={"SecurityGroupIds": ['sg-c387f0b7']} # )["SecurityGroups"][0]["InboundRules"])) print(json.dumps(vms)) from requests import get
import sys sys.path.append("..") from osc_sdk_python import Gateway gw = Gateway() vms = gw.ReadVms() assert isinstance(vms, dict) assert isinstance(vms["Vms"], list) vols = gw.ReadVolumes() assert isinstance(vols, dict) assert isinstance(vols["Volumes"], list)