diff --git a/frontend/public/json/copyparty.json b/frontend/public/json/copyparty.json index e1b94f57e..e554a5ef2 100644 --- a/frontend/public/json/copyparty.json +++ b/frontend/public/json/copyparty.json @@ -8,7 +8,7 @@ "type": "addon", "updateable": true, "privileged": false, - "interface_port": null, + "interface_port": 3923, "documentation": "https://github.com/9001/copyparty?tab=readme-ov-file#the-browser", "website": "https://github.com/9001/copyparty", "logo": "https://cdn.jsdelivr.net/gh/selfhst/icons@main/webp/copyparty.webp", @@ -35,6 +35,10 @@ { "text": "Execute within the Proxmox shell or in LXC", "type": "info" + }, + { + "text": "Update with: update_copyparty", + "type": "info" } ] } diff --git a/tools/addon/copyparty.sh b/tools/addon/copyparty.sh index d144b1e99..ac8c41e3c 100644 --- a/tools/addon/copyparty.sh +++ b/tools/addon/copyparty.sh @@ -5,6 +5,49 @@ # License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE # Source: https://github.com/9001/copyparty +source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/core.func) +source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/tools.func) +source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/error_handler.func) + +# Enable error handling +set -Eeuo pipefail +trap 'error_handler' ERR +load_functions + +# ============================================================================== +# CONFIGURATION +# ============================================================================== +VERBOSE=${var_verbose:-no} +APP="CopyParty" +APP_TYPE="addon" +BIN_PATH="/usr/local/bin/copyparty-sfx.py" +CONF_PATH="/etc/copyparty.conf" +LOG_PATH="/var/log/copyparty" +DATA_PATH="/var/lib/copyparty" +SVC_USER="copyparty" +SVC_GROUP="copyparty" +SRC_URL="https://github.com/9001/copyparty/releases/latest/download/copyparty-sfx.py" +DEFAULT_PORT=3923 + +# ============================================================================== +# OS DETECTION +# ============================================================================== +if [[ -f "/etc/alpine-release" ]]; then + OS="Alpine" + PKG_MANAGER="apk add --no-cache" + SERVICE_PATH="/etc/init.d/copyparty" +elif grep -qE 'ID=debian|ID=ubuntu' /etc/os-release; then + OS="Debian" + PKG_MANAGER="apt-get install -y" + SERVICE_PATH="/etc/systemd/system/copyparty.service" +else + msg_error "Unsupported OS detected. Exiting." + exit 1 +fi + +# ============================================================================== +# HEADER +# ============================================================================== function header_info() { clear cat <<"EOF" @@ -17,46 +60,13 @@ function header_info() { EOF } -YW=$(echo "\033[33m") -GN=$(echo "\033[1;92m") -RD=$(echo "\033[01;31m") -BL=$(echo "\033[36m") -CL=$(echo "\033[m") -CM="${GN}✔️${CL}" -CROSS="${RD}✖️${CL}" -INFO="${BL}ℹ️${CL}" - -APP="CopyParty" -BIN_PATH="/usr/local/bin/copyparty-sfx.py" -CONF_PATH="/etc/copyparty.conf" -LOG_PATH="/var/log/copyparty" -DATA_PATH="/var/lib/copyparty" -SERVICE_PATH_DEB="/etc/systemd/system/copyparty.service" -SERVICE_PATH_ALP="/etc/init.d/copyparty" -SVC_USER="copyparty" -SVC_GROUP="copyparty" -SRC_URL="https://github.com/9001/copyparty/releases/latest/download/copyparty-sfx.py" -DEFAULT_PORT=3923 - -if [[ -f "/etc/alpine-release" ]]; then - OS="Alpine" - PKG_MANAGER="apk add --no-cache" - SERVICE_PATH="$SERVICE_PATH_ALP" -elif [[ -f "/etc/debian_version" ]]; then - OS="Debian" - PKG_MANAGER="apt-get install -y" - SERVICE_PATH="$SERVICE_PATH_DEB" -else - echo -e "${CROSS} Unsupported OS detected. Exiting." - exit 1 -fi - -header_info - -function msg_info() { echo -e "${INFO} ${YW}$1...${CL}"; } -function msg_ok() { echo -e "${CM} ${GN}$1${CL}"; } -function msg_error() { echo -e "${CROSS} ${RD}$1${CL}"; } +# ============================================================================== +# HELPER FUNCTIONS +# ============================================================================== +# ============================================================================== +# HELPER FUNCTIONS +# ============================================================================== function setup_user_and_dirs() { msg_info "Creating $SVC_USER user and directories" if ! id "$SVC_USER" &>/dev/null; then @@ -73,150 +83,161 @@ function setup_user_and_dirs() { msg_ok "User/Group/Dirs ready" } -function uninstall_copyparty() { - msg_info "Uninstalling $APP" - if [[ "$OS" == "Debian" ]]; then - systemctl disable --now copyparty &>/dev/null - rm -f "$SERVICE_PATH_DEB" +# ============================================================================== +# UNINSTALL +# ============================================================================== +function uninstall() { + msg_info "Uninstalling ${APP}" + if [[ "$OS" == "Alpine" ]]; then + rc-service copyparty stop &>/dev/null || true + rc-update del copyparty &>/dev/null || true + rm -f "$SERVICE_PATH" else - rc-service copyparty stop &>/dev/null - rc-update del copyparty &>/dev/null - rm -f "$SERVICE_PATH_ALP" + systemctl disable --now copyparty.service &>/dev/null || true + rm -f "$SERVICE_PATH" fi rm -f "$BIN_PATH" "$CONF_PATH" - msg_ok "$APP has been uninstalled." - exit 0 + rm -rf "$DATA_PATH" "$LOG_PATH" + userdel "$SVC_USER" 2>/dev/null || true + groupdel "$SVC_GROUP" 2>/dev/null || true + rm -f "/usr/local/bin/update_copyparty" + rm -f "$HOME/.copyparty" + msg_ok "${APP} has been uninstalled" } -function update_copyparty() { - msg_info "Updating $APP" +# ============================================================================== +# UPDATE +# ============================================================================== +function update() { + if check_for_gh_release "copyparty-sfx.py" "9001/copyparty"; then + msg_info "Stopping service" + if [[ "$OS" == "Alpine" ]]; then + rc-service copyparty stop &>/dev/null || true + else + systemctl stop copyparty.service &>/dev/null || true + fi + msg_ok "Stopped service" + + msg_info "Updating ${APP}" + curl -fsSL "$SRC_URL" -o "$BIN_PATH" + chmod +x "$BIN_PATH" + chown "$SVC_USER:$SVC_GROUP" "$BIN_PATH" + msg_ok "Updated ${APP}" + + msg_info "Starting service" + if [[ "$OS" == "Alpine" ]]; then + rc-service copyparty start + else + systemctl start copyparty.service + fi + msg_ok "Started service" + msg_ok "Updated successfully!" + exit + fi +} + +# ============================================================================== +# INSTALL +# ============================================================================== +function install() { + local port data_path enable_auth admin_user admin_pass + + echo "" + read -rp "${TAB}Enter port for ${APP} [${DEFAULT_PORT}]: " port + port=${port:-$DEFAULT_PORT} + + read -rp "${TAB}Set data directory [${DATA_PATH}]: " data_path + data_path=${data_path:-$DATA_PATH} + + echo -n "${TAB}Enable authentication? (Y/n): " + read -r enable_auth + if [[ "${enable_auth,,}" =~ ^(n|no)$ ]]; then + admin_user="" + admin_pass="" + msg_ok "Configured without authentication" + else + read -rp "${TAB}Set admin username [admin]: " admin_user + admin_user=${admin_user:-admin} + read -rsp "${TAB}Set admin password [helper-scripts.com]: " admin_pass + echo "" + admin_pass=${admin_pass:-helper-scripts.com} + msg_ok "Configured with admin user: ${admin_user}" + fi + + msg_info "Installing dependencies" + if [[ "$OS" == "Debian" ]]; then + $STD $PKG_MANAGER python3 python3-pil ffmpeg curl + else + $STD $PKG_MANAGER python3 py3-pillow ffmpeg curl + fi + msg_ok "Dependencies installed (with thumbnail support)" + + setup_user_and_dirs + + # Use data_path if provided + if [[ "$data_path" != "$DATA_PATH" ]]; then + DATA_PATH="$data_path" + mkdir -p "$DATA_PATH" + chown "$SVC_USER:$SVC_GROUP" "$DATA_PATH" + fi + + msg_info "Downloading ${APP}" curl -fsSL "$SRC_URL" -o "$BIN_PATH" chmod +x "$BIN_PATH" - msg_ok "Updated successfully!" - exit 0 -} + chown "$SVC_USER:$SVC_GROUP" "$BIN_PATH" + msg_ok "Downloaded to ${BIN_PATH}" -if [[ -f "$BIN_PATH" ]]; then - echo -e "${YW}⚠️ $APP is already installed.${CL}" - echo -n "Uninstall $APP? (y/N): " - read -r uninstall_prompt - if [[ "${uninstall_prompt,,}" =~ ^(y|yes)$ ]]; then - uninstall_copyparty - fi + msg_info "Creating configuration" + cat <"$CONF_PATH" +[global] + p: ${port} + ansi + e2dsa + e2ts + theme: 2 + grid + no-robots + force-js + lo: ${LOG_PATH}/cpp-%Y-%m%d.txt.xz - echo -n "Update $APP? (y/N): " - read -r update_prompt - if [[ "${update_prompt,,}" =~ ^(y|yes)$ ]]; then - update_copyparty - else - echo -e "${YW}⚠️ Update skipped. Exiting.${CL}" - exit 0 - fi -fi - -msg_info "Installing dependencies" -if [[ "$OS" == "Debian" ]]; then - $PKG_MANAGER python3 curl &>/dev/null -else - $PKG_MANAGER python3 curl &>/dev/null -fi -msg_ok "Dependencies installed" - -setup_user_and_dirs - -msg_info "Downloading $APP" -curl -fsSL "$SRC_URL" -o "$BIN_PATH" -chmod +x "$BIN_PATH" -chown "$SVC_USER:$SVC_GROUP" "$BIN_PATH" -msg_ok "Downloaded to $BIN_PATH" - -echo -n "Enter port for $APP (default: $DEFAULT_PORT): " -read -r PORT -PORT=${PORT:-$DEFAULT_PORT} - -echo -n "Set data directory (default: $DATA_PATH): " -read -r USER_DATA_PATH -USER_DATA_PATH=${USER_DATA_PATH:-$DATA_PATH} -mkdir -p "$USER_DATA_PATH" -chown "$SVC_USER:$SVC_GROUP" "$USER_DATA_PATH" - -echo -n "Enable authentication? (Y/n): " -read -r auth_enable -if [[ "${auth_enable,,}" =~ ^(n|no)$ ]]; then - AUTH_LINE="" - msg_ok "Configured without authentication" -else - echo -n "Set admin username [default: admin]: " - read -r ADMIN_USER - ADMIN_USER=${ADMIN_USER:-admin} - echo -n "Set admin password [default: helper-scripts.com]: " - read -rs ADMIN_PASS - ADMIN_PASS=${ADMIN_PASS:-helper-scripts.com} - echo - AUTH_LINE="auth vhost=/:$ADMIN_USER:$ADMIN_PASS:admin,," - msg_ok "Configured with admin user: $ADMIN_USER" -fi - -msg_info "Writing config to $CONF_PATH" -msg_info "Writing config to $CONF_PATH" -{ - echo "[global]" - echo " p: $PORT" - echo " ansi" - echo " e2dsa" - echo " e2ts" - echo " theme: 2" - echo " grid" - echo - if [[ -n "$ADMIN_USER" && -n "$ADMIN_PASS" ]]; then - echo "[accounts]" - echo " $ADMIN_USER: $ADMIN_PASS" - echo - fi - echo "[/]" - echo " $USER_DATA_PATH" - echo " accs:" - if [[ -n "$ADMIN_USER" ]]; then - echo " rw: *" - echo " rwmda: $ADMIN_USER" - else - echo " rw: *" - fi -} >"$CONF_PATH" - -chmod 640 "$CONF_PATH" -chown "$SVC_USER:$SVC_GROUP" "$CONF_PATH" -msg_ok "Config written" - -msg_info "Creating service" -if [[ "$OS" == "Debian" ]]; then - cat <"$SERVICE_PATH_DEB" -[Unit] -Description=Copyparty file server -After=network.target - -[Service] -User=$SVC_USER -Group=$SVC_GROUP -WorkingDirectory=$DATA_PATH -ExecStart=/usr/bin/python3 /usr/local/bin/copyparty-sfx.py -c /etc/copyparty.conf -Restart=always -StandardOutput=append:/var/log/copyparty/copyparty.log -StandardError=append:/var/log/copyparty/copyparty.err - -[Install] -WantedBy=multi-user.target EOF - systemctl enable -q --now copyparty + if [[ -n "$admin_user" && -n "$admin_pass" ]]; then + cat <>"$CONF_PATH" +[accounts] + ${admin_user}: ${admin_pass} -elif [[ "$OS" == "Alpine" ]]; then - cat <<'EOF' >"$SERVICE_PATH_ALP" +EOF + fi + + cat <>"$CONF_PATH" +[/] + ${DATA_PATH} + accs: +EOF + + if [[ -n "$admin_user" ]]; then + cat <>"$CONF_PATH" + rw: * + rwmda: ${admin_user} +EOF + else + cat <>"$CONF_PATH" + rw: * +EOF + fi + + chmod 640 "$CONF_PATH" + chown "$SVC_USER:$SVC_GROUP" "$CONF_PATH" + msg_ok "Created configuration" + + msg_info "Creating service" + if [[ "$OS" == "Alpine" ]]; then + cat <<'SERVICEEOF' >"$SERVICE_PATH" #!/sbin/openrc-run name="copyparty" -description="Copyparty file server" - +description="CopyParty file server" command="$(command -v python3)" command_args="/usr/local/bin/copyparty-sfx.py -c /etc/copyparty.conf" command_background=true @@ -228,21 +249,110 @@ error_log="/var/log/copyparty/copyparty.err" depend() { need net } -EOF +SERVICEEOF + chmod +x "$SERVICE_PATH" + $STD rc-update add copyparty default + $STD rc-service copyparty start + else + cat <"$SERVICE_PATH" +[Unit] +Description=CopyParty file server +After=network.target - chmod +x "$SERVICE_PATH_ALP" - rc-update add copyparty default >/dev/null 2>&1 - rc-service copyparty restart >/dev/null 2>&1 +[Service] +User=${SVC_USER} +Group=${SVC_GROUP} +WorkingDirectory=${DATA_PATH} +ExecStart=/usr/bin/python3 ${BIN_PATH} -c ${CONF_PATH} +Restart=always +StandardOutput=append:${LOG_PATH}/copyparty.log +StandardError=append:${LOG_PATH}/copyparty.err + +[Install] +WantedBy=multi-user.target +SERVICEEOF + systemctl daemon-reload + systemctl enable --now copyparty.service &>/dev/null + fi + msg_ok "Created and started service" + + # Create update script + msg_info "Creating update script" + ensure_usr_local_bin_persist + cat <<'UPDATEEOF' >/usr/local/bin/update_copyparty +#!/usr/bin/env bash +# CopyParty Update Script +type=update bash -c "$(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/tools/addon/copyparty.sh)" +UPDATEEOF + chmod +x /usr/local/bin/update_copyparty + msg_ok "Created update script (/usr/local/bin/update_copyparty)" + + echo "" + msg_ok "${APP} installed successfully" + msg_ok "Web UI: ${BL}http://${LOCAL_IP}:${port}${CL}" + msg_ok "Storage: ${BL}${DATA_PATH}${CL}" + msg_ok "Config: ${BL}${CONF_PATH}${CL}" + if [[ -n "$admin_user" ]]; then + echo "" + msg_ok "Login: ${GN}${admin_user}${CL} / ${GN}${admin_pass}${CL}" + fi +} + +# ============================================================================== +# MAIN +# ============================================================================== +header_info +ensure_usr_local_bin_persist +import_local_ip + +# Handle type=update (called from update script) +if [[ "${type:-}" == "update" ]]; then + if [[ -f "$BIN_PATH" ]]; then + update + else + msg_error "${APP} is not installed. Nothing to update." + exit 1 + fi + exit 0 fi -msg_ok "Service created and started" -IFACE=$(ip -4 route | awk '/default/ {print $5; exit}') -IP=$(ip -4 addr show "$IFACE" | awk '/inet / {print $2}' | cut -d/ -f1 | head -n 1) -[[ -z "$IP" ]] && IP=$(hostname -I | awk '{print $1}') -[[ -z "$IP" ]] && IP="127.0.0.1" +# Check if already installed +if [[ -f "$BIN_PATH" ]]; then + msg_warn "${APP} is already installed." + echo "" -echo -e "${CM} ${GN}$APP is running at: ${BL}http://$IP:$PORT${CL}" -echo -e "${INFO} Storage directory: ${YW}$USER_DATA_PATH${CL}" -if [[ -n "$AUTH_LINE" ]]; then - echo -e "${INFO} Login: ${GN}${ADMIN_USER}${CL} / ${GN}${ADMIN_PASS}${CL}" + echo -n "${TAB}Uninstall ${APP}? (y/N): " + read -r uninstall_prompt + if [[ "${uninstall_prompt,,}" =~ ^(y|yes)$ ]]; then + uninstall + exit 0 + fi + + echo -n "${TAB}Update ${APP}? (y/N): " + read -r update_prompt + if [[ "${update_prompt,,}" =~ ^(y|yes)$ ]]; then + update + exit 0 + fi + + msg_warn "No action selected. Exiting." + exit 0 +fi + +# Fresh installation +msg_warn "${APP} is not installed." +echo "" +echo -e "${TAB}${INFO} This will install:" +echo -e "${TAB} - CopyParty (Python file server)" +echo -e "${TAB} - Thumbnail support (Pillow, FFmpeg)" +echo -e "${TAB} - Systemd/OpenRC service" +echo "" + +echo -n "${TAB}Install ${APP}? (y/N): " +read -r install_prompt +if [[ "${install_prompt,,}" =~ ^(y|yes)$ ]]; then + install +else + msg_warn "Installation cancelled. Exiting." + exit 0 fi