1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
10 # !/usr/bin/env python
17 from oslo.config import cfg
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
24 LOG = logging.getLogger(__name__)
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="")
36 cfg.StrOpt('package', default='', help="the STC python path")
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.
44 agent start with a config file which record rabbitmq's ip, port and user passwd
45 also each agent has its own id.
49 def __init__(self, agent, config_file):
50 """Record the config file, init the daemon.
52 :param str config_file: the config of a VSTF agent.
55 super(Client, self).__init__('/tmp/esp_rpc_client.pid')
56 self.config_file = config_file
62 def init_config(self):
63 """Use olso.config to analyse the config file
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()
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)
82 """Run the rabbitmq consumers as a daemon.
85 signal.signal(signal.SIGTERM, self.process_exit)
87 LOG.info("agent start ok!")
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.
95 LOG.info("daemon catch the signalterm, start to stop the process.")
100 def start_agent(self):
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.
109 LOG.info("call daemon stop.")
110 # kill the main thread
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',
121 choices=["soft", "spirent"],
122 help="the agent type, as now, just soft and spirent")
126 default="/etc/vstf/amqp/amqp.ini",
127 help="some env_build params recorded in the config file")
129 args = parser.parse_args()
131 client = Client(args.agent_type, args.config_file)
132 if "start" == args.action:
134 elif "stop" == args.action:
136 elif "restart" == args.action:
140 raise Exception("only support actions: start/stop/restart")
143 if __name__ == '__main__':