Example #1
0
def deboost_server(request):
    """Deboost a server: ie:
        Credit the users account
        Cancel any scheduled De-Boost
        Set the CPUs and RAM to the previous state
        Put the server in a "Pre_Deboosting" status

    Note that a user can Deboost at ANY time, but they only get credit if credit is due.
    Deboosting a non-boosted server just amounts to a restart.

    :param {vm or name}: ID of VApp which we want to deboost.
    :returns: dict(touch_id, vm_id, credit) where credit is the refunded amount
    """
    vm_id, actor_id = _resolve_vm(request)

    credit = server.get_time_until_deboost(vm_id)[3]
    server.touch_to_add_credit(actor_id, credit)

    #Scheduled timeouts don't need cancelling as they are ignored on unboosted servers,
    #and if the user re-boosts then the new timeout will mask the old one.

    #Previous semantics would return the VM to the previous state, but this is not
    #what I really want - altering the baseline in the config should lead to all VMs
    #ending up in the new state after a Boost/Deboost.
    new_cores, new_ram = server.get_baseline_specification(vm_id)

    server.touch_to_add_specification(vm_id, new_cores, new_ram)

    # Tell the agents to get to work.
    touch_id = server.touch_to_state(actor_id, vm_id, "Pre_Deboosting")

    return dict(touch_id=touch_id, vm_id=vm_id, credit=credit)
 def test_subtract(self):
     """
     Behaviour: Calling the API to add credit should result credit being
     subtracted from the database.
     """
     user = create_user('user', 'testuser3', 'testuser3', 'testuser3')
     touch_to_add_credit(user,-500)
     credit = check_credit(user)
     self.assertEqual(credit, -500)
 def test_add(self):
     """
     Behaviour: Calling the API to add credit should result credit being added to
     the database.
     """
     user = create_user('user','testuser2','testuser2','testuser2')
     touch_to_add_credit(user,1000)
     credit = check_credit(user)
     self.assertEqual(credit, 1000)
Example #4
0
    def setUp(self):
        """Launch pserve using webtest with test settings"""

        #Nope, do this for each test...
        #self.appconf = get_app(test_ini)
        #self.app = TestApp(self.appconf)

        server.choose_engine("SQLite")  # Sets global var "engine" - in the
                                        # case of SQLite this is a fresh RAM
                                        # DB each time.

        # Create new user. For not much reason.
        user_id = server.create_user("users", "testuser", "testuser", "testuser")
        server.touch_to_add_credit(user_id, 200)
    def setUp(self):
        """Launch pserve using webtest with test settings"""

        #Nope, do this for each test...
        #self.appconf = get_app(test_ini)
        #self.app = TestApp(self.appconf)

        server.choose_engine("SQLite")  # Sets global var "engine" - in the
        # case of SQLite this is a fresh RAM
        # DB each time.

        # Create new user. For not much reason.
        user_id = server.create_user("users", "testuser", "testuser",
                                     "testuser")
        server.touch_to_add_credit(user_id, 200)
Example #6
0
def create_user_credit(request):
    """Adds credit to a user account, negative or positive.  Only an administrator can do this
       directly.  Boost and Deboost actions will do this implicitly.

    Checks if username is valid, otherwise throws HTTP 404.
    Checks if credit is an integer, otherwise throws HTTP 400.

    :param name: User for which we are amending credit.
    :returns: JSON containing actor id, credit change and new balance.
    """
    username, credit = request.matchdict["name"], request.POST["credit"]
    try:
        user_id = server.get_user_id_from_name(username)
        server.touch_to_add_credit(user_id, int(credit))
        credits = server.check_credit(user_id)
        return {"actor_id": int(user_id), "credit_change": int(credit), "credit_balance": int(credits)}
    except ValueError:
        return HTTPBadRequest()
    except KeyError:
        return HTTPNotFound()
