- GetSlivers() now returns a single struct
[nodemanager.git] / conf_files.py
1 """configuration files"""
2
3 import grp
4 import os
5 import pwd
6 import sha
7 import string
8 import threading
9
10 import curlwrapper
11 import logger
12 import tools
13
14
15 class conf_files:
16     def __init__(self, config):
17         self.config = config
18         self.cond = threading.Condition()
19         self.data = None
20
21     def checksum(self, path):
22         try:
23             f = open(path)
24             try: return sha.new(f.read()).digest()
25             finally: f.close()
26         except IOError: return None
27
28     def system(self, cmd):
29         if cmd:
30             logger.log('conf_files: running command %s' % cmd)
31             return os.system(cmd)
32         else: return 0
33
34     def update_conf_file(self, cf_rec):
35         if not cf_rec['enabled']: return
36         dest = cf_rec['dest']
37         # XXX Remove once old Node Manager is out of service
38         if dest == '/etc/proper/propd.conf': return
39         err_cmd = cf_rec['error_cmd']
40         mode = string.atoi(cf_rec['file_permissions'], base=8)
41         uid = pwd.getpwnam(cf_rec['file_owner'])[2]
42         gid = grp.getgrnam(cf_rec['file_group'])[2]
43         url = 'https://%s/%s' % (self.config.PLC_BOOT_HOST, cf_rec['source'])
44         contents = curlwrapper.retrieve(url, self.config.cacert)
45         if not cf_rec['always_update'] and sha.new(contents).digest() == self.checksum(dest):
46             return
47         if self.system(cf_rec['preinstall_cmd']):
48             self.system(err_cmd)
49             if not cf_rec['ignore_cmd_errors']: return
50         logger.log('conf_files: installing file %s from %s' % (dest, url))
51         try: os.makedirs(os.path.dirname(dest))
52         except OSError: pass
53         tools.write_file(dest, lambda f: f.write(contents), mode=mode, uidgid=(uid,gid))
54         if self.system(cf_rec['postinstall_cmd']): self.system(err_cmd)
55
56     def run_once(self, data):
57         for f in data['conf_files']:
58             try: self.update_conf_file(f)
59             except: logger.log_exc()
60
61     def run(self):
62         while True:
63             self.cond.acquire()
64             while self.data == None: self.cond.wait()
65             data = self.data
66             self.data = None
67             self.cond.release()
68             self.run_once(data)
69
70     def callback(self, data):
71         if data != None:
72             self.cond.acquire()
73             self.data = data
74             self.cond.notify()
75             self.cond.release()
76
77 main = None
78
79 def start(options, config):
80     global main
81     main = conf_files(config)
82     tools.as_daemon_thread(main.run)
83
84 def GetSlivers(data):
85     global main
86     assert main is not None
87     return main.callback(data)
88
89 if __name__ == '__main__':
90     import optparse
91     parser = optparse.OptionParser()
92     parser.add_option('-f', '--config', action='store', dest='config', default='/etc/planetlab/plc_config', help='PLC configuration file')
93     parser.add_option('-k', '--session', action='store', dest='session', default='/etc/planetlab/session', help='API session key (or file)')
94     (options, args) = parser.parse_args()
95
96     # Load /etc/planetlab/plc_config
97     from config import Config
98     config = Config(options.config)
99
100     # Load /etc/planetlab/session
101     if os.path.exists(options.session):
102         session = file(options.session).read().strip()
103     else:
104         session = options.session
105
106     # Initialize XML-RPC client
107     from plcapi import PLCAPI
108     plc = PLCAPI(config.plc_api_uri, config.cacert, auth = session)
109
110     main = conf_files(config)
111     data = plc.GetSlivers()
112     main.run_once(data)