本文整理汇总了Python中cloudmesh_client.common.Shell.Shell.ssh方法的典型用法代码示例。如果您正苦于以下问题:Python Shell.ssh方法的具体用法?Python Shell.ssh怎么用?Python Shell.ssh使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cloudmesh_client.common.Shell.Shell
的用法示例。
在下文中一共展示了Shell.ssh方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: delete
# 需要导入模块: from cloudmesh_client.common.Shell import Shell [as 别名]
# 或者: from cloudmesh_client.common.Shell.Shell import ssh [as 别名]
def delete(cls, cluster, job, group=None):
"""
This method is used to terminate a job with the specified or a group of jobs
job_id or job_name in a given cluster
:param group:
:param cluster: the cluster like comet
:param job: the job id or name
:return: success message or error
"""
try:
if group is not None:
# get the job ids from the db
arguments = {'cluster': cluster,
'group': group}
db_jobs = cls.cm.find('batchjob',
**arguments)
list1 = []
for i in db_jobs:
list1.append(db_jobs[i]['job_id'])
# read active jobs
active_jobs = json.loads(cls.queue(cluster))
list2 = []
for i in active_jobs:
list2.append(active_jobs[i]['jobid'])
# find intersection
res = set(list1).intersection(set(list2))
if res is not None:
for j in res:
cmd = 'scancel {}'.format(str(j))
Shell.ssh(cluster, cmd)
print("Deleted {}".format(j))
return "All jobs for group {} killed successfully".format(group)
else:
args = 'scancel '
if job.isdigit():
args += job
else:
args += "-n {}".format(job)
Shell.ssh(cluster, args)
return "Job {} killed successfully".format(job)
except Exception as ex:
print("in exceptio")
print(ex)
return ex
示例2: execute
# 需要导入模块: from cloudmesh_client.common.Shell import Shell [as 别名]
# 或者: from cloudmesh_client.common.Shell.Shell import ssh [as 别名]
def execute(self, name, command):
"""executes the command on the named host"""
if name in ["localhost"]:
r = '\n'.join(Shell.sh("-c", command).split()[-1:])
else:
r = '\n'.join(Shell.ssh(name, command).split()[-1:])
return r
示例3: rm
# 需要导入模块: from cloudmesh_client.common.Shell import Shell [as 别名]
# 或者: from cloudmesh_client.common.Shell.Shell import ssh [as 别名]
def rm(cls, cluster, id=None, format=None):
data = {
"CLUSTER": cluster,
"ID": id,
}
result = None
if id is not None:
try:
result = Shell.ssh(cluster, "rm -rf experiment/{ID}".format(**data))
except Exception as e:
pass
else:
try:
result = Shell.ssh(cluster, "rm -rf experiment/*").split("\n")
except Exception as e:
pass
return result
示例4: list
# 需要导入模块: from cloudmesh_client.common.Shell import Shell [as 别名]
# 或者: from cloudmesh_client.common.Shell.Shell import ssh [as 别名]
def list(cls, cluster, id=None, format=None):
data = {
"CLUSTER": cluster,
"ID": id,
}
result = None
if id is not None:
try:
result = Shell.ssh(cluster, "ls experiment/{ID}".format(**data))
result = result.split("\n")
except Exception as e:
result = None
else:
try:
result = Shell.ssh(cluster, "ls experiment").split("\n")
ids = sorted([int(i) for i in result])
if format not in [None, 'txt']:
result = ids
except Exception as e:
result = None
return result
示例5: info
# 需要导入模块: from cloudmesh_client.common.Shell import Shell [as 别名]
# 或者: from cloudmesh_client.common.Shell.Shell import ssh [as 别名]
def info(cls, cluster, format='json', all=False):
if all:
result = Shell.ssh(cluster, 'sinfo --format=\"%all\"')
else:
result = Shell.ssh(
cluster,
'sinfo --format=\"%P|%a|%l|%D|%t|%N\"')
# ignore leading lines till header is found
l = result.splitlines()
for i, res in enumerate(l):
if 'PARTITION|AVAIL|' in res:
result = "\n".join(l[i:])
break
parser = TableParser(strip=False)
d = parser.to_dict(result)
# add cluster and updated to each entry
for key in list(d.keys()):
d[key]['cluster'] = cluster
d[key]['updated'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if format == 'json':
return json.dumps(d, indent=4, separators=(',', ': '))
else:
return (Printer.write(d,
order=['cluster',
'partition',
'avail',
'timelimit',
'nodes',
'state',
'nodelist',
'updated'],
output=format))
示例6: queue
# 需要导入模块: from cloudmesh_client.common.Shell import Shell [as 别名]
# 或者: from cloudmesh_client.common.Shell.Shell import ssh [as 别名]
def queue(cls, cluster, format='json', job=None):
try:
args = 'squeue '
if job is not None:
if job.isdigit():
args += ' -j {} '.format(str(job)) # search by job id
else:
args += ' -n {} '.format(job) # search by job name
f = '--format=%all'
args += f
result = Shell.ssh(cluster, args)
# TODO: process till header is found...(Need a better way)
l = result.splitlines()
for i, res in enumerate(l):
if 'ACCOUNT|GRES|' in res:
result = "\n".join(str(x) for x in l[i:])
break
parser = TableParser(strip=True)
d = parser.to_dict(result)
# add cluster and updated to each entry
for key in list(d.keys()):
d[key]['cluster'] = cluster
d[key]['updated'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if format == 'json':
return json.dumps(d, indent=4, separators=(',', ': '))
else:
return (Printer.write(d,
order=['cluster',
'jobid',
'partition',
'name',
'user',
'st',
'time',
'nodes',
'nodelist',
'updated'],
output=format))
except Exception as e:
Error.traceback(e)
return e
示例7: output
# 需要导入模块: from cloudmesh_client.common.Shell import Shell [as 别名]
# 或者: from cloudmesh_client.common.Shell.Shell import ssh [as 别名]
def output(cls, cluster, id=None, format=None):
data = {
"CLUSTER": cluster,
"ID": id,
}
result = None
if id is None:
ids = list(cluster)
else:
ids = [id]
result = []
for i in ids:
try:
result.append(Shell.ssh(cluster, "cat experiment/{}/*.out".format(i)))
except:
result.append("")
# if result == []:
# result = None
return result
示例8: run
# 需要导入模块: from cloudmesh_client.common.Shell import Shell [as 别名]
# 或者: from cloudmesh_client.common.Shell.Shell import ssh [as 别名]
#.........这里部分代码省略.........
d=$(date)
srun -l echo \"#CLOUDMESH: status, finished, $d\"
d=$(date)
echo \"#CLOUDMESH: status, finished, $d\"
"""
).format(**data).replace("\r\n", "\n").strip()
_from = Config.path_expand('~/.cloudmesh/{script_name}'.format(**data))
_to = '{cluster}:{remote_experiment_dir}'.format(**data)
data["from"] = _from
data["to"] = _to
data["script"] = script
# write the script to local
# print(_from)
# print(_to)
with open(_from, 'w') as local_file:
local_file.write(script)
# copy to remote host
Shell.scp(_from, _to)
# delete local file
# Shell.execute('rm', _from)
# import sys; sys.exit()
# run the sbatch command
cmd = 'sbatch {remote_experiment_dir}/{script_name}'.format(**data)
data["cmd"] = cmd
# print ("CMD>", cmd)
result = Shell.ssh(cluster, cmd)
data["output"] = result
# find id
for line in result.split("\n"):
# print ("LLL>", line)
if "Submitted batch job" in line:
data["job_id"] = int(line.replace("Submitted batch job ", "").strip())
break
#
# HACK, should not depend on Model.py
#
# from cloudmesh_client.db.model import BATCHJOB
# name = ""
# BATCHJOB(name,
# cluster=data["cluster"],
# id=data["id"],
# script=data["script"]) # has user and username which seems wrong
# here what we have in data and want to store the - options are obviously wrong
# and need to be full names
# noinspection PyPep8,PyPep8
"""
{'-D': '/N/u/gvonlasz/experiment/3',
'-N': '1',
'-o': 'gvonlasz-3.out',
'-p': 'delta',
'-t': '1',
'cluster': 'india',
'cmd': 'sbatch /N/u/gvonlasz/experiment/3/gvonlasz-3.sh',
示例9: test
# 需要导入模块: from cloudmesh_client.common.Shell import Shell [as 别名]
# 或者: from cloudmesh_client.common.Shell.Shell import ssh [as 别名]
def test(cls, cluster, time):
result = Shell.ssh(cluster,
"srun -n1 -t {} echo '#CLOUDMESH: Test ok'".format(
time))
return result
示例10: do_network
# 需要导入模块: from cloudmesh_client.common.Shell import Shell [as 别名]
# 或者: from cloudmesh_client.common.Shell.Shell import ssh [as 别名]
#.........这里部分代码省略.........
# Create a virtual cluster
elif arguments["cluster"] and \
arguments["create"]:
group_name = arguments["--group"] or \
Default.get(name="group", category=cloudname)
# Get the group information
group = Group.get_info(name=group_name,
category=cloudname,
output="json")
if group is not None:
# Convert from str to json
group = json.loads(group)
# var contains pub key of all vms
public_keys = ""
login_users = []
login_ips = []
# For each vm in the group
# Create and assign a floating IP
for item in group:
instance_id = group[item]["value"]
# Get the instance dict
instance_dict = Network.get_instance_dict(cloudname=cloudname,
instance_id=instance_id)
# Instance not found
if instance_dict is None:
Console.error("Instance {} not found in the cloudmesh database!"
.format(instance_id))
return ""
# Get the instance name
instance_name = instance_dict["name"]
floating_ip = instance_dict["floating_ip"]
# If vm does not have floating ip, then create
if floating_ip is None:
floating_ip = Network.create_assign_floating_ip(cloudname=cloudname,
instance_name=instance_name)
if floating_ip is not None:
Console.ok("Created and assigned Floating IP {} to instance {}."
.format(floating_ip, instance_name))
# Refresh VM in db
self.refresh_vm(cloudname)
# Get the login user for this machine
user = input("Enter the login user for VM {} : ".format(instance_name))
passphrase = getpass.getpass("Enter the passphrase key on VM {} : ".format(instance_name))
# create list for second iteration
login_users.append(user)
login_ips.append(floating_ip)
login_args = [
user + "@" + floating_ip,
]
keygen_args = [
"ssh-keygen -t rsa -f ~/.ssh/id_rsa -N " + passphrase
]
cat_pubkey_args = [
"cat ~/.ssh/id_rsa.pub"
]
generate_keypair = login_args + keygen_args
result = Shell.ssh(*generate_keypair)
# print("***** Keygen *****")
# print(result)
cat_public_key = login_args + cat_pubkey_args
result = Shell.ssh(*cat_public_key)
public_keys += "\n" + result
# print("***** id_rsa.pub *****")
# print(result)
# print("***** public keys *****")
# print(public_keys)
for user, ip in zip(login_users, login_ips):
arguments = [
user + "@" + ip,
"echo '" + public_keys + "' >> ~/.ssh/authorized_keys"
]
# copy the public key contents to auth_keys
result = Shell.ssh(*arguments)
Console.ok("Virtual cluster creation successfull.")
else:
Console.error("No group {} in the Cloudmesh database."
.format(group_name))
return ""
return ""
示例11: create_remote_dir
# 需要导入模块: from cloudmesh_client.common.Shell import Shell [as 别名]
# 或者: from cloudmesh_client.common.Shell.Shell import ssh [as 别名]
def create_remote_dir(cls, cluster, directory):
Shell.ssh(cluster, "mkdir -p {dir}".format(dir=directory))