Statistics
| Branch: | Revision:

iof-tools / reserve.py @ 67493fe0

History | View | Annotate | Download (4.58 KB)

1 c52be641 Michele Segata
#!/usr/bin/env python
2
import time
3
import datetime as dt
4
from argparse import ArgumentParser
5
from subprocess import Popen, PIPE
6
from os.path import isfile
7
8
9
OMNI = "omni -c ./omni_config -p {{}} {command}"
10
STATUS = OMNI.format(command="status {} {}")
11
CREATE_SLICE = OMNI.format(command="createslice {}")
12
RENEW_SLICE = OMNI.format(command="renewslice {} {}")
13
ALLOCATE = OMNI.format(command="allocate {} {} {}")
14
PROVISION = OMNI.format(command="provision {} {}")
15
START = OMNI.format(command="performoperationalaction {} {} geni_start")
16
DELETE = OMNI.format(command="delete {} {}")
17
18
19
class ProcessOutput:
20
    def __init__(self, out, err):
21
        self.stdout = out.decode('utf-8')
22
        self.stderr = err.decode('utf-8')
23
24
25
def local(command):
26
    proc = Popen(command, stdout=PIPE, stderr=PIPE, shell=True)
27
    stdout, stderr = proc.communicate()
28
    return ProcessOutput(stdout, stderr)
29
30
31
def reserve(testbed, exp_name, duration, rspec, project):
32
    args = "-V3 -a {}".format(testbed)
33
    end = dt.datetime.utcnow() + dt.timedelta(hours=int(duration))
34
    end = end.strftime("%Y%m%dT%H:%M:%S%Z")
35
    checkcmd = STATUS.format(project, args, exp_name)
36
    cmd_result = local(checkcmd)
37
    if cmd_result.stderr.find("geni_ready") != -1:
38
        print("Slice already reserved. Resources ready")
39
        return True
40
41
    # Reserve and start
42
    print("Creating slice and allocating experiment")
43
    local(CREATE_SLICE.format(project, exp_name))
44
    local(RENEW_SLICE.format(project, exp_name, end))
45
    alloc = local(ALLOCATE.format(project, args, exp_name, rspec))
46
47
    if alloc.stderr.find("Error") != -1:
48
        print(alloc.stderr)
49
        print("Resources are not available")
50
        return False
51
52
    local(PROVISION.format(project, args, exp_name))
53
    local(START.format(project, args, exp_name))
54
55
    # Wait till reserved
56
    tic = time.time()
57
    n = 1
58
    cmd_result = local(checkcmd)
59
    while cmd_result.stderr.find("geni_ready") == -1:
60
        if cmd_result.stderr.find("geni_failed") != -1:
61
            print("One node failed to boot. Output:")
62
            print(cmd_result.stderr)
63
            return False
64
        print("Waiting for nodes to perform boot... Attempt {}".format(n))
65
        n += 1
66
        time.sleep(10)
67
        cmd_result = local(checkcmd)
68
    toc = time.time() - tic
69
    print("Resources ready." + " Took: {}".format(toc))
70
    return True
71
72
73
def release(testbed, exp_name, project):
74
    args = "-V3 -a {}".format(testbed)
75
    checkcmd = STATUS.format(project, args, exp_name)
76
    if local(checkcmd).stderr.find("geni_ready") == -1 and \
77
       local(checkcmd).stderr.find("geni_provisioned") == -1:
78
        print("Experiment %s not found" % exp_name)
79
        return False
80
81
    # release the slice
82
    print("Releasing resources")
83
    local(DELETE.format(project, args, exp_name))
84
    return True
85
86
87
def main():
88
    parser = ArgumentParser()
89
    parser.add_argument("-t", "--testbed", dest="testbed",
90
                        default="twist", action="store", metavar="TESTBED",
91
                        help="Testbed to use [default: %(default)s]")
92
    parser.add_argument("-d", "--duration", dest="duration", type=int,
93
                        default=3, action="store", metavar="DURATION",
94
                        help="Duration in hours [default: %(default)s]")
95
    parser.add_argument("-n", "--name", dest="name", default="experiment",
96
                        action="store", metavar="SLICENAME",
97
                        help="Name of the slice [default: %(default)s]")
98
    parser.add_argument("-f", "--rspec", dest="rspec", default="test.rspec",
99
                        action="store", metavar="FILENAME",
100
                        help=".rspec file to use [default: %(default)s]")
101
    parser.add_argument("-p", "--project", dest="project",
102
                        default="internetonfire", action="store",
103
                        help="project the resources should be reserved for "
104
                             "[default: %(" "default)s]")
105
    parser.add_argument("-r", "--release", dest="delete", action="store_true",
106
                        help="Deletes an existing experiment. If not set, "
107
                             "the application creates a new experiment",
108
                        default=False)
109
110
    args = parser.parse_args()
111
    testbed = args.testbed
112
    duration = args.duration
113
    name = args.name
114
    rspec = args.rspec
115
    delete = args.delete
116
    project = args.project
117
118
    if not isfile(rspec):
119
        print("File %s not found" % rspec)
120
        return False
121
122
    if not delete:
123
        return reserve(testbed, name, duration, rspec, project)
124
    else:
125
        return release(testbed, name, project)
126
127
128
if __name__ == "__main__":
129
    main()