Ejemplo n.º 1
0
    async def delete_containers(self):
        '''
        Deletes the containers in docker and the db records
        '''
        logging.info('Deleting containers')

        client = aiodocker.Docker()

        containers = await client.containers.list(all=True)
        # Remove the containers first
        for container in Container.select():
            logging.debug('Deleting %s', container.name)
            for item in containers:
                if item.id == container.container_id:
                    await item.delete(v=True, force=True)

        # Purge the db. Need to find a more efficient way
        # to do this
        for container in Container.select():
            container.delete_instance()

        for port in Port.select():
            port.delete_instance()

        await client.close()
Ejemplo n.º 2
0
    def get_available_address(self, count):
        # Add three to the number of containers to account for the gateway,
        # network, broadcast address
        subnet = utility.Net.generate_cidr(self.config.network.address,
                                           count + 3)

        ip = netaddr.IPNetwork(subnet)

        # Calc the gateway as network address + 1
        gateway = utility.Net.ipint_to_str(ip.first + 1)

        # Broadcast is the last
        broadcast = utility.Net.ipint_to_str(ip.last)

        addresses = [
            str(address) for address in list(netaddr.IPSet([
                subnet,
            ]))
        ]

        if gateway in addresses:
            addresses.remove(gateway)
        if broadcast in addresses:
            addresses.remove(broadcast)
        if self.config.network.address in addresses:
            addresses.remove(self.config.network.address)

        # Remove existing addresses
        for container in Container.select(Container.ip):
            addresses.remove(container.ip)

        return addresses, subnet, gateway
Ejemplo n.º 3
0
    def build_container_map(self):
        '''
        Creates dictionaries for fast retrieval of container information by index.
        '''

        container_map_by_port = {}
        container_map_by_ip = {}
        container_map_by_name = {}

        containers = Container.select()
        for container in containers:
            container_map_by_name[container.name] = container

        end_points = Port.select(Port.number, Port.protocol, Container.ip, Container.name,
                                 Container.container_id, Container.start_delay, Container.start_retry_count,
                                 Container.start_on_create, Container.sub_domain) \
            .join(Container)\
            .order_by(Port.number)

        for end_point in end_points:
            port_key = self.get_port_map_key(end_point.number,
                                             end_point.protocol)
            container = end_point.container

            if port_key not in container_map_by_port:
                container_map_by_port[port_key] = {}

            ip = utility.Net.ipstr_to_int(container.ip)
            container_map_by_port[port_key][ip] = container

            if ip not in container_map_by_ip:
                container_map_by_ip[ip] = container

        return container_map_by_port, container_map_by_ip, container_map_by_name
Ejemplo n.º 4
0
 async def stop_all_containers(self):
     containers = Container.select()
     client = await self.client_pool.get()
     for container in containers:
         try:
             client.stop(container=container.name)
         except:
             pass
Ejemplo n.º 5
0
 def view(self, container_map_by_port=None, container_map_by_ip=None):
     '''
     Displays the created containers specified by the current configuration
     '''
     containers = Container.select()
     for container in containers:
         print('{}.{}.{}\t\t{}'.format(container.name, container.sub_domain,
                                       self.config.network.domain,
                                       container.ip))
Ejemplo n.º 6
0
    def generate_host_names(self, naming_cfg, count):

        containers = Container.select(Container.name)

        unique_hosts = {}

        with open(naming_cfg.word_file, "r") as f:
            array = []
            for line in f:
                word = re.sub(naming_cfg.allowable_host_chars, "",
                              line.lower())
                word_len = len(word)
                if word_len >= naming_cfg.min_host_len and \
                                word_len <= naming_cfg.max_host_len:
                    array.append(word)

            while len(unique_hosts) < count:
                index = random.randint(1, len(array))
                name = array[index - 1]
                if not name in unique_hosts and name not in containers:
                    unique_hosts[name] = name

            return list(unique_hosts)