def delete_license(self, license_id, return_to=None, **kwargs):
        '''
        Deletes a specific license
        '''

        template_args = {
            'return_to': util.make_url_internal(return_to),
            'was_deleted': False
        }

        try:
            # first check that the license is valid
            marked_license = License.get(License.build_id(
                license_id,
                namespace=None,
                owner=None
            ))
            template_args['license'] = marked_license

            # handle delete
            if cherrypy.request.method == 'POST':
                marked_license.delete()
                template_args['was_deleted'] = True
            
        except Exception, e:
            template_args['controller_exception'] = e
Beispiel #2
0
    def view_license(self, license_name, **kw):
        '''
        Handles the license view page
        '''

        template_args = {'license_name': license_name, 'license': None}

        try:
            # first check that the license is valid
            license_object = License.get(
                License.build_id(license_name, namespace=None, owner=None))
            template_args['license'] = license_object

        except Exception, e:
            template_args['controller_exception'] = e
    def add_license(self, licenseFile=None, formset_pasted_license=None, return_to=None, prompt_restart=False, **kw):
        '''
        Handles the license add/edit page
        '''

        template_args = {
            'return_to': util.make_url_internal(return_to),
            'prompt_restart': splunk.util.normalizeBoolean(prompt_restart),
            'is_success': False,
            'pasted_license': formset_pasted_license or '',
            'current_group': None,
            'new_group': None
        }

        license_table = []

        try:
            licenses = License.all()
            for license in licenses:
                license_table.append({
                    'type': license.type,
                    'label': license.label
                })
        except Exception, e:
            logger.exception(e)
            template_args['controller_exception'] = e
Beispiel #4
0
    def add_license(self,
                    licenseFile=None,
                    formset_pasted_license=None,
                    return_to=None,
                    prompt_restart=False,
                    **kw):
        '''
        Handles the license add/edit page
        '''

        template_args = {
            'return_to': util.make_url_internal(return_to),
            'prompt_restart': splunk.util.normalizeBoolean(prompt_restart),
            'is_success': False,
            'pasted_license': formset_pasted_license or '',
            'current_group': None,
            'new_group': None
        }

        license_table = []

        try:
            licenses = License.all()
            for license in licenses:
                license_table.append({
                    'type': license.type,
                    'label': license.label
                })
        except Exception, e:
            logger.exception(e)
            template_args['controller_exception'] = e
Beispiel #5
0
    def switch(self, license_group=None, return_to=None, **unused):

        template_args = {
            'is_success': False,
            'return_to': util.make_url_internal(return_to),
            'available_groups': [],
            'license_group': None,
            'license_map': {}
        }

        active_group = None

        # get all data
        licenses = License.all()
        groups = Group.all()

        # get the active group and all licenses associated with it
        for group in groups:

            template_args['available_groups'].append(group.name)
            if group.is_active:
                active_group = group.name

            template_args['license_map'][group.name] = []
            for license in licenses:
                if license.stack_name in group.stack_names and license.status == 'VALID':
                    template_args['license_map'][group.name].append(
                        license.label)

        if cherrypy.request.method == 'POST':

            # redirect user to add license if newly activated group
            # does not have any licenses
            if len(template_args['license_map'].get(license_group, '')) == 0:
                logger.info(
                    '0 licenses found; redirecting user to "add license" page')
                self.redirect_to_url(
                    ['manager', 'system', 'licensing', 'licenses', 'new'],
                    _qs={
                        'return_to':
                        util.make_url_internal(return_to)
                        or cherrypy.request.relative_uri,
                        'prompt_restart':
                        1
                    })

            try:
                for group in groups:
                    if license_group == group.name:
                        group.is_active = True
                        group.save()
                        template_args['is_success'] = True
                        active_group = group.name
                        break
                else:
                    raise Exception, 'cannot activate unknown license group: %s' % license_group

            except Exception, e:
                template_args['controller_exception'] = e
    def switch(self, license_group=None, return_to=None, **unused):

        template_args = {
            'is_success': False,
            'return_to': util.make_url_internal(return_to),
            'available_groups': [],
            'license_group': None,
            'license_map': {}
        }

        active_group = None

        # get all data
        licenses = License.all()
        groups = Group.all()

        # get the active group and all licenses associated with it
        for group in groups:
        
            template_args['available_groups'].append(group.name)
            if group.is_active:
                active_group = group.name

            template_args['license_map'][group.name] = []
            for license in licenses:
                if license.stack_name in group.stack_names and license.status == 'VALID':
                    template_args['license_map'][group.name].append(license.label)


        if cherrypy.request.method == 'POST':

            # redirect user to add license if newly activated group
            # does not have any licenses
            if len(template_args['license_map'].get(license_group, '')) == 0:
                logger.info('0 licenses found; redirecting user to "add license" page')
                self.redirect_to_url(
                    ['manager','system','licensing','licenses','new'], 
                    _qs={
                        'return_to': util.make_url_internal(return_to) or cherrypy.request.relative_uri,
                        'prompt_restart': 1
                    }
                )
            
            try:
                for group in groups:
                    if license_group == group.name:
                        group.is_active = True
                        group.save()
                        template_args['is_success'] = True
                        active_group = group.name
                        break
                else:
                    raise Exception, 'cannot activate unknown license group: %s' % license_group

            except Exception, e:
                template_args['controller_exception'] = e
    def view_license(self, license_name, **kw):
        '''
        Handles the license view page
        '''

        template_args = {
            'license_name': license_name,
            'license': None
        }

        try:
            # first check that the license is valid
            license_object = License.get(License.build_id(
                license_name,
                namespace=None,
                owner=None
            ))
            template_args['license'] = license_object

        except Exception, e:
            template_args['controller_exception'] = e
