def run(command): banner(command, c='-') parameter = command.split(" ") shell_command = parameter[0] args = parameter[1:] result = Shell.execute(shell_command, args) print(result) return result
def test_006(self): """testing cm group list --category=kilo --format json groupA""" HEADING() banner("cm group list --category=kilo --format=json groupA") result = run("cm group list --category=kilo --format=json groupA") assert "groupA" in result return
def run(command): banner(command, c='-') parameter = command.split(" ") shell_command = parameter[0] args = parameter[1:] result = Shell.execute(shell_command, args) print(result) return result
def run(self, command): command = command.format(**self.data) banner(command) parameter = command.split(" ") shell_command = parameter[0] args = parameter[1:] result = Shell.execute(shell_command, args) return result
def test_004(self): """testing cm group merge groupA groupB groupC""" HEADING() banner("cm group merge groupA groupB groupC") result = run("cm group merge groupA groupB groupC") assert "ok." in result return
def test_004(self): """testing cm group merge groupA groupB groupC""" HEADING() banner("cm group merge groupA groupB groupC") result = run("cm group merge groupA groupB groupC") assert "ok." in result return
def test_006(self): """testing cm group list --category=kilo --format json groupA""" HEADING() banner("cm group list --category=kilo --format=json groupA") result = run("cm group list --category=kilo --format=json groupA") assert "groupA" in result return
def test_003(self): """testing cm group copy groupA groupB""" HEADING() banner("cm group copy groupA groupB") result = run("cm group copy groupA groupB") assert "[groupB]" in result return
def test_003(self): """testing cm list --cloud trial --user fake default""" HEADING() banner("cm list --cloud=trial --user=fake default") result = self.run("cm list --cloud=trial --user=fake default") assert "No" in result return
def test_003(self): """testing cm list --cloud trial --user fake default""" HEADING() banner("cm list --cloud=trial --user=fake default") result = self.run("cm list --cloud=trial --user=fake default") assert "No" in result return
def run(self, command): command = command.format(**self.data) banner(command, c='-') parameter = command.split(" ") shell_command = parameter[0] args = parameter[1:] result = Shell.execute(shell_command, args) print(result) return str(result)
def test_003(self): """testing cm group copy groupA groupB""" HEADING() banner("cm group copy groupA groupB") result = run("cm group copy groupA groupB") assert "[groupB]" in result return
def test_001(self): HEADING() d = self.d banner("VM Data") pprint(d.__dict__) banner("Add VM") cm = CloudmeshDatabase() name = "vm1" uuid = d.id vm = VM(name=name, uuid=uuid, user="******", type="VM", category="mycloud", **d) banner("VM added") pprint(vm.__dict__) vm.bla = "bla" cm.add(vm) cm.save() banner("Get VM from Database") o = cm.find(VM, name=name) #o = cm.find_by_name(VM, name) pprint(o) assert True
def test_009(self): """testing cm group remove --category=kilo --name=groupA --id=test-002""" HEADING() banner("cm group remove --category=kilo --name=groupA --id=test-002") result1 = run("cm group remove --category=kilo --name=groupA --id=" "test-002") print(result1) assert "Successfully removed ID" in result1 result2 = run("cm group list groupA") assert "test-002" not in result2 return
def test_009(self): """testing cm group remove --category=kilo --name=groupA --id=test-002""" HEADING() banner("cm group remove --category=kilo --name=groupA --id=test-002") result1 = run("cm group remove --category=kilo --name=groupA --id=" "test-002") print(result1) assert "Successfully removed ID" in result1 result2 = run("cm group list groupA") assert "test-002" not in result2 return
def test_001(self): HEADING() d = self.d banner("VM Data") pprint(d.__dict__) banner("Add VM") cm = CloudmeshDatabase() name = "vm1" uuid = d.id vm = VM(name=name, uuid=uuid, user="******", type="VM", category="mycloud", **d) banner("VM added") pprint (vm.__dict__) vm.bla = "bla" cm.add(vm) cm.save() banner("Get VM from Database") o = cm.find(VM, name=name) #o = cm.find_by_name(VM, name) pprint (o) assert True
def info(self): banner("Configuration") Console.ok('Object Attibutes: {:}'.format(', '.join(self.order))) Console.ok('Objects: {:}'.format(len(self.data))) Console.ok('Configuration File: {:}'.format(self.config_filename)) Console.ok('Configuration:') print(self.config) try: config = ConfigDict(filename=self.config_filename) except Exception as e: Console.error("Problem reading the yaml file {:}".format( self.config_filename)) Console.error("Please check if the file exists or is empty") print (e) banner("")
def info(self): banner("Configuration") Console.ok('Object Attibutes: {:}'.format(', '.join(self.order))) Console.ok('Objects: {:}'.format(len(self.data))) Console.ok('Configuration File: {:}'.format(self.config_filename)) Console.ok('Configuration:') print(self.config) try: config = ConfigDict(filename=self.config_filename) except Exception as e: Console.error("Problem reading the yaml file {:}".format( self.config_filename)) Console.error("Please check if the file exists or is empty") print(e) banner("")
def test_008(self): """testing cm group add --name=groupX --id albert-00x [WITH DEFAULT CLOUD=kilo, TYPE=VM]""" HEADING() banner("cm group add --name=groupX --id=albert-00x") result1 = run("cm default category=kilo") assert "kilo" in result1 assert "category" in result1 assert "ok" in result1 result1 = run("cm default type=vm") assert "ok." in result1 result2 = run("cm group add groupX --id=albert-00x") assert "albert-00x" in result2 result3 = run("cm group list --category=kilo groupX") assert "kilo" in result3 assert "vm" in result3 return
def test_008(self): """testing cm group add --name=groupX --id albert-00x [WITH DEFAULT CLOUD=kilo, TYPE=VM]""" HEADING() banner("cm group add --name=groupX --id=albert-00x") result1 = run("cm default category=kilo") assert "kilo" in result1 assert "category" in result1 assert "ok" in result1 result1 = run("cm default type=vm") assert "ok." in result1 result2 = run("cm group add groupX --id=albert-00x") assert "albert-00x" in result2 result3 = run("cm group list --category=kilo groupX") assert "kilo" in result3 assert "vm" in result3 return
def test_010(self): """testing cm group delete groupA --category=kilo""" HEADING() """ banner("cm group delete groupA --category=kilo") result = run("cm group delete groupA --category=kilo") print(result) assert "ok." in result """ banner("cm group delete groupB --category=kilo") result = run("cm group delete groupB --category=kilo") assert "ok." in result banner("cm group delete groupC --category=kilo") result = run("cm group delete groupC --category=kilo") assert "ok." in result banner("cm group delete groupX") result = run("cm group delete groupX") assert "ok." in result # Cleanup defaults run("cm default delete category") run("cm default delete type") return
def test_010(self): """testing cm group delete groupA --category=kilo""" HEADING() """ banner("cm group delete groupA --category=kilo") result = run("cm group delete groupA --category=kilo") print(result) assert "ok." in result """ banner("cm group delete groupB --category=kilo") result = run("cm group delete groupB --category=kilo") assert "ok." in result banner("cm group delete groupC --category=kilo") result = run("cm group delete groupC --category=kilo") assert "ok." in result banner("cm group delete groupX") result = run("cm group delete groupX") assert "ok." in result # Cleanup defaults run("cm default delete category") run("cm default delete type") return
def cloudmesh_images(request, cloud=None): banner("images") if cloud is None: cloud = Default.get_cloud() # TODO: make the cloudname a parameter data = Image.list(cloud, format='dict') print json.dumps(data, indent=4) # TODO set proper columns order = [ 'id', 'name', 'cloud', 'minDisk', 'minRam', 'os_image_size', 'progress', 'project', 'status', ] return (dict_table(request, title="Cloudmesh Images {}".format(cloud), data=data, order=order))
def cloudmesh_images(request, cloud=None): banner("images") if cloud is None: cloud = Default.get_cloud() # TODO: make the cloudname a parameter data = Image.list(cloud, format='dict') print json.dumps(data, indent=4) # TODO set proper columns order = [ 'id', 'name', 'cloud', 'minDisk', 'minRam', 'os_image_size', 'progress', 'project', 'status', ] return (dict_table(request, title="Cloudmesh Images {}".format(cloud), data=data, order=order))
def info(self, what=None, kind=None): """ prints information about the database """ count_result = {} if kind is None: kinds = tablenames() else: kinds = Parameter.expand(kind) if what is None: infos = "table,count" else: infos = Parameter.expand(what) banner("Databse table information", c="-") inspector = inspect(self.db.engine) if "table" in infos: for table_name in inspector.get_table_names(): if table_name in kinds: print(table_name + ":") for column in inspector.get_columns(table_name): print(" ", column['name'], column['type']) counter = 0 if "count" in infos: for table_name in inspector.get_table_names(): if table_name in kinds: t = table(table_name) rows = self.session.query(t).count() count_result[table_name] = rows print("Count {:}: {:}".format(table_name, rows)) counter = counter + rows count_result['sum'] = counter return count_result
def info(self, what=None, kind=None): """ prints information about the database """ count_result = {} if kind is None: kinds = tablenames() else: kinds = Parameter.expand(kind) if what is None: infos = "table,count" else: infos = Parameter.expand(what) banner("Databse table information", c="-") inspector = inspect(self.db.engine) if "table" in infos: for table_name in inspector.get_table_names(): if table_name in kinds: print(table_name + ":") for column in inspector.get_columns(table_name): print(" ", column['name'], column['type']) counter = 0 if "count" in infos: for table_name in inspector.get_table_names(): if table_name in kinds: t = table(table_name) rows = self.session.query(t).count() count_result[table_name] = rows print("Count {:}: {:}".format(table_name, rows)) counter = counter + rows count_result['sum'] = counter return count_result
delete_on_cloud = self.delete_on_cloud for map in list(keymap.values()): if delete_on_cloud: self.delete_key_on_cloud(map["cloud_name"], map["key_name_on_cloud"]) sshdb.delete_key_cloud_map_entry(key["name"]) sshdb.delete(keyname) if __name__ == "__main__": print ("HALLO") from cloudmesh_client.util import banner mykeys = SSHKeyManager() mykeys.get_all("laszewsk") banner("ssh keys") print(mykeys) print(mykeys.keys()) print("GIT") mykeys = SSHKeyManager() mykeys.get_from_git("laszewsk") print(mykeys) print(mykeys.keys()) # print(mykeys['id_rsa.pub']) # print (len(mykeys))
def test_power_nodes(action='on'): banner("TEST: power on/off a list of nodes") banner("Authenticating...") # always logon first comet = Comet() Comet.tunnelled = True token = comet.logon() authheader = {'content-type': 'application/json', "Authorization": 'Token %s' % token} url = "https://localhost:8443/nucleus/v1/" vcname = "vc2" vmnames = ["vm-vc2-0", "vm-vc2-1"] vmhosts = { vmnames[0]: "comet-01-05", vmnames[1]: "comet-01-06" } data = {"computes": [{"name": vm, "host": vmhosts[vm]} for vm in vmnames], "cluster": "%s" % vcname} if 'on' == action: banner("Issuing request to poweron nodes...") posturl = "{}/computeset/".format(url) # posturl = "%s%s/compute/poweron" % (url, vcname) r = Comet.http(posturl, action="post", headers=authheader, data=data) banner("RETURNED RESULTS:") print(r) elif 'off' == action: computesetid = 33 banner("Issuing request to poweroff nodes...") puturl = "%s/computeset/%s/poweroff" % (url, computesetid) # posturl = "%s%s/compute/poweron" % (url, vcname) r = Comet.http(puturl, action="put", headers=authheader) banner("RETURNED RESULTS:") print(r) else: print("The Specified Power Action NOT Supported!")
def test_get_cluster_list(): token = '' banner("TEST 1: Get without logon") authheader = {'content-type': 'application/json', "Authorization": 'Token %s' % token} geturl = "https://localhost:8443/nucleus/v1/cluster/" r = requests.get(geturl, headers=authheader, verify=False) # pprint (r) pprint(r.json()) banner("TEST 2: Auth and then get cluster list") # authurl = "https://localhost:8443/nucleus/rest-auth" # auth = Authenticator(authurl) # change user, password to proper value as set in django # in shell, we may ask user input comet = Comet() Comet.tunnelled = True token = comet.logon() # construct a header with auth token after login # for all the following calls before log out authheader = {'content-type': 'application/json', "Authorization": 'Token %s' % token} geturl = "https://localhost:8443/nucleus/v1/" geturl1 = "{}cluster/".format(geturl) r = Comet.get(geturl1, headers=authheader) pprint(r) # as of 2:40pm ET Oct 15, this is changed to 'not implemented' # as of 5:30pm ET this is now fixed and working # Getting only cluster details for those owned by the caller. banner("TEST 3a: Get cluster 'OSG'") geturl1 = "%scluster/%s" % (geturl, "osg/") r1 = Comet.get(geturl1, headers=authheader) pprint(r1) banner("\nTEST 3b: Get cluster 'vc2' via tunnel") geturl1 = "%scluster/%s" % (geturl, "vc2/") r1 = Comet.get(geturl1, headers=authheader) pprint(r1) banner("\nTEST 3c: Get cluster 'vc2' directly") Comet.tunnelled = False geturl1 = Comet.url("cluster/vc2/") r1 = Comet.get(geturl1, headers=authheader) pprint(r1) banner("TEST 4: Get compute nodes sets") Comet.tunnelled = True r1 = Comet.get_computeset() pprint(r1) banner("TEST 4a: Get compute nodes set with id") r1 = Comet.get_computeset(46) pprint(r1) banner("TEST 10: logoff and get cluster list again") comet.logoff() authheader = {'content-type': 'application/json', "Authorization": 'Token %s' % token} geturl = "https://localhost:8443/nucleus/v1/cluster/" r = requests.get(geturl, headers=authheader, verify=False) pprint(r.json())
def sh(self, command): banner(command) run(command)
def makefile(tag, **kwargs): script = "\n".join(yaml.load(make)[tag]) commands = "\n".join(yaml.load(make)[tag]).format(**kwargs) banner("RUNNING") os_execute(commands)
def do_ssh(self, args, arguments): """ :: Usage: ssh table ssh list [--format=FORMAT] ssh cat ssh register NAME PARAMETERS ssh ARGUMENTS conducts a ssh login on a machine while using a set of registered machines specified in ~/.ssh/config Arguments: NAME Name or ip of the machine to log in list Lists the machines that are registered and the commands to login to them PARAMETERS Register te resource and add the given parameters to the ssh config file. if the resoource exists, it will be overwritten. The information will be written in /.ssh/config Options: -v verbose mode --format=FORMAT the format in which this list is given formats incluse table, json, yaml, dict [default: table] --user=USER overwrites the username that is specified in ~/.ssh/config --key=KEY The keyname as defined in the key list or a location that contains a pblic key Description: ssh list lists the hostsnames that are present in the ~/.ssh/config file ssh cat prints the ~/.ssh/config file ssh table prints contents of the ~/.ssh/config file in table format ssh register NAME PARAMETERS registers a host i ~/.ssh/config file Parameters are attribute=value pairs Note: Note yet implemented ssh ARGUMENTS executes the ssh command with the given arguments Example: ssh myhost conducts an ssh login to myhost if it is defined in ~/.ssh/config file """ # pprint(arguments) def read(filename=None): if filename is None: filename = "~/.ssh/config" with open(path_expand("~/.ssh/config"), "r") as f: content = f.readlines() return "".join(content) if arguments["list"]: output_format = arguments["--format"] banner('List SSH config hosts') hosts = ssh_config() for host in hosts.list(): print(host) elif arguments["table"]: content = read(filename="~/.ssh/config").split("\n") entries = [] def empty(): return { "host": None, "hostname": None, "user": None, "proxycommand": None, "serveraliveinterval": None, "localforward": None, "forwardx11": None } entry = empty() for line in content: line = line.strip() if line.startswith("#"): pass elif line.strip() == "": pass elif "Host " in line: hostname = line.strip().split("Host")[1] entry["host"] = hostname.strip() if entry is not None: entries.append(entry) entry = empty() else: attribute, value = line.strip().split(" ", 1) entry[attribute.lower()] = value.strip() pprint(entries) order = [ "host", "hostname", "user", "proxycommand", "serveraliveinterval", "localforward", "forwardx11" ] print(list_printer(entries, order=order)) elif arguments["cat"]: print(read(filename="~/.ssh/config")) elif arguments["register"]: name = arguments["NAME"] parameters = arguments["PARAMETERS"] Console.ok('register {} {}'.format(name, parameters)) TODO.implement("Not implemented") else: # ssh ARGUMENTS... args = arguments["ARGUMENTS"] os.system("ssh {}".format(args)) return "" return ""
from pprint import pprint from __future__ import print_function import requests from cloudmesh_client.util import banner r = requests.get('https://api.github.com/repos/cloudmesh/client/issues') issues = r.json() pprint(issues) for issue in issues: banner(issue["title"]) if issue["milestone"] is not None: print(issue["milestone"]["title"]) if issue["assignee"] is not None: print(issue["assignee"]["login"]) for attribute in ["number", "state", "title", "body"]: if issue[attribute]: print(attribute, ":", issue[attribute])
msg = "Unknown host" try: msg = Shell.ping("-c", "1", host) except: pass if "1 packets transmitted, 1 packets received" in msg: return True elif "Unknown host" in msg: return False else: return False if __name__ == "__main__": i = Inventory() banner("Info") i.info() banner("changing values") i.add(host="i1", cluster="kilo", label="india") i.add(host="i2", cluster="kilo", label="gregor") i.add(host="d[1-4]", cluster="kilo", label="delta") banner("saving") i.save() for output in ['dict', 'yaml', 'csv', 'table']: banner(output) print(i.list(format=output)) banner("reading")
def test_get_cluster_list(): token = '' banner("TEST 1: Get without logon") authheader = { 'content-type': 'application/json', "Authorization": 'Token %s' % token } geturl = "https://localhost:8443/nucleus/v1/cluster/" r = requests.get(geturl, headers=authheader, verify=False) # pprint (r) pprint(r.json()) banner("TEST 2: Auth and then get cluster list") # authurl = "https://localhost:8443/nucleus/rest-auth" # auth = Authenticator(authurl) # change user, password to proper value as set in django # in shell, we may ask user input comet = Comet() Comet.tunnelled = True token = comet.logon() # construct a header with auth token after login # for all the following calls before log out authheader = { 'content-type': 'application/json', "Authorization": 'Token %s' % token } geturl = "https://localhost:8443/nucleus/v1/" geturl1 = "{}cluster/".format(geturl) r = Comet.get(geturl1, headers=authheader) pprint(r) # as of 2:40pm ET Oct 15, this is changed to 'not implemented' # as of 5:30pm ET this is now fixed and working # Getting only cluster details for those owned by the caller. banner("TEST 3a: Get cluster 'OSG'") geturl1 = "%scluster/%s" % (geturl, "osg/") r1 = Comet.get(geturl1, headers=authheader) pprint(r1) banner("\nTEST 3b: Get cluster 'vc2' via tunnel") geturl1 = "%scluster/%s" % (geturl, "vc2/") r1 = Comet.get(geturl1, headers=authheader) pprint(r1) banner("\nTEST 3c: Get cluster 'vc2' directly") Comet.tunnelled = False geturl1 = Comet.url("cluster/vc2/") r1 = Comet.get(geturl1, headers=authheader) pprint(r1) banner("TEST 4: Get compute nodes sets") Comet.tunnelled = True r1 = Comet.get_computeset() pprint(r1) banner("TEST 4a: Get compute nodes set with id") r1 = Comet.get_computeset(46) pprint(r1) banner("TEST 10: logoff and get cluster list again") comet.logoff() authheader = { 'content-type': 'application/json', "Authorization": 'Token %s' % token } geturl = "https://localhost:8443/nucleus/v1/cluster/" r = requests.get(geturl, headers=authheader, verify=False) pprint(r.json())
def test_power_nodes(action='on'): banner("TEST: power on/off a list of nodes") banner("Authenticating...") # always logon first comet = Comet() Comet.tunnelled = True token = comet.logon() authheader = { 'content-type': 'application/json', "Authorization": 'Token %s' % token } url = "https://localhost:8443/nucleus/v1/" vcname = "vc2" vmnames = ["vm-vc2-0", "vm-vc2-1"] vmhosts = {vmnames[0]: "comet-01-05", vmnames[1]: "comet-01-06"} data = { "computes": [{ "name": vm, "host": vmhosts[vm] } for vm in vmnames], "cluster": "%s" % vcname } if 'on' == action: banner("Issuing request to poweron nodes...") posturl = "{}/computeset/".format(url) # posturl = "%s%s/compute/poweron" % (url, vcname) r = Comet.http(posturl, action="post", headers=authheader, data=data) banner("RETURNED RESULTS:") print(r) elif 'off' == action: computesetid = 33 banner("Issuing request to poweroff nodes...") puturl = "%s/computeset/%s/poweroff" % (url, computesetid) # posturl = "%s%s/compute/poweron" % (url, vcname) r = Comet.http(puturl, action="put", headers=authheader) banner("RETURNED RESULTS:") print(r) else: print("The Specified Power Action NOT Supported!")
def sh(self, command): banner(command) run(command)
msg = "Unknown host" try: msg = Shell.ping("-c", "1", host) except: pass if "1 packets transmitted, 1 packets received" in msg: return True elif "Unknown host" in msg: return False else: return False if __name__ == "__main__": i = Inventory() banner("Info") i.info() banner("changing values") i.add(host="i1", cluster="kilo", label="india") i.add(host="i2", cluster="kilo", label="gregor") i.add(host="d[1-4]", cluster="kilo", label="delta") banner("saving") i.save() for output in ['dict', 'yaml', 'csv', 'table']: banner(output) print(i.list(format=output)) banner("reading")
def do_ssh(self, args, arguments): """ :: Usage: ssh table ssh list [--format=FORMAT] ssh cat ssh register NAME PARAMETERS ssh ARGUMENTS conducts a ssh login on a machine while using a set of registered machines specified in ~/.ssh/config Arguments: NAME Name or ip of the machine to log in list Lists the machines that are registered and the commands to login to them PARAMETERS Register te resource and add the given parameters to the ssh config file. if the resoource exists, it will be overwritten. The information will be written in /.ssh/config Options: -v verbose mode --format=FORMAT the format in which this list is given formats incluse table, json, yaml, dict [default: table] --user=USER overwrites the username that is specified in ~/.ssh/config --key=KEY The keyname as defined in the key list or a location that contains a pblic key Description: ssh list lists the hostsnames that are present in the ~/.ssh/config file ssh cat prints the ~/.ssh/config file ssh table prints contents of the ~/.ssh/config file in table format ssh register NAME PARAMETERS registers a host i ~/.ssh/config file Parameters are attribute=value pairs Note: Note yet implemented ssh ARGUMENTS executes the ssh command with the given arguments Example: ssh myhost conducts an ssh login to myhost if it is defined in ~/.ssh/config file """ # pprint(arguments) def read(filename=None): if filename is None: filename = "~/.ssh/config" with open(path_expand("~/.ssh/config"), "r") as f: content = f.readlines() return "".join(content) if arguments["list"]: output_format = arguments["--format"] banner('List SSH config hosts') hosts = ssh_config() for host in hosts.list(): print(host) elif arguments["table"]: content = read(filename="~/.ssh/config").split("\n") entries = [ ] def empty(): return { "host": None, "hostname": None, "user": None, "proxycommand": None, "serveraliveinterval": None, "localforward": None, "forwardx11": None } entry = empty() for line in content: line = line.strip() if line.startswith("#"): pass elif line.strip() == "": pass elif "Host " in line: hostname = line.strip().split("Host")[1] entry["host"] = hostname.strip() if entry is not None: entries.append(entry) entry = empty() else: attribute, value = line.strip().split(" ", 1) entry[attribute.lower()] = value.strip() pprint(entries) order = ["host", "hostname", "user", "proxycommand", "serveraliveinterval", "localforward", "forwardx11"] print(list_printer(entries, order=order)) elif arguments["cat"]: print(read(filename="~/.ssh/config")) elif arguments["register"]: name = arguments["NAME"] parameters = arguments["PARAMETERS"] Console.ok('register {} {}'.format(name, parameters)) TODO.implement("Not implemented") else: # ssh ARGUMENTS... args = arguments["ARGUMENTS"] os.system("ssh {}".format(args)) return "" return ""
from pprint import pprint from __future__ import print_function import requests from cloudmesh_client.util import banner r = requests.get('https://api.github.com/repos/cloudmesh/client/issues') issues = r.json() pprint(issues) for issue in issues: banner(issue["title"]) if issue["milestone"] is not None: print (issue["milestone"]["title"]) if issue["assignee"] is not None: print (issue["assignee"]["login"]) for attribute in ["number", "state", "title", "body"]: if issue[attribute]: print (attribute, ":", issue[attribute])