Move reservation directory to TG node.
[csit.git] / resources / tools / scripts / topo_reservation.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2018 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 """Script managing reservation and un-reservation of testbeds.
17
18 This script provides simple reservation mechanism to avoid
19 simultaneous use of nodes listed in topology file.
20 As source of truth, TG node from the topology file is used.
21 """
22
23 import sys
24 import argparse
25 from resources.libraries.python.ssh import SSH
26 from yaml import load
27
28 RESERVATION_DIR = "/tmp/reservation_dir"
29
30 def main():
31     parser = argparse.ArgumentParser()
32     parser.add_argument("-t", "--topo", required=True,
33                         help="Topology file")
34     parser.add_argument("-c", "--cancel", help="Cancel reservation",
35                         action="store_true")
36     args = parser.parse_args()
37     topology_file = args.topo
38     cancel_reservation = args.cancel
39
40     work_file = open(topology_file)
41     topology = load(work_file.read())['nodes']
42
43     # Even if TG is not guaranteed to be a Linux host,
44     # we are using it, because testing shows SSH access to DUT
45     # during test affects its performance (bursts of lost packets).
46     try:
47         tg_node = topology["TG"]
48     except KeyError:
49         print "Topology file does not contain 'TG' node"
50         return 1
51
52     ssh = SSH()
53     ssh.connect(tg_node)
54
55     # For system reservation we use mkdir it is an atomic operation and we can
56     # store additional data (time, client_ID, ..) within reservation directory.
57     if cancel_reservation:
58         ret, _, err = ssh.exec_command("rm -r {}".format(RESERVATION_DIR))
59     else:
60         ret, _, err = ssh.exec_command("mkdir {}".format(RESERVATION_DIR))
61
62     if ret != 0:
63         print("{} unsuccessful:\n{}".
64               format(("Cancellation " if cancel_reservation else "Reservation"),
65                      err))
66     return ret
67
68 if __name__ == "__main__":
69     sys.exit(main())