Git Product home page Git Product logo

env's Introduction

env

The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code; unlike config files, there is little chance of them being checked into the code repo accidentally; and unlike custom config files, or other config mechanisms such as Java System Properties, they are a language- and OS-agnostic standard.

Another aspect of config management is grouping. Sometimes apps batch config into named groups (often called “environments”) named after specific deploys, such as the development, test, and production environments in Rails. This method does not scale cleanly: as more deploys of the app are created, new environment names are necessary, such as staging or qa. As the project grows further, developers may add their own special environments like joes-staging, resulting in a combinatorial explosion of config which makes managing deploys of the app very brittle.

In a twelve-factor app, env vars are granular controls, each fully orthogonal to other env vars. They are never grouped together as “environments”, but instead are independently managed for each deploy. This is a model that scales up smoothly as the app naturally expands into more deploys over its lifetime.

The Twelve-Factor App

This library is a thin wrapper around the JVM's environment variables and system properties, in the spirit of the Twelve-Factor App philosophy.

Motivation

Very small applications are self-contained. They start with a clean slate, live in a single classloader, and have no persistent mutable state. These kinds of programs can be configured simply via command line arguments or by pulling env vars into a Clojure var and passed to individual components of the application as function parameters.

When the application gets more complex it might be running in multiple Clojure runtimes across multiple machines and performing side effects on external resources like databases, message queues, caches, etc. Furthermore, users inevitably interact with the application via a web application frontend that needs to perform side effects on the backend application. These external linkages all need to be configured in the environment in which the applications are built and deployed.

Finally, complex applications need to be able to bootstrap themselves. That is to say, the program needs to coordinate the bootstrapping of its various components when it starts up. This generally takes the form of the application entry point setting environment vars that the rest of the application components will use to configure themselves. Clojure vars are not the answer here because they cannot be accessed from different Clojure runtimes in the same JVM, and so defeat any classloader isolation mechanism in use.

So, we can compile a list of requirements:

  • Obtain a map of environment variable names and values (both strings) from the global env.
  • Set or override environment variables such that the new values are in effect for the whole JVM.
  • Usable in both Clojure and ClojureScript programs.

and some additional features that might be nice to have:

  • Define Clojure vars with values from the environment:
    • optionally assign default values for missing env vars, and
    • persist defaults to the JVM global env.
  • Optionally throw exception when required env vars are missing
    • with a nice, descriptive error message,
    • at runtime in Clojure, and
    • at compile time in ClojureScript.

and some features we don't want:

  • configuration files (see config files, above)
  • non-string names or values (not portable across classloaders)

Usage

[adzerk/env "0.4.0"] ;; latest release

;; clojure
(ns foo
  (:require [adzerk.env :as env]))

;; clojurescript
(ns foo
  (:require-macros [adzerk.env :as env]))

Get

;; both clojure and clojurescript
(env/def
  FOO nil
  BAR :required
  BAZ "supergood"
  BAF (or BAZ "justokay"))
  • This defines the vars FOO, BAR, BAZ and BAF
  • The values are read from:
    • the system property of the same name if it is set, or
    • the environment variable of the same name otherwise
  • The righthand side provides default values and can be:
    • :required (throw an exception), or
    • an expression that evaluates to a string or nil.
  • If a default is applied it is persisted in the env
    • subsequent calls to env/def or env/env reflect the updated value.

Note: Setting or overriding env vars is not currently supported in the ClojureScript implementation. The env is read-only there and default or overridden values are not persisted in the env.

