Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

4.5 KB
Raw Blame
#!/usr/bin/env bash
# dev-launchd-setup.sh - Install or uninstall the Pulse managed dev LaunchAgent
#
# Usage:
#   ./scripts/dev-launchd-setup.sh           # Install and start
#   ./scripts/dev-launchd-setup.sh uninstall # Stop and remove

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd -P)"
LABEL="com.pulse.hot-dev"
PLIST_TEMPLATE="${SCRIPT_DIR}/com.pulse.hot-dev.plist.template"
PLIST_DST="$HOME/Library/LaunchAgents/${LABEL}.plist"
LOG_DIR="$HOME/Library/Logs/Pulse"
GUI_DOMAIN="gui/$(id -u)"

log_info() { printf "\033[0;34m[launchd-setup]\033[0m %s\n" "$1"; }
log_error() { printf "\033[0;31m[launchd-setup] ERROR:\033[0m %s\n" "$1"; }
log_ok() { printf "\033[0;32m[launchd-setup] ✓\033[0m %s\n" "$1"; }

uninstall() {
    log_info "Uninstalling ${LABEL}..."

    # Bootout (stop + unload) — ignore errors if not loaded
    launchctl bootout "${GUI_DOMAIN}/${LABEL}" 2>/dev/null && \
        log_ok "Service stopped and unloaded" || \
        log_info "Service was not loaded (already stopped)"

    if [[ -L "${PLIST_DST}" ]] || [[ -f "${PLIST_DST}" ]]; then
        rm -f "${PLIST_DST}"
        log_ok "Removed ${PLIST_DST}"
    else
        log_info "Plist not found at ${PLIST_DST} (already removed)"
    fi

    log_ok "Uninstall complete. Log files preserved at ${LOG_DIR}"
}

install() {
    if [[ ! -f "${PLIST_TEMPLATE}" ]]; then
        log_error "Plist template not found: ${PLIST_TEMPLATE}"
        exit 1
    fi

    # Preserve read-only/shared checkouts when Git already supplied the
    # executable bit. Some development hosts allow group content writes but
    # reserve mode changes for the file owner.
    if [[ ! -x "${SCRIPT_DIR}/dev-launchd-wrapper.sh" ]]; then
        chmod +x "${SCRIPT_DIR}/dev-launchd-wrapper.sh"
    fi

    # Create log directory
    mkdir -p "${LOG_DIR}"
    log_ok "Log directory: ${LOG_DIR}"

    # Stop existing service if running
    launchctl bootout "${GUI_DOMAIN}/${LABEL}" 2>/dev/null && \
        log_info "Stopped existing service" || true

    # Render plist into LaunchAgents from template.
    local wrapper_path work_dir stdout_path stderr_path
    wrapper_path="${SCRIPT_DIR}/dev-launchd-wrapper.sh"
    work_dir="${ROOT_DIR}"
    stdout_path="${LOG_DIR}/hot-dev.stdout.log"
    stderr_path="${LOG_DIR}/hot-dev.stderr.log"

    mkdir -p "$HOME/Library/LaunchAgents"
    sed \
        -e "s|__WRAPPER_PATH__|${wrapper_path}|g" \
        -e "s|__WORKDIR__|${work_dir}|g" \
        -e "s|__STDOUT_PATH__|${stdout_path}|g" \
        -e "s|__STDERR_PATH__|${stderr_path}|g" \
        "${PLIST_TEMPLATE}" > "${PLIST_DST}"
    chmod 0644 "${PLIST_DST}"
    log_ok "Rendered plist to ${PLIST_DST}"

    # Bootstrap (load + start)
    launchctl bootstrap "${GUI_DOMAIN}" "${PLIST_DST}"
    log_ok "Service bootstrapped"

    # Give it a moment to start
    sleep 3

    # Verify
    if launchctl print "${GUI_DOMAIN}/${LABEL}" 2>/dev/null | grep -q "pid ="; then
        PID=$(launchctl print "${GUI_DOMAIN}/${LABEL}" 2>/dev/null | grep "pid =" | awk '{print $NF}')
        log_ok "Service is running (PID: ${PID})"
    else
        log_error "Service may not have started. Check logs:"
        log_error "  tail -f ${LOG_DIR}/hot-dev.stderr.log"
        exit 1
    fi

    echo ""
    log_info "=== Pulse Dev LaunchAgent Installed ==="
    log_info ""
    log_info "The managed dev runtime will now auto-start on login and auto-restart on crash."
    log_info ""
    log_info "Browser entrypoint:"
    log_info "  http://127.0.0.1:5173"
    log_info ""
    log_info "Daily runtime commands:"
    log_info "  Start/repair: cd ${ROOT_DIR} && npm run dev"
    log_info "  Restart:      cd ${ROOT_DIR} && npm run dev:restart"
    log_info "  Stop:         cd ${ROOT_DIR} && npm run dev:stop"
    log_info "  Status:       cd ${ROOT_DIR} && npm run dev:status"
    log_info "  Logs:         cd ${ROOT_DIR} && npm run dev:logs"
    log_info ""
    log_info "LaunchAgent maintenance:"
    log_info "  Restart agent: launchctl kickstart -k ${GUI_DOMAIN}/${LABEL}"
    log_info "  Stop agent:    launchctl kill SIGTERM ${GUI_DOMAIN}/${LABEL}"
    log_info "  Agent status:  launchctl print ${GUI_DOMAIN}/${LABEL}"
    log_info "  Agent logs:    tail -f ${LOG_DIR}/hot-dev.stderr.log"
    log_info "  Disable:  launchctl bootout ${GUI_DOMAIN}/${LABEL}"
    log_info "  Remove:   $0 uninstall"
}

case "${1:-install}" in
    uninstall|remove)
        uninstall
        ;;
    install|"")
        install
        ;;
    *)
        echo "Usage: $0 [install|uninstall]"
        exit 1
        ;;
esac