Completed
Push — master ( 159d6c...6ea069 )
by Peter
03:43
created

get_config_fname()   A

Complexity

Conditions 3

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 3
1
# Administration script functionality on the production system
2
3
import sys
4
5
from . import CONFIG_FILE_DEFAULT
6
from .server import fetch_job, fake_fetch_job, send_hostinfo
7
from .running import kill_longrunning
8
from .locking import ScriptLock, break_lock
9
from .config import read_config, has_config, create_config, check_config
10
11
12
def download_and_run(config):
13
    '''
14
    Main operation of the executor.
15
16
    Returns True when a job was downloaded and executed.
17
    Returns False when no job could be downloaded.
18
    '''
19
    job = fetch_job(config)
20
    if job:
21
        job._run_validate()
22
        return True
23
    else:
24
        return False
25
26
27
def copy_and_run(config, src_dir):
28
    '''
29
    Local-only operation of the executor.
30
    Intended for validation script developers,
31
    and the test suite.
32
33
    Please not that this function only works correctly
34
    if the validator has one of the following names:
35
        - validator.py
36
        - validator.zip
37
38
    Returns True when a job was prepared and executed.
39
    Returns False when no job could be prepared.
40
    '''
41
    job = fake_fetch_job(config, src_dir)
42
    if job:
43
        job._run_validate()
44
        return True
45
    else:
46
        return False
47
48
49
def get_config_fname(argv):
50
    for index, entry in enumerate(argv):
51
        if entry == "-c":
52
            return argv[index + 1]
53
    return CONFIG_FILE_DEFAULT
54
55
56
def console_script():
57
    '''
58
        The main entry point for the production
59
        administration script 'opensubmit-exec',
60
        installed by setuptools.
61
    '''
62
    if len(sys.argv) == 1:
63
        print("opensubmit-exec [configcreate <server_url>|configtest|run|test <dir>|unlock|help] [-c config_file]")
64
        return 0
65
66
    if "help" in sys.argv[1]:
67
        print("configcreate <server_url>:  Create initial config file for the OpenSubmit executor.")
68
        print("configtest:                 Check config file for correct installation of the OpenSubmit executor.")
69
        print("run:                        Fetch and run code to be tested from the OpenSubmit web server. Suitable for crontab.")
70
        print("test <dir>:                 Run test script from a local folder for testing purposes.")
71
        print("unlock:                     Break the script lock, because of crashed script.")
72
        print("help:                       Print this help")
73
        print(
74
            "-c config_file    Configuration file to be used (default: {0})".format(CONFIG_FILE_DEFAULT))
75
        return 0
76
77
    # Translate legacy commands
78
    if sys.argv[1] == "configure":
79
        sys.argv[1] = 'configtest'
80
81
    config_fname = get_config_fname(sys.argv)
82
83
    if "configcreate" in sys.argv[1]:
84
        print("Creating config file at " + config_fname)
85
86
        server_url = sys.argv[2]
87
88
        if create_config(config_fname, override_url=server_url):
89
            print("Config file created, fetching jobs from " + server_url)
90
            return 0
91
        else:
92
            return 1
93
94
    if "configtest" in sys.argv[1]:
95
        print("Testing config file at " + config_fname)
96
97
        if has_config(config_fname):
98
            config = read_config(config_fname)
99
            if not check_config(config):
100
                return 1
101
        else:
102
            print("ERROR: Seems like the config file %s does not exist. Call 'opensubmit-exec configcreate <server_url>' first." %
103
                  config_fname)
104
            return 1
105
106
        print("Sending host information update to server ...")
107
        send_hostinfo(config)
108
        return 0
109
110
    if "unlock" in sys.argv[1]:
111
        config = read_config(config_fname)
112
        break_lock(config)
113
        return 0
114
115
    if "run" in sys.argv[1]:
116
        config = read_config(config_fname)
117
        # Perform additional precautions for unattended mode in cron
118
        kill_longrunning(config)
119
        with ScriptLock(config):
120
            download_and_run(config)
121
        return 0
122
123
    if "test" in sys.argv[1]:
124
        config = read_config(config_fname)
125
        copy_and_run(config, sys.argv[2])
126
        return 0
127
128
129
if __name__ == "__main__":
130
    exit(console_script())
131