diskimage-builder/elements/pkg-map/bin/pkg-map
Dan Prince c55539b142 Add pkg-map element.
Adds a new pkg-map element which can be used to install
packages based on an in element 'pkg-map' JSON config
file format.

As part of this change we expose DIB's IMAGE_ELEMENT variable
so that we can have access to the element names which are being
installed in a clean manner.

This change is intended to decouple elements from DIB
and allow new elements to support multiple distributions
(with various package naming schemes) without having to
constantly maintain DIB's various bin/map-packages files.

Change-Id: Ic0a061995e2ae708c95a535cba6237bff58f7d93
2014-05-28 12:21:44 -04:00

106 lines
3.2 KiB
Python
Executable File

#!/usr/bin/env python
# Copyright 2014 Red Hat Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import argparse
import json
import os
import sys
def os_family(distro):
family = None
if distro in ['centos', 'fedora', 'rhel']:
family = 'redhat'
elif distro in ['debian', 'ubuntu']:
family = 'debian'
elif distro == 'opensuse':
family = 'suse'
return family
def main():
parser = argparse.ArgumentParser(
description="Translate package name to distro specific name.")
parser.add_argument('--element', default='',
help='The element (namespace) to use for translation.')
parser.add_argument('--distro', default=os.environ.get('DISTRO_NAME'),
help='The distro name to use for translation.'
' Defaults to DISTRO_NAME')
args, extra = parser.parse_known_args()
if not args.element:
print('Please specify an --element argument.')
sys.exit(1)
if not args.distro:
print('Please specify a --distro argument or set DISTRO_NAME.')
sys.exit(1)
map_file = '/usr/share/pkg-map/%s' % args.element
if not os.path.exists(map_file):
print('Map file for %s element does not exist.' % args.element)
sys.exit(1)
with open(map_file) as fd:
package_names = json.loads(fd.read())
# Parse mapping data in this form using distro/family/default
# Most specific takes priority (distro is most specific).
# Example for Nova and Glance (using fictitious name for Fedora)
# {
# "distro": {
# "fedora": {
# "nova_package": "openstack-compute",
# "glance_package": "openstack-image"
# }
# },
# "family": {
# "redhat": {
# "nova_package": "openstack-nova",
# "glance_package": "openstack-glance"
# }
# },
# "default": {
# "nova_package": "nova",
# "glance_package": "glance"
# }
# }
name_map = package_names.get('default', {})
if 'family' in package_names:
family_map = package_names['family'].get(os_family(args.distro))
if family_map:
name_map.update(family_map)
if 'distro' in package_names:
distro_map = package_names['distro'].get(args.distro)
if distro_map:
name_map.update(distro_map)
for name in extra:
pkg_name = None
if name in name_map:
pkg_name = name_map.get(name)
if pkg_name:
print(pkg_name)
else:
err_msg = 'Missing package name for distro/element: %s/%s'
print(err_msg % (args.distro, args.element))
sys.exit(1)
sys.exit(0)
if __name__ == '__main__':
main()