Esempio n. 1
0
    def test_execute(self):
        """execute() should return a tuple with the output and the exit status code."""
        result = execute('foobar -baz')
        assert result[0] == ''
        assert result[2] == 127

        result = execute('echo foo')
        assert result[0] == 'foo\n'
        assert result[2] == 0
Esempio n. 2
0
def ping_check(fqdn, timeout=None, ip_version=4):
    """Check if the server pings."""
    command = '/usr/bin/ping'
    if ip_version == 6:
        command = '/usr/bin/ping6'

    if timeout is None:
        stdout, stderr, returncode = execute('{} -c1 -q {}'.format(command, fqdn))
    else:
        stdout, stderr, returncode = execute('{} -W{} -c1 -q {}'.format(command, timeout, fqdn))

    return returncode == 0
Esempio n. 3
0
    def _perform(self, action):
        """Common implementation for on, off and reset."""
        result = False

        password, username = self.get_credentials('ipmi')

        template = ServerConfig.objects.by_key('remotepower.ipmi.command')
        if template is None:
            return

        ipmi = {'user': username, 'password': password}

        context = Context({
            'machine': self.machine,
            'ipmi': ipmi,
            'action': action
        })

        command = Template(template).render(context)
        stdout, stderr, exitstatus = execute(command)
        result = True

        if action == 'status':
            if exitstatus == 0:
                return stdout
            else:
                return self.Status.UNKNOWN

        if exitstatus != 0:
            raise Exception(''.join(stderr))

        return result
Esempio n. 4
0
    def execute(self):
        logger.debug("Ansible scan of: %s", self.machines)
        self.render_inventory()
        command = '/usr/bin/ansible-playbook -i {dir}/inventory.yml {dir}/site.yml --private-key /home/orthos/.ssh/master'.format(dir=Ansible.facts_dir)
        stdout, stderr, returncode = execute(command)
        logger.debug("Calling: %s - %d", command, returncode)
        logger.debug("ansible: %s - %s - %s" % (stdout, stderr, returncode))
        files = self.get_json_filelist()
        missing = list(set(self.machines) - set(files))
        if missing:
            logger.warning("Cannot scan machines {0} via ansible, missing json file in {1}".format(self.machines, Ansible.facts_dir))
        success = []
        fail    = []
        for fqdn in self.machines:
            try:
                Ansible.store_machine_info(fqdn)
                success.append(fqdn)
            except Exception:
                logger.exception("Could not store ansible data of host %s", fqdn)
                fail.append(fqdn)
        logger.info("Successfully scanned via ansible: %s", success)
        if fail:
            logger.warning("Exceptions caught during scan for these hosts: %s", fail)

            def get_json_filelist(self) -> list:
        """
        Returns the list of machines for which json files have been
        created via ansible scan (.json suffix removed)
        """
        res_files = []
        for subdir, dirs, files in os.walk(Ansible.data_dir):
            for jfile in files:
                if jfile.endswith(".json"):
                    res_files.append(jfile[:-len(".json")])
        return res_files