Ejemplo n.º 1
0
 def _backend_host_get_server_info(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})
                 server_id = params.get('server_id', None)
                 if server_id is not None:
                     transport = get_xmlrpc_transport(
                         backend['backend_url'],
                         backend['is_kerberos'])
                     s = Servers(transport)
                     server = s.get(id=server_id)
                     result = self._prepare_output(result={
                         'backend_id': backend_id,
                         'server': server})
                     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,
                                               'server': None})
         return result
Ejemplo n.º 2
0
Archivo: servers.py Proyecto: sadig/DC2
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})
Ejemplo n.º 3
0
Archivo: hosts.py Proyecto: 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})