Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom stack environment variables #7

Closed
greenled opened this issue Nov 29, 2018 · 5 comments · Fixed by #8
Closed

Custom stack environment variables #7

greenled opened this issue Nov 29, 2018 · 5 comments · Fixed by #8
Labels
enhancement New feature or request

Comments

@greenled
Copy link
Owner

The script only supports keeping stack envvars between stack updates, not changing them. Even worse, new deployments go without envvars. It would be desirable to allow the user to set ennvars at will.

They could be taken from a file like:

MYSQL_ROOT_PASSWORD=password
ALLOWED_HOSTS=*

Envvars set through the script should overwrite existing ones with the same name, and keep the others.

The Portainer API already has a Env field for this purpose:

[
  {
    "name": "MYSQL_ROOT_PASSWORD",
    "value": "password"
  }
]
@greenled greenled added the enhancement New feature or request label Dec 29, 2018
@tortuetorche
Copy link
Collaborator

Hi @greenled,

Any news on this issue?
I'll be very useful.

Have a good day,
Tortue Torche

@greenled
Copy link
Owner Author

Hi @tortuetorche,

I'm afraid I can not work on this in an immediate future. Too much work here and some health issues :(
Feel free to contribute a solution if you want to 💪 Have a good day too.

@tortuetorche
Copy link
Collaborator

tortuetorche commented Apr 26, 2019

Hi @greenled,

Here a starting point:

Create a .env file, which contains the Docker Stack environment variables:

HOSTNAME=bfaecdeb0cd2
HOME=/root
_BASH_VERSION=5.0
_BASH_PATCH_LEVEL=0
_BASH_LATEST_PATCH=7
TERM=xterm
foo=d=cc

Then execute this jq command:

env -i $(cat .env) jq -n 'env | to_entries | map({name: .key, value: .value})'

This will output:

[
  {
    "name": "HOSTNAME",
    "value": "bfaecdeb0cd2"
  },
  {
    "name": "HOME",
    "value": "/root"
  },
  {
    "name": "_BASH_VERSION",
    "value": "5.0"
  },
  {
    "name": "_BASH_PATCH_LEVEL",
    "value": "0"
  },
  {
    "name": "_BASH_LATEST_PATCH",
    "value": "7"
  },
  {
    "name": "TERM",
    "value": "xterm"
  },
  {
    "name": "foo",
    "value": "d=cc"
  }
]

I hope you'll be better soon,
Tortue Torche

@greenled
Copy link
Owner Author

Thanks @tortuetorche, this is a very good starting point.

Stack envvars are set in the deploy() function. When a new stack is deployed it gets no envvars, and when an existing one is updated its envvars are reused (extracted from its stack definition into the stack_envvars variable and set back again). For the first case it would be just a matter of loading the content of the envvars file and transform it into JSON using the command you provided, and set it as the stack_envvars value. For the second case, though, the script should update the current stack envvars rather than setting them from scratch, keeping any value not previously set in the .env file.

I would also make the environment variables file path customizable with a flag, like -g (could be any of the remaining bfghijkmoqwxyz), and a script configuration envvar, like ENVIRONMENT_VARIABLES_FILE.

@greenled greenled pinned this issue Apr 28, 2019
@tortuetorche
Copy link
Collaborator

Hi @greenled ,

Thank you for your explanations.

Here some snippets to merge two environment variable JSON files with jq:

jq -s 'add | unique_by(.name)' env_new.json env_old.json

Or:

jq -n '[inputs] | add | unique_by(.name)' env_new.json env_old.json

Have a good day,
Tortue Torche

tortuetorche added a commit to tortuetorche/portainer-stack-utils that referenced this issue May 10, 2019
Prior to this pull request `psu` script act like this:
Stack env vars are set in the `deploy()` function. When a new stack is deployed it gets no env vars, and when an existing one is updated its envvars are reused (extracted from its stack definition into the stack_envvars variable and set back again).

For the first case this pull request load the content of the env vars file and transform it into JSON using a `jq` command and set it as the `stack_envvars` value.
For the second case, though, the script update the current stack env vars rather than setting them from scratch, keeping any value not previously set in the env file.

The environment variables file path is customizable with the environment variable `$ENVIRONMENT_VARIABLES_FILE` or the `-g` flag, like this:
```bash
export ACTION="deploy"
export PORTAINER_USER="admin"
export PORTAINER_PASSWORD="password"
export PORTAINER_URL="http://portainer.local"
export PORTAINER_STACK_NAME="mystack"
export DOCKER_COMPOSE_FILE="/path/to/docker-compose.yml"
export ENVIRONMENT_VARIABLES_FILE="/path/to/env_vars_file"

./psu
```

Or with flags:
```bash
./psu -a deploy -u admin -p password -l http://portainer.local -n mystack -c /path/to/docker-compose.yml -g /path/to/env_vars_file
```

close greenled#7
@greenled greenled unpinned this issue May 24, 2019
tortuetorche added a commit to tortuetorche/portainer-stack-utils that referenced this issue Jul 10, 2019
Prior to this pull request `psu` script act like this:
Stack env vars are set in the `deploy()` function. When a new stack is deployed it gets no env vars, and when an existing one is updated its envvars are reused (extracted from its stack definition into the stack_envvars variable and set back again).

For the first case this pull request load the content of the env vars file and transform it into JSON using a `jq` command and set it as the `stack_envvars` value.
For the second case, though, the script update the current stack env vars rather than setting them from scratch, keeping any value not previously set in the env file.

The environment variables file path is customizable with the environment variable `$ENVIRONMENT_VARIABLES_FILE` or the `-g` flag, like this:
```bash
export ACTION="deploy"
export PORTAINER_USER="admin"
export PORTAINER_PASSWORD="password"
export PORTAINER_URL="http://portainer.local"
export PORTAINER_STACK_NAME="mystack"
export DOCKER_COMPOSE_FILE="/path/to/docker-compose.yml"
export ENVIRONMENT_VARIABLES_FILE="/path/to/env_vars_file"

./psu
```

Or with flags:
```bash
./psu -a deploy -u admin -p password -l http://portainer.local -n mystack -c /path/to/docker-compose.yml -g /path/to/env_vars_file
```

close greenled#7
tortuetorche added a commit to tortuetorche/portainer-stack-utils that referenced this issue Nov 7, 2019
Prior to this pull request `psu` script act like this:
Stack env vars are set in the `deploy()` function. When a new stack is deployed it gets no env vars, and when an existing one is updated its envvars are reused (extracted from its stack definition into the stack_envvars variable and set back again).

For the first case this pull request load the content of the env vars file and transform it into JSON using a `jq` command and set it as the `stack_envvars` value.
For the second case, though, the script update the current stack env vars rather than setting them from scratch, keeping any value not previously set in the env file.

The environment variables file path is customizable with the environment variable `$ENVIRONMENT_VARIABLES_FILE` or the `-g` flag, like this:
```bash
export ACTION="deploy"
export PORTAINER_USER="admin"
export PORTAINER_PASSWORD="password"
export PORTAINER_URL="http://portainer.local"
export PORTAINER_STACK_NAME="mystack"
export DOCKER_COMPOSE_FILE="/path/to/docker-compose.yml"
export ENVIRONMENT_VARIABLES_FILE="/path/to/env_vars_file"

./psu
```

Or with flags:
```bash
./psu -a deploy -u admin -p password -l http://portainer.local -n mystack -c /path/to/docker-compose.yml -g /path/to/env_vars_file
```

close greenled#7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants