def init_host(settings): """ Reinitialize or initialize host instance from environment and stored settings. """ host = dict( name = get_hostname() ) hostnameId = host['name'].lower() if hostnameId not in settings.nodes: ifaces = {} for iface, mac, spec in domain.inet_ifaces(): if mac in settings.interfaces: raise Exception("Found existing interface", mac) else: settings.interfaces[mac] = ifaces[mac] = dict( node = hostnameId ) if settings.interactive: name = Prompt.raw_input("Give a name for this node", host['name']) host.update(dict( unid = str(uuid.uuid4()), interfaces = ifaces.keys() )) settings.nodes[hostnameId] = host open(hostIdFile, 'w+').write(" ".join((host['unid'], host['name']))) log.std("{bwhite}Wrote new host, {green}%s {default}<{bblack}%s{default}>", host['name'], host['unid']) settings.commit() else: host = get_current_host(settings) log.std("{bwhite}Found host, {green}%s {default}<{bblack}%s{default}>", host['name'], host['unid']) return host
def cmd_update(settings): """ Initialize current host, creating host ID if not present yet and record hosts interfaces. Then determine the current domain, and record current iface IP's. Commit data if needed. """ # read host ID file or record node/ifaces host = init_host(settings) print 'host', host.path() gateway, mac, gateway_addr = get_gateway(settings) net = get_network(settings, gateway.domain) # read SSH pubkey user domain #user, domain = init_domain(settings) print 'domain', net.path() # update host IP's, and update network updated = False for iface, mac, spec in domainmod.inet_ifaces(): ip = spec['inet']['ip'] if mac not in settings.interfaces: log.err("Missing iface type for HW-addr '%s'" % mac) continue iface_type = settings.interfaces[mac].type print '\t', iface_type, ip # Keep IP on host net_host = host.get('net') if iface_type not in net_host: net_host.get(iface_type, dict(ip=None)) if net_host[iface_type].ip != ip: net_host[iface_type].ip = ip updated = True # Track the current network fulldomain = net.get(host.name) if iface_type not in fulldomain: fulldomain.get(iface_type, dict(ip=None)) if fulldomain[iface_type].ip != ip: fulldomain[iface_type].ip = ip updated = True if updated: settings.commit()