Merge "Build on jenkins nodes with label docker"
[osm/SO.git] / rwlaunchpad / plugins / rwlaunchpadtasklet / rift / package / config.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16 #
17
18 import re
19 import os.path
20
21 from . import package
22
23
24 class ConfigExtractionError(Exception):
25 pass
26
27
28 class PackageConfigExtractor(object):
29 """ This class is reponsible for extracting config data to the correct directory
30
31 In order to remain compatible with the existing ConfigManager, we extract the config
32 to a known location (RIFT-13282)
33 """
34 DEFAULT_INSTALL_DIR = os.path.join(
35 os.environ["RIFT_ARTIFACTS"],
36 "launchpad"
37 )
38
39 CONFIG_REGEX = "{prefix}(ns_config|vnf_config)/(?P<config_name>[^/]+.yaml)$"
40
41 def __init__(self, log, install_dir=DEFAULT_INSTALL_DIR):
42 self._log = log
43 self._install_dir = install_dir
44
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)
48 return dest_path
49
50 @classmethod
51 def package_config_files(cls, package):
52 config_map = {}
53 regex = cls.CONFIG_REGEX.format(prefix=package.prefix)
54
55 for file_name in package.files:
56 match = re.match(
57 cls.CONFIG_REGEX.format(prefix=package.prefix), file_name,
58 )
59 if match is None:
60 continue
61
62 config_name = match.group("config_name")
63
64 config_map[config_name] = file_name
65
66 return config_map
67
68 def get_extracted_config_path(self, package_id, config_name):
69 return os.path.join(
70 self._get_rel_dest_path(package_id, os.path.basename(config_name)),
71 )
72
73 def extract_configs(self, pkg):
74 """ Extract any configuration files from the DescriptorPackage
75
76 Arguments:
77 pkg - A DescriptorPackage
78
79 Raises:
80 ConfigExtractionError - The configuration could not be extracted
81 """
82 descriptor_id = pkg.descriptor_id
83
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)
88
89 self._log.debug("Extracting %s config to %s", config_name, dest_path)
90 try:
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