Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright (c) 2016-2017 GigaSpaces Technologies Ltd. All rights reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | # |
| 16 | |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 17 | import sys |
| 18 | import logging |
| 19 | import argparse |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 20 | import os |
| 21 | import shutil |
| 22 | import tempfile |
| 23 | |
Lianhao Lu | 0af4624 | 2018-08-29 18:17:46 +0800 | [diff] [blame] | 24 | import pkg_resources |
| 25 | |
| 26 | from vnfsdk_pkgtools.packager import csar |
Lianhao Lu | 9003bbf | 2019-11-21 12:56:51 +0800 | [diff] [blame] | 27 | from vnfsdk_pkgtools.packager import manifest |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 28 | from vnfsdk_pkgtools import validator |
Lianhao Lu | 0af4624 | 2018-08-29 18:17:46 +0800 | [diff] [blame] | 29 | from vnfsdk_pkgtools import vnfreq |
| 30 | |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 31 | |
| 32 | def csar_create_func(namespace): |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 33 | |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 34 | csar.write(namespace.source, |
| 35 | namespace.entry, |
| 36 | namespace.destination, |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 37 | args=namespace) |
Lianhao Lu | 1de1b09 | 2018-08-02 16:16:24 +0800 | [diff] [blame] | 38 | |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 39 | def csar_open_func(namespace): |
| 40 | csar.read(namespace.source, |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 41 | namespace.destination, |
| 42 | namespace.no_verify_cert) |
Lianhao Lu | 1de1b09 | 2018-08-02 16:16:24 +0800 | [diff] [blame] | 43 | |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 44 | def csar_validate_func(namespace): |
| 45 | workdir = tempfile.mkdtemp() |
| 46 | try: |
Lianhao Lu | 0af4624 | 2018-08-29 18:17:46 +0800 | [diff] [blame] | 47 | err = 0 |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 48 | reader = csar.read(namespace.source, |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 49 | workdir, |
| 50 | no_verify_cert=True) |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 51 | |
| 52 | driver = validator.get_validator(namespace.parser) |
| 53 | driver.validate(reader) |
Lianhao Lu | 0af4624 | 2018-08-29 18:17:46 +0800 | [diff] [blame] | 54 | print("---Basic & HPA validation passed!---") |
| 55 | if namespace.test_reqs: |
| 56 | print("---Check VNF Requirements---") |
| 57 | err = vnfreq.check_and_print(namespace.test_reqs, |
| 58 | reader, |
| 59 | driver) |
| 60 | return err |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 61 | finally: |
| 62 | shutil.rmtree(workdir, ignore_errors=True) |
| 63 | |
| 64 | |
| 65 | def parse_args(args_list): |
| 66 | """ |
| 67 | CLI entry point |
| 68 | """ |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 69 | parser = argparse.ArgumentParser(description='VNF SDK CSAR manipulation tool') |
Lianhao Lu | 1de1b09 | 2018-08-02 16:16:24 +0800 | [diff] [blame] | 70 | parser.add_argument('-v', '--verbose', |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 71 | dest='verbosity', |
| 72 | action='count', |
| 73 | default=0, |
| 74 | help='Set verbosity level (can be passed multiple times)') |
Lianhao Lu | 1de1b09 | 2018-08-02 16:16:24 +0800 | [diff] [blame] | 75 | |
| 76 | subparsers = parser.add_subparsers(help='csar-create') |
| 77 | csar_create = subparsers.add_parser('csar-create') |
| 78 | csar_create.set_defaults(func=csar_create_func) |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 79 | csar_create.add_argument( |
| 80 | 'source', |
| 81 | help='Service template directory') |
| 82 | csar_create.add_argument( |
| 83 | 'entry', |
| 84 | help='Entry definition file relative to service template directory') |
| 85 | csar_create.add_argument( |
| 86 | '-d', '--destination', |
| 87 | help='Output CSAR zip destination', |
| 88 | required=True) |
| 89 | csar_create.add_argument( |
| 90 | '--manifest', |
| 91 | help='Manifest file relative to service template directory') |
| 92 | csar_create.add_argument( |
| 93 | '--history', |
| 94 | help='Change history file relative to service template directory') |
| 95 | csar_create.add_argument( |
| 96 | '--tests', |
| 97 | help='Directory containing test information, relative to service template directory') |
| 98 | csar_create.add_argument( |
| 99 | '--licenses', |
| 100 | help='Directory containing license information, relative to service template directory') |
Lianhao Lu | cd02d1f | 2018-03-26 13:35:22 +0800 | [diff] [blame] | 101 | csar_create.add_argument( |
| 102 | '--digest', |
Lianhao Lu | 9003bbf | 2019-11-21 12:56:51 +0800 | [diff] [blame] | 103 | choices=manifest.SUPPORTED_HASH_ALGO, |
Lianhao Lu | cd02d1f | 2018-03-26 13:35:22 +0800 | [diff] [blame] | 104 | help='If present, means to check the file deigest in manifest; compute the digest using the specified hash algorithm of all files in the csar package to be put into the manifest file') |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 105 | csar_create.add_argument( |
| 106 | '--certificate', |
| 107 | help='Certificate file for certification, relative to service template directory') |
| 108 | csar_create.add_argument( |
| 109 | '--privkey', |
| 110 | help='Private key file for certification, absoluate or relative path') |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 111 | |
| 112 | |
| 113 | csar_open = subparsers.add_parser('csar-open') |
| 114 | csar_open.set_defaults(func=csar_open_func) |
| 115 | csar_open.add_argument( |
| 116 | 'source', |
| 117 | help='CSAR file location') |
| 118 | csar_open.add_argument( |
| 119 | '-d', '--destination', |
| 120 | help='Output directory to extract the CSAR into', |
| 121 | required=True) |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 122 | csar_open.add_argument( |
| 123 | '--no-verify-cert', |
| 124 | action='store_true', |
| 125 | help="Do NOT verify the signer's certificate") |
| 126 | |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 127 | |
| 128 | csar_validate = subparsers.add_parser('csar-validate') |
| 129 | csar_validate.set_defaults(func=csar_validate_func) |
| 130 | csar_validate.add_argument( |
| 131 | 'source', |
| 132 | help='CSAR file location') |
| 133 | csar_validate.add_argument( |
| 134 | '-p', '--parser', |
Lianhao Lu | 40f7a0f | 2018-08-02 14:48:53 +0800 | [diff] [blame] | 135 | default='toscaparser', |
Lianhao Lu | 0af4624 | 2018-08-29 18:17:46 +0800 | [diff] [blame] | 136 | choices=[ep.name for ep in pkg_resources.iter_entry_points(validator.NS)], |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 137 | help='use which csar parser to validate') |
Lianhao Lu | 0af4624 | 2018-08-29 18:17:46 +0800 | [diff] [blame] | 138 | csar_validate.add_argument( |
| 139 | '--test-reqs', |
| 140 | nargs='*', |
| 141 | default=[], |
| 142 | choices=[ep.name for ep in pkg_resources.iter_entry_points(vnfreq.NS)], |
| 143 | help='list of the ID of VNF Requirements to check, i.e. R-66070') |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 144 | |
| 145 | return parser.parse_args(args_list) |
| 146 | |
Lianhao Lu | 1de1b09 | 2018-08-02 16:16:24 +0800 | [diff] [blame] | 147 | |
| 148 | def init_logging(args): |
| 149 | verbosity = [logging.WARNING, logging.INFO, logging.DEBUG] |
| 150 | |
| 151 | logging.basicConfig() |
| 152 | logger = logging.getLogger('vnfsdk_pkgtools') |
| 153 | if args.verbosity >= len(verbosity): |
| 154 | verbose = verbosity[-1] |
| 155 | else: |
| 156 | verbose = verbosity[args.verbosity] |
| 157 | logger.setLevel(verbose) |
| 158 | |
| 159 | |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 160 | def main(): |
| 161 | args = parse_args(sys.argv[1:]) |
Lianhao Lu | 1de1b09 | 2018-08-02 16:16:24 +0800 | [diff] [blame] | 162 | init_logging(args) |
Lianhao Lu | 0af4624 | 2018-08-29 18:17:46 +0800 | [diff] [blame] | 163 | return args.func(args) |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 164 | |
| 165 | |
| 166 | if __name__ == '__main__': |
| 167 | main() |