;; clojure only (see https://github.com/adzerk-oss/env/issues/1)
(env/env)
  • Returns a map of all env vars, with system property overrides as above.

Set

;; clojure only
(alter-var-root #'FOO (constantly "new value"))
  • The underlying system property will be updated as well as the var itself.
  • Subsequent calls to env/def and env/env will reflect the updated value.

Hacking

# build and install locally
boot build-jar
# push snapshot
boot build-jar push-snapshot
# push release
boot build-jar push-release

License

Copyright © 2014 Adzerk

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

env's People

Contributors

alandipert avatar arichiardi avatar daveyarwood avatar jumblerg avatar micha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

env's Issues

Exposing environment vars

The env macro can potentially be harmful when used with clojurescript, by making it easy to mistakenly expose supposedly secret environment vars to the world.

Simply using (env) in code can will typically result in all environment vars being embedded in the compiled javascript served to the browser. This may include vars supposed to be kept confidential such as secret keys.

However, it is useful to be able to embed environment vars during cljs compile time, particularly for browser apps. I suggest changing the env macro to require the name of an environment var as argument, and make sure to only inline the value of the specified var in compiled code.

Unexpected def behavior

There might be some problem either in the README or in the code or in my understanding 💃

I have (env/def SOME_VAR "test) in my build.boot but (System.getenv "SOME_VAR") always returns nil in the repl. Instead, (System/getProperty "SOME_VAR") returns "test".

So system prop are defined instead of environment vars but the README states:

the system property of the same name if it is set, or`

...and in my case no property with name SOME_VAR is not set...

It is not a big deal, but some tool (cprop) expects different syntax form System properties vs Env vars so it would be cool to address the problem (if any).

Thanks for this very nice tool again!

Env/def does not persist vars with dots

For example, in:

(env/def
  database_jdbc.url "jdbc:postgresql://..."
  database_username "..."
  database_password "...")

The first env var in not present when I query it with (env/env).

Not finding something that is defined

This is probably some silly error on my part, but I am having trouble getting this to pick up an environmental variable. It says it is not defined, even though it also lists it as defined in it's list of all variables.
This code:

(println (adzerk.env/env))
(adzerk.env/def
  CLOJUSH_URI :required)

Produces:

{LEIN_VERSION 2.7.1, HOME /Users/saul, Apple_PubSub_Socket_Render /private/tmp/com.apple.launchd.uLuj5E1bMs/Render, LEIN_FAST_TRAMPOLINE true, USER saul, LEIN_HOME /Users/saul/.lein, gopherProxySet false, TMPDIR /var/folders/67/rgrfj0s939vf1gh0pvdf55ww0000gn/T/, __CF_USER_TEXT_ENCODING 0x1F5:0x0:0x0, XPC_FLAGS 0x0, SHELL /usr/local/bin/fish, TERM_PROGRAM iTerm.app, COLORFGBG 15;0, JVM_OPTS , TERM xterm-256color, LOGNAME saul, SSH_AUTH_SOCK /private/tmp/com.apple.launchd.kc3y1sOCez/Listeners, JAVA_MAIN_CLASS_55194 clojure.main, ITERM_PROFILE Default, PATH /Applications/Genymotion.app/Contents/MacOS/:/Applications/Genymotion Shell.app/Contents/MacOS/:/Users/saul/.local/bin:/Users/saul/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin, ANDROID_HOME /usr/local/opt/android-sdk, SECURITYSESSIONID 186a7, DISPLAY /private/tmp/com.apple.launchd.NoQLnz9aFX/org.macosforge.xquartz:0, TRAMPOLINE_FILE target/trampolines/33f44f5f639a35f17c7a689fc82e8865bcd966da, JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home, XPC_SERVICE_NAME 0, PWD /Users/saul/projects/ici-recorder, LEIN_JAVA_CMD java, TERM_PROGRAM_VERSION 3.1.20170305-nightly, JAVA_CMD java, TIMBRE_LEVEL :trace, ITERM_SESSION_ID w0t1p0:162ACDD3-02EA-4A48-8351-F43545103CC3, LEIN_JVM_OPTS -XX:+TieredCompilation -XX:TieredStopAtLevel=1, SHLVL 1, COLORTERM truecolor, LANG en_US.UTF-8, CLOJUSH_URI alluxio://localhost:19998/clojush/, TERM_SESSION_ID w0t1p0:162ACDD3-02EA-4A48-8351-F43545103CC3}
Exception in thread "main" java.lang.ExceptionInInitializerError, compiling:(/private/var/folders/67/rgrfj0s939vf1gh0pvdf55ww0000gn/T/form-init5236910218834380630.clj:1:5)
	at clojure.lang.Compiler.load(Compiler.java:7442)
	at clojure.lang.Compiler.loadFile(Compiler.java:7368)
	at clojure.main$load_script.invokeStatic(main.clj:277)
	at clojure.main$init_opt.invokeStatic(main.clj:279)
	at clojure.main$init_opt.invoke(main.clj:279)
	at clojure.main$initialize.invokeStatic(main.clj:310)
	at clojure.main$null_opt.invokeStatic(main.clj:344)
	at clojure.main$null_opt.invoke(main.clj:341)
	at clojure.main$main.invokeStatic(main.clj:423)
	at clojure.main$main.doInvoke(main.clj:386)
	at clojure.lang.RestFn.applyTo(RestFn.java:137)
	at clojure.lang.Var.applyTo(Var.java:700)
	at clojure.main.main(main.java:37)
Caused by: java.lang.ExceptionInInitializerError
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:348)
	at clojure.lang.RT.classForName(RT.java:2183)
	at clojure.lang.RT.classForName(RT.java:2192)
	at clojure.lang.RT.loadClassForName(RT.java:2211)
	at clojure.lang.RT.load(RT.java:445)
	at clojure.lang.RT.load(RT.java:421)
	at clojure.core$load$fn__7846.invoke(core.clj:6008)
	at clojure.core$load.invokeStatic(core.clj:6007)
	at clojure.core$load.doInvoke(core.clj:5991)
	at clojure.lang.RestFn.invoke(RestFn.java:408)
	at clojure.core$load_one.invokeStatic(core.clj:5812)
	at clojure.core$load_one.invoke(core.clj:5807)
	at clojure.core$load_lib$fn__7791.invoke(core.clj:5852)
	at clojure.core$load_lib.invokeStatic(core.clj:5851)
	at clojure.core$load_lib.doInvoke(core.clj:5832)
	at clojure.lang.RestFn.applyTo(RestFn.java:142)
	at clojure.core$apply.invokeStatic(core.clj:659)
	at clojure.core$load_libs.invokeStatic(core.clj:5889)
	at clojure.core$load_libs.doInvoke(core.clj:5873)
	at clojure.lang.RestFn.applyTo(RestFn.java:137)
	at clojure.core$apply.invokeStatic(core.clj:659)
	at clojure.core$require.invokeStatic(core.clj:5911)
	at clojure.core$require.doInvoke(core.clj:5911)
	at clojure.lang.RestFn.invoke(RestFn.java:408)
	at user$eval13$fn__17.invoke(form-init5236910218834380630.clj:1)
	at user$eval13.invokeStatic(form-init5236910218834380630.clj:1)
	at user$eval13.invoke(form-init5236910218834380630.clj:1)
	at clojure.lang.Compiler.eval(Compiler.java:6978)
	at clojure.lang.Compiler.eval(Compiler.java:6968)
	at clojure.lang.Compiler.load(Compiler.java:7430)
	... 12 more
Caused by: clojure.lang.ExceptionInfo: Required env vars: [CLOJUSH_URI] {:missing-vars [CLOJUSH_URI]}, compiling:(ici/clojush.clj:21:1)
	at clojure.lang.Compiler.load(Compiler.java:7442)
	at clojure.lang.RT.loadResourceScript(RT.java:374)
	at clojure.lang.RT.loadResourceScript(RT.java:365)
	at clojure.lang.RT.load(RT.java:455)
	at clojure.lang.RT.load(RT.java:421)
	at clojure.core$load$fn__7846.invoke(core.clj:6008)
	at clojure.core$load.invokeStatic(core.clj:6007)
	at clojure.core$load.doInvoke(core.clj:5991)
	at clojure.lang.RestFn.invoke(RestFn.java:408)
	at clojure.core$load_one.invokeStatic(core.clj:5812)
	at clojure.core$load_one.invoke(core.clj:5807)
	at clojure.core$load_lib$fn__7791.invoke(core.clj:5852)
	at clojure.core$load_lib.invokeStatic(core.clj:5851)
	at clojure.core$load_lib.doInvoke(core.clj:5832)
	at clojure.lang.RestFn.applyTo(RestFn.java:142)
	at clojure.core$apply.invokeStatic(core.clj:659)
	at clojure.core$load_libs.invokeStatic(core.clj:5889)
	at clojure.core$load_libs.doInvoke(core.clj:5873)
	at clojure.lang.RestFn.applyTo(RestFn.java:137)
	at clojure.core$apply.invokeStatic(core.clj:659)
	at clojure.core$require.invokeStatic(core.clj:5911)
	at clojure.core$require.doInvoke(core.clj:5911)
	at clojure.lang.RestFn.invoke(RestFn.java:619)
	at ici.test$loading__7732__auto____86.invoke(test.clj:1)
	at ici.test__init.load(Unknown Source)
	at ici.test__init.<clinit>(Unknown Source)
	... 43 more
Caused by: clojure.lang.ExceptionInfo: Required env vars: [CLOJUSH_URI] {:missing-vars [CLOJUSH_URI]}
	at clojure.core$ex_info.invokeStatic(core.clj:4725)
	at clojure.core$ex_info.invoke(core.clj:4725)
	at ici.clojush$eval76.invokeStatic(clojush.clj:21)
	at ici.clojush$eval76.invoke(clojush.clj:21)
	at clojure.lang.Compiler.eval(Compiler.java:6978)
	at clojure.lang.Compiler.eval(Compiler.java:6968)
	at clojure.lang.Compiler.load(Compiler.java:7430)
	... 68 more

When run with:

env LEIN_FAST_TRAMPOLINE=true CLOJUSH_URI=alluxio://localhost:19998/clojush/ TIMBRE_LEVEL=:trace lein trampoline run

CLOJUSH_URI shows up in env/env but it still says it is missing. Any ideas? I am running Clojure 1.9.0-alpha14, Java 1.8.0_112, lein 2.7.1, on macOs 10.12.3.

Env vars get "captured" at compile time

Hey there,

I'm using env/def to define some env vars with defaults, e.g.

(env/def SERVER_PORT "8080")

I'm running into some trouble using AOT compilation in my boot build task. It seems that once I've AOT'd and uberjar'd my project, these environment definitions get "baked in", if that makes sense. Attempting to set them as follows always results in the default value for the SERVER_PORT var:

SERVER_PORT=9000 java -jar target/my-project.jar

in my logs:

Starting server on port 8080

I'm guessing that I may be misunderstanding the use of env/def, and that this is maybe intended behavior...any help would be much appreciated.

env errors when included via ns' `:require` in cljs 9

adzerk.boot_cljs.util.proxy$clojure.lang.ExceptionInfo$ff19274a: ERROR: No such namespace: adzerk.env, could not locate adzerk/env.cljs, adzerk/env.cljc, or Closure namespace "adzerk.env" in file...
    data: {:tag :cljs/analysis-error,
           :file "file.cljs",
           :from :boot-cljs}
adzerk.boot_cljs.util.proxy$clojure.lang.ExceptionInfo$ff19274a: No such namespace: adzerk.env, could not locate adzerk/env.cljs, adzerk/env.cljc, or Closure namespace "adzerk.env" in file...
    data: {:tag :cljs/analysis-error}
               clojure.core/ex-info/invokeStatic       core.clj: 4617
                            clojure.core/ex-info       core.clj: 4617
                cljs.analyzer$error.invokeStatic  analyzer.cljc:  632
                      cljs.analyzer$error.invoke  analyzer.cljc:  628
                cljs.analyzer$error.invokeStatic  analyzer.cljc:  630
                      cljs.analyzer$error.invoke  analyzer.cljc:  628
         cljs.analyzer$analyze_deps.invokeStatic  analyzer.cljc: 1774
               cljs.analyzer$analyze_deps.invoke  analyzer.cljc: 1753
      cljs.analyzer$ns_side_effects.invokeStatic  analyzer.cljc: 2969
            cljs.analyzer$ns_side_effects.invoke  analyzer.cljc: 2964
     cljs.analyzer$analyze_STAR_$fn__2307.invoke  analyzer.cljc: 3059
jumblerg [1:38 PM]  
can you think of a reason offhand `adzerk/env` would break with cljs 9?  does it depend on some implementation details?

micha [1:39 PM]  
yeah
it needs to know if it's being called as a macro or not
``

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.