add update opnfv-testapi script in testAPI
[releng.git] / utils / test / result_collection_api / update / update_api.py
1 ##############################################################################
2 # Copyright (c) 2016 ZTE Corporation
3 # feng.xiaowei@zte.com.cn
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 import subprocess
10
11 possible_processes = [
12     'result_collection_api',
13     'opnfv-testapi'
14 ]
15
16
17 def kill_olds():
18     for proc in possible_processes:
19         query = 'ps -ef | grep {} | grep -v grep'.format(proc)
20         runnings = execute_with_output(query)
21         if runnings:
22             for running in runnings:
23                 kill = 'kill -kill ' + running.split()[1]
24                 execute_with_output(kill)
25             runnings = execute_with_output(query)
26         assert len(runnings) == 0, 'kill %s failed'.format(proc)
27
28
29 def install_dependencies():
30     execute_with_assert('pip install -r ../requirements.txt')
31
32
33 def install_new():
34     execute_with_assert('cd ../ && python setup.py install')
35
36
37 def run_new():
38     execute_with_assert('opnfv-testapi &')
39
40
41 def execute_with_output(cmd):
42     return subprocess.Popen(cmd, shell=True,
43                             stdout=subprocess.PIPE).stdout.readlines()
44
45
46 def execute_with_assert(cmd):
47     execute_output = subprocess.call(cmd, shell=True)
48     assert execute_output == 0
49
50
51 if __name__ == '__main__':
52     kill_olds()
53     install_dependencies()
54     install_new()
55     run_new()