Exemple #1
0
    def get(self):
        result = super(ComputerResource, self).get()
        node_collection = self.request.db.nodes
        
        if not result.get('node_chef_id', None):
            return result
        try:
            api = get_chef_api(self.request.registry.settings, self.request.user)
            computer_node = ChefNode(result['node_chef_id'], api)
            ohai = to_deep_dict(computer_node.attributes)
            nodeid = result.get('_id',None)
            usernames = [i['username'] for i in ohai.get('ohai_gecos', {}).get('users', [])]
            users = list(node_collection.find({
                "$and": [
                    { "$or": [{"name": {"$in": usernames}}] }, 
                    { "type":"user"},
                    { "computers": {"$elemMatch": {"$eq": ObjectId(nodeid)}}}
                 ]
            },{'_id':1,'name':1,'path':1}))
            # ObjectId to string for JSON serialize
            [d.update({'_id': str(d['_id'])}) for d in users]
            
            # Create a list of users that provides at least one user policy to this computer
            users_inheritance_pre = list(node_collection.find({
                "$and": [
                    { "$or": [{"name": {"$in": usernames}}] }, 
                    { "type":"user"},
                    { "computers": {"$elemMatch": {"$eq": ObjectId(nodeid)}}}
                 ]
            },{'_id':1,'name':1,'path':1, 'inheritance': 1}))
            [d.update({'_id': str(d['_id'])}) for d in users_inheritance_pre]

            users_inheritance = []
            for usr_inh in users_inheritance_pre:
                if 'inheritance' in usr_inh:
                    policies_list = get_inheritance_tree_policies_list(usr_inh['inheritance'], [])
                    if len(policies_list) > 0:
                        users_inheritance.append(usr_inh)
            
            
            cpu = ohai.get('cpu', {}).get('0', {})
            dmi = ohai.get('dmi', {})
            
            result.update({'ohai': ohai,
                           'users': users, # Users related with this computer
                           'users_inheritance': users_inheritance, # Users related with this computer that provides at least one user policy
                           'uptime': ohai.get('uptime', ''),
                           #'gcc_link': ohai.get('gcc_link',True),
                           'ipaddress': ohai.get('ipaddress', ''),
                           'cpu': '%s %s' % (cpu.get('vendor_id', ''), cpu.get('model_name', '')),
                           'product_name': dmi.get('system', {}).get('product_name', ''),
                           'manufacturer': dmi.get('system', {}).get('manufacturer', ''),
                           'ram': ohai.get('memory', {}).get('total', ''),
                           'lsb': ohai.get('lsb', {}),
                           'kernel': ohai.get('kernel', {}),
                           'filesystem': ohai.get('filesystem', {}),
                           })
        except (urllib2.URLError, ChefError, ChefServerError):
            pass
        return result
Exemple #2
0
 def get(self):
     result = super(ComputerResource, self).get()
     if not result.get('node_chef_id', None):
         return result
     try:
         api = get_chef_api(self.request.registry.settings, self.request.user)
         computer_node = ChefNode(result['node_chef_id'], api)
         ohai = to_deep_dict(computer_node.attributes)
         cpu = ohai.get('cpu', {}).get('0', {})
         dmi = ohai.get('dmi', {})
         result.update({'ohai': ohai,
                        'users': ','.join([i['username'] for i in ohai.get('ohai_gecos', {}).get('users', [])]),
                        'uptime': ohai.get('uptime', ''),
                        'ipaddress': ohai.get('ipaddress', ''),
                        'cpu': '%s %s' % (cpu.get('vendor_id', ''), cpu.get('model_name', '')),
                        'product_name': dmi.get('system', {}).get('product_name', ''),
                        'manufacturer': dmi.get('system', {}).get('manufacturer', ''),
                        'ram': ohai.get('memory', {}).get('total', ''),
                        'lsb': ohai.get('lsb', {}),
                        'kernel': ohai.get('kernel', {}),
                        'filesystem': ohai.get('filesystem', {}),
                        })
     except (urllib2.URLError, ChefError, ChefServerError):
         pass
     return result
