def ban_user_and_remove_vessels(username):

    try:
        geniuser = maindb.get_user(username, allow_inactive=True)
    except DoesNotExistError:
        print "No such user: %s." % username
        sys.exit(1)

    # Lock the user.
    lockserver_handle = lockserver.create_lockserver_handle()
    lockserver.lock_user(lockserver_handle, geniuser.username)

    try:
        if geniuser.is_active:
            geniuser.is_active = False
            geniuser.save()
            print "This account has been set to inactive (banned)."
        else:
            print "This account is already inactive (banned)."

        acquired_vessels = maindb.get_acquired_vessels(geniuser)
        if not acquired_vessels:
            print "No acquired vessels to stop/remove access to."
        else:
            print "Vessels acquired by this user: %s" % acquired_vessels
            print "Indicating to the backend to release %s vessels." % len(
                acquired_vessels)
            vessels.release_vessels(lockserver_handle, geniuser,
                                    acquired_vessels)
            print "Release indicated. Monitoring db to see if the backend cleaned them up."
            while True:
                for vessel in acquired_vessels[:]:
                    updated_vessel = maindb.get_vessel(
                        vessel.node.node_identifier, vessel.name)
                    if vessel.node.is_broken or not vessel.node.is_active:
                        print "Node %s is broken or inactive, so backend won't contact it." % vessel.node
                        acquired_vessels.remove(vessel)
                        continue

                    if updated_vessel.is_dirty:
                        print "Vessel %s has not been cleaned up yet." % updated_vessel
                    else:
                        print "Vessel %s has been cleaned up." % updated_vessel
                        acquired_vessels.remove(vessel)

                if not acquired_vessels:
                    print "All vessels have been cleaned up."
                    break
                else:
                    print "%s vessels remain to be cleaned up." % len(
                        acquired_vessels)

                print "Sleeping 10 seconds."
                time.sleep(10)

    finally:
        # Unlock the user.
        lockserver.unlock_user(lockserver_handle, geniuser.username)
        lockserver.destroy_lockserver_handle(lockserver_handle)
def ban_user_and_remove_vessels(username):

  try:
    geniuser = maindb.get_user(username, allow_inactive=True)
  except DoesNotExistError:
    print "No such user: %s." % username
    sys.exit(1)

  # Lock the user.
  lockserver_handle = lockserver.create_lockserver_handle()
  lockserver.lock_user(lockserver_handle, geniuser.username)

  try:
    if geniuser.is_active:
      geniuser.is_active = False
      geniuser.save()
      print "This account has been set to inactive (banned)."
    else:
      print "This account is already inactive (banned)."

    acquired_vessels = maindb.get_acquired_vessels(geniuser)
    if not acquired_vessels:
      print "No acquired vessels to stop/remove access to."
    else:
      print "Vessels acquired by this user: %s" % acquired_vessels
      print "Indicating to the backend to release %s vessels." % len(acquired_vessels)
      vessels.release_vessels(lockserver_handle, geniuser, acquired_vessels)
      print "Release indicated. Monitoring db to see if the backend cleaned them up."
      while True:
        for vessel in acquired_vessels[:]:
          updated_vessel = maindb.get_vessel(vessel.node.node_identifier, vessel.name)
          if vessel.node.is_broken or not vessel.node.is_active:
            print "Node %s is broken or inactive, so backend won't contact it." % vessel.node
            acquired_vessels.remove(vessel)
            continue

          if updated_vessel.is_dirty:
            print "Vessel %s has not been cleaned up yet." % updated_vessel
          else:
            print "Vessel %s has been cleaned up." % updated_vessel
            acquired_vessels.remove(vessel)

        if not acquired_vessels:
          print "All vessels have been cleaned up."
          break
        else:
          print "%s vessels remain to be cleaned up." % len(acquired_vessels)

        print "Sleeping 10 seconds."
        time.sleep(10)

  finally:
    # Unlock the user.
    lockserver.unlock_user(lockserver_handle, geniuser.username)
    lockserver.destroy_lockserver_handle(lockserver_handle)
Example #3
0
def release_vessels(geniuser, vessel_list):
  """
  <Purpose>
    Remove a user from a vessel that is assigned to the user.
  <Arguments>
    geniuser
      The GeniUser who is to be removed from the vessel.
    vessel_list
      A list of vessels the user is to be removed from.
  <Exceptions>
    InvalidRequestError
      If any of the vessels in the vessel_list are not currently acquired by
      geniuser or if the list of vessels is empty.
  <Side Effects>
    The vessel is no longer assigned to the user. If this was the last user
    assigned to the vessel, the vessel is freed.
  <Returns>
    None
  """
  assert_geniuser(geniuser)
  assert_list(vessel_list)
  for vessel in vessel_list:
    assert_vessel(vessel)

  if not vessel_list:
    raise InvalidRequestError("The list of vessels cannot be empty.")

  # Lock the user.
  lockserver_handle = lockserver.create_lockserver_handle()
  lockserver.lock_user(lockserver_handle, geniuser.username)
  
  try:
    # Make sure the user still exists now that we hold the lock. Also makes
    # sure that we see any changes made to the user before we obtained the lock.
    try:
      geniuser = maindb.get_user(geniuser.username)
    except DoesNotExistError:
      raise InternalError(traceback.format_exc())
    
    vessels.release_vessels(lockserver_handle, geniuser, vessel_list)

  finally:
    # Unlock the user.
    lockserver.unlock_user(lockserver_handle, geniuser.username)
    lockserver.destroy_lockserver_handle(lockserver_handle)
Example #4
0
def release_vessels(geniuser, vessel_list):
  """
  <Purpose>
    Remove a user from a vessel that is assigned to the user.
  <Arguments>
    geniuser
      The GeniUser who is to be removed from the vessel.
    vessel_list
      A list of vessels the user is to be removed from.
  <Exceptions>
    InvalidRequestError
      If any of the vessels in the vessel_list are not currently acquired by
      geniuser or if the list of vessels is empty.
  <Side Effects>
    The vessel is no longer assigned to the user. If this was the last user
    assigned to the vessel, the vessel is freed.
  <Returns>
    None
  """
  assert_geniuser(geniuser)
  assert_list(vessel_list)
  for vessel in vessel_list:
    assert_vessel(vessel)

  if not vessel_list:
    raise InvalidRequestError("The list of vessels cannot be empty.")

  # Lock the user.
  lockserver_handle = lockserver.create_lockserver_handle()
  lockserver.lock_user(lockserver_handle, geniuser.username)
  
  try:
    # Make sure the user still exists now that we hold the lock. Also makes
    # sure that we see any changes made to the user before we obtained the lock.
    try:
      geniuser = maindb.get_user(geniuser.username)
    except DoesNotExistError:
      raise InternalError(traceback.format_exc())
    
    vessels.release_vessels(lockserver_handle, geniuser, vessel_list)

  finally:
    # Unlock the user.
    lockserver.unlock_user(lockserver_handle, geniuser.username)
    lockserver.destroy_lockserver_handle(lockserver_handle)