DropsBrowse Pastes
Login with GitHub

install.sh

September 30th, 2025Views: 5(4 unique)Plaintext
#!/bin/bash

set -e

work_dir=$1
if [ -z "$work_dir" ]; then
    echo "错误: 未提供工作目录。" >&2
    echo "用法: $0 <working_directory>" >&2
    exit 1
fi
work_dir=$(realpath "$work_dir")

go_bin=$(which go)
if [ -z "$go_bin" ]; then
    echo "错误: 'go' 命令未在您的 PATH 环境变量中找到。" >&2
    exit 1
fi

user_config_dir="${XDG_CONFIG_HOME:-$HOME/.config}"

systemd_user_dir="$user_config_dir/systemd/user"
service_file_path="$systemd_user_dir/epp@.service"
mkdir -p "$systemd_user_dir"
cat << EOF > "$service_file_path"
[Unit]
Description=EPP Service - %I

[Service]
WorkingDirectory=$work_dir
ExecStartPre=$go_bin build -o application/%I ./cmd/%I
ExecStartPre=/usr/bin/mkdir -p $work_dir/logs
ExecStartPre=/usr/bin/rm -f $work_dir/logs/%I.log
ExecStart=$work_dir/application/%I
Environment=APP_WORK_DIR=$work_dir
EnvironmentFile=%E/epp/%I.env
StandardOutput=file:$work_dir/logs/%I.log
StandardError=file:$work_dir/logs/%I.log

[Install]
WantedBy=multi-user.target
EOF

epp_config_dir="$user_config_dir/epp"
mkdir -p "$epp_config_dir"
services=(
    "audit-svc"
    "assert-svc"
    "cascade-agent-svc"
    "cltmgr"
    "data-viz-svc"
    "ops-svc"
    "plymgr"
    "setting-svc"
)
for service in "${services[@]}"; do
    env_file_path="$epp_config_dir/$service.env"
    module_name="${service%-svc}"
    config_files="configs/app.yaml,\
configs/api.yaml,\
configs/$module_name/modules.yml,\
configs/$module_name/service.yaml"
        
    if [ "$service" == "setting-svc" ]; then
        config_files="$config_files,\
configs/setting/data_lifecycle/backup_modules.toml,\
configs/setting/data_lifecycle/clean_modules.toml,\
configs/setting/data_lifecycle/export_configs.toml,\
configs/setting/data_lifecycle/table_configs.toml"
    fi
        
    echo "APP_CONFIG_FILES=\"$config_files\"" > "$env_file_path"
done

systemctl --user daemon-reload
if [[ "$SHELL" == *bash ]]; then
    shell_config="$HOME/.bashrc"
    cat << 'EOF' >> $shell_config
_epp_completion() {
  local cur="${COMP_WORDS[COMP_CWORD]}"
  local actions="start stop restart status enable disable"
  local modules="audit-svc assert-svc cascade-agent-svc cltmgr data-viz-svc ops-svc plymgr setting-svc"

  case "$COMP_CWORD" in
    1) COMPREPLY=($(compgen -W "${actions}" -- "${cur}"));;
    2) COMPREPLY=($(compgen -W "${modules}" -- "${cur}"));;
    *) COMPREPLY=();;
  esac
}
complete -F _epp_completion epp
EOF
elif [[ "$SHELL" == *zsh ]]; then
    shell_config="$HOME/.zshrc"
    cat << 'EOF' > $HOME/.oh-my-zsh/custom/completions/_epp
#compdef epp

_epp() {
  local -a actions
  actions=(
    'start:Start the service'
    'stop:Stop the service'
    'restart:Restart the service'
    'status:Show service status'
    'enable:Enable service to start on boot'
    'disable:Disable service from starting on boot'
  )
  local -a modules
  modules=(
    "audit-svc" "assert-svc" "cascade-agent-svc" "cltmgr"
    "data-viz-svc" "ops-svc" "plymgr" "setting-svc"
  )
  _arguments \
    '1:action:($(printf "%s\n" "${actions[@]}"))' \
    '2:module:(${modules[@]})' \
    '*:options:->options'
}
_epp "$@"
EOF
else
    exit 0
fi

cat << 'EOF' >> $shell_config
epp() {
  if [ "$#" -lt 2 ]; then
    echo "用法: epp <action> <module> [options...]"
    echo "action: start, stop, restart, status, enable, disable 等"
    echo "module: audit-svc, setting-svc 等"
    return 1
  fi

  local action="$1"
  local module="$2"
  shift 2
  local service_unit="epp@$(systemd-escape "$module")"
  systemctl --user "$action" "$service_unit" "$@"
}
EOF