#108 [WIP] Support to get more values from OpenShift env
Merged 4 years ago by frantisekz. Opened 4 years ago by frantisekz.

file modified
+8 -16
@@ -6,6 +6,8 @@ 

  import os

  import sys

  

+ from . import config

+ 

  from .util.login import FakeFas

  

  # the version as used in setup.py and docs
@@ -17,17 +19,21 @@ 

  

  fas = None

  

+ # Is this an OpenShift deployment?

+ openshift = os.getenv('OPENSHIFT_PROD')

+ 

  # load default configuration

  if os.getenv('DEV') == 'true':

      app.config.from_object('blockerbugs.config.DevelopmentConfig')

      fas = FakeFas(app)

- elif os.getenv('TEST') == 'true':

+ elif os.getenv('TEST') == 'true' or openshift == "0":

      fas = FAS(app)

      app.config.from_object('blockerbugs.config.TestingConfig')

  else:

      fas = FAS(app)

      app.config.from_object('blockerbugs.config.ProductionConfig')

- 

+ if openshift:

+     config.openshift_config(app.config, openshift)

  

  # load real configuration values to override defaults

  config_file = '/etc/blockerbugs/settings.py'
@@ -84,20 +90,6 @@ 

  

  setup_logging()

  

- # check if we're running in an OpenShift deployment

- if app.config['OPENSHIFT']:

-     try:

-         dbuser = os.environ["POSTGRESQL_USER"]

-         dbpass = os.environ["POSTGRESQL_PASSWORD"]

-         dbname = os.environ["POSTGRESQL_DATABASE"]

-         dbhost = os.environ["POSTGRESQL_SERVICE_HOST"]

-         dbport = os.environ["POSTGRESQL_SERVICE_PORT"]

-     except(KeyError):

-         print("OpenShift mode enabled but values required for db connection couldn't be fetched. "

-         "Check, if you have these variables defined in you env: "

-         "(POSTGRESQL_[USER, PASSWORD, DATABASE, SERVICE_HOST, SERVICE_PORT])", file=sys.stderr)

-         sys.exit(1)

-     app.config['SQLALCHEMY_DATABASE_URI'] = ("postgresql+psycopg2://%s:%s@%s:%s/%s" % (dbuser, dbpass, dbhost, dbport, dbname))

  

  app.logger.debug('using DBURI: %s' % app.config['SQLALCHEMY_DATABASE_URI'])

  

file modified
+31 -1
@@ -20,6 +20,9 @@ 

  # Authors:

  #   Tim Flink <tflink@redhat.com>

  

+ import os

+ import sys

+ 

  

  class Config(object):

      DEBUG = True
@@ -39,7 +42,6 @@ 

      PRODUCTION = False

      FEDMENU_URL = ""

      FEDMENU_DATA_URL = ""

-     OPENSHIFT = False

  

  

  class ProductionConfig(Config):
@@ -61,3 +63,31 @@ 

  class TestingConfig(Config):

      TESTING = True

  

+ 

+ def openshift_config(config_object, openshift_production):

+     # First, get db details from env

+     try:

+         config_object["SQLALCHEMY_DATABASE_URI"] = "postgresql+psycopg2://%s:%s@%s:%s/%s" % (

+             os.environ["POSTGRESQL_USER"],

+             os.environ["POSTGRESQL_PASSWORD"],

+             os.environ["POSTGRESQL_SERVICE_HOST"],

+             os.environ["POSTGRESQL_SERVICE_PORT"],

+             os.environ["POSTGRESQL_DATABASE"]

+         )

+     except(KeyError):

+         print("OpenShift mode enabled but values required for db connection couldn't be fetched. "

+               "Check, if you have these variables defined in you env: "

+               "(POSTGRESQL_[USER, PASSWORD, DATABASE, SERVICE_HOST, SERVICE_PORT])", file=sys.stderr)

+         sys.exit(1)

+ 

+     # And then try to get more data from OpenShift env

+     try:

+         config_object["FAS_PASSWORD"] = os.environ["FAS_PASSWORD"]

+         config_object["FAS_USER"] = os.environ["FAS_USER"]

+         config_object["FAS_ADMIN_GROUP"] = os.environ["FAS_ADMIN_GROUP"]

+     except(KeyError):

+         print("Expected aditional data to be defined OpenShift env: "

+               "FAS_PASSWORD, FAS_USER, FAS_ADMIN_GROUP, OPENSHIFT_PROD.", file=sys.stderr)

+         # In production OpenShift, exit on missing FAS account details

+         if openshift_production == "1":

+             sys.exit(1)

@@ -15,4 +15,3 @@ 

  STREAM_LOGGING = True

  FEDMENU_URL = "https://apps.fedoraproject.org/fedmenu/"

  FEDMENU_DATA_URL = "https://apps.fedoraproject.org/js/data.js"

- OPENSHIFT = False

Maybe also remove this from config.py?

rebased onto a7f546d

4 years ago

