Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
global cfg
cfg = pkconfig.init(
hosts=pkconfig.RequiredUnlessDev(tuple(), tuple, 'execution hosts'),
image=('radiasoft/sirepo', str, 'docker image to run all jobs'),
parallel=dict(
cores=(1, int, 'cores per parallel job'),
gigabytes=(1, int, 'gigabytes per parallel job'),
slots_per_host=(1, int, 'parallel slots per node'),
),
sequential=dict(
gigabytes=(1, int, 'gigabytes per sequential job'),
slots_per_host=(1, int, 'sequential slots per node'),
),
tls_dir=pkconfig.RequiredUnlessDev(None, _cfg_tls_dir, 'directory containing host certs'),
dev_volumes=(pkconfig.channel_in('dev'), bool, 'mount ~/.pyenv, ~/.local and ~/src for development'),
)
if not cfg.tls_dir or not cfg.hosts:
_init_dev_hosts()
_init_hosts()
return DockerDriver.init_class()
def default_command():
global cfg
cfg = pkconfig.init(
debug=(pkconfig.channel_in('dev'), bool, 'run supervisor in debug mode'),
ip=(sirepo.job.DEFAULT_IP, str, 'ip address to listen on'),
port=(sirepo.job.DEFAULT_PORT, int, 'what port to listen on'),
)
sirepo.job_supervisor.init()
pkio.mkdir_parent(sirepo.job.DATA_FILE_ROOT)
pkio.mkdir_parent(sirepo.job.LIB_FILE_ROOT)
app = tornado.web.Application(
[
(sirepo.job.AGENT_URI, _AgentMsg),
(sirepo.job.SERVER_URI, _ServerReq),
(sirepo.job.SERVER_PING_URI, _ServerPing),
(sirepo.job.DATA_FILE_URI + '/(.*)', _DataFileReq),
],
debug=cfg.debug,
static_path=sirepo.job.SUPERVISOR_SRV_ROOT.join(sirepo.job.LIB_FILE_URI),
# tornado expects a trailing slash
def _crypto(self):
if not self.crypto:
if cfg.private_key is None:
assert pkconfig.channel_in('dev'), \
'must configure private_key in non-dev channel={}'.format(pkconfig.cfg.channel)
cfg.private_key = base64.urlsafe_b64encode(b'01234567890123456789012345678912')
assert len(base64.urlsafe_b64decode(cfg.private_key)) == 32, \
'private_key must be 32 characters and encoded with urlsafe_b64encode'
self.crypto = cryptography.fernet.Fernet(cfg.private_key)
return self.crypto
def _cfg_hosts(value):
value = pkconfig.parse_tuple(value)
if value:
return value
assert pkconfig.channel_in('dev'), \
'required config'
return None
def _init_dev_hosts():
assert pkconfig.channel_in('dev')
from sirepo import srdb
assert not (cfg.tls_dir or cfg.hosts), \
'neither cfg.tls_dir and cfg.hosts nor must be set to get auto-config'
# dev mode only; see _cfg_tls_dir and _cfg_hosts
cfg.tls_dir = srdb.root().join('docker_tls')
cfg.hosts = ('localhost.localdomain',)
d = cfg.tls_dir.join(cfg.hosts[0])
if d.check(dir=True):
return
pkdlog('initializing docker dev env; initial docker pull will take a few minutes...')
d.ensure(dir=True)
for f in 'key.pem', 'cert.pem':
o = subprocess.check_output(['sudo', 'cat', '/etc/docker/tls/' + f]).decode('utf-8')
assert o.startswith('-----BEGIN'), \
'incorrect tls file={} content={}'.format(f, o)
def app_version():
"""Force the version to be dynamic if running in dev channel
Returns:
str: chronological version
"""
if pkconfig.channel_in('dev'):
return _timestamp()
return SCHEMA_COMMON.version
def cfg_job_class(value):
"""Return job queue class based on name
Args:
value (object): May be class or str.
Returns:
object: `Background` or `Celery` class.
"""
if isinstance(value, type) and issubclass(value, (Celery, Background)):
# Already initialized but may call initializer with original object
return value
if value == 'Celery':
if pkconfig.channel_in('dev'):
_assert_celery()
return Celery
elif value == 'Docker':
return Docker
elif value == 'Background':
signal.signal(signal.SIGCHLD, Background._sigchld_handler)
return Background
elif value is None:
return None
else:
raise AssertionError('{}: unknown job_class'.format(value))
@pkconfig.parse_none
def _cfg_parse_modules(value):
global _CLASSES, _DEFAULT_CLASS
assert not _CLASSES
s = pkconfig.parse_set(value)
if not s:
s = frozenset(('docker',))
if pkconfig.channel_in('dev'):
s = frozenset(('local',))
assert not {'local', 'docker'}.issubset(s), \
'modules={} can only contain one of "docker" or "local"'.format(s)
assert 'docker' in s or 'local' in s, \
'modules={} must contain "docker" or "local"'.format(s)
p = pkinspect.this_module().__name__
_CLASSES = PKDict()
for n in s:
m = importlib.import_module(pkinspect.module_name_join((p, n)))
_CLASSES[n] = m.init_class()
if 'docker' in s:
_DEFAULT_CLASS = _CLASSES['docker']
else:
_DEFAULT_CLASS = _CLASSES['local']
return s
def api_robotsTxt():
"""Disallow the app (dev, prod) or / (alpha, beta)"""
global _ROBOTS_TXT
if not _ROBOTS_TXT:
# We include dev so we can test
if pkconfig.channel_in('prod', 'dev'):
u = [
sirepo.uri.api('root', params={'simulation_type': x})
for x in sorted(feature_config.cfg().sim_types)
]
else:
u = ['/']
_ROBOTS_TXT = ''.join(
['User-agent: *\n'] + ['Disallow: /{}\n'.format(x) for x in u],
)
return flask.Response(_ROBOTS_TXT, mimetype='text/plain')