Exemplo n.º 1
0
def main(argv):
  parser = _GetParser()
  opts = parser.parse_args(argv)
  opts.Freeze()

  if opts.dry_run:
    logging.getLogger().setLevel(logging.DEBUG)

  if not os.path.isfile(opts.source_image):
    raise UploadGceImageRuntimError('%s is not a valid file.')

  source_dir, source_image_name = os.path.split(opts.source_image)
  with osutils.TempDir() as tempdir:
    logging.info('Generating tarball from %s', opts.source_image)
    tarball_name = commands.BuildGceTarball(tempdir, source_dir,
                                            source_image_name)
    # We must generate a uuid when uploading the tarball because repeated
    # uploads are likely to be named similarly. We'll just use tempdir to keep
    # files separate.
    temp_tarball_dir = os.path.join(opts.temp_gcs_path,
                                    os.path.basename(tempdir))
    gs_context = gs.GSContext(init_boto=True, retries=5, acl='private',
                              dry_run=opts.dry_run)
    gc_context = gcloud.GCContext(opts.project, dry_run=opts.dry_run)
    try:
      logging.info('Uploading tarball %s to %s',
                   tarball_name, temp_tarball_dir)
      gs_context.CopyInto(os.path.join(tempdir, tarball_name), temp_tarball_dir)
      logging.info('Creating image %s', opts.target_name)
      gc_context.CreateImage(opts.target_name,
                             source_uri=os.path.join(temp_tarball_dir,
                                                     tarball_name))
    except:
      logging.error('Oops! Something went wonky.')
      logging.error('Trying to clean up temporary artifacts...')
      try:
        with cros_build_lib.OutputCapturer() as output_capturer:
          gc_context.ListImages()
        if opts.target_name in ''.join(output_capturer.GetStdoutLines()):
          logging.info('Removing image %s', opts.target_name)
          gc_context.DeleteImage(opts.target_name, quiet=True)
      except gcloud.GCContextException:
        # Gobble up this error so external error is visible.
        logging.error('Failed to clean up image %s', opts.target_name)

      raise
    finally:
      logging.info('Removing GS tempdir %s', temp_tarball_dir)
      gs_context.Remove(temp_tarball_dir, ignore_missing=True)

  logging.info('All done!')
  def testGceTarballGeneration(self):
    """Verifies BuildGceTarball produces correct archives"""
    image_dir = os.path.join(self.tempdir, 'inputs')
    archive_dir = os.path.join(self.tempdir, 'outputs')
    image = constants.TEST_IMAGE_BIN
    output = constants.TEST_IMAGE_GCE_TAR

    osutils.SafeMakedirs(image_dir)
    osutils.SafeMakedirs(archive_dir)
    osutils.Touch(os.path.join(image_dir, image))

    output_tar = commands.BuildGceTarball(archive_dir, image_dir, image)
    self.assertEquals(output, output_tar)

    output_path = os.path.join(archive_dir, output_tar)
    self.assertExists(output_path)

    # GCE expects the tarball to be in a particular format.
    cros_test_lib.VerifyTarball(output_path, ['disk.raw'])
Exemplo n.º 3
0
    def _BuildGceTarballs(self):
        """Creates .tar.gz files that can be converted to GCE images.

    These files will be used by VMTestStage for tests on GCE. They will also be
    be uploaded to GCS buckets, where they can be used as input to the "gcloud
    compute images create" command. This will convert them into images that can
    be used to create GCE VM instances.
    """
        if self._run.config.run_gce_tests:
            image_bins = []
            if 'base' in self._run.config['images']:
                image_bins.append(constants.IMAGE_TYPE_TO_NAME['base'])
            if 'test' in self._run.config['images']:
                image_bins.append(constants.IMAGE_TYPE_TO_NAME['test'])

            image_dir = self.GetImageDirSymlink('latest')
            for image_bin in image_bins:
                if os.path.exists(os.path.join(image_dir, image_bin)):
                    commands.BuildGceTarball(image_dir, image_dir, image_bin)
                else:
                    logging.warning('Missing image file skipped: %s',
                                    image_bin)