|
| 1 | +""" |
| 2 | + Copyright 2014-2016 Red Hat, Inc. |
| 3 | +
|
| 4 | + This file is part of Atomic App. |
| 5 | +
|
| 6 | + Atomic App is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU Lesser General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + Atomic App is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU Lesser General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU Lesser General Public License |
| 17 | + along with Atomic App. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import print_function |
| 21 | +import os |
| 22 | + |
| 23 | +import logging |
| 24 | +import errno |
| 25 | +from constants import (INDEX_IMAGE, |
| 26 | + INDEX_LOCATION, |
| 27 | + INDEX_DEFAULT_IMAGE_LOCATION, |
| 28 | + INDEX_GEN_DEFAULT_OUTPUT_LOC, |
| 29 | + INDEX_NAME) |
| 30 | +from nulecule.container import DockerHandler |
| 31 | +from nulecule.base import Nulecule |
| 32 | + |
| 33 | +from copy import deepcopy |
| 34 | + |
| 35 | +import anymarkup |
| 36 | +from atomicapp.utils import Utils |
| 37 | + |
| 38 | +logger = logging.getLogger(__name__) |
| 39 | + |
| 40 | + |
| 41 | +class IndexException(Exception): |
| 42 | + pass |
| 43 | + |
| 44 | + |
| 45 | +class Index(object): |
| 46 | + |
| 47 | + """ |
| 48 | + This class represents the 'index' command for Atomic App. This lists |
| 49 | + all available packaged applications to use. |
| 50 | + """ |
| 51 | + |
| 52 | + index_template = {"location": ".", "nulecules": []} |
| 53 | + |
| 54 | + def __init__(self): |
| 55 | + |
| 56 | + self.index = deepcopy(self.index_template) |
| 57 | + self.index_location = os.path.join(Utils.getUserHome(), INDEX_LOCATION) |
| 58 | + self._load_index_file(self.index_location) |
| 59 | + |
| 60 | + def list(self): |
| 61 | + """ |
| 62 | + This command lists all available Nulecule packaged applications in a |
| 63 | + properly formatted way. |
| 64 | + """ |
| 65 | + |
| 66 | + # In order to "format" it correctly, find the largest length of 'name', 'id', and 'appversion' |
| 67 | + # Set a minimum length of '7' due to the length of each column name |
| 68 | + id_length = 7 |
| 69 | + app_length = 7 |
| 70 | + location_length = 7 |
| 71 | + |
| 72 | + # Loop through each 'nulecule' and retrieve the largest string length |
| 73 | + for entry in self.index["nulecules"]: |
| 74 | + id = entry.get('id') or "" |
| 75 | + version = entry['metadata'].get('appversion') or "" |
| 76 | + location = entry['metadata'].get('location') or INDEX_DEFAULT_IMAGE_LOCATION |
| 77 | + |
| 78 | + if len(id) > id_length: |
| 79 | + id_length = len(id) |
| 80 | + if len(version) > app_length: |
| 81 | + app_length = len(version) |
| 82 | + if len(location) > location_length: |
| 83 | + location_length = len(location) |
| 84 | + |
| 85 | + # Print out the "index bar" with the lengths |
| 86 | + index_format = ("{0:%s} {1:%s} {2:10} {3:%s}" % (id_length, app_length, location_length)) |
| 87 | + print(index_format.format("ID", "VER", "PROVIDERS", "LOCATION")) |
| 88 | + |
| 89 | + # Loop through each entry of the index and spit out the formatted line |
| 90 | + for entry in self.index["nulecules"]: |
| 91 | + # Get the list of providers (first letter) |
| 92 | + providers = "" |
| 93 | + for provider in entry["providers"]: |
| 94 | + providers = "%s,%s" % (providers, provider[0].capitalize()) |
| 95 | + |
| 96 | + # Remove the first element, add brackets |
| 97 | + providers = "{%s}" % providers[1:] |
| 98 | + |
| 99 | + # Retrieve the entry information |
| 100 | + id = entry.get('id') or "" |
| 101 | + version = entry['metadata'].get('appversion') or "" |
| 102 | + location = entry['metadata'].get('location') or INDEX_DEFAULT_IMAGE_LOCATION |
| 103 | + |
| 104 | + # Print out the row |
| 105 | + print(index_format.format( |
| 106 | + id, |
| 107 | + version, |
| 108 | + providers, |
| 109 | + location)) |
| 110 | + |
| 111 | + def update(self, index_image=INDEX_IMAGE): |
| 112 | + """ |
| 113 | + Fetch the latest index image and update the file based upon |
| 114 | + the INDEX_IMAGE attribute. By default, this should pull the |
| 115 | + 'official' Nulecule index. |
| 116 | + """ |
| 117 | + |
| 118 | + logger.info("Updating the index list") |
| 119 | + logger.info("Pulling latest index image...") |
| 120 | + self._fetch_index_container() |
| 121 | + logger.info("Index updated") |
| 122 | + |
| 123 | + # TODO: Error out if the locaiton does not have a Nulecule file / dir |
| 124 | + def generate(self, location, output_location=INDEX_GEN_DEFAULT_OUTPUT_LOC): |
| 125 | + """ |
| 126 | + Generate an index.yaml with a provided directory location |
| 127 | + """ |
| 128 | + logger.info("Generating index.yaml from %s" % location) |
| 129 | + self.index = deepcopy(self.index_template) |
| 130 | + |
| 131 | + if not os.path.isdir(location): |
| 132 | + raise Exception("Location must be a directory") |
| 133 | + |
| 134 | + for f in os.listdir(location): |
| 135 | + nulecule_dir = os.path.join(location, f) |
| 136 | + if f.startswith("."): |
| 137 | + continue |
| 138 | + if os.path.isdir(nulecule_dir): |
| 139 | + index_info = self._nulecule_get_info(nulecule_dir) |
| 140 | + index_info["path"] = f |
| 141 | + self.index["nulecules"].append(index_info) |
| 142 | + |
| 143 | + if len(index_info) > 0: |
| 144 | + anymarkup.serialize_file(self.index, output_location, format="yaml") |
| 145 | + logger.info("index.yaml generated") |
| 146 | + |
| 147 | + def _fetch_index_container(self, index_image=INDEX_IMAGE): |
| 148 | + """ |
| 149 | + Fetch the index container |
| 150 | + """ |
| 151 | + # Create the ".atomicapp" dir if it does not exist |
| 152 | + if not os.path.exists(os.path.dirname(self.index_location)): |
| 153 | + try: |
| 154 | + os.makedirs(os.path.dirname(self.index_location)) |
| 155 | + except OSError as exc: # Guard against race condition |
| 156 | + if exc.errno != errno.EEXIST: |
| 157 | + raise |
| 158 | + |
| 159 | + dh = DockerHandler() |
| 160 | + dh.pull(index_image) |
| 161 | + dh.extract(index_image, "/" + INDEX_NAME, self.index_location, file=True) |
| 162 | + |
| 163 | + def _load_index_file(self, index_file=INDEX_LOCATION): |
| 164 | + """ |
| 165 | + Load the index file. If it does not exist, fetch it. |
| 166 | + """ |
| 167 | + # If the file/path does not exist, retrieve the index yaml |
| 168 | + if not os.path.exists(index_file): |
| 169 | + logger.warning("Couldn't load index file: %s", index_file) |
| 170 | + logger.info("Retrieving index...") |
| 171 | + self._fetch_index_container() |
| 172 | + self.index = anymarkup.parse_file(index_file) |
| 173 | + |
| 174 | + def _nulecule_get_info(self, nulecule_dir): |
| 175 | + """ |
| 176 | + Get the required information in order to generate an index.yaml |
| 177 | + """ |
| 178 | + index_info = {} |
| 179 | + nulecule = Nulecule.load_from_path( |
| 180 | + nulecule_dir, nodeps=True) |
| 181 | + index_info["id"] = nulecule.id |
| 182 | + index_info["metadata"] = nulecule.metadata |
| 183 | + index_info["specversion"] = nulecule.specversion |
| 184 | + |
| 185 | + if len(nulecule.components) == 0: |
| 186 | + raise IndexException("Unable to load any Nulecule components from path") |
| 187 | + |
| 188 | + providers_set = set() |
| 189 | + for component in nulecule.components: |
| 190 | + if component.artifacts: |
| 191 | + if len(providers_set) == 0: |
| 192 | + providers_set = set(component.artifacts.keys()) |
| 193 | + else: |
| 194 | + providers_set = providers_set.intersection(set(component.artifacts.keys())) |
| 195 | + |
| 196 | + index_info["providers"] = list(providers_set) |
| 197 | + return index_info |
0 commit comments