tuanchris 1 місяць тому
батько
коміт
c28542c560
2 змінених файлів з 261 додано та 14 видалено
  1. 241 0
      dw
  2. 20 14
      setup-pi.sh

+ 241 - 0
dw

@@ -0,0 +1,241 @@
+#!/bin/bash
+#
+# Dune Weaver CLI
+#
+# Usage: dw <command>
+#
+# Commands:
+#   install     Run initial setup (Docker + WiFi fix)
+#   start       Start Dune Weaver
+#   stop        Stop Dune Weaver
+#   restart     Restart Dune Weaver
+#   update      Pull latest changes and restart
+#   logs        View live logs (Ctrl+C to exit)
+#   status      Show container status
+#   shell       Open a shell in the container
+#   help        Show this help message
+#
+
+set -e
+
+# Colors
+RED='\033[0;31m'
+GREEN='\033[0;32m'
+YELLOW='\033[1;33m'
+BLUE='\033[0;34m'
+NC='\033[0m'
+
+# Find dune-weaver directory
+find_install_dir() {
+    # Check common locations
+    if [[ -f "$HOME/dune-weaver/main.py" ]]; then
+        echo "$HOME/dune-weaver"
+    elif [[ -f "/home/pi/dune-weaver/main.py" ]]; then
+        echo "/home/pi/dune-weaver"
+    elif [[ -f "./main.py" ]]; then
+        pwd
+    else
+        echo ""
+    fi
+}
+
+# Check if using Docker or systemd
+is_docker_mode() {
+    [[ -f "$INSTALL_DIR/docker-compose.yml" ]] && command -v docker &> /dev/null && docker compose ps &> /dev/null
+}
+
+INSTALL_DIR=$(find_install_dir)
+
+# Check if installed
+check_installed() {
+    if [[ -z "$INSTALL_DIR" ]]; then
+        echo -e "${RED}Error: Dune Weaver not found${NC}"
+        echo ""
+        echo "Please install first:"
+        echo "  git clone https://github.com/tuanchris/dune-weaver --single-branch"
+        echo "  cd dune-weaver"
+        echo "  dw install"
+        exit 1
+    fi
+}
+
+# Commands
+cmd_install() {
+    if [[ -z "$INSTALL_DIR" ]]; then
+        # Not installed, check if we're in the right directory
+        if [[ -f "./docker-compose.yml" ]] && [[ -f "./main.py" ]]; then
+            INSTALL_DIR=$(pwd)
+        else
+            echo -e "${RED}Error: Run this from the dune-weaver directory${NC}"
+            echo ""
+            echo "  git clone https://github.com/tuanchris/dune-weaver --single-branch"
+            echo "  cd dune-weaver"
+            echo "  dw install"
+            exit 1
+        fi
+    fi
+
+    cd "$INSTALL_DIR"
+    bash setup-pi.sh "$@"
+}
+
+cmd_start() {
+    check_installed
+    echo -e "${BLUE}Starting Dune Weaver...${NC}"
+    cd "$INSTALL_DIR"
+
+    if is_docker_mode; then
+        docker compose up -d
+    else
+        sudo systemctl start dune-weaver
+    fi
+
+    echo -e "${GREEN}Started!${NC} Access at http://$(hostname -I | awk '{print $1}'):8080"
+}
+
+cmd_stop() {
+    check_installed
+    echo -e "${BLUE}Stopping Dune Weaver...${NC}"
+    cd "$INSTALL_DIR"
+
+    if is_docker_mode; then
+        docker compose down
+    else
+        sudo systemctl stop dune-weaver
+    fi
+
+    echo -e "${GREEN}Stopped${NC}"
+}
+
+cmd_restart() {
+    check_installed
+    echo -e "${BLUE}Restarting Dune Weaver...${NC}"
+    cd "$INSTALL_DIR"
+
+    if is_docker_mode; then
+        docker compose restart
+    else
+        sudo systemctl restart dune-weaver
+    fi
+
+    echo -e "${GREEN}Restarted${NC}"
+}
+
+cmd_update() {
+    check_installed
+    echo -e "${BLUE}Updating Dune Weaver...${NC}"
+    cd "$INSTALL_DIR"
+
+    echo "Pulling latest code..."
+    git pull
+
+    if is_docker_mode; then
+        echo "Pulling latest Docker image..."
+        docker compose pull
+
+        echo "Restarting with new version..."
+        docker compose up -d
+    else
+        echo "Updating Python dependencies..."
+        source .venv/bin/activate
+        pip install -r requirements.txt
+
+        echo "Restarting service..."
+        sudo systemctl restart dune-weaver
+    fi
+
+    echo -e "${GREEN}Updated!${NC}"
+}
+
+cmd_logs() {
+    check_installed
+    cd "$INSTALL_DIR"
+
+    if is_docker_mode; then
+        docker compose logs -f
+    else
+        sudo journalctl -u dune-weaver -f
+    fi
+}
+
+cmd_status() {
+    check_installed
+    cd "$INSTALL_DIR"
+
+    if is_docker_mode; then
+        docker compose ps
+    else
+        sudo systemctl status dune-weaver
+    fi
+}
+
+cmd_shell() {
+    check_installed
+    cd "$INSTALL_DIR"
+
+    if is_docker_mode; then
+        docker compose exec dune-weaver /bin/bash
+    else
+        echo -e "${YELLOW}Shell not available in systemd mode${NC}"
+        echo "Use: cd $INSTALL_DIR && source .venv/bin/activate"
+    fi
+}
+
+cmd_help() {
+    echo -e "${GREEN}Dune Weaver CLI${NC}"
+    echo ""
+    echo "Usage: dw <command>"
+    echo ""
+    echo "Commands:"
+    echo "  install     Run initial setup (Docker + WiFi fix)"
+    echo "  start       Start Dune Weaver"
+    echo "  stop        Stop Dune Weaver"
+    echo "  restart     Restart Dune Weaver"
+    echo "  update      Pull latest changes and restart"
+    echo "  logs        View live logs (Ctrl+C to exit)"
+    echo "  status      Show container status"
+    echo "  shell       Open a shell in the container"
+    echo "  help        Show this help message"
+    echo ""
+    if [[ -n "$INSTALL_DIR" ]]; then
+        echo -e "Install directory: ${BLUE}$INSTALL_DIR${NC}"
+    fi
+}
+
+# Main
+case "${1:-help}" in
+    install)
+        shift
+        cmd_install "$@"
+        ;;
+    start)
+        cmd_start
+        ;;
+    stop)
+        cmd_stop
+        ;;
+    restart)
+        cmd_restart
+        ;;
+    update)
+        cmd_update
+        ;;
+    logs)
+        cmd_logs
+        ;;
+    status)
+        cmd_status
+        ;;
+    shell)
+        cmd_shell
+        ;;
+    help|--help|-h)
+        cmd_help
+        ;;
+    *)
+        echo -e "${RED}Unknown command: $1${NC}"
+        echo ""
+        cmd_help
+        exit 1
+        ;;
+esac

+ 20 - 14
setup-pi.sh

@@ -92,7 +92,7 @@ check_raspberry_pi() {
         return
     fi
 
-    MODEL=$(cat /proc/device-tree/model)
+    MODEL=$(tr -d '\0' < /proc/device-tree/model)
     echo "Detected: $MODEL"
 
     # Check for 64-bit OS
@@ -214,6 +214,17 @@ check_directory() {
     print_success "Running from $INSTALL_DIR"
 }
 
+# Install dw CLI command
+install_cli() {
+    print_step "Installing 'dw' command..."
+
+    # Copy dw script to /usr/local/bin
+    sudo cp "$INSTALL_DIR/dw" /usr/local/bin/dw
+    sudo chmod +x /usr/local/bin/dw
+
+    print_success "'dw' command installed"
+}
+
 # Deploy with Docker
 deploy_docker() {
     print_step "Deploying Dune Weaver with Docker Compose..."
@@ -301,19 +312,13 @@ print_final_instructions() {
     echo -e "  ${BLUE}http://$HOSTNAME.local:8080${NC}"
     echo ""
 
-    if [[ "$USE_DOCKER" == "true" ]]; then
-        echo "Useful commands:"
-        echo "  View logs:     cd $INSTALL_DIR && docker compose logs -f"
-        echo "  Restart:       cd $INSTALL_DIR && docker compose restart"
-        echo "  Update:        cd $INSTALL_DIR && git pull && docker compose pull && docker compose up -d"
-        echo "  Stop:          cd $INSTALL_DIR && docker compose down"
-    else
-        echo "Useful commands:"
-        echo "  View logs:     sudo journalctl -u dune-weaver -f"
-        echo "  Restart:       sudo systemctl restart dune-weaver"
-        echo "  Status:        sudo systemctl status dune-weaver"
-        echo "  Stop:          sudo systemctl stop dune-weaver"
-    fi
+    echo "Manage with the 'dw' command:"
+    echo "  dw logs        View live logs"
+    echo "  dw restart     Restart Dune Weaver"
+    echo "  dw update      Pull latest and restart"
+    echo "  dw stop        Stop Dune Weaver"
+    echo "  dw status      Show status"
+    echo "  dw help        Show all commands"
     echo ""
 
     if [[ "$DOCKER_GROUP_ADDED" == "true" ]]; then
@@ -369,6 +374,7 @@ main() {
         deploy_python
     fi
 
+    install_cli
     print_final_instructions
 }