Esempio n. 1
0
    def do_registrar(self, db, dry_run, change_master, offline):
        """Setup hostname for a newly bootstraped server.

        Args:
            db: mongodb database reference.
            dry_run: bool, should we actually apply changes.
            change_master: bool, should we swap masters.
            offline: bool, should we connect to aws.
        Returns:
            bool, True if system settings are correctly changed.
        """

        service, service_type, aliases = self.parse_service_info()
        instance_id, local_ip = aerostat.get_aws_data(offline)
        if change_master:
            self.change_master(db, service, service_type, instance_id)

            return True

        hostname = self.pick_name(db, service, service_type, instance_id)
        change_host = False
        # If we successfully aquired a hostname (not dup) from mongodb
        if dry_run:
            logging.debug("DRY RUN: you would register with: %s" % hostname)

            return False

        if hostname:
            change_host = self.set_sys_hostname(hostname)
        if change_host:
            self.register_name(db, hostname, local_ip, instance_id, service, service_type, aliases)

        return True
Esempio n. 2
0
    def append_hosts_line(self, ip, hostname):
        """Format string appropriate for /etc/hosts file.

        Args:
            ip: str, ip address which mapping is made against.
            hostname: str, hostname to map against ip.
        Modifies:
            self.hosts_data, appends more hostname -> ip mappings. One mapping
            per list item, which translates into one line in the file.
        """
        logging.debug("Parsing mapping for host %s and ip %s" % (hostname, ip))
        self.hosts_data.append("%s %s" % (ip, hostname))
Esempio n. 3
0
    def do_update(self, db, dry_run=None, legacy_updater=None):
        """Update /etc/hosts.

        Args:
            db: mongdb db reference.
            dry_run: bool, whether or not to actually update /etc/hosts.
            legacy_updater: binary to run in order to update /etc/hosts
            (helpful for transitions).
        Returns:
            bool, True if changes are made to the system.
        """

        if legacy_updater:
            # Call legacy host updater, allow it to write to /etc/hosts.
            retcode = subprocess.call([legacy_updater])
            if retcode < 0:
                logging.error('Call to %s failed!' % legacy_updater)
                sys.exit(1)

        self.hosts_data = ['127.0.0.1 localhost']  # Reset data, otherwise we append
        aerostat_data = db.servers.find()

        # extract hostname, ip and aliases
        for item in aerostat_data:
            if item['ip']:
                self.append_hosts_line(item['ip'], item['hostname'])
            if item['aliases']:
                self.format_aliases(item['ip'], item['aliases'])

        if dry_run:
            dry_run_output = '\n'.join(self.hosts_data) + '\n'
            logging.debug(
                    ('DRY RUN: Your /etc/hosts file would look'
                    'like this: \n%s' % dry_run_output))
            return False

        # Only make any changes if there are actual data available to write.
        if self.hosts_data:
            logging.info('Copying /etc/hosts to /etc/hosts.bak')
            shutil.copyfile('/etc/hosts', '/etc/hosts.bak')
            logging.info('Writing new /etc/hosts file.')
            self.write_hosts_file()
        else:
            logging.error('No data returned from aerostat. Write aborted.')

        return True
Esempio n. 4
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)
Esempio n. 5
0
    def get_types(self):
        """Returns server_type and service_type.

        /etc/aerostat_info is a file that's created by the user_data script,
        which is executed the first time Ubuntu servers are booted up in EC2.

        The file format looks something like:
        <service_name> <service_type> <alias1> <alias2> <aliasN>

        Only service_name field is required.
        User_data is set in the installer config.
        """
        aerostat_info_file = os.environ.get('AEROSTAT_INFO','/etc/aerostat_info')
        logging.debug('get_types(): opening type info from %s.' % aerostat_info_file)
        server_info = open(aerostat_info_file, 'r')
        logging.debug('get_types(): reading type info from %s.' % aerostat_info_file)
        types = server_info.read().strip().split()
        logging.debug('recovered type info from %s.' % aerostat_info_file)
        server_info.close()

        return types