#!/bin/bash # ============================================= # PERIPHERY INSTALLER - Shell Script Version # ============================================= set -e # Dừng script nếu có lỗi # Màu sắc cho output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Hàm in thông báo print_info() { echo -e "${GREEN}[INFO]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } # ============================================= # CẤU HÌNH CỐ ĐỊNH # ============================================= # Lấy phiên bản mới nhất từ GitHub print_info "Đang lấy phiên bản mới nhất của Komodo..." LATEST_VERSION=$(curl -s "https://api.github.com/repos/moghtech/komodo/releases/latest" | grep -o '"tag_name":"[^"]*"' | cut -d'"' -f4) if [ -z "$LATEST_VERSION" ]; then print_error "Không thể lấy phiên bản mới nhất. Vui lòng kiểm tra kết nối internet." exit 1 fi print_info "Phiên bản mới nhất: $LATEST_VERSION" # Cấu hình cố định VERSION="$LATEST_VERSION" ROOT_DIRECTORY="/etc/komodo" CONFIG_URL="https://raw.githubusercontent.com/moghtech/komodo/refs/heads/main/config/periphery.config.toml" BINARY_URL="https://github.com/moghtech/komodo/releases/download" # ============================================= # KIỂM TRA HỆ THỐNG # ============================================= # Kiểm tra systemd if ! command -v systemctl &> /dev/null || [ ! -d "/run/systemd/system/" ]; then print_error "Script này yêu cầu systemd. Hệ thống của bạn không hỗ trợ systemd." exit 1 fi # Kiểm tra curl if ! command -v curl &> /dev/null; then print_error "Script yêu cầu curl. Vui lòng cài đặt curl trước." exit 1 fi # ============================================= # THIẾT LẬP ĐƯỜNG DẪN # ============================================= HOME_DIR="$HOME" BIN_DIR="/usr/local/bin" CONFIG_DIR="/etc/komodo" SERVICE_DIR="/etc/systemd/system" print_info "=== THÔNG TIN CÀI ĐẶT ===" print_info "Phiên bản: $VERSION" print_info "Thư mục gốc: $ROOT_DIRECTORY" print_info "Thư mục binary: $BIN_DIR" print_info "Thư mục cấu hình: $CONFIG_DIR" print_info "Thư mục service: $SERVICE_DIR" # ============================================= # TẢI BINARY # ============================================= download_binary() { print_info "Đang tải binary Periphery..." # Dừng Periphery nếu đang chạy if systemctl is-active --quiet periphery; then print_info "Dừng Periphery đang chạy..." systemctl stop periphery fi # Tạo thư mục binary nếu chưa tồn tại mkdir -p "$BIN_DIR" # Xóa binary cũ nếu tồn tại BIN_PATH="$BIN_DIR/periphery" if [ -f "$BIN_PATH" ]; then print_info "Xóa binary cũ..." rm -f "$BIN_PATH" fi # Xác định kiến trúc ARCH=$(uname -m) case "$ARCH" in aarch64|arm64) print_info "Phát hiện kiến trúc ARM64" PERIPHERY_BIN="periphery-aarch64" ;; x86_64|amd64) print_info "Phát hiện kiến trúc x86_64" PERIPHERY_BIN="periphery-x86_64" ;; *) print_error "Kiến trúc không được hỗ trợ: $ARCH" exit 1 ;; esac # Tải binary DOWNLOAD_URL="$BINARY_URL/$VERSION/$PERIPHERY_BIN" print_info "Đang tải từ: $DOWNLOAD_URL" if ! curl -f -sSL "$DOWNLOAD_URL" -o "$BIN_PATH"; then print_error "Không thể tải binary từ $DOWNLOAD_URL" print_error "Vui lòng kiểm tra phiên bản: https://github.com/moghtech/komodo/tags" exit 1 fi # Thêm quyền thực thi chmod +x "$BIN_PATH" print_info "Binary đã được tải thành công: $BIN_PATH" } # ============================================= # TẠO FILE CẤU HÌNH # ============================================= create_config() { CONFIG_FILE="$CONFIG_DIR/periphery.config.toml" # Kiểm tra nếu file config đã tồn tại if [ -f "$CONFIG_FILE" ]; then print_warning "File cấu hình đã tồn tại tại $CONFIG_FILE" read -p "Bạn có muốn ghi đè không? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then print_info "Bỏ qua tạo file cấu hình." return fi print_info "Ghi đè file cấu hình cũ..." fi print_info "Đang tạo file cấu hình tại $CONFIG_FILE..." # Tạo thư mục cấu hình mkdir -p "$CONFIG_DIR" # Tải template và xử lý print_info "Đang tải template cấu hình..." TEMPLATE=$(curl -s "$CONFIG_URL") if [ -z "$TEMPLATE" ]; then print_error "Không thể tải template cấu hình từ $CONFIG_URL" exit 1 fi # Xử lý các dòng trong template echo "$TEMPLATE" | while IFS= read -r line; do # Thay thế root_directory if [[ "$line" == "root_directory ="* ]]; then echo "root_directory = \"$ROOT_DIRECTORY\"" # Bỏ comment core_address (giữ nguyên mặc định) elif [[ "$line" == "# core_address ="* ]]; then echo "# core_address =" # Bỏ comment connect_as (giữ nguyên mặc định) elif [[ "$line" == "# connect_as ="* ]]; then echo "# connect_as =" # Bỏ comment onboarding_key (giữ nguyên mặc định) elif [[ "$line" == "# onboarding_key ="* ]]; then echo "# onboarding_key =" else echo "$line" fi done > "$CONFIG_FILE" print_info "File cấu hình đã được tạo thành công." } # ============================================= # TẠO SYSTEMD SERVICE # ============================================= create_service() { SERVICE_FILE="$SERVICE_DIR/periphery.service" # Kiểm tra nếu file service đã tồn tại if [ -f "$SERVICE_FILE" ]; then print_warning "File service đã tồn tại tại $SERVICE_FILE" read -p "Bạn có muốn ghi đè không? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then print_info "Bỏ qua tạo file service." return fi print_info "Ghi đè file service cũ..." rm -f "$SERVICE_FILE" fi print_info "Đang tạo file service tại $SERVICE_FILE..." # Tạo thư mục service mkdir -p "$SERVICE_DIR" # Tạo file service cat > "$SERVICE_FILE" << EOF [Unit] Description=Agent to connect with Komodo Core After=network.target [Service] Environment="HOME=$HOME_DIR" ExecStart=$BIN_DIR/periphery --config-path $CONFIG_DIR/periphery.config.toml Restart=on-failure RestartSec=10 TimeoutStartSec=0 StandardOutput=journal StandardError=journal [Install] WantedBy=multi-user.target EOF print_info "File service đã được tạo thành công." # Reload systemd print_info "Reload systemd daemon..." systemctl daemon-reload } # ============================================= # CÀI ĐẶT VÀ KÍCH HOẠT SERVICE # ============================================= setup_service() { print_info "Cấu hình và kích hoạt Periphery service..." # Enable service để tự động khởi động khi boot systemctl enable periphery # Khởi động service systemctl start periphery # Kiểm tra trạng thái if systemctl is-active --quiet periphery; then print_info "✅ Periphery đã được cài đặt và đang chạy thành công!" print_info "Service đã được cấu hình để tự động khởi động sau khi reboot." else print_error "❌ Periphery không thể khởi động. Kiểm tra log với: journalctl -u periphery" exit 1 fi } # ============================================= # HIỂN THỊ THÔNG TIN HƯỚNG DẪN # ============================================= show_instructions() { echo echo "=========================================" echo "✅ CÀI ĐẶT PERIPHERY HOÀN TẤT!" echo "=========================================" echo echo "📌 Các lệnh quản lý Periphery:" echo " - Xem trạng thái: systemctl status periphery" echo " - Dừng Periphery: systemctl stop periphery" echo " - Khởi động lại: systemctl restart periphery" echo " - Xem log: journalctl -u periphery -f" echo " - Xem log gần đây: journalctl -u periphery -n 50" echo echo "📁 Vị trí các file:" echo " - Binary: $BIN_DIR/periphery" echo " - Cấu hình: $CONFIG_DIR/periphery.config.toml" echo " - Service: $SERVICE_DIR/periphery.service" echo echo "🔄 Periphery sẽ tự động khởi động khi hệ thống reboot." echo echo "⚠️ Để áp dụng thay đổi cấu hình sau khi chỉnh sửa:" echo " sudo systemctl restart periphery" echo "=========================================" } # ============================================= # THỰC THI CHÍNH # ============================================= main() { echo "=========================================" echo " PERIPHERY INSTALLER - Shell Script" echo "=========================================" echo # Kiểm tra quyền root if [ "$EUID" -ne 0 ]; then print_error "Script này cần chạy với quyền root (sudo)." print_error "Vui lòng chạy: sudo $0" exit 1 fi # Thực hiện các bước cài đặt download_binary create_config create_service setup_service show_instructions } # Chạy hàm main main