in daisy_workflows/image_build/install_package/installpackage.py [0:0]
def main():
image = utils.GetMetadataAttribute('image', raise_on_not_found=True)
package = utils.GetMetadataAttribute('gcs_package_path',
raise_on_not_found=True)
package_name = package.split('/')[-1]
mount_disk = get_mount_disk(image)
logging.info('Mount device %s at /mnt', mount_disk)
run(f'mount {mount_disk} /mnt')
# The rpm utility requires /dev/random to initialize GnuTLS
logging.info('Mount dev filesystem in chroot')
run('mount -o bind /dev /mnt/dev')
# Enable DNS resolution in the chroot, for fetching dependencies.
if os.path.isfile('/mnt/etc/resolv.conf'):
os.rename('/mnt/etc/resolv.conf', '/mnt/etc/resolv.conf.bak')
utils.WriteFile('/mnt/etc/resolv.conf', utils.ReadFile('/etc/resolv.conf'))
utils.DownloadFile(package, f'/mnt/tmp/{package_name}')
distribution = get_distro_from_image(image)
if distribution == 'debian':
install_cmd = 'apt install -y '
elif distribution == 'enterprise_linux':
install_cmd = 'dnf install -y'
else:
raise Exception('Unknown Linux distribution.')
logging.info('Installing package %s', package_name)
run(f'chroot /mnt {install_cmd} /tmp/{package_name}')
if distribution == 'enterprise_linux':
run('chroot /mnt /sbin/setfiles -v -F '
'/etc/selinux/targeted/contexts/files/file_contexts /')
os.remove('/mnt/etc/resolv.conf')
# Restore resolv.conf if necessary
if os.path.isfile('/mnt/etc/resolv.conf.bak'):
os.rename('/mnt/etc/resolv.conf.bak', '/mnt/etc/resolv.conf')
# Best effort to unmount prior to shutdown.
run('sync', check=False)
run('umount /mnt/dev', check=False)
run('umount /mnt', check=False)
logging.success('Package %s installed successfully', package_name)