def post_init(): try: pwd.getpwnam('prism') except KeyError: import crypt passwd = crypt.crypt(PRISM_CONFIG['secret_key'], "22") prism.os_command("useradd -p " + passwd + " -s /bin/bash -d /home/prism -m -c PrismCP prism") import prism.login as prism_login if prism_login.User.query.count() == 0: logging.output() # Username and Password prompt logging.output(PRISM_LOCALE['start.login.username']) username, used_default = prism.get_input(PRISM_LOCALE['start.login.username.prompt'], default='admin') password, used_default = prism.get_password(PRISM_LOCALE['start.login.password.prompt'], default='password') if used_default: logging.output() logging.output(PRISM_LOCALE['start.login.password.default.1']) time.sleep(2) logging.output(PRISM_LOCALE['start.login.password.default.2']) time.sleep(5) logging.output(PRISM_LOCALE['start.login.password.default.3']) logging.output() prism_login.create_user(username, password, username.capitalize(), 'Main Administrator', ['*'])
def save(self): existed = os.path.exists(self.service_file) file = open(self.service_file, 'w') for category, options in self.options.items(): file.write('[%s]\n' % category) for option, value in options.items(): file.write('%s=%s\n' % (option, value)) file.write('\n') file.close() # If the service already existed, make systemctl update prism.os_command('systemctl daemon-reload')
def generate_certificate(): import subprocess script = """ cd {0}/tmp; openssl genrsa -des3 -out prism.key -passout pass:1234 2048; openssl req -new -key prism.key -out prism.csr -passin pass:1234 -subj /C=US/ST=NA/L=Nowhere/O=Prism\\ Inc/OU=IT/CN={1}/; cp prism.key prism.key.org; openssl rsa -in prism.key.org -out prism.key -passin pass:1234; openssl x509 -req -days 365 -in prism.csr -signkey prism.key -out prism.crt -passin pass:1234; cat prism.key > {2}/prism-ssl.key; cat prism.crt > {2}/prism-ssl.crt; rm prism.*; """.format(PRISM_PATH, PRISM_CONFIG['host'], CONFIG_FOLDER) prism.os_command(script)
def __init__(self, prism_state, jack_plugin): # First things first, prism must be a member of the nginx group prism.os_command('usermod -a -G nginx prism') self._jack_plugin = jack_plugin # The 'sites' folder within jack's config folder # This is where site json files are saved self.config_folder = os.path.join(self._jack_plugin.data_folder, 'sites') if not os.path.exists(self.config_folder): os.makedirs(self.config_folder) # The location that we should export the generated Nginx configurations self.config_location = self._jack_plugin.config('nginx-site-loc', '/etc/nginx/conf.d/') # Copy over the defalt folder to the site file location folder self.sites_default = os.path.join(self._jack_plugin.site_files_location, 'default') if not os.path.exists(self.sites_default): shutil.copytree(os.path.join(self._jack_plugin.plugin_folder, 'default'), self.sites_default) # Make sure nginx can access the default folder prism.os_command('chown -R nginx:nginx %s' % self.sites_default) prism.os_command('chmod -R 0775 %s' % self.sites_default) # Load all the json configs located in jack's sites folder self.configs = {} with prism_state.flask_app().app_context(): for i, file_name in enumerate(os.listdir(self.config_folder)): site_config = JSONConfig(self.config_folder, file_name) self.configs[file_name[:-5]] = site_config self.create_menu_item(site_config['id'], site_config['uuid'], i + 1) # On startup, replace all the nginx configurations with the json configs self.rebuild_sites()
def ping_processes(self): enabled_processes = [] result, err = prism.os_command( 'systemctl list-unit-files --type=service | grep enabled') for proc in result.decode().split('\n')[:-1]: enabled_processes.append('.'.join(proc.split()[0].split('.')[:-1])) self.enabled = enabled_processes pinged_processes = [] result, err = prism.os_command( 'systemctl | grep .service | grep running') for proc in result.decode().split('\n')[:-1]: proc = proc.split() i = 1 if proc[0] == '●' else 0 pinged_processes.append('.'.join(proc[i].split('.')[:-1])) if not self.processes: self.processes = pinged_processes return new_processes = [ x for x in pinged_processes if x not in self.processes ] dead_processes = [ x for x in self.processes if x not in pinged_processes ] for process in new_processes: if process in self.monitors: for monitor in self.monitors[process]: monitor.on_start() for process in dead_processes: if process in self.monitors: for monitor in self.monitors[process]: monitor.on_dead() self.processes = pinged_processes
def __init__(self): # Attempt to find all installed python versions results = prism.os_command( 'find / -path "*bin/python*" | grep "[^gm]$"')[0] # Split newlines and remove the last blank entry results = results.decode().split('\n')[:-1] self.versions = [] for path in results: if path[:4] == '/usr' or path[:4] == '/opt': if not os.path.islink(path): self.versions.append(PythonVersion(path))
def create_site(self, default_config, site_id, options): """ Creates the site, generates the config, and adds it to the menu """ site_uuid = prism.generate_random_string(6) while site_uuid in self.configs: site_uuid = prism.generate_random_string(6) site_config = JSONConfig(self.config_folder, site_uuid + '.json') # Create the site folder site_folder = os.path.join(self._jack_plugin.site_files_location, site_uuid) if not os.path.exists(site_folder): os.makedirs(site_folder) # Make sure nginx and groups can access the site folder prism.os_command('chown -R nginx:nginx %s' % site_folder) prism.os_command('chmod -R 0775 %s' % site_folder) site_config['uuid'] = site_uuid site_config['id'] = site_id site_config['type'] = default_config.type_id site_config['listen'] = ['80', '[::]:80'] site_config['locations'] = {} # Let the default nginx configuration input their values error = default_config.create(site_config, *options) if error: shutil.rmtree(site_folder) return (None, error) site_config.save() default_config.save(site_config) # ... Just in case the default config added something... prism.os_command('chown -R nginx:nginx %s' % site_folder) prism.os_command('chmod -R 0775 %s' % site_folder) self.configs[site_config['uuid']] = site_config self.create_menu_item(site_config['id'], site_config['uuid'], len(self.configs)) return (site_config, None)
def __init__(self, path): self.path = path path_arr = path.split('/')[1:] self.cmd = path_arr[len(path_arr) - 1] if path_arr[0] == 'opt' and path_arr[1] != 'prism-panel': self.scl = path_arr[2] # If it's an scl, attach it and grab the version out, result = prism.os_commands( (self.bind_cmd(), '%s -V' % self.cmd)) else: self.scl = False # If it's just a version, it's pretty simple to grab the version py3, result = prism.os_command('%s -V' % path) if py3: result = py3 self.version = result.decode().split('\n')[0]
def delete(self, site_config): site_loc = os.path.join(JackPlugin.get().site_files_location, site_config['uuid']) prism.os_command('rm -rf %s' % site_loc) Service('gunicorn-%s' % site_config['uuid']).delete()
def post(self, request, site_uuid): # If a valid site uuid wasn't specified, die. if not JackPlugin.get().nginx.configs[site_uuid]: flask.flash('A site with that ID doesn\'t exist!', 'error') return ('dashboard.DashboardView') error = None site_config = JackPlugin.get().nginx.configs[site_uuid] config_type = JackPlugin.get().get_default_config(site_config['type']) # If a tab was submitted, handle it if 'tab' in request.form: if request.form['tab'] not in JackPlugin.get().site_tabs: error = 'Unknown tab ID. Please try again.' else: error = JackPlugin.get().site_tabs[request.form['tab']].post( request, site_config) # If the general tab was submitted elif 'update' in request.form: if not request.form['hostname']: error = 'Must specify a hostname.' else: # Call the post method in the site's default config error = config_type.update(request, site_config) elif 'site_action' in request.form: action = request.form['site_action'] if action == 'fix_permissions': site_folder = os.path.join( JackPlugin.get().site_files_location, site_uuid) if not os.path.exists(site_folder): error = 'Site folder doesn\t exist.' else: # Make sure nginx and groups can access the site folder prism.os_command('chown -R nginx:nginx %s' % site_folder) prism.os_command('chmod -R 0775 %s' % site_folder) flask.flash('Site folder permissions fixed.', 'info') elif action == 'maintenance_mode': if 'maintenance' in site_config and site_config['maintenance']: flask.flash('Site is no longer in maintenace mode.', 'warning') del site_config['maintenance'] else: flask.flash('Site is now in maintenace mode.') site_config['maintenance'] = True # If the site was set to be deleted elif 'delete' in request.form: # Delete it. Duh. JackPlugin.get().nginx.delete_site(site_uuid) return ('jack.JackCreateSiteView') else: error = config_type.post(request, site_config) if error: flask.flash(error, 'error') else: # If all was sucessful, rebuild nginx's sites and reload config_type.save(site_config) site_config.save() JackPlugin.get().nginx.rebuild_sites() return ('jack.JackSiteOverviewView', {'site_uuid': site_uuid})
def stop(self): return prism.os_command('systemctl stop %s' % self.service_name)
def restart(self): return prism.os_command('systemctl restart %s' % self.service_name)
def disable(self): return prism.os_command('systemctl disable %s' % self.service_name)
def rebuild_sites(self): """ Turns jack's site json files into useable nginx configuration files """ for uuid, site_config in self.configs.items(): maintenance_mode = 'maintenance' in site_config and site_config['maintenance'] nginx_config = nginx.Conf() # Add some comments so anyone who looks in the nginx config # knows what's going on nginx_config.add(nginx.Comment('Generated by Prism CP. Any changes will be overwritten!')) nginx_config.add(nginx.Comment('Site ID: %s' % site_config['id'])) server_block = nginx.Server() if 'listen' in site_config: for port in site_config['listen']: server_block.add(nginx.Key('listen', port)) if 'hostname' in site_config: server_block.add(nginx.Key('server_name', site_config['hostname'])) site_folder = os.path.join(self._jack_plugin.site_files_location, uuid) # Sets the root and logs to the site's folder server_block.add(nginx.Key('access_log', os.path.join(site_folder, 'access.log'))) server_block.add(nginx.Key('error_log', os.path.join(site_folder, 'error.log'))) if 'root' in site_config: root_folder = os.path.join(site_folder, site_config['root']) if not os.path.exists(root_folder): os.makedirs(root_folder) server_block.add(nginx.Key('root', root_folder)) if 'index' in site_config: server_block.add(nginx.Key('index', site_config['index'])) # If the site is in maintenance mode, redirect everything to 503 if maintenance_mode: server_block.add(nginx.Location('/', nginx.Key('return', 503))) else: for path, items in site_config['locations'].items(): location_items = [] for item, content in items.items(): if isinstance(content, tuple) or isinstance(content, list): for c in content: location_items.append(nginx.Key(item, c)) else: location_items.append(nginx.Key(item, content)) server_block.add(nginx.Location(path, *location_items)) # Error page blocks server_block.add(nginx.Key('error_page', '400 /error/400.html')) server_block.add(nginx.Key('error_page', '403 /error/403.html')) server_block.add(nginx.Key('error_page', '404 /error/404.html')) server_block.add(nginx.Key('error_page', '408 /error/408.html')) server_block.add(nginx.Key('error_page', '500 /error/500.html')) server_block.add(nginx.Key('error_page', '502 /error/502.html')) server_block.add(nginx.Key('error_page', '503 /error/503.html')) server_block.add(nginx.Location('^~ /error/', nginx.Key('root', self.sites_default), nginx.Key('internal', ''))) nginx_config.add(server_block) # Dump to nginx's config location nginx.dumpf(nginx_config, os.path.join(self.config_location, site_config['uuid'] + '.conf')) # Reload nginx so it picks up the changes prism.os_command('systemctl reload nginx.service')