summaryrefslogtreecommitdiffstats
path: root/vstf/vstf/agent/env/builder.py
blob: a66a8873ce1d2250870a707d99de7f686f940981 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
##############################################################################
# Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import logging

import stevedore

LOG = logging.getLogger(__name__)


class PluginManager(object):
    def __init__(self):
        self.instance = None
        self.saved = {}

    def build(self, cfg):
        scheme = cfg["scheme"]
        if scheme in self.saved:
            # reuse old instance
            self.instance = self.saved[scheme]
        else:
            mgr = stevedore.driver.DriverManager(namespace="env_build.plugins",
                                                 name=scheme,
                                                 invoke_on_load=False)
            self.instance = mgr.driver()
            self.saved[scheme] = self.instance

        self.instance.clean()
        return self.instance.build(cfg)

    def clean(self):
        if self.instance:
            self.instance.clean()
        self.instance = None


if __name__ == "__main__":
    import argparse
    from vstf.controller.env_build.env_build import IntentParser

    parser = argparse.ArgumentParser()
    parser.add_argument('--config', help='config file to parse')
    args = parser.parse_args()
    logging.basicConfig(level=logging.INFO)
    parser = IntentParser(args.config)
    cfg_intent = parser.parse_cfg_file()
    for host_cfg in cfg_intent['env-build']:
        tn = PluginManager()
        tn.build(host_cfg)