def keyadd(name): bar = Bar('Processing', max=5) try: bar.next() nova('keypair-add', '--pub-key', '~/.ssh/id_rsa.pub', '%s' % name) except: # print "Key add error on %s" % name bar.next() try: bar.next() # print "Tryig to delete key" result = nova('keypair-delete', '%s' % name) # print result # print "Tryig to add key" bar.next() results = nova('keypair-add', '--pub-key', '~/.ssh/id_rsa.pub', '%s' % name) except: # print result print ''' Key deletion error on %s ''' % name bar.next() bar.finish() result = nova('keypair-list') print result
def delete_n(self, name, n): length = len(str(n)) name_format = "{0}-{1:0" + str(length) + "d}" for index in range(1, n + 1): print "DELETE VM", index print nova("delete", name_format.format(name, index))
def delete_n(self,name,n): length = len (str(n)) name_format = "{0}-{1:0" + str(length) + "d}" for index in range(1, n + 1): print "DELETE VM", index print nova ("delete", name_format.format(name, index))
def show(): """show all the instances with prefix-*""" try: instances = fgrep(nova('list'), prefix) for line in instances: columns = line.split('|') id = columns[1].strip() name = columns[2].strip() print 'Found %s to show' % name print nova('show', '%s' % id) except: print 'Found 0 instances to show'
def start_n(self, name, n): """fill this out""" length = len(str(n)) name_format = "{0}-{1:0" + str(length) + "d}" print "start", n, "vm(s) with name", name for index in range(1, n + 1): print "START VM", index try: print nova("boot", "--flavor", "m1.small", "--image", "futuregrid/ubuntu-12.04", "--key_name", "dmoney4454-key", name_format.format(name, index)) except: print "machine could not be started because of an error", name.format( "dmoney4454", index)
def usage(self, start, end, format='dict'): """ returns the usage information of the tennant""" # print 70 * "-" # print self.cloud.certs.__dict__.get() # print 70 * "-" tenantid = "member" # not sure how to get that iso_start = self.parse_isotime(start) iso_end = self.parse_isotime(end) print ">>>>>", iso_start, iso_end info = self.cloud.usage.get(tenantid, iso_start, iso_end) # print info.__dict__ sys.exit() result = fgrep(nova("usage", "--start", start, "--end", end), "|") (headline, matrix) = self.table_matrix(result) headline.append("Start") headline.append("End") matrix[0].append(start) matrix[0].append(end) if format == 'dict': result = {} for i in range(0, len(headline)): result[headline[i]] = matrix[0][i] return result else: return (headline, matrix[0])
def reindex(): """updates the names of all active images""" try: instances = fgrep(nova('list', '--status', 'active'), prefix) index = 0 for line in instances: line = line.replace(' ', '') ( a, id, name, status, network, rest, ) = line.split('|') newname = generate_name(index) if name != newname: nova.rename(id, newname) print 'Renaming %s -> %s' % (name, newname) else: print 'Skipping %s ' % name index = index + 1 except: print 'Found 0 instances with status error to kill' menu()
def _refresh_servers(): global instances_cache global servers try: servers = {} instances_cache = fgrep(nova('list'), prefix) for line in instances_cache: ( a, id, name, status, ip, b, ) = line.split('|') id = id.strip() servers[id] = { 'id': id, 'cloud': 'india'.strip(), 'name': name.strip(), 'status': status.strip(), 'ip': ip.strip(), } except: instances_cache = ''
def _get_keynames(): global key_cache key_cache = [] result = fgrep(tail(nova('keypair-list'), '-n', '+4'), '-v', '+') for line in result: (front, name, signature, back) = line.split('|') key_cache.append(name.strip())
def start_n (self, name, n): """fill this out""" length = len (str(n)) name_format = "{0}-{1:0" + str(length) + "d}" print "start", n, "vm(s) with name", name for index in range(1, n + 1): print "START VM", index try: print nova ("boot", "--flavor", "m1.small", "--image", "futuregrid/ubuntu-12.04", "--key_name", "~/.ssh/id_rsa.pub", name_format.format(name, index)) except: print "machine could not be started because of an error", name.format(username, index)
def start(self, name): result = nova ("boot", "--flavor", "m1.small", "--image", "futuregrid/ubuntu-12.04", "--key_name", "~/.ssh/id_rsa.pub", name) return result
def getIP(): node_name = "ACC4_" item = sh.nova("list") item_row = sh.grep(item, node_name) grepEo = sh.grep.bake("-Eo") ip_adr = grepEo(item_row, '\<Network=.*\>') ip_adr = ip_adr.replace("Network=", "") ip_adr_list = str(ip_adr).splitlines() return ip_adr_list
def list(): """Lists just my own images""" try: instances = nova('list') print instances except: print 'Found 0 instances' menu()
def bwait(index): """create a vm with the label prefix-index""" try: name = generate_name(index) tmp = nova('show', name) print tmp except: print 'Failure to launch %s' % name
def ls(): """Lists just my list - wn images""" try: instances = fgrep(nova('list'), prefix) print instances except: print 'Found 0 instances' menu()
def refresh(self): self.vms = {} lines = grep (nova("list"), "|").split("\n")[1:-1] for line in lines: line = line[2:-2] (id, name, status, networks) = line.split("|") self.vms[id] = {"id": id.strip(), "name":name.strip(), "status":status.strip(), "networks": networks.strip()}
def s(index): '''prints the summary status of instance with the index''' name = generate_name(index) try: instances = fgrep(nova('list'), name) print instances except: print 'Found 0 instances' menu()
def removeNodes(N): stringReturn = None state, amountWorkers, IP, tokens = getState() Nmax = amountWorkers if(N >= -1 and N <= Nmax): if(N == -1): N = Nmax item = sh.nova("delete","ACC4_master1") stringReturn = "You deleted the entire network" state = "shutdown" N = Nmax else: stringReturn = "You deleted "+ str(N) + "workers" for i in range(Nmax, Nmax-N, -1): item = sh.nova("delete","ACC4_worker_"+str(i)) amountWorkers -= N return stringReturn, amountWorkers
def refresh(self): self.vms = {} lines = grep(nova("list"), "|").split("\n")[1:-1] for line in lines: line = line[2:-2] (id, name, status, networks) = line.split("|") self.vms[id] = { "id": id.strip(), "name": name.strip(), "status": status.strip(), "networks": networks.strip() }
def fix(): """kills all the instances with prefix-* and in error state""" try: instances = fgrep(nova('list'), prefix) print instances for line in instances: columns = line.split('|') id = columns[1].strip() name = columns[2].strip() number = name.split('-')[1] status = columns[3].strip() if status == 'ERROR': print 'Killing %s' % name nova('delete', '%s' % id) print 'Restarting %s' % number boot(number) else: print 'Keeping %s' % name except: print 'Found 0 instances with status error to kill' menu()
def wait(): """not yet implemented""" try: instances = fgrep(nova('list'), prefix) total = _count(instances) exit bar = Bar('Processing', max=total) old = total while True: # _status() current = _count(instances) if old != current: bar.next() if current == 0: bar.finish() break sleep(2) instances = fgrep(nova('list'), prefix) except: print 'done'
def created(): """shows when a VM was created*""" t_now = datetime.now() try: instances = fgrep(nova('list'), '|') print instances for line in instances: columns = line.split('|') id = columns[1].strip() name = columns[2].strip() if id != 'ID': line = fgrep(nova('show', id), 'created') line = line.replace('\n', '') line = line.replace(' ', '') fields = line.split('|') value = fields[2] value = value.replace('T', ' ') value = value.replace('Z', '') t_a = datetime.strptime(value, '%Y-%m-%d %H:%M:%S') delta = t_now - t_a print name, '=', delta except: print 'Found 0 instances to show'
def boot(index): '''starts a virtual machine with the given index''' try: number = str(index).zfill(3) name = '%s-%s' % (prefix, number) print 'Launching VM %s' % name tmp = nova( 'boot', '--flavor=1', '--image=%s' % image_name, '--key_name', '%s' % prefix, '%s' % name, ) print tmp except Exception, e: print e print 'Failure to launch %s' % name
def vm_list(self): print "List Vms" try: servers = {} now = str(datetime.now()) instances = fgrep(nova("list"), self.user) for line in instances: (a, id, name, status, ip, b) = line.split("|") id = id.strip() servers[id] = { 'id': id, 'cloud' : cloudname.strip(), 'name': name.strip(), 'status' : status.strip(), 'ip' : ip.strip(), 'refresh' : now.strip() } except Exception, e: print e
def start(self, name): result = nova("boot", "--flavor", "m1.small", "--image", "futuregrid/ubuntu-12.04", "--key_name", "~/.ssh/id_rsa.pub", name) return result
def _del_server(id): global bar bar.next() nova('delete', '%s' % id)
def vm_del(self, id): r = False try: r = nova("delete", id) except Exception, e: print e
# from sh import nova from cloudmesh import vm_name username = "******" # simulator = False simulator = True if simulator: def nova(*args, **kwargs): return args else: from sh import nova n = 3 for index in range(1, n + 1): print "START VM", index try: print nova("boot", "--flavor", "m1.small", "--image", "futuregrid/ubuntu-12.04", "--key_name", "~/.ssh/id_rsa.pub", vm_name(username, index)) except: print "machine could not be started because of an error", name.format( username, index) for index in range(1, n + 1): print "DELETE VM", index print nova("delete", vm_name(username, index))
from sh import nova print nova ("flavor-list") print nova ("boot", "--flavor", "m1.small", "--image", "futuregrid/ubuntu-12.04", "--key_name", "dmoney4454-key", "dmoney4454-001")
def keylist(): """list my key""" nova('keypair-list')
def delete(self, name): results = nova("delete", name) return results
from sh import nova from sh import grep from pprint import pprint print 70 * "-" a = nova("list") print a print 70 * "-" b = grep(a, "|") print b print 70 * "-" c = b.split("\n") print c print 70 * "-" lines = grep(nova("list"), "|").split("\n") pprint(lines) print " I am there" lines = lines[1:-1] pprint(lines) print " I am here" vms = {} print 70 * "=" pprint(lines) print 70 * "="
simulator = False # simulator = True if simulator: def nova(*args, **kwargs): return args else: from sh import nova n = 10000 length = len(str(n)) print length name = "{0}-{1:0" + str(length) + "d}" for index in range(1, n + 1): print "START VM", index try: print nova("boot", "--flavor", "m1.small", "--image", "futuregrid/ubuntu-12.04", "--key_name", "dmoney4454-key", name.format("dmoney4454", index)) except: print "machine could not be started because of an error", name.format( "dmoney4454", index) for index in range(1, n + 1): print "DELETE VM", index print nova("delete", name.format("dmoney4454", index))
from sh import nova print nova("flavor-list") print nova("boot", "--flavor", "m1.small", "--image", "futuregrid/ubuntu-12.04", "--key_name", "dmoney4454-key", "dmoney4454-001")
def limits(): print nova('absolute-limits')
def delete(self,name): results = nova ("delete", name) return results
def start(self, name): result = nova("boot", "--flavor", "m1.small", "--image", "futuregrid/ubuntu-12.04", "--key_name", "dmoney4454-key", name) return result
def getFlavourDiskSize(flavour): from sh import nova, tail, grep, awk return int(awk(grep(nova("flavor-show", flavour), '| disk'), '{ print $4 }'))
# from sh import nova from cloudmesh import vm_name username = "******" # simulator = False simulator = True if simulator: def nova (*args, **kwargs): return args else: from sh import nova n = 3 for index in range(1, n + 1): print "START VM", index try: print nova ("boot", "--flavor", "m1.small", "--image", "futuregrid/ubuntu-12.04", "--key_name", "~/.ssh/id_rsa.pub", vm_name(username, index)) except: print "machine could not be started because of an error", name.format(username, index) for index in range(1, n + 1): print "DELETE VM", index print nova ("delete", vm_name(username, index))
def keydelete(name): """delets the named key""" nova('keypair-delete', '%s' % name)
if simulator: def nova (*args, **kwargs): return args else: from sh import nova n = 10000 length = len (str(n)) print length name = "{0}-{1:0" + str(length) + "d}" for index in range(1, n + 1): print "START VM", index try: print nova ("boot", "--flavor", "m1.small", "--image", "futuregrid/ubuntu-12.04", "--key_name", "dmoney4454-key", name.format("dmoney4454", index)) except: print "machine could not be started because of an error", name.format("dmoney4454", index) for index in range(1, n + 1): print "DELETE VM", index print nova ("delete", name.format("dmoney4454", index))
from sh import nova from sh import grep from pprint import pprint import webbrowser import random import math import os print 70 * "-" a = nova("list") print a print 70 * "-" b = grep(a,"|") print b print 70 * "-" c = b.split("\n") print c print 70 * "-" lines = grep (nova("list"), "|").split("\n") pprint (lines) print " I am there" lines = lines[1:-1] pprint (lines) print " I am here" vms = {}
# from sh import nova simulator = False # simulator = True if simulator: def nova(*args, **kwargs): return args else: from sh import nova n = 10000 length = len(str(n)) print length name = "{0}-{1:0" + str(length) + "d}" for index in range(1, n + 1): print "DELETE VM", index, name.format("dmoney4454", index) print nova("delete", name.format("dmoney4454", index))