def nodebalancercreate(labels): if not labels: sys.exit('You must enter the labels of the webheads.') vpsids = {} toadd = {} webheads = {} configs = {} for vps in api.linode_list(): vpsids[vps['LABEL']] = vps['LINODEID'] for label in labels: for k, v in vpsids.iteritems(): if label in k: toadd[label] = v for k, v in toadd.iteritems(): webheads[k] = nodehasprivateip(v) r = api.nodebalancer_create(DatacenterID=datacenter, PaymentTerm=payment_term) if r['NodeBalancerID']: port = 80 a = api.nodebalancer_config_create( **nodebalancer_config(r['NodeBalancerID'], protocol='tcp')) else: sys.exit( 'No node balancer id after creation attempt, something failed majorly.' ) if a['ConfigID']: configs[a['ConfigID']] = port else: sys.exit('Failed trying to create HTTP config for node balancer.') port = 443 a = api.nodebalancer_config_create( **nodebalancer_config(r['NodeBalancerID'], protocol='tcp', port=port)) if a['ConfigID']: configs[a['ConfigID']] = port else: sys.exit( 'Failed trying to create HTTPS (SSL) config for node balancer.') # Add the nodes to the load balancer for l, ip in webheads.iteritems(): for cid, port in configs.iteritems(): a = api.nodebalancer_node_create(ConfigID=cid, Label=l, Address='{0}:{1}'.format( ip, port)) if not a['NodeID']: sys.exit( 'Failed trying to add a node to the load balancer ID: {0}'. format(cid)) print "Node Balancer configs created:{0}".format(configs)
def reboot(labels): if not labels: sys.exit('You must enter the labels to reboot') vpsids = {} toreboot = {} for vps in api.linode_list(): vpsids[vps['LABEL']] = vps['LINODEID'] for label in labels: for k, v in vpsids.iteritems(): if label in k: toreboot[label] = v for l in toreboot.values(): r = api.linode_reboot(LinodeID=l) print r
def listnodes(): vpslist = {} for ips in api.linode_ip_list(): linodeid = ips['LINODEID'] label = api.linode_list(LINODEID=linodeid)[0]['LABEL'] ip = ips['IPADDRESS'] public = ips['ISPUBLIC'] if not vpslist.get(label): vpslist[label] = {} vpslist[label][linodeid] = [] vpslist[label][linodeid].append(dict(ISPUBLIC=public, IPADDRESS=ip)) else: vpslist[label][linodeid].append(dict(ISPUBLIC=public, IPADDRESS=ip)) for key, value in vpslist.iteritems(): vpslist[key] print '{0}: {1}'.format(key, value)
def delete(labels): vpsids = {} todelete = {} # dict label:linodeid of the nodes to be deleted for vps in api.linode_list(): vpsids[vps['LABEL']] = vps['LINODEID'] for label in labels: for key, value in vpsids.iteritems(): if label in key: todelete[label] = value if todelete: print todelete if prompt('Are you sure you want to delete the above instances?'): for key, value in todelete.iteritems(): r = api.linode_delete(LINODEID=value, skipChecks=1) if r: print r if cf_delete_dns_record(cf_get_identifier(key)): print "{0} DNS record deleted.".format(key) else: print "Can't delete, no linodes matched: " + " ".join(labels[1:])