Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Using Operating System Environment Variables for properties

Geert edited this page Jun 8, 2017 · 1 revision

In some cases you might want to use Operating System environment variables for your configurations, or otherwise override properties from your command-line.

Take for example a username and password. Rather than placing your super secret password in the .properties file you want to set it in the OS environment. There are three approaches you can use:

  1. Built-in command-line overrides for properties
  2. Built-in ENV overrides for properties
  3. Custom ENV variable handling

Built-in command-line overrides for properties

Roxy provides a very generic and flexible way to override any known property using --ml.{propertyname} arguments. You can put them anywhere among the arguments. You can use it for instance like:

$ ./ml --ml.user=xxx local --ml.password=yyy info --ml.rest-ext.dir=zzz

You can see the full list of properties that come out of the box using ml env info, but you can define ones of your own too. Just put a new name in build.properties, or one of your env specific ones, and start overriding it.

Built-in ENV overrides for properties

Roxy also provides a generic way to override any known property using environment variables. Environment variables don't allow using characters like . and - (period and dash). No worries, just prefix the property name with ml_ and replace any non-letter/digit/underscore with an underscore, and use that to refer to the property. You can do for instance something like:

$ ml_rest_ext_dir=zzz ./ml local info

Custom ENV variable handling

In case you need more complex handling of ENV variables or more complex logic to initialize your properties, you can use the deploy/app_specific.rb file. Below some examples. Note that it makes use of the principle to Decorate built-in methods:

  # this line creates a "backup" of the original initialize method
  alias_method :original_init, :initialize

  # this method replaces, and effectively decorates the original initialize method
  def initialize(options)
    
    properties = options[:properties]

    # now you can add or set any property you like by accessing it from the OS environment
    # note how I put the if at the end. That means it only sets the property if the env variable is legit.
    properties['ml.server'] = ENV['my_server'] if ENV['my_server']

    # you can even get tricky and use the current roxy environment (local, dev, prod)
    environment = properties["environment"]
    if environment == "local"
      properties['ml.important-password'] = ENV['local_password']
    elsif environment == "prod"
      properties['ml.important-password'] = ENV['prod_password']
    end

    # although I would probably write the above like so instead:
    environment = properties["environment"]
    properties['ml.important-password'] = ENV["#{environment}_password"]

    # it's super important to call the original init last
    original_init(options)
  end

Now when you run any Roxy command the properties you set from the Environment variables are instantly available and set.

To test this out use the info command:

$ ./ml local info