Example #7
0
def deboost_server(request):
    """Deboost a server: ie:
        Credit the users account
        Cancel any scheduled De-Boost
        Set the CPUs and RAM to the previous state
        Put the server in a "Pre_Deboosting" status

    Note that a user can Deboost at ANY time, but they only get credit if credit is due.
    Deboosting a non-boosted server just amounts to a restart.

    :param {vm or name}: ID of VApp which we want to deboost.
    :returns: ???
    """
    vm_id, actor_id = _resolve_vm(request)

    credit = server.get_time_until_deboost(vm_id)[3]
    server.touch_to_add_credit(actor_id, credit)

    #Scheduled timeouts don't need cancelling as they are ignored on unboosted servers.

    #FIXME - yet more hard-coding for cores/RAM
    prev_cores = 1
    prev_ram = 16
    try:
        prev_cores, prev_ram = server.get_previous_specification(vm_id)
    except:
        #OK, use the defaults.
        pass

    #If we're not careful, with this "go back to previous config" semantics, if a user de-boosts
    #a server twice they will actually end up setting their baseline config to the boosted specs.
    #Therefore do a check.
    current_cores, current_ram = server.get_latest_specification(vm_id)

    if not (prev_ram > current_ram):
        server.touch_to_add_specification(vm_id, prev_cores, prev_ram)

    # Tell the agents to get to work.
    touch_id = server.touch_to_state(actor_id, vm_id, "Pre_Deboosting")

    return dict(touch_id=touch_id, vm_id=vm_id, credit=credit)
Example #8
0
def deboost_server(request):
    """Deboost a server: ie:
        Credit the users account
        Cancel any scheduled De-Boost
        Set the CPUs and RAM to the previous state
        Put the server in a "Pre_Deboosting" status

    Note that a user can Deboost at ANY time, but they only get credit if credit is due.
    Deboosting a non-boosted server just amounts to a restart.

    :param {vm or name}: ID of VApp which we want to deboost.
    :returns: ???
    """
    vm_id, actor_id = _resolve_vm(request)

    credit = server.get_time_until_deboost(vm_id)[3]
    server.touch_to_add_credit(actor_id, credit)

    # Scheduled timeouts don't need cancelling as they are ignored on unboosted servers.

    # FIXME - yet more hard-coding for cores/RAM
    prev_cores = 1
    prev_ram = 16
    try:
        prev_cores, prev_ram = server.get_previous_specification(vm_id)
    except:
        # OK, use the defaults.
        pass

    # If we're not careful, with this "go back to previous config" semantics, if a user de-boosts
    # a server twice they will actually end up setting their baseline config to the boosted specs.
    # Therefore do a check.
    current_cores, current_ram = server.get_latest_specification(vm_id)

    if not (prev_ram > current_ram):
        server.touch_to_add_specification(vm_id, prev_cores, prev_ram)

    # Tell the agents to get to work.
    touch_id = server.touch_to_state(actor_id, vm_id, "Pre_Deboosting")

    return dict(touch_id=touch_id, vm_id=vm_id, credit=credit)
Example #9
0
def create_user_credit(request):
    """Adds credit to a user account, negative or positive.  Only an administrator can do this
       directly.  Boost and Deboost actions will do this implicitly.

    Checks if username is valid, otherwise throws HTTP 404.
    Checks if credit is an integer, otherwise throws HTTP 400.

    :param name: User for which we are amending credit.
    :returns: JSON containing actor id, credit change and new balance.
    """
    username, credit = request.matchdict['name'], request.POST['credit']
    try:
        user_id = server.get_user_id_from_name(username)
        server.touch_to_add_credit(user_id, int(credit))
        credits = server.check_credit(user_id)
        return  {'actor_id': int(user_id),
                 'credit_change': int(credit),
                 'credit_balance': int(credits)}
    except ValueError:
        return HTTPBadRequest()
    except KeyError:
        return HTTPNotFound()
Example #10
0
def add_credit(amount, owner):
    owner_id = server.get_user_id_from_name(owner)
    server.touch_to_add_credit(owner_id, int(amount))
def add_credit(amount, owner):
    owner_id = server.get_user_id_from_name(owner)
    server.touch_to_add_credit(owner_id, int(amount))