def upload_packages(): config = BibOSConfig() data = config.get_data() admin_url = data['admin_url'] xml_rpc_url = data.get('xml_rpc_url', '/admin-xml/') uid = data['uid'] admin = BibOSAdmin(urlparse.urljoin(admin_url, xml_rpc_url)) # TODO: Make option to turn off/avoid repeating this. os.system('get_package_data /tmp/packages.csv') upgrade_pkgs = set(get_upgrade_packages()) with open('/tmp/packages.csv') as f: package_reader = csv.reader(f, delimiter=';') package_data = [{ 'name': n, 'status': 'needs upgrade' if n in upgrade_pkgs else s, 'version': v, 'description': d } for (n, s, v, d) in package_reader] try: admin.send_status_info(uid, package_data, None) except Exception as e: print >> sys.stderr, 'Error:', str(e) sys.exit(1)
def upload_dist_packages(): config = BibOSConfig() data = config.get_data() admin_url = data['admin_url'] xml_rpc_url = data.get('xml_rpc_url', '/admin-xml/') distribution = data['distribution'] admin = BibOSAdmin(urlparse.urljoin(admin_url, xml_rpc_url)) # TODO: Make option to turn off/avoid repeating this. os.system('get_package_data /tmp/packages.csv') with open('/tmp/packages.csv') as f: package_reader = csv.reader(f, delimiter=';') package_data = [{ 'name': n, 'status': s, 'version': v, 'description': d } for (n, s, v, d) in package_reader] try: admin.upload_dist_packages(distribution, package_data) except Exception as e: print >> sys.stderr, 'Error:', str(e) sys.exit(1)
def translate_parameters(self): if 'parameters' not in self: return config = BibOSConfig() admin_url = config.get_value('admin_url') local_params = [] self['local_parameters'] = local_params params = self['parameters'] del self['parameters'] for param in params: if param['type'] == 'FILE': # Make sure we have the directory if not os.path.isdir(self.attachments_path): os.mkdir(self.attachments_path) value = param['value'] basename = value[value.rindex('/') + 1:] filename = self.attachments_path + '/' + basename # TODO this is probably not the right URL full_url = urlparse.urljoin(admin_url, value) remote_file = urllib2.urlopen(full_url) attachment_fh = open(filename, 'w') attachment_fh.write(remote_file.read()) attachment_fh.close() local_params.append(filename) else: local_params.append(param['value'])
def translate_parameters(self): if not 'parameters' in self: return config = BibOSConfig() admin_url = config.get_value('admin_url') local_params = [] self['local_parameters'] = local_params params = self['parameters'] del self['parameters'] for param in params: if param['type'] == 'FILE': # Make sure we have the directory if not os.path.isdir(self.attachments_path): os.mkdir(self.attachments_path) value = param['value'] basename = value[value.rindex('/') + 1:] filename = self.attachments_path + '/' + basename # TODO this is probably not the right URL full_url = urlparse.urljoin(admin_url, value) remote_file = urllib2.urlopen(full_url) attachment_fh = open(filename, 'w') attachment_fh.write(remote_file.read()) attachment_fh.close() local_params.append(filename) else: local_params.append(param['value'])
def upload_packages(): config = BibOSConfig() data = config.get_data() admin_url = data['admin_url'] xml_rpc_url = data.get('xml_rpc_url', '/admin-xml/') uid = data['uid'] admin = BibOSAdmin(urlparse.urljoin(admin_url, xml_rpc_url)) # TODO: Make option to turn off/avoid repeating this. os.system('get_package_data /tmp/packages.csv') upgrade_pkgs = set(get_upgrade_packages()) with open('/tmp/packages.csv') as f: package_reader = csv.reader(f, delimiter=';') package_data = [ { 'name': n, 'status': 'needs upgrade' if n in upgrade_pkgs else s, 'version': v, 'description': d } for (n, s, v, d) in package_reader ] try: admin.send_status_info(uid, package_data, None) except Exception as e: print >> sys.stderr, 'Error:', str(e) sys.exit(1)
def get_url_and_uid(): config = BibOSConfig() uid = config.get_value('uid') config_data = config.get_data() admin_url = config_data.get('admin_url', 'http://bibos.magenta-aps.dk/') xml_rpc_url = config_data.get('xml_rpc_url', '/admin-xml/') rpc_url = urlparse.urljoin(admin_url, xml_rpc_url) return(rpc_url, uid)
def get_instructions(): (remote_url, uid) = get_url_and_uid() remote = BibOSAdmin(remote_url) tmpfilename, updated_pkgs, removed_pkgs = get_local_package_diffs() try: instructions = remote.get_instructions(uid, { 'updated_packages': updated_pkgs, 'removed_packages': removed_pkgs }) # Everything went well, overwrite old package list if tmpfilename: subprocess.call(['mv', tmpfilename, PACKAGE_LIST_FILE]) except Exception as e: print >>os.sys.stderr, "Error while getting instructions:" + str(e) if tmpfilename: subprocess.call(['rm', tmpfilename]) return False if 'configuration' in instructions: # Update configuration bibos_config = BibOSConfig() local_config = {} for key, value in bibos_config.get_data().items(): # We only care about string values if isinstance(value, basestring): local_config[key] = value for key, value in instructions['configuration'].items(): bibos_config.set_value(key, value) if key in local_config: del local_config[key] # Anything left in local_config needs to be removed for key in local_config.keys(): bibos_config.remove_key(key) bibos_config.save() # Import jobs if 'jobs' in instructions: for j in instructions['jobs']: local_job = LocalJob(data=j) local_job.save() local_job.logline("Job imported at %s" % datetime.datetime.now()) if ('do_send_package_info' in instructions and instructions['do_send_package_info']): try: # Send full package info to server. upload_packages() except Exception as e: print >>os.sys.stderr, "Package upload failed" + str(e)
def upload_dist_packages(): config = BibOSConfig() data = config.get_data() admin_url = data['admin_url'] xml_rpc_url = data.get('xml_rpc_url', '/admin-xml/') distribution = data['distribution'] admin = BibOSAdmin(urlparse.urljoin(admin_url, xml_rpc_url)) # TODO: Make option to turn off/avoid repeating this. os.system('get_package_data /tmp/packages.csv') with open('/tmp/packages.csv') as f: package_reader = csv.reader(f, delimiter=';') package_data = [ {'name': n, 'status': s, 'version': v, 'description': d} for (n, s, v, d) in package_reader] try: admin.upload_dist_packages(distribution, package_data) except Exception as e: print >> sys.stderr, 'Error:', str(e) sys.exit(1)
def get_instructions(): (remote_url, uid) = get_url_and_uid() remote = BibOSAdmin(remote_url) tmpfilename, updated_pkgs, removed_pkgs = get_local_package_diffs() try: instructions = remote.get_instructions(uid, { 'updated_packages': updated_pkgs, 'removed_packages': removed_pkgs }) # Everything went well, overwrite old package list if tmpfilename: subprocess.call(['mv', tmpfilename, PACKAGE_LIST_FILE]) except Exception as e: print >> os.sys.stderr, "Error while getting instructions:" + str(e) if tmpfilename: subprocess.call(['rm', tmpfilename]) # No instructions likely = no network. Do not continue. raise if 'configuration' in instructions: # Update configuration bibos_config = BibOSConfig() local_config = {} for key, value in bibos_config.get_data().items(): # We only care about string values if isinstance(value, basestring): local_config[key] = value for key, value in instructions['configuration'].items(): bibos_config.set_value(key, value) if key in local_config: del local_config[key] # Anything left in local_config needs to be removed for key in local_config.keys(): bibos_config.remove_key(key) bibos_config.save() # Import jobs if 'jobs' in instructions: for j in instructions['jobs']: local_job = LocalJob(data=j) local_job.save() local_job.logline("Job imported at %s" % datetime.now()) # if security dir exists if os.path.isdir(SECURITY_DIR): # Always remove old security scripts. # Could be pc is moved to another group, # and does not need security scripts any more. os.popen('rm -f ' + SECURITY_DIR + '/s_*') # Import security scripts if 'security_scripts' in instructions: for s in instructions['security_scripts']: fpath = SECURITY_DIR + '/s_' + str(s['name']).replace(' ', '') fh = open(fpath, 'w') fh.write(s['executable_code'].encode("utf8")) fh.close() os.chmod(fpath, stat.S_IRWXU) if ('do_send_package_info' in instructions and instructions['do_send_package_info']): try: # Send full package info to server. upload_packages() except Exception as e: print >> os.sys.stderr, "Package upload failed" + str(e)
def get_default_admin(verbose=False): conf_data = BibOSConfig().get_data() admin_url = conf_data.get('admin_url', 'http://bibos.magenta-aps.dk') xml_rpc_url = conf_data.get('xml_rpc_url', '/admin-xml/') return BibOSAdmin(''.join([admin_url, xml_rpc_url]), verbose=verbose)
def get_proxy_setup(self, pc_uid): return self._rpc_srv.get_proxy_setup(pc_uid) def push_config_keys(self, pc_uid, config_dict): return self._rpc_srv.push_config_keys(pc_uid, config_dict) if __name__ == '__main__': """Simple test suite.""" import netifaces from bibos_utils.bibos_config import BibOSConfig admin_url = 'http://localhost:8080/admin-xml/' bibos_config_file = '/etc/bibos/bibos.conf' bibos_config = BibOSConfig(bibos_config_file) admin = BibOSAdmin(admin_url) # Find HW address to use as UID try: addrs = netifaces.ifaddresses('eth0') mac = netifaces.ifaddresses('eth0')[netifaces.AF_LINK][0]['addr'] uid = mac except: # Don't use mac address, generate random number instead uid = 'pop' print admin.register_new_computer('pip', uid, 'BIBOS', 'AAKB', bibos_config.get_data()) # Find list of all packages for status.
def push_config_keys(self, pc_uid, config_dict): return self._rpc_srv.push_config_keys(pc_uid, config_dict) def push_security_events(self, pc_uid, csv_data): return self._rpc_srv.push_security_events(pc_uid, csv_data) if __name__ == '__main__': """Simple test suite.""" import netifaces from bibos_utils.bibos_config import BibOSConfig admin_url = 'http://localhost:8080/admin-xml/' bibos_config_file = '/etc/bibos/bibos.conf' bibos_config = BibOSConfig(bibos_config_file) admin = BibOSAdmin(admin_url) # Find HW address to use as UID try: addrs = netifaces.ifaddresses('eth0') mac = netifaces.ifaddresses('eth0')[netifaces.AF_LINK][0]['addr'] uid = mac except: # Don't use mac address, generate random number instead uid = 'pop' print admin.register_new_computer('pip', uid, 'BIBOS', 'AAKB', bibos_config.get_data()) # Find list of all packages for status.