Skip to content

Commit

Permalink
Use JSON import for environment variables
Browse files Browse the repository at this point in the history
The env can be provided as a JSON list `[{"name": "PORT", "value": "8080"}]`.
The `dict2items` filter provided by ansible is _almost_ what we want, but it
keeps the value original types (a boolean is kept as a boolean in the JSON value).
Since environment variables are strings and `clever-tools` does not want to make
the implicit coercion for us, we need to do it ourselves.
  • Loading branch information
clementd-fretlink committed Jun 19, 2020
1 parent d5c5bf8 commit 244339e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
8 changes: 8 additions & 0 deletions filter_plugins/env_json_map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env python

class FilterModule(object):
def filters(self):
return {'json_env_map': self.json_env_map}

def json_env_map(self, env):
return [{'name': k, 'value': str(v)} for k,v in env.items()]
2 changes: 1 addition & 1 deletion tasks/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
CONFIGURATION_FILE: "{{ clever_login_file }}"

- name: Push Environment
shell: "clever env import < {{ clever_app_confdir }}/env"
shell: "clever env import --json < {{ clever_app_confdir }}/env"
args:
chdir: "{{ clever_app_root }}"
environment:
Expand Down
13 changes: 7 additions & 6 deletions templates/env.j2
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{% for key, value in (clever_base_env | combine(clever_env)).items() %}
{{ key }}={{ value | to_json }}
{% endfor %}

{%- if clever_haskell_entry_point %}
{% set static_env = clever_base_env | combine(clever_env) %}
{% set dynamic_env = {} %}
{% if clever_haskell_entry_point %}
{# Haskell only #}
{# https://www.clever-cloud.com/doc/get-help/reference-environment-variables/#haskell #}
CC_RUN_COMMAND={{'~/.local/bin/' + clever_haskell_entry_point | to_json }}
{% set dynamic_env = { 'CC_RUN_COMMAND': '~/.local/bin/' + clever_haskell_entry_point } %}
{% endif %}
{# dict2items is not enough here, all the values have to be stringified #}
{# git-blame this line for more explanations #}
{{ static_env | combine(dynamic_env) | json_env_map | to_json }}

0 comments on commit 244339e

Please sign in to comment.