|
| 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 | +from st2common.constants.pack import PACK_VERSION_SEPARATOR |
| 16 | +from st2common.runners.base_action import Action |
| 17 | +from st2common.util.pack import get_pack_metadata |
| 18 | +from st2common.util.pack_management import get_repo_url |
| 19 | + |
| 20 | + |
| 21 | +class TesetInstalledPackVersionAction(Action): |
| 22 | + def run(self, installed_pack): |
| 23 | + """ |
| 24 | + :param installed_pack: Installed pack name with version |
| 25 | + :type: installed_pack: ``string`` |
| 26 | + """ |
| 27 | + |
| 28 | + if not installed_pack: |
| 29 | + return False, False |
| 30 | + |
| 31 | + pack_and_version = installed_pack.split(PACK_VERSION_SEPARATOR) |
| 32 | + pack_name = pack_and_version[0] |
| 33 | + pack_version = pack_and_version[1] if len(pack_and_version) > 1 else None |
| 34 | + |
| 35 | + # Pack version is not specified. Get pack version from index.json file. |
| 36 | + if not pack_version: |
| 37 | + try: |
| 38 | + _, pack_version = get_repo_url(pack_name, proxy_config=None) |
| 39 | + except Exception: |
| 40 | + print ('No record of the "%s" pack in the index.' % (pack_name)) |
| 41 | + return False, False |
| 42 | + |
| 43 | + # Get installed pack version from local pack metadata file. |
| 44 | + try: |
| 45 | + pack_dir = '/opt/stackstorm/packs/%s/' % (pack_name) |
| 46 | + pack_metadata = get_pack_metadata(pack_dir=pack_dir) |
| 47 | + local_pack_version = pack_metadata.get('version', None) |
| 48 | + except Exception: |
| 49 | + print ('Could not open pack.yaml file at location %s' % (pack_dir)) |
| 50 | + return False, False |
| 51 | + |
| 52 | + if pack_version == local_pack_version: |
| 53 | + return True, True |
| 54 | + else: |
| 55 | + return False, False |
0 commit comments