Ejemplo n.º 1
0
def clone(c, test_results, experiment, login="", passs=""):

    if test_results["latest"] != "OK":
        test_results = record_no_run(test_results, "clone")
        return test_results

    repo = experiment["repo"]
    branch = experiment["branch"]

    result = c.run("mkdir test_today", warn=True, echo=True)
    if result.failed:
        c.run("rm -rf test_today; mkdir test_today", warn=True, echo=True)

    responder_user = Responder(pattern=r"Username for", response=f"{login}\n",)
    responder_pass = Responder(pattern=r"Password for", response=f"{passs}\n",)

    result = c.run(
        f"module load git; cd test_today;\
                git clone -b {branch} {repo}",
        echo=True,
        pty=True,
        watchers=[responder_user, responder_pass],
    )

    test_results = record_result(test_results, "clone", result)
    test_results["latest"] = "OK"

    return test_results
Ejemplo n.º 2
0
def create_user(c):
    c.user = '******'
    user = c.config.user
    password_responder = Responder(
        pattern=r'.*new UNIX password.*',
        response=f'{c.config.password}\n',
    )
    press_ENTER_responder = Responder(
        pattern=r'Full Name|Room number|Work Phone|Home Phone|Other|correct?',
        response='\n\n',  # for some reason one '\n' is not enough
    )
    c.run(f'adduser {user}', watchers=[
        password_responder,
        press_ENTER_responder,
    ])
    c.run(f'adduser {user} sudo')

    # ssh stuff
    c.run(f'mkdir /home/{user}/.ssh')
    copy_authorized_keys(c)

    c.put(c.config.deploy.ssh_host_key, f'/home/{user}/.ssh/id_rsa')
    c.put(f'{c.config.deploy.ssh_host_key}.pub',  # just in case
          f'/home/{user}/.ssh/id_rsa.pub')

    c.run(f'mkdir -p /home/{user}/sites/{c.host}')

    c.run(f'chown -R {user}:{user} /home/{user}/')
Ejemplo n.º 3
0
    def patterns_span_multiple_lines(self):
        r = Responder(pattern=r'call.*problem', response='So sorry')
        output = """
You only call me
when you have a problem
You never call me
Just to say hi
"""
        eq_(list(r.submit(output)), ['So sorry'])
Ejemplo n.º 4
0
    def patterns_span_multiple_lines(self):
        r = Responder(pattern=r"call.*problem", response="So sorry")
        output = """
You only call me
when you have a problem
You never call me
Just to say hi
"""
        assert list(r.submit(output)) == ["So sorry"]
Ejemplo n.º 5
0
def _get_github_auth_responders():
    username_responder = Responder(
        pattern="Username for 'https://github.com':",
        response='{}\n'.format(github_username))
    password_responder = Responder(
        pattern="Password for 'https://{}@github.com':".format(
            github_username),
        response='{}\n'.format(github_password))
    return [username_responder, password_responder]
Ejemplo n.º 6
0
    def patterns_span_multiple_lines(self):
        r = Responder(pattern=r'call.*problem', response='So sorry')
        output = """
You only call me
when you have a problem
You never call me
Just to say hi
"""
        eq_(list(r.submit(output)), ['So sorry'])
Ejemplo n.º 7
0
def auto_pick(c):
    boy = Responder(
        pattern=r"boy",
        response="Tom\n"
    )
    girl = Responder(
        pattern=r"girl",
        response="Alice\n"
    )
    c.run("echo boy; read answer; echo $answer; echo girl; read answer; echo $answer", watchers=[boy, girl])
    c.run("echo girl; read answer; echo $answer; echo boy; read answer; echo $answer", watchers=[boy, girl])
Ejemplo n.º 8
0
def _get_github_auth_responders():
    """返回Github用户密码填充器"""
    username_responder = Responder(
        pattern="Username for 'https://github.com':",
        response='{}\n'.format("test")
    )
    password_responder = Responder(
        pattern="Password for 'https://github.com':",
        response='{}\n'.format("test")
    )
    return [username_responder, password_responder]
Ejemplo n.º 9
0
 def both_streams(self):
     watchers = [
         Responder("standard out", "with it\n"),
         Responder("standard error", "between chair and keyboard\n"),
     ]
     run(
         "python -u respond_both.py",
         watchers=watchers,
         hide=True,
         timeout=5,
     )
Ejemplo n.º 10
0
def _get_github_auth_responders(c):
    """
    返回 GitHub 用户名密码自动填充器
    """
    username_responder = Responder(
        pattern="Username for 'https://github.com':",
        response='{}\n'.format(c.github_username))
    password_responder = Responder(
        pattern="Password for 'https://{}@github.com':".format(
            c.github_username),
        response='{}\n'.format(c.github_password))
    return [username_responder, password_responder]
