Esempio n. 1
0
 def check_host(self, hostname, result_only=False):
     command_string = '/usr/bin/sudo -u %s ansible ' % get_admin_user()
     gen_file_path = self.create_json_gen_file()
     err_msg = None
     output = None
     try:
         inventory_string = '-i ' + gen_file_path
         ping_string = ' %s %s' % (hostname, '-m ping')
         cmd = (command_string + inventory_string + ping_string)
         err_msg, output = utils.run_cmd(cmd, False)
     except Exception as e:
         raise e
     finally:
         if gen_file_path:
             os.remove(gen_file_path)
     if err_msg:
         if result_only:
             return False
         else:
             raise exceptions.CommandError(
                 'Host (%s) check failed : %s %s' %
                 (hostname, err_msg, output))
     else:
         if not result_only:
             self.log.info('Host (%s) check succeeded' % hostname)
     return True
Esempio n. 2
0
 def check_host(self, hostname, result_only=False):
     command_string = '/usr/bin/sudo -u %s ansible ' % get_admin_user()
     gen_file_path = self.create_json_gen_file()
     err_msg = None
     output = None
     try:
         inventory_string = '-i ' + gen_file_path
         ping_string = ' %s %s' % (hostname, '-m ping')
         cmd = (command_string + inventory_string + ping_string)
         err_msg, output = utils.run_cmd(cmd, False)
     except Exception as e:
         raise e
     finally:
         if gen_file_path:
             os.remove(gen_file_path)
     if err_msg:
         if result_only:
             return False
         else:
             raise exceptions.CommandError(
                 'Host (%s) check failed : %s %s'
                 % (hostname, err_msg, output))
     else:
         if not result_only:
             self.log.info('Host (%s) check succeeded' % hostname)
     return True
Esempio n. 3
0
def clear_password(pwd_key):
    """clear a password

    if the password exists, it will be removed from the passwords file
    """
    cmd = "%s -k %s -c" % (_get_cmd_prefix(), pwd_key)
    err_msg, output = utils.run_cmd(cmd, print_output=False)
    if err_msg:
        raise CommandError("%s %s" % (err_msg, output))
Esempio n. 4
0
def clear_password(pwd_key):
    """clear a password

    if the password exists, it will be removed from the passwords file
    """
    cmd = '%s -k %s -c' % (_get_cmd_prefix(), pwd_key)
    err_msg, output = utils.run_cmd(cmd, print_output=False)
    if err_msg:
        raise CommandError('%s %s' % (err_msg, output))
Esempio n. 5
0
def set_password(pwd_key, pwd_value):
    """set a password value

    If the password name exists, it will be changed.
    If it doesn't exist, a new password will be added.
    """
    cmd = "%s -k %s -v %s" % (_get_cmd_prefix(), pwd_key, pwd_value)
    err_msg, output = utils.run_cmd(cmd, print_output=False)
    if err_msg:
        raise CommandError("%s %s" % (err_msg, output))
Esempio n. 6
0
def set_password(pwd_key, pwd_value):
    """set a password value

    If the password name exists, it will be changed.
    If it doesn't exist, a new password will be added.
    """
    cmd = '%s -k %s -v %s' % (_get_cmd_prefix(), pwd_key, pwd_value)
    err_msg, output = utils.run_cmd(cmd, print_output=False)
    if err_msg:
        raise CommandError('%s %s' % (err_msg, output))
Esempio n. 7
0
def get_password_names():
    """return a list of password names"""
    cmd = "%s -l" % (_get_cmd_prefix())
    err_msg, output = utils.run_cmd(cmd, print_output=False)
    if err_msg:
        raise CommandError("%s %s" % (err_msg, output))

    pwd_names = []
    if output and "," in output:
        pwd_names = output.strip().split(",")
    return pwd_names
Esempio n. 8
0
def get_password_names():
    """return a list of password names"""
    cmd = '%s -l' % (_get_cmd_prefix())
    err_msg, output = utils.run_cmd(cmd, print_output=False)
    if err_msg:
        raise CommandError('%s %s' % (err_msg, output))

    pwd_names = []
    if output and ',' in output:
        pwd_names = output.strip().split(',')
    return pwd_names
Esempio n. 9
0
    def _add_cmd_info(self, tar):
        # run all the kollacli list commands
        cmds = ['kollacli service listgroups',
                'kollacli service list',
                'kollacli group listservices',
                'kollacli group listhosts',
                'kollacli host list',
                'kollacli property list',
                'kollacli password list']

        # collect the json inventory output
        inventory = Inventory.load()
        inv_path = inventory.create_json_gen_file()
        cmds.append(inv_path)

        try:
            fd, path = tempfile.mkstemp(suffix='.tmp')
            os.close(fd)
            with open(path, 'w') as tmp_file:
                for cmd in cmds:
                    err_msg, output = run_cmd(cmd, False)
                    tmp_file.write('\n\n$ %s\n' % cmd)
                    if err_msg:
                        tmp_file.write('Error message: %s\n' % err_msg)
                    lines = output.split('\n')
                    for line in lines:
                        tmp_file.write(line + '\n')
            tar.add(path, arcname=os.path.join('kolla', 'cmds_output'))

        except Exception as e:
            raise e
        finally:
            if path:
                os.remove(path)
            if inv_path:
                os.remove(inv_path)
        return