Exemple #3
0
 def get(self):
     result = super(ComputerResource, self).get()
     if not result.get('node_chef_id', None):
         return result
     try:
         api = get_chef_api(self.request.registry.settings,
                            self.request.user)
         computer_node = ChefNode(result['node_chef_id'], api)
         ohai = to_deep_dict(computer_node.attributes)
         cpu = ohai.get('cpu', {}).get('0', {})
         dmi = ohai.get('dmi', {})
         result.update({
             'ohai':
             ohai,
             'users':
             ','.join([
                 i['username']
                 for i in ohai.get('ohai_gecos', {}).get('users', [])
             ]),
             'uptime':
             ohai.get('uptime', ''),
             'ipaddress':
             ohai.get('ipaddress', ''),
             'cpu':
             '%s %s' %
             (cpu.get('vendor_id', ''), cpu.get('model_name', '')),
             'product_name':
             dmi.get('system', {}).get('product_name', ''),
             'manufacturer':
             dmi.get('system', {}).get('manufacturer', ''),
             'ram':
             ohai.get('memory', {}).get('total', ''),
             'lsb':
             ohai.get('lsb', {}),
             'kernel':
             ohai.get('kernel', {}),
             'filesystem':
             ohai.get('filesystem', {}),
         })
     except (urllib2.URLError, ChefError, ChefServerError):
         pass
     return result
