gocd / docker-gocd-agent

GoCD Agent Docker image
https://hub.docker.com/r/gocd/
47 stars 37 forks source link

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

Open MPV opened 6 years ago

MPV commented 6 years ago

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"

3. I've created the `/etc/default/go-agent` file, and have it load `/etc/profile`: 
```shell
$ 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
  3. 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 commented 6 years ago

I'd probably try adding scripts in /docker-entrypoint.d and see if that works as expected? See https://github.com/gocd/docker-gocd-agent/blob/2b25324f1b16bf41ebafc607e929a379d6562d45/docker-entrypoint.sh.erb#L113-L124

MPV commented 6 years ago

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:

https://github.com/gocd/docker-gocd-agent/blob/2b25324f1b16bf41ebafc607e929a379d6562d45/docker-entrypoint.sh.erb#L126

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

MPV commented 6 years ago

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 commented 6 years ago

@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 commented 5 years ago

@MPV

Here's how things have been designed: