def delete_inactive_hosts(): running_containers = [docker.inspect_container(container['Id']) for container in docker.containers(quiet=True)] active_ip_list = [info['NetworkSettings']['IPAddress'] for info in running_containers if 'IPAddress' in info['NetworkSettings']] hosts = Hosts(HOSTS_PATH) reversed_list = hosts.get_reversed() for ip in reversed_list: if ip[0:9] == '172.17.0.' and ip not in active_ip_list: hosts.remove_all(reversed_list[ip]) hosts.write(HOSTS_PATH) return bottle.redirect(bottle.request.headers.get('Referer', '/').strip())
def delete_inactive_hosts(): running_containers = [ docker.inspect_container(container['Id']) for container in docker.containers(quiet=True) ] active_ip_list = [ info['NetworkSettings']['IPAddress'] for info in running_containers if 'IPAddress' in info['NetworkSettings'] ] hosts = Hosts(HOSTS_PATH) reversed_list = hosts.get_reversed() for ip in reversed_list: # TODO: DO NOT hardcode ip here if ip[0:9] == '172.17.0.' and ip not in active_ip_list: hosts.remove_all(reversed_list[ip]) hosts.write(HOSTS_PATH) return bottle.redirect(bottle.request.headers.get('Referer', '/').strip())
def delete_host(hostname): hosts = Hosts(HOSTS_PATH) hosts.remove_one(hostname) hosts.write(HOSTS_PATH) return bottle.redirect(bottle.request.headers.get('Referer', '/').strip())
else: return None if __name__ == '__main__': docker = Client() hostname = getenv('HOSTNAME', None) if hostname: if '.' not in hostname: hostname = "%s%s" % (hostname, DOMAIN_SUFFIX) print "Adding %s" % hostname hosts = Hosts(HOSTS_PATH) my_ip = commands.getoutput("ip -4 -f inet -o addr show eth0 | awk '{print $4}' | cut -d/ -f1") hosts.set_one(hostname, my_ip) hosts.write(HOSTS_PATH) print "Go to http://%s/" % hostname for event in docker.events(): event = json.loads(event) if 'status' not in event: continue if event['status'] == 'start': hostname = get_hostname(event['id']) if hostname is None: continue print "Adding %s" % hostname hosts = Hosts(HOSTS_PATH) hosts.set_one(hostname, get_ip(event['id'])) hosts.write(HOSTS_PATH) elif event['status'] == 'die':
docker = Client() hostname = getenv('HOSTNAME', None) if hostname: if '.' not in hostname: hostname = "{}{}".format(hostname, DOMAIN_SUFFIX) print("Adding {}".format(hostname)) hosts = Hosts(HOSTS_PATH) my_ip = check_output([ "/bin/sh", "-c", "ip -4 -f inet -o addr show eth0 | awk '{print $4}' | cut -d/ -f1" ]) my_ip = my_ip.decode().strip() print("My IP: {}".format(my_ip)) hosts.set_one(hostname, my_ip) hosts.write(HOSTS_PATH) print("Go to http://%s/" % hostname) for event in docker.events(): event = json.loads(event) if 'status' not in event: continue if event['status'] == 'start': hostname = get_hostname(event['id']) if hostname is None: print( "ERR: Event 'start' received but no hostname found for {}". format(event['id'])) continue container_ip = get_ip(event['id']) if not container_ip: