예제 #1
0
def setup_cloud_init(g: guestfs.GuestFS):
    """ Install cloud-init if not present, and configure to the cloud provider.

  Args:
    g: A mounted GuestFS instance.
  """
    a = Apt(run)
    curr_version = a.get_package_version(g, 'cloud-init')
    available_versions = a.list_available_versions(g, 'cloud-init')
    # Try to avoid installing 21.3-1, which conflicts which the guest agent.
    # On first boot, systemd reaches a deadlock deadlock and doest start
    # its unit. If a version other than 21.3-1 isn't explicitly found, *and*
    # cloud-init isn't currently installed, then this allows apt to pick the
    # version to install.
    version_to_install = Apt.determine_version_to_install(
        curr_version, available_versions, {'21.3-1'})
    pkg_to_install = ''
    if version_to_install:
        pkg_to_install = 'cloud-init=' + version_to_install
    elif curr_version == '':
        pkg_to_install = 'cloud-init'
    # If this block doesn't execute, it means that cloud-init was found
    # on the system, but there wasn't an upgrade candidate. Therefore
    # leave the version that's currently installed.
    if pkg_to_install:
        logging.info(pkg_to_install)
        utils.install_apt_packages(g, pkg_to_install)
    # Ubuntu 14.04's version of cloud-init doesn't have `clean`.
    if g.gcp_image_major > '14':
        run(g, 'cloud-init clean')
    # Remove cloud-init configs that may conflict with GCE's.
    #
    # - subiquity disables automatic network configuration
    #     https://bugs.launchpad.net/ubuntu/+source/cloud-init/+bug/1871975
    for cfg in [
            'azure', 'curtin', 'waagent', 'walinuxagent', 'aws', 'amazon',
            'subiquity'
    ]:
        run(g, 'rm -f /etc/cloud/cloud.cfg.d/*%s*' % cfg)
    g.write('/etc/cloud/cloud.cfg.d/91-gce-system.cfg', cloud_init_config)
예제 #2
0
 def test_return_empty_when_nothing_newer(self):
   assert Apt.determine_version_to_install(
     '2', {'1.1'}, set()) == ''
예제 #3
0
 def test_blocklist_uses_prefix_matching(self):
   assert Apt.determine_version_to_install(
     '1', {'2.1', '2.2'}, {'2'}) == ''
예제 #4
0
 def test_dont_return_blocked(self):
   assert Apt.determine_version_to_install(
     '1', {'3', '2', '1'}, {'3'}) == '2'
예제 #5
0
 def test_allow_current_version_empty(self):
   assert Apt.determine_version_to_install(
     '', {'3', '2', '1'}, {}) == '3'
예제 #6
0
 def test_return_greatest_match(self):
   assert Apt.determine_version_to_install(
     '1', {'3', '2', '1'}, {}) == '3'