Beispiel #1
0
def removeRequiretty(vm, pk_file):
    if not vm['master']:
        logger.info("Removing requiretty to VM: " + vm['ip'])
        try:
            private_key = vm['private_key']
            if pk_file:
                private_key = pk_file
            ssh_client = SSH(vm['ip'], vm['user'], vm['passwd'], private_key,
                             vm['remote_port'])
            # Activate tty mode to avoid some problems with sudo in REL
            ssh_client.tty = True
            sudo_pass = ""
            if ssh_client.password:
                sudo_pass = "******" + ssh_client.password + "' | "
            (stdout, stderr, code) = ssh_client.execute_timeout(
                sudo_pass +
                "sudo -S sed -i 's/.*requiretty$/#Defaults requiretty/' /etc/sudoers",
                5)
            logger.debug("OUT: " + stdout + stderr)
            return code == 0
        except:
            logger.exception("Error removing requiretty to VM: " + vm['ip'])
            return False
    else:
        return True
Beispiel #2
0
def changeVMCredentials(vm, pk_file):
    if vm['os'] == "windows":
        if 'passwd' in vm and vm['passwd'] and 'new_passwd' in vm and vm[
                'new_passwd']:
            try:
                import winrm
                url = "https://" + vm['ip'] + ":5986"
                s = winrm.Session(url, auth=(vm['user'], vm['passwd']))
                r = s.run_cmd('net', ['user', vm['user'], vm['new_passwd']])

                # this part of the code is never reached ...
                if r.status_code == 0:
                    vm['passwd'] = vm['new_passwd']
                    return True
                else:
                    logger.error("Error changing password to Windows VM: " +
                                 r.std_out)
                    return False
            except winrm.exceptions.AuthenticationError:
                # if the password is correctly changed the command returns this
                # error
                try:
                    # let's check that the new password works
                    s = winrm.Session(url, auth=(vm['user'], vm['new_passwd']))
                    r = s.run_cmd('echo', ['OK'])
                    if r.status_code == 0:
                        vm['passwd'] = vm['new_passwd']
                        return True
                    else:
                        logger.error(
                            "Error changing password to Windows VM: " +
                            r.std_out)
                        return False
                except:
                    logger.exception(
                        "Error changing password to Windows VM: " + vm['ip'] +
                        ".")
                    return False
            except:
                logger.exception("Error changing password to Windows VM: " +
                                 vm['ip'] + ".")
                return False
    else:  # Linux VMs
        # Check if we must change user credentials in the VM
        if 'passwd' in vm and vm['passwd'] and 'new_passwd' in vm and vm[
                'new_passwd']:
            logger.info("Changing password to VM: " + vm['ip'])
            private_key = vm['private_key']
            if pk_file:
                private_key = pk_file
            try:
                ssh_client = SSH(vm['ip'], vm['user'], vm['passwd'],
                                 private_key, vm['remote_port'])

                sudo_pass = ""
                if ssh_client.password:
                    sudo_pass = "******" + ssh_client.password + "' | "
                (out, err, code) = ssh_client.execute(
                    sudo_pass + 'sudo -S bash -c \'echo "' + vm['user'] + ':' +
                    vm['new_passwd'] +
                    '" | /usr/sbin/chpasswd && echo "OK"\' 2> /dev/null')
            except:
                logger.exception("Error changing password to VM: " + vm['ip'] +
                                 ".")
                return False

            if code == 0:
                vm['passwd'] = vm['new_passwd']
                return True
            else:
                logger.error("Error changing password to VM: " + vm['ip'] +
                             ". " + out + err)
                return False

        if 'new_public_key' in vm and vm[
                'new_public_key'] and 'new_private_key' in vm and vm[
                    'new_private_key']:
            logger.info("Changing public key to VM: " + vm['ip'])
            private_key = vm['private_key']
            if pk_file:
                private_key = pk_file
            try:
                ssh_client = SSH(vm['ip'], vm['user'], vm['passwd'],
                                 private_key, vm['remote_port'])
                (out, err, code) = ssh_client.execute_timeout(
                    'echo ' + vm['new_public_key'] +
                    ' >> .ssh/authorized_keys', 5)
            except:
                logger.exception("Error changing public key to VM: " +
                                 vm['ip'] + ".")
                return False

            if code != 0:
                logger.error("Error changing public key to VM:: " + vm['ip'] +
                             ". " + out + err)
                return False
            else:
                vm['private_key'] = vm['new_private_key']
                return True

    return False