def index(): running = docker.containers(quiet=True, all=True) container_list = [] for con in running: container_info = docker.inspect_container(con['Id']) container_list.append(container_info) running_containers = [ container for container in container_list if container['State']['Running'] is True ] human_containers = [ container for container in container_list if container['State']['Running'] is False and len( re.findall(r"^[a-f\d]{64}$|^[A-F\d]{64}$", container['Config'] ['Image'])) == 0 ] computer_containers = [ container for container in container_list if container['State']['Running'] is False and len( re.findall(r"^[a-f\d]{64}$|^[A-F\d]{64}$", container['Config'] ['Image'])) == 1 ] # Sort by name running_containers, human_containers, computer_containers = \ sorted(running_containers, key=lambda x: x['Name']), \ sorted(human_containers, key=lambda x: x['Name']), \ sorted(computer_containers, key=lambda x: x['Name']) # Then by last used running_containers, human_containers, computer_containers = \ sorted(running_containers, key=lambda x: x['State']['StartedAt'], reverse=True), \ sorted(human_containers, key=lambda x: x['State']['StartedAt'], reverse=True), \ sorted(computer_containers, key=lambda x: x['State']['StartedAt'], reverse=True) hostname = bottle.request.get_header('Host', 'localhost').split(':')[0] running_containers = group_containers_by_name(running_containers) human_containers = group_containers_by_name(human_containers) computer_containers = group_containers_by_name(computer_containers) return bottle.template('index.html', title="Appstore", menu=generate_menu(), hosts=Hosts(HOSTS_PATH).get_reversed(), running_containers=running_containers, human_containers=human_containers, computer_containers=computer_containers, hostname=hostname)
def list_hosts(): hosts = Hosts(HOSTS_PATH) running_containers = [ docker.inspect_container(container['Id']) for container in docker.containers(quiet=True) ] ip_list = [ info['NetworkSettings']['IPAddress'] for info in running_containers if 'IPAddress' in info['NetworkSettings'] ] return bottle.template('hosts.html', title="Hosts | Appstore", menu=generate_menu(), hosts=hosts, active_ip_list=ip_list, begins_with='172.17.0.')
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())
if info['Config']['Hostname']: hostname = info['Config']['Hostname'] return "%s%s" % (hostname, dom_name) 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)
dom_name = '.%s' % dom_name if info['Config']['Hostname']: return "%s%s" % (info['Config']['Hostname'], dom_name) else: return None if __name__ == '__main__': 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':