- be less verbose
[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         err_cmd = cf_rec['error_cmd']
38         mode = string.atoi(cf_rec['file_permissions'], base=8)
39         uid = pwd.getpwnam(cf_rec['file_owner'])[2]
40         gid = grp.getgrnam(cf_rec['file_group'])[2]
41         url = 'https://%s/%s' % (self.config.PLC_BOOT_HOST, cf_rec['source'])
42         contents = curlwrapper.retrieve(url, self.config.cacert)
43         if not cf_rec['always_update'] and sha.new(contents).digest() == self.checksum(dest):
44             return
45         if self.system(cf_rec['preinstall_cmd']):
46             self.system(err_cmd)
47             if not cf_rec['ignore_cmd_errors']: return
48         logger.log('conf_files: installing file %s from %s' % (dest, url))
49         try: os.makedirs(os.path.dirname(dest))
50         except OSError: pass
51         tools.write_file(dest, lambda f: f.write(contents), mode=mode, uidgid=(uid,gid))
52         if self.system(cf_rec['postinstall_cmd']): self.system(err_cmd)
53
54     def run_once(self, data):
55         for d in data:
56             for f in d['conf_files']:
57                 try: self.update_conf_file(f)
58                 except: logger.log_exc()
59
60     def run(self):
61         while True:
62             self.cond.acquire()
63             while self.data == None: self.cond.wait()
64             data = self.data
65             self.data = None
66             self.cond.release()
67             self.run_once(data)
68
69     def callback(self, data):
70         if data != None:
71             self.cond.acquire()
72             self.data = data
73             self.cond.notify()
74             self.cond.release()
75
76 main = None
77
78 def start(options, config):
79     global main
80     main = conf_files(config)
81     tools.as_daemon_thread(main.run)
82
83 def GetSlivers(data):
84     global main
85     assert main is not None
86     return main.callback(data)
87
88 if __name__ == '__main__':
89     import optparse
90     parser = optparse.OptionParser()
91     parser.add_option('-f', '--config', action='store', dest='config', default='/etc/planetlab/plc_config', help='PLC configuration file')
92     parser.add_option('-k', '--session', action='store', dest='session', default='/etc/planetlab/session', help='API session key (or file)')
93     (options, args) = parser.parse_args()
94
95     # Load /etc/planetlab/plc_config
96     from config import Config
97     config = Config(options.config)
98
99     # Load /etc/planetlab/session
100     if os.path.exists(options.session):
101         session = file(options.session).read().strip()
102     else:
103         session = options.session
104
105     # Initialize XML-RPC client
106     from plcapi import PLCAPI
107     plc = PLCAPI(config.plc_api_uri, config.cacert, auth = session)
108
109     main = conf_files(config)
110     data = plc.GetSlivers()
111     main.run_once(data)