2015-05-14 20:47:38 +00:00
|
|
|
""" Superuser Config API. """
|
|
|
|
|
2015-01-04 19:38:41 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2015-01-23 22:19:15 +00:00
|
|
|
import signal
|
2017-12-01 19:04:43 +00:00
|
|
|
import subprocess
|
2015-01-04 19:38:41 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
from flask import abort
|
2015-01-04 19:38:41 +00:00
|
|
|
|
2018-08-17 14:57:22 +00:00
|
|
|
from app import app, config_provider
|
2015-01-04 19:38:41 +00:00
|
|
|
from auth.permissions import SuperUserPermission
|
2017-07-24 20:30:44 +00:00
|
|
|
from endpoints.api.suconfig_models_pre_oci import pre_oci_model as model
|
2018-08-17 14:57:22 +00:00
|
|
|
from endpoints.api import (ApiResource, nickname, resource, internal_only, show_if, verify_not_prod)
|
2015-01-04 19:38:41 +00:00
|
|
|
|
|
|
|
import features
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2015-01-04 19:38:41 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2015-01-04 19:38:41 +00:00
|
|
|
def database_is_valid():
|
2015-01-08 17:53:36 +00:00
|
|
|
""" Returns whether the database, as configured, is valid. """
|
2015-01-23 22:19:15 +00:00
|
|
|
if app.config['TESTING']:
|
|
|
|
return False
|
|
|
|
|
2017-07-24 20:30:44 +00:00
|
|
|
return model.is_valid()
|
2015-01-04 19:38:41 +00:00
|
|
|
|
2015-01-23 22:19:15 +00:00
|
|
|
|
2015-01-04 19:38:41 +00:00
|
|
|
def database_has_users():
|
2015-01-08 17:53:36 +00:00
|
|
|
""" Returns whether the database has any users defined. """
|
2017-07-24 20:30:44 +00:00
|
|
|
return model.has_users()
|
2015-01-04 19:38:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/superuser/registrystatus')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserRegistryStatus(ApiResource):
|
|
|
|
""" Resource for determining the status of the registry, such as if config exists,
|
|
|
|
if a database is configured, and if it has any defined users.
|
|
|
|
"""
|
|
|
|
@nickname('scRegistryStatus')
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2015-01-04 19:38:41 +00:00
|
|
|
def get(self):
|
2015-01-23 22:19:15 +00:00
|
|
|
""" Returns the status of the registry. """
|
2015-02-17 16:31:50 +00:00
|
|
|
|
2016-12-05 18:22:14 +00:00
|
|
|
# If we have SETUP_COMPLETE, then we're ready to go!
|
|
|
|
if app.config.get('SETUP_COMPLETE', False):
|
|
|
|
return {
|
2017-01-13 20:50:50 +00:00
|
|
|
'provider_id': config_provider.provider_id,
|
2016-12-05 18:22:14 +00:00
|
|
|
'requires_restart': config_provider.requires_restart(app.config),
|
|
|
|
'status': 'ready'
|
|
|
|
}
|
|
|
|
|
2015-01-23 22:19:15 +00:00
|
|
|
# If there is no conf/stack volume, then report that status.
|
2015-07-24 18:52:19 +00:00
|
|
|
if not config_provider.volume_exists():
|
2015-01-23 22:19:15 +00:00
|
|
|
return {
|
|
|
|
'status': 'missing-config-dir'
|
|
|
|
}
|
|
|
|
|
|
|
|
# If there is no config file, we need to setup the database.
|
2015-07-27 15:17:44 +00:00
|
|
|
if not config_provider.config_exists():
|
2015-01-23 22:19:15 +00:00
|
|
|
return {
|
|
|
|
'status': 'config-db'
|
|
|
|
}
|
|
|
|
|
|
|
|
# If the database isn't yet valid, then we need to set it up.
|
|
|
|
if not database_is_valid():
|
|
|
|
return {
|
|
|
|
'status': 'setup-db'
|
|
|
|
}
|
|
|
|
|
2015-01-04 19:38:41 +00:00
|
|
|
return {
|
2015-01-23 22:19:15 +00:00
|
|
|
'status': 'create-superuser' if not database_has_users() else 'config'
|
2015-01-04 19:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-23 22:19:15 +00:00
|
|
|
class _AlembicLogHandler(logging.Handler):
|
|
|
|
def __init__(self):
|
|
|
|
super(_AlembicLogHandler, self).__init__()
|
|
|
|
self.records = []
|
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
self.records.append({
|
|
|
|
'level': record.levelname,
|
|
|
|
'message': record.getMessage()
|
|
|
|
})
|
|
|
|
|
2017-12-01 19:04:43 +00:00
|
|
|
# From: https://stackoverflow.com/a/44712205
|
|
|
|
def get_process_id(name):
|
|
|
|
"""Return process ids found by (partial) name or regex.
|
|
|
|
|
|
|
|
>>> get_process_id('kthreadd')
|
|
|
|
[2]
|
|
|
|
>>> get_process_id('watchdog')
|
|
|
|
[10, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61] # ymmv
|
|
|
|
>>> get_process_id('non-existent process')
|
|
|
|
[]
|
|
|
|
"""
|
|
|
|
child = subprocess.Popen(['pgrep', name], stdout=subprocess.PIPE, shell=False)
|
|
|
|
response = child.communicate()[0]
|
|
|
|
return [int(pid) for pid in response.split()]
|
|
|
|
|
2015-01-23 22:19:15 +00:00
|
|
|
|
|
|
|
@resource('/v1/superuser/shutdown')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserShutdown(ApiResource):
|
|
|
|
""" Resource for sending a shutdown signal to the container. """
|
|
|
|
|
|
|
|
@verify_not_prod
|
|
|
|
@nickname('scShutdownContainer')
|
|
|
|
def post(self):
|
|
|
|
""" Sends a signal to the phusion init system to shut down the container. """
|
|
|
|
# Note: This method is called to set the database configuration before super users exists,
|
|
|
|
# so we also allow it to be called if there is no valid registry configuration setup.
|
|
|
|
if app.config['TESTING'] or not database_has_users() or SuperUserPermission().can():
|
|
|
|
# Note: We skip if debugging locally.
|
|
|
|
if app.config.get('DEBUGGING') == True:
|
|
|
|
return {}
|
|
|
|
|
2017-12-01 19:04:43 +00:00
|
|
|
os.kill(get_process_id('my_init')[0], signal.SIGINT)
|
2015-01-23 22:19:15 +00:00
|
|
|
return {}
|
|
|
|
|
|
|
|
abort(403)
|