blob: 175fcb249e6c56facb232ea01059484fd5ad7bf3 [file] [log] [blame]
Lianhao Lu7676ca52018-03-22 20:39:04 +08001#
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 Lu7676ca52018-03-22 20:39:04 +080017import sys
18import logging
19import argparse
Lianhao Lu7676ca52018-03-22 20:39:04 +080020import os
21import shutil
22import tempfile
23
Lianhao Lu0af46242018-08-29 18:17:46 +080024import pkg_resources
25
26from vnfsdk_pkgtools.packager import csar
Lianhao Lu9003bbf2019-11-21 12:56:51 +080027from vnfsdk_pkgtools.packager import manifest
Lianhao Lu7676ca52018-03-22 20:39:04 +080028from vnfsdk_pkgtools import validator
Lianhao Lu0af46242018-08-29 18:17:46 +080029from vnfsdk_pkgtools import vnfreq
30
Lianhao Lu7676ca52018-03-22 20:39:04 +080031
32def csar_create_func(namespace):
Lianhao Lu0efadc12018-08-27 17:11:10 +080033
Lianhao Lu7676ca52018-03-22 20:39:04 +080034 csar.write(namespace.source,
35 namespace.entry,
36 namespace.destination,
Lianhao Lu7676ca52018-03-22 20:39:04 +080037 args=namespace)
Lianhao Lu1de1b092018-08-02 16:16:24 +080038
Lianhao Lu7676ca52018-03-22 20:39:04 +080039def csar_open_func(namespace):
40 csar.read(namespace.source,
Lianhao Lu0efadc12018-08-27 17:11:10 +080041 namespace.destination,
42 namespace.no_verify_cert)
Lianhao Lu1de1b092018-08-02 16:16:24 +080043
Lianhao Lu7676ca52018-03-22 20:39:04 +080044def csar_validate_func(namespace):
45 workdir = tempfile.mkdtemp()
46 try:
Lianhao Lu0af46242018-08-29 18:17:46 +080047 err = 0
Lianhao Lu7676ca52018-03-22 20:39:04 +080048 reader = csar.read(namespace.source,
Lianhao Lu0efadc12018-08-27 17:11:10 +080049 workdir,
50 no_verify_cert=True)
Lianhao Lu7676ca52018-03-22 20:39:04 +080051
52 driver = validator.get_validator(namespace.parser)
53 driver.validate(reader)
Lianhao Lu0af46242018-08-29 18:17:46 +080054 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 Lu7676ca52018-03-22 20:39:04 +080061 finally:
62 shutil.rmtree(workdir, ignore_errors=True)
63
64
65def parse_args(args_list):
66 """
67 CLI entry point
68 """
Lianhao Lu7676ca52018-03-22 20:39:04 +080069 parser = argparse.ArgumentParser(description='VNF SDK CSAR manipulation tool')
Lianhao Lu1de1b092018-08-02 16:16:24 +080070 parser.add_argument('-v', '--verbose',
Lianhao Lu7676ca52018-03-22 20:39:04 +080071 dest='verbosity',
72 action='count',
73 default=0,
74 help='Set verbosity level (can be passed multiple times)')
Lianhao Lu1de1b092018-08-02 16:16:24 +080075
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 Lu7676ca52018-03-22 20:39:04 +080079 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 Lucd02d1f2018-03-26 13:35:22 +0800101 csar_create.add_argument(
102 '--digest',
Lianhao Lu9003bbf2019-11-21 12:56:51 +0800103 choices=manifest.SUPPORTED_HASH_ALGO,
Lianhao Lucd02d1f2018-03-26 13:35:22 +0800104 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 Lu0efadc12018-08-27 17:11:10 +0800105 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 Lu7676ca52018-03-22 20:39:04 +0800111
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 Lu0efadc12018-08-27 17:11:10 +0800122 csar_open.add_argument(
123 '--no-verify-cert',
124 action='store_true',
125 help="Do NOT verify the signer's certificate")
126
Lianhao Lu7676ca52018-03-22 20:39:04 +0800127
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 Lu40f7a0f2018-08-02 14:48:53 +0800135 default='toscaparser',
Lianhao Lu0af46242018-08-29 18:17:46 +0800136 choices=[ep.name for ep in pkg_resources.iter_entry_points(validator.NS)],
Lianhao Lu7676ca52018-03-22 20:39:04 +0800137 help='use which csar parser to validate')
Lianhao Lu0af46242018-08-29 18:17:46 +0800138 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 Lu7676ca52018-03-22 20:39:04 +0800144
145 return parser.parse_args(args_list)
146
Lianhao Lu1de1b092018-08-02 16:16:24 +0800147
148def 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 Lu7676ca52018-03-22 20:39:04 +0800160def main():
161 args = parse_args(sys.argv[1:])
Lianhao Lu1de1b092018-08-02 16:16:24 +0800162 init_logging(args)
Lianhao Lu0af46242018-08-29 18:17:46 +0800163 return args.func(args)
Lianhao Lu7676ca52018-03-22 20:39:04 +0800164
165
166if __name__ == '__main__':
167 main()