Future pave()

in packages/fuchsia_ctl/lib/src/image_paver.dart [56:138]


  Future<OperationResult> pave(
    String imageTgzPath,
    String deviceName, {
    String publicKeyPath,
    bool verbose = true,
    Duration timeoutMs = defaultPaveTimeoutMs,
  }) async {
    assert(imageTgzPath != null);
    if (deviceName == null) {
      stderr.writeln('Warning: No device name specified. '
          'If multiple devices are attached, this may result in paving '
          'an unexpected device.');
    }
    final SshKeyManager sshKeyManager = sshKeyManagerProvider(
      processManager: processManager,
      publicKeyPath: publicKeyPath,
      fs: fs,
    );
    final String uuid = const Uuid().v4();
    final Directory imageDirectory = fs.directory('image_$uuid');
    if (verbose) {
      stdout.writeln('Using ${imageDirectory.path} as temp path.');
    }
    await imageDirectory.create();
    final OperationResult untarResult = await tar.untar(
      imageTgzPath,
      imageDirectory.path,
    );

    if (!untarResult.success) {
      if (verbose) {
        stderr.writeln('Unpacking image $imageTgzPath failed.');
      }
      imageDirectory.deleteSync(recursive: true);
      return untarResult;
    }

    final OperationResult sshResult = await sshKeyManager.createKeys();
    if (!sshResult.success) {
      if (verbose) {
        stderr.writeln('Creating SSH Keys failed.');
      }
      imageDirectory.deleteSync(recursive: true);
      return sshResult;
    }
    final Process paveProcess = await processManager.start(
      <String>[
        '${imageDirectory.path}/pave.sh',
        '--fail-fast',
        '-1', // pave once and exit
        '--allow-zedboot-version-mismatch',
        if (deviceName != null) ...<String>['-n', deviceName],
        '--authorized-keys', '.ssh/authorized_keys',
      ],
    ).timeout(timeoutMs);
    final StringBuffer paveStdout = StringBuffer();
    final StringBuffer paveStderr = StringBuffer();
    paveProcess.stdout.transform(utf8.decoder).forEach((String s) {
      if (verbose) {
        stdout.write(s);
      }
      paveStdout.write(s);
    });
    paveProcess.stderr.transform(utf8.decoder).forEach((String s) {
      if (verbose) {
        stderr.write(s);
      }
      paveStderr.write(s);
    });
    final int exitCode = await paveProcess.exitCode;
    await stdout.flush();
    await stderr.flush();
    imageDirectory.deleteSync(recursive: true);

    return OperationResult.fromProcessResult(
      ProcessResult(
        paveProcess.pid,
        exitCode,
        paveStdout.toString(),
        paveStderr.toString(),
      ),
    );
  }