|
| 1 | +#!/usr/bin/env python2.7 |
| 2 | + |
| 3 | +# Requires git, python 2.7 and python modules 'PyYAML' and 'semver' |
| 4 | +# |
| 5 | +# apt-get install git python-pip |
| 6 | +# pip install -r requirements.txt |
| 7 | + |
| 8 | +import argparse |
| 9 | +import os |
| 10 | +import re |
| 11 | +import semver |
| 12 | +import sets |
| 13 | +import subprocess |
| 14 | +import sys |
| 15 | +import uuid |
| 16 | +import yaml |
| 17 | + |
| 18 | +description = """Computes Docker images required to run each infrastructure service for a |
| 19 | +specific Rancher version. This is valuable when preparing an air-gapped Rancher |
| 20 | +installation for the latest infrastructure services without legacy image bloat. |
| 21 | +""" |
| 22 | + |
| 23 | +parser = argparse.ArgumentParser(description=description) |
| 24 | + |
| 25 | +parser.add_argument('-u', '--url', |
| 26 | + default='https://git.rancher.io/rancher-catalog', |
| 27 | + help='Rancher catalog URL accessible in airgap environment') |
| 28 | +parser.add_argument('-b', '--branch', |
| 29 | + help='Rancher catalog branch accessible in airgap environment') |
| 30 | +parser.add_argument('-v', '--version', |
| 31 | + required=True, |
| 32 | + help='Rancher Server version') |
| 33 | +args = parser.parse_args() |
| 34 | + |
| 35 | + |
| 36 | +def get_catalog_branch(version): |
| 37 | + if semver.match(version, "<=1.6.0"): |
| 38 | + return "master" |
| 39 | + elif semver.match(version, ">1.6.0") and semver.match(version, "<2.0.0"): |
| 40 | + return "v1.6-release" |
| 41 | + elif semver.match(version, ">=2.0.0"): |
| 42 | + return "v2.0-release" |
| 43 | + else: |
| 44 | + print "Unknown version" |
| 45 | + sys.exit(1) |
| 46 | + |
| 47 | + |
| 48 | +def print_keys(header, iter): |
| 49 | + temp = header |
| 50 | + for key in iter.iterkeys(): |
| 51 | + temp += " " + key |
| 52 | + print temp |
| 53 | + |
| 54 | + |
| 55 | +def optimal_version_dir(rancher_version, service_dir): |
| 56 | + # Parse each version dir's rancher-compose.yml |
| 57 | + version_dirs = {} |
| 58 | + for service_version_dir in os.listdir(service_dir): |
| 59 | + version_dir = service_dir + "/" + service_version_dir |
| 60 | + if os.path.isdir(version_dir): |
| 61 | + rancher_compose_filepath = version_dir + "/rancher-compose.yml" |
| 62 | + if os.path.isfile(rancher_compose_filepath): |
| 63 | + try: |
| 64 | + with file(rancher_compose_filepath, 'r') as f: |
| 65 | + rancher_compose = yaml.load(f) |
| 66 | + version_dirs[service_version_dir] = rancher_compose |
| 67 | + except yaml.YAMLError, exc: |
| 68 | + print "Error in rancher-compose.yml file: ", exc |
| 69 | + else: |
| 70 | + print version_dir + ": missing rancher-compose.yml" |
| 71 | + # print_keys("Unfiltered:", version_dirs) |
| 72 | + |
| 73 | + # Filter version dirs by min/max rancher version |
| 74 | + filtered = {} |
| 75 | + for key, value in version_dirs.iteritems(): |
| 76 | + if '.catalog' in value: |
| 77 | + catalog = value['.catalog'] |
| 78 | + if 'minimum_rancher_version' in catalog: |
| 79 | + min_version = catalog['minimum_rancher_version'].lstrip('v') |
| 80 | + if semver.compare(rancher_version, min_version) < 0: |
| 81 | + continue |
| 82 | + if 'maximum_rancher_version' in catalog: |
| 83 | + max_version = catalog['maximum_rancher_version'].lstrip('v') |
| 84 | + if semver.compare(rancher_version, max_version) > 0: |
| 85 | + continue |
| 86 | + filtered[key] = value |
| 87 | + # print_keys("Server Version:", filtered) |
| 88 | + |
| 89 | + # Bail out if only one remains |
| 90 | + if len(filtered) == 1: |
| 91 | + for key, value in filtered.iteritems(): |
| 92 | + return key, value['.catalog']['version'] |
| 93 | + return list(filtered)[0] |
| 94 | + |
| 95 | + # Try to return the template version in config.yml |
| 96 | + try: |
| 97 | + template_config = yaml.load(file(service_dir + "/config.yml", 'r')) |
| 98 | + if 'version' in template_config: |
| 99 | + version = template_config['version'] |
| 100 | + for key, value in filtered.iteritems(): |
| 101 | + if '.catalog' in value: |
| 102 | + catalog = value['.catalog'] |
| 103 | + if 'version' in catalog and catalog['version'] == version: |
| 104 | + return key, value['.catalog']['version'] |
| 105 | + except yaml.YAMLError, exc: |
| 106 | + print "Error in config.yml file: ", exc |
| 107 | + |
| 108 | + # Choose the highest ordinal value |
| 109 | + maxkey = -1 |
| 110 | + for key in filtered.iterkeys(): |
| 111 | + try: |
| 112 | + keyint = int(key) |
| 113 | + if keyint > maxkey: |
| 114 | + maxkey = keyint |
| 115 | + except: |
| 116 | + pass |
| 117 | + if maxkey > -1: |
| 118 | + return str(maxkey), filtered[str(maxkey)]['.catalog']['version'] |
| 119 | + else: |
| 120 | + return "", "" |
| 121 | + |
| 122 | + |
| 123 | +def version_images(service_version_dir): |
| 124 | + images = sets.Set() |
| 125 | + compose_filepath = service_version_dir + "/docker-compose.yml" |
| 126 | + compose_tpl_filepath = service_version_dir + "/docker-compose.yml.tpl" |
| 127 | + |
| 128 | + filedata = '' |
| 129 | + if os.path.isfile(compose_tpl_filepath): |
| 130 | + with open(compose_tpl_filepath, 'r') as f: |
| 131 | + filedata = f.read() |
| 132 | + filedata, subs = re.subn('{{[^}]*}}', '', filedata) |
| 133 | + elif os.path.isfile(compose_filepath): |
| 134 | + with open(compose_filepath, 'r') as f: |
| 135 | + filedata = f.read() |
| 136 | + else: |
| 137 | + print "missing docker-compose.yml[.tpl]" |
| 138 | + return images |
| 139 | + |
| 140 | + try: |
| 141 | + docker_compose = yaml.load(filedata) |
| 142 | + # handle v1/v2 docker-compose |
| 143 | + services = docker_compose |
| 144 | + if 'services' in services: |
| 145 | + services = docker_compose['services'] |
| 146 | + |
| 147 | + for serviceName in services: |
| 148 | + service = services[serviceName] |
| 149 | + if 'image' in service: |
| 150 | + images.add(service['image']) |
| 151 | + |
| 152 | + except yaml.YAMLError, exc: |
| 153 | + print "Error in docker-compose.yml file: ", exc |
| 154 | + |
| 155 | + return images |
| 156 | + |
| 157 | + |
| 158 | +version = args.version.lstrip('v') |
| 159 | +if args.branch is None: |
| 160 | + args.branch = get_catalog_branch(version) |
| 161 | + |
| 162 | +print 'Rancher Version: ' + version |
| 163 | +print 'Catalog URL: ' + args.url |
| 164 | +print 'Catalog Branch: ' + args.branch |
| 165 | +print |
| 166 | + |
| 167 | +catalog_dir = str(uuid.uuid4()) |
| 168 | +try: |
| 169 | + subprocess.check_call(["git", "clone", args.url, |
| 170 | + "--quiet", "--single-branch", "--branch", args.branch, |
| 171 | + catalog_dir]) |
| 172 | +except subprocess.CalledProcessError: |
| 173 | + sys.exit(1) |
| 174 | + |
| 175 | + |
| 176 | +infra_dir = catalog_dir + "/infra-templates" |
| 177 | +for infra_service in os.listdir(infra_dir): |
| 178 | + service_dir = infra_dir + "/" + infra_service |
| 179 | + if os.path.isdir(service_dir): |
| 180 | + version_dir, template_ver = optimal_version_dir(version, service_dir) |
| 181 | + if version_dir != "": |
| 182 | + print infra_service + ": " + template_ver |
| 183 | + for image in version_images(service_dir + "/" + version_dir): |
| 184 | + print " - " + image |
| 185 | + |
| 186 | +subprocess.call(["rm", "-rf", catalog_dir]) |
0 commit comments