Use only Qemu Kill on the teardown of double qemu setup
[csit.git] / resources / tools / topo_reservation.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """This script provides simple reservation mechanism to avoid
17    simultaneous use of nodes listed in topology file.
18    As source of truth is used DUT1 node from the topology file."""
19
20 import sys
21 import argparse
22 from resources.libraries.python.ssh import SSH
23 from yaml import load
24
25 RESERVATION_DIR = "/tmp/reservation_dir"
26
27 def main():
28     parser = argparse.ArgumentParser()
29     parser.add_argument("-t", "--topo", required=True,
30                         help="Topology file")
31     parser.add_argument("-c", "--cancel", help="Cancel reservation",
32                         action="store_true")
33     args = parser.parse_args()
34     topology_file = args.topo
35     cancel_reservation = args.cancel
36
37     work_file = open(topology_file)
38     topology = load(work_file.read())['nodes']
39
40     #we are using DUT1 node because we expect DUT1 to be a linux host
41     #we don't use TG because we don't expect TG to be linux only host
42     try:
43         tg_node = topology["DUT1"]
44     except KeyError:
45         print "Topology file does not contain 'DUT1' node"
46         return 1
47
48     ssh = SSH()
49     ssh.connect(tg_node)
50
51     #For system reservation we use mkdir it is an atomic operation and we can
52     #store additional data (time, client_ID, ..) within reservation directory
53     if cancel_reservation:
54         ret, _, err = ssh.exec_command("rm -r {}".format(RESERVATION_DIR))
55     else:
56         ret, _, err = ssh.exec_command("mkdir {}".format(RESERVATION_DIR))
57
58     if ret != 0:
59         print("{} unsuccessful:\n{}".
60               format(("Cancellation " if cancel_reservation else "Reservation"),
61                      err))
62     return ret
63
64 if __name__ == "__main__":
65     sys.exit(main())