Exemple #1
0
    def pick_name(self, db, service, service_type, instance_id):
        """Check against the names in the aerostat database.

        This is the basic logic behind deciding which names are available.
        It checks the aerostat database, and using a combination of the
        service_type and the instance_id, it determines if there are:
            1) duplicate entries - in which case, it overwrites the older entry.
            2) depending on type, it will determine the next logical name in the
            services progression.
            3) this function then returns that string value.

        Args:
            db: a pymongo.Connection.db instance.
            service: str, the name retrieved from /etc/aerostat_info.
            service_type: str, kind of service hierarchy, masterful or iterative.
            instance_id: str, name of instance, to check for dups.
        Returns:
            str, the appropriate hostname for the client node.
        """
        hostname = None
        # Check for duplicates. But only if instances have names.
        if self.check_dup(db, instance_id) and aerostat.get_hostname(
                db, instance_id):
            logging.warn('Duplicate instance found')
            return None

        results = list(db.servers.find({'service': service}))
        # We only want to count instances in our service with hostnames.
        named_in_service = [item for item in results if item['hostname']]
        num = len(named_in_service)
        logging.info('%s number of hosts with same service found' % num)

        if service_type == 'masterful':
            master_hostname = '%s-master' % (service,)
            if not named_in_service:
                hostname = master_hostname  # first instance will be master.
            elif aerostat.hostname_exists(db,
                        master_hostname) and not self.hostname_instance_exists(
                                db, master_hostname):
                hostname = master_hostname  # replace fallen master.
            else:
                smallest_slave_gap = self.get_smallest_gap(db, service)
                if smallest_slave_gap:
                    hostname = smallest_slave_gap
                else:
                    hostname = '%s-slave-%s' % (service, num)
        else:  # We're iterative.
            smallest_gap = self.get_smallest_gap(db, service)
            # find out if there are gaps in the hostnames, use smallest.
            if smallest_gap:
                hostname = smallest_gap
            else:
                hostname =  '%s-%s' % (service, num) # New instance, no gaps.

        return hostname
Exemple #2
0
    def register_name(self, db, hostname, local_ip, instance_id, service, service_type, aliases):
        """Insert Hostname -> IP relationship into Aerostat DB.

        Args:
            db: a pymongo.Connection.db instnace.
            hostname: str, name of client host.
            local_ip: str, ip address of client host.
            instance_id: str, name of EC2 instance.
            service: str, name of service.
            service_type: str, name of server category.
            aliases: list of str, alternate names.
        Returns:
            True if registration succeeded.
        """
        if aliases:
            aliases = list(set(aliases))  # remove any duplicates.
            conflicting_aliases = self.alias_exists(db, aliases)
            if conflicting_aliases:
                # Update Aliases in all hosts to not have said alias anymore.
                self.reset_conflict_aliases(db, conflicting_aliases)

        if aerostat.hostname_exists(db, hostname):
            # hostname already exists, fill in the gap.
            db.servers.update(
                {"hostname": hostname},
                {
                    "$set": {
                        "ip": local_ip,
                        "service": service,
                        "service_type": service_type,
                        "instance_id": instance_id,
                        "aliases": aliases,
                    }
                },
            )
        else:
            # This is a new host being added to the service cluster.
            db.servers.insert(
                {
                    "hostname": hostname,
                    "ip": local_ip,
                    "service": service,
                    "service_type": service_type,
                    "instance_id": instance_id,
                    "aliases": aliases,
                }
            )

        # If registration succeeded
        return True