minor small fixes (undefined parameters and empty stderr file)
[slapos.git] / slapos / recipe / erp5testnode / SlapOSControler.py
1 import slapos.slap, subprocess, os, time
2 from xml_marshaller import xml_marshaller
3
4 class SlapOSControler(object):
5
6 def __init__(self, config, process_group_pid_set=None):
7 self.config = config
8 # By erasing everything, we make sure that we are able to "update"
9 # existing profiles. This is quite dirty way to do updates...
10 if os.path.exists(config['proxy_database']):
11 os.unlink(config['proxy_database'])
12 proxy = subprocess.Popen([config['slapproxy_binary'],
13 config['slapos_config']], close_fds=True, preexec_fn=os.setsid)
14 process_group_pid_set.add(proxy.pid)
15 # XXX: dirty, giving some time for proxy to being able to accept
16 # connections
17 time.sleep(10)
18 slap = slapos.slap.slap()
19 slap.initializeConnection(config['master_url'])
20 # register software profile
21 self.software_profile = config['custom_profile_path']
22 slap.registerSupply().supply(
23 self.software_profile,
24 computer_guid=config['computer_id'])
25 computer = slap.registerComputer(config['computer_id'])
26 # create partition and configure computer
27 partition_reference = config['partition_reference']
28 partition_path = os.path.join(config['instance_root'], partition_reference)
29 if not os.path.exists(partition_path):
30 os.mkdir(partition_path)
31 os.chmod(partition_path, 0750)
32 computer.updateConfiguration(xml_marshaller.dumps({
33 'address': config['ipv4_address'],
34 'instance_root': config['instance_root'],
35 'netmask': '255.255.255.255',
36 'partition_list': [{'address_list': [{'addr': config['ipv4_address'],
37 'netmask': '255.255.255.255'},
38 {'addr': config['ipv6_address'],
39 'netmask': 'ffff:ffff:ffff::'},
40 ],
41 'path': partition_path,
42 'reference': partition_reference,
43 'tap': {'name': partition_reference},
44 }
45 ],
46 'reference': config['computer_id'],
47 'software_root': config['software_root']}))
48
49 def runSoftwareRelease(self, config, environment, process_group_pid_set=None,
50 stdout=None, stderr=None):
51 print "SlapOSControler.runSoftwareRelease"
52 cpu_count = os.sysconf("SC_NPROCESSORS_ONLN")
53 os.putenv('MAKEFLAGS', '-j%s' % cpu_count)
54 os.environ['PATH'] = environment['PATH']
55 slapgrid = subprocess.Popen([config['slapgrid_software_binary'], '-v', '-c',
56 #'--buildout-parameter',"'-U -N' -o",
57 config['slapos_config']],
58 stdout=stdout, stderr=stderr,
59 close_fds=True, preexec_fn=os.setsid)
60 process_group_pid_set.add(slapgrid.pid)
61 slapgrid.wait()
62 stdout.seek(0)
63 stderr.seek(0)
64 process_group_pid_set.remove(slapgrid.pid)
65 status_dict = {'status_code':slapgrid.returncode,
66 'stdout':stdout.read(),
67 'stderr':stderr.read()}
68 stdout.close()
69 stderr.close()
70 return status_dict
71
72 def runComputerPartition(self, config, environment,
73 process_group_pid_set=None,
74 stdout=None, stderr=None):
75 print "SlapOSControler.runSoftwareRelease"
76 slap = slapos.slap.slap()
77 slap.registerOpenOrder().request(self.software_profile,
78 partition_reference='testing partition',
79 partition_parameter_kw=config['instance_dict'])
80 slapgrid = subprocess.Popen([config['slapgrid_partition_binary'],
81 config['slapos_config'], '-c', '-v'],
82 stdout=stdout, stderr=stderr,
83 close_fds=True, preexec_fn=os.setsid)
84 process_group_pid_set.add(slapgrid.pid)
85 slapgrid.wait()
86 stdout.seek(0)
87 stderr.seek(0)
88 process_group_pid_set.remove(slapgrid.pid)
89 status_dict = {'status_code':slapgrid.returncode,
90 'stdout':stdout.read(),
91 'stderr':stderr.read()}
92 stdout.close()
93 stderr.close()
94 return status_dict