|
| 1 | +# Copyright 2019 Extreme Networks, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import six |
| 16 | + |
| 17 | +from st2common.constants.pack import PACK_VERSION_SEPARATOR |
| 18 | +from st2common.content.utils import get_pack_base_path |
| 19 | +from st2common.runners.base_action import Action |
| 20 | +from st2common.util.pack import get_pack_metadata |
| 21 | + |
| 22 | +STACKSTORM_EXCHANGE_PACK = 'StackStorm-Exchange/stackstorm-' |
| 23 | +LOCAL_FILE_PREFIX = 'file://' |
| 24 | + |
| 25 | + |
| 26 | +class GetPackDependencies(Action): |
| 27 | + def __init__(self, config=None, action_service=None): |
| 28 | + self.dependency_list = [] |
| 29 | + self.conflict_list = [] |
| 30 | + |
| 31 | + def run(self, packs_status, nested): |
| 32 | + """ |
| 33 | + :param packs_status: Name of the pack in Exchange or a git repo URL and download status. |
| 34 | + :type: packs_status: ``dict`` |
| 35 | +
|
| 36 | + :param nested: Nested level of dependencies to prevent infinite or really |
| 37 | + long download loops. |
| 38 | + :type nested: ``integer`` |
| 39 | + """ |
| 40 | + result = {} |
| 41 | + |
| 42 | + if not packs_status or nested == 0: |
| 43 | + return result |
| 44 | + |
| 45 | + for pack, status in six.iteritems(packs_status): |
| 46 | + if 'success' not in status.lower(): |
| 47 | + continue |
| 48 | + |
| 49 | + dependency_packs = get_dependency_list(pack) |
| 50 | + if not dependency_packs: |
| 51 | + continue |
| 52 | + |
| 53 | + for dependency_pack in dependency_packs: |
| 54 | + pack_and_version = dependency_pack.split(PACK_VERSION_SEPARATOR) |
| 55 | + name_or_url = pack_and_version[0] |
| 56 | + pack_version = pack_and_version[1] if len(pack_and_version) > 1 else None |
| 57 | + |
| 58 | + if name_or_url.startswith(LOCAL_FILE_PREFIX): |
| 59 | + self.get_local_pack_dependencies(name_or_url, pack_version, dependency_pack) |
| 60 | + elif (len(name_or_url.split('/')) == 1 and STACKSTORM_EXCHANGE_PACK not in |
| 61 | + name_or_url) or STACKSTORM_EXCHANGE_PACK in name_or_url: |
| 62 | + self.get_exchange_pack_dependencies(name_or_url, pack_version, dependency_pack) |
| 63 | + else: |
| 64 | + self.get_none_exchange_pack_dependencies(name_or_url, pack_version, |
| 65 | + dependency_pack) |
| 66 | + |
| 67 | + result['dependency_list'] = self.dependency_list |
| 68 | + result['conflict_list'] = self.conflict_list |
| 69 | + result['nested'] = nested - 1 |
| 70 | + |
| 71 | + return result |
| 72 | + |
| 73 | + # For StackStorm Exchange packs. E.g. |
| 74 | + # email |
| 75 | + # https://github.com/StackStorm-Exchange/stackstorm-email |
| 76 | + # https://github.com/StackStorm-Exchange/stackstorm-email.git |
| 77 | + def get_exchange_pack_dependencies(self, name_or_url, pack_version, dependency_pack): |
| 78 | + if len(name_or_url.split('/')) == 1: |
| 79 | + pack_name = name_or_url |
| 80 | + else: |
| 81 | + name_or_git = name_or_url.split(STACKSTORM_EXCHANGE_PACK)[-1] |
| 82 | + pack_name = name_or_git if '.git' not in name_or_git else name_or_git.split('.')[0] |
| 83 | + |
| 84 | + existing_pack_version = get_pack_version(pack_name) |
| 85 | + self.check_conflict(dependency_pack, pack_version, existing_pack_version) |
| 86 | + |
| 87 | + # For None StackStorm Exchange packs. E.g. |
| 88 | + # https://github.com/EncoreTechnologies/stackstorm-freeipa.git |
| 89 | + # https://github.com/EncoreTechnologies/stackstorm-freeipa |
| 90 | + # https://github.com/EncoreTechnologies/pack.git |
| 91 | + def get_none_exchange_pack_dependencies(self, name_or_url, pack_version, dependency_pack): |
| 92 | + name_or_git = name_or_url.split('/')[-1] |
| 93 | + name = name_or_git if '.git' not in name_or_git else name_or_git.split('.')[0] |
| 94 | + pack_name = name.split('-')[-1] if "stackstorm-" in name else name |
| 95 | + |
| 96 | + existing_pack_version = get_pack_version(pack_name) |
| 97 | + self.check_conflict(dependency_pack, pack_version, existing_pack_version) |
| 98 | + |
| 99 | + # For local file. E.g |
| 100 | + # file:///opt/stackstorm/st2/lib/python3.6/site-packages/st2tests/fixtures/packs/dummy_pack_3 |
| 101 | + def get_local_pack_dependencies(self, name_or_url, pack_version, dependency_pack): |
| 102 | + pack_name = name_or_url.split("/")[-1] |
| 103 | + |
| 104 | + existing_pack_version = get_pack_version(pack_name) |
| 105 | + self.check_conflict(dependency_pack, pack_version, existing_pack_version) |
| 106 | + |
| 107 | + def check_conflict(self, pack, version, existing_version): |
| 108 | + if existing_version: |
| 109 | + existing_version = 'v' + existing_version |
| 110 | + if version and existing_version != version and pack not in self.conflict_list: |
| 111 | + self.conflict_list.append(pack) |
| 112 | + elif pack not in self.dependency_list: |
| 113 | + self.dependency_list.append(pack) |
| 114 | + |
| 115 | + |
| 116 | +def get_pack_version(pack=None): |
| 117 | + pack_path = get_pack_base_path(pack) |
| 118 | + try: |
| 119 | + pack_metadata = get_pack_metadata(pack_dir=pack_path) |
| 120 | + return pack_metadata.get('version', None) |
| 121 | + except Exception: |
| 122 | + return None |
| 123 | + |
| 124 | + |
| 125 | +def get_dependency_list(pack=None): |
| 126 | + pack_path = get_pack_base_path(pack) |
| 127 | + |
| 128 | + try: |
| 129 | + pack_metadata = get_pack_metadata(pack_dir=pack_path) |
| 130 | + return pack_metadata.get('dependencies', None) |
| 131 | + except Exception: |
| 132 | + print ('Could not open pack.yaml at location %s' % pack_path) |
| 133 | + return None |
0 commit comments