Allow common patches file
[apex.git] / apex / deployment / tripleo.py
1 ##############################################################################
2 # Copyright (c) 2018 Tim Rozet (trozet@redhat.com) and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 # TODO(trozet): this will serve as the deployment class as we migrate logic out
11 # of deploy.py
12 import logging
13 import os
14 import pprint
15
16 from apex.common.exceptions import ApexDeployException
17 from apex.common import utils
18
19
20 class ApexDeployment:
21     def __init__(self, deploy_settings, patch_file, ds_file):
22         self.ds = deploy_settings
23         # TODO(trozet): remove ds_file from args and have this class inherit
24         # super deployment class init which does all the settings
25         self.ds_file = ds_file
26         self.ds_globals = self.ds['global_params']
27         self.p_file = patch_file
28
29     def determine_patches(self):
30         patches = self.ds_globals['patches']
31         if not os.path.isfile(self.p_file):
32             new_file = os.path.join(os.path.dirname(self.ds_file),
33                                     'common-patches.yaml')
34             if os.path.isfile(new_file):
35                 logging.warning('Patch file {} not found, falling back to '
36                                 '{}'.format(self.p_file, new_file))
37                 self.p_file = new_file
38             else:
39                 logging.error('Unable to find common patch file: '
40                               '{}'.format(self.p_file))
41                 raise ApexDeployException(
42                     'Specified common patch file not found: {}'.format(
43                         self.p_file))
44         logging.info('Loading patches from common patch file {}'.format(
45             self.p_file))
46         common_patches = utils.parse_yaml(self.p_file)
47         logging.debug('Content from common patch file is: {}'.format(
48             pprint.pformat(common_patches)))
49         if 'patches' not in common_patches.keys():
50             logging.error('Error parsing common patches file, wrong format. '
51                           'Missing "patches" dictionary')
52             raise ApexDeployException('Invalid format of common patch file')
53         else:
54             common_patches = common_patches['patches']
55         for ptype in ('undercloud', 'overcloud'):
56             if ptype in common_patches:
57                 patches[ptype] = utils.unique(patches[ptype] +
58                                               common_patches[ptype])
59         return patches