Simple Deployment Agent

Updated
This post describes use of the project which is a simple deployment agent (deployment-agent). GitHub: dtchanpura/deployment-agent

Introduction

This post is mainly about a project which I carried out to achieve following simple tasks.

To carry out these tasks the project has a listener which listens for a webhook called from a CI server once the updated code is tested and can be deployed. After the webhook, it executes the configured scripts or commands with arguments on the server which can be used to download a file (archive) from cloud and deploy to app server.

Use

The usage is really straight forward, initialize, add configurations, modify it (currently by editing config file) and start listener. For a common use case following are steps.

Get the binary

tar -xvf deployment-agent-*.tar.gz -C path/to/extract
cp path/to/extract/deployment-agent $HOME/bin

Initialize and add a new configuration ➕

Initialize

deployment-agent init

Adding a configuration

Configuration contains following things.

Following is a sample command to run for adding a project configuration.

deployment-agent add --name name \
    --work-dir work/dir/location \
    --hook /script/hook.sh \
    --ip-cidr 192.168.0.0/16 # To allow
    # 192.168.0.0 to 192.168.255.255 IPs

This will return two things which are necessary for the accessing the listener.

UUID for this project is: ece419ae-8ee2-44e3-a0d3-589eae79cd27
Hash to be used for 192.168.0.0/16: Cgcf012PIoTAx9lG93N7qHg_Cg9qYM_g_TMjh690xGDS

Serving 🍽

This part is mainly to start the server which listens for webhook.

It can be configured by changing the host/port in configuration file.

Serve command comes with an option to detect change in project configurations by appending --watch-config.

deployment-agent serve --watch-config

This should start a server listening.

Access

Once the server starts listening, there is a route /reload for running the hooks on remote server.

Following is a sample cURL for the same.

curl -i https://localhost:8080/reload/ece419ae-8ee2-44e3-a0d3-589eae79cd27/Cgcf012PIoTAx9lG93N7qHg_Cg9qYM_g_TMjh690xGDS

One can use following systemd file if needed to serve in background using Systemd.

[Unit]
Description=Deployment Agent Listener
After=network.target

[Service]
Type=simple
WorkingDirectory=/home/$USERNAME
ExecStart=/home/$USERNAME/bin/deployment-agent serve --watch-config
Restart=on-abort

[Install]
WantedBy=multi-user.target

Replace the text $USERNAME with your username and copy it to user folder of systemd configurations. For my Debian system it works in ~/.config/systemd/user/ you can also use /etc/systemd/user/. Then do reload systemd configurations and start server followed by checking status.

systemctl --user daemon-reload
systemctl --user start deployment-agent.service
systemctl --user status deployment-agent.service

Future work