Choosing test config and settings in python flask project when running py.test - python-3.x

How to control which _settings.py or config/.py is loaded when starting py.test? I have some defaults set in init.py and some values in development.py. I have tried to load test values from conftest.py but conftest gets executed after the other 2 are loaded. I am specifically looking to change the db being used in development, test and production modes. How to achieve this?

What you are looking for is configuration management in flask. Take a look at this document: flask config management
This SO question could help you too: enter link description here
Basically, depending on your environment (devel / production / test) you change parameter specifying with which settings your app should start. Using system environment variables is one of the option.
app = Flask(__name__)
app.config.from_envvar('YOURAPPLICATION_SETTINGS')
Then, on your devel machine, just set env variable
export YOURAPPLICATION_SETTINGS="/path/to/settings.conf"
and run the application.
On production environment, do the same thing, just change value to /path/to/production/settings.conf

Related

running heroku django app from a virtual environment

I have a django app which i run from my virtual environment on my localhost and in that virtual environment i made some changes to django admin panel's html source code but when i deploy it to heroku. it installs another django with pip and doesn't run from the virtual environment i made changes to resulting into the loss of the changes i made.
You should extend the templates and not hard-code overwrite them. Checkout this SO answer to see some ways to do that.

How to Load Azure App Service Application Settings into VUE?

We have a Vue app hosted as an Azure App Service. Under Settings\Configuration in Azure Portal We have added application settings like VUE_APP_API_ENDPOINT_URL. These become environment variables like the documentation explains, and can be verified by opening a console from the portal and type 'env'.
I Had hoped that These env variables would now be accessible inside Vue by use of
process.env.VUE_APP_API_ENDPOINT_URL
My guess is that its only becomes available in VUE when aplication is build with WebPack or similar.
At least it doesn't work.
Are there any nice way to read those env variables created from Azure App Settings into the vue app?
Some people mentions dotenv npm package, but we need to read the env variables not add them from a config file.
You would be correct that the Environment Variables only become available when you build the application. And to elaborate on that, only the Environment Variables that you specify/supply at build time are the ones that become available in the application from the build process as per the documentation here:
https://cli.vuejs.org/guide/mode-and-env.html#environment-variables
Specifically look at this:
Note that only NODE_ENV, BASE_URL, and variables that start with VUE_APP_ will be statically embedded into the client bundle with webpack.DefinePlugin. It is to avoid accidentally exposing a private key on the machine that could have the same name.
I was struggling to accomplish essentially the same thing that you're trying to accomplish. I was trying to get a value from Azure's Application Settings so that I could setup multiple environments for my application and not have to constantly change values depending on the environment I published the app to.
After realizing that you might be onto something and reading the confirmation of such in the documentation, I decided to try putting the Environment Variable that I was trying to get from Azure's Application Settings in a .env file with a default so that it would be specified at build time. I then published my app to Azure and tested it and it worked!
Try creating a .env file with all of the Azure Application Settings that you're trying to set with default values or something, like:
VUE_APP_API_ENDPOINT_URL=default_value
And then set those same variables into your Azure Application Settings with the proper values and it should work.
Zoull's comment, while somewhat factual, is not possible. His comment implies that setting VUE_APP_API_ENDPOINT in Azure's Static App Settings blade will seamlessly include that var, and perhaps other VUE_APP_* vars into the vue app.
This is wrong.
Webpack is responsible for inclusion of VUE_APP_* vars into the build, and this is only possible at build-time.
This can be verified by following his logic, and then dumping to console "env" at runtime. Values will be set to, permanently, whatever they were at build-time.
tl;dr: Vue will never read, post-build, vars from Application Settings.
I use Github actions to build and deploy. By adding an env: setting after the with: stanza, and including VUE_APP_* vars there, I can do what OP is trying to do.
I believe I can also set some github "secrets" in githubs settings for my repo, and also include them dynamically in the YAML.
Ex. If I have a github secret key/val of: "VUE_APP_FOO: true", in my github action yml, I can do:
env:
VUE_APP_FOO: ${{secrets.VUE_APP_FOO}}
Then, in my final vue build, I should have a value of "true" when I read the process.env.VUE_APP_FOO var.
Savvy?