Ejemplo n.º 11
0
def _get_github_auth_responders():
    """
    返回 GitHub 用户名密码自动填充器
    """
    username_responder = Responder(
        pattern="Username for 'https://github.com':",
        response=f'{github_username}\n'
    )
    password_responder = Responder(
        pattern=f"Password for 'https://{github_username}@github.com':",
        response=f'{github_password}\n'
    )
    return [username_responder, password_responder]
Ejemplo n.º 12
0
 def _create_responder(self, su=False):
     if su:
         # Responder with SU credentials
         self.responder = Responder(
             pattern=r'Password:'******'{}\n'.format(self.device.root_password),
         )
         return self.responder
     else:
         # responder requires sudo password
         self.responder = Responder(
             pattern=r'\[sudo\] password for *',
             response='{}\n'.format(self.device.password),
         )
         return self.responder
Ejemplo n.º 13
0
def passd(i):
    sudopass = Responder(
        pattern=
        r'\[sudo\] password for sasuke:',  #terminal output pattern matching
        response='helloworld\n',
    )  #password
    i.run('sudo apt update', pty=True, watchers=[sudopass])
Ejemplo n.º 14
0
 def execute_ssh_cmd(self, cmd):
     self.logger.debug("ssh cmd: %s ", cmd)
     try:
         with Connection(
                 self.host,
                 user=self.ssh_user,
                 connect_kwargs={
                     #"key_filename": self.ssh_key,
                     "password": self.ssh_pass,
                 }) as c:
             sudopass = Responder(pattern=r'\[sudo\] password for ' +
                                  self.ssh_user + ':',
                                  response=self.sudo_pass + '\n')
             result = c.run(cmd,
                            pty=True,
                            watchers=[sudopass],
                            warn=True,
                            hide=True)
             if result and result.exited == 0 and result.ok:
                 self.logger.debug('SSH executed fine. %s for node: %s',
                                   cmd, self.host)
                 return True
             else:
                 self.logger.debug('%s', result)
                 return result.stdout.strip()
     except IOError as e:
         self.logger.error("Cannot connect to node: %s via network. %s",
                           self.host, e)
         return None
     except ValueError as e:
         self.logger.error("Wrong parameter for fabric lib. %s", e)
         return None
     except Exception as e:
         self.logger.error('Command get exception: %s', e)
         return None
Ejemplo n.º 15
0
 def clean_chain_data(self):
     try:
         with Connection(
                 self.host,
                 user=self.ssh_user,
                 connect_kwargs={
                     #"key_filename": self.ssh_key,
                     "password": self.ssh_pass
                 }) as c:
             sudopass = Responder(pattern=r'\[sudo\] password for ' +
                                  self.ssh_user + ':',
                                  response=self.sudo_pass + '\n')
             result = c.run('sudo rm -rf {}'.format(
                 DEPLOYMENT_DIR.format(self.ssh_user)),
                            pty=True,
                            watchers=[sudopass],
                            warn=True,
                            hide=True)
             if result and result.exited == 0 and result.ok:
                 self.logger.info('chain data cleaned. %s', self.host)
             else:
                 self.logger.error('chain data cleaning failed. %s',
                                   self.host)
     except Exception as e:
         self.logger.error("cannot clean chain data. %s, %s.", self.host, e)
         return False
     return True
Ejemplo n.º 16
0
 def stop_client(self):
     try:
         with Connection(
                 self.host,
                 user=self.ssh_user,
                 connect_kwargs={
                     #"key_filename": self.ssh_key,
                     "password": self.ssh_pass
                 }) as c:
             sudopass = Responder(pattern=r'\[sudo\] password for ' +
                                  self.ssh_user + ':',
                                  response=self.sudo_pass + '\n')
             #cmd = self.generate_stop_cmd()
             cmd = SYSTEMD_STOP_CLIENT
             result = c.run(cmd,
                            pty=True,
                            watchers=[sudopass],
                            warn=True,
                            hide=True)
             if result and result.exited == 0 and result.ok:
                 self.logger.info('system service stopped. %s', self.host)
                 self.client_stopped = True
                 return True
             else:
                 self.logger.error('system service stopping failed. %s',
                                   self.host)
     except Exception as e:
         self.logger.error("cannot stop client, %s, %s", self.host, e)
     return False
Ejemplo n.º 17
0
 def load_systemd_file(self):
     try:
         with Connection(
                 self.host,
                 user=self.ssh_user,
                 connect_kwargs={
                     #"key_filename": self.ssh_key,
                     "password": self.ssh_pass
                 }) as c:
             sudopass = Responder(pattern=r'\[sudo\] password for ' +
                                  self.ssh_user + ':',
                                  response=self.sudo_pass + '\n')
             src = '/home/{}/network-data/{}/autonity.service'.format(
                 self.ssh_user, self.host)
             result = c.run('sudo cp {} {}'.format(src, SYSTEM_SERVICE_DIR),
                            pty=True,
                            watchers=[sudopass],
                            warn=True,
                            hide=True)
             if result and result.exited == 0 and result.ok:
                 self.logger.info('system service loaded. %s', self.host)
             else:
                 self.logger.error('systemd file loading failed. %s',
                                   self.host)
     except Exception as e:
         self.logger.error("cannot load systemd file. %s, %s", self.host, e)
Ejemplo n.º 18
0
def deploy(c):
    password = c.config['connect_kwargs']['password'] + '\n'
    sudopass = Responder(
        pattern=r'\[sudo\] password:'******'systemctl stop gunicorn_djremo ', pty=True, watchers=[sudopass])
    with tarfile.open('djremo.tar.gz', 'w:gz') as tar:
        for root, dirs, files in os.walk('.'):
            for file in files:
                tar.add(os.path.join(root, file), filter=filter_function)

    c.run('mkdir -p temp_djremo')
    c.put('djremo.tar.gz', 'temp_djremo')
    c.local('del djremo.tar.gz')
    c.run('mkdir -p djremo')

    c.run('tar -xzvf temp_djremo/djremo.tar.gz -C djremo')
    c.run('rm -rf temp_djremo')

    c.run('''. ./env/bin/activate
cd djremo
pip install -r requirements.txt
python manage.py migrate

    ''',
          pty=True)

    c.sudo('systemctl start gunicorn_djremo ', pty=True, watchers=[sudopass])
Ejemplo n.º 19
0
def test_response(c):
    responder = Responder(
        pattern=r"Enter your license:",
        response="/home/test/Downloads/Foglight.license\n",
    )

    c.run("python3.7 target.py", watchers=[responder])
Ejemplo n.º 20
0
def import_snapshot(ctx, source_profile, snapshot_name=None):

    snapshot_file_path = download_snapshot(snapshot_name, ctx.config.aws.search_content_bucket, source_profile)
    database = ctx.config.postgres.database

    # Setup auto-responser.
    # Administrative postgres user on localhost will always be postgres (depends on POSTGRES_USER environment variable)
    postgres_password = ctx.config.secrets.postgres.password
    postgres_password_responder = Responder(pattern="Password", response=postgres_password + "\n")

    print("Importing snapshot")
    ctx.run(f"psql -h localhost -U postgres -d {database} -f {snapshot_file_path}",
            pty=True, watchers=[postgres_password_responder])

    print("creating superuser")
    admin_password = ctx.config.secrets.django.admin_password
    harvester_key = ctx.config.secrets.harvester.api_key
    insert_user = insert_django_user_statement(
        "supersurf", admin_password, harvester_key, is_search_service=True
    )
    ctx.run(
        f'psql -h localhost -U postgres -d {database} -W -c "{insert_user}"',
        echo=True,
        pty=True,
        warn=True,
        watchers=[postgres_password_responder],
    )

    print("Cleanup")
    ctx.run(f"rm {snapshot_file_path}.bak", warn=True)
Ejemplo n.º 21
0
    def createUser(ip, user, passwd, username, password, dirname=None):
        if not dirname:
            dirname = username

        responder = Responder(
            pattern=r'(Retype)?(N|n)?ew password:'******'{password}\n',
        )
        command = "useradd -m -d /home/" + dirname + "/ -g users -s /bin/bash " + username
        passwdCmd = "passwd " + username
        try:
            with Connection(host=ip,
                            user=user,
                            connect_kwargs={"password": passwd}) as conn:
                conn.sudo(command, password=passwd, hide=True).stdout.strip()
                val2 = conn.sudo(passwdCmd,
                                 password=passwd,
                                 hide=True,
                                 watchers=[responder]).exited
            if val2 == 0:
                return ('User Created: ' + username, 0)
            else:
                return ('Cannot create user', 1)
        except Exception as e:
            return ('User already Exists', 1)
