From 26bbab6fc75f82f3e505eeaa9d08ad1c543f102b Mon Sep 17 00:00:00 2001 From: "gasperfele@fri1.uni-lj.si" Date: Fri, 30 Jan 2015 15:20:05 +0000 Subject: test_prepare_disks now seems to work. git-svn-id: https://svn.lusy.fri.uni-lj.si/kpov-public-svn/kpov-public@253 5cf9fbd1-b2bc-434c-b4b7-e852f4f63414 --- .../tasks/copy_rename_20_files_tail_env/task.py | 6 ++ kpov_judge/test_prepare_disks.py | 119 +++++++++++++-------- 2 files changed, 81 insertions(+), 44 deletions(-) (limited to 'kpov_judge') diff --git a/kpov_judge/tasks/copy_rename_20_files_tail_env/task.py b/kpov_judge/tasks/copy_rename_20_files_tail_env/task.py index 22b8b2d..a321bc2 100644 --- a/kpov_judge/tasks/copy_rename_20_files_tail_env/task.py +++ b/kpov_judge/tasks/copy_rename_20_files_tail_env/task.py @@ -169,5 +169,11 @@ def task_check(results, params): def prepare_disks(templates, params): # d = templates['simpleArbiterDhcp'] + print "Haha!" + print params + print templates + d = templates['malishell'] + d.touch('/home/student/mamica_je_ena_sama.txt') + d.chown('student', 'www-data', '/home/student/mamica_je_ena_sama.txt') pass diff --git a/kpov_judge/test_prepare_disks.py b/kpov_judge/test_prepare_disks.py index a8bd539..2df42ec 100755 --- a/kpov_judge/test_prepare_disks.py +++ b/kpov_judge/test_prepare_disks.py @@ -1,117 +1,107 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import pymongo import sys import inspect import kpov_random_helpers -import settings import guestfs import os import glob import subprocess import fcntl -import pyssh import paramiko +import urllib2 +import yaml +from test_task import http_auth -class SSHGuestFs(): - def __init__(hostname, path, username, password): +class SSHGuestFs: + def __init__(self, hostname, username, password): return_results = {} client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(hostname, username=username, password=password) self.conn = client - self.mountdir = path def chmod(self, mode, path): - self.conn.exec_command('chmod {} "{}"'.format(mode, os.path.join(self.mountdir, path))) - def chown(self, owner, group, path) - self.conn.exec_command('chown {}.{} "{}"'.format(owner, group, os.path.join(self.mountdir, path)) + self.conn.exec_command('chmod {} "{}"'.format(mode, path)) + def chown(self, owner, group, path): + self.conn.exec_command('chown {}.{} "{}"'.format(owner, group, path)) def command(self, arguments): self.conn.exec_command(arguments) def cp(self, src, dest): - src = os.path.join(self.mountdir, src) - dst = os.path.join(self.mountdir, dst) self.conn.exec_command('cp "{}" "{}"'.format(src, dest)) def cp_a(self, src, dest): - src = os.path.join(self.mountdir, src) - dst = os.path.join(self.mountdir, dst) self.conn.exec_command('cp -a "{}" "{}"'.format(src, dest)) def cp_r(self, src, dest): - src = os.path.join(self.mountdir, src) - dst = os.path.join(self.mountdir, dst) self.conn.exec_command('cp -r "{}" "{}"'.format(src, dest)) def dd(self, src, dest): - src = os.path.join(self.mountdir, src) - dst = os.path.join(self.mountdir, dst) self.conn.exec_command('dd if="{}" of="{}"'.format(src, dest)) def df(self): stdin, stdout, stderr = self.conn.exec_command('df') return stdin.read() def download(self, remotefilename, filename): - path = os.path.join(self.mountdir, remotefilename) stdin, stdout, stderr = self.conn.exec_command('dd if="{}"'.format(path)) with open(filename, 'w') as f: - while data = stdin.read(4096): + data = stdin.read(4096) + while data: f.write(data) + data = stdin.read(4096) def download_offset (self, remotefilename, filename, offset, size): - path = os.path.join(self.mountdir, remotefilename) stdin, stdout, stderr = self.conn.exec_command('dd bs=1 skip={} count={} if="{}"'.format( offset, size ,path)) with open(filename, 'w') as f: - while data = stdin.read(4096): + data = stdin.read(4096) + while data: f.write(data) - def du(self, path) - path = os.path.join(self.mountdir, path) + data = stdin.read(4096) + def du(self, path): stdin, stdout, stderr = self.conn.exec_command('du "{}"'.format(path)) return stdin.read() - def equal(self, file1, file2) - src = os.path.join(self.mountdir, src) - dst = os.path.join(self.mountdir, dst) + def equal(self, file1, file2): pass - def file(self, path) - path = os.path.join(self.mountdir, path) + def file(self, path): stdin, stdout, stderr = self.conn.exec_command('file "{}"'.format(path)) return stdin.read() - def getxattrs(self, path) + def getxattrs(self, path): pass - #path = os.path.join(self.mountdir, path) + #path = path) #stdin, stdout, stderr = self.conn.exec_command('du "{}"'.format(path)) #return stdin.read() def mv (self, src, dest): - src = os.path.join(self.mountdir, src) - dst = os.path.join(self.mountdir, dst) self.conn.exec_command('mv "{}" "{}"'.format(src, dest)) def mkdir (self, path): - path = os.path.join(self.mountdir, path) sftp_client = self.conn.open_sftp() sftp_client.mkdir(path) def read_file (self, path): - path = os.path.join(self.mountdir, path) sftp_client = self.conn.open_sftp() f = sftp_client.file(path, mode='r', bufsize=-1) s = f.read() f.close() return s def readdir (self, dir): - path = os.path.join(self.mountdir, path) sftp_client = self.conn.open_sftp() return sftp_client.listdir(path) def readlink (self, path): - path = os.path.join(self.mountdir, path) stdin, stdout, stderr = self.conn.exec_command('readlink "{}"'.format(path)) return stdin.read() def rename (self, oldpath, newpath): return self.mv(oldpath, newpath) def rm (self, path): - path = os.path.join(self.mountdir, path) stdin, stdout, stderr = self.conn.exec_command('rm "{}"'.format(path)) def rm_rf (self, path): - path = os.path.join(self.mountdir, path) stdin, stdout, stderr = self.conn.exec_command('rm -rf "{}"'.format(path)) def rmdir (self, path): - path = os.path.join(self.mountdir, path) stdin, stdout, stderr = self.conn.exec_command('rmdir "{}"'.format(path)) + def touch (self, path): + """Touch acts like the touch(1) command. It can be used to + update the timestamps on a file, or, if the file does + not exist, to create a new zero-length file. + + This command only works on regular files, and will fail + on other file types such as directories, symbolic links, + block special etc. + """ + self.conn.exec_command('touch "{}"'.format(path)) def setxattr (self, xattr, val, vallen, path): pass def write (self, path, content): @@ -121,7 +111,6 @@ class SSHGuestFs(): See also "g.write_append". """ - path = os.path.join(self.mountdir, path) sftp_client = self.conn.open_sftp() f = sftp_client.file(path, mode='w', bufsize=-1) f.write(content) @@ -134,7 +123,6 @@ class SSHGuestFs(): See also "g.write". """ - path = os.path.join(self.mountdir, path) sftp_client = self.conn.open_sftp() f = sftp_client.file(path, mode='a', bufsize=-1) f.write(content) @@ -142,7 +130,50 @@ class SSHGuestFs(): if __name__ == '__main__': - if len(sys.argv) != 2: - print "Usage: {0} task_file" + if len(sys.argv) != 1: + print "Usage: {0}" print "Run prepare_disks on running computers over ssh" - + print "The task name and params are read from ~/.kpov_params.yaml" + yaml_config_file = os.path.expanduser("~/.kpov_params.yaml") + with open(yaml_config_file) as f: + params = yaml.load(f) + task_name = params['task_name'] + try: + task_url = params['task_url'] + task_name = params['task_name'] + if task_url.startswith('http'): + http_auth(task_url, params['username'], params['password']) + req = urllib2.Request("{task_url}/{task_name}/task.py".format(**params)) + source = urllib2.urlopen(req).read() + t = compile(source, 'task.py', 'exec') + exec(t) + except Exception as e: + print(e) + exit(1) + templates = dict() + sshguestfs_params = params.get('sshguestfs_params', dict()) + task_sshguestfs_params = sshguestfs_params.get(task_name, dict()) + for computer_name, computer in computers.iteritems(): + comp_params = task_sshguestfs_params.get(computer_name, dict()) + for k in ['hostname', 'username', 'password']: + try: + p = comp_params[k] + except: + p = raw_input("{} {}:".format(computer_name, k)) + comp_params[k] = p + comp_connection = None + try: + comp_connection = SSHGuestFs(**comp_params) + task_sshguestfs_params[computer_name] = comp_params + except Exception, e: + print e + task_sshguestfs_params.pop(computer_name, None) + for disk in computer['disks']: + disk_name = disk['name'] + templates[disk_name] = comp_connection + sshguestfs_params[task_name] = task_sshguestfs_params + params['sshguestfs_params'] = sshguestfs_params + with open(yaml_config_file, 'w') as f: + # print "dumping", params + yaml.dump(params, f) + prepare_disks(templates, params['task_params'][task_name]) -- cgit v1.2.1