Beispiel #8
0
    def delete_license(self, license_id, return_to=None, **kwargs):
        '''
        Deletes a specific license
        '''

        template_args = {
            'return_to': util.make_url_internal(return_to),
            'was_deleted': False
        }

        try:
            # first check that the license is valid
            marked_license = License.get(
                License.build_id(license_id, namespace=None, owner=None))
            template_args['license'] = marked_license

            # handle delete
            if cherrypy.request.method == 'POST':
                marked_license.delete()
                template_args['was_deleted'] = True

        except Exception, e:
            template_args['controller_exception'] = e
    def list_license(self, return_to=None, **kw):
        '''
        Handles the license listing page
        '''

        licenses = License.all()
        try:
            self_config = SelfConfig.get()
        except splunk.AuthorizationFailed:
            return self.redirect_to_url(['manager'])

        template_args = {
            'licenses': licenses or [],
            'return_to': util.make_url_internal(return_to) or self.make_url(['manager','system','licensing']),
            'server_name': self_config.slave_label
        }
        return self.render_template('/licensing/licenses/list.html', template_args)
Beispiel #10
0
    def list_license(self, return_to=None, **kw):
        '''
        Handles the license listing page
        '''

        licenses = License.all()
        try:
            self_config = SelfConfig.get()
        except splunk.AuthorizationFailed:
            return self.redirect_to_url(['manager'])

        template_args = {
            'licenses':
            licenses or [],
            'return_to':
            util.make_url_internal(return_to)
            or self.make_url(['manager', 'system', 'licensing']),
            'server_name':
            self_config.slave_label
        }
        return self.render_template('/licensing/licenses/list.html',
                                    template_args)
    def show_summary(self, **unused):
        '''
        Renders the new license summary page
        '''

        #
        # retrieve data
        #

        # get local slave info; unauthorized access is simply
        # booted to manager TOC
        try:
            self_config = SelfConfig.get()
        except splunk.AuthorizationFailed:
            return self.redirect_to_url(['manager'])


        local_master_uri = None 
        if self_config.master_uri.lower() not in ['', 'self']:
            return self.show_slave_summary()

        # get all slaves, and find the local one in the mix
        slave_label_map = {}
        slaves = Slave.all(count_per_req=10000)
        local_slave = None
        for slave in slaves:
            slave_label_map[slave.name] = slave.label
            if slave.name == self_config.slave_name:
                local_slave = slave
        if not local_slave:
            raise Exception, 'Could not retrieve slave information for slave name: %s' % self_config.slave_name
        slave_count = len(slaves)
    
        # get active group
        active_group = Group.all().filter(is_active=True)
        if len(active_group) == 0:
            logger.warn('no active license groups found; redirecting user to "add license" page')
            self.redirect_to_url(['manager','system','licensing','licenses','new'], _qs={'return_to': cherrypy.request.relative_uri})

        active_group = active_group[0]

        # get associated stacks
        stack_query = Stack.all()
        stacks = []
        for stack in stack_query:
            if stack.name in active_group.stack_names:
                stacks.append(stack)

        # get associated pools
        pool_query = Pool.all()
        pools = []
        catchall_pool_names = []
        for pool in pool_query:
            if pool.stack_name in [s.name for s in stacks]:
                pools.append(pool)
                if pool.slaves == CATCHALL_SLAVE_LIST:
                    catchall_pool_names.append(pool.name)

        licenses = License.all()
        messages = Message.all()


        #
        # generate output info
        #

        stack_table = []
        for stack in stacks:
            #if not stack.quota_bytes:
            #    remaining_perc = None
            #else:
            #    remaining_perc = stack.remaining_bytes / stack.quota_bytes
            stack_table.append({
                'name': stack.name,
                'quota_bytes': stack.quota_bytes,
                'label': stack.label
                #'remaining_bytes': stack.remaining_bytes,
                #'remaining_perc': remaining_perc
            })

        # compile a summary list of messages by category
        hard_messages = {}
        soft_messages = {}
        for message in messages:
            if message.category == 'license_window':
                message_type = hard_messages
            else: 
                message_type = soft_messages   

            message_type.setdefault(message.category, {
                'severity': message.severity.lower(),
                'count': 0, 
                'latest_time': datetime.datetime.fromtimestamp(0, splunk.util.localTZ), 
                'slaves': set()}
            )
            message_type[message.category]['count'] += 1
            message_type[message.category]['latest_time'] = max(
                message.create_time,
                message_type[message.category]['latest_time']
            )
            message_type[message.category]['slaves'].add(slave_label_map.get(message.slave_name, message.slave_name))




        # loop over the per-slave data embedded in each pool descriptor
        pool_table = []
        slave_table = []
        local_used_bytes = 0.0
        for pool in pools:

            effective_global_quota = pool.quota_bytes['byte_value']
            if pool.quota_bytes['value_mode'] == 'MAX':
                for stack in stacks:
                    if pool.stack_name == stack.name:
                        effective_global_quota = stack.quota_bytes
                        break
                
            pool_table.append({
                'name': pool.name,
                'stack_name': pool.stack_name,
                'used_bytes': pool.used_bytes,
                'quota_bytes': effective_global_quota,
                'quota_mode': pool.quota_bytes['value_mode']
            })

            for slave in sorted(pool.slaves_usage_bytes):
                tmp_slave_bytes = float(pool.slaves_usage_bytes[slave])
                
                # accum the usage for the local slave
                if slave == self_config.slave_name:
                    local_used_bytes += tmp_slave_bytes
                
                if not effective_global_quota:
                    used_perc = None
                else:
                    used_perc = tmp_slave_bytes / effective_global_quota

                slave_table.append({
                    'pool_name': pool.name,
                    'name': slave_label_map.get(slave, slave),
                    'used_bytes': tmp_slave_bytes,
                    'used_perc': used_perc
                })

        license_table = []
        for license in licenses:
            license_table.append({
                'name': license.name,
                'label': license.label,
                'type': license.type,
                'stack_name': license.stack_name,
                'quota_bytes': license.quota_bytes,
                'expiration_time': license.expiration_time,
                'status': license.status.upper(),
                'can_remove': license.metadata.can_remove
            })
        license_table.sort(key=operator.itemgetter('expiration_time'))


        # the UI will only support managing pools within the enterprise stack
        if active_group.name in POOLABLE_GROUPS:
            can_edit_pools = True
        else:
            can_edit_pools = False
        

        # assemble into mako dict
        template_args = {
            'local_slave_name': local_slave.label,
            'local_used_bytes': local_used_bytes,
            'local_warning_count': local_slave.warning_count,
            'local_master_uri': local_master_uri,
            'active_group_name': active_group.name,
            'default_stack_name': DEFAULT_STACK_NAME,
            'slave_count': slave_count,
            'pool_table': pool_table,
            'stack_table': stack_table,
            'slave_table': slave_table,
            'license_table': license_table,
            'hard_messages': hard_messages,
            'soft_messages': soft_messages,
            'can_edit_pools': can_edit_pools,
            'catchall_pool_names': catchall_pool_names,
            'can_be_remote_master': self_config.features.get('CanBeRemoteMaster') == 'ENABLED',
            'showLicenseUsage': (cherrypy.config['product_type'] != 'hunk')
        }

        return self.render_template('/licensing/overview.html', template_args)
        if cherrypy.request.method == 'POST':
        
            license_object = None

            # process uploaded file first; if not there, then check for inline
            # XML being pasted in
            if isinstance(licenseFile, cgi.FieldStorage) and licenseFile.filename:

                logger.info('processing incoming license to add: %s' % licenseFile.filename)

                # pull license into stringbuffer
                license_contents = []
                for i in range(32):
                    license_contents.append(licenseFile.file.read(8192))

                license_object = License(name=licenseFile.filename, namespace=None, owner=None)
                license_object.payload = ''.join(license_contents)

                # check for clear text; fake a new exception to notify user
                try:
                    unicode(license_object.payload, 'utf-8')
                except Exception, e:
                    template_args['controller_exception'] = ValueError('Invalid license file submitted')
                    license_object = None
                    
            
            elif formset_pasted_license:
                license_object = License(name=('web_%s.lic' % time.time()), namespace=None, owner=None)
                license_object.payload = formset_pasted_license

