Skip to content
Snippets Groups Projects
Select Git revision
  • de77be4fffd8edbb5a03e5822904ee70ab7e66bb
  • python3 default protected
  • feature/exabgp_support2
  • feature/exabgp_support2.bgpextcommunity
  • feature/exabgp_support2.django4.2
  • fix/existingcheck_honor_fragtype
  • feature/python3-authz_netmask
  • feature/authz_netmask
  • fix/wrong_ratelimit_stats
  • feature/requirements_version_update2024-01
  • feature/split_celery
  • feature/improved-warning-mails
  • fix/reenable_expireset_via_restapi
  • feature/admin_user_delete_with_owned_rule_reassigning1
  • feature/admin_user_delete_with_owned_rule_reassigning
  • feature/branded_doc
  • fix/forked_snmp_polling_worker_exit_issue
  • fix/false_user_activation_error
  • feature/exabgp_with_docker-compose
  • fix/prefix_overlap_handling
  • fix/js_security_issues-a
  • save1
  • rpm-1.5-7
  • working1
  • myv1.6
  • t12b1
  • v1.5_newnew2
  • merged_final
  • v1.5_newnew
  • startstop_old
  • myadd2
  • tomas3
  • merge_jra2t6_and_RESTAPI
  • mytomas2
  • mynew1
  • new_jra2t6
  • v1.5_final
  • fod16_ruleroutes-merged_old
  • merged_new
  • v1.6_new_old
  • v1.5_new_old_follower
41 results

_version.py

Blame
  • install-sdist-to-test.py 2.44 KiB
    from getpass import getpass
    import os
    import logging
    import threading
    
    import click
    from paramiko import SSHClient
    from scp import SCPClient
    
    DEFAULT_HOSTNAMES = [
        'test-inventory-provider01.geant.org',
        'test-inventory-provider02.geant.org'
    ]
    
    
    def _install_proc(hostname, username, password, sdist):
    
        logging.info(f'installing on {hostname}')
    
        scp_destination = f'/tmp/{os.path.basename(sdist)}'
    
        ssh = SSHClient()
        ssh.load_system_host_keys()
        ssh.connect(hostname=hostname, username=username, password=password)
        scp = SCPClient(ssh.get_transport())
        scp.put(sdist, scp_destination)
    
        channel = ssh.invoke_shell()
        stdin = channel.makefile('wb')
        stdout = channel.makefile('rb')
    
        pip = '/home/inventory/venv/bin/pip'
        commands = [
            f'sudo su - -c \'{pip} uninstall -y inventory\'',
            f'sudo su - -c \'{pip} install {scp_destination}\'',
            f'rm {scp_destination}',
            'sudo su - -c \'chown -R inventory.inventory /home/inventory/venv\'',
            'exit'
        ]
        stdin.write('\n'.join(commands) + '\n')
        print('\n'.join(commands))
        print(stdout.read())
        stdout.close()
        stdin.close()
    
        processes = [
            'inventory-provider.service',
            'inventory-worker.service',
            'inventory-monitor.service'
        ]
        restart_cmd = 'sudo su - -c \'systemctl restart ' \
            + ' '.join(processes) + '\''
        stdin, stdout, _ = ssh.exec_command(restart_cmd)
        output = stdout.read()
        status = stdout.channel.recv_exit_status()
        logging.debug(f'status: {status}, output: {output}')
    
        ssh.close()
    
        logging.info(f'finished installing on {hostname}')
    
    
    @click.command()
    @click.option(
        "--hostname",
        multiple=True,
        default=DEFAULT_HOSTNAMES,
        type=click.STRING,
        help="hostname [%r]"
             % str(DEFAULT_HOSTNAMES))
    @click.option(
        "--user",
        default=None,
        type=click.STRING,
        help="ssh username")
    @click.option(
        "--sdist",
        required=True,
        type=click.Path(exists=True, dir_okay=False),
        help="sdist filename")
    def cli(hostname, user, sdist):
        password = getpass(prompt='Password: ', stream=None)
    
        def _make_thread(h):
            t = threading.Thread(
                target=_install_proc,
                args=[h, user, password, sdist])
            t.start()
            return t
    
        threads = [_make_thread(h) for h in hostname]
    
        for t in threads:
            t.join()
    
    
    if __name__ == '__main__':
        logging.basicConfig(level=logging.INFO)
        cli()