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
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
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
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
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
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)
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)