示例#1
0
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
示例#2
0
    # 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(gw.CreateKeypair(KeypairName='test1234'),
                   sort_keys=True,
                   indent=4))
    print(json.dumps(gw.ReadKeypairs(), sort_keys=True, indent=4))

    from requests import get