|
| 1 | +#!/bin/env bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# Colores |
| 5 | +RED='\033[0;31m' |
| 6 | +GREEN='\033[0;32m' |
| 7 | +YELLOW='\033[1;33m' |
| 8 | +NC='\033[0m' # No Color |
| 9 | + |
| 10 | +REPO="RustLangES/grhooks" |
| 11 | +VERSION=${GRHOOKS_VERSION:-"latest"} |
| 12 | +INSTALL_DIR=${GRHOOKS_INSTALL_DIR:-"/usr/local/bin"} |
| 13 | +CONFIG_DIR=${GRHOOKS_CONFIG_DIR:-"/etc/grhooks"} |
| 14 | +SERVICE_NAME=${GRHOOKS_SERVICE_NAME:-"grhooks"} |
| 15 | +LOG_LEVEL=${GRHOOKS_LOG_LEVEL:-"info"} |
| 16 | + |
| 17 | +if [[ "$(uname)" == "Darwin" ]]; then |
| 18 | + echo -e "${RED}Error: This script is just for linux systems${NC}" |
| 19 | + exit 1 |
| 20 | +fi |
| 21 | + |
| 22 | +echo -e "${YELLOW}Install Configuration:${NC}" |
| 23 | +echo -e "Version: ${GREEN}${VERSION}${NC}" |
| 24 | +echo -e "Install Dir: ${GREEN}${INSTALL_DIR}${NC}" |
| 25 | +echo -e "Configuration: ${GREEN}${CONFIG_DIR}${NC}" |
| 26 | +echo -e "Service: ${GREEN}${SERVICE_NAME}${NC}" |
| 27 | +echo -e "Log Level: ${GREEN}${LOG_LEVEL}${NC}" |
| 28 | +echo "" |
| 29 | + |
| 30 | +ARCH=$(uname -m) |
| 31 | +case $ARCH in |
| 32 | + x86_64) ARCH="x86_64" ;; |
| 33 | + aarch64) ARCH="arm64" ;; |
| 34 | + *) echo -e "${RED}Arquitectura no soportada: $ARCH${NC}"; exit 1 ;; |
| 35 | +esac |
| 36 | + |
| 37 | +if [ -f /etc/debian_version ]; then |
| 38 | + PKG_TYPE="deb" |
| 39 | +elif [ -f /etc/redhat-release ]; then |
| 40 | + PKG_TYPE="rpm" |
| 41 | +else |
| 42 | + PKG_TYPE="tar.xz" |
| 43 | +fi |
| 44 | + |
| 45 | +function prompt_yes_no { |
| 46 | + while true; do |
| 47 | + read -p "$1 [y/N]: " yn |
| 48 | + case $yn in |
| 49 | + [Yy]* ) return 0;; |
| 50 | + [Nn]* ) return 1;; |
| 51 | + * ) return 1;; |
| 52 | + esac |
| 53 | + done |
| 54 | +} |
| 55 | + |
| 56 | +if ! prompt_yes_no "Do you want to continue with the installation?"; then |
| 57 | + echo -e "${RED}Installation aborted.${NC}" |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | + |
| 61 | +echo -e "${YELLOW}[1/4] Downloading ...${NC}" |
| 62 | + |
| 63 | +if [ "$VERSION" == "latest" ]; then |
| 64 | + DOWNLOAD_URL=$(curl -s https://api.github.com/repos/$REPO/releases/latest | grep "browser_download_url.*$ARCH" | grep "linux.*$PKG_TYPE\"" | cut -d '"' -f 4) |
| 65 | +else |
| 66 | + DOWNLOAD_URL="https://github.com/$REPO/releases/download/$VERSION/grhooks_${VERSION#v}_${ARCH}.${PKG_TYPE}" |
| 67 | +fi |
| 68 | + |
| 69 | +if [ -z "$DOWNLOAD_URL" ]; then |
| 70 | + echo -e "${RED}Cannot found package for your system (Arch: $ARCH, Pkg Type: $PKG_TYPE)${NC}" |
| 71 | + exit 1 |
| 72 | +fi |
| 73 | + |
| 74 | +echo "Downloading: $DOWNLOAD_URL" |
| 75 | +TEMP_DIR=$(mktemp -d) |
| 76 | +curl -sSL $DOWNLOAD_URL -o "$TEMP_DIR/grhooks.${PKG_TYPE}" || { |
| 77 | + echo -e "${RED}Fail to download package${NC}" |
| 78 | + exit 1 |
| 79 | +} |
| 80 | + |
| 81 | +echo -e "${YELLOW}[2/4] Installing...${NC}" |
| 82 | + |
| 83 | +case $PKG_TYPE in |
| 84 | + deb) |
| 85 | + sudo dpkg -i "$TEMP_DIR/grhooks.deb" || sudo apt-get install -f -y |
| 86 | + ;; |
| 87 | + rpm) |
| 88 | + sudo rpm -ivh "$TEMP_DIR/grhooks.rpm" || sudo yum install -y |
| 89 | + ;; |
| 90 | + tar.xz) |
| 91 | + sudo tar -xJf "$TEMP_DIR/grhooks.tar.xz" -C $TEMP_DIR |
| 92 | + sudo install -Dm755 "$TEMP_DIR/grhooks" "$INSTALL_DIR/grhooks" |
| 93 | + sudo chmod +x "$INSTALL_DIR/grhooks" |
| 94 | + ;; |
| 95 | +esac |
| 96 | + |
| 97 | +echo -e "${YELLOW}[3/4] Creating Config Directory...${NC}" |
| 98 | +sudo mkdir -p $CONFIG_DIR |
| 99 | +sudo chown $USER:$USER $CONFIG_DIR |
| 100 | + |
| 101 | +if prompt_yes_no "Do you want to configure GRHooks as a systemd service?"; then |
| 102 | + echo -e "${YELLOW}[4/4] Configuring systemd service...${NC}" |
| 103 | + |
| 104 | + SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" |
| 105 | + cat <<EOL | sudo tee $SERVICE_FILE > /dev/null |
| 106 | +[Unit] |
| 107 | +Description=GRHooks Webhook Server |
| 108 | +After=network.target |
| 109 | +
|
| 110 | +[Service] |
| 111 | +Type=simple |
| 112 | +User=$USER |
| 113 | +ExecStart=${INSTALL_DIR}/grhooks ${CONFIG_DIR} |
| 114 | +Restart=always |
| 115 | +RestartSec=5 |
| 116 | +Environment="GRHOOKS_LOG=${LOG_LEVEL}" |
| 117 | +
|
| 118 | +[Install] |
| 119 | +WantedBy=multi-user.target |
| 120 | +EOL |
| 121 | + |
| 122 | + sudo systemctl daemon-reload |
| 123 | + sudo systemctl enable $SERVICE_NAME |
| 124 | + |
| 125 | + if prompt_yes_no "Do you want to start the service now?"; then |
| 126 | + sudo systemctl start $SERVICE_NAME |
| 127 | + echo -e "${GREEN}Service started. You can view the logs with: journalctl -u $SERVICE_NAME -f${NC}" |
| 128 | + fi |
| 129 | +else |
| 130 | + echo -e "${YELLOW}[4/4] Skipping systemd service configuration.${NC}" |
| 131 | +fi |
| 132 | + |
| 133 | +rm -rf $TEMP_DIR |
| 134 | + |
| 135 | +echo -e "${GREEN}Installation completed!${NC}" |
| 136 | +echo "" |
| 137 | +echo -e "Post your manifests here: ${YELLOW}${CONFIG_DIR}${NC}" |
| 138 | +echo -e "Binary: ${YELLOW}${INSTALL_DIR}/grhooks${NC}" |
| 139 | +if [ -f "$SERVICE_FILE" ]; then |
| 140 | + echo -e "Service: ${YELLOW}systemctl status ${SERVICE_NAME}${NC}" |
| 141 | +fi |
0 commit comments