| #!/bin/bash
|
|
|
|
|
|
|
|
|
| set -e
|
|
|
|
|
| RED='\033[0;31m'
|
| GREEN='\033[0;32m'
|
| YELLOW='\033[1;33m'
|
| BLUE='\033[0;34m'
|
| NC='\033[0m'
|
|
|
|
|
| SERVICE_NAME="axonhub"
|
|
|
| if [[ -n "$SUDO_USER" && "$SUDO_USER" != "root" ]]; then
|
| USER_HOME="$(eval echo ~${SUDO_USER})"
|
| TARGET_USER="$SUDO_USER"
|
| else
|
| USER_HOME="$HOME"
|
| TARGET_USER="$USER"
|
| fi
|
| TARGET_GROUP="$(id -gn "$TARGET_USER" 2>/dev/null || echo "$TARGET_USER")"
|
| BASE_DIR="${USER_HOME}/.config/axonhub"
|
| CONFIG_FILE="${BASE_DIR}/config.yml"
|
| BINARY_PATH="/usr/local/bin/axonhub"
|
| DEFAULT_PORT=8090
|
| PID_FILE="${BASE_DIR}/axonhub.pid"
|
| LOG_FILE="${BASE_DIR}/axonhub.log"
|
|
|
| print_info() {
|
| echo -e "${BLUE}[INFO]${NC} $1"
|
| }
|
|
|
| print_success() {
|
| echo -e "${GREEN}[SUCCESS]${NC} $1"
|
| }
|
|
|
| print_warning() {
|
| echo -e "${YELLOW}[WARNING]${NC} $1"
|
| }
|
|
|
| print_error() {
|
| echo -e "${RED}[ERROR]${NC} $1"
|
| }
|
|
|
|
|
|
|
| start_directly() {
|
| print_info "Starting AxonHub directly..."
|
|
|
|
|
| if [[ -f "$PID_FILE" ]]; then
|
| local pid=$(cat "$PID_FILE")
|
| if kill -0 "$pid" 2>/dev/null; then
|
| print_warning "AxonHub is already running (PID: $pid)"
|
| return 0
|
| else
|
| print_info "Removing stale PID file"
|
| rm -f "$PID_FILE"
|
| fi
|
| fi
|
|
|
|
|
| if [[ ! -x "$BINARY_PATH" ]]; then
|
| print_error "AxonHub binary not found at $BINARY_PATH"
|
| print_info "Please run the install script first: ./deploy/install.sh"
|
| return 1
|
| fi
|
|
|
|
|
| if [[ ! -f "$CONFIG_FILE" ]]; then
|
| print_warning "Configuration file not found at $CONFIG_FILE"
|
| print_info "Starting with default configuration..."
|
| CONFIG_ARGS=""
|
| else
|
|
|
| CONFIG_ARGS=""
|
| fi
|
|
|
|
|
| mkdir -p "$BASE_DIR"
|
| chown "$TARGET_USER:$TARGET_GROUP" "$BASE_DIR" 2>/dev/null || true
|
|
|
|
|
| print_info "Starting AxonHub process..."
|
|
|
| if [[ $EUID -eq 0 ]]; then
|
|
|
| print_info "Running as user: $TARGET_USER"
|
| sudo -u "$TARGET_USER" bash -c "mkdir -p '$BASE_DIR'; \"$BINARY_PATH\" $CONFIG_ARGS >> '$LOG_FILE' 2>&1 & echo \$! > '$PID_FILE'"
|
| local pid
|
| pid=$(cat "$PID_FILE" 2>/dev/null || true)
|
| else
|
| "$BINARY_PATH" $CONFIG_ARGS > "$LOG_FILE" 2>&1 &
|
| local pid=$!
|
| echo "$pid" > "$PID_FILE"
|
| fi
|
|
|
|
|
| sleep 2
|
|
|
| if kill -0 "$pid" 2>/dev/null; then
|
| print_success "AxonHub started successfully (PID: $pid)"
|
| print_info "Process information:"
|
| echo " • PID: $pid"
|
| echo " • Log file: $LOG_FILE"
|
| echo " • Config: ${CONFIG_FILE:-"default"}"
|
| local port
|
| port=$(get_configured_port)
|
| echo " • Web interface: http://localhost:${port}"
|
| echo
|
| print_info "To stop AxonHub: ./stop.sh"
|
| print_info "To view logs: tail -f $LOG_FILE"
|
| else
|
| print_error "AxonHub failed to start"
|
| if [[ -f "$LOG_FILE" ]]; then
|
| print_info "Last few log lines:"
|
| tail -n 10 "$LOG_FILE"
|
| fi
|
| rm -f "$PID_FILE"
|
| return 1
|
| fi
|
| }
|
|
|
| get_configured_port() {
|
| local port="$DEFAULT_PORT"
|
|
|
|
|
| if [[ -x "$BINARY_PATH" ]]; then
|
| local config_port
|
| config_port=$("$BINARY_PATH" config get server.port 2>/dev/null) || true
|
| if [[ -n "$config_port" && "$config_port" =~ ^[0-9]+$ ]]; then
|
| port="$config_port"
|
| fi
|
| fi
|
|
|
| echo "$port"
|
| }
|
|
|
| check_port() {
|
| local port=${1:-$DEFAULT_PORT}
|
|
|
| if command -v netstat >/dev/null 2>&1; then
|
| if netstat -tuln | grep -q ":$port "; then
|
| print_warning "Port $port is already in use"
|
| print_info "Processes using port $port:"
|
| netstat -tulnp | grep ":$port " || true
|
| return 1
|
| fi
|
| elif command -v ss >/dev/null 2>&1; then
|
| if ss -tuln | grep -q ":$port "; then
|
| print_warning "Port $port is already in use"
|
| print_info "Processes using port $port:"
|
| ss -tulnp | grep ":$port " || true
|
| return 1
|
| fi
|
| fi
|
|
|
| return 0
|
| }
|
|
|
| main() {
|
| print_info "Starting AxonHub..."
|
|
|
|
|
| local port
|
| port=$(get_configured_port)
|
|
|
|
|
| if ! check_port "$port"; then
|
| print_error "Cannot start AxonHub: port $port is already in use"
|
| return 1
|
| fi
|
|
|
|
|
| start_directly
|
| }
|
|
|
|
|
| case "${1:-}" in
|
| --help|-h)
|
| echo "Usage: $0"
|
| echo
|
| echo "This script starts AxonHub directly (no systemd)."
|
| echo "Logs: $LOG_FILE"
|
| echo "PID file: $PID_FILE"
|
| exit 0
|
| ;;
|
| "")
|
| main
|
| ;;
|
| *)
|
| print_error "Unknown option: $1"
|
| print_info "Use --help for usage information"
|
| exit 1
|
| ;;
|
| esac
|
|
|