Example #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
Example #2
0
    def write_configs(self, config_data):
        """Write configs to appropriate paths with appropriate permisions.

        Args:
            config_data: list of dicts of configuration data for server.
        Returns:
            list, containing tuples of return codes for each file's perm ops.
        """
        return_codes = []
        for config in config_data:
            _size, temp_file = tempfile.mkstemp(text=True)
            if 'contents' in config.keys(): # Possibly an empty config
                # If we have Jinja templ values to insert into the config
                if 'keywords' in config.keys():
                    logging.debug('replacing keywords in config template')
                    config['contents'] = self.customize_template(
                            config['contents'], config['keywords'])
                temp = open(temp_file, 'w')
                temp.write(config['contents'])
                temp.close()

            if not self._create_dir_path(config['path']):
                logging.error(
                    'Could not create directory tree for %s' % config['path'])
                continue

            try:
                shutil.move(temp_file, config['path'])
            except shutil.Error, e:
                logging.warn(
                        'Invalid Path Specified: %s. Traceback:\n %s' % (
                            config['path'], e))
                continue

            file_ret_codes = self._update_conf_perms(
                    config['name'], config['path'], config['owner'],
                    config['group'], config['mode'])

            return_codes.append(file_ret_codes)