def osci_install():
    args = get_args_or_die(
        parse_osci_install_args,
        issues_for_osci_install_args)

    env = OSCIInstallEnv(
        args.osci_repo, args.osci_branch, args.swift_api_key, args.image_name,
        args.vote)

    with remote.connect(args.username, args.host, args.port) as connection:
        connection.put(args.gerrit_key, 'gerrit.key')
        connection.put(
            data.install_script('osci_rewrite_config.sh'),
            'osci_rewrite_config.sh'
        )
        connection.put(
            data.install_script('osci_installscript.sh'),
            'osci_installscript.sh'
        )
        connection.put(
            data.install_script('functions.sh'),
            'functions.sh'
        )
        connection.run(
            '%s bash osci_installscript.sh' % env.bashline
        )
        connection.run(
            'rm -f gerrit.key osci_installscript.sh osci_rewrite_config.sh'
            ' functions.sh')
def nodepool_rewrite_config():
    args = get_args_or_die(
        parse_nodepool_rewrite_config_args,
        issues_for_nodepool_rewrite_config
    )

    env = NodepoolConfigEnv(
        get_params_or_die(args.openrc),
        args.image_name,
        args.min_ready,
        args.rackspace_password,
        args.key_name,
        iad_max=args.iad_max,
        ord_max=args.ord_max,
        dfw_max=args.dfw_max
    )
    nodepool_config_file = data.nodepool_config(env.as_dict())

    with remote.connect(args.username, args.host, args.port) as connection:
        connection.put(
            data.install_script('nodepool_rewrite_config.sh'),
            'nodepool_rewrite_config.sh'
        )
        connection.put(
            nodepool_config_file,
            'nodepool.yaml'
        )

        connection.run('bash nodepool_rewrite_config.sh')
        connection.run('rm -f nodepool_rewrite_config.sh')
        connection.run('rm -f nodepool.yaml')
def restore():
    args = get_args_or_die(parse_restore_args, issues_for_restore_args)

    with remote.connect(args.username, args.host, args.port) as connection:
        connection.put(data.install_script('restore.sh'), 'restore.sh')
        connection.put(args.dump_file, 'osci-backup.tgz')
        connection.run('bash restore.sh')
        connection.run('rm -f restore.sh osci-backup.tgz')
def backup():
    args = get_args_or_die(parse_backup_args, issues_for_backup_args)

    with remote.connect(args.username, args.host, args.port) as connection:
        connection.put(data.install_script('backup.sh'), 'backup.sh')
        connection.run('bash backup.sh')
        connection.get('osci-backup.tgz', args.output)
        connection.run('rm -f backup.sh osci-backup.tgz')
def nodepool_update():
    args = get_args_or_die(parse_update_args, issues_for_update_args)

    env = NodepoolInstallEnv(args.nodepool_repo, args.nodepool_branch)

    with remote.connect(args.username, args.host, args.port) as connection:
        connection.put(data.install_script('update.sh'), 'update.sh')
        connection.put(data.install_script('functions.sh'), 'functions.sh')
        connection.run('%s bash update.sh' % env.bashline)
        connection.run('rm -f update.sh functions.sh')
def nodepool_configure():
    args = get_args_or_die(
        _parse_nodepool_configure_args,
        _issues_for_nodepool_configure_args
    )

    env = NodepoolConfigEnv(
        get_params_or_die(args.openrc),
        args.image_name,
        args.min_ready,
        args.rackspace_password,
        args.key_name,
        iad_max=args.iad_max,
        ord_max=args.ord_max,
        dfw_max=args.dfw_max
    )
    env.project_config_branch = args.project_config_branch
    nodepool_config_file = data.nodepool_config(env.as_dict())

    with remote.connect(args.username, args.host, args.port) as connection:
        connection.put(
            data.install_script('nodepool_rewrite_config.sh'),
            'nodepool_rewrite_config.sh'
        )
        connection.put(
            data.install_script('nodepool_config.sh'),
            'nodepool_config.sh'
        )
        connection.put(
            data.install_script('functions.sh'),
            'functions.sh'
        )
        connection.put(
            nodepool_config_file,
            'nodepool.yaml'
        )

        connection.put(
            args.nodepool_keyfile,
            'nodepool.priv'
        )

        connection.put(
            args.jenkins_keyfile,
            'jenkins.priv'
        )

        connection.run('%s bash nodepool_config.sh' % env.bashline)

        connection.run('rm -f nodepool_config.sh')
        connection.run('rm -f nodepool.yaml')
        connection.run('rm -f nodepool.priv')
        connection.run('rm -f jenkins.priv')
        connection.run('rm -f functions.sh')
        connection.run('rm -f nodepool_rewrite_config.sh')
