Written by

Senior Startups and Community Programs Manager at InterSystems Corporation
Question Evgeny Shvarov · Jun 5, 2023

Is it possible to use ENV variables for production parameters?

Hi folks!

Is it possible to use Environment variables for production settings?

E.g. I have a telegram adapter for which I need to setup token(part of the production below):

<ItemName="shvarov.i14y.ChatOperation"Category="Reddit"ClassName="Telegram.BusinessOperation"PoolSize="1"Enabled="true"Foreground="false"Comment=""LogTraceEvents="false"Schedule=""><SettingTarget="Adapter"Name="SSLConfig">default</Setting><SettingTarget="Adapter"Name="Token">"some-telegram-token"</Setting></Item>

Is it possible to point the expression for Token setting "some-telegram-token" which will collect it from env-variable?

Comments

Guillaume Rongier · Jun 5, 2023

Hi Evegny,

You can do it in the following way:

  • in the production settings, with DefaultSettings docs here
    • but default settings is a kind of replacement for environment variables
  • you can to it in the code :
    • ObjectScript: $system.Util.GetEnviron("MY_ENV_VAR")
    • Python: os.environ['MY_ENV_VAR']
  • you can use iop (interoperabilty on python) to import production in python way :

settings.py

import os

PRODUCTIONS = [
        {
            'shvarov.i14y.Production': {
                "Item": [
                    {
                        "@Name": "shvarov.i14y.ChatOperation",
                        "@ClassName": "Telegram.BusinessOperation",
                        "@Category": "Reddit",
                        "@PoolSize": "1",
                        "@Enabled": "true",
                        "@Foreground": "false",
                        "@Comment": "",
                        "@LogTraceEvents": "false",
                        "@Schedule": "",
                        "Setting": [
                            {
                                "@Target": "Adapter",
                                "@Name": "SSLConfig",
                                "#text": "default"
                            },
                            {
                                "@Target": "Adapter",
                                "@Name": "Token",
                                "#text": os.environ['TELEGRAM_TOKEN']
                            }
                        ]
                    }
                ]
            }
        } 
    ]

and then in the terminal:

$ iop -M settings.py

More information about iop and settings.pyhere

0
Evgeny Shvarov  Jun 8, 2023 to Guillaume Rongier

Thanks @Guillaume Rongier !

Is it possible to use your export/import mechanism to change only one element of the production?

0
Evgeny Shvarov  Jun 10, 2023 to Guillaume Rongier

For now it ended in the following way:

USER>d ##class(shvarov.telegramgpt.Setup).Init($system.Util.GetEnviron("TG_BOT_TOKEN"),$system.Util.GetEnviron("OPENAPI_KEY"))

This is how we can setup production after docker build and start in the following project. Thank you, @Guillaume Rongier 

0
Guillaume Rongier  Jun 7, 2023 to Alex Woodhead

Good idea, it environment variable is not found switch to settings value.

0