An overview,

In practical engineering development, different database environments are often configured for different environments such as development, testing and production. The traditional way can achieve the effect of dynamic switch during deployment by adding configuration files of different environments. This is fine, but different environments tend to share the same configuration, causing some duplication of effort.

Project code

./ ├─ ├─ test.pyCopy the code

Although there are only 2 files, the actual situation is more than 10 files, here is just an example!

Conf.py this is the configuration file that all other py files depend on

#! /usr/bin/env python3 # coding: Utf-8 # # k8s configuration file information # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # development environment # # # # # # # # # # # # # # # # # # # # K8S_MASTER = "192.168.0.162" K8S_USERNAME = "root" K8S_PASSWD = "root" # # production # # # # # # # # # # # # # # # # # # # # # # K8S_MASTER = "10.0.0.167 K8S_USERNAME = "root" # K8S_PASSWD = "abcd@1234"Copy the code

Test.py test file, the following code is pseudo code, for demonstration only!

#! /usr/bin/env python3 # coding: utf-8 import conf def ssh2(ip,username,password,cmd): Print (IP,username,password, CMD) return True res = ssh2(conf.K8S_MASTER,conf.K8S_USERNAME,conf.K8S_PASSWD,"uptime") print(res)Copy the code

The current state of the requirement is that test.py is executed like this

python3 test.py
Copy the code

If you need to change the environment, change conf.py and comment out the corresponding variable!

But if there are too many variables, the annotation is too cumbersome! You need a variable that can switch environments with one click

Modify the configuration file

One-click toggle conf.py

#! /usr/bin/env python3 # coding: utf-8 # configuration file import OS class Config(object): # get attribute def __getitem__(self, key): return self.__getattribute__(key) class ProductionConfig(Config): # production environment K8S_MASTER = "10.0.0.167" K8S_USERNAME = "root" K8S_PASSWD = "abcd@1234" Class DevelopmentConfig(Config): K8S_MASTER = "192.168.0.162" K8S_USERNAME = "root" K8S_PASSWD = "root" DevelopmentConfig, 'production': ProductionConfig, 'default': DevelopmentConfig} # One-click toggle environment APP_ENV = os.envion. get('APP_ENV', 'default').lower() # set the environment variable to default config = mapping[APP_ENV]() # obtain the specified environmentCopy the code

Test. py needs to modify the imported module

#! /usr/bin/env python3 # coding: utf-8 # import conf from conf import config as conf def ssh2(ip,username,password,cmd): Print (IP,username,password, CMD) return True res = ssh2(conf.K8S_MASTER,conf.K8S_USERNAME,conf.K8S_PASSWD,"uptime") print(res)Copy the code

Execute test.py and output:

192.168.0.162 root root uptime
True
Copy the code

Now you need to switch environments and change the penultimate line of conf.py directly

APP_ENV = os.enviroget ('APP_ENV', 'production').lower() # set the environment variable to default config = mapping[APP_ENV]() # retrieve the specified environmentCopy the code

Execute test.py again, output:

10.0.0.167 root abcd@1234 uptime
True
Copy the code

Conf.py is not flexible enough to change the environment when executing the script. This parameter determines the environment, for example:

python test.py dev
Copy the code

That way, you can switch to the development environment! So how do we do that?

It’s as simple as changing conf.py! Because all the other files depend on it

conf.py

#! /usr/bin/env python3 # coding: utf-8 # configuration file import OS class Config(object): # get attribute def __getitem__(self, key): return self.__getattribute__(key) class ProductionConfig(Config): # production environment K8S_MASTER = "10.0.0.167" K8S_USERNAME = "root" K8S_PASSWD = "abcd@1234" Class DevelopmentConfig(Config): K8S_MASTER = "192.168.0.162" K8S_USERNAME = "root" K8S_PASSWD = "root" DevelopmentConfig, 'pro': ProductionConfig, 'default': DevelopmentConfig} # # Toggle environment # APP_ENV = os.envion. get('APP_ENV', 'production').lower() # set environment variable to default # config = mapping[APP_ENV]() Import sys # print(sys.argv) num = len(sys.argv) -1 if num < 1 or num > 1: exit Such as: Python xx. Py dev | pro | default ") env = sys. Argv [1] # # print environment (env) APP_ENV = OS. Environ. Get (' APP_ENV ', Env).lower() config = mapping[APP_ENV]() #Copy the code

Execute test.py again

python test.py
Copy the code

Output:

Parameter error, must pass environment variable! Such as: python xx. Py dev | pro | defaultCopy the code

Switch to the development environment

python test.py dev
Copy the code

Output:

192.168.0.162 root root uptime
True
Copy the code

Switch to the production environment

Output:

10.0.0.167 root abcd@1234 uptime
True
Copy the code