Ejemplo n.º 1
0
    def delete(self):
        node_id = self.request.GET.get('node_id')
        computer = self.collection.find_one({'node_chef_id': node_id})
        node_deleted = self.collection.remove({'node_chef_id': node_id, 'type': 'computer'})
        num_node_deleted = node_deleted['n']
        if num_node_deleted >= 1:
            # Create a job so the administrator can see the 'detached' action
            job_storage = JobStorage(self.request.db.jobs, self.request.user)
            job_storage.create(obj=computer,
                            op='detached',
                            status='finished',
                            message=self._("Pending: %d") %(0),
                            policy={'name':'detached computer',
                                    'name_es':self._('detached') + " " + self._('computer')},
                            administrator_username=self.request.user['username'])
        

            invalidate_delete(self.request, computer)
            if num_node_deleted == 1:
                delete_computer(computer['_id'], computer['path'])
                return {'ok': True}
            return {'ok': False,
                    'message': 'Deleted %s computers' % num_node_deleted}
        elif num_node_deleted < 1:
            return {'ok': False,
                    'message': 'This node does not exist (mongodb)'}
Ejemplo n.º 2
0
    def delete(self):
        node_id = self.request.GET.get('node_id')
        if node_id is None:
            return {'ok': False, 'message': 'Required parameter: node_id'}

        logger.info("/register/computer: Deleting computer for node_id = %s" %
                    (str(node_id)))
        # Find the computer node
        computer = self.collection.find_one({'node_chef_id': node_id})

        # Delete the reference from users
        users = self.collection.find({
            'computers': computer['_id'],
            'type': 'user'
        })
        for user in users:
            logger.info(
                "/register/computer: Removing computer %s relationship from user %s"
                % (str(computer['_id']), str(user['_id'])))
            self.collection.update_one(
                {'_id': user['_id']},
                {'$pull': {
                    'computers': computer['_id']
                }})

        # Delete the computer node
        node_deleted = self.collection.delete_one({
            'node_chef_id': node_id,
            'type': 'computer'
        })
        num_node_deleted = node_deleted.deleted_count
        if num_node_deleted >= 1:
            # Create a job so the administrator can see the 'detached' action
            job_storage = JobStorage(self.request.db.jobs, self.request.user)
            job_storage.create(
                obj=computer,
                op='detached',
                status='finished',
                message=self._("Pending: %d") % (0),
                policy={
                    'name': 'detached computer',
                    'name_es': self._('detached') + " " + self._('computer')
                },
                administrator_username=self.request.user['username'])

            invalidate_delete(self.request, computer)
            if num_node_deleted == 1:
                delete_computer(computer['_id'], computer['path'])
                return {'ok': True}
            return {
                'ok': False,
                'message': 'Deleted %s computers' % num_node_deleted
            }
        elif num_node_deleted < 1:
            return {
                'ok': False,
                'message': 'This node does not exist (mongodb)'
            }
Ejemplo n.º 3
0
 def report_generic_error(self, user, obj, action, message, computer=None, status='errors'):
     job_storage = JobStorage(self.db.jobs, user)
     job_status = status
     job = dict(obj=obj,
                op=action,
                status=job_status,
                message=message,
                administrator_username=user['username'])
     if computer:
         job['computer'] = computer
     job_storage.create(**job)
Ejemplo n.º 4
0
 def report_generic_error(self,
                          user,
                          obj,
                          action,
                          message,
                          computer=None,
                          status='errors'):
     job_storage = JobStorage(self.db.jobs, user)
     job_status = status
     job = dict(obj=obj,
                op=action,
                status=job_status,
                message=message,
                administrator_username=user['username'])
     if computer:
         job['computer'] = computer
     job_storage.create(**job)
Ejemplo n.º 5
0
 def update_node_job_id(self, user, obj, action, computer, node, policy, attr, attributes_updated, job_ids_by_computer):
     if node.attributes.has_dotted(attr):
         job_ids = node.attributes.get_dotted(attr)
     else:
         job_ids = []
     job_storage = JobStorage(self.db.jobs, user)
     job_status = 'processing'
     computer_name = computer['name']
     if is_user_policy(policy.get('path', '')) and 'user' in computer:
         computer['user_and_name'] = '%s / %s' % (computer_name, computer['user']['name'])
     else:
         computer['user_and_name'] = None
     job_id = job_storage.create(obj=obj,
                                 op=action,
                                 status=job_status,
                                 computer=computer,
                                 policy=policy,
                                 administrator_username=user['username'])
     job_ids.append(unicode(job_id))
     job_ids_by_computer.append(job_id)
     attributes_updated.append(attr)
     node.attributes.set_dotted(attr, job_ids)
Ejemplo n.º 6
0
 def update_node_job_id(self, user, obj, action, computer, node, policy,
                        attr, attributes_updated, job_ids_by_computer):
     if node.attributes.has_dotted(attr):
         job_ids = node.attributes.get_dotted(attr)
     else:
         job_ids = []
     job_storage = JobStorage(self.db.jobs, user)
     job_status = 'processing'
     computer_name = computer['name']
     if is_user_policy(policy.get('path', '')) and 'user' in computer:
         computer['user_and_name'] = '%s / %s' % (computer_name,
                                                  computer['user']['name'])
     else:
         computer['user_and_name'] = None
     job_id = job_storage.create(obj=obj,
                                 op=action,
                                 status=job_status,
                                 computer=computer,
                                 policy=policy,
                                 administrator_username=user['username'])
     job_ids.append(unicode(job_id))
     job_ids_by_computer.append(job_id)
     attributes_updated.append(attr)
     node.attributes.set_dotted(attr, job_ids)