Esempio n. 10
0
    def run(self):
        globals_string = None
        password_string = None
        inventory_path = None
        cmd = ''
        try:
            flag = ''
            # verbose levels: 1=not verbose, 2=more verbose
            if self.verbose_level > 1:
                flag = '-vvv'

            admin_user = get_admin_user()
            command_string = ('/usr/bin/sudo -u %s ansible-playbook %s'
                              % (admin_user, flag))
            inventory = Inventory.load()
            inventory_filter = {}
            if self.hosts:
                for hostname in self.hosts:
                    host = inventory.get_host(hostname)
                    if not host:
                        raise CommandError(
                            'Host (%s) not found. ' % hostname)
                inventory_filter['deploy_hosts'] = self.hosts
            elif self.groups:
                for groupname in self.groups:
                    group = inventory.get_group(groupname)
                    if not group:
                        raise CommandError(
                            'Group (%s) not found. ' % groupname)
                inventory_filter['deploy_groups'] = self.groups

            inventory_path = inventory.create_json_gen_file(inventory_filter)
            inventory_string = '-i ' + inventory_path
            cmd = (command_string + ' ' + inventory_string)

            if self.include_globals:
                globals_string = self._get_globals_path()
                cmd = (cmd + ' ' + globals_string)

            if self.include_passwords:
                password_string = self._get_password_path()
                cmd = (cmd + ' ' + password_string)

            cmd = (cmd + ' ' + self.playbook_path)

            if self.extra_vars or self.serial:
                extra_vars = ''
                if self.extra_vars:
                    extra_vars = self.extra_vars
                    if self.serial:
                        extra_vars += ' '
                if self.serial:
                    extra_vars += 'serial_var=1'

                cmd = (cmd + ' --extra-vars \"' +
                       extra_vars + '\"')

            if self.services:
                service_string = ''
                first = True
                for service in self.services:
                    valid_service = inventory.get_service(service)
                    if not valid_service:
                        raise CommandError(
                            'Service (%s) not found. ' % service)
                    if not first:
                        service_string = service_string + ','
                    else:
                        first = False
                    service_string = service_string + service
                cmd = (cmd + ' --tags ' + service_string)

            if self.flush_cache:
                cmd = (cmd + ' --flush-cache')

            if self.verbose_level > 1:
                # log the ansible command
                self.log.debug('cmd:' + cmd)

                if self.verbose_level > 2:
                    # log the inventory
                    dbg_gen = inventory_path
                    (inv, _) = \
                        subprocess.Popen(dbg_gen.split(' '),
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE).communicate()
                    self.log.debug(inv)

            err_msg, output = run_cmd(cmd, self.print_output)
            if err_msg:
                if not self.print_output:
                    # since the user didn't see the output, include it in
                    # the error message
                    err_msg = '%s %s' % (err_msg, output)
                raise CommandError(err_msg)

            self.log.info('Success')
        except CommandError as e:
            raise e
        except Exception:
            raise Exception(traceback.format_exc())
        finally:
            if inventory_path:
                os.remove(inventory_path)
Esempio n. 11
0
    def run(self):
        globals_string = None
        password_string = None
        inventory_path = None
        cmd = ''
        try:
            flag = ''
            # verbose levels: 1=not verbose, 2=more verbose
            if self.verbose_level > 1:
                flag = '-vvv'

            admin_user = get_admin_user()
            command_string = ('/usr/bin/sudo -u %s ansible-playbook %s' %
                              (admin_user, flag))
            inventory = Inventory.load()
            inventory_filter = {}
            if self.hosts:
                for hostname in self.hosts:
                    host = inventory.get_host(hostname)
                    if not host:
                        raise CommandError('Host (%s) not found. ' % hostname)
                inventory_filter['deploy_hosts'] = self.hosts
            elif self.groups:
                for groupname in self.groups:
                    group = inventory.get_group(groupname)
                    if not group:
                        raise CommandError('Group (%s) not found. ' %
                                           groupname)
                inventory_filter['deploy_groups'] = self.groups

            inventory_path = inventory.create_json_gen_file(inventory_filter)
            inventory_string = '-i ' + inventory_path
            cmd = (command_string + ' ' + inventory_string)

            if self.include_globals:
                globals_string = self._get_globals_path()
                cmd = (cmd + ' ' + globals_string)

            if self.include_passwords:
                password_string = self._get_password_path()
                cmd = (cmd + ' ' + password_string)

            cmd = (cmd + ' ' + self.playbook_path)

            if self.extra_vars or self.serial:
                extra_vars = ''
                if self.extra_vars:
                    extra_vars = self.extra_vars
                    if self.serial:
                        extra_vars += ' '
                if self.serial:
                    extra_vars += 'serial_var=1'

                cmd = (cmd + ' --extra-vars \"' + extra_vars + '\"')

            if self.services:
                service_string = ''
                first = True
                for service in self.services:
                    valid_service = inventory.get_service(service)
                    if not valid_service:
                        raise CommandError('Service (%s) not found. ' %
                                           service)
                    if not first:
                        service_string = service_string + ','
                    else:
                        first = False
                    service_string = service_string + service
                cmd = (cmd + ' --tags ' + service_string)

            if self.flush_cache:
                cmd = (cmd + ' --flush-cache')

            if self.verbose_level > 1:
                # log the ansible command
                self.log.debug('cmd:' + cmd)

                if self.verbose_level > 2:
                    # log the inventory
                    dbg_gen = inventory_path
                    (inv, _) = \
                        subprocess.Popen(dbg_gen.split(' '),
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE).communicate()
                    self.log.debug(inv)

            err_msg, output = run_cmd(cmd, self.print_output)
            if err_msg:
                if not self.print_output:
                    # since the user didn't see the output, include it in
                    # the error message
                    err_msg = '%s %s' % (err_msg, output)
                raise CommandError(err_msg)

            self.log.info('Success')
        except CommandError as e:
            raise e
        except Exception:
            raise Exception(traceback.format_exc())
        finally:
            if inventory_path:
                os.remove(inventory_path)