Beispiel #13
0
    def show_summary(self, **unused):
        '''
        Renders the new license summary page
        '''

        #
        # retrieve data
        #

        # get local slave info; unauthorized access is simply
        # booted to manager TOC
        try:
            self_config = SelfConfig.get()
        except splunk.AuthorizationFailed:
            return self.redirect_to_url(['manager'])

        local_master_uri = None
        if self_config.master_uri.lower() not in ['', 'self']:
            return self.show_slave_summary()

        # get all slaves, and find the local one in the mix
        slave_label_map = {}
        slaves = Slave.all()
        local_slave = None
        for slave in slaves:
            slave_label_map[slave.name] = slave.label
            if slave.name == self_config.slave_name:
                local_slave = slave
        if not local_slave:
            raise Exception, 'Could not retrieve slave information for slave name: %s' % self_config.slave_name
        slave_count = len(slaves)

        # get active group
        active_group = Group.all().filter(is_active=True)
        if len(active_group) == 0:
            logger.warn(
                'no active license groups found; redirecting user to "add license" page'
            )
            self.redirect_to_url(
                ['manager', 'system', 'licensing', 'licenses', 'new'],
                _qs={'return_to': cherrypy.request.relative_uri})

        active_group = active_group[0]

        # get associated stacks
        stack_query = Stack.all()
        stacks = []
        for stack in stack_query:
            if stack.name in active_group.stack_names:
                stacks.append(stack)

        # get associated pools
        pool_query = Pool.all()
        pools = []
        catchall_pool_names = []
        for pool in pool_query:
            if pool.stack_name in [s.name for s in stacks]:
                pools.append(pool)
                if pool.slaves == CATCHALL_SLAVE_LIST:
                    catchall_pool_names.append(pool.name)

        licenses = License.all()
        messages = Message.all()

        #
        # generate output info
        #

        stack_table = []
        for stack in stacks:
            #if not stack.quota_bytes:
            #    remaining_perc = None
            #else:
            #    remaining_perc = stack.remaining_bytes / stack.quota_bytes
            stack_table.append({
                'name': stack.name,
                'quota_bytes': stack.quota_bytes,
                'label': stack.label
                #'remaining_bytes': stack.remaining_bytes,
                #'remaining_perc': remaining_perc
            })

        # compile a summary list of messages by category
        hard_messages = {}
        soft_messages = {}
        for message in messages:
            if message.category == 'license_window':
                message_type = hard_messages
            else:
                message_type = soft_messages

            message_type.setdefault(
                message.category, {
                    'severity':
                    message.severity.lower(),
                    'count':
                    0,
                    'latest_time':
                    datetime.datetime.fromtimestamp(0, splunk.util.localTZ),
                    'slaves':
                    set()
                })
            message_type[message.category]['count'] += 1
            message_type[message.category]['latest_time'] = max(
                message.create_time,
                message_type[message.category]['latest_time'])
            message_type[message.category]['slaves'].add(
                slave_label_map.get(message.slave_name, message.slave_name))

        # loop over the per-slave data embedded in each pool descriptor
        pool_table = []
        slave_table = []
        local_used_bytes = 0.0
        for pool in pools:

            effective_global_quota = pool.quota_bytes['byte_value']
            if pool.quota_bytes['value_mode'] == 'MAX':
                for stack in stacks:
                    if pool.stack_name == stack.name:
                        effective_global_quota = stack.quota_bytes
                        break

            pool_table.append({
                'name': pool.name,
                'stack_name': pool.stack_name,
                'used_bytes': pool.used_bytes,
                'quota_bytes': effective_global_quota,
                'quota_mode': pool.quota_bytes['value_mode']
            })

            for slave in sorted(pool.slaves_usage_bytes):
                tmp_slave_bytes = float(pool.slaves_usage_bytes[slave])

                # accum the usage for the local slave
                if slave == self_config.slave_name:
                    local_used_bytes += tmp_slave_bytes

                if not effective_global_quota:
                    used_perc = None
                else:
                    used_perc = tmp_slave_bytes / effective_global_quota

                slave_table.append({
                    'pool_name': pool.name,
                    'name': slave_label_map.get(slave, slave),
                    'used_bytes': tmp_slave_bytes,
                    'used_perc': used_perc
                })

        license_table = []
        for license in licenses:
            license_table.append({
                'name': license.name,
                'label': license.label,
                'type': license.type,
                'stack_name': license.stack_name,
                'quota_bytes': license.quota_bytes,
                'expiration_time': license.expiration_time,
                'status': license.status.upper(),
                'can_remove': license.metadata.can_remove
            })
        license_table.sort(key=operator.itemgetter('expiration_time'))

        # the UI will only support managing pools within the enterprise stack
        if active_group.name in POOLABLE_GROUPS:
            can_edit_pools = True
        else:
            can_edit_pools = False

        # assemble into mako dict
        template_args = {
            'local_slave_name':
            local_slave.label,
            'local_used_bytes':
            local_used_bytes,
            'local_warning_count':
            local_slave.warning_count,
            'local_master_uri':
            local_master_uri,
            'active_group_name':
            active_group.name,
            'default_stack_name':
            DEFAULT_STACK_NAME,
            'slave_count':
            slave_count,
            'pool_table':
            pool_table,
            'stack_table':
            stack_table,
            'slave_table':
            slave_table,
            'license_table':
            license_table,
            'hard_messages':
            hard_messages,
            'soft_messages':
            soft_messages,
            'can_edit_pools':
            can_edit_pools,
            'catchall_pool_names':
            catchall_pool_names,
            'can_be_remote_master':
            self_config.features.get('CanBeRemoteMaster') == 'ENABLED'
        }

        return self.render_template('/licensing/overview.html', template_args)
Beispiel #14
0
            # process uploaded file first; if not there, then check for inline
            # XML being pasted in
            if isinstance(licenseFile,
                          cgi.FieldStorage) and licenseFile.filename:

                logger.info('processing incoming license to add: %s' %
                            licenseFile.filename)

                # pull license into stringbuffer
                license_contents = []
                for i in range(32):
                    license_contents.append(licenseFile.file.read(8192))

                license_object = License(name=licenseFile.filename,
                                         namespace=None,
                                         owner=None)
                license_object.payload = ''.join(license_contents)

                # check for clear text; fake a new exception to notify user
                try:
                    unicode(license_object.payload, 'utf-8')
                except Exception, e:
                    template_args['controller_exception'] = ValueError(
                        'Invalid license file submitted')
                    license_object = None

            elif formset_pasted_license:
                license_object = License(name=('web_%s.lic' % time.time()),
                                         namespace=None,
                                         owner=None)