Skip to content
This repository has been archived by the owner on Apr 26, 2019. It is now read-only.

How do I set environment variables to agent (like /etc/default/go-agent) #74

Open
MPV opened this issue Sep 6, 2018 · 5 comments
Open

Comments

@MPV
Copy link

MPV commented Sep 6, 2018

What I'd like to do:
Run scripts that set environment variables, which become available to my agents.

Steps I've done:

  1. I've created a Docker image based on this image (gocd/gocd-agent-docker-dind specifically).
  2. I've added some scripts I'd like to run in /etc/profile.d/, for example this one:
$ cat /etc/profile.d/add-python-scripts-to-path.sh
#!/bin/false

export PATH="/home/go/.local/bin:$PATH"
  1. I've created the /etc/default/go-agent file, and have it load /etc/profile:
$ cat /etc/default/go-agent
. /etc/profile
  1. ...and /etc/profile in turn should load the files in /etc/profile.d:
$ tail -n5 /etc/profile
for script in /etc/profile.d/*.sh ; do
        if [ -r $script ] ; then
                . $script
        fi
done

...however when I run jobs using this image, my scripts haven't been run, and the environment variables aren't set.

Questions:

  1. Am I configuring the right file? (/etc/default/go-agent)
  2. Am I running without service_mode? I suppose I am (?) — and then my guess (judging from /go-agent/agent.sh) is that maybe my file won't be loaded:
$ grep "/etc/default" -B 1 -A 5 /go-agent/agent.sh
if [ "$2" == "service_mode" ]; then
  if [ -f /etc/default/${SERVICE_NAME} ]; then
    . /etc/default/${SERVICE_NAME}
  fi

  # no point in not daemonizing the service
  DAEMON=Y
fi
  1. If that's the case, is there another way I should be telling the gocd agent to run my files for setting environment variables etc?
@ketan
Copy link
Member

ketan commented Sep 6, 2018

I'd probably try adding scripts in /docker-entrypoint.d and see if that works as expected? See

yell "Running custom scripts in /docker-entrypoint.d/ ..."
# to prevent expansion to literal string `/docker-entrypoint.d/*` when there is nothing matching the glob
shopt -s nullglob
for file in /docker-entrypoint.d/*; do
if [ -f "$file" ] && [ -x "$file" ]; then
try "$file"
else
yell "Ignoring $file, it is either not a file or is not executable"
fi
done

@MPV
Copy link
Author

MPV commented Sep 6, 2018

Oh maybe I've interpreted it wrong... I thought /docker-entrypoint.d was only run as root?

I want my scripts to be run as the go user (to be set for pipeline jobs).

I thought this line made it run /goagent/agent.sh as the go user, using gosu:

try exec /usr/local/sbin/tini -- /usr/local/sbin/gosu go "$0" "$@"

...but maybe it's running itself again and thus loading /docker-entrypoint.d/ files, also as go?

@MPV
Copy link
Author

MPV commented Sep 6, 2018

This is what led me to configure the /etc/default/go-agent file from before:

Users can override default startup arguments for a GoCD agent in a Linux machine by editing the file etc/default/go-agent.

https://docs.gocd.org/current/installation/install/agent/linux.html#overriding-default-startup-arguments-and-environment

@MPV
Copy link
Author

MPV commented Sep 6, 2018

@ketan Looking at the logs of my container, I see this happening during startup:

/docker-entrypoint.sh: Creating directories and symlinks to hold GoCD configuration, data, and logs
[...]
$ chown go:go /go/config/autoregister.properties
/docker-entrypoint.sh: Running custom scripts in /docker-entrypoint.d/ ...
$ exec /usr/local/sbin/tini -- /usr/local/sbin/gosu go /docker-entrypoint.sh /go-agent/agent.sh
/docker-entrypoint.sh: line 57: /usr/local/bin/nohup.out: Permission denied
$ exec /go-agent/agent.sh
[Thu Sep 06 11:18:35 GMT 2018] Starting process:
[...]

...thus it doesn't seem like it runs those files in /docker-entrypoint.d as go, but only as root?

Any suggestions of what I could try?

@ketan
Copy link
Member

ketan commented Dec 3, 2018

@MPV

Here's how things have been designed:

  • Stuff in /docker-entrypoint.d executes as root user, intentionally so. Use this to provision, install packages etc.
  • Stuff in /etc/default/go-agent is sourced from the agent startup script (/goagent/agent.sh). Use this to ensure that variables are available to the agent process (which runs as the go user). This file can be volume mounted (or baked into the image, as the case may be).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants