예제 #1
0
def allow_root_login(params):
    '''allow root ssh login'''
    _, host, user, ssh_key = connection_cache_key(params)
    if user == 'root':
        # user root --- nothing to do
        logger.debug('user already root for %s', brief(params))
        return params

    from textwrap import dedent
    command = dedent(r'''
        sudo cp -af /home/%s/.ssh/authorized_keys /root/.ssh/authorized_keys && \
        sudo chown root.root /root/.ssh/authorized_keys && \
        sudo restorecon -Rv /root/.ssh && \
        echo SUCCESS
    ''' % user)

    # Exceptions cause retries, save for ExpectFailed
    with connection_ctx(host, user, ssh_key) as con:
        save_bash_history(con)
        try:
            Expect.ping_pong(con, command, '(?s).*\r\nSUCCESS\r\n.*')
        except ExpectFailed as err:
            # retry
            raise EAgain(err)

    # update user to root
    params['ssh']['user'] = '******'
    return params
예제 #2
0
파일: stage.py 프로젝트: alexxa/dva
def global_setup_script(params):
    """
    Custom Setup stage of testing
    @param params: testing parameters
    @type params:  dict
    """
    hostname = params['hostname']
    script = None
    try:
        script = params['global_setup_script']
        logger.debug('%s %s got global setup script: %s', brief(params), hostname, script)
    except KeyError as err:
        logger.debug('%s no global setup script', brief(params))
    if script is None:
        # nothing to do
        return params

    script = os.path.expandvars(os.path.expanduser(script))
    remote_script = '/tmp/' + basename(script)

    script_timeout = params.get('global_setup_script_timeout', DEFAULT_GLOBAL_SETUP_SCRIPT_TIMEOUT)

    con = get_connection(params)
    logger.debug('%s: got connection', hostname)
    con.sftp.put(script, remote_script)
    logger.debug('%s sftp succeeded %s -> %s', hostname, script, remote_script)
    con.sftp.chmod(remote_script, 0700)
    logger.debug('%s chmod succeeded 0700 %s', hostname, remote_script)
    Expect.ping_pong(con, '%s && echo SUCCESS' % remote_script, '\r\nSUCCESS\r\n', timeout=script_timeout)
    logger.debug('%s set up script finished %s', hostname, remote_script)

    return params
예제 #3
0
파일: stage.py 프로젝트: alexxa/dva
def allow_root_login(params):
    '''allow root ssh login'''
    _, host, user, ssh_key = connection_cache_key(params)
    if user  == 'root':
        # user root --- nothing to do
        logger.debug('user already root for %s', brief(params))
        return params

    from textwrap import dedent
    command = dedent(r'''
        sudo cp -af /home/%s/.ssh/authorized_keys /root/.ssh/authorized_keys && \
        sudo chown root.root /root/.ssh/authorized_keys && \
        sudo restorecon -Rv /root/.ssh && \
        echo SUCCESS
    ''' % user)

    # Exceptions cause retries, save for ExpectFailed
    with connection_ctx(host, user, ssh_key) as con:
        save_bash_history(con)
        try:
            Expect.ping_pong(con, command, '(?s).*\r\nSUCCESS\r\n.*')
        except ExpectFailed as err:
            # retry
            raise EAgain(err)

    # update user to root
    params['ssh']['user'] = '******'
    return params
예제 #4
0
def global_setup_script(params):
    """
    Custom Setup stage of testing
    @param params: testing parameters
    @type params:  dict
    """
    hostname = params['hostname']
    script = None
    try:
        script = params['global_setup_script']
        logger.debug('%s %s got global setup script: %s', brief(params), hostname, script)
    except KeyError as err:
        logger.debug('%s no global setup script', brief(params))
    if script is None:
        # nothing to do
        return params

    script = os.path.expandvars(os.path.expanduser(script))
    remote_script = '/tmp/' + os.path.basename(script)

    script_timeout = params.get('global_setup_script_timeout', DEFAULT_GLOBAL_SETUP_SCRIPT_TIMEOUT)

    con = get_connection(params)
    logger.debug('%s: got connection', hostname)
    con.sftp.put(script, remote_script)
    logger.debug('%s sftp succeeded %s -> %s', hostname, script, remote_script)
    con.sftp.chmod(remote_script, 0700)
    logger.debug('%s chmod succeeded 0700 %s', hostname, remote_script)
    Expect.ping_pong(con, '%s && echo SUCCESS' % remote_script, '\r\nSUCCESS\r\n', timeout=script_timeout)
    logger.debug('%s set up script finished %s', hostname, remote_script)

    return params
예제 #5
0
def allow_root_login(params):
    '''allow root ssh login'''
    _, host, user, ssh_key = connection_cache_key(params)
    if user  == 'root':
        # user root --- nothing to do
        logger.debug('user already root for %s', brief(params))
        return params

    from textwrap import dedent
    # FIXME: copying the skel items explicitly so paramiko minds the prompt
    # rhel/fedora atomic issue: http://ask.projectatomic.io/en/question/141/root-home-missing-usual-skel-items/
    command = dedent(r'''
        sudo cp -af /home/%s/.ssh/authorized_keys /root/.ssh/authorized_keys && \
        sudo chown root.root /root/.ssh/authorized_keys && \
        sudo restorecon -Rv /root/.ssh && \
        sudo cp -f /etc/skel/.bash* ~root/ && \
        echo SUCCESS
    ''' % user)

    # Exceptions cause retries, save for ExpectFailed
    with connection_ctx(host, user, ssh_key) as con:
        save_bash_history(con)
        try:
            Expect.ping_pong(con, command, '(?s).*\r\nSUCCESS\r\n.*')
        except ExpectFailed as err:
            # retry
            raise EAgain(err)

    # update user to root
    params['ssh']['user'] = '******'
    return params
예제 #6
0
파일: testcase.py 프로젝트: dparalen/dva
 def ping_pong(self, connection, command, expectation, timeout=10):
     """ Expect.ping_pong wrapper """
     result = {"command": command, "expectation": expectation}
     try:
         Expect.ping_pong(connection, command, expectation, timeout)
         result["result"] = "passed"
     except ExpectFailed, err:
         result["result"] = "failed"
         result["actual"] = err.message
예제 #7
0
파일: testcase.py 프로젝트: alexxa/dva
 def ping_pong(self, connection, command, expectation, timeout=10):
     """ Expect.ping_pong wrapper """
     result = {"command": command, "expectation": expectation}
     try:
         Expect.ping_pong(connection, command, expectation, timeout)
         result["result"] = "passed"
     except ExpectFailed, err:
         result["result"] = "failed"
         result["actual"] = err.message
예제 #8
0
파일: cache.py 프로젝트: dparalen/dva
def assert_connection(connection):
    '''assert a connection is alive; wel... ;) at a point in time'''
    try:
        logger.debug('asserting connection: %s', connection)
        Expect.ping_pong(connection, 'uname', 'Linux')
        logger.debug('asserting connection: %s passed', connection)
    except (IOError, socket.error, socket.timeout, StitchesConnectionException, ExpectFailed, \
            paramiko.ssh_exception.AuthenticationException, paramiko.ssh_exception.SSHException) as err:
        logger.debug('asserting %s got: %s(%s): %s', connection, type(err), err, traceback.format_exc())
        raise EAgain(err)
예제 #9
0
파일: testcase.py 프로젝트: alexxa/dva
 def match(self, connection, command, regexp, grouplist=[1], timeout=10):
     """ Expect.match wrapper """
     try:
         Expect.enter(connection, command)
         result = Expect.match(connection, regexp, grouplist, timeout)
         self.log.append({"result": "passed", "match": regexp.pattern, "command": command, "value": str(result)})
         return result
     except ExpectFailed, err:
         self.log.append({"result": "failed", "match": regexp.pattern, "command": command, "actual": err.message})
         return None
예제 #10
0
파일: testcase.py 프로젝트: alexxa/dva
 def get_result(self, connection, command, timeout=10):
     """ Expect.match wrapper """
     try:
         Expect.enter(connection, "echo '###START###'; " + command + "; echo '###END###'")
         regexp = re.compile(".*\r\n###START###\r\n(.*)\r\n###END###\r\n.*", re.DOTALL)
         result = Expect.match(connection, regexp, [1], timeout)
         self.log.append({"result": "passed", "command": command, "value": result[0]})
         return result[0]
     except ExpectFailed, err:
         self.log.append({"result": "failed", "command": command, "actual": err.message})
         return None
예제 #11
0
파일: testcase.py 프로젝트: dparalen/dva
 def match(self, connection, command, regexp, grouplist=[1], timeout=10):
     """ Expect.match wrapper """
     try:
         Expect.enter(connection, command)
         result = Expect.match(connection, regexp, grouplist, timeout)
         self.log.append({
             "result": "passed",
             "match": regexp.pattern,
             "command": command,
             "value": str(result)
         })
         return result
     except ExpectFailed, err:
         self.log.append({
             "result": "failed",
             "match": regexp.pattern,
             "command": command,
             "actual": err.message
         })
         return None
예제 #12
0
파일: testcase.py 프로젝트: dparalen/dva
 def get_result(self, connection, command, timeout=10):
     """ Expect.match wrapper """
     try:
         Expect.enter(
             connection,
             "echo '###START###'; " + command + "; echo '###END###'")
         regexp = re.compile(".*\r\n###START###\r\n(.*)\r\n###END###\r\n.*",
                             re.DOTALL)
         result = Expect.match(connection, regexp, [1], timeout)
         self.log.append({
             "result": "passed",
             "command": command,
             "value": result[0]
         })
         return result[0]
     except ExpectFailed, err:
         self.log.append({
             "result": "failed",
             "command": command,
             "actual": err.message
         })
         return None
예제 #13
0
def save_bash_history(connection):
    '''prevent dva messing with bash history by saving any original history in a separate file'''
    # save old hist, but just once i.e. do not copy history if old hist file already exist
    Expect.ping_pong(connection, '[ -f ~/.bash_history -a ! -f %s ] && cp -f ~/.bash_history %s ; touch %s ; echo "###DONE###"' % \
            (OLD_BASH_HISTORY_FILE, OLD_BASH_HISTORY_FILE, OLD_BASH_HISTORY_FILE), '(?s).*\r\n###DONE###\r\n.*', 10)
예제 #14
0
파일: test.py 프로젝트: liangxiao1/dva
def reboot_instance(params):
    '''call a reboot'''
    with alive_connection(params) as connection:
        Expect.expect_retval(connection, 'nohup sleep 1s && nohup reboot &')
    time.sleep(10)
예제 #15
0
파일: test.py 프로젝트: RedHatQE/dva
def reboot_instance(params):
    '''call a reboot'''
    with alive_connection(params) as connection:
        Expect.expect_retval(connection, 'nohup sleep 1s && nohup reboot &')
    time.sleep(10)
예제 #16
0
파일: stage.py 프로젝트: alexxa/dva
def save_bash_history(connection):
    '''prevent dva messing with bash history by saving any original history in a separate file'''
    # save old hist, but just once i.e. do not copy history if old hist file already exist
    Expect.ping_pong(connection, '[ -f ~/.bash_history -a ! -f %s ] && cp -f ~/.bash_history %s ; touch %s ; echo "###DONE###"' % \
            (OLD_BASH_HISTORY_FILE, OLD_BASH_HISTORY_FILE, OLD_BASH_HISTORY_FILE), '(?s).*\r\n###DONE###\r\n.*', 10)