lxc-test.py 3.95 KB
import lxc
import os
import sys
import hashlib
import json
import getpass
import subprocess

scriptpath = os.path.dirname(os.path.realpath(__file__))

def lxc_start(cont):
    if (os.getuid() != 0):
        uname = getpass.getuser()
        subprocess.call([scriptpath + '/create_user_cgroup']);
        for i in os.listdir('/sys/fs/cgroup'):
            if (i == 'openrc'):
                continue
            with open('/sys/fs/cgroup/'+i+'/'+uname+'/tasks', 'a') as f:
                f.write(str(os.getpid())+'\n');

    cont.start();

def basic(cont):
    def work(data):
        print data
        sys.stdout.flush()
        sys.exit(0)
    
    lxc_read, lxc_write = os.pipe()
    
    cont.attach_wait(work, 'testtest', stdout=lxc_write)
    os.close(lxc_write)
    
    lxc_readfd = os.fdopen(lxc_read)
    data = lxc_readfd.readline().rstrip('\n')
    
    lxc_readfd.close()
    
    print data

def basic2(cont):
    def work(data):
        print data
        sys.stdout.flush()
        sys.exit(0)
    
    lxc_read, lxc_write = os.pipe()
    
    cont.attach(work, 'testtest', stdout=lxc_write)
    os.close(lxc_write)
    
    lxc_readfd = os.fdopen(lxc_read)
    data = lxc_readfd.readline().rstrip('\n')
    
    lxc_readfd.close()
    
    print data

def copy(cont, src, dest):
    def work(dest):
        infno = sys.stdin.fileno()
        data = os.read(infno, 100)
        print(data)

    lxc_read, lxc_write = os.pipe()

    pid = cont.attach(work, 'foo', stdin=lxc_read)
    os.close(lxc_read)

    os.write(lxc_write, 'fooooobadooooo')

    os.close(lxc_write)
    os.waitpid(pid, 0)

def copy2(cont, src, dest):
    bufsize = 2048

    def work(dest):
        infno = sys.stdin.fileno()
        with open(dest, 'w') as outfile:
            data = os.read(infno, bufsize)
            while (bufsize == len(data)):
                outfile.write(data)
                data = os.read(infno, bufsize)
            outfile.write(data)

    lxc_read, lxc_write = os.pipe()

    pid = cont.attach(work, dest, stdin=lxc_read)
    os.close(lxc_read)

    with open(src, 'r') as infile:
        data = infile.read(bufsize)
        while (bufsize == len(data)):
            os.write(lxc_write, data)
            data = infile.read(bufsize)
        os.write(lxc_write, data)

    os.close(lxc_write)
    os.waitpid(pid, 0)

def verify(cont, src, dest):
    bufsize = 2048

    def work(dest):
        digest = ''
        try:
            chk = hashlib.sha1()
            with open(dest, 'r') as dfile:
                data = dfile.read(bufsize)
                while(bufsize == len(data)):
                    chk.update(data)
                    data = dfile.read(bufsize)
                chk.update(data)
                digest = chk.hexdigest()
        except:
            pass
        sys.stdout.write(digest)
        sys.stdout.flush()

    lxc_read, lxc_write = os.pipe()

    pid = cont.attach(work, dest, stdout=lxc_write)
    os.close(lxc_write)

    chk = hashlib.sha1()
    with open(src, 'r') as dfile:
        data = dfile.read(bufsize)
        while (bufsize == len(data)):
            chk.update(data)
            data = dfile.read(bufsize)
        chk.update(data)

    lxc_chk = os.read(lxc_read, 100)
    os.close(lxc_read)

    return json.dumps({
        'equal': lxc_chk == chk.hexdigest(),
        'local_sum': chk.hexdigest(),
        'container_sum': lxc_chk})

cont = lxc.Container('ansible_test')

assert cont.defined

started = False

if not cont.running:
    lxc_start(cont)
    if not cont.wait('RUNNING', timeout=5):
        raise EnvironmentError(0x01, '[lxc] unable to start container', cont)
    started = True

#basic(cont)
#basic2(cont)
#copy(cont, 'verkauf.jpg', '/tmp/foo')

print __file__
print os.path.realpath(__file__)
print os.path.dirname(os.path.realpath(__file__))

vres = json.loads(verify(cont, 'verkauf.jpg', '/tmp/foo'))
if not vres['equal']:
    print "copy file"
    copy2(cont, 'verkauf.jpg', '/tmp/foo')
else:
    print "files are equal"

if started:
    cont.shutdown(5)