Skip to content

Instantly share code, notes, and snippets.

@SiddharthBharadwaj
Last active October 18, 2024 19:43
Show Gist options
  • Save SiddharthBharadwaj/d18ed034d9fe36bef26cb1f209bacae1 to your computer and use it in GitHub Desktop.
Save SiddharthBharadwaj/d18ed034d9fe36bef26cb1f209bacae1 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
OS=$(awk -F= '/^ID=/{print tolower($2)}' /etc/os-release | tr -d '"')
echo "Detected OS: $OS"
# TRICKEST_DATA_DIR - do not change this one, it it still not configurable, coming soon
TRICKEST_DATA_DIR="/data"
# TRICKEST_JOB_LOGS_PATH - do not change this one, it it still not configurable, coming soon
TRICKEST_JOB_LOGS_PATH="${TRICKEST_DATA_DIR}/storage/container"
TRICKEST_RSYSLOG_CONF_PATH="/etc/rsyslog.d/99-trickest.conf"
detect_agent_service() {
AGENT_SERVICE_STATUS=$(
systemctl status trickest-agent >/dev/null 2>&1
echo $?
)
case "$AGENT_SERVICE_STATUS" in
4)
echo "Service not detected, proceeding..."
;;
3)
echo "Service already stopped, proceeding..."
;;
0)
echo "Service already running..."
choice="y"
case "$choice" in
[yY][eE][sS] | [yY])
echo "Stopping service..."
systemctl stop trickest-agent
;;
*)
exit "Aborting initialization"
;;
esac
;;
*)
exit "Service is in non running, nor stopped state, assuming not working..."
;;
esac
}
check_docker() {
if command -v docker 2>&1 >/dev/null; then
echo "Docker found..."
else
echo "Docker not found..."
choice="y"
case "$choice" in
[yY][eE][sS] | [yY])
install_docker
;;
*)
echo "Refer to https://docs.docker.com/engine/install/ for installation instructions."
exit "Aborting initialization"
;;
esac
fi
}
install_docker() {
curl -fsSL https://get.docker.com -o get-docker.sh
sh ./get-docker.sh
rm ./get-docker.sh
}
check_rsyslog() {
if command -v rsyslogd 2>&1 >/dev/null; then
echo "rsyslog found..."
else
echo "rsyslog not found..."
choice="y"
case "$choice" in
[yY][eE][sS] | [yY])
install_rsyslog
;;
*)
echo "Refer to https://www.rsyslog.com for installation instructions."
exit "Aborting initialization"
;;
esac
fi
}
install_rsyslog() {
case "$OS" in
ubuntu)
-qq
echo "Adding rsyslog repository..."
sudo add-apt-repository -y ppa:adiscon/v8-stable >/dev/null
echo "Updating apt-get..."
sudo apt-get -qq update
echo "Installing rsyslog on Ubuntu..."
sudo apt-get -qq install -y rsyslog
;;
fedora)
echo "Installing rsyslog on Fedora..."
dnf install -y rsyslog >/dev/null
echo "rsyslog installed successfully."
;;
centos)
echo "Installing rsyslog on CentOS..."
yum install -y rsyslog >/dev/null
echo "rsyslog installed successfully."
;;
*)
echo "Unsupported OS. rsyslog installation not supported."
exit 1
;;
esac
echo "rsyslog is installed..."
}
configure_rsyslog() {
echo "Configuring rsyslog for Trickest agent..."
echo "The rsyslog configuration file will be created at: ${TRICKEST_RSYSLOG_CONF_PATH}"
confirm="y"
if [[ $confirm =~ ^[Yy]$ ]]; then
if ! cat <<EOF >"${TRICKEST_RSYSLOG_CONF_PATH}"; then
$EscapeControlCharactersOnReceive off
$template DockerFileName,"/data/storage/container/%syslogtag:R,ERE,1,FIELD:twe/(.*)\[--end%-%syslogpriority%.log"
$template DockerLog,"%msg:2:$%\n"
:syslogtag,startswith,"twe/" ?DockerFileName;DockerLog
& ~
EOF
echo "Failed to configure rsyslog."
exit 1
else
echo "rsyslog configured successfully. Configuration file created at: ${TRICKEST_RSYSLOG_CONF_PATH}"
fi
else
echo "rsyslog configuration cancelled."
exit 1
fi
}
ensure_data_dir_structure() {
if [ -d ${TRICKEST_DATA_DIR} ]; then
echo "Directory intended for trickest data usage already exists at ${TRICKEST_DATA_DIR}"
choice="y"
case "$choice" in
[yY][eE][sS] | [yY])
echo "Proceeding..."
clear_cache
;;
*)
exit "Aborting initialization"
;;
esac
else
echo "Creating directory structure intended for Trickest data usage at ${TRICKEST_DATA_DIR}"
create_data_directory
fi
}
create_data_directory() {
if ! mkdir -p ${TRICKEST_JOB_LOGS_PATH}; then
echo "Failed to create data directory structure."
exit 1
fi
}
clear_cache() {
echo "Clearing cache..."
if [ -f "${TRICKEST_DATA_DIR}/agent.crt" ]; then
rm -f "${TRICKEST_DATA_DIR}/agent.crt"
fi
if [ -f "${TRICKEST_DATA_DIR}/agent.key" ]; then
rm -f "${TRICKEST_DATA_DIR}/agent.key"
fi
if [ -f "${TRICKEST_DATA_DIR}/ca.crt" ]; then
rm -f "${TRICKEST_DATA_DIR}/ca.crt"
fi
}
download_trickest_agent() {
echo "Downloading latest Trickest agent..."
agent_url="https://trickest-agent-binary.s3.eu-central-1.amazonaws.com/latest/linux/amd64/twe-agent"
agent_path="${TRICKEST_DATA_DIR}/trickest-agent"
if ! curl -s -o "$agent_path" "$agent_url"; then
echo "Failed to download Trickest agent."
exit 1
fi
chmod +x "$agent_path"
}
ensure_auth_env_variables() {
if [[ -z "$TRICKEST_CLIENT_AUTH_ID" ]]; then
echo "TRICKEST_CLIENT_AUTH_ID is not set. Go to https://trickest.io/dashboard/settings/fleet and click on "Add Machine" to generate new TRICKEST_CLIENT_AUTH_ID for your machine"
exit 1
fi
if [[ -z "$TRICKEST_CLIENT_AUTH_SECRET" ]]; then
echo "TRICKEST_CLIENT_AUTH_SECRET is not set. Go to https://trickest.io/dashboard/settings/fleet and click on "Add Machine"to generate new TRICKEST_CLIENT_AUTH_SECRET for your machine"
exit 1
fi
}
create_systemd_service() {
service_file="/etc/systemd/system/trickest-agent.service"
log_file="/data/trickest-agent.log"
if ! touch "$service_file"; then
echo "Failed to create systemd service file."
exit 1
fi
if ! touch "$log_file"; then
echo "Failed to create log file."
exit 1
fi
cat <<EOF >"$service_file"
[Unit]
Description=Trickest Workflow Engine - Agent
After=network-online.target
Wants=docker.service
StartLimitBurst=25
StartLimitIntervalSec=100
[Service]
User=root
Type=simple
ExecStart=/bin/bash -c '${TRICKEST_DATA_DIR}/trickest-agent -c ${TRICKEST_DATA_DIR}/conf.yaml run'
Restart=always
RestartSec=15
[Install]
WantedBy=multi-user.target
EOF
echo "Systemd service file created at: $service_file"
echo "Reloading systemd daemon..."
systemctl daemon-reload
}
generate_config_file() {
echo "Generating config file..."
ensure_auth_env_variables
config_file="${TRICKEST_DATA_DIR}/conf.yaml"
if ! touch "$config_file"; then
echo "Failed to create config file."
exit 1
fi
cat <<EOF >"$config_file"
log:
file: ${TRICKEST_DATA_DIR}/trickest-agent.log
node:
endpoint: "https://api.trickest.io/node"
client:
auth:
id: "${TRICKEST_CLIENT_AUTH_ID}"
secret: "${TRICKEST_CLIENT_AUTH_SECRET}"
endpoint: "https://api.trickest.io/oauth2/token"
EOF
}
start_and_verify_service() {
echo "Starting Trickest agent service..."
systemctl daemon-reload
set -e
systemctl start trickest-agent.service
set +e
sleep 5
let try=0
echo "Waiting for Trickest agent service..."
while true; do
if [ $(systemctl show -p SubState --value trickest-agent.service) == "running" ]; then
echo "Trickest agent service started successfully."
break
fi
let try+=1
if [ $try -gt 15 ]; then
echo "Trickest agent service failed to start. Please start it manually using the following command:"
echo "/data/trickest-agent -c /data/conf.yaml run"
echo "Please send the output of the manual start command to [email protected] for further assistance."
exit
fi
sleep 2
done
}
detect_agent_service
check_docker
check_rsyslog
configure_rsyslog
ensure_data_dir_structure
download_trickest_agent
create_systemd_service
generate_config_file
start_and_verify_service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment