/
__init__.py
155 lines (128 loc) · 5.18 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import logging
import os
import subprocess
import time
from string import Template
from pkg_resources import resource_string
from six import text_type
from galaxy.util import unicodify
log = logging.getLogger(__name__)
DEFAULT_SHELL = '/bin/bash'
DEFAULT_JOB_FILE_TEMPLATE = Template(
resource_string(__name__, 'DEFAULT_JOB_FILE_TEMPLATE.sh').decode('UTF-8')
)
SLOTS_STATEMENT_CLUSTER_DEFAULT = \
resource_string(__name__, 'CLUSTER_SLOTS_STATEMENT.sh').decode('UTF-8')
SLOTS_STATEMENT_SINGLE = """
GALAXY_SLOTS="1"
"""
INTEGRITY_INJECTION = """
# The following block can be used by the job system
# to ensure this script is runnable before actually attempting
# to run it.
if [ -n "$ABC_TEST_JOB_SCRIPT_INTEGRITY_XYZ" ]; then
exit 42
fi
"""
INTEGRITY_SYNC_COMMAND = "/bin/sync"
DEFAULT_INTEGRITY_CHECK = True
DEFAULT_INTEGRITY_COUNT = 35
DEFAULT_INTEGRITY_SLEEP = .25
REQUIRED_TEMPLATE_PARAMS = ['working_directory', 'command', 'exit_code_path']
OPTIONAL_TEMPLATE_PARAMS = {
'galaxy_lib': None,
'galaxy_virtual_env': None,
'headers': '',
'env_setup_commands': [],
'slots_statement': SLOTS_STATEMENT_CLUSTER_DEFAULT,
'instrument_pre_commands': '',
'instrument_post_commands': '',
'integrity_injection': INTEGRITY_INJECTION,
'shell': DEFAULT_SHELL,
'preserve_python_environment': True,
}
def job_script(template=DEFAULT_JOB_FILE_TEMPLATE, **kwds):
"""
>>> has_exception = False
>>> try: job_script()
... except Exception as e: has_exception = True
>>> has_exception
True
>>> script = job_script(working_directory='wd', command='uptime', exit_code_path='ec')
>>> '\\nuptime\\n' in script
True
>>> 'echo $? > ec' in script
True
>>> 'GALAXY_LIB="None"' in script
True
>>> script.startswith('#!/bin/sh\\n#PBS -test\\n')
False
>>> script = job_script(working_directory='wd', command='uptime', exit_code_path='ec', headers='#PBS -test')
>>> script.startswith('#!/bin/bash\\n\\n#PBS -test\\n')
True
>>> script = job_script(working_directory='wd', command='uptime', exit_code_path='ec', slots_statement='GALAXY_SLOTS="$SLURM_JOB_NUM_NODES"')
>>> script.find('GALAXY_SLOTS="$SLURM_JOB_NUM_NODES"\\nexport GALAXY_SLOTS\\n') > 0
True
"""
if any([param not in kwds for param in REQUIRED_TEMPLATE_PARAMS]):
raise Exception("Failed to create job_script, a required parameter is missing.")
job_instrumenter = kwds.get("job_instrumenter", None)
if job_instrumenter:
del kwds["job_instrumenter"]
working_directory = kwds.get("metadata_directory", kwds["working_directory"])
kwds["instrument_pre_commands"] = job_instrumenter.pre_execute_commands(working_directory) or ''
kwds["instrument_post_commands"] = job_instrumenter.post_execute_commands(working_directory) or ''
template_params = OPTIONAL_TEMPLATE_PARAMS.copy()
template_params.update(**kwds)
env_setup_commands_str = "\n".join(template_params["env_setup_commands"])
template_params["env_setup_commands"] = env_setup_commands_str
for key, value in template_params.items():
template_params[key] = unicodify(value)
if not isinstance(template, Template):
template = Template(template)
return template.safe_substitute(template_params)
def check_script_integrity(config):
return getattr(config, "check_job_script_integrity", DEFAULT_INTEGRITY_CHECK)
def write_script(path, contents, config, mode=0o755):
dir = os.path.dirname(path)
if not os.path.exists(dir):
os.makedirs(dir)
with open(path, 'w') as f:
if isinstance(contents, text_type):
contents = contents.encode("UTF-8")
f.write(contents)
os.chmod(path, mode)
_handle_script_integrity(path, config)
def _handle_script_integrity(path, config):
if not check_script_integrity(config):
return
script_integrity_verified = False
count = getattr(config, "check_job_script_integrity_count", DEFAULT_INTEGRITY_COUNT)
sleep_amt = getattr(config, "check_job_script_integrity_sleep", DEFAULT_INTEGRITY_SLEEP)
for i in range(count):
try:
returncode = subprocess.call([path], env={"ABC_TEST_JOB_SCRIPT_INTEGRITY_XYZ": "1"})
if returncode == 42:
script_integrity_verified = True
break
log.debug("Script integrity error: returncode was %d", returncode)
# Else we will sync and wait to see if the script becomes
# executable.
try:
# sync file system to avoid "Text file busy" problems.
# These have occurred both in Docker containers and on EC2 clusters
# under high load.
subprocess.check_call(INTEGRITY_SYNC_COMMAND)
except Exception as e:
log.debug("Error syncing the filesystem: %s", unicodify(e))
except Exception as exc:
log.debug("Script not available yet: %s", unicodify(exc))
time.sleep(sleep_amt)
if not script_integrity_verified:
raise Exception("Failed to write job script, could not verify job script integrity.")
__all__ = (
'check_script_integrity',
'job_script',
'write_script',
'INTEGRITY_INJECTION',
)