summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgasperfele@fri1.uni-lj.si <gasperfele@fri1.uni-lj.si@5cf9fbd1-b2bc-434c-b4b7-e852f4f63414>2015-01-29 15:52:09 +0000
committergasperfele@fri1.uni-lj.si <gasperfele@fri1.uni-lj.si@5cf9fbd1-b2bc-434c-b4b7-e852f4f63414>2015-01-29 15:52:09 +0000
commit1796b9b9df07c0cc472e345e28680f43fb81822b (patch)
tree56066088c5eb41a22cb3f1ba48434114d9fd2e51
parentbb25ec3904bfc5063555a418b3c6d36d162d764b (diff)
Started writing script for testing prepare_disks.py
git-svn-id: https://svn.lusy.fri.uni-lj.si/kpov-public-svn/kpov-public@251 5cf9fbd1-b2bc-434c-b4b7-e852f4f63414
-rwxr-xr-xkpov_judge/create_disk_images.py126
-rwxr-xr-xkpov_judge/test_prepare_disks.py148
2 files changed, 148 insertions, 126 deletions
diff --git a/kpov_judge/create_disk_images.py b/kpov_judge/create_disk_images.py
index 1f5f643..6fffb77 100755
--- a/kpov_judge/create_disk_images.py
+++ b/kpov_judge/create_disk_images.py
@@ -14,132 +14,6 @@ import fcntl
import pyssh
import paramiko
-class SSHGuestFs():
- def __init__(hostname, path, 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))
- 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):
- f.write(data)
- 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):
- f.write(data)
- def du(self, path)
- path = os.path.join(self.mountdir, 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)
- pass
- def file(self, path)
- path = os.path.join(self.mountdir, path)
- stdin, stdout, stderr = self.conn.exec_command('file "{}"'.format(path))
- return stdin.read()
- def getxattrs(self, path)
- pass
- #path = os.path.join(self.mountdir, 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 setxattr (self, xattr, val, vallen, path):
- pass
- def write (self, path, content):
- """This call creates a file called "path". The content of
- the file is the string "content" (which can contain any
- 8 bit data).
-
- 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)
- f.close()
- return r
-
- def write_append (self, path, content):
- """This call appends "content" to the end of file "path".
- If "path" does not exist, then a new file is created.
-
- 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)
- f.close()
-
def get_prepare_disks(db, task_id):
prepare_disks_source = db.prepare_disks.find_one({'task_id':task_id})['source']
prepare_disks_code = compile(prepare_disks_source, 'prepare_disks.py', 'exec')
diff --git a/kpov_judge/test_prepare_disks.py b/kpov_judge/test_prepare_disks.py
new file mode 100755
index 0000000..a8bd539
--- /dev/null
+++ b/kpov_judge/test_prepare_disks.py
@@ -0,0 +1,148 @@
+#!/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
+
+class SSHGuestFs():
+ def __init__(hostname, path, 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))
+ 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):
+ f.write(data)
+ 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):
+ f.write(data)
+ def du(self, path)
+ path = os.path.join(self.mountdir, 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)
+ pass
+ def file(self, path)
+ path = os.path.join(self.mountdir, path)
+ stdin, stdout, stderr = self.conn.exec_command('file "{}"'.format(path))
+ return stdin.read()
+ def getxattrs(self, path)
+ pass
+ #path = os.path.join(self.mountdir, 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 setxattr (self, xattr, val, vallen, path):
+ pass
+ def write (self, path, content):
+ """This call creates a file called "path". The content of
+ the file is the string "content" (which can contain any
+ 8 bit data).
+
+ 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)
+ f.close()
+ return r
+
+ def write_append (self, path, content):
+ """This call appends "content" to the end of file "path".
+ If "path" does not exist, then a new file is created.
+
+ 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)
+ f.close()
+
+
+if __name__ == '__main__':
+ if len(sys.argv) != 2:
+ print "Usage: {0} task_file"
+ print "Run prepare_disks on running computers over ssh"
+