Beispiel #1
0
def delete_system_user(user_id, username):
    """
    Delete a system user from Joomla and also delete all accounts on all VMs.

    FIXME: Delete users in a celery task? What do we do if the user can't be deleted, the VM is off, etc????
    """

    user_db = Cvl_users.select(Cvl_users.q.id==user_id).getOne()

    # For each VM that the user is a member of:
    for vm_id, vm_ip, vm_name in cvlsql.get_users_vms(user_id):
        # Delete the user's account on the VM.
        try:
            cvlfabric.env.hosts = [vm_ip]
            cvlfabric.execute(cvlfabric.delete_user, username=username, warn_only=True)
        except:
            logging.error('Could not delete user %s from VM %s' % (username, vm_ip,))

        # Remove the mapping from the user to the VM.
        cvlsql.delete_user_from_vm(user_id, vm_id)

    # Remove the user's user_group mapping:
    Cvl_user_usergroup_map.delete(Cvl_user_usergroup_map.select(Cvl_user_usergroup_map.q.id==user_id).getOne().id)

    # Remove the user record.
    Cvl_users.delete(user_id)
    def DeleteServer(self): # FIXME perhaps this should be "DeleteUserFromServer"? What's the deal with DeleteVmUserAccount???
        logging.debug('Deleting user account <%s> on the VM %s' % (self.username, self.serverIp,))

        try:
            cvlfabric.env.hosts = [self.serverIp]
            cvlfabric.execute(cvlfabric.delete_user, username=self.username)
        except:
            logging.error('Could not remove user %s from vm %s: %s' % (self.username, self.serverIp, traceback.format_exc()))
            print 'Could not remove the user %s from the vm %s. Please check if the user is currently logged in.' % (self.username, self.serverIp,)
            return False
 
        try:
            logging.debug('Removing VM user map for user_id = %s to vm_id %s' % (self.userId, self.vmDbId))
            cvlsql.delete_user_from_vm(self.userId, self.vmDbId)
        except:
            logging.error('Error removing user with id %s from vm with id %s.' % (self.userId, self.vmDbId,))
            return False
 
        print 'Deleted user %s from the vm %s.' % (self.username, self.serverIp,)
        return True