Ejemplo n.º 22
0
    def unzip(self):
        """
        解压
        :return:
        """
        responder = Responder(
            pattern=r".*",
            response="A\n",
        )
        cli.info("unzip {} -d {}".format(path.join(self.zip_process_path,
                                                   self.zipfile_newname),
                                         self.zip_process_path))
        result = self.local_connection.local("unzip {} -d {}".format(path.join(self.zip_process_path,
                                                                               self.zipfile_newname),
                                                                     self.zip_process_path),
                                             pty=True)  # type:Result
        # if result.ok:
        #     cli.info("解压完成")
        # elif result.failed:
        #     cli.error("解压出错了")
        #     cli.error(result.stderr + "\n")
        #     exit(result.return_code)

        # 压缩包解压后的名字按命名规范重命名
        cli.info("mv {} {}".format(path.join(self.zip_process_path, self.zipfile_root_foldername),
                                   path.join(self.zip_process_path, self.zipfile_newname[:-4])
                                   ))
        self.local_connection.local("mv {} {}".format(path.join(self.zip_process_path, self.zipfile_root_foldername),
                                                      path.join(self.zip_process_path, self.zipfile_newname)[:-4]
                                                      ))
Ejemplo n.º 23
0
    def __init__(self,
                 sddc_server: str,
                 sddc_user: str,
                 sddc_pass: str,
                 verbose: bool = False):

        # Variables
        self.sddc_server = sddc_server
        self.sddc_user = sddc_user
        self.sddc_pass = sddc_pass
        self.verbose = verbose

        # Parameters
        self.java_store = '/etc/alternatives/jre/lib/security/cacerts'
        self.java_store_password = '******'
        self.common_store = '/etc/vmware/vcf/commonsvcs/trusted_certificates.store'
        self.common_key = '/etc/vmware/vcf/commonsvcs/trusted_certificates.key'

        # Create SDDC manager SSH connection
        self.sddc_ssh = Connection(host=self.sddc_server,
                                   user=self.sddc_user,
                                   connect_kwargs={"password": self.sddc_pass})
        self.sddc_su = Responder(pattern=r'Password:'******'{self.sddc_pass}\n')

        #  Get certificate store password
        cmd = f'cat {self.common_key}'
        result = self.__su_cmd(cmd)
        self.common_store_password = result.stdout
Ejemplo n.º 24
0
 def _command_helper(self, cmd, sudo, responder):
     try:
         if sudo:
             # CLASS_LOG.debug("sudo command")
             if responder:
                 resp = Responder(
                     pattern=r'\[sudo\] password for *',
                     response='{}\n'.format(self.device.password),
                 )
                 self.result = self.connection.sudo(
                     self.device.cmd_format(cmd), pty=True, watchers=[resp])
             else:
                 self.result = self.connection.sudo(
                     self.device.cmd_format(cmd))
         else:
             if responder:
                 resp = self._create_responder()
                 self.result = self.connection.run(
                     self.device.cmd_format(cmd), pty=True, watchers=[resp])
             else:
                 self.result = self.connection.run(
                     self.device.cmd_format(cmd))
         CLASS_LOG.debug(
             "Finished command helper. Obtained result: {}".format(
                 self.result))
     except UnexpectedExit:
         self.stderr = "Unexpected exit probably expecting a responder"
         CLASS_LOG.critical(
             "Unexpected exit probably expecting a responder")
     return self.result
Ejemplo n.º 25
0
 def deliver_package(self):
     try:
         with Connection(
                 self.host,
                 user=self.ssh_user,
                 connect_kwargs={
                     #"key_filename": self.ssh_key,
                     "password": self.ssh_pass
                 }) as c:
             sudopass = Responder(pattern=r'\[sudo\] password for ' +
                                  self.ssh_user + ':',
                                  response=self.sudo_pass + '\n')
             c.put(PACKAGE_NAME.format(self.host),
                   REMOTE_NAME.format(self.ssh_user, self.host))
             self.logger.info('Chain package was uploaded to %s.',
                              self.host)
             result = c.run('sudo tar -C /home/{} -zxvf {}'.format(
                 self.ssh_user, REMOTE_NAME.format(self.ssh_user,
                                                   self.host)),
                            pty=True,
                            watchers=[sudopass],
                            warn=True,
                            hide=True)
             if result and result.exited == 0 and result.ok:
                 self.logger.info('Chain package was unpacked to %s.',
                                  self.host)
                 return True
             else:
                 self.logger.error('chain package fail to unpacked to %s.',
                                   self.host)
     except Exception as e:
         self.logger.error("cannot deliver package to host. %s, %s",
                           self.host, e)
     return False
