3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 class Profile(object):
21 self.description = None
23 self.included_profiles = []
25 return 'name:{}\ndescription:{}\ninherits:{}\nincluded_profiles:{}\n'.format(self.name, self.description, self.inherits, self.included_profiles)
27 class Profiles(object):
28 def __init__(self, location='/etc/service-profiles/'):
29 self.location = location
33 def _load_profiles(self):
34 files = self._get_profiles_files()
36 self._profile_from_file(f)
38 #update the included profiles
39 for name, profile in self.profiles.iteritems():
40 included_profiles = []
41 self._update_included_profiles(profile, included_profiles)
42 profile.included_profiles = included_profiles
44 def _update_included_profiles(self, profile, included_profiles):
45 included_profiles.append(profile.name)
46 for b in profile.inherits:
47 self._update_included_profiles(self.profiles[b], included_profiles)
49 def _get_profiles_files(self):
50 files = os.listdir(self.location)
51 pattern = re.compile('.*[.]profile$')
54 fullpath = self.location + '/' + f
55 if os.path.isfile(fullpath) and pattern.match(f):
56 result.append(fullpath)
59 def _profile_from_file(self, filename):
61 with open(filename) as f:
62 lines=f.read().splitlines()
66 raise Exception('Invalid line %s in file %s' % (l, filename))
67 elif data[0] == 'name':
68 profile.name = data[1]
69 elif data[0] == 'description':
70 profile.description = data[1]
71 elif data[0] == 'inherits':
72 profile.inherits = data[1].split(',')
74 raise Exception('Invalid line %s in file %s' % (l, filename))
75 self.profiles[profile.name] = profile
77 def get_included_profiles(self, name):
78 return self.profiles[name].included_profiles
80 def get_profiles(self):
83 def get_children_profiles(self, name):
85 for pfname, profile in self.profiles.iteritems():
86 if name in profile.inherits:
90 def get_service_profiles(self):
91 #profiles_files = self._get_profiles_files()
92 #profiles_names = [profile_file[:-len('.profile')] for profile_file in profiles_files]
93 profiles_names = self.profiles.keys()
97 def get_node_service_profiles(self, name):
98 path = '/etc/service-profiles/config.ini'
100 with open(path) as f:
101 content = f.readlines()
104 node = tmp.split(':')[0]
106 profiles = tmp.split(':')[1].split(',')
111 def get_management_service_profile():
115 def get_base_service_profile():
119 def get_controller_service_profile():
123 def get_caasmaster_service_profile():
127 def get_caasworker_service_profile():
131 def get_compute_service_profile():
135 def get_storage_service_profile():
139 if __name__ == '__main__':
144 parser = argparse.ArgumentParser(description='Test service profiles',
147 parser.add_argument('--location',
151 help='The location for service profile files',
155 parser.add_argument('--get-included-profiles',
156 dest='get_included_profiles',
157 help='Get the profiles included in some profile name',
160 parser.add_argument('--get-all-profiles',
161 help='Get the profiles list',
162 dest='get_all_profiles',
165 parser.add_argument('--get-children-profiles',
166 dest='get_children_profiles',
167 help='Get the children of a profile',
170 parser.add_argument('--name',
173 help='The name of the profile',
177 args = parser.parse_args(sys.argv[1:])
178 location = args.location
179 profiles = Profiles(location)
180 if args.get_included_profiles or args.get_children_profiles:
182 raise Exception('Missing profile name')
184 if args.get_included_profiles:
185 included_profiles = profiles.get_included_profiles(args.name)
186 print('Included profiles')
187 for p in included_profiles:
189 if args.get_children_profiles:
190 children_profiles = profiles.get_children_profiles(args.name)
191 print('Children profiles')
192 for p in children_profiles:
194 elif args.get_all_profiles:
195 all = profiles.get_profiles()
196 for name, p in all.iteritems():
198 except Exception as exp:
199 print('Failed with error %s' % exp)
200 traceback.print_exc()