def nodepool_upload_keys():
    args = get_args_or_die(
        _parse_nodepool_upload_keys_args,
        _issues_for_nodepool_upload_keys_args
    )

    env = NodepoolConfigEnv(
        get_params_or_die(args.openrc),
        'ignored',
        'ignored',
        'ignored',
        args.key_name,
    )
    nodepool_config_file = data.nodepool_config(env.as_dict())
    nova_commands = NovaCommands(env)

    regions = image_provider_regions()

    with remote.connect(args.username, args.host, args.port) as connection:
        key_exists_in_regions = []
        for region in regions:
            result = connection.run(
                nova_commands.keypair_show(region, env.key_name),
                ignore_failures=True,
            )
            if result.succeeded:
                key_exists_in_regions.append(region)

        if key_exists_in_regions and not args.remove:
            raise SystemExit(
                'Keypair "{keypair}" already exists at regions: {regions}'
                ' Please remove them manually or use --remove'.format(
                    keypair=env.key_name,
                    regions=','.join(key_exists_in_regions)
                )
            )

        if args.remove:
            for region in key_exists_in_regions:
                connection.run(
                    nova_commands.keypair_delete(region, env.key_name)
                )

        for region in regions:
            result = connection.run(
                nova_commands.keypair_add(
                    region,
                    env.key_name,
                    '{home}/.ssh/id_rsa.pub'.format(home=env.home))
            )
def osci_upload_control():
    args = get_args_or_die(parse_osci_status_report_args, system_access_issues)

    with remote.connect(args.username, args.host, args.port) as connection:
        connection.quiet = True
        if args.action == STATUS_REPORT_QUERY:
            check_status_upload_is_disabled =  connection.run(
                "test -e %s" % STATUS_REPORT_DISABLE_FILE, ignore_failures=True)
            if check_status_upload_is_disabled.succeeded:
                print "DISABLED"
            else:
                print "ENABLED"
        elif args.action == STATUS_REPORT_ENABLE:
            connection.sudo("rm -f %s" % STATUS_REPORT_DISABLE_FILE)
        elif args.action == STATUS_REPORT_DISABLE:
            connection.sudo("touch %s" % STATUS_REPORT_DISABLE_FILE)
def ci_status():
    args = get_args_or_die(
        parse_ci_status_args,
        system_access_issues)

    with remote.connect(args.username, args.host, args.port) as connection:
        connection.quiet = True
        for service in service_names(args.service):
            result = connection.run(
                'service %s status' % service,
                ignore_failures=True
            )
            if result.succeeded:
                print result
            else:
                print '%s missing' % service
def osci_rewrite_config():
    args = get_args_or_die(
        parse_osci_rewrite_args,
        issues_for_osci_rewrite_args
    )

    env = OSCIConfigEnv(
        args.swift_api_key,
        args.image_name,
        args.vote
    )

    with remote.connect(args.username, args.host, args.port) as connection:
        connection.put(
            data.install_script('osci_rewrite_config.sh'),
            'osci_rewrite_config.sh'
        )
        connection.run('%s bash osci_rewrite_config.sh' % env.bashline)
        connection.run('rm -f osci_rewrite_config.sh')
def osci_update():
    args = get_args_or_die(
        parse_osci_update_args,
        issues_for_osci_update_args)

    env = OSCIInstallEnv(
        args.osci_repo, args.osci_branch, 'IRRELEVANT', 'IRRELEVANT')

    with remote.connect(args.username, args.host, args.port) as connection:
        connection.put(
            data.install_script('osci_release.sh'),
            'osci_release.sh'
        )
        connection.put(
            data.install_script('functions.sh'),
            'functions.sh'
        )
        connection.run(
            '%s bash osci_release.sh' % env.bashline
        )
        connection.run(
            'rm -f gerrit.key osci_release.sh functions.sh')
def osci_stop():
    args = get_args_or_die(parse_osci_stop_args, system_access_issues)

    with remote.connect(args.username, args.host, args.port) as connection:
        for service in service_names(args.service):
            connection.sudo('service %s stop' % service)