Ejemplo n.º 26
0
def resize(host_start, host_end, site):
    """
    Gathers sudo password, connects to host(s) and resizes the completely used 3TB of /dev/vg0/var_data. It then prints
    the final disk sizes and physical volume sizes to confirm the newly freed space.

    """
    # TODO: This kills the partitions, fix it
    sudo_pass = getpass("Env Password: "******"Do you really want to reduce vg0/var_data? \[y\/n\]:",
        response="y\n")
    for i in range(int(host_start), (host_end + 1)):
        conn = Connection(f"vm{i}.product.{site}.domain.com", config=config)
        conn.sudo("systemctl stop node_exporter.service")
        conn.sudo("mv /var/data/node_exporter /var/tmp/")
        conn.sudo("umount /var/data")
        conn.sudo("e2fsck -f /dev/vg0/var_data")
        conn.sudo("resize2fs /dev/vg0/var_data 100G")
        conn.sudo("lvreduce -L 100G /dev/vg0/var_data",
                  pty=True,
                  watchers=[confirm])
        conn.sudo("mount /var/data")
        conn.sudo("mv /var/tmp/node_exporter /var/data")
        conn.sudo("systemctl start node_exporter.service")
        print(run("df -h", shell=True), "\n", run("pvs", shell=True))

    return None
Ejemplo n.º 27
0
 def base_case(self):
     # Basic "doesn't explode" test: respond.py will exit nonzero unless
     # this works, causing a Failure.
     watcher = Responder(r"What's the password\?", "Rosebud\n")
     # Gotta give -u or Python will line-buffer its stdout, so we'll
     # never actually see the prompt.
     run("python -u respond_base.py", watchers=[watcher], hide=True)
Ejemplo n.º 28
0
def upload_archive():

    print('uploading...')
    # establishing connection to the server
    c = Connection(
        host="178.128.193.27",
        user="******",
        connect_kwargs={
            "key_filename": "D:\projects\doggo\.ssh\openSSH",
        },
    )
    # uploading the zip to myproject directory
    c.put(zipName, 'myproject/')

    print('exctracting...')
    # unziping and overwriting the files
    c.run('cd myproject && unzip -o %s' % zipName)

    # removing the archive
    c.run('cd myproject && rm %s' % zipName)

    # does server restart correctly?
    # sudo -S -p '[sudo] password: '******'\[sudo\] password:'******'1992\n',
    )
    # does not work : c.config.sudo.password('1992')
    print('restarting...')
    c.sudo('systemctl restart myproject', pty=True, watchers=[sudopass])
Ejemplo n.º 29
0
def add_PPAs(c, ppas):
    c.sudo('rm -f /etc/apt/sources.list.d/*')
    press_ENTER_responder = Responder(pattern=r'ENTER', response='\n')
    for ppa in ppas:
        c.sudo(f'add-apt-repository ppa:{ppa}',
               watchers=[press_ENTER_responder])
    c.sudo('apt-get update')
Ejemplo n.º 30
0
    def __init__(self, name, pattern=None, response=None):
        """__init__
        Under Construction.
        """

        # Override these in the parent class...
        self.name = name
        self.responder = Responder(pattern=pattern, response=response)
Ejemplo n.º 31
0
 def prompt(c):
     name = '/tmp/abc'
     responder = Responder(
         pattern=r"rm: remove directory ' + name + '? ",
         response="y\n",
     )
     c.run("mkdir -p " + name)
     c.run("rm -r " + name, watchers=[responder])
Ejemplo n.º 32
0
 def yields_response_when_regular_string_pattern_seen(self):
     r = Responder(pattern="empty", response="handed")
     assert list(r.submit("the house was empty")) == ["handed"]
Ejemplo n.º 33
0
 def multiple_hits_within_stream_yield_multiple_responses(self):
     r = Responder(pattern="jump", response="how high?")
     assert list(r.submit("jump, wait, jump, wait")) == ["how high?"] * 2
Ejemplo n.º 34
0
 def yields_response_when_regex_seen(self):
     r = Responder(pattern=r"tech.*debt", response="pay it down")
     response = r.submit("technically, it's still debt")
     assert list(response) == ["pay it down"]
Ejemplo n.º 35
0
 def yields_response_when_regular_string_pattern_seen(self):
     r = Responder(pattern='empty', response='handed')
     eq_(list(r.submit('the house was empty')), ['handed'])
Ejemplo n.º 36
0
 def multiple_hits_within_stream_yield_multiple_responses(self):
     r = Responder(pattern='jump', response='how high?')
     eq_(list(r.submit('jump, wait, jump, wait')), ['how high?'] * 2)
Ejemplo n.º 37
0
 def yields_response_when_regex_seen(self):
     r = Responder(pattern=r'tech.*debt', response='pay it down')
     eq_(list(r.submit("technically, it's still debt")), ['pay it down'])