def GetSlivers(data, config=None, plc=None): """For each sliver with the vsys attribute, set the script ACL, create the vsys directory in the slice, and restart vsys.""" if 'slivers' not in data: logger.log_missing_data("vsys.GetSlivers",'slivers') return # Touch ACLs and create dict of available scripts = {} for script in touchAcls(): scripts[script] = [] # slices that need to be written to the conf slices = [] _restart = False # Parse attributes and update dict of scripts if 'slivers' not in data: logger.log_missing_data("vsys.GetSlivers",'slivers') return for sliver in data['slivers']: for attribute in sliver['attributes']: if attribute['tagname'] == 'vsys': if sliver['name'] not in slices: # add to conf slices.append(sliver['name']) _restart = createVsysDir(sliver['name']) or _restart if attribute['value'] in scripts.keys(): scripts[attribute['value']].append(sliver['name']) # Write the conf _restart = writeConf(slices, parseConf()) or _restart # Write out the ACLs if writeAcls(scripts, parseAcls()) or _restart: logger.log("vsys: restarting vsys service") logger.log_call(["/etc/init.d/vsys", "restart", ])
def GetSlivers (self, data, conf=None, plc=None): # check we're using a compliant GetSlivers if 'reservation_policy' not in data: logger.log_missing_data("reservation.GetSlivers",'reservation_policy') return self.reservation_policy=data['reservation_policy'] if 'leases' not in data: logger.log_missing_data("reservation.GetSlivers",'leases') return # store data locally # since we've asked for persistent_data, we should not get an empty data here if data: self.data = data # regular nodes are not affected if self.reservation_policy == 'none': return elif self.reservation_policy not in ['lease_or_idle','lease_or_shared']: logger.log("reservation: ignoring -- unexpected value for reservation_policy %r"%self.reservation_policy) return # at this point we have reservation_policy in ['lease_or_idle','lease_or_shared'] # we make no difference for now logger.log("reservation.GetSlivers: reservable node -- policy=%s"%self.reservation_policy) self.sync_timers_from_leases() logger.log("reservation.GetSlivers: listing timers") if reservation.debug: self.list_timers()
def GetSlivers(data, config, plc): # added by caglar # band-aid for short period as old API returns networks instead of interfaces global KEY_NAME KEY_NAME = "interfaces" ################# logger.verbose("net: GetSlivers called.") if not 'interfaces' in data: # added by caglar # band-aid for short period as old API returns networks instead of interfaces # logger.log_missing_data('net.GetSlivers','interfaces') # return if not 'networks' in data: logger.log_missing_data('net.GetSlivers','interfaces') return else: KEY_NAME = "networks" ################## plnet.InitInterfaces(logger, plc, data) if 'OVERRIDES' in dir(config): if config.OVERRIDES.get('net_max_rate') == '-1': logger.log("net: Slice and node BW Limits disabled.") if len(bwlimit.tc("class show dev %s" % dev_default)): logger.verbose("net: *** DISABLING NODE BW LIMITS ***") bwlimit.stop() else: InitNodeLimit(data) InitI2(plc, data) else: InitNodeLimit(data) InitI2(plc, data) InitNAT(plc, data)
def GetSlivers(data, config, plc): if 'OVERRIDES' in dir(config): if config.OVERRIDES.get('sliverauth') == '-1': logger.log("sliverauth: Disabled", 2) return if 'slivers' not in data: logger.log_missing_data("sliverauth.GetSlivers", 'slivers') return for sliver in data['slivers']: path = '/vservers/%s' % sliver['name'] if not os.path.exists(path): # ignore all non-plc-instantiated slivers instantiation = sliver.get('instantiation','') if instantiation == 'plc-instantiated': logger.log("sliverauth: plc-instantiated slice %s does not yet exist111. IGNORING!" % sliver['name']) continue system_slice = False for chunk in sliver['attributes']: if chunk['tagname'] == "system": if chunk['value'] in (True, 1, '1') or chunk['value'].lower() == "true": system_slice = True logger.log("sliverauth: system_slice is %s " % system_slice) for chunk in sliver['attributes']: if chunk['tagname']=='enable_hmac' and not system_slice: logger.log("sliverauth: slice %s is hmac" % sliver['name']) manage_hmac (plc, sliver) if chunk['tagname']=='omf_control': logger.log("sliverauth: slice %s is omf_cotrol" % sliver['name']) manage_sshkey (plc, sliver)
def GetSlivers(data, config=None, plc=None): if 'slivers' not in data: logger.log_missing_data("vsys_privs.GetSlivers",'slivers') return privs = {} # Parse attributes and update dict of scripts if 'slivers' not in data: logger.log_missing_data("vsys_privs.GetSlivers",'slivers') return for sliver in data['slivers']: slice = sliver['name'] for attribute in sliver['attributes']: tag = attribute['tagname'] value = attribute['value'] if tag.startswith('vsys_'): if (privs.has_key(slice)): slice_priv = privs[slice] if (slice_priv.has_key(tag)): slice_priv[tag].append(value) else: slice_priv[tag]=[value] privs[slice] = slice_priv else: privs[slice] = {tag:[value]} cur_privs = read_privs() write_privs(cur_privs, privs)
def GetSlivers(data, config=None, plc=None): if 'slivers' not in data: logger.log_missing_data("vsys_privs.GetSlivers", 'slivers') return privs = {} # Parse attributes and update dict of scripts if 'slivers' not in data: logger.log_missing_data("vsys_privs.GetSlivers", 'slivers') return for sliver in data['slivers']: slice = sliver['name'] for attribute in sliver['attributes']: tag = attribute['tagname'] value = attribute['value'] if tag.startswith('vsys_'): if (privs.has_key(slice)): slice_priv = privs[slice] if (slice_priv.has_key(tag)): slice_priv[tag].append(value) else: slice_priv[tag] = [value] privs[slice] = slice_priv else: privs[slice] = {tag: [value]} cur_privs = read_privs() write_privs(cur_privs, privs)
def GetSlivers(data, conf = None, plc = None): node_id = tools.node_id() if 'slivers' not in data: logger.log_missing_data("privatebridge.GetSlivers",'slivers') return valid_bridges = [] for sliver in data['slivers']: sliver_name = sliver['name'] # build a dict of attributes, because it's more convenient attributes={} for attribute in sliver['attributes']: attributes[attribute['tagname']] = attribute['value'] bridge_name = attributes.get('slice_bridge_name',None) if bridge_name: configure_slicebridge(sliver, attributes) valid_bridges.append(bridge_name) # now, delete the bridges that we don't want bridges = ovs_listbridge() for bridge_name in bridges: if not bridge_name.startswith("br-slice-"): # ignore ones we didn't create continue if bridge_name in valid_bridges: # ignore ones we want to keep continue logger.log("privatebridge: deleting unused bridge %s" % bridge_name) ovs_delbridge(bridge_name)
def GetSlivers(data, config=None, plc=None): if 'slivers' not in data: logger.log_missing_data("rawdisk.GetSlivers",'slivers') return devices = get_unused_devices() for sliver in data['slivers']: for attribute in sliver['attributes']: name = attribute.get('tagname',attribute.get('name','')) if name == 'rawdisk': for i in devices: st = os.stat(i) path = "/vservers/%s%s" % (sliver['name'], i) if os.path.exists(path): # should check whether its the proper type of device continue logger.log("rawdisk: Copying %s to %s" % (i, path)) try: if os.path.exists(path): os.unlink(path) except: pass try: os.makedirs(os.path.dirname(path), 0755) except: pass os.mknod(path, st.st_mode, st.st_rdev)
def run_once(self, data): if data.has_key("conf_files"): for f in data['conf_files']: try: self.update_conf_file(f) except: logger.log_exc("conf_files: failed to update conf_file") else: logger.log_missing_data("conf_files.run_once",'conf_files')
def GetSlivers(self, data, conf=None, plc=None): # check we're using a compliant GetSlivers if 'reservation_policy' not in data: logger.log_missing_data("reservation.GetSlivers", 'reservation_policy') return self.reservation_policy = data['reservation_policy'] if 'leases' not in data: logger.log_missing_data("reservation.GetSlivers", 'leases') return # store data locally # since we've asked for persistent_data, we should not get an empty data here if data: self.data = data # regular nodes are not affected if self.reservation_policy == 'none': return elif self.reservation_policy not in [ 'lease_or_idle', 'lease_or_shared' ]: logger.log( "reservation: ignoring -- unexpected value for reservation_policy %r" % self.reservation_policy) return # at this point we have reservation_policy in ['lease_or_idle','lease_or_shared'] # we make no difference for now logger.log("reservation.GetSlivers: reservable node -- policy=%s" % self.reservation_policy) self.sync_timers_from_leases() logger.log("reservation.GetSlivers: listing timers") if reservation.debug: self.list_timers()
def GetSlivers(data, config=None, plc=None): if 'slivers' not in data: logger.log_missing_data("rawdisk.GetSlivers", 'slivers') return devices = get_unused_devices() for sliver in data['slivers']: for attribute in sliver['attributes']: name = attribute.get('tagname', attribute.get('name', '')) if name == 'rawdisk': for i in devices: st = os.stat(i) path = "/vservers/%s%s" % (sliver['name'], i) if os.path.exists(path): # should check whether its the proper type of device continue logger.log("rawdisk: Copying %s to %s" % (i, path)) try: if os.path.exists(path): os.unlink(path) except: pass try: os.makedirs(os.path.dirname(path), 0755) except: pass os.mknod(path, st.st_mode, st.st_rdev)
def GetSlivers(data, config=None, plc=None): if 'slivers' not in data: logger.log_missing_data("hostmap.GetSlivers",'slivers') return if 'hostname' not in data: logger.log_missing_data("hostmap.GetSlivers", 'hostname') hostname = data['hostname'] for sliver in data['slivers']: slicename = sliver['name'] for tag in sliver['attributes']: if tag['tagname'] == 'slice_hostmap': fn = "/vservers/%s/etc/hosts" % slicename if not os.path.exists(fn): continue contents = file(fn,"r").read() hostmap = [] for index, entry in enumerate(tag["value"].split("\n")): parts = entry.split(" ") if len(parts)==2: line = "%s pvt.%s private%d" % (parts[0], parts[1], index) if (index==0): line = line + " headnode" if parts[1] == hostname: line = line + " pvt.self" hostmap.append(line) hostmap = "\n".join(hostmap) hostmap = PREFIX + "\n" + hostmap + "\n" + SUFFIX + "\n" if (hostmap in contents): # it's already there continue # remove anything between PREFIX and SUFFIX from contents pattern = PREFIX + ".*" + SUFFIX + "\n" regex = re.compile(pattern, re.DOTALL) if regex.search(contents) != None: contents = regex.sub(hostmap, contents) else: contents = contents + hostmap try: file(fn, "w").write(contents) except: logger.log_exc("hostmap (%s): failed to write %s" % (slicename, fn))
def GetSlivers(data, conf=None, plc=None): node_id = tools.node_id() if 'slivers' not in data: logger.log_missing_data("syndicate.GetSlivers", 'slivers') return syndicate_sliver = None for sliver in data['slivers']: if sliver['name'] == "princeton_syndicate": syndicate_sliver = sliver if not syndicate_sliver: logger.log( "Syndicate: no princeton_syndicate sliver on this node. aborting.") return syndicate_ip = tools.get_sliver_ip("princeton_syndicate") if not syndicate_ip: logger.log("Syndicate: unable to get syndicate sliver ip. aborting.") return for sliver in data['slivers']: enable_syndicate = False # build a dict of attributes, because it's more convenient attributes = {} for attribute in sliver['attributes']: attributes[attribute['tagname']] = attribute['value'] sliver_name = sliver['name'] syndicate_mountpoint = os.path.join("/vservers", sliver_name, "syndicate") enable_syndicate = attributes.get("enable_syndicate", False) has_syndicate = os.path.exists(syndicate_mountpoint) if enable_syndicate and (not has_syndicate): logger.log("Syndicate: enabling syndicate for %s" % sliver_name) #enable_syndicate_mount(sliver, syndicate_mountpoint, syndicate_ip) t = Thread(target=enable_syndicate_mount, args=(sliver, syndicate_mountpoint, syndicate_ip)) t.start() elif (not enable_syndicate) and (has_syndicate): logger.log("Syndicate: disabling syndicate for %s" % sliver_name) #disable_syndicate_mount(sliver, syndicate_mountpoint, syndicate_ip) t = Thread(target=disable_syndicate_mount, args=(sliver, syndicate_mountpoint, syndicate_ip)) t.start()
def GetSlivers(data, conf = None, plc = None): node_id = tools.node_id() if 'slivers' not in data: logger.log_missing_data("syndicate.GetSlivers",'slivers') return syndicate_sliver = None for sliver in data['slivers']: if sliver['name'] == "princeton_syndicate": syndicate_sliver = sliver if not syndicate_sliver: logger.log("Syndicate: no princeton_syndicate sliver on this node. aborting.") return syndicate_ip = tools.get_sliver_ip("princeton_syndicate") if not syndicate_ip: logger.log("Syndicate: unable to get syndicate sliver ip. aborting.") return for sliver in data['slivers']: enable_syndicate = False # build a dict of attributes, because it's more convenient attributes={} for attribute in sliver['attributes']: attributes[attribute['tagname']] = attribute['value'] sliver_name = sliver['name'] syndicate_mountpoint = os.path.join("/vservers", sliver_name, "syndicate") enable_syndicate = attributes.get("enable_syndicate", False) has_syndicate = os.path.exists(syndicate_mountpoint) if enable_syndicate and (not has_syndicate): logger.log("Syndicate: enabling syndicate for %s" % sliver_name) #enable_syndicate_mount(sliver, syndicate_mountpoint, syndicate_ip) t = Thread(target=enable_syndicate_mount, args=(sliver, syndicate_mountpoint, syndicate_ip)) t.start() elif (not enable_syndicate) and (has_syndicate): logger.log("Syndicate: disabling syndicate for %s" % sliver_name) #disable_syndicate_mount(sliver, syndicate_mountpoint, syndicate_ip) t = Thread(target=disable_syndicate_mount, args=(sliver, syndicate_mountpoint, syndicate_ip)) t.start()
def GetSlivers(data, conf = None, plc = None): DRL_SLICE_NAME = '' HAVE_DRL = 0 node_id = tools.node_id() if 'slivers' not in data: logger.log_missing_data("drl.GetSlivers",'slivers') return for sliver in data['slivers']: for attribute in sliver['attributes']: tag = attribute['tagname'] value = attribute['value'] if tag == 'drl' and value == '1': HAVE_DRL = 1 DRL_SLICE_NAME = sliver['name'] if HAVE_DRL: site_id = plc.GetNodes({'node_id': int(node_id) }, ['site_id']) site_id = site_id[0]['site_id'] q = plc.GetSites({'site_id': site_id, 'enabled': True, 'peer_site_id': None}, ['name', 'node_ids']) for i in q: if i['node_ids'] != [] and len(i['node_ids']) > 1: z = plc.GetInterfaces({'node_id': i['node_ids'], 'is_primary': True, '~bwlimit': None}, ['node_id', 'ip', 'bwlimit']) total_bwlimit = 0 peer = '' node_has_bwlimit = 0 for j in range(len(z)): total_bwlimit += z[j]['bwlimit'] if z[j]['node_id'] != int(node_id): peer += '\t<peer>%s</peer>\n' % z[j]['ip'] else: node_has_bwlimit = 1 if node_has_bwlimit: DRLSetup(i['name'], DRL_SLICE_NAME, site_id, total_bwlimit/1000, peer) else: logger.log('drl: This node has no bwlimit') else: logger.log('drl: This site has only %s node' % len(i['node_ids'])) else: logger.log('drl: This node has no drl slice!...')
def GetSlivers(data, config=None, plc=None): """For each sliver with the vsys attribute, set the script ACL, create the vsys directory in the slice, and restart vsys.""" if 'slivers' not in data: logger.log_missing_data("vsys.GetSlivers", 'slivers') return slices = [] for sliver in data['slivers']: slicename = sliver["name"] for attribute in sliver['attributes']: if attribute['tagname'].startswith('vsys_sysctl.'): dir = "/vservers/%s/vsys_sysctl" % slicename if not os.path.exists(dir): try: logger.log("vsys_sysctl: create dir %s" % dir) os.mkdir(dir) except: logger.log("vsys_sysctl: failed to create dir %s" % dir) (junk, key) = attribute['tagname'].split(".", 1) value = str(attribute['value']) fn = os.path.join(dir, key) if not test_value(fn, value): # All we need to do to make vsys_sysctl work is to lxcsu # into the slice and do anything. result = os.system("lxcsu -r %s :" % slicename) if result != 0: logger.log("vsys_sysctl: failed to lxcsu into %s" % slicename) continue # Store the key name and value inside of /vsys_sysctl in the # slice. This lets us know that we've done the sysctl. try: logger.log("vsys_sysctl: create file %s value %s" % (fn, value)) with open(fn, "w") as f: f.write(value+"\n") except: logger.log("vsys_sysctl: failed to create file %s" % fn)
def GetSlivers(data, conf=None, plc=None): if 'accounts' not in data: logger.log_missing_data("specialaccounts.GetSlivers", 'accounts') return for account in data['accounts']: name = account['name'] new_keys = account['keys'] logger.log('specialaccounts: dealing with account %s' % name) # look up account name, which must exist pw_info = pwd.getpwnam(name) uid = pw_info[2] gid = pw_info[3] pw_dir = pw_info[5] # populate account's .ssh/authorized_keys file dot_ssh = os.path.join(pw_dir, '.ssh') #~/.ssh if not os.access(dot_ssh, os.F_OK): os.mkdir(dot_ssh) #mkdir ~/.ssh auth_keys = os.path.join(dot_ssh, 'authorized_keys') #=~/.ssh/authorized_keys # catenate all keys in string, add newlines just in case (looks like keys already have this, but) auth_keys_contents = '\n'.join(new_keys) + '\n' changes = tools.replace_file_with_string(auth_keys, auth_keys_contents) if changes: logger.log("specialaccounts: keys file changed: %s" % auth_keys) # always set permissions properly os.chmod(dot_ssh, 0700) os.chown(dot_ssh, uid, gid) os.chmod(auth_keys, 0600) os.chown(auth_keys, uid, gid) logger.log('specialaccounts: installed ssh keys for %s' % name)
def GetSlivers(data, config, plc): if 'OVERRIDES' in dir(config): if config.OVERRIDES.get('sliverauth') == '-1': logger.log("sliverauth: Disabled", 2) return if 'slivers' not in data: logger.log_missing_data("sliverauth.GetSlivers", 'slivers') return for sliver in data['slivers']: path = '/vservers/%s' % sliver['name'] if not os.path.exists(path): # ignore all non-plc-instantiated slivers instantiation = sliver.get('instantiation', '') if instantiation == 'plc-instantiated': logger.log( "sliverauth: plc-instantiated slice %s does not yet exist111. IGNORING!" % sliver['name']) continue system_slice = False for chunk in sliver['attributes']: if chunk['tagname'] == "system": if chunk['value'] in (True, 1, '1') or chunk['value'].lower() == "true": system_slice = True logger.log("sliverauth: system_slice is %s " % system_slice) for chunk in sliver['attributes']: if chunk['tagname'] == 'enable_hmac' and not system_slice: logger.log("sliverauth: slice %s is hmac" % sliver['name']) manage_hmac(plc, sliver) if chunk['tagname'] == 'omf_control': logger.log("sliverauth: slice %s is omf_cotrol" % sliver['name']) manage_sshkey(plc, sliver)
def GetSlivers(data, conf = None, plc = None): if 'accounts' not in data: logger.log_missing_data("specialaccounts.GetSlivers",'accounts') return for account in data['accounts']: name = account['name'] new_keys = account['keys'] logger.log('specialaccounts: dealing with account %s'%name) # look up account name, which must exist pw_info = pwd.getpwnam(name) uid = pw_info[2] gid = pw_info[3] pw_dir = pw_info[5] # populate account's .ssh/authorized_keys file dot_ssh = os.path.join(pw_dir,'.ssh') if not os.access(dot_ssh, os.F_OK): os.mkdir(dot_ssh) auth_keys = os.path.join(dot_ssh,'authorized_keys') # catenate all keys in string, add newlines just in case (looks like keys already have this, but) auth_keys_contents = '\n'.join(new_keys)+'\n' changes = tools.replace_file_with_string(auth_keys,auth_keys_contents) if changes: logger.log("specialaccounts: keys file changed: %s" % auth_keys) # always set permissions properly os.chmod(dot_ssh, 0700) os.chown(dot_ssh, uid,gid) os.chmod(auth_keys, 0600) os.chown(auth_keys, uid,gid) logger.log('specialaccounts: installed ssh keys for %s' % name)
def GetSlivers(data, conf=None, plc=None): if "accounts" not in data: logger.log_missing_data("omf_resctl.GetSlivers", "accounts") return try: xmpp_server = data["xmpp"]["server"] if not xmpp_server: # we have the key but no value, just as bad raise Exception except: # disabled feature - bailing out # xxx might need to clean up more deeply.. logger.log( "PLC config unsufficient (not enabled, or no server set), see the PLC_OMF category -- plugin exiting" ) return # as hrn is set only at AddNode-time, upgraded myplcs might still miss this # clue: just overwrite the hostname of all nodes # for node in GetNodes(): UpdateNode(node['node_id'],{'hostname':node['hostname']}) try: node_hrn = data["hrn"] if not node_hrn: raise Exception except: logger.log("Failed to read hrn from GetSlivers, using 'default' - *please upgrade PLCAPI*") node_hrn = "default # Failed to read hrn from GetSlivers, please upgrade PLCAPI" for sliver in data["slivers"]: name = sliver["name"] sliver_pub_key_dir = os.path.join("/home", name, ".ssh/") sliver_private_key = os.path.join(sliver_pub_key_dir, "id_rsa") for chunk in sliver["attributes"]: if chunk["tagname"] == "omf_control": # scan all versions of omf-resctl etc_path = "/vservers/%s/etc/" % name pattern = etc_path + "omf-resctl-*/omf-resctl.yaml.in" templates = glob.glob(pattern) if not templates: logger.log( "WARNING: omf_resctl plugin, no template found for slice %s using pattern %s" % (name, pattern) ) continue for template in templates: # remove the .in extension yaml = template[:-3] # figure service name as subdir under etc/ service_name = os.path.split(template.replace(etc_path, ""))[0] # read template and replace template_contents = file(template).read() yaml_contents = ( template_contents.replace("@XMPP_SERVER@", xmpp_server) .replace("@NODE_HRN@", node_hrn) .replace("@SLICE_NAME@", name) .replace("@SLIVER_PRIVATE_KEY@", sliver_private_key) .replace("@SLIVER_PUB_KEY_DIR@", sliver_pub_key_dir) ) changes = tools.replace_file_with_string(yaml, yaml_contents) logger.log("yaml_contents length=%d, changes=%r" % (len(yaml_contents), changes)) if changes: sp = subprocess.Popen( ["vserver", name, "exec", "service", service_name, "restart"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) (output, retcod) = sp.communicate() logger.log("omf_resctl: %s: restarted resource controller (retcod=%r)" % (name, retcod)) logger.log("omf_resctl: got output\n%s" % output) else: logger.log("omf_resctl: %s: omf_control'ed sliver has no change" % name)
def GetSlivers(data, config=None, plc=None): if 'slivers' not in data: logger.log_missing_data("interfaces.GetSlivers", 'slivers') return for sliver in data['slivers']: slicename = sliver['name'] if not os.path.exists("/vservers/%s" % slicename): # Avoid creating slice directory if slice does not exist, as it # breaks slice creation when sliver_lxc eventually gets around # to creating the sliver. logger.log("vserver %s does not exist yet. Skipping interfaces." % slicename) continue for tag in sliver['attributes']: if tag['tagname'] == 'interface': interfaces = eval(tag['value']) if not isinstance(interfaces, (list, tuple)): # if interface is not a list, then make it into a singleton list interfaces = [interfaces] for mydict in interfaces: contents="" # First look for filename/url combination for custom config files if 'filename' in mydict and 'url' in mydict: dest = "/vservers/%s/%s" % (slicename, mydict['filename']) url = mydict['url'] try: contents = curlwrapper.retrieve(url) except xmlrpc.client.ProtocolError as e: logger.log('interfaces (%s): failed to retrieve %s' % (slicename, url)) continue else: # Otherwise generate /etc/sysconfig/network-scripts/ifcfg-<device> try: dest = "/vservers/%s/etc/sysconfig/network-scripts/ifcfg-%s" % (slicename, mydict['DEVICE']) except: logger.log('interfaces (%s): no DEVICE specified' % slicename) continue for key, value in list(mydict.items()): if key in ['bridge', 'vlan']: continue contents += '%s="%s"\n' % (key, value) if sha(contents).digest() == checksum(dest): logger.log('interfaces (%s): no changes to %s' % (slicename, dest)) continue logger.log('interfaces (%s): installing file %s' % (slicename, dest)) try: os.makedirs(os.path.dirname(dest)) except OSError: pass try: f = open (dest, "w") f.write(contents) f.close() except: logger.log('interfaces (%s): error writing file %s' % (slicename, dest)) continue # TD: Call lxcsu with '-r'. Otherwise, setns.drop_caps() would remove then required CAP_NET_ADMIN capability! result = logger.log_call(['/usr/sbin/lxcsu', '-r', slicename, '/sbin/service', 'network', 'restart']) if not result: logger.log('interfaces (%s): error restarting network service' % slicename)
def GetSlivers(data, config, plc = None): """ For each sliver with the codemux attribute, parse out "host,port" and make entry in conf. Restart service after. """ if 'OVERRIDES' in dir(config): if config.OVERRIDES.get('codemux') == '-1': logger.log("codemux: Disabled", 2) stopService() return logger.log("codemux: Starting.", 2) # slices already in conf slicesinconf = parseConf() # slices that need to be written to the conf codemuxslices = {} # XXX Hack for planetflow if slicesinconf.has_key("root"): _writeconf = False else: _writeconf = True # Parse attributes and update dict of scripts if 'slivers' not in data: logger.log_missing_data("codemux.GetSlivers", 'slivers') return for sliver in data['slivers']: for attribute in sliver['attributes']: if attribute['tagname'] == 'codemux': # add to conf. Attribute is [host, port] parts = attribute['value'].split(",") if len(parts)<2: logger.log("codemux: attribute value (%s) for codemux not separated by comma. Skipping."%attribute['value']) continue if len(parts) == 3: ip = parts[2] else: ip = "" params = {'host': parts[0], 'port': parts[1], 'ip': ip} try: # Check to see if sliver is running. If not, continue if slivermanager.is_running(sliver['name']): # Check if new or needs updating if (sliver['name'] not in slicesinconf.keys()) \ or (params not in slicesinconf.get(sliver['name'], [])): logger.log("codemux: Updating slice %s using %s" % \ (sliver['name'], params['host'])) # Toggle write. _writeconf = True # Add to dict of codemuxslices. Make list to support more than one # codemuxed host per slice. codemuxslices.setdefault(sliver['name'],[]) codemuxslices[sliver['name']].append(params) except: logger.log("codemux: sliver %s not running yet. Deferring."\ % sliver['name']) pass # Remove slices from conf that no longer have the attribute for deadslice in set(slicesinconf.keys()) - set(codemuxslices.keys()): # XXX Hack for root slice if deadslice != "root": logger.log("codemux: Removing %s" % deadslice) _writeconf = True if _writeconf: writeConf(sortDomains(codemuxslices)) # ensure the service is running startService()
def GetSlivers(data, config=None, plc=None): if 'slivers' not in data: logger.log_missing_data("interfaces.GetSlivers", 'slivers') return for sliver in data['slivers']: slicename = sliver['name'] for tag in sliver['attributes']: if tag['tagname'] == 'interface': interfaces = eval(tag['value']) if not isinstance(interfaces, (list, tuple)): # if interface is not a list, then make it into a singleton list interfaces = [interfaces] for mydict in interfaces: contents = "" # First look for filename/url combination for custom config files if 'filename' in mydict and 'url' in mydict: dest = "/vservers/%s/%s" % (slicename, mydict['filename']) url = mydict['url'] try: contents = curlwrapper.retrieve(url) except xmlrpclib.ProtocolError, e: logger.log( 'interfaces (%s): failed to retrieve %s' % (slicename, url)) continue else: # Otherwise generate /etc/sysconfig/network-scripts/ifcfg-<device> try: dest = "/vservers/%s/etc/sysconfig/network-scripts/ifcfg-%s" % ( slicename, mydict['DEVICE']) except: logger.log('interfaces (%s): no DEVICE specified' % slicename) continue for key, value in mydict.items(): if key in ['bridge', 'vlan']: continue contents += '%s="%s"\n' % (key, value) if sha(contents).digest() == checksum(dest): logger.log('interfaces (%s): no changes to %s' % (slicename, dest)) continue logger.log('interfaces (%s): installing file %s' % (slicename, dest)) try: os.makedirs(os.path.dirname(dest)) except OSError: pass try: f = open(dest, "w") f.write(contents) f.close() except: logger.log('interfaces (%s): error writing file %s' % (slicename, dest)) continue try: subprocess.check_call([ '/usr/sbin/lxcsu', slicename, '/sbin/service', 'network', 'restart' ]) except: logger.log( 'interfaces (%s): error restarting network service' % slicename)
def GetSlivers(data, config, plc = None): """ For each sliver with the codemux attribute, parse out "host,port" and make entry in conf. Restart service after. """ if 'OVERRIDES' in dir(config): if config.OVERRIDES.get('codemux') == '-1': logger.log("codemux: Disabled", 2) stopService() return logger.log("codemux: Starting.", 2) # slices already in conf slicesinconf = parseConf() # slices that need to be written to the conf codemuxslices = {} # XXX Hack for planetflow if slicesinconf.has_key("root"): _writeconf = False else: _writeconf = True # Parse attributes and update dict of scripts if 'slivers' not in data: logger.log_missing_data("codemux.GetSlivers", 'slivers') return for sliver in data['slivers']: for attribute in sliver['attributes']: if attribute['tagname'] == 'codemux': # add to conf. Attribute is [host, port] parts = attribute['value'].split(",") if len(parts)<2: logger.log("codemux: attribute value (%s) for codemux not separated by comma. Skipping."%attribute['value']) continue if len(parts) == 3: ip = parts[2] else: ip = "" params = {'host': parts[0], 'port': parts[1], 'ip': ip} try: # Check to see if sliver is running. If not, continue if vserver.VServer(sliver['name']).is_running(): # Check if new or needs updating if (sliver['name'] not in slicesinconf.keys()) \ or (params not in slicesinconf.get(sliver['name'], [])): logger.log("codemux: Updating slice %s using %s" % \ (sliver['name'], params['host'])) # Toggle write. _writeconf = True # Add to dict of codemuxslices. Make list to support more than one # codemuxed host per slice. codemuxslices.setdefault(sliver['name'],[]) codemuxslices[sliver['name']].append(params) except: logger.log("codemux: sliver %s not running yet. Deferring."\ % sliver['name']) pass # Remove slices from conf that no longer have the attribute for deadslice in set(slicesinconf.keys()) - set(codemuxslices.keys()): # XXX Hack for root slice if deadslice != "root": logger.log("codemux: Removing %s" % deadslice) _writeconf = True if _writeconf: writeConf(sortDomains(codemuxslices)) # ensure the service is running startService()
def GetSlivers(data, conf = None, plc = None): logger.log("omf_resctl.GetSlivers") if 'accounts' not in data: logger.log_missing_data("omf_resctl.GetSlivers", 'accounts') return try: xmpp_server=data['xmpp']['server'] if not xmpp_server: # we have the key but no value, just as bad raise Exception except: # disabled feature - bailing out logger.log("omf_resctl: PLC_OMF config unsufficient (not enabled, or no server set), -- plugin exiting") return hostname = data['hostname'] def is_omf_friendly (sliver): for chunk in sliver['attributes']: if chunk['tagname']=='omf_control': return True for sliver in data['slivers']: # skip non OMF-friendly slices if not is_omf_friendly (sliver): continue slicename=sliver['name'] expires=str(sliver['expires']) yaml_template = config_ple_template yaml_contents = yaml_template\ .replace('_xmpp_server_', xmpp_server)\ .replace('_slicename_', slicename)\ .replace('_hostname_', hostname)\ .replace('_expires_', expires) yaml_full_path="/vservers/%s/%s"%(slicename, yaml_slice_path) yaml_full_dir=os.path.dirname(yaml_full_path) if not os.path.isdir(yaml_full_dir): try: os.makedirs(yaml_full_dir) except OSError: pass config_changes=tools.replace_file_with_string(yaml_full_path, yaml_contents) logger.log("yaml_contents length=%d, config_changes=%r"%(len(yaml_contents), config_changes)) # would make sense to also check for changes to authorized_keys # would require saving a copy of that some place for comparison # xxx todo keys_changes = False if config_changes or keys_changes: # instead of restarting the service we call a companion script try: fetch_trigger_script_if_missing (slicename) # the trigger script actually needs to be run in the slice context of course # in addition there is a requirement to pretend we run as a login shell # hence sudo -i slice_command = [ "sudo", "-i", omf_rc_trigger_script ] to_run = tools.command_in_slice (slicename, slice_command) log_filename = "/vservers/%s/%s"%(slicename, omf_rc_trigger_log) logger.log("omf_resctl: starting %s"%to_run) logger.log("redirected into %s"%log_filename) logger.log("*not* waiting for completion..") with open(log_filename, "a") as log_file: subprocess.Popen(to_run, stdout=log_file, stderr=subprocess.STDOUT) # a first version tried to 'communicate' on that subprocess instance # but that tended to create deadlocks in some cases # causing nodemanager to stall... # we're only losing the child's retcod, no big deal except: import traceback traceback.print_exc() logger.log_exc("omf_resctl: WARNING: Could not call trigger script %s"%\ omf_rc_trigger_script, name=slicename) else: logger.log("omf_resctl: %s: omf_control'ed sliver has no change" % slicename)
def GetSlivers(data, conf=None, plc=None): logger.log("omf_resctl.GetSlivers") if 'accounts' not in data: logger.log_missing_data("omf_resctl.GetSlivers", 'accounts') return try: xmpp_server = data['xmpp']['server'] if not xmpp_server: # we have the key but no value, just as bad raise Exception except: # disabled feature - bailing out logger.log( "omf_resctl: PLC_OMF config unsufficient (not enabled, or no server set), -- plugin exiting" ) return hostname = data['hostname'] def is_omf_friendly(sliver): for chunk in sliver['attributes']: if chunk['tagname'] == 'omf_control': return True for sliver in data['slivers']: # skip non OMF-friendly slices if not is_omf_friendly(sliver): continue slicename = sliver['name'] expires = str(sliver['expires']) yaml_template = config_ple_template yaml_contents = yaml_template\ .replace('_xmpp_server_',xmpp_server)\ .replace('_slicename_',slicename)\ .replace('_hostname_',hostname)\ .replace('_expires_',expires) yaml_full_path = "/vservers/%s/%s" % (slicename, yaml_slice_path) yaml_full_dir = os.path.dirname(yaml_full_path) if not os.path.isdir(yaml_full_dir): try: os.makedirs(yaml_full_dir) except OSError: pass config_changes = tools.replace_file_with_string( yaml_full_path, yaml_contents) logger.log("yaml_contents length=%d, config_changes=%r" % (len(yaml_contents), config_changes)) # would make sense to also check for changes to authorized_keys # would require saving a copy of that some place for comparison # xxx todo keys_changes = False if config_changes or keys_changes: # instead of restarting the service we call a companion script try: fetch_trigger_script_if_missing(slicename) # the trigger script actually needs to be run in the slice context of course # in addition there is a requirement to pretend we run as a login shell # hence sudo -i slice_command = ["sudo", "-i", omf_rc_trigger_script] to_run = tools.command_in_slice(slicename, slice_command) log_filename = "/vservers/%s/%s" % (slicename, omf_rc_trigger_log) logger.log("omf_resctl: starting %s" % to_run) logger.log("redirected into %s" % log_filename) logger.log("*not* waiting for completion..") with open(log_filename, "a") as log_file: subprocess.Popen(to_run, stdout=log_file, stderr=subprocess.STDOUT) # a first version tried to 'communicate' on that subprocess instance # but that tended to create deadlocks in some cases # causing nodemanager to stall... # we're only losing the child's retcod, no big deal except: import traceback traceback.print_exc() logger.log_exc("omf_resctl: WARNING: Could not call trigger script %s"%\ omf_rc_trigger_script, name=slicename) else: logger.log("omf_resctl: %s: omf_control'ed sliver has no change" % slicename)
def GetSlivers(data, config = None, plc=None, fullupdate=True): """This function has two purposes. One, convert GetSlivers() data into a more convenient format. Two, even if no updates are coming in, use the GetSlivers() heartbeat as a cue to scan for expired slivers.""" logger.verbose("slivermanager: Entering GetSlivers with fullupdate=%r"%fullupdate) for key in data.keys(): logger.verbose('slivermanager: GetSlivers key : ' + key) node_id = None try: f = open('/etc/planetlab/node_id') try: node_id = int(f.read()) finally: f.close() except: logger.log_exc("slivermanager: GetSlivers failed to read /etc/planetlab/node_id") if data.has_key('node_id') and data['node_id'] != node_id: return if data.has_key('networks'): for network in data['networks']: if network['is_primary'] and network['bwlimit'] is not None: DEFAULT_ALLOCATION['net_max_rate'] = network['bwlimit'] / 1000 # Take initscripts (global) returned by API, build a hash scriptname->code iscripts_hash = {} if 'initscripts' not in data: logger.log_missing_data("slivermanager.GetSlivers",'initscripts') return for initscript_rec in data['initscripts']: logger.verbose("slivermanager: initscript: %s" % initscript_rec['name']) iscripts_hash[str(initscript_rec['name'])] = initscript_rec['script'] adjustReservedSlivers (data) for sliver in data['slivers']: logger.verbose("slivermanager: %s: slivermanager.GetSlivers in slivers loop"%sliver['name']) rec = sliver.copy() rec.setdefault('timestamp', data['timestamp']) # convert attributes field to a proper dict attributes = {} for attr in rec.pop('attributes'): attributes[attr['tagname']] = attr['value'] rec.setdefault("attributes", attributes) # squash keys keys = rec.pop('keys') rec.setdefault('keys', '\n'.join([key_struct['key'] for key_struct in keys])) ## 'Type' isn't returned by GetSlivers() for whatever reason. We're overloading ## instantiation here, but i suppose its the same thing when you think about it. -FA # Handle nm-controller here if rec['instantiation'].lower() == 'nm-controller': rec.setdefault('type', attributes.get('type', 'controller.Controller')) else: rec.setdefault('type', attributes.get('type', sliver_default_type)) # set the vserver reference. If none, set to default. rec.setdefault('vref', attributes.get('vref', 'default')) ### set initscripts; set empty rec['initscript'] if not # if tag 'initscript_code' is set, that's what we use iscode = attributes.get('initscript_code','') if iscode: rec['initscript']=iscode else: isname = attributes.get('initscript') if isname is not None and isname in iscripts_hash: rec['initscript'] = iscripts_hash[isname] else: rec['initscript'] = '' # set delegations, if none, set empty rec.setdefault('delegations', attributes.get("delegations", [])) # extract the implied rspec rspec = {} rec['rspec'] = rspec for resname, default_amount in DEFAULT_ALLOCATION.iteritems(): try: t = type(default_amount) amount = t.__new__(t, attributes[resname]) except (KeyError, ValueError): amount = default_amount rspec[resname] = amount # add in sysctl attributes into the rspec for key in attributes.keys(): if key.find("sysctl.") == 0: rspec[key] = attributes[key] # also export tags in rspec so they make it to the sliver_vs.start call rspec['tags']=attributes database.db.deliver_record(rec) if fullupdate: database.db.set_min_timestamp(data['timestamp']) # slivers are created here. database.db.sync()
def GetSlivers(data, config=None, plc=None): if 'slivers' not in data: logger.log_missing_data("interfaces.GetSlivers",'slivers') return for sliver in data['slivers']: slicename = sliver['name'] for tag in sliver['attributes']: if tag['tagname'] == 'interface': interfaces = eval(tag['value']) if not isinstance(interfaces, (list,tuple)): # if interface is not a list, then make it into a singleton list interfaces = [interfaces] for mydict in interfaces: contents="" # First look for filename/url combination for custom config files if 'filename' in mydict and 'url' in mydict: dest = "/vservers/%s/%s" % (slicename, mydict['filename']) url = mydict['url'] try: contents = curlwrapper.retrieve(url) except xmlrpclib.ProtocolError,e: logger.log('interfaces (%s): failed to retrieve %s' % (slicename, url)) continue else: # Otherwise generate /etc/sysconfig/network-scripts/ifcfg-<device> try: dest = "/vservers/%s/etc/sysconfig/network-scripts/ifcfg-%s" % (slicename, mydict['DEVICE']) except: logger.log('interfaces (%s): no DEVICE specified' % slicename) continue for key, value in mydict.items(): if key in ['bridge', 'vlan']: continue contents += '%s="%s"\n' % (key, value) if sha(contents).digest() == checksum(dest): logger.log('interfaces (%s): no changes to %s' % (slicename, dest)) continue logger.log('interfaces (%s): installing file %s' % (slicename, dest)) try: os.makedirs(os.path.dirname(dest)) except OSError: pass try: f = open (dest, "w") f.write(contents) f.close() except: logger.log('interfaces (%s): error writing file %s' % (slicename, dest)) continue try: subprocess.check_call(['/usr/sbin/lxcsu', slicename, '/sbin/service', 'network', 'restart']) except: logger.log('interfaces (%s): error restarting network service' % slicename)
def GetSlivers(data, config = None, plc=None, fullupdate=True): """This function has two purposes. One, convert GetSlivers() data into a more convenient format. Two, even if no updates are coming in, use the GetSlivers() heartbeat as a cue to scan for expired slivers.""" logger.verbose("slivermanager: Entering GetSlivers with fullupdate=%r"%fullupdate) for key in data.keys(): logger.verbose('slivermanager: GetSlivers key : ' + key) node_id = None try: f = open('/etc/planetlab/node_id') try: node_id = int(f.read()) finally: f.close() except: logger.log_exc("slivermanager: GetSlivers failed to read /etc/planetlab/node_id") if data.has_key('node_id') and data['node_id'] != node_id: return if data.has_key('networks'): for network in data['networks']: if network['is_primary'] and network['bwlimit'] is not None: DEFAULT_ALLOCATION['net_max_rate'] = network['bwlimit'] / 1000 # Take initscripts (global) returned by API, build a hash scriptname->code iscripts_hash = {} if 'initscripts' not in data: logger.log_missing_data("slivermanager.GetSlivers",'initscripts') return for initscript_rec in data['initscripts']: logger.verbose("slivermanager: initscript: %s" % initscript_rec['name']) iscripts_hash[str(initscript_rec['name'])] = initscript_rec['script'] adjustReservedSlivers (data) for sliver in data['slivers']: logger.verbose("slivermanager: %s: slivermanager.GetSlivers in slivers loop"%sliver['name']) rec = sliver.copy() rec.setdefault('timestamp', data['timestamp']) # convert attributes field to a proper dict attributes = {} for attr in rec.pop('attributes'): attributes[attr['tagname']] = attr['value'] rec.setdefault("attributes", attributes) # squash keys keys = rec.pop('keys') rec.setdefault('keys', '\n'.join([key_struct['key'] for key_struct in keys])) ## 'Type' isn't returned by GetSlivers() for whatever reason. We're overloading ## instantiation here, but i suppose its the same thing when you think about it. -FA # Handle nm-controller here if rec['instantiation'].lower() == 'nm-controller': rec.setdefault('type', attributes.get('type', 'controller.Controller')) else: rec.setdefault('type', attributes.get('type', 'sliver.VServer')) # set the vserver reference. If none, set to default. rec.setdefault('vref', attributes.get('vref', 'default')) ### set initscripts; set empty rec['initscript'] if not # if tag 'initscript_code' is set, that's what we use iscode = attributes.get('initscript_code','') if iscode: rec['initscript']=iscode else: isname = attributes.get('initscript') if isname is not None and isname in iscripts_hash: rec['initscript'] = iscripts_hash[isname] else: rec['initscript'] = '' # set delegations, if none, set empty rec.setdefault('delegations', attributes.get("delegations", [])) # extract the implied rspec rspec = {} rec['rspec'] = rspec for resname, default_amount in DEFAULT_ALLOCATION.iteritems(): try: t = type(default_amount) amount = t.__new__(t, attributes[resname]) except (KeyError, ValueError): amount = default_amount rspec[resname] = amount # add in sysctl attributes into the rspec for key in attributes.keys(): if key.find("sysctl.") == 0: rspec[key] = attributes[key] # also export tags in rspec so they make it to the sliver_vs.start call rspec['tags']=attributes database.db.deliver_record(rec) if fullupdate: database.db.set_min_timestamp(data['timestamp']) # slivers are created here. database.db.sync()