NodeJS - Get environment variables set using cmd

I just wanted to know if there is any way to simply read environment variables I'vet set using SET in cmd.
I've read about process.env.[ENVVAR], but when I console.log the variable I've set in cmd, it shows undefined.
On other threads I read that it isn't even possible at all to access windows env. variables.
So what is actually right?
I will summarize my comments into an answer.
When you start node.js from a cmd window, a copy of the current user environment is created just for that node.js process. That environment can be accessed via process.env.
That environment will not be changed by any outside agents. Once the node.js process is started, its environment belongs uniquely to the node.js process.
Making changes to the Windows default environment via Windows Control Panel > System > Advanced System Settings > Advanced > Environment Variables affects what variables/values will be set in newly created environments (e.g newly created cmd windows). It does not affect currently open or running environments.
Using process.env, you can read all the existing environment variables in your own environment. You can modify the process.env object directly (changing values, removing properties, etc...) and those changes will be seen by any other code within your process accessing process.env. But outside changes to an environment in some other cmd window will not affect the environment in a running node.js program.

Passing app secrets in Yesod and Keter

I'm building a web app with Yesod and am currently passing in secrets such as API keys via environment variables (as per The Twelve-Factor App) to avoid storing these values in version-controlled configuration files. For example, I run my app in dev mode as follows:
SOME_API_KEY=value yesod devel
I have a value in my config/settings.yml file that is defined in terms of this environment variable with an empty value as follows:
meetup-api-key: "_env:SOME_API_KEY:"
To deploy using Keter, I'm building the Keter package using the yesod keter command and dropping the resulting file into Keter 'sincoming directory. Since I'm using environment variable configuration, my app's .keter file does not contain the SOME_API_KEY value (which is intentional).
How should I pass SOME_API_KEY into the instance of my app running inside Keter?
I would like to avoid baking the value into my keter-config.yaml for at least the following three reasons:
It is less secure than the environment variable approach.
Being embedded directly into the configuration file for Keter itself, as opposed to the configuration to the app, the secret cannot be changed without stopping and restarting the entire Keter process.
The environment variables are passed into every app being managed by Keter.
I am hoping that there are some "best practices" out there for this scenario.
Set your secrets as environment variables on your server and ‘forward’ them to your app using forward-env in config/keter.yml as seen in the following patch: https://github.com/snoyberg/keter/commit/9e9fca314fb78860fb5c9b08cad212d92b0b20d4

nodejs openshift cartridge - not reading custom environment variables

I am identifying my openshift nodejs app environment through system environment variables Ex: 'staging', 'production'. My custom environment variable name is OPENSHIFT_APP_ENV. I have set this in .bash_profile and ran source .bash_profile.
When I did printenv or echo $OPENSHIFT_APP_ENV in command line in my openshift app, I can see the values set properly.
But these variables are not read/set in my nodejs app. I am simply trying to read it as global.ENV = process.env.OPENSHIFT_APP_ENV || "development";
I feel that it should be simple setting issue, but could not get this working somehow. Any help will be appreciated.
You should use the rhc env set command as explained in this section (https://developers.openshift.com/en/managing-environment-variables.html#custom-variables) of the Developer Center to set your environment variables. Especially if you are using a scaled application, that makes sure that your custom environment variables are created on all gears.
It is also standard practice (i believe) to use the NODE_ENV environment variable to determine what environment you are operating in.
You should also make sure to stop & start (not restart) your application after you create environment variables to make sure that your process picks them up correctly (may solve your issue of your application not seeing the ones from your .bash_profile, but i would still recommend using the rhc env set command instead)

Resources