Merge "Build on jenkins nodes with label docker"
[osm/SO.git] / rwlaunchpad / plugins / rwlaunchpadtasklet / rift / package / charm.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 CharmExtractionError(Exception):
25 pass
26
27
28 class PackageCharmExtractor(object):
29 """ This class is reponsible for extracting charms to the correct directory
30
31 In order to remain compatible with the existing Jujuclient, we extract the charms
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 CHARM_REGEX = "{prefix}charms/(trusty/)?(?P<charm_name>[^/]+)$"
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, charm_name):
46 dest_rel_path = "libs/{}/charms/trusty/{}".format(descriptor_id, charm_name)
47 dest_path = os.path.join(self._install_dir, dest_rel_path)
48 return dest_path
49
50 @classmethod
51 def charm_dir_map(cls, package):
52 charm_map = {}
53 regex = cls.CHARM_REGEX.format(prefix=package.prefix)
54
55 for dir_name in package.dirs:
56 match = re.match(
57 cls.CHARM_REGEX.format(prefix=package.prefix), dir_name,
58 )
59 if match is None:
60 continue
61
62 charm_name = match.group("charm_name")
63 if charm_name == "trusty":
64 continue
65
66 charm_map[charm_name] = dir_name
67
68 return charm_map
69
70 def get_extracted_charm_dir(self, package_id, charm_name):
71 return os.path.join(
72 self._get_rel_dest_path(package_id, charm_name),
73 )
74
75 def extract_charms(self, pkg):
76 """ Extract charms contained within the DescriptorPackage
77 to the known charm directory.
78
79 Arguments:
80 pkg - The descriptor package that MAY contain charm directories
81
82 Raises:
83 CharmExtractionError - Charms in the package failed to get extracted
84 """
85 descriptor_id = pkg.descriptor_id
86 charm_dir_map = PackageCharmExtractor.charm_dir_map(pkg)
87
88 for charm_name, charm_dir in charm_dir_map.items():
89 dest_rel_path = self._get_rel_dest_path(descriptor_id, charm_name)
90 dest_path = os.path.join(self._install_dir, dest_rel_path)
91
92 self._log.debug("Extracting %s charm to %s", charm_name, dest_path)
93 try:
94 pkg.extract_dir(charm_dir, dest_path)
95 except package.ExtractError as e:
96 raise CharmExtractionError("Failed to extract charm %s" % charm_name) from e