Exemplo n.º 1
0
 def _backend_classtemplate_get(self, *args, **kwargs):
     verb = kwargs.get('verb', None)
     if verb is not None:
         try:
             params = web.input()
             backend_id = params.get('backend_id', None)
             template_id = params.get('template_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 = ClassTemplates(transport)
                 template_list = s.get(id=template_id)
                 if template_list is not None:
                     result = self._prepare_output(result={
                         'backend_id': backend_id,
                         'datalist': template_list
                     })
                     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
Exemplo n.º 2
0
Arquivo: hosts.py Projeto: sadig/DC2
 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()
Exemplo n.º 3
0
 def _init_backend(self, backend):
     self._transport = get_xmlrpc_transport(backend['backend_url'],
                                            backend['is_kerberos'])
     self._defaultclasses = DefaultClasses(self._transport)
     self._classtemplates = ClassTemplates(self._transport)
Exemplo n.º 4
0
class BackendClassTemplatesController(AdminController):
    CONTROLLER_IDENT = {'title': 'DC2 Backends Classtemplates',
                        'url': '/admin/backends/classtemplates',
                        'show_in_menu': 'False'}

    def _init_backend(self, backend):
        self._transport = get_xmlrpc_transport(backend['backend_url'],
                                               backend['is_kerberos'])
        self._defaultclasses = DefaultClasses(self._transport)
        self._classtemplates = ClassTemplates(self._transport)

    @needs_auth
    @needs_admin
    @Logger(logger=logger)
    def _index(self, *args, **kwargs):
        params = web.input()
        verb = kwargs.get('verb', None)
        page = self._prepare_page(verb)
        backend_list = backends.backend_list()
        backend_id = params.get('backend_id', None)
        backend = backends.backend_get({'_id': backend_id})
        self._init_backend(backend)
        classtemplates = self._classtemplates.list()
        page.set_title('DC2 Admincenter - Backends - Class Templates - Index')
        page.add_page_data({
            'backendlist': backend_list,
            'backend_id': backend_id,
            'backend_classtemplates': classtemplates
        })
        page.set_action('index')
        result = self._prepare_output(verb['request_type'],
                                      verb['request_content_type'],
                                      output={'content': page.render()})
        return result

    @needs_auth
    @needs_admin
    @Logger(logger=logger)
    def _new(self, *args, **kwargs):
        params = web.input()
        verb = kwargs.get('verb', None)
        page = self._prepare_page(verb)
        backend_list = backends.backend_list()
        backend_id = params.get('backend_id', None)
        backend = backends.backend_get({'_id': backend_id})
        self._init_backend(backend)
        defaultclasses = self._defaultclasses.list()
        classtemplate = self._classtemplates.new()
        page.set_title('DC2 Admindcenter - Backends - Class Templates - Add')
        page.add_page_data({
            'backendlist': backend_list,
            'backend_id': backend_id,
            'defaultclasses': defaultclasses,
            'tmpl': classtemplate,
        })
        page.set_action('new')
        result = self._prepare_output(verb['request_type'],
                                      verb['request_content_type'],
                                      output={'content': page.render()})
        return result

    @needs_auth
    @needs_admin
    @Logger(logger=logger)
    def _edit(self, *args, **kwargs):
        params = web.input()
        verb = kwargs.get('verb', None)
        page = self._prepare_page(verb)
        backend_list = backends.backend_list()
        backend_id = params.get('backend_id', None)
        backend = backends.backend_get({'_id': backend_id})
        self._init_backend(backend)
        defaultclasses = self._defaultclasses.list()
        classtemplate = self._classtemplates.get(id=verb['request_data']['id'])
        if classtemplate['classes'] is not None:
            for classe in classtemplate['classes']:
                for k in xrange(len(defaultclasses)):
                    if defaultclasses[k]['classname'] == classe:
                        a = defaultclasses[k]
                        defaultclasses.remove(a)
                        break
        else:
            classtemplate['classes'] = []
        page.set_title('DC2 Admindcenter - Backends - Class Templates - Edit')
        page.add_page_data({
            'backendlist': backend_list,
            'backend_id': backend_id,
            'defaultclasses': defaultclasses,
            'tmpl': classtemplate,
        })
        page.set_action('edit')
        result = self._prepare_output(verb['request_type'],
                                      verb['request_content_type'],
                                      output={'content': page.render()})
        return result

    @needs_auth
    @needs_admin
    @Logger(logger=logger)
    def _create(self, *args, **kwargs):
        params = web.input()
        verb = kwargs.get('verb', None)
        backend_id = params.get('backend_id', None)
        backend = backends.backend_get({'_id': backend_id})
        self._init_backend(backend)
        result = json.loads(web.data())
        classtemplate = {}
        classtemplate['name'] = result['result']['classtemplate']['name']
        classtemplate['description'] = result[
            'result']['classtemplate']['description']
        classtemplate['classes'] = result['result']['classtemplate']['classes']
        self._classtemplates.add(classtemplate=classtemplate)
        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}?backend_id={1}'.format(
                                                self._controller_path,
                                                backend_id),
                                            'absolute': 'true'}})
        else:
            result = self._prepare_output(
                verb['request_type'],
                verb['request_content_type'],
                output={'redirect': {
                    'url': '{0}?backend_id={1}'.format(
                        self._controller_path,
                        backend_id),
                    'absolute': 'true'}})
        return result

    @needs_auth
    @needs_admin
    @Logger(logger=logger)
    def _update(self, *args, **kwargs):
        params = web.input()
        verb = kwargs.get('verb', None)
        backend_id = params.get('backend_id', None)
        backend = backends.backend_get({'_id': backend_id})
        self._init_backend(backend)
        tmpl_id = verb['request_data']['id']
        result = json.loads(web.data())
        classtemplate = {}
        classtemplate['_id'] = tmpl_id
        classtemplate['name'] = result['result']['classtemplate']['name']
        classtemplate['description'] = result[
            'result']['classtemplate']['description']
        classtemplate['classes'] = result['result']['classtemplate']['classes']
        self._classtemplates.update(classtemplate=classtemplate)
        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}?backend_id={1}'.format(
                                            self._controller_path,
                                            backend_id),
                                            'absolute': 'true'}})
        else:
            result = self._prepare_output(
                verb['request_type'],
                verb['request_content_type'],
                output={'redirect': {
                    'url': '{0}?backend_id={1}'.format(
                        self._controller_path,
                        backend_id),
                    'absolute': 'true'}})
        return result

    @needs_auth
    @needs_admin
    @Logger(logger=logger)
    def _delete(self, *args, **kwargs):
        params = web.input()
        verb = kwargs.get('verb', None)
        backend_id = params.get('backend_id', None)
        backend = backends.backend_get({'_id': backend_id})
        self._init_backend(backend)
        tmpl_id = verb['request_data']['id']
        result = self._classtemplates.delete(id=tmpl_id)
        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}?backend_id={1}'.format(
                                            self._controller_path,
                                            backend_id),
                                            'absolute': 'true'}})
        else:
            result = self._prepare_output(
                verb['request_type'],
                verb['request_content_type'],
                output={'redirect': {
                    'url': '{0}?backend_id={1}'.format(
                        self._controller_path,
                        backend_id),
                    'absolute': 'true'}})
        return result
Exemplo n.º 5
0
Arquivo: hosts.py Projeto: sadig/DC2
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})