def run_translate(g: guestfs.GuestFS):
    if (g.exists('/etc/redhat-release')
            and 'Red Hat' in g.cat('/etc/redhat-release')):
        distro = Distro.RHEL
    else:
        distro = Distro.CENTOS

    use_rhel_gce_license = utils.GetMetadataAttribute('use_rhel_gce_license')
    el_release = utils.GetMetadataAttribute('el_release')
    install_gce = utils.GetMetadataAttribute('install_gce_packages')
    spec = TranslateSpec(g=g,
                         use_rhel_gce_license=use_rhel_gce_license == 'true',
                         distro=distro,
                         el_release=el_release,
                         install_gce=install_gce == 'true')

    try:
        DistroSpecific(spec)
    except BaseException as raised:
        logging.debug('Translation failed due to: {}'.format(raised))
        for check in checks:
            msg = check(spec)
            if msg:
                raise RuntimeError('{} {}'.format(msg, raised)) from raised
        raise raised
def install_cloud_sdk(g: guestfs.GuestFS, ubuntu_release: str) -> None:
    """ Installs Google Cloud SDK, supporting apt and snap.

  Args:
    g: A mounted GuestFS instance.
    ubuntu_release: The release nickname (eg: trusty).
  """
    try:
        run(g, 'gcloud --version')
        logging.info(
            'Found gcloud. Skipping installation of Google Cloud SDK.')
        return
    except RuntimeError:
        logging.info('Did not find previous install of gcloud.')

    if g.gcp_image_major < '18':
        g.write('/etc/apt/sources.list.d/partner.list',
                apt_cloud_sdk.format(ubuntu_release=ubuntu_release))
        utils.update_apt(g)
        utils.install_apt_packages(g, 'google-cloud-sdk')
        logging.info('Installed Google Cloud SDK with apt.')
        return

    # Starting at 18.04, Canonical installs the sdk using snap.
    # Running `snap install` directly is not an option here since it
    # requires the snapd daemon to be running on the guest.
    g.write('/etc/cloud/cloud.cfg.d/91-google-cloud-sdk.cfg',
            cloud_init_cloud_sdk)
    logging.info(
        'Google Cloud SDK will be installed using snap with cloud-init.')

    # Include /snap/bin in the PATH for startup and shutdown scripts.
    # This was present in the old guest agent, but lost in the new guest
    # agent.
    for p in [
            '/lib/systemd/system/google-shutdown-scripts.service',
            '/lib/systemd/system/google-startup-scripts.service'
    ]:
        logging.debug('[%s] Checking whether /bin/snap is on PATH.', p)
        if not g.exists(p):
            logging.debug('[%s] Skipping: Unit not found.', p)
            continue
        original_unit = g.cat(p)
        # Check whether the PATH is already set; if so, skip patching to avoid
        # overwriting existing directive.
        match = re.search('Environment=[\'"]?PATH.*',
                          original_unit,
                          flags=re.IGNORECASE)
        if match:
            logging.debug(
                '[%s] Skipping: PATH already defined in unit file: %s.', p,
                match.group())
            continue
        # Add Environment directive to unit file, and show diff in debug log.
        patched_unit = original_unit.replace('[Service]', snap_env_directive)
        g.write(p, patched_unit)
        diff = '\n'.join(Differ().compare(original_unit.splitlines(),
                                          patched_unit.splitlines()))
        logging.debug('[%s] PATH not defined. Added:\n%s', p, diff)
Beispiel #3
0
def _update_grub(g: guestfs.GuestFS):
  """Update and rebuild grub to ensure the image is bootable on GCP.
  See https://cloud.google.com/compute/docs/import/import-existing-image
  """
  g.write('/etc/default/grub', configs.update_grub_conf(
      g.cat('/etc/default/grub'),
      GRUB_CMDLINE_LINUX_DEFAULT='console=ttyS0,38400n8',
      GRUB_CMDLINE_LINUX='',
  ))
  g.command(['grub2-mkconfig', '-o', '/boot/grub2/grub.cfg'])
Beispiel #4
0
def _stage_etc_hosts(g: guestfs.GuestFS, dst: Path):
    """Stage copy of guest's /etc/hosts at dst with metadata server added."""
    original = ''
    if g.exists('/etc/hosts'):
        original = g.cat('/etc/hosts')

    metadata_host_line = None
    with open('/etc/hosts') as worker_etc_hosts:
        for line in worker_etc_hosts:
            if 'metadata.google.internal' in line:
                metadata_host_line = line
                break
    if not metadata_host_line:
        raise AssertionError(
            'Did not find metadata host in worker\'s /etc/hosts')

    dst.write_text('{}\n{}\n'.format(original, metadata_host_line))
Beispiel #5
0
def _remove_existing_subscriptions(g: guestfs.GuestFS):
    """Remove existing subscriptions from guest.

  Implements steps from https://www.suse.com/support/kb/doc/?id=000019085
  """
    for cmd in [
            'rm -f /etc/SUSEConnect',
            'rm -f /etc/zypp/{repos,services,credentials}.d/*'
    ]:
        g.sh(cmd)

    updated = []
    skip = False
    for line in g.cat('/etc/hosts').splitlines():
        if skip:
            skip = False
        elif '# Added by SMT reg' in line:
            log.info('Removing previous SMT host from /etc/hosts')
            skip = True
        else:
            updated.append(line)
    g.write('/etc/hosts', '\n'.join(updated))
Beispiel #6
0
def DistroSpecific(g: guestfs.GuestFS):
    el_release = utils.GetMetadataAttribute('el_release')
    install_gce = utils.GetMetadataAttribute('install_gce_packages')
    rhel_license = utils.GetMetadataAttribute('use_rhel_gce_license')

    # This must be performed prior to making network calls from the guest.
    # Otherwise, if /etc/resolv.conf is present, and has an immutable attribute,
    # guestfs will fail with:
    #
    #   rename: /sysroot/etc/resolv.conf to
    #     /sysroot/etc/i9r7obu6: Operation not permitted
    utils.common.ClearEtcResolv(g)

    # Some imported images haven't contained `/etc/yum.repos.d`.
    if not g.exists('/etc/yum.repos.d'):
        g.mkdir('/etc/yum.repos.d')

    if rhel_license == 'true':
        if 'Red Hat' in g.cat('/etc/redhat-release'):
            g.command(['yum', 'remove', '-y', '*rhui*'])
            logging.info('Adding in GCE RHUI package.')
            g.write('/etc/yum.repos.d/google-cloud.repo',
                    repo_compute % el_release)
            yum_install(g, 'google-rhui-client-rhel' + el_release)

    if install_gce == 'true':
        logging.info('Installing GCE packages.')
        g.write('/etc/yum.repos.d/google-cloud.repo',
                repo_compute % el_release)
        if el_release == '6':
            if 'CentOS' in g.cat('/etc/redhat-release'):
                logging.info('Installing CentOS SCL.')
                g.command(['rm', '-f', '/etc/yum.repos.d/CentOS-SCL.repo'])
                yum_install(g, 'centos-release-scl')
            # Install Google Cloud SDK from the upstream tar and create links for the
            # python27 SCL environment.
            logging.info('Installing python27 from SCL.')
            yum_install(g, 'python27')
            logging.info('Installing Google Cloud SDK from tar.')
            sdk_base_url = 'https://dl.google.com/dl/cloudsdk/channels/rapid'
            sdk_base_tar = '%s/google-cloud-sdk.tar.gz' % sdk_base_url
            tar = utils.HttpGet(sdk_base_tar)
            g.write('/tmp/google-cloud-sdk.tar.gz', tar)
            g.command(
                ['tar', 'xzf', '/tmp/google-cloud-sdk.tar.gz', '-C', '/tmp'])
            sdk_version = g.cat('/tmp/google-cloud-sdk/VERSION').strip()

            logging.info('Getting Cloud SDK Version %s', sdk_version)
            sdk_version_tar = 'google-cloud-sdk-%s-linux-x86_64.tar.gz' % sdk_version
            sdk_version_tar_url = '%s/downloads/%s' % (sdk_base_url,
                                                       sdk_version_tar)
            logging.info('Getting versioned Cloud SDK tar file from %s',
                         sdk_version_tar_url)
            tar = utils.HttpGet(sdk_version_tar_url)
            sdk_version_tar_file = os.path.join('/tmp', sdk_version_tar)
            g.write(sdk_version_tar_file, tar)
            g.mkdir_p('/usr/local/share/google')
            g.command([
                'tar', 'xzf', sdk_version_tar_file, '-C',
                '/usr/local/share/google', '--no-same-owner'
            ])

            logging.info('Creating CloudSDK SCL symlinks.')
            sdk_bin_path = '/usr/local/share/google/google-cloud-sdk/bin'
            g.ln_s(os.path.join(sdk_bin_path, 'git-credential-gcloud.sh'),
                   os.path.join('/usr/bin', 'git-credential-gcloud.sh'))
            for binary in ['bq', 'gcloud', 'gsutil']:
                binary_path = os.path.join(sdk_bin_path, binary)
                new_bin_path = os.path.join('/usr/bin', binary)
                bin_str = '#!/bin/bash\nsource /opt/rh/python27/enable\n%s $@' % \
                    binary_path
                g.write(new_bin_path, bin_str)
                g.chmod(0o755, new_bin_path)
        else:
            g.write_append('/etc/yum.repos.d/google-cloud.repo',
                           repo_sdk % el_release)
            yum_install(g, 'google-cloud-sdk')
        yum_install(g, 'google-compute-engine', 'google-osconfig-agent')

    logging.info('Updating initramfs')
    for kver in g.ls('/lib/modules'):
        # Although each directory in /lib/modules typically corresponds to a
        # kernel version  [1], that may not always be true.
        # kernel-abi-whitelists, for example, creates extra directories in
        # /lib/modules.
        #
        # Skip building initramfs if the directory doesn't look like a
        # kernel version. Emulates the version matching from depmod [2].
        #
        # 1. https://tldp.org/LDP/Linux-Filesystem-Hierarchy/html/lib.html
        # 2. https://kernel.googlesource.com/pub/scm/linux/kernel/git/mmarek/kmod
        # /+/tip/tools/depmod.c#2537
        if not re.match(r'^\d+.\d+', kver):
            logging.debug(
                '/lib/modules/{} doesn\'t look like a kernel directory. '
                'Skipping creation of initramfs for it'.format(kver))
            continue
        if not g.exists(os.path.join('/lib/modules', kver, 'modules.dep')):
            try:
                g.command(['depmod', kver])
            except RuntimeError as e:
                logging.info(
                    'Failed to write initramfs for {kver}. If image fails to '
                    'boot, verify that depmod /lib/modules/{kver} runs on '
                    'the original machine'.format(kver=kver))
                logging.debug('depmod error: {}'.format(e))
                continue
        if el_release == '6':
            # Version 6 doesn't have option --kver
            g.command(['dracut', '-v', '-f', kver])
        else:
            g.command(['dracut', '--stdlog=1', '-f', '--kver', kver])

    logging.info('Update grub configuration')
    if el_release == '6':
        # Version 6 doesn't have grub2, file grub.conf needs to be updated by hand
        g.write('/tmp/grub_gce_generated', grub_cfg)
        g.sh(r'grep -P "^[\t ]*initrd|^[\t ]*root|^[\t ]*kernel|^[\t ]*title" '
             r'/boot/grub/grub.conf >> /tmp/grub_gce_generated;'
             r'sed -i "s/console=ttyS0[^ ]*//g" /tmp/grub_gce_generated;'
             r'sed -i "/^[\t ]*kernel/s/$/ console=ttyS0,38400n8/" '
             r'/tmp/grub_gce_generated;'
             r'mv /tmp/grub_gce_generated /boot/grub/grub.conf')
    else:
        g.write('/etc/default/grub', grub2_cfg)
        g.command(['grub2-mkconfig', '-o', '/boot/grub2/grub.cfg'])

    # Reset network for DHCP.
    logging.info('Resetting network to DHCP for eth0.')
    # Remove NetworkManager-config-server if it's present. The package configures
    # NetworkManager to *not* use DHCP.
    #  https://access.redhat.com/solutions/894763
    g.command(['yum', 'remove', '-y', 'NetworkManager-config-server'])
    g.write('/etc/sysconfig/network-scripts/ifcfg-eth0', ifcfg_eth0)
def DistroSpecific(g: guestfs.GuestFS):
  el_release = utils.GetMetadataAttribute('el_release')
  install_gce = utils.GetMetadataAttribute('install_gce_packages')
  rhel_license = utils.GetMetadataAttribute('use_rhel_gce_license')

  # This must be performed prior to making network calls from the guest.
  # Otherwise, if /etc/resolv.conf is present, and has an immutable attribute,
  # guestfs will fail with:
  #
  #   rename: /sysroot/etc/resolv.conf to
  #     /sysroot/etc/i9r7obu6: Operation not permitted
  utils.common.ClearEtcResolv(g)

  # Some imported images haven't contained `/etc/yum.repos.d`.
  if not g.exists('/etc/yum.repos.d'):
    g.mkdir('/etc/yum.repos.d')

  if rhel_license == 'true':
    if 'Red Hat' in g.cat('/etc/redhat-release'):
      g.command(['yum', 'remove', '-y', '*rhui*'])
      logging.info('Adding in GCE RHUI package.')
      g.write('/etc/yum.repos.d/google-cloud.repo', repo_compute % el_release)
      yum_install(g, 'google-rhui-client-rhel' + el_release)

  if install_gce == 'true':
    logging.info('Installing GCE packages.')
    g.write('/etc/yum.repos.d/google-cloud.repo', repo_compute % el_release)
    if el_release == '6':
      if 'CentOS' in g.cat('/etc/redhat-release'):
        logging.info('Installing CentOS SCL.')
        g.command(['rm', '-f', '/etc/yum.repos.d/CentOS-SCL.repo'])
        yum_install(g, 'centos-release-scl')
      # Install Google Cloud SDK from the upstream tar and create links for the
      # python27 SCL environment.
      logging.info('Installing python27 from SCL.')
      yum_install(g, 'python27')
      logging.info('Installing Google Cloud SDK from tar.')
      sdk_base_url = 'https://dl.google.com/dl/cloudsdk/channels/rapid'
      sdk_base_tar = '%s/google-cloud-sdk.tar.gz' % sdk_base_url
      tar = utils.HttpGet(sdk_base_tar)
      g.write('/tmp/google-cloud-sdk.tar.gz', tar)
      g.command(['tar', 'xzf', '/tmp/google-cloud-sdk.tar.gz', '-C', '/tmp'])
      sdk_version = g.cat('/tmp/google-cloud-sdk/VERSION').strip()

      logging.info('Getting Cloud SDK Version %s', sdk_version)
      sdk_version_tar = 'google-cloud-sdk-%s-linux-x86_64.tar.gz' % sdk_version
      sdk_version_tar_url = '%s/downloads/%s' % (sdk_base_url, sdk_version_tar)
      logging.info('Getting versioned Cloud SDK tar file from %s',
                   sdk_version_tar_url)
      tar = utils.HttpGet(sdk_version_tar_url)
      sdk_version_tar_file = os.path.join('/tmp', sdk_version_tar)
      g.write(sdk_version_tar_file, tar)
      g.mkdir_p('/usr/local/share/google')
      g.command(['tar', 'xzf', sdk_version_tar_file, '-C',
                 '/usr/local/share/google', '--no-same-owner'])

      logging.info('Creating CloudSDK SCL symlinks.')
      sdk_bin_path = '/usr/local/share/google/google-cloud-sdk/bin'
      g.ln_s(os.path.join(sdk_bin_path, 'git-credential-gcloud.sh'),
             os.path.join('/usr/bin', 'git-credential-gcloud.sh'))
      for binary in ['bq', 'gcloud', 'gsutil']:
        binary_path = os.path.join(sdk_bin_path, binary)
        new_bin_path = os.path.join('/usr/bin', binary)
        bin_str = '#!/bin/bash\nsource /opt/rh/python27/enable\n%s $@' % \
            binary_path
        g.write(new_bin_path, bin_str)
        g.chmod(0o755, new_bin_path)
    else:
      g.write_append(
          '/etc/yum.repos.d/google-cloud.repo', repo_sdk % el_release)
      yum_install(g, 'google-cloud-sdk')
    yum_install(g, 'google-compute-engine', 'google-osconfig-agent')

  logging.info('Updating initramfs')
  for kver in g.ls('/lib/modules'):
    if not g.exists(os.path.join('/lib/modules', kver, 'modules.dep')):
      g.command(['depmod', kver])
    if el_release == '6':
      # Version 6 doesn't have option --kver
      g.command(['dracut', '-v', '-f', kver])
    else:
      g.command(['dracut', '--stdlog=1', '-f', '--kver', kver])

  logging.info('Update grub configuration')
  if el_release == '6':
    # Version 6 doesn't have grub2, file grub.conf needs to be updated by hand
    g.write('/tmp/grub_gce_generated', grub_cfg)
    g.sh(
        r'grep -P "^[\t ]*initrd|^[\t ]*root|^[\t ]*kernel|^[\t ]*title" '
        r'/boot/grub/grub.conf >> /tmp/grub_gce_generated;'
        r'sed -i "s/console=ttyS0[^ ]*//g" /tmp/grub_gce_generated;'
        r'sed -i "/^[\t ]*kernel/s/$/ console=ttyS0,38400n8/" '
        r'/tmp/grub_gce_generated;'
        r'mv /tmp/grub_gce_generated /boot/grub/grub.conf')
  else:
    g.write('/etc/default/grub', grub2_cfg)
    g.command(['grub2-mkconfig', '-o', '/boot/grub2/grub.cfg'])

  # Reset network for DHCP.
  logging.info('Resetting network to DHCP for eth0.')
  # Remove NetworkManager-config-server if it's present. The package configures
  # NetworkManager to *not* use DHCP.
  #  https://access.redhat.com/solutions/894763
  g.command(['yum', 'remove', '-y', 'NetworkManager-config-server'])
  g.write('/etc/sysconfig/network-scripts/ifcfg-eth0', ifcfg_eth0)