Skip to content
Snippets Groups Projects
Commit 1f3b2fa3 authored by Kevin S. Hahn's avatar Kevin S. Hahn
Browse files

disables apps features if apps_path not defined

parent bfb34763
No related branches found
No related tags found
No related merge requests found
...@@ -136,7 +136,7 @@ if __name__ == '__main__': ...@@ -136,7 +136,7 @@ if __name__ == '__main__':
arg_parser.add_argument('--port', default='8080', help='TCP port to listen on [8080]') arg_parser.add_argument('--port', default='8080', help='TCP port to listen on [8080]')
arg_parser.add_argument('--db_uri', help='SciTran DB URI', default='mongodb://localhost/scitran') arg_parser.add_argument('--db_uri', help='SciTran DB URI', default='mongodb://localhost/scitran')
arg_parser.add_argument('--data_path', help='path to storage area', required=True) arg_parser.add_argument('--data_path', help='path to storage area', required=True)
arg_parser.add_argument('--apps_path', help='path to apps storage', required=True) arg_parser.add_argument('--apps_path', help='path to apps storage')
arg_parser.add_argument('--log_level', help='log level [info]', default='info') arg_parser.add_argument('--log_level', help='log level [info]', default='info')
arg_parser.add_argument('--ssl_cert', help='path to SSL certificate file, containing private key and certificate chain', required=True) arg_parser.add_argument('--ssl_cert', help='path to SSL certificate file, containing private key and certificate chain', required=True)
arg_parser.add_argument('--site_id', help='site ID for Scitran Central [local]', default='local') arg_parser.add_argument('--site_id', help='site ID for Scitran Central [local]', default='local')
...@@ -163,6 +163,11 @@ if __name__ == '__main__': ...@@ -163,6 +163,11 @@ if __name__ == '__main__':
os.makedirs(app.config['quarantine_path']) os.makedirs(app.config['quarantine_path'])
if not os.path.exists(app.config['upload_path']): if not os.path.exists(app.config['upload_path']):
os.makedirs(app.config['upload_path']) os.makedirs(app.config['upload_path'])
if not application.config['apps_path']:
log.warning('apps_path is not defined. Apps functionality disabled')
else:
if not os.path.exists(application.config['apps_path']):
os.makedirs(application.config['apps_path'])
db_client = pymongo.MongoReplicaSetClient(args.db_uri) if 'replicaSet' in args.db_uri else pymongo.MongoClient(args.db_uri) db_client = pymongo.MongoReplicaSetClient(args.db_uri) if 'replicaSet' in args.db_uri else pymongo.MongoClient(args.db_uri)
app.db = db_client.get_default_database() app.db = db_client.get_default_database()
......
...@@ -18,7 +18,7 @@ os.umask(0o022) ...@@ -18,7 +18,7 @@ os.umask(0o022)
ap = argparse.ArgumentParser() ap = argparse.ArgumentParser()
ap.add_argument('--db_uri', help='SciTran DB URI', required=True) ap.add_argument('--db_uri', help='SciTran DB URI', required=True)
ap.add_argument('--data_path', help='path to storage area', required=True) ap.add_argument('--data_path', help='path to storage area', required=True)
ap.add_argument('--apps_path', help='path to apps storage', required=True) ap.add_argument('--apps_path', help='path to apps storage')
ap.add_argument('--ssl_cert', help='path to SSL certificate file, containing private key and certificate chain', required=True) ap.add_argument('--ssl_cert', help='path to SSL certificate file, containing private key and certificate chain', required=True)
ap.add_argument('--api_uri', help='api uri, with https:// prefix') ap.add_argument('--api_uri', help='api uri, with https:// prefix')
ap.add_argument('--site_id', help='site ID for Scitran Central [local]', default='local') ap.add_argument('--site_id', help='site ID for Scitran Central [local]', default='local')
...@@ -49,6 +49,11 @@ if not os.path.exists(application.config['quarantine_path']): ...@@ -49,6 +49,11 @@ if not os.path.exists(application.config['quarantine_path']):
os.makedirs(application.config['quarantine_path']) os.makedirs(application.config['quarantine_path'])
if not os.path.exists(application.config['upload_path']): if not os.path.exists(application.config['upload_path']):
os.makedirs(application.config['upload_path']) os.makedirs(application.config['upload_path'])
if not application.config['apps_path']:
log.warning('apps_path is not defined. Apps functionality disabled')
else:
if not os.path.exists(application.config['apps_path']):
os.makedirs(application.config['apps_path'])
# connect to db # connect to db
application.db = None application.db = None
......
...@@ -20,6 +20,7 @@ log = logging.getLogger('nimsapi.jobs') ...@@ -20,6 +20,7 @@ log = logging.getLogger('nimsapi.jobs')
import tempdir as tempfile import tempdir as tempfile
import base import base
import util
# TODO: create schemas to verify various json payloads # TODO: create schemas to verify various json payloads
APP_SCHEMA = { APP_SCHEMA = {
...@@ -64,18 +65,27 @@ class Apps(base.RequestHandler): ...@@ -64,18 +65,27 @@ class Apps(base.RequestHandler):
"""Return information about the all the apps.""" """Return information about the all the apps."""
def get(self): def get(self):
apps_path = self.app.config.get('apps_path')
if not apps_path:
self.abort(503, 'GET api/apps/<id> unavailable. apps_path not defined')
return list(self.app.db.apps.find()) return list(self.app.db.apps.find())
def count(self): def count(self):
apps_path = self.app.config.get('apps_path')
if not apps_path:
self.abort(503, 'GET api/apps/<id> unavailable. apps_path not defined')
return self.app.db.apps.count() return self.app.db.apps.count()
def post(self): def post(self):
"""Create a new App.""" """Create a new App."""
# this handles receive and writing the file # this handles receive and writing the file
# but the the json validation and database is handled by util. # but the the json validation and database is handled by util.
apps_path = self.app.config['apps_path']
if not apps_path:
self.abort(503, 'POST api/apps unavailable. apps_path not defined')
if self.public_request: # TODO: how to handle auth during bootstrap? if self.public_request: # TODO: how to handle auth during bootstrap?
self.abort(403, 'must be logged in to upload apps') self.abort(403, 'must be logged in to upload apps')
apps_path = self.app.config['apps_path']
app_meta = None app_meta = None
with tempfile.TemporaryDirectory(prefix='.tmp', dir=apps_path) as tempdir_path: with tempfile.TemporaryDirectory(prefix='.tmp', dir=apps_path) as tempdir_path:
hash_ = hashlib.sha1() hash_ = hashlib.sha1()
...@@ -100,21 +110,27 @@ class Apps(base.RequestHandler): ...@@ -100,21 +110,27 @@ class Apps(base.RequestHandler):
except (ValueError, jsonschema.ValidationError) as e: except (ValueError, jsonschema.ValidationError) as e:
self.abort(400, str(e)) self.abort(400, str(e))
util.insert_app(self.app.db, app_temp, apps_path, app_meta=app_meta) # pass meta info, prevent re-reading util.insert_app(self.app.db, app_temp, apps_path, app_meta=app_meta) # pass meta info, prevent re-reading
log.debug('Recieved App: %s' % app_info.get('_id')) log.debug('Recieved App: %s' % app_meta.get('_id'))
class App(base.RequestHandler): class App(base.RequestHandler):
def get(self, _id): def get(self, _id):
# TODO: auth? should viewing apps be restricted? # TODO: auth? should viewing apps be restricted?
apps_path = self.app.config.get('apps_path')
if not apps_path:
self.abort(503, 'GET api/apps/<id> unavailable. apps_path not defined')
return self.app.db.apps.find_one({'_id': _id}) return self.app.db.apps.find_one({'_id': _id})
def get_file(self, _id): def get_file(self, _id):
apps_path = self.app.config.get('apps_path')
if not apps_path:
self.abort(503, 'GET api/apps/<id> unavailable. apps_path not defined')
if self.public_request: # this will most often be a drone request if self.public_request: # this will most often be a drone request
self.abort(403, 'must be logged in to download apps') self.abort(403, 'must be logged in to download apps')
name, version = _id.split(':') name, version = _id.split(':')
fn = '%s-%s.tar' % (name, version) fn = '%s-%s.tar' % (name, version)
fp = os.path.join(self.app.config['apps_path'], name, fn) fp = os.path.join(apps_path, name, fn)
self.response.app_iter = open(fp, 'rb') self.response.app_iter = open(fp, 'rb')
self.response.headers['Content-Length'] = str(os.path.getsize(fp)) # must be set after setting app_iter self.response.headers['Content-Length'] = str(os.path.getsize(fp)) # must be set after setting app_iter
self.response.headers['Content-Type'] = 'application/octet-stream' self.response.headers['Content-Type'] = 'application/octet-stream'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment