JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / controller / env_build / env_build.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 from vstf.controller.fabricant import Fabricant
13 from vstf.rpc_frame_work.rpc_producer import Server
14 from vstf.controller.env_build.cfg_intent_parse import IntentParser
15
16 LOG = logging.getLogger(__name__)
17
18
19 class EnvBuildApi(object):
20     def __init__(self, conn, config_file):
21         LOG.info("welcome to EnvBuilder")
22         self.conn = conn
23         intent_parser = IntentParser(config_file)
24         self.cfg_intent = intent_parser.parse_cfg_file()
25
26     def build(self):
27         LOG.info("start build")
28         for host_cfg in self.cfg_intent['env-build']:
29             rpc = Fabricant(host_cfg['ip'], self.conn)
30             rpc.build_env(timeout=1800, cfg_intent=host_cfg)
31         return True
32
33     def clean(self):
34         for host_cfg in self.cfg_intent['env-build']:
35             rpc = Fabricant(host_cfg['ip'], self.conn)
36             rpc.clean_env(timeout=120)
37         return True
38
39     def get_hosts(self):
40         result = []
41         for host_cfg in self.cfg_intent['env-build']:
42             host = {
43                 'name': host_cfg['ip'],
44                 'nic': "82599ES 10-Gigabit"
45             }
46             result.append(host)
47         return result
48
49
50 class TransmitterBuild(object):
51     def __init__(self, conn, config_file):
52         LOG.info("welcome to TransmitterBuild")
53         self.conn = conn
54         self._cfg_intent = config_file["transmitter-build"]
55
56     def build(self):
57         LOG.info("start build")
58         for cfg in self.cfg_intent:
59             rpc = Fabricant(cfg['ip'], self.conn)
60             cfg.setdefault("scheme", 'transmitter')
61             rpc.build_env(timeout=1800, cfg_intent=cfg)
62         return True
63
64     def clean(self):
65         for cfg in self.cfg_intent:
66             rpc = Fabricant(cfg['ip'], self.conn)
67             rpc.clean_env(timeout=10)
68         return True
69
70
71 if __name__ == "__main__":
72     import argparse
73
74     parser = argparse.ArgumentParser()
75     parser.add_argument('--rpc_server', help='rabbitmq server for deliver messages.')
76     parser.add_argument('--config', help='config file to parse')
77     args = parser.parse_args()
78     logging.basicConfig(level=logging.INFO)
79     conn = Server(args.rpc_server)
80     tn = EnvBuildApi(conn, args.config)
81     tn.build()