summaryrefslogtreecommitdiffstats
path: root/update/update_api.py
blob: db8ad2d906e4c7c3e4f498a97e472421c34e770b (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) 2016 ZTE Corporation
# feng.xiaowei@zte.com.cn
# 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 subprocess

possible_processes = [
    'result_collection_api',
    'opnfv-testapi'
]


def kill_olds():
    for proc in possible_processes:
        query = 'ps -ef | grep {} | grep -v grep'.format(proc)
        runnings = execute_with_output(query)
        if runnings:
            for running in runnings:
                kill = 'kill -kill ' + running.split()[1]
                execute_with_output(kill)
            runnings = execute_with_output(query)
        assert len(runnings) == 0, 'kill %s failed'.format(proc)


def install_dependencies():
    execute_with_assert('pip install -r ../requirements.txt')


def install_new():
    execute_with_assert('cd ../ && python setup.py install')


def run_new():
    execute_with_assert('opnfv-testapi &')


def execute_with_output(cmd):
    return subprocess.Popen(cmd, shell=True,
                            stdout=subprocess.PIPE).stdout.readlines()


def execute_with_assert(cmd):
    execute_output = subprocess.call(cmd, shell=True)
    assert execute_output == 0


if __name__ == '__main__':
    kill_olds()
    install_dependencies()
    install_new()
    run_new()