Exemple #4
0
    def get(self):
        result = super(ComputerResource, self).get()
        node_collection = self.request.db.nodes

        if not result.get('node_chef_id', None):
            return result

        logger.info("/api/computers/: node_chef_id: %s" %
                    (str(result.get('node_chef_id', None))))

        try:
            api = get_chef_api(self.request.registry.settings,
                               self.request.user)
            computer_node = ChefNode(result['node_chef_id'], api)
            ohai = to_deep_dict(computer_node.attributes)
            nodeid = result.get('_id', None)
            usernames = [
                i['username']
                for i in ohai.get('ohai_gecos', {}).get('users', [])
            ]
            users = list(
                node_collection.find(
                    {
                        "$and": [{
                            "$or": [{
                                "name": {
                                    "$in": usernames
                                }
                            }]
                        }, {
                            "type": "user"
                        }, {
                            "computers": {
                                "$elemMatch": {
                                    "$eq": ObjectId(nodeid)
                                }
                            }
                        }]
                    }, {
                        '_id': 1,
                        'name': 1,
                        'path': 1
                    }))
            # ObjectId to string for JSON serialize
            [d.update({'_id': str(d['_id'])}) for d in users]

            # Create a list of users that provides at least one user policy to this computer
            users_inheritance_pre = list(
                node_collection.find(
                    {
                        "$and": [{
                            "$or": [{
                                "name": {
                                    "$in": usernames
                                }
                            }]
                        }, {
                            "type": "user"
                        }, {
                            "computers": {
                                "$elemMatch": {
                                    "$eq": ObjectId(nodeid)
                                }
                            }
                        }]
                    }, {
                        '_id': 1,
                        'name': 1,
                        'path': 1,
                        'inheritance': 1
                    }))
            [d.update({'_id': str(d['_id'])}) for d in users_inheritance_pre]

            users_inheritance = []
            for usr_inh in users_inheritance_pre:
                if 'inheritance' in usr_inh:
                    policies_list = get_inheritance_tree_policies_list(
                        usr_inh['inheritance'], [])
                    if len(policies_list) > 0:
                        users_inheritance.append(usr_inh)

            cpu = ohai.get('cpu', {}).get('0', {})
            dmi = ohai.get('dmi', {})

            # debug_mode flag for logs tab
            debug_mode = False
            try:
                debug_mode = computer_node.attributes.get_dotted(
                    DEBUG_MODE_ENABLE_ATTR_PATH)
            except KeyError:
                pass

            # Get logs info
            logs_data = node_collection.find_one(
                {
                    "type": "computer",
                    "_id": ObjectId(nodeid)
                }, {"logs": True})
            logs = {}
            if logs_data is not None and 'logs' in logs_data:
                logs_data = logs_data['logs']

                date_format = locale.nl_langinfo(locale.D_T_FMT)
                date = datetime.datetime(
                    *map(int,
                         re.split('[^\d]', logs_data['date'])[:-1]))
                localename = locale.normalize(
                    get_current_request().locale_name + '.UTF-8')
                logger.debug("/api/computers/: localename: %s" %
                             (str(localename)))
                locale.setlocale(locale.LC_TIME, localename)
                logs['date'] = date.strftime(date_format)
                logger.debug("/api/computers/: date: %s" % (str(logs['date'])))

                logs['files'] = logs_data['files']
                for filedata in logs_data['files']:
                    # Do not send file contents
                    del filedata['content']

            # Get Help Channel info
            help_channel_enabled = True
            helpchannel_data = self.request.db.helpchannel.find({
                "computer_node_id":
                result['node_chef_id']
            }).sort([("last_modified", pymongo.DESCENDING)]).limit(6)
            helpchannel = {}
            helpchannel['current'] = None
            helpchannel['last'] = []
            if helpchannel_data is not None:
                c = 0
                for hcdata in helpchannel_data:
                    # Format date
                    date_format = locale.nl_langinfo(locale.D_T_FMT)
                    logger.info("last_modified: {0}".format(
                        hcdata['last_modified']))
                    last_mod = re.split('[^\d]', str(hcdata['last_modified']))
                    logger.info("last_mod: {0}".format(last_mod))

                    date = datetime.datetime(*map(int, last_mod[:-2]))
                    localename = locale.normalize(
                        get_current_request().locale_name + '.UTF-8')
                    logger.debug("/api/computers/: localename: %s" %
                                 (str(localename)))
                    locale.setlocale(locale.LC_TIME, localename)
                    hcdata['last_modified'] = date.strftime(date_format)

                    if hcdata['user_node_id']:
                        # Format user
                        user_data = node_collection.find_one({
                            "type":
                            "user",
                            "_id":
                            ObjectId(hcdata['user_node_id'])
                        })
                        if user_data:
                            hcdata['user'] = user_data['name']
                        else:
                            logger.error("User not found: {0}".format(
                                hcdata['user_node_id']))
                    else:
                        hcdata['user'] = ''

                    if hcdata['adminuser_id']:
                        # Format user
                        user_data = self.request.db.adminusers.find_one(
                            {"_id": ObjectId(hcdata['adminuser_id'])})
                        if user_data:
                            hcdata['admin'] = user_data['username']
                        else:
                            logger.error("Admin user not found: {0}".format(
                                hcdata['adminuser_id']))
                    else:
                        hcdata['admin'] = ''

                    # Translate status info
                    hcdata['status'] = _('Unknown status')
                    if hcdata['action'] == 'request':
                        hcdata['status'] = _('User is requesting support')
                    elif hcdata['action'] == 'accepted':
                        hcdata['status'] = _('User is requesting support')
                    elif hcdata['action'] == 'finished user':
                        hcdata['status'] = _('Terminated by user')
                    elif hcdata['action'] == 'finished tech':
                        hcdata['status'] = _('Terminated by technician')
                    elif hcdata['action'] == 'finished error':
                        hcdata['status'] = _(
                            'Terminated because of a communication error')
                    elif hcdata['action'] == 'giving support':
                        hcdata['status'] = _(
                            'The technician is giving support to the user')

                    hcdata['_id'] = str(hcdata['_id'])

                    if (c == 0 and hcdata['action'] == 'accepted'):
                        helpchannel['current'] = hcdata
                    else:
                        helpchannel['last'].append(hcdata)

                    c = c + 1

            result.update({
                'ohai':
                ohai,
                'users':
                users,  # Users related with this computer
                'users_inheritance':
                users_inheritance,  # Users related with this computer that provides at least one user policy
                'uptime':
                ohai.get('uptime', ''),
                #'gcc_link': ohai.get('gcc_link',True),
                'ipaddress':
                ohai.get('ipaddress', ''),
                'cpu':
                '%s %s' %
                (cpu.get('vendor_id', ''), cpu.get('model_name', '')),
                'product_name':
                dmi.get('system', {}).get('product_name', ''),
                'manufacturer':
                dmi.get('system', {}).get('manufacturer', ''),
                'ram':
                ohai.get('memory', {}).get('total', ''),
                'lsb':
                ohai.get('lsb', {}),
                'kernel':
                ohai.get('kernel', {}),
                'filesystem':
                ohai.get('filesystem', {}),
                'debug_mode':
                debug_mode,
                'logs':
                logs,
                'helpchannel':
                helpchannel,
                'help_channel_enabled':
                help_channel_enabled
            })
        except (urllib2.URLError, ChefError, ChefServerError):
            logger.error(
                "/api/computers/: error getting data: node_chef_id: %s " %
                (str(result.get('node_chef_id', None))))
            logger.error(traceback.format_exc())

        return result
