3 # pylint: disable=c0111
6 update local configuration files from PLC
12 from hashlib import sha1 as sha
18 from config import Config
24 def __init__(self, noscripts=False):
25 self.config = Config()
26 self.noscripts = noscripts
29 def checksum(self, path):
31 with open(path) as feed:
32 return sha(feed.read().encode()).digest()
36 def system(self, cmd):
37 if not self.noscripts and cmd:
38 logger.verbose('conf_files: running command %s' % cmd)
39 return tools.fork_as(None, os.system, cmd)
42 def update_conf_file(self, cf_rec):
43 if not cf_rec['enabled']:
46 err_cmd = cf_rec['error_cmd']
47 mode = int(cf_rec['file_permissions'], base=8)
49 uid = pwd.getpwnam(cf_rec['file_owner'])[2]
51 logger.log('conf_files: cannot find user %s -- %s not updated'
52 %(cf_rec['file_owner'], dest))
55 gid = grp.getgrnam(cf_rec['file_group'])[2]
57 logger.log('conf_files: cannot find group %s -- %s not updated'
58 %(cf_rec['file_group'], dest))
60 url = 'https://%s/%s' % (self.config.PLC_BOOT_HOST, cf_rec['source'])
61 # set node_id at the end of the request - hacky
67 url += "node_id=%d"%tools.node_id()
69 logger.log('conf_files: %s -- WARNING, cannot add node_id to request'
72 logger.verbose("conf_files: retrieving URL=%s"%url)
73 contents = curlwrapper.retrieve(url, self.config.cacert)
74 except xmlrpc.client.ProtocolError as e:
75 logger.log('conf_files: failed to retrieve %s from %s, skipping' % (dest, url))
77 if not cf_rec['always_update'] and sha(contents).digest() == self.checksum(dest):
79 if self.system(cf_rec['preinstall_cmd']):
81 if not cf_rec['ignore_cmd_errors']:
83 logger.log('conf_files: installing file %s from %s' % (dest, url))
85 os.makedirs(os.path.dirname(dest))
88 tools.write_file(dest, lambda f: f.write(contents.decode()),
89 mode=mode, uidgid=(uid, gid))
90 if self.system(cf_rec['postinstall_cmd']):
93 def run_once(self, data):
94 if "conf_files" in data:
95 for file in data['conf_files']:
97 self.update_conf_file(file)
99 logger.log_exc("conf_files: failed to update conf_file")
101 logger.log_missing_data("conf_files.run_once", 'conf_files')
108 def GetSlivers(data, config=None, plc=None):
109 logger.log("conf_files: Running.")
110 instance = conf_files()
111 instance.run_once(data)
112 logger.log("conf_files: Done.")
116 from argparse import ArgumentParser
117 parser = ArgumentParser()
118 parser.add_argument('-f', '--config', action='store', dest='config',
119 default='/etc/planetlab/plc_config',
120 help='PLC configuration file')
121 parser.add_argument('-k', '--session', action='store', dest='session',
122 default='/etc/planetlab/session',
123 help='API session key (or file)')
124 parser.add_argument('--noscripts', action='store_true', dest='noscripts',
126 help='Do not run pre- or post-install scripts')
127 args = parser.parse_args()
129 # Load /etc/planetlab/plc_config
130 config = Config(args.config)
132 # Load /etc/planetlab/session
133 if os.path.exists(args.session):
134 with open(args.session) as feed:
135 session = feed.read().strip()
137 session = args.session
139 # Initialize XML-RPC client
140 from plcapi import PLCAPI
141 plc = PLCAPI(config.plc_api_uri, config.cacert, auth=session)
142 data = plc.GetSlivers()
144 instance = conf_files(args.noscripts)
145 instance.run_once(data)
148 if __name__ == '__main__':