Exemplo n.º 1
0
def openwhisk(options):
    ''' return enhanced openwhisk client capable of deploying compositions '''

    # try to extract apihost and key first from whisk property file file and then from os.environ
    # api_key precedence; only relevant for CF namespaces, from highest to lowest:
    # 1. CLI option --auth
    # 2. Environment variable __OW_API_KEY
    # 3. AUTH value in ~/.wskprops file

    try:
        wskpropsPath = os.environ[
            'WSK_CONFIG_FILE'] if 'WSK_CONFIG_FILE' in os.environ else os.path.expanduser(
                '~/.wskprops')
        with open(wskpropsPath) as f:
            lines = f.readlines()

        options = dict(options)

        for line in lines:
            parts = line.strip().split('=')
            if len(parts) == 2:
                if parts[0] == 'APIHOST':
                    options['apihost'] = parts[1]
                elif parts[0] == 'AUTH':
                    api_key = parts[1]
    except:
        pass

    if '__OW_API_HOST' in os.environ:
        options['apihost'] = os.environ['__OW_API_HOST']

    if '__OW_API_KEY' in os.environ:
        api_key = os.environ['__OW_API_KEY']

    if 'api_key' in options:
        api_key = options['api_key']

    namespace_mode = get_namespace_mode()
    if namespace_mode == NamespaceType.IAM:
        options['auth_header'] = get_iam_auth_header()
        options['namespace'] = get_namespace_id()
    else:
        options['api_key'] = api_key
        options['namespace'] = '_'

    print('deploying to {}-based namespace \'{}\' ...'.format(
        namespace_mode.name, options['namespace']))

    try:
        import openwhisk
        wsk = openwhisk.Client(options)
    except:
        # FIXME proper exception handling, Client is not defined here
        wsk = Client(options)

    wsk.compositions = Compositions(wsk)
    return wsk
def openwhisk(options):
    ''' return enhanced openwhisk client capable of deploying compositions '''

    # try to extract apihost and key first from whisk property file file and then from os.environ
    try:
        wskpropsPath = os.environ[
            'WSK_CONFIG_FILE'] if 'WSK_CONFIG_FILE' in os.environ else os.path.expanduser(
                '~/.wskprops')
        with open(wskpropsPath) as f:
            lines = f.readlines()

        options = dict(options)

        for line in lines:
            parts = line.strip().split('=')
            if len(parts) == 2:
                if parts[0] == 'APIHOST':
                    options['apihost'] = parts[1]
                elif parts[0] == 'AUTH':
                    options['api_key'] = parts[1]
    except:
        pass

    if '__OW_API_HOST' in os.environ:
        options['apihost'] = os.environ['__OW_API_HOST']

    if '__OW_API_KEY' in os.environ:
        options['api_key'] = os.environ['__OW_API_KEY']

    try:
        import openwhisk
        wsk = openwhisk.Client(options)
    except:
        wsk = Client(options)

    wsk.compositions = Compositions(wsk)
    return wsk