def main(): config = read_config() c = conn(config, timeout=5) args = docopt(__doc__, version='%s %s' % (__cli_name__, __version__)) if args['list']: list_args = {'listall': 'true'} if args['--search']: list_args['keyword'] = args['--search'] res = c.list_networks(**list_args) try: res += c.list_vpcs(**list_args) except: pass if args['delete']: id = get_network(c, args['NETWORK'])[0].id if id: res = c.delete_network(id=id).get_result() if res: print "network deleted" sys.exit(0) else: print "failed to delete the network, check that there are no running instances deployed on the network." sys.exit(1) if args['create']: create_args = {'name': args['NETWORK'], 'displaytext': args['NETWORK']} if args['--offering']: create_args['networkofferingid'] = get_offering_id( c, args['--offering']) if args['--domain']: create_args['networkdomain'] = args['--domain'] if args['--zone']: zone = get_zone(c, args['--zone'])[0] create_args['zoneid'] = zone.id else: zone = get_zone(c)[0] create_args['zoneid'] = zone.id try: c.create_network(**create_args) res = c.list_networks(keyword=args['NETWORK']) except JSONDecodeError: res = c.list_networks(keyword=args['NETWORK']) except ResponseError as e: print "Error creating network: %s" % e[1] sys.exit(1) if not res: print "Error creating network" if res: print_tabulate(res, noheader=args['--noheader'], short=args['--short']) else: print "Unable to execute command" sys.exit(1)
def main(): config = read_config() c = conn(config, timeout=5) args = docopt(__doc__, version='%s %s' % (__cli_name__, __version__)) if args['list']: list_args ={'listall': 'true'} if args['--search']: list_args['keyword'] = args['--search'] res = c.list_networks(**list_args) try: res += c.list_vpcs(**list_args) except: pass if args['delete']: id = get_network(c, args['NETWORK'])[0].id if id: res = c.delete_network(id=id).get_result() if res: print "network deleted" sys.exit(0) else: print "failed to delete the network, check that there are no running instances deployed on the network." sys.exit(1) if args['create']: create_args = {'name': args['NETWORK'], 'displaytext': args['NETWORK']} if args['--offering']: create_args['networkofferingid'] = get_offering_id(c, args['--offering']) if args['--domain']: create_args['networkdomain'] = args['--domain'] if args['--zone']: zone = get_zone(c, args['--zone'])[0] create_args['zoneid'] = zone.id else: zone = get_zone(c)[0] create_args['zoneid'] = zone.id try: c.create_network(**create_args) res = c.list_networks(keyword=args['NETWORK']) except JSONDecodeError: res = c.list_networks(keyword=args['NETWORK']) except ResponseError as e: print "Error creating network: %s" % e[1] sys.exit(1) if not res: print "Error creating network" if res: print_tabulate(res, noheader=args['--noheader'], short=args['--short']) else: print "Unable to execute command" sys.exit(1)
def deploy_vm(c, a): deploy_args = {} if a['--number']: n = int(a['--number']) else: n = 1 if a['INSTANCE']: deploy_args['name'] = a['INSTANCE'] deploy_args['displayname'] = a['INSTANCE'] if a['--group']: deploy_args['group'] = a['--group'] if a['--template']: try: template = get_template(c, a['--template'])[0] deploy_args['templateid'] = template.id except: print "Unable to get templateid for %s" % a['--template'] sys.exit(1) if a['--offering']: try: offering = get_offering(c, a['--offering'])[0] deploy_args['serviceofferingid'] = offering.id except: print "Unable to get offeringid for %s" % a['--offering'] sys.exit(1) if a['--network']: try: network = get_network(c, a['--network'])[0] deploy_args['networkids'] = network.id except: print "Unable to get networkid for %s" % a['--network'] sys.exit(1) if a['--zone']: zone = get_zone(c, a['--zone'])[0] deploy_args['zoneid'] = zone.id else: zone = get_zone(c)[0] deploy_args['zoneid'] = zone.id if a['--sshkey']: try: sshkey = get_sshkey(c, a['--sshkey'])[0] deploy_args['keypair'] = sshkey.name except: print "Unable to get ssh key: %s" % a['--sshkey'] sys.exit(1) if a['--user-data']: deploy_args['userdata'] = base64.b64encode(a['--user-data']) if a['--user-data-file']: if os.path.isfile(a['--user-data-file']): with open(a['--user-data-file']) as f: encoded = base64.b64encode(f.read()) size = sys.getsizeof(encoded) deploy_args['userdata'] = encoded # Deploy it! res = [] for i in range(n): if n > 1: append_hash = hashlib.new('sha1', str(random.randint(0,1000000))).hexdigest()[:3] deploy_args['name'] = "%s-%s" % (a['INSTANCE'], append_hash) deploy_args['displayname'] = "%s-%s" % (a['INSTANCE'], append_hash) res.append(c.deploy_virtualmachine(**deploy_args)) vms = [] if a['--tags']: tags = {} for tag in a['--tags'].split(','): regex = re.compile(r"\b(\w+)\s*:\s*([^:]*)(?=\s+\w+\s*:|$)") tags.update(dict(regex.findall(tag))) if not a['--nowait']: for j in res: if a['--tags']: c.create_tags(resourceids=j.id, resourcetype='UserVm', tags=tags) vms.append(j.get_result()) else: for j in res: if a['--tags']: c.create_tags(resourceids=j.id, resourcetype='UserVm', tags=tags) vms.append(c.list_virtualmachines(id=j.id)[0]) return vms
def deploy_vm(c, a): deploy_args = {} if a['--number']: n = int(a['--number']) else: n = 1 if a['INSTANCE']: deploy_args['name'] = a['INSTANCE'] deploy_args['displayname'] = a['INSTANCE'] if a['--group']: deploy_args['group'] = a['--group'] if a['--template']: try: template = get_template(c, a['--template'])[0] deploy_args['templateid'] = template.id except: print "Unable to get templateid for %s" % a['--template'] sys.exit(1) if a['--offering']: try: offering = get_offering(c, a['--offering'])[0] deploy_args['serviceofferingid'] = offering.id except: print "Unable to get offeringid for %s" % a['--offering'] sys.exit(1) if a['--network']: try: network = get_network(c, a['--network'])[0] deploy_args['networkids'] = network.id except: print "Unable to get networkid for %s" % a['--network'] sys.exit(1) if a['--zone']: zone = get_zone(c, a['--zone'])[0] deploy_args['zoneid'] = zone.id else: zone = get_zone(c)[0] deploy_args['zoneid'] = zone.id if a['--sshkey']: try: sshkey = get_sshkey(c, a['--sshkey'])[0] deploy_args['keypair'] = sshkey.name except: print "Unable to get ssh key: %s" % a['--sshkey'] sys.exit(1) if a['--user-data']: deploy_args['userdata'] = base64.b64encode(a['--user-data']) if a['--user-data-file']: if os.path.isfile(a['--user-data-file']): with open(a['--user-data-file']) as f: encoded = base64.b64encode(f.read()) size = sys.getsizeof(encoded) deploy_args['userdata'] = encoded # Deploy it! res = [] for i in range(n): if n > 1: append_hash = hashlib.new('sha1', str(random.randint( 0, 1000000))).hexdigest()[:3] deploy_args['name'] = "%s-%s" % (a['INSTANCE'], append_hash) deploy_args['displayname'] = "%s-%s" % (a['INSTANCE'], append_hash) res.append(c.deploy_virtualmachine(**deploy_args)) vms = [] if a['--tags']: tags = {} for tag in a['--tags'].split(','): regex = re.compile(r"\b(\w+)\s*:\s*([^:]*)(?=\s+\w+\s*:|$)") tags.update(dict(regex.findall(tag))) if not a['--nowait']: for j in res: if a['--tags']: c.create_tags(resourceids=j.id, resourcetype='UserVm', tags=tags) vms.append(j.get_result()) else: for j in res: if a['--tags']: c.create_tags(resourceids=j.id, resourcetype='UserVm', tags=tags) vms.append(c.list_virtualmachines(id=j.id)[0]) return vms