class MainController(RESTController): @Logger(logger=logger) def __init__(self, *args, **kwargs): super(MainController, self).__init__(self, *args, **kwargs) self._prepare_page() @Logger(logger=logger) def _prepare_page(self): self._page = Page(None, tmpl_env, self._request_context) self._page.set_cssfiles(CSS_FILES) self._page.set_jslibs(JS_LIBS) if ('authenticated' in self._request_context.session and self._request_context.session.authenticated): user_info = {} user_info['username'] = self._request_context.session.username user_info['realname'] = self._request_context.session.realname user_info['is_dc2admin'] =\ self._request_context.session.is_dc2admin self._page.add_page_data({'user': user_info}) self._page.add_page_data({'admin_is_link': True}) @Logger(logger=logger) def _index(self, *args, **kwargs): verb = kwargs.get('verb', None) self._page.template_name = verb['template'] self._page.set_title('Dashboard') self._page.set_action('index') self._fill_backends() result = self._prepare_output( verb['request_type'], verb['request_content_type'], output={'content': self._page.render()}) return result @Logger(logger=logger) def _fill_backends(self): backend_list = backends.backend_list() self._page.add_page_data({'backendlist': backend_list})
class ServerController(RESTController): @Logger(logger=logger) def __init__(self, *args, **kwargs): super(ServerController, self).__init__(*args, **kwargs) self._prepare_page() @Logger(logger=logger) def _prepare_page(self): self._page = Page(None, tmpl_env, self._request_context) self._page.set_cssfiles(CSS_FILES) self._page.set_jslibs(JS_LIBS) if ('authenticated' in self._request_context.session and self._request_context.session.authenticated): user_info = {} user_info['username'] = self._request_context.session.username user_info['realname'] = self._request_context.session.realname user_info[ 'is_dc2admin'] = self._request_context.session.is_dc2admin self._page.add_page_data({'user': user_info}) self._page.add_page_data({'admin_is_link': True}) self._fill_backends() self._page.set_page_value('controller_path', self._controller_path) @Logger(logger=logger) def _init_backend(self): params = web.input() self._backend_id = params.get('backend_id', None) self._page.add_page_data({'backend_id': self._backend_id}) self._backend = backends.backend_get({'_id': self._backend_id}) self._transport = get_xmlrpc_transport( self._backend['backend_url'], self._backend['is_kerberos']) self._servers = Servers(self._transport) self._macs = Macs(self._transport) self._ribs = Ribs(self._transport) self._hosts = Hosts(self._transport) @needs_auth @Logger(logger=logger) def _show(self, *args, **kwargs): verb = kwargs.get('verb', None) server_id = None self._init_backend() self._page.template_name = verb['template'] self._page.set_action('show') self._page.set_page_value('show_button', True) request_data = verb.get('request_data', None) if request_data is not None: server_id = request_data.get('id', None) if server_id is not None: server = self._servers.get(id=server_id) macs = self._macs.get(server_id=server_id) rib = self._ribs.get(server_id=server_id) host = self._hosts.get(server_id=server_id) rib_def = ribs.rib_list() self._page.set_title('Server {0} ({1} - {2})'.format( server['serial_no'], server['manufacturer'], server['product_name'])) self._page.add_page_data({ 'server': server, 'macs': macs, 'ribs': rib, 'host': host, 'ribdef': rib_def }) result = self._prepare_output( verb['request_type'], verb['request_content_type'], output={'content': self._page.render()}) return result @needs_auth @Logger(logger=logger) def _edit(self, *args, **kwargs): verb = kwargs.get('verb', None) self._init_backend() self._page.template_name = verb['template'] self._page.set_action('edit') self._page.set_page_value('update_button', True) request_data = verb.get('request_data', None) server_id = None if request_data is not None: server_id = request_data.get('id', None) if server_id is not None: server = self._servers.get(id=server_id) macs = self._macs.get(server_id=server_id) rib = self._ribs.get(server_id=server_id) rib_def = ribs.rib_list() self._page.set_title('Edit Server {0} ({1} - {2})'.format( server['serial_no'], server['manufacturer'], server['product_name'])) self._page.add_page_data({ 'entry_id': server['_id'], 'server': server, 'macs': macs, 'ribs': rib, 'ribdef': rib_def, }) result = self._prepare_output( verb['request_type'], verb['request_content_type'], output={'content': self._page.render()}) return result @needs_auth @csrf_protected @Logger(logger=logger) def _update(self, *args, **kwargs): verb = kwargs.get('verb', None) self._init_backend() params = web.data() data = json.loads(params)['result'] server = {} server['_id'] = data['server_id'].strip() server['uuid'] = data['uuid'].strip() server['serial_no'] = data['serial_no'].strip() server['manufacturer'] = data['manufacturer'].strip() server['product_name'] = data['product_name'].strip() server['location'] = data['location'].strip() server['asset_tags'] = data['asset_tags'].strip() self._servers.update(server=server) for key in data['mac']: mac = {} if key != 'new': if key.find('new') == -1 and key != 'new': mac['_id'] = key.strip() if (data['mac'][key]['mac_addr'] != '' and data['mac'][key]['device_name'] != ''): mac['server_id'] = data['server_id'].strip() mac['mac_addr'] = data['mac'][key]['mac_addr'].strip() mac['device_name'] = data['mac'][ key]['device_name'].strip() if '_id' in mac: self._macs.update(mac=mac) else: self._macs.add(mac=mac) for key in data['rib']: rib = {} if key != 'new': if key.find('new') == -1: rib['_id'] = key.strip() if data['rib'][key]['remote_ip'] != '': rib['server_id'] = data['server_id'].strip() rib['remote_type'] = data['rib'][ key]['remote_type'].strip() rib['remote_ip'] = data['rib'][key]['remote_ip'].strip() if '_id' in rib: self._ribs.update(rib=rib) else: self._ribs.add(rib=rib) result = self._prepare_output( 'json', verb['request_content_type'], 'json', {'redirect': { 'url': '{0}/{1}?backend_id={2}'.format( self._controller_path, data['server_id'], self._backend_id), 'absolute': 'true'}}) return result @Logger(logger=logger) def _fill_backends(self): backend_list = backends.backend_list() self._page.add_page_data({'backendlist': backend_list})
class InstallStateController(RESTController): @Logger(logger=logger) def __init__(self, *args, **kwargs): super(InstallStateController, self).__init__(*args, **kwargs) self._prepare_page() @Logger(logger=logger) def _prepare_page(self): self._page = Page(None, tmpl_env, self._request_context) self._page.set_cssfiles(CSS_FILES) self._page.set_jslibs(JS_LIBS) if ('authenticated' in self._request_context.session and self._request_context.session.authenticated): user_info = {} user_info['username'] = self._request_context.session.username user_info['realname'] = self._request_context.session.realname user_info[ 'is_dc2admin'] = self._request_context.session.is_dc2admin self._page.add_page_data({'user': user_info}) self._page.add_page_data({'admin_is_link': True}) self._fill_backends() self._page.set_page_value('controller_path', self._controller_path) @Logger(logger=logger) def _init_backend(self): params = web.input() self._backend_id = params.get('backend_id', None) self._page.add_page_data({'backend_id': self._backend_id}) self._backend = backends.backend_get({'_id': self._backend_id}) self._transport = get_xmlrpc_transport( self._backend['backend_url'], self._backend['is_kerberos']) self._installstate = InstallState(self._transport) self._backend_settings = BackendSettings(self._transport) self._hosts = Hosts(self._transport) self._freeipa = None backendsettings = self._backend_settings.get() if backendsettings['IS_FREEIPA_ENABLED']: try: from dc2.api.dc2.addons.freeipa import Hosts as FreeIPA_Hosts except ImportError as e: print('You did not install dc2.api') print(e) sys.exit(1) self._freeipa = FreeIPA_Hosts(self._transport) @needs_auth @Logger(logger=logger) def _show(self, *args, **kwargs): verb = kwargs.get('verb', None) installstate_id = None self._init_backend() self._page.template_name = verb['template'] self._page.set_action('show') self._page.set_page_value('show_button', True) request_data = verb.get('request_data', None) if request_data is not None: installstate_id = request_data.get('id', None) if installstate_id is not None: installstate = self._installstate.get(id=installstate_id) install_methods = installmethods.installmethod_list() backendsettings = self._backend_settings.get() self._page.set_title( 'Deployment State of %s' % installstate['hostname']) self._page.add_page_data({ 'entry_id': request_data['id'], 'installstate': installstate, 'installmethods': install_methods, 'backend_settings': backendsettings }) result = self._prepare_output(verb['request_type'], verb[ 'request_content_type'], output={ 'content': self._page.render()}) return result @needs_auth @Logger(logger=logger) def _edit(self, *args, **kwargs): verb = kwargs.get('verb', None) installstate_id = None self._init_backend() self._page.template_name = verb['template'] self._page.set_action('edit') self._page.set_page_value('update_button', True) request_data = verb.get('request_data', None) if request_data is not None: installstate_id = request_data.get('id', None) if installstate_id is not None: installstate = self._installstate.get(id=installstate_id) self._page.set_title( 'Deployment State of %s' % installstate['hostname']) install_methods = installmethods.installmethod_list() backendsettings = self._backend_settings.get() self._page.add_page_data({ 'entry_id': request_data['id'], 'installstate': installstate, 'installmethods': install_methods, 'backend_settings': backendsettings }) result = self._prepare_output(verb['request_type'], verb[ 'request_content_type'], output={ 'content': self._page.render()}) return result @needs_auth @Logger(logger=logger) def _update(self, *args, **kwargs): verb = kwargs.get('verb', None) self._init_backend() result = json.loads(web.data()) installstate = {} installstate = result['result']['installstate'] installstate_rec = self._installstate.get(id=installstate['_id']) installstate_rec['status'] = installstate['status'] self._installstate.update(rec=installstate_rec) backend_settings = self._backend_settings.get() if backend_settings['IS_FREEIPA_ENABLED'] and KERBEROS_AUTH_ENABLED: if installstate['status'] == 'deploy': host = self._hosts.get(id=installstate_rec['host_id']) if self._freeipa.check('{0}.{1}'.format( host['hostname'], host['domainname'])): ipa_result = self._freeipa.delete( '{0}.{1}'.format(host['hostname'], host['domainname'])) ipa_info = {'description': 'Auto-Added from DC2', 'random': True} if FREEIPA_FORCE_ADD: ipa_info['force'] = True ipa_info['no_reverse'] = True print('IPA Info {0}'.format(ipa_info)) ipa_result = self._freeipa.add( '{0}.{1}'.format( host['hostname'], host['domainname']), ipa_info) print('IPA Result {0}'.format(ipa_result)) result = self._prepare_output( 'json', verb['request_content_type'], 'json', { 'redirect': {'url': '{0}/{1}?backend_id={2}'.format( self._controller_path, installstate['_id'], self._backend_id), 'absolute': 'true'}}) return result @Logger(logger=logger) def _fill_backends(self): backend_list = backends.backend_list() self._page.add_page_data({'backendlist': backend_list})
class HostController(RESTController): @Logger(logger=logger) def __init__(self, *args, **kwargs): super(HostController, self).__init__(*args, **kwargs) self._prepare_page() @Logger(logger=logger) def _prepare_page(self): self._page = Page(None, tmpl_env, self._request_context) self._page.set_cssfiles(CSS_FILES) self._page.set_jslibs(JS_LIBS) if ('authenticated' in self._request_context.session and self._request_context.session.authenticated): user_info = {} user_info['username'] = self._request_context.session.username user_info['realname'] = self._request_context.session.realname user_info[ 'is_dc2admin'] = self._request_context.session.is_dc2admin self._page.add_page_data({'user': user_info}) self._page.add_page_data({'admin_is_link': True}) self._fill_backends() self._page.set_page_value('controller_path', self._controller_path) @Logger(logger=logger) def _init_backend(self): params = web.input() self._backend_id = params.get('backend_id', None) self._page.add_page_data({'backend_id': self._backend_id}) self._backend = backends.backend_get({'_id': self._backend_id}) self._transport = get_xmlrpc_transport( self._backend['backend_url'], self._backend['is_kerberos']) self._servers = Servers(self._transport) self._macs = Macs(self._transport) self._ribs = Ribs(self._transport) self._hosts = Hosts(self._transport) self._backend_settings = BackendSettings(self._transport) self._environments = Environments(self._transport) self._defaultclasses = DefaultClasses(self._transport) self._classtemplates = ClassTemplates(self._transport) self._itypes_list = interfacetypes.itype_list() self._inet_list = inettypes.inet_list() @needs_auth @Logger(logger=logger) def _show(self, *args, **kwargs): verb = kwargs.get('verb', None) host_id = None self._init_backend() self._page.template_name = verb['template'] self._page.set_action('show') self._page.set_page_value('show_button', True) request_data = verb.get('request_data', None) if request_data is not None: host_id = request_data.get('id', None) if host_id is not None: host = self._hosts.get(id=host_id) if host is not None: server = self._servers.get(id=host['server_id']) server_macs = self._macs.get(server_id=host['server_id']) classtemplates = self._classtemplates.list() backendsettings = self._backend_settings.get() self._page.set_title( 'Host %s.%s' % (host['hostname'], host['domainname'])) self._page.add_page_data({ 'classtemplates': classtemplates, 'itypes': self._itypes_list, 'inetlist': self._inet_list, 'server': server, 'server_macs': server_macs, 'host': host, 'backend_settings': backendsettings }) result = self._prepare_output(verb['request_type'], verb[ 'request_content_type'], output={ 'content': self._page.render()}) return result @needs_auth @Logger(logger=logger) def _edit(self, *args, **kwargs): verb = kwargs.get('verb', None) host_id = None self._init_backend() self._page.template_name = verb['template'] self._page.set_action('edit') self._page.set_page_value('update_button', True) request_data = verb.get('request_data', None) if request_data is not None: host_id = request_data.get('id', None) if host_id is not None: host = self._hosts.get(id=host_id) host['hostclasses'] = sorted(host['hostclasses']) serverlist = self._servers.list() environmentlist = self._environments.list() defaultclasses = self._defaultclasses.list() classtemplates = self._classtemplates.list() server_macs = self._macs.get(server_id=host['server_id']) self._page.set_title('Edit Host %s.%s' % (host['hostname'], host['domainname'])) self._page.add_page_data({ 'classtemplates': classtemplates, 'itypes': self._itypes_list, 'inetlist': self._inet_list, 'entry_id': host['_id'], 'serverlist': serverlist, 'environlist': environmentlist, 'server_macs': server_macs, 'defaultclasses': defaultclasses, 'host': host, }) result = self._prepare_output(verb['request_type'], verb[ 'request_content_type'], output= {'content': self._page.render()}) return result @needs_auth @Logger(logger=logger) def _update(self, *args, **kwargs): params = web.input() backend_id = params.get('backend_id', None) verb = kwargs.get('verb', None) self._init_backend() params = web.data() data = json.loads(params)['result'] request_data = verb.get('request_data', None) host_id = None if request_data is not None: host_id = request_data['id'] host = {} host['_id'] = host_id host['server_id'] = data['host']['server'] host['hostname'] = data['host']['hostname'] host['domainname'] = data['host']['domainname'] host['environments'] = data['host']['environments'] host['hostclasses'] = [] for key in data['host']['hostclasses']: if key != 'new': host['hostclasses'].append(data['host']['hostclasses'][key]) host['interfaces'] = [] for key in data['host']['interfaces']: if key != 'new': host['interfaces'].append(data['host']['interfaces'][key]) self._hosts.update(host=host) output_format = verb.get('request_output_format', None) if output_format.lower() == 'json': result = self._prepare_output( 'json', verb['request_content_type'], verb['request_output_format'], {'redirect': {'url': '{0}/{1}?backend_id={2}'.format( self._controller_path, host_id, backend_id), 'absolute': 'true'}}) else: result = self._prepare_output( verb['request_type'], verb['request_content_type'], output={ 'redirect': {'url': '{0}/{1}?backend_id={2}'.format( self._controller_path, host_id, backend_id), 'absolute': 'true'}}) return result @Logger(logger=logger) def _fill_backends(self): backend_list = backends.backend_list() self._page.add_page_data({'backendlist': backend_list})
class BackendsCtrl(RESTController): @Logger(logger=logger) def __init__(self, *args, **kwargs): super(BackendsCtrl, self).__init__(*args, **kwargs) self._prepare_page() @Logger(logger=logger) def _prepare_page(self): self._page = Page(None, tmpl_env, self._request_context) self._page.set_cssfiles(CSS_FILES) self._page.set_jslibs(JS_LIBS) if ('authenticated' in self._request_context.session and self._request_context.session.authenticated): user_info = {} user_info['username'] = self._request_context.session.username user_info['realname'] = self._request_context.session.realname user_info[ 'is_dc2admin'] = self._request_context.session.is_dc2admin self._page.add_page_data({'user': user_info}) self._page.add_page_data({'admin_is_link': True}) self._fill_backends() @needs_auth @Logger(logger=logger) def _index(self, *args, **kwargs): verb = kwargs.get('verb', None) self._page.template_name = verb['template'] self._page.set_action('index') params = web.input() backend_id = params.get('backend_id', None) if backend_id is not None: backend = backends.backend_get({'_id': backend_id}) self._page.add_page_data({'backend_id': backend_id}) self._page.set_title('Backend %s (Loc: %s)' % (backend['title'], backend['location'])) result = self._prepare_output( verb['request_type'], verb['request_content_type'], output={'content': self._page.render()}) return result @needs_auth @Logger(logger=logger) def _show(self, *args, **kwargs): verb = kwargs.get('verb', None) self._page.template_name = verb['template'] self._page.set_action('show') request_data = verb.get('request_data', None) backend_id = request_data.get('id', None) if backend_id is not None: backend = backends.backend_get({'_id': backend_id}) self._page.add_page_data({'backend_id': backend_id}) self._page.set_title('Backend %s (Loc: %s)' % (backend['title'], backend['location'])) result = self._prepare_output( verb['request_type'], verb['request_content_type'], output={'content': self._page.render()}) return result @Logger(logger=logger) def _fill_backends(self): backend_list = backends.backend_list() self._page.add_page_data({'backendlist': backend_list})