lxc-test.py
3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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)