def _backend_deployment_list(self, *args, **kwargs): verb = kwargs.get('verb', None) if verb is not None: try: params = web.input() backend_id = params.get('backend_id', None) if backend_id is not None: backend = backends.backend_get({'_id': backend_id}) transport = get_xmlrpc_transport(backend['backend_url'], backend['is_kerberos']) s = InstallState(transport) deploymentlist = s.list() result = self._prepare_output( result={'backend_id': backend_id, 'datalist': deploymentlist}) return result except KerberosError as e: (first, last) = e.message (message, error_no) = last result = self._prepare_output(result={'backend_id': backend_id, 'error': True, 'error_type': 'Kerberos', 'error_msg': message, 'error_no': error_no}) return result result = self._prepare_output(result={'backend_id': backend_id, 'datalist': []}) return result
def _backend_deployment_stats(self, *args, **kwargs): verb = kwargs.get('verb', None) if verb is not None: try: params = web.input() backend_id = params.get('backend_id', None) what = params.get('status', None) if backend_id is not None: backend = backends.backend_get({'_id': backend_id}) if backend is not None: transport = get_xmlrpc_transport( backend['backend_url'], backend['is_kerberos']) s = InstallState(transport) if what is not None and what == 'all': count_deploy = s.count('deploy') count_localboot = s.count('localboot') result = self._prepare_output(result={ 'backend_id': backend_id, 'status': 'all', 'count_localboot': count_localboot, 'count_deploy': count_deploy}) elif (what is not None and what in ('deploy', 'localboot')): count = s.count(what) result = self._prepare_output(result={ 'backend_id': backend_id, 'status': what, 'count': count}) return result except KerberosError as e: (first, last) = e.message (message, error_no) = last result = self._prepare_output(result={ 'backend_id': backend_id, 'error': True, 'error_type': 'Kerberos', 'error_msg': message, 'error_no': error_no}) return result if what is None: result = self._prepare_output(result={ 'backend_id': backend_id, 'status': 'all', 'count_localboot': 0, 'count_deploy': 0}) elif what is not None and what == 'all': result = self._prepare_output(result={ 'backend_id': backend_id, 'status': 'all', 'count_localboot': 0, 'count_deploy': 0}) elif what is not None and what in ('deploy', 'localboot'): result = self._prepare_output(result={ 'backend_id': backend_id, 'status': what, 'count': 0}) return result
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)
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})