JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / agent / env / builder.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd 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 import logging
11
12 import stevedore
13
14 LOG = logging.getLogger(__name__)
15
16
17 class PluginManager(object):
18     def __init__(self):
19         self.instance = None
20         self.saved = {}
21
22     def build(self, cfg):
23         scheme = cfg["scheme"]
24         if scheme in self.saved:
25             # reuse old instance
26             self.instance = self.saved[scheme]
27         else:
28             mgr = stevedore.driver.DriverManager(namespace="env_build.plugins",
29                                                  name=scheme,
30                                                  invoke_on_load=False)
31             self.instance = mgr.driver()
32             self.saved[scheme] = self.instance
33
34         self.instance.clean()
35         return self.instance.build(cfg)
36
37     def clean(self):
38         if self.instance:
39             self.instance.clean()
40         self.instance = None
41
42
43 if __name__ == "__main__":
44     import argparse
45     from vstf.controller.env_build.env_build import IntentParser
46
47     parser = argparse.ArgumentParser()
48     parser.add_argument('--config', help='config file to parse')
49     args = parser.parse_args()
50     logging.basicConfig(level=logging.INFO)
51     parser = IntentParser(args.config)
52     cfg_intent = parser.parse_cfg_file()
53     for host_cfg in cfg_intent['env-build']:
54         tn = PluginManager()
55         tn.build(host_cfg)