Just in general - I can understand using env-vars for usernames/passwords (I've seen that secret stores usually provide passwords via this channel), maybe even for the DB-related stuff (is it really that dynamic, though?) but why don't you use the standard means of configuration for the BUGZILLA_* vars? I'd rather see an Openshift config section added to config.py, and handling that properly there. Come to think of it, I'd rather see all the config-like-parsing from env vars handled there.

I guess you would find out sooner or later, but this if-clause does not make any sense. Either the openshift_production variable is not yet defined (since the try block "died" on one of those four lines it contains, and you get to die on a stack of KeyError -> NameError traceback (aka not exitting gracefully), or everything was fine, and so this is a dead branch.

On top of that, the if-clause is equivalent to if openshift_production == "1": (I'm guessing here, that OPENSHIFT_PROD is always set, but it might be in the 0/1 range.

1 new commit added

  • Move config handling for OpenShift to config.py
4 years ago

Way better, other than the fact it does not work, I like the direction you took:

$ pwd
/home/jskladan/src/blockerbugs/blockerbugs
$ python3
Python 3.7.5 (default, Oct 17 2019, 12:09:47) 
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import config
OpenShift mode enabled but values required for db connection couldn't be fetched. Check, if you have these variables defined in you env: (POSTGRESQL_[USER, PASSWORD, DATABASE, SERVICE_HOST, SERVICE_PORT])

tldr - u no can has that kind of logic in the class' top level. Makes me a sad panda too :(

1 new commit added

  • Little bit of cleaning for PR
4 years ago

3 new commits added

  • Little bit of cleaning for PR
  • Move config handling for OpenShift to config.py
  • Support to get more values from OpenShift env
4 years ago

Okay, should be in a better shape now, didn't test on real OpenShift yet.

Couldn't this be effectively solved by just using the "Production" config object (that will/should get used anyway by default in production deployment?).
Not a huge deal, but this could effectively cause some heisenbug situations, where you use the /etc/blockerbugs/settings.py file to configure the BUGZILLA_URL, and then the values are "randomly overwritten" later on.
Just food for thought, based on how we treat these two values "in general".
It would make sense if the "runs in openshift & is in production" BUGZILLA values were different from just "is in production" but to me it seems this is not the case.

Maybe also remove this from config.py?

1 new commit added

  • Moar work
4 years ago

Okay, I've reshuffled things a little, I was mind-locked into using default settings.py.example and always trying to override it all over the place. It seems better to just use special settings.py.openshift (will add it later in Dockerfile PR eventually) which by default wouldn't set values we're going to define later in the app anyway.

Seems a lot cleaner now #happypanda

Edit: I am just not sure atm, what are we using on stg instance. Might need a little bit of tweaking.

we could make this even "cleaner" (but I'll leave it on your decision, whether it's better or not:

    try:
    config_object["SQLALCHEMY_DATABASE_URI"] = "postgresql+psycopg2://%s:%s@%s:%s/%s" % (
            os.environ["POSTGRESQL_USER"],
            os.environ["POSTGRESQL_PASSWORD"],
            os.environ["POSTGRESQL_SERVICE_HOST"],
            os.environ["POSTGRESQL_SERVICE_PORT"],
            os.environ["POSTGRESQL_DATABASE"]
        )

    except(KeyError):
        print("OpenShift mode enabled but values required for db connection couldn't be fetched. "
        "Check, if you have these variables defined in you env: "
        "(POSTGRESQL_[USER, PASSWORD, DATABASE, SERVICE_HOST, SERVICE_PORT])", file=sys.stderr)
        sys.exit(1)

I just noticed the extra brackets around the right side of the statement. While this has no impact on functionality, let's remove these just for a slight increase in readability.

1 new commit added

  • Few more finishing touches
4 years ago

LGTM overall, I'm leaving the actual "does this work OK in Openshift" on you here.

If you wanted to make me extra-happy, run autopep8 on the config.py file :)

some copy-pasta ate my whitespaces, obviously the line in the try-block needs to be properly indented... sorry

we could make this even "cleaner" (but I'll leave it on your decision, whether it's better or not:
try:
config_object["SQLALCHEMY_DATABASE_URI"] = "postgresql+psycopg2://%s:%s@%s:%s/%s" % (
os.environ["POSTGRESQL_USER"],
os.environ["POSTGRESQL_PASSWORD"],
os.environ["POSTGRESQL_SERVICE_HOST"],
os.environ["POSTGRESQL_SERVICE_PORT"],
os.environ["POSTGRESQL_DATABASE"]
)

except(KeyError):
    print("OpenShift mode enabled but values required for db connection couldn't be fetched. "
    "Check, if you have these variables defined in you env: "
    "(POSTGRESQL_[USER, PASSWORD, DATABASE, SERVICE_HOST, SERVICE_PORT])", file=sys.stderr)
    sys.exit(1)

some copy-pasta ate my whitespaces, obviously the line in the try-block needs to be properly indented... sorry

Actually, I didn't copy your code, though this was correct way to indent :D ...

5 new commits added

  • Few more finishing touches
  • Moar work
  • Little bit of cleaning for PR
  • Move config handling for OpenShift to config.py
  • Support to get more values from OpenShift env
4 years ago

rebased onto dabaf57

4 years ago

Pull-Request has been merged by frantisekz

4 years ago