Exemplo n.º 1
0
Arquivo: common.py Projeto: pst/cctrl
def shutdown(api):
    """
        shutdown handles updating or deleting the token file on disk each time
        cctrl finishes or gets interrupted.
    """
    if api.check_token():
        update_tokenfile(api)
    else:
        delete_tokenfile()
Exemplo n.º 2
0
def shutdown(api):
    """
        shutdown handles updating or deleting the token file on disk each time
        cctrl finishes or gets interrupted.
    """
    if api.check_token():
        update_tokenfile(api)
    else:
        delete_tokenfile()
Exemplo n.º 3
0
def run(args, api):
    """
        run takes care of calling the action with the needed arguments parsed
        using argparse.

        We first try to call the action. In case the called action requires a
        valid token and the api instance does not have one a TokenRequiredError
        gets raised. In this case we catch the error and ask the user for a
        email/password combination to create a new token. After that we call
        the action again.

        pycclib raises an exception any time the API does answer with a
        HTTP STATUS CODE other than 200, 201 or 204. We catch these exceptions
        here and stop cctrlapp using sys.exit and show the error message to the
        user.
    """

    # check if there is a newer version of cctrl or pycclib
    try:
        check_for_updates(api.check_versions()['cctrl'])
    except KeyError:
        pass

    while True:
        try:
            try:
                args.func(args)
            except TokenRequiredError:
                # check ENV for credentials first
                try:
                    email = env.pop('CCTRL_EMAIL')
                    password = env.pop('CCTRL_PASSWORD')
                except KeyError:
                    email, password = get_credentials()
                try:
                    api.create_token(email, password)
                except UnauthorizedError:
                    sys.exit(messages['NotAuthorized'])
                else:
                    pass
            except ParseAppDeploymentName:
                sys.exit(messages['InvalidAppOrDeploymentName'])
            else:
                break
        except UnauthorizedError, e:
            if delete_tokenfile():
                api.set_token(None)
            else:
                sys.exit(messages['NotAuthorized'])
        except ForbiddenError, e:
            sys.exit(messages['NotAllowed'])
Exemplo n.º 4
0
def run(args, api):
    """
        run takes care of calling the action with the needed arguments parsed
        using argparse.

        We first try to call the action. In case the called action requires a
        valid token and the api instance does not have one a TokenRequiredError
        gets raised. In this case we catch the error and ask the user for a
        email/password combination to create a new token. After that we call
        the action again.

        pycclib raises an exception any time the API does answer with a
        HTTP STATUS CODE other than 200, 201 or 204. We catch these exceptions
        here and stop cctrlapp using sys.exit and show the error message to the
        user.
    """

    while True:
        try:
            try:
                args.func(args)
            except cclib.TokenRequiredError:
                # check ENV for credentials first
                try:
                    email = os.environ.pop('CCTRL_EMAIL')
                    password = os.environ.pop('CCTRL_PASSWORD')
                except KeyError:
                    email, password = get_credentials()
                try:
                    api.create_token(email, password)
                except cclib.UnauthorizedError:
                    sys.exit(messages['NotAuthorized'])
                else:
                    pass
            except ParseAppDeploymentName:
                sys.exit(messages['InvalidAppOrDeploymentName'])
            else:
                break
        except cclib.UnauthorizedError, e:
            if delete_tokenfile():
                api.set_token(None)
            else:
                sys.exit(messages['NotAuthorized'])
        except cclib.ForbiddenError, e:
            sys.exit(messages['NotAllowed'])