Exemple #5
0
 def validate_data(self, node, cookbook, api):
     schema = cookbook['metadata']['attributes']['json_schema']['object']
     validate(to_deep_dict(node.attributes), schema)
Exemple #6
0
    def get(self):
        result = super(ComputerResource, self).get()
        node_collection = self.request.db.nodes
        
        if not result.get('node_chef_id', None):
            return result
        
        logger.info("/api/computers/: node_chef_id: %s" % (str(result.get('node_chef_id', None))))

        try:
            api = get_chef_api(self.request.registry.settings, self.request.user)
            computer_node = ChefNode(result['node_chef_id'], api)
            ohai = to_deep_dict(computer_node.attributes)
            nodeid = result.get('_id',None)
            usernames = [i['username'] for i in ohai.get('ohai_gecos', {}).get('users', [])]
            users = list(node_collection.find({
                "$and": [
                    { "$or": [{"name": {"$in": usernames}}] }, 
                    { "type":"user"},
                    { "computers": {"$elemMatch": {"$eq": ObjectId(nodeid)}}}
                 ]
            },{'_id':1,'name':1,'path':1}))
            # ObjectId to string for JSON serialize
            [d.update({'_id': str(d['_id'])}) for d in users]
            
            # Create a list of users that provides at least one user policy to this computer
            users_inheritance_pre = list(node_collection.find({
                "$and": [
                    { "$or": [{"name": {"$in": usernames}}] }, 
                    { "type":"user"},
                    { "computers": {"$elemMatch": {"$eq": ObjectId(nodeid)}}}
                 ]
            },{'_id':1,'name':1,'path':1, 'inheritance': 1}))
            [d.update({'_id': str(d['_id'])}) for d in users_inheritance_pre]

            users_inheritance = []
            for usr_inh in users_inheritance_pre:
                if 'inheritance' in usr_inh:
                    policies_list = get_inheritance_tree_policies_list(usr_inh['inheritance'], [])
                    if len(policies_list) > 0:
                        users_inheritance.append(usr_inh)
            
            
            cpu = ohai.get('cpu', {}).get('0', {})
            dmi = ohai.get('dmi', {})
            
            # debug_mode flag for logs tab
            debug_mode = False
            try:
                debug_mode = computer_node.attributes.get_dotted(DEBUG_MODE_ENABLE_ATTR_PATH)
            except KeyError:
                pass
            
            # Get logs info
            logs_data = node_collection.find_one({"type": "computer", "_id": ObjectId(nodeid)}, {"logs": True})
            logs = {}
            if logs_data is not None and 'logs' in logs_data:
                logs_data = logs_data['logs']
                
                date_format = locale.nl_langinfo(locale.D_T_FMT)
                date = datetime.datetime(*map(int, re.split('[^\d]', logs_data['date'])[:-1]))
                localename = locale.normalize(get_current_request().locale_name+'.UTF-8')
                logger.debug("/api/computers/: localename: %s" % (str(localename)))
                locale.setlocale(locale.LC_TIME, localename)
                logs['date'] = date.strftime(date_format)
                logger.debug("/api/computers/: date: %s" % (str(logs['date'])))
                
                logs['files'] = logs_data['files']
                for filedata in logs_data['files']:
                    # Do not send file contents
                    del filedata['content']
            
            # Get Help Channel info
            help_channel_enabled = True
            helpchannel_data = self.request.db.helpchannel.find(
                {"computer_node_id" : result['node_chef_id']}).sort(
                    [("last_modified", pymongo.DESCENDING)]).limit(6)
            helpchannel = {}
            helpchannel['current'] = None
            helpchannel['last'] = []
            if helpchannel_data is not None:
                c = 0
                for hcdata in helpchannel_data:
                    # Format date
                    date_format = locale.nl_langinfo(locale.D_T_FMT)
                    logger.info("last_modified: {0}".format( hcdata['last_modified']))
                    last_mod = re.split('[^\d]', str(hcdata['last_modified']))
                    logger.info("last_mod: {0}".format(last_mod))

                    date = datetime.datetime(*map(int, last_mod[:-2]))
                    localename = locale.normalize(get_current_request().locale_name+'.UTF-8')
                    logger.debug("/api/computers/: localename: %s" % (str(localename)))
                    locale.setlocale(locale.LC_TIME, localename)
                    hcdata['last_modified'] = date.strftime(date_format)
                    
                    if hcdata['user_node_id']:
                        # Format user
                        user_data = node_collection.find_one({"type": "user", "_id": ObjectId(hcdata['user_node_id'])})
                        if user_data:
                            hcdata['user'] = user_data['name']
                        else:
                            logger.error("User not found: {0}".format(hcdata['user_node_id']))
                    else:
                        hcdata['user'] = ''
                    
                    if hcdata['adminuser_id']:
                        # Format user
                        user_data = self.request.db.adminusers.find_one({"_id": ObjectId(hcdata['adminuser_id'])})
                        if user_data:
                            hcdata['admin'] = user_data['username']
                        else:
                            logger.error("Admin user not found: {0}".format(hcdata['adminuser_id']))
                    else:
                        hcdata['admin'] = ''
                        
                    # Translate status info
                    hcdata['status'] = _('Unknown status')
                    if hcdata['action'] == 'request':
                        hcdata['status'] = _('User is requesting support')
                    elif hcdata['action'] == 'accepted':
                        hcdata['status'] = _('User is requesting support')
                    elif hcdata['action'] == 'finished user':
                        hcdata['status'] = _('Terminated by user')
                    elif hcdata['action'] == 'finished tech':
                        hcdata['status'] = _('Terminated by technician')
                    elif hcdata['action'] == 'finished error':
                        hcdata['status'] = _('Terminated because of a communication error')
                    elif hcdata['action'] == 'giving support':
                        hcdata['status'] = _('The technician is giving support to the user')
                        
                        
                    hcdata['_id'] = str(hcdata['_id'])                
                    
                    if (c==0 and hcdata['action']=='accepted'):
                        helpchannel['current'] = hcdata
                    else:
                        helpchannel['last'].append(hcdata)
                    
                    c = c + 1
           
            
            result.update({'ohai': ohai,
                           'users': users, # Users related with this computer
                           'users_inheritance': users_inheritance, # Users related with this computer that provides at least one user policy
                           'uptime': ohai.get('uptime', ''),
                           #'gcc_link': ohai.get('gcc_link',True),
                           'ipaddress': ohai.get('ipaddress', ''),
                           'cpu': '%s %s' % (cpu.get('vendor_id', ''), cpu.get('model_name', '')),
                           'product_name': dmi.get('system', {}).get('product_name', ''),
                           'manufacturer': dmi.get('system', {}).get('manufacturer', ''),
                           'ram': ohai.get('memory', {}).get('total', ''),
                           'lsb': ohai.get('lsb', {}),
                           'kernel': ohai.get('kernel', {}),
                           'filesystem': ohai.get('filesystem', {}),
                           'debug_mode': debug_mode,
                           'logs': logs,
                           'helpchannel': helpchannel,
                           'help_channel_enabled': help_channel_enabled
                           })
        except (urllib2.URLError, ChefError, ChefServerError):
            pass
        return result
Exemple #7
0
 def validate_data(self, node, cookbook, api):
     schema = cookbook['metadata']['attributes']['json_schema']['object']
     validate(to_deep_dict(node.attributes), schema)