619e924dfc9aeec517126eab08ba9a59a3a1c6e7
3 # Copyright 2016 RIFT.IO Inc
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
24 class ConfigExtractionError(Exception):
28 class PackageConfigExtractor(object):
29 """ This class is reponsible for extracting config data to the correct directory
31 In order to remain compatible with the existing ConfigManager, we extract the config
32 to a known location (RIFT-13282)
34 DEFAULT_INSTALL_DIR
= os
.path
.join(
35 os
.environ
["RIFT_VAR_ROOT"],
39 CONFIG_REGEX
= "{prefix}(ns_config|vnf_config)/(?P<config_name>[^/]+.yaml)$"
41 def __init__(self
, log
, install_dir
=DEFAULT_INSTALL_DIR
):
43 self
._install
_dir
= install_dir
45 def _get_rel_dest_path(self
, descriptor_id
, config_name
):
46 dest_rel_path
= "libs/{}/config/{}".format(descriptor_id
, config_name
)
47 dest_path
= os
.path
.join(self
._install
_dir
, dest_rel_path
)
51 def package_config_files(cls
, package
):
53 regex
= cls
.CONFIG_REGEX
.format(prefix
=package
.prefix
)
55 for file_name
in package
.files
:
57 cls
.CONFIG_REGEX
.format(prefix
=package
.prefix
), file_name
,
62 config_name
= match
.group("config_name")
64 config_map
[config_name
] = file_name
68 def get_extracted_config_path(self
, package_id
, config_name
):
70 self
._get
_rel
_dest
_path
(package_id
, os
.path
.basename(config_name
)),
73 def extract_configs(self
, pkg
):
74 """ Extract any configuration files from the DescriptorPackage
77 pkg - A DescriptorPackage
80 ConfigExtractionError - The configuration could not be extracted
82 descriptor_id
= pkg
.descriptor_id
84 config_files
= PackageConfigExtractor
.package_config_files(pkg
).items()
85 for config_name
, config_file
in config_files
:
86 dest_rel_path
= self
._get
_rel
_dest
_path
(descriptor_id
, config_name
)
87 dest_path
= os
.path
.join(self
._install
_dir
, dest_rel_path
)
89 self
._log
.debug("Extracting %s config to %s", config_name
, dest_path
)
91 pkg
.extract_file(config_file
, dest_path
)
92 except package
.ExtractError
as e
:
93 raise ConfigExtractionError("Failed to extract config %s" % config_name
) from e