Store "tag" and "url" in files in reservation dir
[csit.git] / resources / tools / scripts / topo_reservation.py
1 #!/usr/bin/env python2
2
3 # Copyright (c) 2019 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 import yaml
26
27 from resources.libraries.python.ssh import exec_cmd
28
29
30 RESERVATION_DIR = "/tmp/reservation_dir"
31
32
33 def diag_cmd(node, cmd):
34     """Execute cmd, print cmd and stdout, ignore stderr and rc; return None.
35
36     :param node: Node object as parsed from topology file to execute cmd on.
37     :param cmd: Command to execute.
38     :type ssh: dict
39     :type cmd: str
40     """
41     print "+", cmd
42     _, stdout, _ = exec_cmd(node, cmd)
43     print stdout
44
45
46 def main():
47     """Parse arguments, perform the action, write useful output, propagate RC.
48
49     If the intended action is cancellation, reservation dir is deleted.
50
51     If the intended action is reservation, the list is longer:
52     1. List contents of reservation dir.
53     2. List contents of test.url file in the dir.
54     3. Create reservation dir.
55     4. Touch file according to -r option.
56     5. Put -u option string to file test.url
57     From these 5 steps, 1 and 2 are performed always, their RC ignored.
58     RC of step 3 gives the overall result.
59     If the result is success, steps 4-5 are executed without any output,
60     their RC is ignored.
61
62     The two files in reservation dir are there for reporting
63     which test run holds the reservation, so people can manually fix the testbed
64     if the rest run has been aborted, or otherwise failed to unregister.
65
66     The two files have different audiences.
67
68     The URL content is useful for people scheduling their test runs
69     and wondering why the reservation takes so long.
70     For them, a URL (if available) to copy and paste into browser
71     to see which test runs are blocking testbeds is the most convenient.
72
73     The "run tag" as a filename is useful for admins accessing the testbed
74     via a graphical terminal, which does not allow copying of text,
75     as they need less keypresses to identify the test run holding the testbed.
76     Also, the listing shows timestamps, which is useful for both audiences.
77     """
78     parser = argparse.ArgumentParser()
79     parser.add_argument("-t", "--topo", required=True,
80                         help="Topology file")
81     parser.add_argument("-c", "--cancel", help="Cancel reservation",
82                         action="store_true")
83     parser.add_argument("-r", "--runtag", required=False, default="Unknown",
84                         help="Identifier for test run suitable as filename")
85     parser.add_argument("-u", "--url", required=False, default="Unknown",
86                         help="Identifier for test run suitable as URL")
87     args = parser.parse_args()
88
89     with open(args.topo, "r") as topo_file:
90         topology = yaml.load(topo_file.read())['nodes']
91
92     # Even if TG is not guaranteed to be a Linux host,
93     # we are using it, because testing shows SSH access to DUT
94     # during test affects its performance (bursts of lost packets).
95     try:
96         tgn = topology["TG"]
97     except KeyError:
98         print "Topology file does not contain 'TG' node"
99         return 1
100
101     # For system reservation we use mkdir it is an atomic operation and we can
102     # store additional data (time, client_ID, ..) within reservation directory.
103     if args.cancel:
104         ret, _, err = exec_cmd(tgn, "rm -r {}".format(RESERVATION_DIR))
105         if ret:
106             print "Cancellation unsuccessful:\n{}".format(err)
107         return ret
108     # Before critical section, output can be outdated already.
109     print "Diagnostic commands:"
110     # -d and * are to supress "total <size>", see https://askubuntu.com/a/61190
111     diag_cmd(tgn, "ls --full-time -cd '{dir}'/*".format(dir=RESERVATION_DIR))
112     diag_cmd(tgn, "head -1 '{dir}/run.url'".format(dir=RESERVATION_DIR))
113     print "Attempting reservation."
114     # Entering critical section.
115     # TODO: Add optional argument to exec_cmd_no_error to make it
116     # sys.exit(ret) instead raising? We do not want to deal with stacktrace.
117     ret, _, err = exec_cmd(tgn, "mkdir '{dir}'".format(dir=RESERVATION_DIR))
118     # Critical section is over.
119     if ret:
120         print "Reservation unsuccessful:\n{}".format(err)
121         return ret
122     # Here the script knows it is the only owner of the testbed.
123     print "Success, writing test run info to reservation dir."
124     # TODO: Add optional argument to exec_cmd_no_error to print message
125     # to console instead raising? We do not want to deal with stacktrace.
126     ret2, _, err = exec_cmd(
127         tgn, "touch '{dir}/{runtag}' && ( echo '{url}' > '{dir}/run.url' )"\
128         .format(dir=RESERVATION_DIR, runtag=args.runtag, url=args.url))
129     if ret2:
130         print "Writing test run info failed, but continuing anyway:\n{}".format(
131             err)
132     return ret
133
134
135 if __name__ == "__main__":
136     sys.exit(main())