Beispiel #1
0
def main(args):
    print('Connecting to remote host...')
    with SSHManager(args.hostname, USERNAME, PASSWORD) as ssh:
        args.mode = detect_bos_mode(ssh)

        backup_path = args.backup
        if not backup_path:
            mac = backup.ssh_mac(ssh)
            # try to find backup from default backup directory
            backup_path = backup.get_output_dir(mac, date=False, create=False)
            backup_path = sorted(glob('*{}*'.format(backup_path)),
                                 reverse=True)
            if not backup_path:
                # recover firmware without previous backup
                restore_firmware(args, ssh)
                return
            backup_path = backup_path[0]
            print('Found backup: {}'.format(backup_path))

        if os.path.isdir(backup_path):
            restore_firmware(args, ssh, backup_path)
        else:
            with TemporaryDirectory() as backup_dir:
                tar = tarfile.open(backup_path)
                print('Extracting backup tarball...')
                tar.extractall(path=backup_dir)
                tar.close()
                uenv_path = glob(os.path.join(backup_dir, '*', 'uEnv.txt'))
                if not uenv_path:
                    print('Invalid backup tarball!')
                    return
                backup_dir = os.path.split(uenv_path[0])[0]
                restore_firmware(args, ssh, backup_dir)
Beispiel #2
0
def main(args):
    stage3_user_path = args.post_upgrade
    stage3_builtin_path = None

    if stage3_user_path:
        check_stage3_path(stage3_user_path)
    if os.path.isdir(STAGE3_BUILTIN_DIR):
        check_stage3_path(STAGE3_BUILTIN_DIR)
        stage3_builtin_path = STAGE3_BUILTIN_DIR

    print("Connecting to remote host...")
    with SSHManager(args.hostname, USERNAME, PASSWORD) as ssh:
        # check compatibility of remote server
        check_compatibility(ssh)

        if args.backup:
            mac = backup.ssh_mac(ssh)
            backup_dir = backup.get_output_dir(mac)
            if not platform.backup_firmware(args, ssh, backup_dir, mac):
                raise UpgradeStop

        # prepare target directory
        ssh.run('rm', '-fr', TARGET_DIR)
        ssh.run('mkdir', '-p', TARGET_DIR)

        # upgrade remote system with missing utilities
        print("Preparing remote system...")
        platform.prepare_system(ssh, SYSTEM_DIR)

        # copy firmware files to the server over SFTP
        sftp = ssh.open_sftp()
        sftp.chdir(TARGET_DIR)
        print("Uploading firmware...")
        upload_local_files(sftp, SOURCE_DIR)
        if stage3_user_path or stage3_builtin_path:
            print("Uploading post-upgrade (stage3)...")
            with tempfile.TemporaryDirectory() as stage3_dir:
                stage3_file = os.path.join(stage3_dir, STAGE3_FILE)
                with tarfile.open(stage3_file, "w:gz") as stage3:
                    if stage3_builtin_path:
                        stage3.add(stage3_builtin_path, STAGE3_DIR)
                    if stage3_user_path:
                        stage3_user_exclude = None
                        if stage3_builtin_path:
                            stage3_user_script_path = os.path.join(
                                stage3_user_path, STAGE3_SCRIPT)
                            stage3.add(stage3_user_script_path,
                                       arcname="{}/{}".format(
                                           STAGE3_DIR, STAGE3_USER_SCRIPT))
                            stage3_user_exclude = lambda path: path == stage3_user_script_path
                        stage3.add(stage3_user_path,
                                   STAGE3_DIR,
                                   exclude=stage3_user_exclude)
                    stage3.close()
                    with Progress(STAGE3_FILE,
                                  os.path.getsize(stage3_file)) as progress:
                        sftp.put(stage3_file, STAGE3_FILE, callback=progress)
        sftp.close()

        # generate HW identifier for miner
        hw_id = hwid.generate()

        # get other stage1 parameters
        if args.psu_power_limit == 0:
            # 0 is special parameter for disabling autotuning
            psu_power_limit = ''
        else:
            psu_power_limit = args.psu_power_limit or 'default'

        if args.keep_hostname:
            keep_hostname = 'yes'
        elif args.no_keep_hostname:
            keep_hostname = 'no'
        else:
            # keep only user defined hostname and skip factory one (default behaviour)
            keep_hostname = 'cond'

        pool_user = args.pool_user or ''
        keep_network = 'no' if args.no_keep_network else 'yes'
        keep_pools = 'no' if args.no_keep_pools else 'yes'
        dry_run = 'yes' if args.dry_run else 'no'

        # run stage1 upgrade process
        try:
            print("Upgrading firmware...")
            stdout, _ = ssh.run(
                'cd', TARGET_DIR, '&&', 'ls', '-l', '&&',
                "/bin/sh stage1.sh '{}' '{}' '{}' '{}' '{}' '{}' '{}'".format(
                    hw_id, pool_user, psu_power_limit, keep_network,
                    keep_hostname, keep_pools, dry_run))
        except subprocess.CalledProcessError as error:
            cleanup_system(ssh)
            print()
            print("Error log:")
            for line in error.stderr.readlines():
                print(line, end='')
            raise UpgradeStop
        else:
            if args.dry_run:
                cleanup_system(ssh)
                print('Dry run of upgrade was successful!')
                raise UpgradeStop

            for line in stdout.readlines():
                print(line, end='')
            print('Upgrade was successful!')
            print('Rebooting...', end='')
            try:
                ssh.run('/sbin/reboot')
            except subprocess.CalledProcessError:
                # reboot returns exit status -1
                pass

    if args.no_wait:
        print()
        print(
            'Wait for 120 seconds before the system becomes fully operational!'
        )
    else:
        wait_for_port(args.hostname, 80, REBOOT_DELAY)
Beispiel #3
0
def main(args):
    print("Connecting to remote host...")
    with SSHManager(args.hostname, USERNAME, PASSWORD) as ssh:
        # check compatibility of remote server
        check_compatibility(ssh)

        if not args.no_backup:
            mac = backup.ssh_mac(ssh)
            backup_dir = backup.get_output_dir(BACKUP_DIR, mac)
            if not platform.backup_firmware(args, ssh, backup_dir, mac):
                raise UpgradeStop

        # prepare target directory
        ssh.run('rm', '-fr', TARGET_DIR)
        ssh.run('mkdir', '-p', TARGET_DIR)

        # upgrade remote system with missing utilities
        if not platform.prepare_system(ssh, SYSTEM_DIR):
            raise UpgradeStop

        # copy firmware files to the server over SFTP
        sftp = ssh.open_sftp()
        sftp.chdir(TARGET_DIR)
        print("Uploading firmware...")
        upload_local_files(sftp, SOURCE_DIR)
        sftp.close()

        # generate HW identifier for miner
        hw_id = hwid.generate()

        # get other stage1 parameters
        keep_network = 'no' if args.no_keep_network else 'yes'
        keep_hostname = 'yes' if args.keep_hostname else 'no'

        # run stage1 upgrade process
        try:
            print("Upgrading firmware...")
            stdout, _ = ssh.run(
                'cd', TARGET_DIR, '&&', 'ls', '-l', '&&',
                "/bin/sh stage1.sh '{}' {} {}".format(hw_id, keep_network,
                                                      keep_hostname))
        except subprocess.CalledProcessError as error:
            for line in error.stderr.readlines():
                print(line, end='')
            raise UpgradeStop
        else:
            for line in stdout.readlines():
                print(line, end='')
            print('Upgrade was successful!')
            print('Rebooting...', end='')
            try:
                ssh.run('/sbin/reboot')
            except subprocess.CalledProcessError:
                # reboot returns exit status -1
                pass

    if args.no_wait:
        print()
        print(
            'Wait for 120 seconds before the system becomes fully operational!'
        )
    else:
        wait_for_port(args.hostname, 80, REBOOT_DELAY)
Beispiel #4
0
def main(args):
    stage3_path = args.post_upgrade
    if stage3_path:
        if not os.path.isdir(stage3_path):
            print("Post-upgrade path '{}' is missing or is not a directory!".
                  format(stage3_path))
            raise UpgradeStop
        if not os.path.isfile(os.path.join(stage3_path, STAGE3_SCRIPT)):
            print("Script '{}' is missing in '{}'!".format(
                STAGE3_SCRIPT, stage3_path))
            raise UpgradeStop

    print("Connecting to remote host...")
    with SSHManager(args.hostname, USERNAME, PASSWORD) as ssh:
        # check compatibility of remote server
        check_compatibility(ssh)

        if not args.no_backup:
            mac = backup.ssh_mac(ssh)
            backup_dir = backup.get_output_dir(mac)
            if not platform.backup_firmware(args, ssh, backup_dir, mac):
                raise UpgradeStop

        # prepare target directory
        ssh.run('rm', '-fr', TARGET_DIR)
        ssh.run('mkdir', '-p', TARGET_DIR)

        # upgrade remote system with missing utilities
        print("Preparing remote system...")
        platform.prepare_system(ssh, SYSTEM_DIR)

        # copy firmware files to the server over SFTP
        sftp = ssh.open_sftp()
        sftp.chdir(TARGET_DIR)
        print("Uploading firmware...")
        upload_local_files(sftp, SOURCE_DIR)
        if stage3_path:
            print("Uploading post-upgrade (stage3)...")
            with tempfile.TemporaryDirectory() as stage3_dir:
                stage3_file = os.path.join(stage3_dir, STAGE3_FILE)
                with tarfile.open(stage3_file, "w:gz") as stage3:
                    stage3.add(stage3_path, STAGE3_DIR)
                    stage3.close()
                    with Progress(STAGE3_FILE,
                                  os.path.getsize(stage3_file)) as progress:
                        sftp.put(stage3_file, STAGE3_FILE, callback=progress)
        sftp.close()

        # generate HW identifier for miner
        hw_id = hwid.generate()

        # get other stage1 parameters
        keep_network = 'no' if args.no_keep_network else 'yes'
        keep_hostname = 'yes' if args.keep_hostname else 'no'
        dry_run = 'yes' if args.dry_run else 'no'

        # run stage1 upgrade process
        try:
            print("Upgrading firmware...")
            stdout, _ = ssh.run(
                'cd', TARGET_DIR, '&&', 'ls', '-l', '&&',
                "/bin/sh stage1.sh '{}' {} {} {}".format(
                    hw_id, keep_network, keep_hostname, dry_run))
        except subprocess.CalledProcessError as error:
            cleanup_system(ssh)
            print()
            print("Error log:")
            for line in error.stderr.readlines():
                print(line, end='')
            raise UpgradeStop
        else:
            if args.dry_run:
                cleanup_system(ssh)
                print('Dry run of upgrade was successful!')
                raise UpgradeStop

            for line in stdout.readlines():
                print(line, end='')
            print('Upgrade was successful!')
            print('Rebooting...', end='')
            try:
                ssh.run('/sbin/reboot')
            except subprocess.CalledProcessError:
                # reboot returns exit status -1
                pass

    if args.no_wait:
        print()
        print(
            'Wait for 120 seconds before the system becomes fully operational!'
        )
    else:
        wait_for_port(args.hostname, 80, REBOOT_DELAY)