From 4eb56e781bfb6d56c1c7705b2b4b6d843a9c66fc Mon Sep 17 00:00:00 2001 From: nyaoouo Date: Mon, 9 Jun 2025 03:51:41 -0700 Subject: [PATCH] Initial commit --- OneClickBackup.cmd | 203 ++++++++++++++++++++++++++ OneClickDeploy.cmd | 346 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 549 insertions(+) create mode 100644 OneClickBackup.cmd create mode 100644 OneClickDeploy.cmd diff --git a/OneClickBackup.cmd b/OneClickBackup.cmd new file mode 100644 index 0000000..753cb55 --- /dev/null +++ b/OneClickBackup.cmd @@ -0,0 +1,203 @@ +@echo off +SETLOCAL EnableDelayedExpansion +goto :main + +:parseArgs +@REM Parse command line arguments by `call :parseArgs %*` +@REM use !ARG[%n%]! to access arguments +set "_ARG_MAX=0" +for %%A in (%*) do ( + set "_ARG[!_ARG_MAX!]=%%A" + set /a _ARG_MAX+=1 +) +set "ARG_MAX=0" +set "_ARG_PTR=0" + +:while + if !_ARG_PTR! geq !_ARG_MAX! goto :endwhile + set "_ARG=!_ARG[%_ARG_PTR%]!" + if "!_ARG:~0,1!"=="-" ( + set "_ARG_KEY=!_ARG:~1!" + set /a _ARG_PTR+=1 + call set "_ARG_VALUE=%%_ARG[!_ARG_PTR!]%%" + set "ARG[!_ARG_KEY!]=!_ARG_VALUE!" + ) else ( + set "ARG[!ARG_MAX!]=!_ARG!" + set /a ARG_MAX+=1 + ) + set /a _ARG_PTR+=1 + goto :while +:endwhile +exit /b 0 + +:raise +set gErrorLevel=%~2 +echo %~1 +goto :end + +:assert +if %errorlevel% neq 0 ( + call :raise "Assertion failed: %~1" %errorlevel% +) +exit /b 0 + +:download +SETLOCAL EnableDelayedExpansion +set "url=%~1" +set "dest=%~2" + +set "destDir=%~dp2" +if not exist "%destDir%" ( + mkdir "%destDir%" + call :assert "Failed to create directory %destDir%" +) + +if not exist "%dest%" ( + echo Downloading %url% to %dest%... + @REM powershell -Command "Invoke-WebRequest -Uri '%url%' -OutFile '%dest%'" + powershell -Command "$ProgressPreference='SilentlyContinue'; Invoke-WebRequest -Uri '%url%' -OutFile '%dest%'" + call :assert "Failed to download %url%" +) else ( + call :raise "File %dest% already exists." +) +ENDLOCAL +exit /b 0 + +:unzip +SETLOCAL EnableDelayedExpansion +set "zipFile=%~1" +set "destDir=%~2" +if not exist "%zipFile%" ( + call :raise "Zip file %zipFile% does not exist." +) +if not exist "%destDir%" ( + mkdir "%destDir%" +) +powershell -Command "Expand-Archive -Path '%zipFile%' -DestinationPath '%destDir%' -Force" +call :assert "Failed to unzip %zipFile% to %destDir%" +ENDLOCAL +exit /b 0 + +:ensure-mongodb-tools +SETLOCAL EnableDelayedExpansion +set "archivePath=%ExternalDir%\.tmp\mongodb-tools.zip" +set "dir=%ExternalDir%\mongodb-tools" +set "expectDir=%ExternalDir%\mongodb-database-tools-windows-x86_64-100.12.2" +set "dlLink=https://fastdl.mongodb.org/tools/db/mongodb-database-tools-windows-x86_64-100.12.2.zip" + +if not exist "%dir%" ( + if not exist "%archivePath%" ( + echo Downloading MongoDB tools... + call :download "%dlLink%" "%archivePath%" + ) + call :unzip "%archivePath%" "%ExternalDir%" + if exist "%expectDir%" ( + move /Y "%expectDir%" "%dir%" + ) else ( + call :raise "Expected directory %expectDir% does not exist after unzipping." + ) +) +if not exist "%dir%\bin\mongodump.exe" ( + call :raise "mongodump executable not found in %dir%\bin." +) +if not exist "%dir%\bin\mongorestore.exe" ( + call :raise "mongorestore executable not found in %dir%\bin." +) +echo MongoDB tools are available at %dir% +ENDLOCAL +exit /b 0 + +:ensure-mongosh +SETLOCAL EnableDelayedExpansion +set "archivePath=%ExternalDir%\.tmp\mongosh.zip" +set "dir=%ExternalDir%\mongosh" +set "expectDir=%ExternalDir%\mongosh-2.5.2-win32-x64" +set "dlLink=https://downloads.mongodb.com/compass/mongosh-2.5.2-win32-x64.zip" + +if not exist "%dir%" ( + if not exist "%archivePath%" ( + echo Downloading mongosh... + call :download "%dlLink%" "%archivePath%" + ) + call :unzip "%archivePath%" "%ExternalDir%" + if exist "%expectDir%" ( + move /Y "%expectDir%" "%dir%" + ) else ( + call :raise "Expected directory %expectDir% does not exist after unzipping." + ) +) +if not exist "%dir%\bin\mongosh.exe" ( + call :raise "mongosh executable not found in %dir%\bin." +) +echo mongosh is available at %dir% +ENDLOCAL +exit /b 0 + +:do-backup +SETLOCAL EnableDelayedExpansion +set "FILE_NAME=%date:~-4,4%-%date:~-10,2%-%date:~-7,2%_%time:~0,2%-%time:~3,2%-%time:~6,2%.gz" +set "FILE_NAME=%FILE_NAME: =0%" +if not exist "%BackupDir%" ( + mkdir "%BackupDir%" + call :assert "Failed to create backup directory %BackupDir%" +) +"%ExternalDir%\mongodb-tools\bin\mongodump.exe" --uri="%MongodbUri%" --gzip --archive="%BackupDir%\%FILE_NAME%" >> "%LOG_FILE%" 2>&1 +call :assert "Error during MongoDB dump. Check %LOG_FILE% for details." +echo Backup completed successfully. Archive saved as %FILE_NAME%. +ENDLOCAL +exit /b 0 + +:do-restore +"%ExternalDir%\mongosh\bin\mongosh.exe" --quiet --eval "db.dropDatabase();" "%MongodbUri%" >> "%LOG_FILE%" 2>&1 +call :assert "Error during MongoDB collection clearing. Check %LOG_FILE% for details." +"%ExternalDir%\mongodb-tools\bin\mongorestore.exe" --uri="%MongodbUri%" --gzip --archive="%RESTORE_ARCHIVE%" >> "%LOG_FILE%" 2>&1 +call :assert "Error during MongoDB restore from %RESTORE_ARCHIVE%. Check %LOG_FILE% for details." +echo "%ExternalDir%\mongodb-tools\bin\mongorestore.exe" --uri="%MongodbUri%" --gzip --archive="%RESTORE_ARCHIVE%" +echo Restore completed successfully from %RESTORE_ARCHIVE%. +exit /b 0 + + +:main +set gErrorLevel=0 +call :parseArgs %* +echo Usage: %0 [options] [backup-archive(optional)] +echo Options: +echo -externalDir [path] Set the external directory (default: current script directory\external) +echo -backupDir [path] Set the backup directory (default: current script directory\backup) +echo -mongodbUri [uri] Set the MongoDB URI (default: mongodb://127.0.0.1:27017/openWF) +echo ============================================================ + +if defined ARG[externalDir] set "ExternalDir=%ARG[externalDir]%" +if defined ARG[backupDir] set "BackupDir=%ARG[backupDir]%" +if defined ARG[mongodbUri] set "MongodbUri=%ARG[mongodbUri]%" +if not defined ExternalDir set "ExternalDir=%~dp0\external" +if not defined BackupDir set "BackupDir=%~dp0\backup" +if not defined MongodbUri set "MongodbUri=mongodb://127.0.0.1:27017/openWF" + +set "LOG_FILE=%BackupDir%\.backup_log.txt" +set "RESTORE_ARCHIVE=%ARG[0]%" + +echo External Directory: %ExternalDir% +echo Backup Directory: %BackupDir% +echo MongoDB URI: %MongodbUri% + +call :ensure-mongodb-tools +call :ensure-mongosh + +if not exist "%BackupDir%" ( + mkdir "%BackupDir%" + call :assert "Failed to create backup directory %BackupDir%" +) + + +call :do-backup +if exist "%RESTORE_ARCHIVE%" ( + call :do-restore +) else ( + echo No restore archive specified or file does not exist: %RESTORE_ARCHIVE% +) + +:end +ENDLOCAL +pause +exit %gErrorLevel% \ No newline at end of file diff --git a/OneClickDeploy.cmd b/OneClickDeploy.cmd new file mode 100644 index 0000000..839ec8e --- /dev/null +++ b/OneClickDeploy.cmd @@ -0,0 +1,346 @@ +@echo off +SETLOCAL EnableDelayedExpansion +goto :main + +:parseArgs +@REM Parse command line arguments by `call :parseArgs %*` +@REM use !ARG[%n%]! to access arguments +set "_ARG_MAX=0" +for %%A in (%*) do ( + set "_ARG[!_ARG_MAX!]=%%A" + set /a _ARG_MAX+=1 +) +set "ARG_MAX=0" +set "_ARG_PTR=0" + +:while + if !_ARG_PTR! geq !_ARG_MAX! goto :endwhile + set "_ARG=!_ARG[%_ARG_PTR%]!" + if "!_ARG:~0,1!"=="-" ( + set "_ARG_KEY=!_ARG:~1!" + set /a _ARG_PTR+=1 + call set "_ARG_VALUE=%%_ARG[!_ARG_PTR!]%%" + set "ARG[!_ARG_KEY!]=!_ARG_VALUE!" + ) else ( + set "ARG[!ARG_MAX!]=!_ARG!" + set /a ARG_MAX+=1 + ) + set /a _ARG_PTR+=1 + goto :while +:endwhile +exit /b 0 + +:raise +set gErrorLevel=%~2 +echo %~1 +goto :end + +:assert +if %errorlevel% neq 0 ( + call :raise "Assertion failed: %~1" %errorlevel% +) +exit /b 0 + +:download +SETLOCAL EnableDelayedExpansion +set "url=%~1" +set "dest=%~2" + +set "destDir=%~dp2" +if not exist "%destDir%" ( + mkdir "%destDir%" + call :assert "Failed to create directory %destDir%" +) + +if not exist "%dest%" ( + echo Downloading %url% to %dest%... + @REM powershell -Command "Invoke-WebRequest -Uri '%url%' -OutFile '%dest%'" + powershell -Command "$ProgressPreference='SilentlyContinue'; Invoke-WebRequest -Uri '%url%' -OutFile '%dest%'" + call :assert "Failed to download %url%" +) else ( + call :raise "File %dest% already exists." +) +ENDLOCAL +exit /b 0 + +:unzip +SETLOCAL EnableDelayedExpansion +set "zipFile=%~1" +set "destDir=%~2" +if not exist "%zipFile%" ( + call :raise "Zip file %zipFile% does not exist." +) +if not exist "%destDir%" ( + mkdir "%destDir%" +) +powershell -Command "Expand-Archive -Path '%zipFile%' -DestinationPath '%destDir%' -Force" +call :assert "Failed to unzip %zipFile% to %destDir%" +ENDLOCAL +exit /b 0 + +:ensure-mongodb +SETLOCAL EnableDelayedExpansion +set "mongodbArchive=%ExternalDir%\.tmp\mongodb.zip" +set "mongodbDir=%ExternalDir%\mongodb\bin" +set "expectedDir=%ExternalDir%\mongodb-win32-x86_64-windows-8.0.10" +set "dlLink=https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-8.0.10.zip" + +if not exist "%mongodbDir%" ( + echo MongoDB not found... + if not exist "%mongodbArchive%" ( + echo Downloading MongoDB... + call :download "%dlLink%" "%mongodbArchive%" + ) + echo Unzipping MongoDB... + call :unzip "%mongodbArchive%" "%ExternalDir%" + if exist "%expectedDir%" ( + move /Y "%expectedDir%" "%ExternalDir%\mongodb" + ) else ( + call :raise "Expected directory %expectedDir% does not exist after unzipping." + ) +) +SET "PATH=%mongodbDir%;%PATH%" +where mongod >nul 2>&1 +call :assert "MongoDB executable not found in PATH after setup." +echo MongoDB setup complete. +ENDLOCAL +exit /b 0 + +:ensure-nodejs +SETLOCAL EnableDelayedExpansion +set "nodejsArchive=%ExternalDir%\.tmp\nodejs.zip" +set "nodejsDir=%ExternalDir%\nodejs" +set "expectedDir=%ExternalDir%\node-v22.16.0-win-x64" +set "dlLink=https://nodejs.org/dist/v22.16.0/node-v22.16.0-win-x64.zip" +if not exist "%nodejsDir%" ( + echo Node.js not found... + if not exist "%nodejsArchive%" ( + echo Downloading Node.js... + call :download "%dlLink%" "%nodejsArchive%" + ) + echo Unzipping Node.js... + call :unzip "%nodejsArchive%" "%ExternalDir%" + if exist "%expectedDir%" ( + move /Y "%expectedDir%" "%ExternalDir%\nodejs" + ) else ( + call :raise "Expected directory %expectedDir% does not exist after unzipping." + ) +) +SET "PATH=%nodejsDir%;%PATH%" +where npm >nul 2>&1 +call :assert "Node.js executable not found in PATH after setup." +echo Node.js setup complete. +ENDLOCAL +exit /b 0 + + +:ensure-git +SETLOCAL EnableDelayedExpansion +set "gitSelfInstall=%ExternalDir%\.tmp\PortableGit-2.49.0-64-bit.7z.exe" +set "gitDir=%ExternalDir%\git" +set "dlLink=https://github.com/git-for-windows/git/releases/download/v2.49.0.windows.1/PortableGit-2.49.0-64-bit.7z.exe" +if "%~1" neq "" ( + where git >nul 2>&1 + if %errorlevel% == 0 ( + echo Using global Git installation... + set "foundGit=1" + ) +) +if not defined foundGit ( + if not exist "%gitDir%" ( + echo Git not found... + if not exist "%gitSelfInstall%" ( + echo Downloading Git... + call :download "%dlLink%" "%gitSelfInstall%" + ) + echo Unzipping Git... + start /b /wait "" "%gitSelfInstall%" -o "%gitDir%" -y + if not exist "%gitDir%" ( + call :raise "Expected directory %gitDir% does not exist after unzipping." + ) + ) + SET "PATH=%gitDir%;%PATH%" + where git >nul 2>&1 + call :assert "Git executable not found in PATH." + echo Git setup complete. +) +ENDLOCAL +exit /b 0 + +:ensure-server +SETLOCAL EnableDelayedExpansion +if exist "%ServerDir%" ( + echo Server repository already exists, updating... + pushd "%ServerDir%" + git pull + @REM call :assert "Failed to update server repository %ServerRepo% in %ServerDir%" + if %errorlevel% neq 0 ( + echo Failed to update server repository in %ServerDir%. + ) + popd + echo Server updated in %ServerDir%. +) else ( + git clone "%ServerRepo%" "%ServerDir%" + call :assert "Failed to clone server repository %ServerRepo% to %ServerDir%" + echo Server repository cloned to %ServerDir%. +) +if not exist "%ServerDir%\config.json" ( + copy "%ServerDir%\config.json.example" "%ServerDir%\config.json" + call :assert "Failed to copy config.json.example to config.json" +) + +ENDLOCAL +exit /b 0 + +:ensure-extras +SETLOCAL EnableDelayedExpansion +set "extrasDir=%ServerDir%\static\data" +set "repoDir=%extrasDir%\0" +set "flagDontDownload=%extrasDir%\.dontDownload" +set "repoUrl=https://openwf.io/0.git" + +if exist "%flagDontDownload%" ( + echo Skipping extra data download as per flag. +) else ( + if exist "%repoDir%" ( + echo Extra data repository already exists, updating... + pushd "%repoDir%" + git pull + @REM call :assert "Failed to update repository %repoUrl% in %repoDir%" + if %errorlevel% neq 0 ( + echo Failed to update repository in %repoDir%. + ) + popd + echo Extra data updated in %repoDir%. + ) else ( + set /p "downloadExtras=Do you want to download extra data? (y/n): " + if /i "!downloadExtras!"=="y" ( + echo Downloading extra data... + git clone "%repoUrl%" "%repoDir%" + call :assert "Failed to clone repository %repoUrl% to %repoDir%" + echo Extra data downloaded to %repoDir%. + ) else ( + echo Skipping extra data download. + echo . > "%flagDontDownload%" + echo If you want to download it later, delete the file "%flagDontDownload%". + ) + ) +) +ENDLOCAL +exit /b 0 + +:ensure-irc +SETLOCAL EnableDelayedExpansion +set "ircPath=%ExternalDir%\IRC\warframe-irc-server.exe" +set "noIrcFlag=%ExternalDir%\.dontDownloadIRC" +set "ircDlLink=https://github.com/Sainan/warframe-irc-server/releases/download/1.3.1/warframe-irc-server.exe" +if exist "%noIrcFlag%" ( + echo Skipping IRC server download as per flag. +) else ( + if exist "%ircPath%" ( + echo IRC server already exists, skipping download. + ) else ( + set /p "downloadIrc=Do you want to download the IRC server? (y/n): " + if /i "!downloadIrc!"=="y" ( + call :download "%ircDlLink%" "%ircPath%" + ) else ( + echo Skipping IRC server download. + echo . > "%noIrcFlag%" + echo If you want to download it later, delete the file "%noIrcFlag%". + ) + ) +) +ENDLOCAL +exit /b 0 + +:main +set gErrorLevel=0 +call :parseArgs %* +echo Usage: %0 [options] +echo Options: +echo -externalDir [path] Set the external directory (default: current script directory\external) +echo -serverDir [path] Set the server directory (default: current script directory\SpaceNinjaServer) +echo -serverRepo [url] Set the server repository URL (default: https://openwf.io/SpaceNinjaServer.git) +echo -databasePath [path] Set the MongoDB data directory (default: externalDir\mongodb-data) +echo -ignorePull Ignore pulling updates for the server repository +echo -irc-options [args] Options to pass to the IRC server executable +echo -db-options [args] Options to pass to the MongoDB server executable +echo -server-options [args] Options to pass to the main server +echo ============================================================ + +if defined ARG[externalDir] ( + set "ExternalDir=%ARG[externalDir]%" +) else ( + set "ExternalDir=%~dp0\external" +) +if defined ARG[serverDir] ( + set "ServerDir=%ARG[serverDir]%" +) else ( + set "ServerDir=%~dp0\SpaceNinjaServer" +) +if defined ARG[serverRepo] ( + set "ServerRepo=%ARG[serverRepo]%" +) else ( + set "ServerRepo=https://openwf.io/SpaceNinjaServer.git" +) +if defined ARG[databasePath] ( + set "DatabasePath=%ARG[databasePath]%" +) else ( + set "DatabasePath=%ExternalDir%\mongodb-data" +) + +echo Using External Directory: %ExternalDir% +echo Using Server Directory: %ServerDir% +echo Using Server Repository: %ServerRepo% +echo Using MongoDB Data Directory: %DatabasePath% + +if not exist "%ExternalDir%" ( + mkdir "%ExternalDir%" +) +call :ensure-mongodb +call :ensure-nodejs +call :ensure-git +call :ensure-server +call :ensure-extras +call :ensure-irc + +pushd "%ServerDir%" +call npm install --omit=dev +call :assert "Failed to install Node.js dependencies." +call npm run build +call :assert "Failed to build the server." +popd + +if exist "%ExternalDir%\IRC\warframe-irc-server.exe" ( + set "hasIrc=1" +) else ( + set "hasIrc=0" +) + +if not exist "%DatabasePath%" ( + mkdir "%DatabasePath%" + call :assert "Failed to create MongoDB data directory %DatabasePath%" +) + +@REM Maybe use docker-compose instead? +if %hasIrc%==1 ( + echo Extra Irc options: %ARG[irc-options]% + if defined ARG[irc-options] set "IRC_OPTIONS=%ARG[irc-options]:"=%" + start /D "%ExternalDir%\IRC" "IRC Server" "%ExternalDir%\IRC\warframe-irc-server.exe" %IRC_OPTIONS% +) +echo Starting MongoDB with options: %ARG[db-options]% +if defined ARG[db-options] set "DB_OPTIONS=%ARG[db-options]:"=%" +start /D "%ExternalDir%" "Database" "%ExternalDir%\mongodb\bin\mongod" --dbpath "%DatabasePath%" %DB_OPTIONS% +echo Starting Main Server with options: %ARG[server-options]% +if defined ARG[server-options] set "SERVER_OPTIONS=%ARG[server-options]:"=%" +@REM start /D "%ServerDir%" "Main Server" "%ExternalDir%\nodejs\npm" run start %SERVER_OPTIONS% +@REM exit /b 0 +pushd "%ServerDir%" +call npm run start %SERVER_OPTIONS% +popd +exit /b %errorlevel% + +:end +ENDLOCAL +pause +exit %gErrorLevel%