Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / vstf / agent / agent.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 # !/usr/bin/env python
11 # coding=utf-8
12
13 import logging
14 import argparse
15 import signal
16
17 from oslo.config import cfg
18
19 from vstf.rpc_frame_work import rpc_consumer
20 from vstf.common.log import setup_logging
21 from vstf.common import daemon
22 from vstf.common.cfgparser import CfgParser
23
24 LOG = logging.getLogger(__name__)
25
26 server_opts = [
27     cfg.StrOpt('user', default='guest', help="the rabbit's user, default guest"),
28     cfg.StrOpt('passwd', default='guest', help="the rabbit's passwd, default guest"),
29     cfg.StrOpt('host', default='localhost', help="tell the process wich interface to listen"),
30     cfg.StrOpt('port', default=5672, help=""),
31     cfg.StrOpt('id', default='agent', help="")
32
33 ]
34
35 stc_opts = [
36     cfg.StrOpt('package', default='', help="the STC python path")
37 ]
38
39
40 class Client(daemon.Daemon):
41     """This is a consumer of vstf-agent which will create two channel to the
42     rabbitmq-server, one for direct call, one for fan call.
43
44     agent start with a config file which record rabbitmq's ip, port and user passwd
45     also each agent has its own id.
46
47     """
48
49     def __init__(self, agent, config_file):
50         """Record the config file, init the daemon.
51
52         :param str config_file: the config of a VSTF agent.
53
54         """
55         super(Client, self).__init__('/tmp/esp_rpc_client.pid')
56         self.config_file = config_file
57         self.agent = agent
58         self.config = None
59         self.proxy = None
60         self.run_flag = True
61
62     def init_config(self):
63         """Use olso.config to analyse the config file
64
65         """
66         parser = CfgParser(self.config_file)
67         parser.register_my_opts(server_opts, "rabbit")
68         parser.register_my_opts(stc_opts, "spirent")
69         self.config = parser.parse()
70
71     def loop_thread(self):
72         LOG.info("Try to create direct proxy...")
73         self.proxy = rpc_consumer.VstfConsumer(self.agent,
74                                                self.config.rabbit.user,
75                                                self.config.rabbit.passwd,
76                                                self.config.rabbit.host,
77                                                self.config.rabbit.port,
78                                                self.config.rabbit.id)
79         self.proxy.run()
80
81     def run(self):
82         """Run the rabbitmq consumers as a daemon.
83
84         """
85         signal.signal(signal.SIGTERM, self.process_exit)
86         self.loop_thread()
87         LOG.info("agent start ok!")
88
89     def process_exit(self, signum, frame):
90         """This function try to stop the agent after running agent stop.
91         When we call vstf-agent stop which will send a signal SIGTERM to agent
92         When the agent catch the SIGTERM signal will call this function.
93
94         """
95         LOG.info("daemon catch the signalterm, start to stop the process.")
96         self.run_flag = False
97         if self.proxy:
98             self.proxy.stop()
99
100     def start_agent(self):
101         self.init_config()
102         self.start()
103
104     def stop_agent(self):
105         """Notice that: this function just kill the agent by pid file, it has
106         none vars of the agent.
107
108         """
109         LOG.info("call daemon stop.")
110         # kill the main thread
111         self.stop()
112
113
114 def main():
115     setup_logging(level=logging.INFO, log_file="/var/log/vstf/vstf-agent.log")
116     parser = argparse.ArgumentParser(description='agent option')
117     parser.add_argument('action', choices=('start', 'stop', "restart"),
118                         default="start", help="start or stop agent")
119     parser.add_argument('--agent_type', action='store',
120                         default="soft",
121                         choices=["soft", "spirent"],
122                         help="the agent type, as now, just soft and spirent")
123     parser.add_argument(
124         '--config_file',
125         action='store',
126         default="/etc/vstf/amqp/amqp.ini",
127         help="some env_build params recorded in the config file")
128
129     args = parser.parse_args()
130
131     client = Client(args.agent_type, args.config_file)
132     if "start" == args.action:
133         client.start_agent()
134     elif "stop" == args.action:
135         client.stop_agent()
136     elif "restart" == args.action:
137         client.stop_agent()
138         client.start_agent()
139     else:
140         raise Exception("only support actions: start/stop/restart")
141
142
143 if __name__ == '__main__':
144     main()