Skip to content

Instantly share code, notes, and snippets.

@joshualouden
Forked from SiddharthBharadwaj/trickest-agent.sh
Created October 18, 2024 19:43
Show Gist options
  • Select an option

  • Save joshualouden/6a44a92cbe1f8119dba68caf30335d1f to your computer and use it in GitHub Desktop.

Select an option

Save joshualouden/6a44a92cbe1f8119dba68caf30335d1f to your computer and use it in GitHub Desktop.

Revisions

  1. @SiddharthBharadwaj SiddharthBharadwaj revised this gist Dec 9, 2023. 1 changed file with 12 additions and 39 deletions.
    51 changes: 12 additions & 39 deletions trickest-agent.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    #!/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
    @@ -10,7 +12,7 @@ TRICKEST_RSYSLOG_CONF_PATH="/etc/rsyslog.d/99-trickest.conf"

    detect_agent_service() {
    AGENT_SERVICE_STATUS=$(
    sudo systemctl status trickest-agent >/dev/null 2>&1
    systemctl status trickest-agent >/dev/null 2>&1
    echo $?
    )
    case "$AGENT_SERVICE_STATUS" in
    @@ -26,7 +28,7 @@ detect_agent_service() {
    case "$choice" in
    [yY][eE][sS] | [yY])
    echo "Stopping service..."
    sudo systemctl stop trickest-agent
    systemctl stop trickest-agent
    ;;
    *)
    exit "Aborting initialization"
    @@ -64,7 +66,7 @@ install_docker() {
    }

    check_rsyslog() {
    if sudo bash -c 'command -v rsyslogd 2>&1 >/dev/null'; then
    if command -v rsyslogd 2>&1 >/dev/null; then
    echo "rsyslog found..."
    else
    echo "rsyslog not found..."
    @@ -84,6 +86,7 @@ check_rsyslog() {
    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..."
    @@ -116,19 +119,15 @@ configure_rsyslog() {
    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"
    $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
    sudo systemctl restart rsyslog
    echo "rsyslog configured successfully. Configuration file created at: ${TRICKEST_RSYSLOG_CONF_PATH}"
    fi
    else
    @@ -137,31 +136,6 @@ EOF
    fi
    }

    test_rsyslog() {
    if command -v logger >/dev/null 2>&1
    then
    echo "Testing rsyslog implementation..."
    msg="testing Trickest rsyslog implementation"
    logger -p daemon.info -t 'twe/init-test[99]' "${msg}" && sleep 2
    if [[ $(grep "${msg}" "${TRICKEST_JOB_LOGS_PATH}/init-test-6.log" 2>&1 >/dev/null; echo $?) != 0 ]]; then
    echo "rsyslog implementation test failed at stdout..."
    echo "Please write to us at Discord for support: https://trickest.com/community/"
    exit 1
    fi
    rm "${TRICKEST_JOB_LOGS_PATH}/init-test-6.log"

    logger -p daemon.err -t 'twe/init-test[99]' "${msg}" && sleep 2
    if [[ $(grep "${msg}" "${TRICKEST_JOB_LOGS_PATH}/init-test-3.log" 2>&1 >/dev/null; echo $?) != 0 ]]; then
    echo "rsyslog implementation test failed at stderr..."
    echo "Please write to us at Discord for support: https://trickest.com/community/"
    exit 1
    fi
    rm "${TRICKEST_JOB_LOGS_PATH}/init-test-3.log"
    else
    echo "Cloud not find logger command, cannot test rsyslog implementation, skipping ..."
    fi
    }

    ensure_data_dir_structure() {
    if [ -d ${TRICKEST_DATA_DIR} ]; then
    echo "Directory intended for trickest data usage already exists at ${TRICKEST_DATA_DIR}"
    @@ -258,7 +232,7 @@ WantedBy=multi-user.target
    EOF
    echo "Systemd service file created at: $service_file"
    echo "Reloading systemd daemon..."
    sudo systemctl daemon-reload
    systemctl daemon-reload
    }

    generate_config_file() {
    @@ -286,9 +260,9 @@ EOF

    start_and_verify_service() {
    echo "Starting Trickest agent service..."
    sudo systemctl daemon-reload
    systemctl daemon-reload
    set -e
    sudo systemctl start trickest-agent.service
    systemctl start trickest-agent.service
    set +e
    sleep 5
    let try=0
    @@ -313,7 +287,6 @@ detect_agent_service
    check_docker
    check_rsyslog
    configure_rsyslog
    test_rsyslog
    ensure_data_dir_structure
    download_trickest_agent
    create_systemd_service
  2. @SiddharthBharadwaj SiddharthBharadwaj revised this gist Dec 9, 2023. 1 changed file with 44 additions and 17 deletions.
    61 changes: 44 additions & 17 deletions trickest-agent.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    #!/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
    @@ -12,7 +10,7 @@ TRICKEST_RSYSLOG_CONF_PATH="/etc/rsyslog.d/99-trickest.conf"

    detect_agent_service() {
    AGENT_SERVICE_STATUS=$(
    systemctl status trickest-agent >/dev/null 2>&1
    sudo systemctl status trickest-agent >/dev/null 2>&1
    echo $?
    )
    case "$AGENT_SERVICE_STATUS" in
    @@ -24,11 +22,11 @@ detect_agent_service() {
    ;;
    0)
    echo "Service already running..."
    read -r -p "Stop the service now, and proceed with update? [y/N] : " choice
    choice="y"
    case "$choice" in
    [yY][eE][sS] | [yY])
    echo "Stopping service..."
    systemctl stop trickest-agent
    sudo systemctl stop trickest-agent
    ;;
    *)
    exit "Aborting initialization"
    @@ -46,7 +44,7 @@ check_docker() {
    echo "Docker found..."
    else
    echo "Docker not found..."
    read -r -p "Do you wish to attempt automatic installation of the Docker? (needs sudo privilege!) [y/N] : " choice
    choice="y"
    case "$choice" in
    [yY][eE][sS] | [yY])
    install_docker
    @@ -66,11 +64,11 @@ install_docker() {
    }

    check_rsyslog() {
    if command -v rsyslogd 2>&1 >/dev/null; then
    if sudo bash -c 'command -v rsyslogd 2>&1 >/dev/null'; then
    echo "rsyslog found..."
    else
    echo "rsyslog not found..."
    read -r -p "Do you wish to attempt automatic installation of the rsyslog? (needs sudo privilege!) [y/N] : " choice
    choice="y"
    case "$choice" in
    [yY][eE][sS] | [yY])
    install_rsyslog
    @@ -86,7 +84,6 @@ check_rsyslog() {
    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..."
    @@ -116,18 +113,22 @@ configure_rsyslog() {
    echo "Configuring rsyslog for Trickest agent..."

    echo "The rsyslog configuration file will be created at: ${TRICKEST_RSYSLOG_CONF_PATH}"
    read -r -p "Please confirm to proceed with rsyslog configuration (y/n): " confirm
    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"
    \$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
    sudo systemctl restart rsyslog
    echo "rsyslog configured successfully. Configuration file created at: ${TRICKEST_RSYSLOG_CONF_PATH}"
    fi
    else
    @@ -136,10 +137,35 @@ EOF
    fi
    }

    test_rsyslog() {
    if command -v logger >/dev/null 2>&1
    then
    echo "Testing rsyslog implementation..."
    msg="testing Trickest rsyslog implementation"
    logger -p daemon.info -t 'twe/init-test[99]' "${msg}" && sleep 2
    if [[ $(grep "${msg}" "${TRICKEST_JOB_LOGS_PATH}/init-test-6.log" 2>&1 >/dev/null; echo $?) != 0 ]]; then
    echo "rsyslog implementation test failed at stdout..."
    echo "Please write to us at Discord for support: https://trickest.com/community/"
    exit 1
    fi
    rm "${TRICKEST_JOB_LOGS_PATH}/init-test-6.log"

    logger -p daemon.err -t 'twe/init-test[99]' "${msg}" && sleep 2
    if [[ $(grep "${msg}" "${TRICKEST_JOB_LOGS_PATH}/init-test-3.log" 2>&1 >/dev/null; echo $?) != 0 ]]; then
    echo "rsyslog implementation test failed at stderr..."
    echo "Please write to us at Discord for support: https://trickest.com/community/"
    exit 1
    fi
    rm "${TRICKEST_JOB_LOGS_PATH}/init-test-3.log"
    else
    echo "Cloud not find logger command, cannot test rsyslog implementation, skipping ..."
    fi
    }

    ensure_data_dir_structure() {
    if [ -d ${TRICKEST_DATA_DIR} ]; then
    echo "Directory intended for trickest data usage already exists at ${TRICKEST_DATA_DIR}"
    read -r -p "Do you wish to proceed? [y/N] : " choice
    choice="y"
    case "$choice" in
    [yY][eE][sS] | [yY])
    echo "Proceeding..."
    @@ -232,7 +258,7 @@ WantedBy=multi-user.target
    EOF
    echo "Systemd service file created at: $service_file"
    echo "Reloading systemd daemon..."
    systemctl daemon-reload
    sudo systemctl daemon-reload
    }

    generate_config_file() {
    @@ -260,9 +286,9 @@ EOF

    start_and_verify_service() {
    echo "Starting Trickest agent service..."
    systemctl daemon-reload
    sudo systemctl daemon-reload
    set -e
    systemctl start trickest-agent.service
    sudo systemctl start trickest-agent.service
    set +e
    sleep 5
    let try=0
    @@ -287,6 +313,7 @@ detect_agent_service
    check_docker
    check_rsyslog
    configure_rsyslog
    test_rsyslog
    ensure_data_dir_structure
    download_trickest_agent
    create_systemd_service
  3. @SiddharthBharadwaj SiddharthBharadwaj created this gist Dec 7, 2023.
    294 changes: 294 additions & 0 deletions trickest-agent.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,294 @@
    #!/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..."
    read -r -p "Stop the service now, and proceed with update? [y/N] : " choice
    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..."
    read -r -p "Do you wish to attempt automatic installation of the Docker? (needs sudo privilege!) [y/N] : " choice
    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..."
    read -r -p "Do you wish to attempt automatic installation of the rsyslog? (needs sudo privilege!) [y/N] : " choice
    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}"
    read -r -p "Please confirm to proceed with rsyslog configuration (y/n): " confirm
    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}"
    read -r -p "Do you wish to proceed? [y/N] : " choice
    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