Compare commits

...

106 Commits

Author SHA1 Message Date
github-actions[bot]
b6d231e708 chore: add VERSION v0.5.1 2025-12-01 09:35:22 +00:00
Michel Roegl-Brunner
7c4683012f Update update.sh 2025-12-01 10:29:41 +01:00
github-actions[bot]
cfcdc1e342 chore: add VERSION v0.5.1 (#361)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-11-29 15:57:44 +00:00
Michel Roegl-Brunner
07cf03a408 Merge pull request #360 from community-scripts/feat/clone_lxc_vm
feat: Add VM/LXC cloning functionality
2025-11-29 16:56:48 +01:00
Michel Roegl-Brunner
dd17d2cbec feat: Add VM/LXC cloning functionality
- Add CloneCountInputModal component for specifying clone count
- Implement clone handlers and state management in InstalledScriptsTab
- Add clone menu item to ScriptInstallationCard
- Extend StorageSelectionModal to support clone storage selection (rootdir only)
- Add clone terminal support to Terminal component
- Implement startSSHCloneExecution in server.js with sequential ID retrieval
- Add clone-related API endpoints (getClusterNextId, getContainerType, getCloneStorages, generateCloneHostnames, executeClone, addClonedContainerToDatabase)
- Integrate with VM/LXC detection from main branch
- Fix storage fetching to use correct serverId parameter
- Fix clone execution to pass storage parameter correctly
- Remove unused eslint-disable comments
2025-11-29 16:53:58 +01:00
Michel Roegl-Brunner
f3d14c6746 Merge pull request #359 from community-scripts/fix/352
fix: align toggle switches in repository settings
2025-11-29 16:14:07 +01:00
Michel Roegl-Brunner
447332e558 fix: align toggle switches in repository settings
- Remove fixed label width from Toggle component
- Move delete button to left of toggle switches
- Add matching border/padding to 'Enable after adding' section to align with repository items
- Ensure all toggles have consistent right-side alignment
2025-11-29 16:12:20 +01:00
Michel Roegl-Brunner
9bbc19ae44 Merge pull request #358 from community-scripts/fix/357_356
fix: Add dynamic text to container control loading modal
2025-11-29 16:00:49 +01:00
Michel Roegl-Brunner
5564ae0393 fix: add dynamic text to container control loading modal
- Update LoadingModal to display action text (Starting/Stopping LXC/VM)
- Update handleStartStop to include container type (LXC/VM) in action text
- Show clear feedback when starting or stopping containers
2025-11-29 15:58:30 +01:00
Michel Roegl-Brunner
93d7842f6c feat: implement batch container type detection for performance optimization
- Add batchDetectContainerTypes() helper function that uses pct list and qm list to detect all container types in 2 SSH calls per server
- Update getAllInstalledScripts to use batch detection instead of individual isVM() calls per script
- Update getInstalledScriptsByServer to use batch detection for single server
- Update database queries to include lxc_config relation for fallback detection
- Fix isVM() function to properly default to LXC when VM config doesn't exist
- Significantly improves performance: reduces from N SSH calls per script to 2 SSH calls per server
2025-11-29 15:55:43 +01:00
Michel Roegl-Brunner
84c02048bc Fix a false detection as a VM when it is a LXC 2025-11-29 15:41:49 +01:00
github-actions[bot]
66a3bb3203 chore: add VERSION v0.5.0 (#355)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-11-28 14:04:05 +00:00
Michel Roegl-Brunner
0da802be42 Merge pull request #354 from community-scripts/bugfixing_bumps
Add TypeScript Runtime Support and add Prisma 7 Compatibility
2025-11-28 14:56:43 +01:00
CanbiZ
5bc3933d11 fix gh action runner 2025-11-28 14:48:38 +01:00
CanbiZ
1c6d1ac120 fix gh action 2025-11-28 14:48:06 +01:00
CanbiZ
ba1e6478d7 Update package-lock.json - add tsx to lock file 2025-11-28 14:46:18 +01:00
CanbiZ
e3af248456 Merge main into bugfixing_bumps - keep bugfixing_bumps versions (Prisma 7, tsx support) 2025-11-28 14:43:25 +01:00
CanbiZ
43bafb610f Add initializeRepositories to autoSyncInit.ts 2025-11-28 14:34:28 +01:00
CanbiZ
8e22568efb Add detailed logging for autoSyncInit dynamic import and function checks 2025-11-28 14:33:11 +01:00
CanbiZ
6bb9ed5182 Use dynamic import for autoSyncInit to avoid tsx caching issues 2025-11-28 14:30:43 +01:00
CanbiZ
b6c3954f98 Fix start script to use tsx for Prisma 7 TypeScript support 2025-11-28 14:14:16 +01:00
CanbiZ
f73b303172 Use node --import tsx for better TypeScript support 2025-11-28 14:11:42 +01:00
CanbiZ
50d066669e Fix db.js - add .ts extension for Prisma client import 2025-11-28 14:11:09 +01:00
CanbiZ
68541c0046 Use tsx to run server.js - enables TypeScript imports for Prisma 7 2025-11-28 14:09:25 +01:00
CanbiZ
644222e958 Fix db.js - require index.js explicitly 2025-11-28 14:08:39 +01:00
CanbiZ
31a5fd97d4 Fix db.js - use absolute path to .prisma/client 2025-11-28 14:07:54 +01:00
CanbiZ
b54fbf15f6 Fix db.js - use createRequire to load .prisma/client CJS module 2025-11-28 14:07:22 +01:00
CanbiZ
a787e60e7c Fix db.js - import from .prisma/client runtime location 2025-11-28 14:06:35 +01:00
CanbiZ
1e250306dc Fix db.js - use CommonJS-style import for @prisma/client 2025-11-28 14:05:37 +01:00
CanbiZ
d64a296ebe Fix db.js - import PrismaClient from @prisma/client for Node ESM compatibility 2025-11-28 14:04:58 +01:00
CanbiZ
691b27c924 Fix db.js import - add .js extension for Prisma client 2025-11-28 14:04:05 +01:00
CanbiZ
dbc591aa63 Add JS wrappers for ESM compatibility (repositoryService, githubJsonService) and fix .js import extensions 2025-11-28 14:01:18 +01:00
CanbiZ
5ea6828f8c Fix ESM import in database-prisma.js - add .js extension 2025-11-28 13:58:05 +01:00
CanbiZ
3dabacd055 Revert "Auto-initialize default repositories on first API call"
This reverts commit aebc8a6171.
2025-11-28 13:56:29 +01:00
CanbiZ
e8ee829577 Add explicit type annotations and return types
This commit adds TypeScript type definitions for database entities and updates all methods in DatabaseServicePrisma to use explicit type annotations and return types. This improves type safety, code clarity, and maintainability by ensuring consistent return values and better integration with Prisma-generated types.
2025-11-28 13:50:44 +01:00
CanbiZ
aebc8a6171 Auto-initialize default repositories on first API call 2025-11-28 13:41:21 +01:00
CanbiZ
c5db169441 Fix login race condition: don't call checkAuth after successful login 2025-11-28 13:34:51 +01:00
CanbiZ
bef5bef875 Remove .js extensions from dynamic imports 2025-11-28 13:32:51 +01:00
CanbiZ
3a4f86942f Remove circular import wrapper files (githubJsonService.js, localScripts.js) 2025-11-28 13:30:17 +01:00
CanbiZ
94eb772467 Fix cross-origin cookie issues: use lax SameSite and add CORS headers 2025-11-28 13:28:53 +01:00
CanbiZ
3a2a1b2cd6 Add datasource property to Prisma config for migrate/db push support 2025-11-28 13:25:33 +01:00
CanbiZ
69c10b05ac Ensure recursive creation of ssh-keys directory
Updated the initialization logic to use the 'recursive' option when creating the data/ssh-keys directory, ensuring parent directories are created if they do not exist.
2025-11-28 13:23:31 +01:00
Michel Roegl-Brunner
7833d5d408 Fix type errors 2025-11-28 13:21:37 +01:00
CanbiZ
e0baa79d6b Remove file extensions from import statements
Updated import statements across several server files to omit explicit file extensions. This improves compatibility with module resolution and aligns with common import practices.
2025-11-28 13:21:03 +01:00
CanbiZ
737c9c94f3 Fix repositoryService import extension in autoSyncInit.js 2025-11-28 13:19:53 +01:00
CanbiZ
c57586acae Fix Prisma import path in db.js and update allowedDevOrigins format 2025-11-28 13:18:29 +01:00
CanbiZ
74030b5806 Refactor lint comments and minor code improvements
Removed or updated unnecessary eslint-disable comments across several server and service files to improve code clarity. Fixed import paths and added TypeScript ignore comments where needed for compatibility. Minor formatting adjustments were made for readability.
2025-11-28 13:13:01 +01:00
CanbiZ
cc276ddff3 Improve script downloader and auto-sync services
Added detailed JSDoc comments and type annotations to ScriptDownloaderService for better maintainability and clarity. Refactored initialization logic to always set scriptsDirectory and repoUrl. Enhanced autoSyncService to specify cron job timezone and options. Updated PrismaClient import path for compatibility with generated client structure.
2025-11-28 13:10:20 +01:00
CanbiZ
375c551a3a Update Prisma to v7 and add better-sqlite3 adapter
Upgraded Prisma and related dependencies to version 7.0.1 and added @prisma/adapter-better-sqlite3 and better-sqlite3 to support the new adapter. This enables improved SQLite integration and compatibility with the latest Prisma features.
2025-11-28 13:06:40 +01:00
CanbiZ
e3e4556f83 Add type annotations and improve script services
Enhanced type safety and documentation in several files, including adding explicit type annotations for script objects and function parameters. Improved error handling and code clarity in scriptDownloader.js, and updated autoSyncService.js to remove unnecessary cron job options. Refactored prisma.config.ts for schema configuration and updated server.js to support backup storage and improve parameter defaults.
2025-11-28 13:06:16 +01:00
CanbiZ
7fa132e09c Update Prisma adapter and type annotations
Replaces usage of PrismaBetterSQLite3 with PrismaBetterSqlite3 for consistency and correct casing. Updates type annotations in several components and API router for improved type safety. Also adjusts PrismaClient import paths in db files.
2025-11-28 13:02:34 +01:00
CanbiZ
1a1dbe6975 upgrade betterauthsql 2025-11-28 12:57:56 +01:00
CanbiZ
1a5881c935 Migration to Prisma 7 2025-11-28 12:55:32 +01:00
CanbiZ
2d7176914e Enable JS type checking in tsconfig.json
Set `checkJs` to true to allow type checking for JavaScript files. Updated include/exclude patterns to support JS and CJS files in the project.
2025-11-28 12:51:07 +01:00
CanbiZ
987ac3da1b Fix: Remove .js extensions from TypeScript imports for Next.js bundler 2025-11-28 12:48:10 +01:00
CanbiZ
03e31d66a7 Refactor type usage and improve data normalization
Updated several components to use explicit TypeScript types for better type safety. Normalized appriseUrls to always be an array in auto-sync settings API. Improved handling of optional server_id in BackupsTab and adjusted IP detection logic in InstalledScriptsTab. Removed unnecessary eslint-disable comments and improved code clarity in various places.
2025-11-28 12:47:09 +01:00
CanbiZ
7547dff67d Fix type annotations and module imports
Added explicit type annotations to array mapping functions for better type safety. Updated incorrect TypeScript import extensions from .ts to .js for compatibility. Ensured default values for optional parameters and improved code clarity in API routers.
2025-11-28 12:29:15 +01:00
CanbiZ
1945b14694 Update ESLint config to use FlatCompat and expand ignores
Switched to using FlatCompat for ESLint configuration and extended the ignore list to include 'next-env.d.ts', 'postcss.config.js', and 'prettier.config.js'. This improves compatibility and prevents linting of config and environment files.
2025-11-28 12:23:47 +01:00
CanbiZ
ec23600861 Switch ESLint config to eslint-config-next/core-web-vitals
Replaces @eslint/eslintrc and FlatCompat with eslint-config-next/core-web-vitals for ESLint configuration. Updates linting scripts in package.json to use eslint directly instead of next lint. Removes @eslint/eslintrc from devDependencies.
2025-11-28 12:20:53 +01:00
CanbiZ
41a9c0ae11 Switch to ESLint CLI (Deprecation of ESLint)
Added and updated eslint-disable comments in repositoryService, restoreService, and storageService to cover additional TypeScript rules. Simplified error handling by removing unused variables and catch parameters, and removed unused imports and variables in restoreService for cleaner code.
2025-11-28 12:19:47 +01:00
CanbiZ
c266c4cb3c Refactor InstalledScriptsTab for code style consistency
Updated InstalledScriptsTab.tsx to use double quotes and consistent formatting throughout the file. Improved type annotations, code readability, and standardized state initialization and mutation usage. No functional changes were made; this is a style and maintainability refactor.
2025-11-28 12:16:18 +01:00
CanbiZ
b5bce88398 Refactor InstalledScriptsTab for code style consistency
Standardizes quote usage, formatting, and code style in InstalledScriptsTab.tsx. Improves readability and maintains consistent conventions across the file without changing logic or functionality.
2025-11-28 12:15:27 +01:00
CanbiZ
48cf86a449 Refactor nullish checks and add type safety
Replaces many uses of logical OR (||) with nullish coalescing (??) for more accurate handling of undefined/null values. Adds explicit type annotations and interfaces to improve type safety, especially in API routes and server-side code. Updates SSH connection test handling and config parsing in installedScripts router for better reliability. Minor fixes to deduplication logic, cookie handling, and error reporting.
2025-11-28 12:10:15 +01:00
CanbiZ
d40aeb6c82 Refactor scripts grid and filter handling for robustness
Improves type safety and normalization in filter, repository, and script status handling across multiple components. Refactors ScriptsGrid for better readability, deduplication, and error messaging, and updates UI markup for consistency. Also adds explicit types for auto-sync settings and ensures string conversion for credential fields.
2025-11-28 11:58:38 +01:00
CanbiZ
9c759ba99b fix: ESLint/TypeScript fixes - nullish coalescing, regexp-exec, optional-chain, unescaped-entities, unused-vars, type-safety 2025-11-28 11:53:04 +01:00
root
f467b9ad7b fix vulnerabilities 2025-11-28 11:49:39 +01:00
Michel Roegl-Brunner
7fe2a8b453 Merge pull request #353 from community-scripts/fix/vm_detection
Add VM status check and UI improvements
2025-11-28 11:46:39 +01:00
Michel Roegl-Brunner
5274737ab8 Add VM status check and UI improvements
- Add VM status checking using qm status command
- Hide update button for VMs (only show for LXC containers)
- Hide shell button for VMs (only show for LXC containers)
- Hide LXC Settings option for VMs
- Display VM/LXC indicator badges in table before script names
- Update statistics cards to differentiate between LXC and VMs
- Update container control to support both pct (LXC) and qm (VM) commands
- Improve status parsing to handle both container types
2025-11-28 11:44:58 +01:00
CanbiZ
40805f39f7 Update dependencies and adjust TypeScript JSX setting
Upgraded multiple dependencies and devDependencies in package.json to their latest versions for improved stability and features. Changed the TypeScript 'jsx' compiler option from 'react-jsx' to 'preserve' in tsconfig.json to better align with project requirements.
2025-11-28 11:44:26 +01:00
Michel Roegl-Brunner
f9af7536d0 Update Confirmation modal 2025-11-28 11:27:12 +01:00
Michel Roegl-Brunner
0d39a9bbd0 Update update.sh to inlcude node update 2025-11-26 11:33:26 +01:00
Michel Roegl-Brunner
66f8a84260 Various small fixes (#349)
* Fix script viewer to support vm/ and tools/ scripts

- Update ScriptDetailModal to extract scriptName from any path (ct/, vm/, tools/)
- Refactor TextViewer to use actual script paths from install_methods
- Remove hardcoded path assumptions and use dynamic script paths
- Only show Install Script tab for ct/ scripts that have install scripts
- Rename CT Script tab to Script for better clarity

* Fix downloaded scripts count to include vm/ and tools/ scripts

- Update matching logic to use same robust approach as DownloadedScriptsTab
- Add normalized slug matching to handle filename-based slugs vs JSON slugs
- Add multiple fallback matching strategies for better script detection
- Fixes issue where scripts in vm/ and tools/ directories weren't being counted

* Filter categories to only show those with scripts

- Add filter to exclude categories with count 0 from category sidebar
- Only categories with at least one script will be displayed
- Reduces UI clutter by hiding empty categories

* Fix intermittent page reloads from VersionDisplay reconnect logic

- Add guards to prevent reload when not updating
- Use refs to track isUpdating and isNetworkError state in interval callbacks
- Add hasReloadedRef flag to prevent multiple reloads
- Clear reconnect interval when update completes or component unmounts
- Only start reconnect attempts when actually updating
- Prevents false positive reloads when server responds normally

* Fix Next.js HMR WebSocket and static asset handling

- Add WebSocket upgrade detection to only intercept /ws/script-execution
- Pass all other WebSocket upgrades (including HMR) to Next.js handler
- Ensure _next routes and static assets are properly handled by Next.js
- Fixes 400 errors for Next.js HMR WebSocket connections
- Fixes 403 errors for static assets by ensuring proper routing

* Fix WebSocket upgrade handling to properly route Next.js HMR

- Create WebSocketServer with noServer: true to avoid auto-attaching
- Manually handle upgrade events to route /ws/script-execution to our WebSocketServer
- Route all other WebSocket upgrades (including Next.js HMR) to Next.js handler
- This ensures Next.js HMR WebSocket connections are properly handled
- Fixes 400 errors for /_next/webpack-hmr WebSocket connections

* Revert WebSocket handling to simpler approach

- Go back to attaching WebSocketServer directly with path option
- Remove manual upgrade event handling that was causing errors
- The path option should filter to only /ws/script-execution
- Next.js should handle its own HMR WebSocket upgrades naturally

* Fix WebSocket upgrade handling to preserve Next.js HMR handlers

- Save existing upgrade listeners before adding our own
- Call existing listeners for non-matching paths to allow Next.js HMR
- Only handle /ws/script-execution ourselves
- This ensures Next.js can handle its own WebSocket upgrades for HMR

* Fix random page reloads during normal app usage

- Memoize startReconnectAttempts with useCallback to prevent recreation on every render
- Fix useEffect dependency arrays to include memoized function
- Add stricter guards checking refs before starting reconnect attempts
- Ensure reconnect logic only runs when actually updating (not during normal usage)
- Add early return in fallback useEffect to prevent false triggers
- Add ref guards in ResyncButton to prevent multiple simultaneous sync operations
- Only reload after sync if it was user-initiated

* Fix critical bug: prevent reloads from stale updateLogsData.isComplete

- Add isUpdating guard before processing updateLogsData.isComplete
- Reset shouldSubscribe when update completes or fails
- Prevent stale isComplete data from triggering reloads during normal usage

* Add update confirmation modal with changelog display

- Add UpdateConfirmationModal component that shows changelog before update
- Modify getVersionStatus to include release body (changelog) in response
- Update VersionDisplay to show confirmation modal instead of starting update directly
- Users must review changelog and click 'Proceed with Update' to start update
- Ensures users see potential breaking changes before updating
2025-11-26 10:21:14 +01:00
Michel Roegl-Brunner
2a9921a4e1 Merge pull request #348 from community-scripts/fix/update_script
fix: Detect script changes from remote repository to allow Script updates
2025-11-26 08:43:27 +01:00
Michel Roegl-Brunner
50f657ba00 Update Node.js version to 24.x in workflow 2025-11-26 08:33:50 +01:00
Michel Roegl-Brunner
5d5eba72de fix: detect script changes from remote repository
- Add refetchOnMount and staleTime: 0 to compareScriptContent query to bypass React Query cache
- Add visible refresh button in script detail modal to manually check for updates
- Improve comparison error handling and logging for better debugging
- Display error messages in UI when comparison fails
- Ensure comparison always checks remote repository when modal opens
2025-11-26 08:32:13 +01:00
Michel Roegl-Brunner
577b96518e package-lock.json 2025-11-26 08:19:39 +01:00
Michel Roegl-Brunner
c6c27271d6 Merge pull request #342 from community-scripts/node24_securityfix 2025-11-24 21:34:06 +01:00
ProxmoxVE Developer
72c0246d8c chore(deps): upgrade to Next.js 16, Vitest 4, and Node.js 24
BREAKING CHANGES:
- Upgrade Next.js from 15.1.6 to 16.0.4
- Use Webpack instead of Turbopack for compatibility with server-side modules
- Upgrade Node.js requirement to >=24.0.0

FEATURES:
- Upgrade Vitest to 4.0.13 with improved testing capabilities
- Update all vitest-related packages (@vitest/ui, @vitest/coverage-v8)
- Upgrade react-syntax-highlighter to 16.1.0
- Update node-cron to 4.2.1
- Update lucide-react to 0.554.0

SECURITY:
- Resolve glob command injection vulnerability (CVE) via v10.5.0
- Fix all npm audit vulnerabilities (0 vulnerabilities found)
- Update prisma/client to 6.19.0
- Update all dependencies to latest secure versions

FIXES:
- Configure next.config.js for webpack with proper server-side module handling
- Update tsconfig.json for Next.js 16 compatibility (jsx: react-jsx)
- Add engines field to require Node.js >=24.0.0
- Remove deprecated webpack config in favor of Next.js 16 compatibility
2025-11-24 21:27:38 +01:00
github-actions[bot]
06d4786e0a chore: add VERSION v0.4.13 (#341)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-11-24 20:08:21 +00:00
CanbiZ
bc31896586 core: remove uv cache clean | remove log rotation (#340)
* core: remove uv cache clean

* Initialize functions on core.func source

Added function initialization call when sourcing core.func
2025-11-24 20:52:31 +01:00
github-actions[bot]
213a606fc0 chore: add VERSION v0.4.12 (#336)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-11-20 17:59:41 +00:00
CanbiZ
3579f2258e Update supported Proxmox VE versions to include 9.1 (#335)
The pve_check function now allows Proxmox VE 9.1 in addition to 9.0 and 8.x. Error messages and comments have been updated to reflect the expanded support.
2025-11-20 18:57:55 +01:00
Michel Roegl-Brunner
5b861ade05 add missing api.func to FUNCTIONS_FILE_PATH 2025-11-18 09:26:39 +01:00
Michel Roegl-Brunner
553eae6ce7 docs: add LXC Backups section to help modal 2025-11-18 09:22:13 +01:00
Michel Roegl-Brunner
c2ca88f033 Merge pull request #328 from community-scripts/dependabot/npm_and_yarn/types/node-24.10.1
build(deps-dev): Bump @types/node from 24.9.1 to 24.10.1
2025-11-18 09:20:49 +01:00
Michel Roegl-Brunner
67d44a6a5f Merge pull request #329 from community-scripts/dependabot/npm_and_yarn/eslint-9.39.1
build(deps-dev): Bump eslint from 9.38.0 to 9.39.1
2025-11-18 09:20:40 +01:00
Michel Roegl-Brunner
fe6cca5c63 Merge pull request #331 from community-scripts/feat/lxc_backups
feat: Add LXC container backup functionality
2025-11-18 09:20:21 +01:00
Michel Roegl-Brunner
3a8088ded6 chore: add missing migration for backups and pbs_storage_credentials tables 2025-11-18 09:16:31 +01:00
Michel Roegl-Brunner
5d48c7b61c Merge branch 'main' into feat/lxc_backups 2025-11-18 09:15:03 +01:00
Michel Roegl-Brunner
5be88d361f chore: cleanup debug output from backup modals 2025-11-18 09:11:56 +01:00
dependabot[bot]
014e5b69e9 build(deps-dev): Bump eslint from 9.38.0 to 9.39.1
Bumps [eslint](https://github.com/eslint/eslint) from 9.38.0 to 9.39.1.
- [Release notes](https://github.com/eslint/eslint/releases)
- [Commits](https://github.com/eslint/eslint/compare/v9.38.0...v9.39.1)

---
updated-dependencies:
- dependency-name: eslint
  dependency-version: 9.39.1
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-11-17 19:48:23 +00:00
dependabot[bot]
f37b2cb26f build(deps-dev): Bump @types/node from 24.9.1 to 24.10.1
Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 24.9.1 to 24.10.1.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node)

---
updated-dependencies:
- dependency-name: "@types/node"
  dependency-version: 24.10.1
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-11-17 19:48:15 +00:00
Michel Roegl-Brunner
81c00f5d40 Merge pull request #327 from community-scripts/dependabot/npm_and_yarn/npm_and_yarn-3c67cbb9cd 2025-11-16 23:21:24 +01:00
dependabot[bot]
9bae95d0aa build(deps-dev): Bump js-yaml
Bumps the npm_and_yarn group with 1 update in the / directory: [js-yaml](https://github.com/nodeca/js-yaml).


Updates `js-yaml` from 4.1.0 to 4.1.1
- [Changelog](https://github.com/nodeca/js-yaml/blob/master/CHANGELOG.md)
- [Commits](https://github.com/nodeca/js-yaml/compare/4.1.0...4.1.1)

---
updated-dependencies:
- dependency-name: js-yaml
  dependency-version: 4.1.1
  dependency-type: indirect
  dependency-group: npm_and_yarn
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-11-16 10:12:09 +00:00
Michel Roegl-Brunner
088d354fd2 Merge pull request #322 from community-scripts/dependabot/npm_and_yarn/jsdom-27.2.0 2025-11-16 11:11:11 +01:00
Michel Roegl-Brunner
0d47fa5d2a Merge pull request #324 from community-scripts/dependabot/npm_and_yarn/types/react-19.2.4 2025-11-16 11:10:59 +01:00
dependabot[bot]
57fd5f802b build(deps-dev): Bump @types/react from 19.2.2 to 19.2.4
Bumps [@types/react](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/react) from 19.2.2 to 19.2.4.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/react)

---
updated-dependencies:
- dependency-name: "@types/react"
  dependency-version: 19.2.4
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-11-14 19:24:46 +00:00
dependabot[bot]
a1825302fa build(deps-dev): Bump jsdom from 27.1.0 to 27.2.0
Bumps [jsdom](https://github.com/jsdom/jsdom) from 27.1.0 to 27.2.0.
- [Release notes](https://github.com/jsdom/jsdom/releases)
- [Changelog](https://github.com/jsdom/jsdom/blob/main/Changelog.md)
- [Commits](https://github.com/jsdom/jsdom/compare/27.1.0...27.2.0)

---
updated-dependencies:
- dependency-name: jsdom
  dependency-version: 27.2.0
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-11-14 19:24:26 +00:00
Michel Roegl-Brunner
570eea41b9 Implement real-time restore progress updates with polling
- Add restore.log file writing in restoreService.ts for progress tracking
- Create getRestoreProgress query endpoint for polling restore logs
- Implement polling-based progress updates in BackupsTab (1 second interval)
- Update LoadingModal to display all progress logs with auto-scroll
- Remove console.log debug output from restoreService
- Add static 'Restore in progress' text under spinner
- Show success checkmark when restore completes
- Prevent modal dismissal during restore, allow ESC/X button when complete
- Remove step prefixes from log messages for cleaner output
- Keep success/error modals open until user dismisses manually
2025-11-14 15:43:33 +01:00
Michel Roegl-Brunner
33a5b8e4d0 PBS restore working :) 2025-11-14 15:19:34 +01:00
Michel Roegl-Brunner
63174d2ea1 Fix PBS backup discovery command and authentication
- Fix PBS login to use PBS_PASSWORD environment variable instead of stdin
- Change backup discovery command from 'snapshots host/<CT_ID>' to 'snapshot list ct/<CT_ID>'
- Use full repository string (root@pam@IP:DATASTORE) instead of storage name
- Parse table format output correctly (snapshot | size | files)
- Extract snapshot name, size, and date from table output
- Convert size units (MiB/GiB) to bytes for storage
- Fix TypeScript errors with proper null checks
2025-11-14 13:21:53 +01:00
Michel Roegl-Brunner
eda41e5101 Implement PBS authentication support for backup discovery
- Add PBSStorageCredential model to database schema (fingerprint now required)
- Create PBS credentials API router with CRUD operations
- Add PBS login functionality to backup service before discovery
- Create PBSCredentialsModal component for managing credentials
- Integrate PBS credentials management into ServerStoragesModal
- Update storage service to extract PBS IP and datastore info
- Add helpful hint about finding fingerprint on PBS dashboard
- Auto-accept fingerprint during login using stored credentials
2025-11-14 13:12:39 +01:00
Michel Roegl-Brunner
4a50da4968 Add backup discovery tab with support for local and storage backups
- Add Backup model to Prisma schema with fields for container_id, server_id, hostname, backup info
- Create backupService with discovery methods for local (/var/lib/vz/dump/) and storage (/mnt/pve/<storage>/dump/) backups
- Add database methods for backup CRUD operations and grouping by container
- Create backupsRouter with getAllBackupsGrouped and discoverBackups procedures
- Add BackupsTab component with collapsible cards grouped by CT_ID and hostname
- Integrate backups tab into main page navigation
- Filter storages by node hostname matching to only show applicable storages
- Skip PBS backups discovery (temporarily disabled)
- Add comprehensive logging for backup discovery process
2025-11-14 13:04:59 +01:00
Michel Roegl-Brunner
d50ea55e6d Add LXC container backup functionality
- Add backup capability before updates or as standalone action
- Implement storage service to fetch and parse backup-capable storages from PVE nodes
- Add backup storage selection modal for user choice
- Support backup+update flow with sequential execution
- Add standalone backup option in Actions menu
- Add storage viewer in server section to show available storages
- Parse /etc/pve/storage.cfg to identify backup-capable storages
- Cache storage data for performance
- Handle backup failures gracefully (warn but allow update to proceed)
2025-11-14 10:30:27 +01:00
Michel Roegl-Brunner
f558aa4f43 Fix selectedRepositories undefined error with generic filter validation (#321)
- Create filterUtils.ts with getDefaultFilters() and mergeFiltersWithDefaults()
- Update ScriptsGrid, DownloadedScriptsTab, and FilterBar to use utility functions
- Prevents crashes when loading old saved filters missing new properties
- Future-proof: new filter properties automatically get defaults
- Fixes TypeError: can't access property 'length', selectedRepositories is undefined
2025-11-14 09:37:13 +01:00
Michel Roegl-Brunner
4ea49be97d Initial for Backup function 2025-11-14 08:44:33 +01:00
77 changed files with 17059 additions and 6065 deletions

View File

@@ -16,7 +16,7 @@ jobs:
strategy: strategy:
matrix: matrix:
node-version: [22.x] node-version: [24.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/ # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps: steps:

3
.gitignore vendored
View File

@@ -16,6 +16,9 @@
db.sqlite db.sqlite
data/settings.db data/settings.db
# prisma generated client
/prisma/generated/
# ssh keys (sensitive) # ssh keys (sensitive)
data/ssh-keys/ data/ssh-keys/

View File

@@ -1 +1 @@
0.4.11 0.5.1

View File

@@ -1,15 +1,23 @@
import { FlatCompat } from "@eslint/eslintrc"; import eslintPluginNext from "@next/eslint-plugin-next";
import tseslint from "typescript-eslint"; import tseslint from "typescript-eslint";
import reactPlugin from "eslint-plugin-react";
const compat = new FlatCompat({ import reactHooksPlugin from "eslint-plugin-react-hooks";
baseDirectory: import.meta.dirname,
});
export default tseslint.config( export default tseslint.config(
{ {
ignores: [".next"], ignores: [".next", "next-env.d.ts", "postcss.config.js", "prettier.config.js"],
},
{
plugins: {
"@next/next": eslintPluginNext,
"react": reactPlugin,
"react-hooks": reactHooksPlugin,
},
rules: {
...eslintPluginNext.configs.recommended.rules,
...eslintPluginNext.configs["core-web-vitals"].rules,
},
}, },
...compat.extends("next/core-web-vitals"),
{ {
files: ["**/*.ts", "**/*.tsx"], files: ["**/*.ts", "**/*.tsx"],
extends: [ extends: [

View File

@@ -18,31 +18,25 @@ const config = {
}, },
], ],
}, },
// Allow cross-origin requests from local network ranges // Allow cross-origin requests from local network in dev mode
allowedDevOrigins: [ // Note: In Next.js 16, we disable this check entirely for dev
'http://localhost:3000', async headers() {
'http://127.0.0.1:3000', return [
'http://[::1]:3000', {
'http://10.*', source: '/:path*',
'http://172.16.*', headers: [
'http://172.17.*', { key: 'Access-Control-Allow-Origin', value: '*' },
'http://172.18.*', { key: 'Access-Control-Allow-Methods', value: 'GET,POST,PUT,DELETE,OPTIONS' },
'http://172.19.*', { key: 'Access-Control-Allow-Headers', value: 'Content-Type, Authorization' },
'http://172.20.*', ],
'http://172.21.*', },
'http://172.22.*', ];
'http://172.23.*', },
'http://172.24.*',
'http://172.25.*',
'http://172.26.*',
'http://172.27.*',
'http://172.28.*',
'http://172.29.*',
'http://172.30.*',
'http://172.31.*',
'http://192.168.*',
],
turbopack: {
// Disable Turbopack and use Webpack instead for compatibility
// This is necessary for server-side code that uses child_process
},
webpack: (config, { dev, isServer }) => { webpack: (config, { dev, isServer }) => {
if (dev && !isServer) { if (dev && !isServer) {
config.watchOptions = { config.watchOptions = {
@@ -50,15 +44,18 @@ const config = {
aggregateTimeout: 300, aggregateTimeout: 300,
}; };
} }
// Handle server-side modules
if (isServer) {
config.externals = config.externals || [];
if (!config.externals.includes('child_process')) {
config.externals.push('child_process');
}
}
return config; return config;
}, },
// Ignore ESLint errors during build (they can be fixed separately) // TypeScript errors will fail the build
eslint: {
ignoreDuringBuilds: true,
},
// Ignore TypeScript errors during build (they can be fixed separately)
typescript: { typescript: {
ignoreBuildErrors: true, ignoreBuildErrors: false,
}, },
}; };

3783
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -4,17 +4,20 @@
"private": true, "private": true,
"type": "module", "type": "module",
"scripts": { "scripts": {
"build": "next build", "build": "prisma generate && next build --webpack",
"check": "next lint && tsc --noEmit", "check": "eslint . && tsc --noEmit",
"dev": "next dev", "dev": "next dev --webpack",
"dev:server": "node server.js", "dev:server": "node --import tsx server.js",
"dev:next": "next dev", "dev:next": "next dev --webpack",
"format:check": "prettier --check \"**/*.{ts,tsx,js,jsx,mdx}\" --cache", "format:check": "prettier --check \"**/*.{ts,tsx,js,jsx,mdx}\" --cache",
"format:write": "prettier --write \"**/*.{ts,tsx,js,jsx,mdx}\" --cache", "format:write": "prettier --write \"**/*.{ts,tsx,js,jsx,mdx}\" --cache",
"lint": "next lint", "generate": "prisma generate",
"lint:fix": "next lint --fix", "lint": "eslint .",
"lint:fix": "eslint --fix .",
"migrate": "prisma migrate dev",
"preview": "next build && next start", "preview": "next build && next start",
"start": "node server.js", "postinstall": "prisma generate",
"start": "node --import tsx server.js",
"test": "vitest", "test": "vitest",
"test:ui": "vitest --ui", "test:ui": "vitest --ui",
"test:run": "vitest run", "test:run": "vitest run",
@@ -22,76 +25,82 @@
"typecheck": "tsc --noEmit" "typecheck": "tsc --noEmit"
}, },
"dependencies": { "dependencies": {
"@prisma/client": "^6.18.0", "@prisma/adapter-better-sqlite3": "^7.0.1",
"@prisma/client": "^7.0.1",
"@radix-ui/react-dropdown-menu": "^2.1.16", "@radix-ui/react-dropdown-menu": "^2.1.16",
"@radix-ui/react-slot": "^1.2.3", "@radix-ui/react-slot": "^1.2.4",
"@t3-oss/env-nextjs": "^0.13.8", "@t3-oss/env-nextjs": "^0.13.8",
"@tailwindcss/typography": "^0.5.19", "@tailwindcss/typography": "^0.5.19",
"@tanstack/react-query": "^5.90.5", "@tanstack/react-query": "^5.90.11",
"@trpc/client": "^11.6.0", "@trpc/client": "^11.7.2",
"@trpc/react-query": "^11.6.0", "@trpc/react-query": "^11.7.2",
"@trpc/server": "^11.6.0", "@trpc/server": "^11.7.2",
"@types/react-syntax-highlighter": "^15.5.13", "@types/react-syntax-highlighter": "^15.5.13",
"@types/ws": "^8.18.1", "@types/ws": "^8.18.1",
"@xterm/addon-fit": "^0.10.0", "@xterm/addon-fit": "^0.10.0",
"@xterm/addon-web-links": "^0.11.0", "@xterm/addon-web-links": "^0.11.0",
"@xterm/xterm": "^5.5.0", "@xterm/xterm": "^5.5.0",
"axios": "^1.7.9", "axios": "^1.13.2",
"bcryptjs": "^3.0.2", "bcryptjs": "^3.0.3",
"better-sqlite3": "^12.4.6",
"class-variance-authority": "^0.7.1", "class-variance-authority": "^0.7.1",
"clsx": "^2.1.1", "clsx": "^2.1.1",
"cron-validator": "^1.2.0", "cron-validator": "^1.4.0",
"dotenv": "^17.2.3", "dotenv": "^17.2.3",
"jsonwebtoken": "^9.0.2", "jsonwebtoken": "^9.0.2",
"lucide-react": "^0.553.0", "lucide-react": "^0.555.0",
"next": "^15.1.6", "next": "^16.0.5",
"node-cron": "^3.0.3", "node-cron": "^4.2.1",
"node-pty": "^1.0.0", "node-pty": "^1.0.0",
"react": "^19.0.0", "react": "^19.2.0",
"react-dom": "^19.0.0", "react-dom": "^19.2.0",
"react-markdown": "^10.1.0", "react-markdown": "^10.1.0",
"react-syntax-highlighter": "^15.6.6", "react-syntax-highlighter": "^16.1.0",
"refractor": "^5.0.0", "refractor": "^5.0.0",
"remark-gfm": "^4.0.1", "remark-gfm": "^4.0.1",
"server-only": "^0.0.1", "server-only": "^0.0.1",
"strip-ansi": "^7.1.2", "strip-ansi": "^7.1.2",
"superjson": "^2.2.3", "superjson": "^2.2.6",
"tailwind-merge": "^3.3.1", "tailwind-merge": "^3.4.0",
"ws": "^8.18.3", "ws": "^8.18.3",
"zod": "^4.1.12" "zod": "^4.1.13"
}, },
"devDependencies": { "devDependencies": {
"@eslint/eslintrc": "^3.3.1", "@tailwindcss/postcss": "^4.1.17",
"@tailwindcss/postcss": "^4.1.16",
"@testing-library/jest-dom": "^6.9.1", "@testing-library/jest-dom": "^6.9.1",
"@testing-library/react": "^16.3.0", "@testing-library/react": "^16.3.0",
"@testing-library/user-event": "^14.6.1", "@testing-library/user-event": "^14.6.1",
"@types/bcryptjs": "^3.0.0", "@types/bcryptjs": "^3.0.0",
"@types/better-sqlite3": "^7.6.8", "@types/better-sqlite3": "^7.6.13",
"@types/jsonwebtoken": "^9.0.10", "@types/jsonwebtoken": "^9.0.10",
"@types/node": "^24.9.1", "@types/node": "^24.10.1",
"@types/node-cron": "^3.0.11", "@types/node-cron": "^3.0.11",
"@types/react": "^19.0.0", "@types/react": "^19.2.7",
"@types/react-dom": "^19.2.2", "@types/react-dom": "^19.2.3",
"@vitejs/plugin-react": "^5.1.0", "@vitejs/plugin-react": "^5.1.1",
"@vitest/coverage-v8": "^3.2.4", "@vitest/coverage-v8": "^4.0.14",
"@vitest/ui": "^3.2.4", "@vitest/ui": "^4.0.14",
"eslint": "^9.38.0", "baseline-browser-mapping": "^2.8.32",
"eslint-config-next": "^15.1.6", "eslint": "^9.39.1",
"jsdom": "^27.1.0", "eslint-config-next": "^16.0.5",
"postcss": "^8.5.3", "jsdom": "^27.2.0",
"prettier": "^3.5.3", "postcss": "^8.5.6",
"prettier": "^3.7.1",
"prettier-plugin-tailwindcss": "^0.7.1", "prettier-plugin-tailwindcss": "^0.7.1",
"prisma": "^6.19.0", "prisma": "^7.0.1",
"tailwindcss": "^4.1.17", "tailwindcss": "^4.1.17",
"typescript": "^5.8.2", "tsx": "^4.19.4",
"typescript-eslint": "^8.46.2", "typescript": "^5.9.3",
"vitest": "^3.2.4" "typescript-eslint": "^8.48.0",
"vitest": "^4.0.14"
}, },
"ct3aMetadata": { "ct3aMetadata": {
"initVersion": "7.39.3" "initVersion": "7.39.3"
}, },
"packageManager": "npm@10.9.3", "packageManager": "npm@10.9.3",
"engines": {
"node": ">=24.0.0"
},
"overrides": { "overrides": {
"prismjs": "^1.30.0" "prismjs": "^1.30.0"
} }

20
prisma.config.ts Normal file
View File

@@ -0,0 +1,20 @@
import 'dotenv/config'
import path from 'path'
import { defineConfig } from 'prisma/config'
// Resolve database path
const dbPath = process.env.DATABASE_URL ?? `file:${path.join(process.cwd(), 'data', 'pve-scripts.db')}`
export default defineConfig({
schema: 'prisma/schema.prisma',
datasource: {
url: dbPath,
},
// @ts-expect-error - Prisma 7 config types are incomplete
studio: {
adapter: async () => {
const { PrismaBetterSqlite3 } = await import('@prisma/adapter-better-sqlite3')
return new PrismaBetterSqlite3({ url: dbPath })
},
},
})

View File

@@ -0,0 +1,41 @@
-- CreateTable
CREATE TABLE IF NOT EXISTS "backups" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"container_id" TEXT NOT NULL,
"server_id" INTEGER NOT NULL,
"hostname" TEXT NOT NULL,
"backup_name" TEXT NOT NULL,
"backup_path" TEXT NOT NULL,
"size" BIGINT,
"created_at" DATETIME,
"storage_name" TEXT NOT NULL,
"storage_type" TEXT NOT NULL,
"discovered_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "backups_server_id_fkey" FOREIGN KEY ("server_id") REFERENCES "servers" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE IF NOT EXISTS "pbs_storage_credentials" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"server_id" INTEGER NOT NULL,
"storage_name" TEXT NOT NULL,
"pbs_ip" TEXT NOT NULL,
"pbs_datastore" TEXT NOT NULL,
"pbs_password" TEXT NOT NULL,
"pbs_fingerprint" TEXT NOT NULL,
"created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" DATETIME NOT NULL,
CONSTRAINT "pbs_storage_credentials_server_id_fkey" FOREIGN KEY ("server_id") REFERENCES "servers" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
-- CreateIndex
CREATE INDEX IF NOT EXISTS "backups_container_id_idx" ON "backups"("container_id");
-- CreateIndex
CREATE INDEX IF NOT EXISTS "backups_server_id_idx" ON "backups"("server_id");
-- CreateIndex
CREATE INDEX IF NOT EXISTS "pbs_storage_credentials_server_id_idx" ON "pbs_storage_credentials"("server_id");
-- CreateIndex
CREATE UNIQUE INDEX IF NOT EXISTS "pbs_storage_credentials_server_id_storage_name_key" ON "pbs_storage_credentials"("server_id", "storage_name");

View File

@@ -1,10 +1,10 @@
generator client { generator client {
provider = "prisma-client-js" provider = "prisma-client"
output = "./generated/prisma"
} }
datasource db { datasource db {
provider = "sqlite" provider = "sqlite"
url = env("DATABASE_URL")
} }
model InstalledScript { model InstalledScript {
@@ -41,6 +41,8 @@ model Server {
ssh_key_path String? ssh_key_path String?
key_generated Boolean? @default(false) key_generated Boolean? @default(false)
installed_scripts InstalledScript[] installed_scripts InstalledScript[]
backups Backup[]
pbs_credentials PBSStorageCredential[]
@@map("servers") @@map("servers")
} }
@@ -96,6 +98,42 @@ model LXCConfig {
@@map("lxc_configs") @@map("lxc_configs")
} }
model Backup {
id Int @id @default(autoincrement())
container_id String
server_id Int
hostname String
backup_name String
backup_path String
size BigInt?
created_at DateTime?
storage_name String
storage_type String // 'local', 'storage', or 'pbs'
discovered_at DateTime @default(now())
server Server @relation(fields: [server_id], references: [id], onDelete: Cascade)
@@index([container_id])
@@index([server_id])
@@map("backups")
}
model PBSStorageCredential {
id Int @id @default(autoincrement())
server_id Int
storage_name String
pbs_ip String
pbs_datastore String
pbs_password String
pbs_fingerprint String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
server Server @relation(fields: [server_id], references: [id], onDelete: Cascade)
@@unique([server_id, storage_name])
@@index([server_id])
@@map("pbs_storage_credentials")
}
model Repository { model Repository {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
url String @unique url String @unique

10
restore.log Normal file
View File

@@ -0,0 +1,10 @@
Starting restore...
Reading container configuration...
Stopping container...
Destroying container...
Logging into PBS...
Downloading backup from PBS...
Packing backup folder...
Restoring container...
Cleaning up temporary files...
Restore completed successfully

View File

@@ -60,7 +60,7 @@ root_check() {
} }
# This function checks the version of Proxmox Virtual Environment (PVE) and exits if the version is not supported. # This function checks the version of Proxmox Virtual Environment (PVE) and exits if the version is not supported.
# Supported: Proxmox VE 8.0.x 8.9.x and 9.0 (NOT 9.1+) # Supported: Proxmox VE 8.0.x 8.9.x, 9.0 and 9.1
pve_check() { pve_check() {
local PVE_VER local PVE_VER
PVE_VER="$(pveversion | awk -F'/' '{print $2}' | awk -F'-' '{print $1}')" PVE_VER="$(pveversion | awk -F'/' '{print $2}' | awk -F'-' '{print $1}')"
@@ -76,12 +76,12 @@ pve_check() {
return 0 return 0
fi fi
# Check for Proxmox VE 9.x: allow ONLY 9.0 # Check for Proxmox VE 9.x: allow 9.0 and 9.1
if [[ "$PVE_VER" =~ ^9\.([0-9]+) ]]; then if [[ "$PVE_VER" =~ ^9\.([0-9]+) ]]; then
local MINOR="${BASH_REMATCH[1]}" local MINOR="${BASH_REMATCH[1]}"
if ((MINOR != 0)); then if ((MINOR < 0 || MINOR > 1)); then
msg_error "This version of Proxmox VE is not yet supported." msg_error "This version of Proxmox VE is not supported."
msg_error "Supported: Proxmox VE version 9.0" msg_error "Supported: Proxmox VE version 9.0 9.1"
exit 1 exit 1
fi fi
return 0 return 0
@@ -89,7 +89,7 @@ pve_check() {
# All other unsupported versions # All other unsupported versions
msg_error "This version of Proxmox VE is not supported." msg_error "This version of Proxmox VE is not supported."
msg_error "Supported versions: Proxmox VE 8.0 8.x or 9.0" msg_error "Supported versions: Proxmox VE 8.0 8.x or 9.0 9.1"
exit 1 exit 1
} }
@@ -1323,9 +1323,9 @@ EOF'
msg_ok "Customized LXC Container" msg_ok "Customized LXC Container"
if [ "$var_os" == "alpine" ]; then if [ "$var_os" == "alpine" ]; then
FUNCTIONS_FILE_PATH="$(cat "$CORE_DIR/core.func" && echo && cat "$CORE_DIR/tools.func" && echo && cat "$CORE_DIR/alpine-install.func")" FUNCTIONS_FILE_PATH="$(cat "$CORE_DIR/core.func" && echo && cat "$CORE_DIR/tools.func" && echo && cat "$CORE_DIR/api.func" && echo && cat "$CORE_DIR/alpine-install.func")"
else else
FUNCTIONS_FILE_PATH="$(cat "$CORE_DIR/core.func" && echo && cat "$CORE_DIR/tools.func" && echo && cat "$CORE_DIR/install.func")" FUNCTIONS_FILE_PATH="$(cat "$CORE_DIR/core.func" && echo && cat "$CORE_DIR/tools.func" && echo && cat "$CORE_DIR/api.func" && echo && cat "$CORE_DIR/install.func")"
fi fi
FUNCTIONS_FILE="/tmp/functions.sh" FUNCTIONS_FILE="/tmp/functions.sh"

View File

@@ -392,8 +392,6 @@ cleanup_lxc() {
# Python pip # Python pip
if command -v pip &>/dev/null; then $STD pip cache purge || true; fi if command -v pip &>/dev/null; then $STD pip cache purge || true; fi
# Python uv
if command -v uv &>/dev/null; then $STD uv cache clear || true; fi
# Node.js npm # Node.js npm
if command -v npm &>/dev/null; then $STD npm cache clean --force || true; fi if command -v npm &>/dev/null; then $STD npm cache clean --force || true; fi
# Node.js yarn # Node.js yarn
@@ -410,7 +408,6 @@ cleanup_lxc() {
if command -v composer &>/dev/null; then $STD composer clear-cache || true; fi if command -v composer &>/dev/null; then $STD composer clear-cache || true; fi
if command -v journalctl &>/dev/null; then if command -v journalctl &>/dev/null; then
$STD journalctl --rotate || true
$STD journalctl --vacuum-time=10m || true $STD journalctl --vacuum-time=10m || true
fi fi
msg_ok "Cleaned" msg_ok "Cleaned"

735
server.js
View File

@@ -8,9 +8,12 @@ import stripAnsi from 'strip-ansi';
import { spawn as ptySpawn } from 'node-pty'; import { spawn as ptySpawn } from 'node-pty';
import { getSSHExecutionService } from './src/server/ssh-execution-service.js'; import { getSSHExecutionService } from './src/server/ssh-execution-service.js';
import { getDatabase } from './src/server/database-prisma.js'; import { getDatabase } from './src/server/database-prisma.js';
import { initializeAutoSync, initializeRepositories, setupGracefulShutdown } from './src/server/lib/autoSyncInit.js';
import dotenv from 'dotenv'; import dotenv from 'dotenv';
// Dynamic import for auto sync init to avoid tsx caching issues
/** @type {any} */
let autoSyncModule = null;
// Load environment variables from .env file // Load environment variables from .env file
dotenv.config(); dotenv.config();
// Fallback minimal global error handlers for Node runtime (avoid TS import) // Fallback minimal global error handlers for Node runtime (avoid TS import)
@@ -71,7 +74,14 @@ const handle = app.getRequestHandler();
* @property {ServerInfo} [server] * @property {ServerInfo} [server]
* @property {boolean} [isUpdate] * @property {boolean} [isUpdate]
* @property {boolean} [isShell] * @property {boolean} [isShell]
* @property {boolean} [isBackup]
* @property {boolean} [isClone]
* @property {string} [containerId] * @property {string} [containerId]
* @property {string} [storage]
* @property {string} [backupStorage]
* @property {number} [cloneCount]
* @property {string[]} [hostnames]
* @property {'lxc'|'vm'} [containerType]
*/ */
class ScriptExecutionHandler { class ScriptExecutionHandler {
@@ -79,14 +89,27 @@ class ScriptExecutionHandler {
* @param {import('http').Server} server * @param {import('http').Server} server
*/ */
constructor(server) { constructor(server) {
// Create WebSocketServer without attaching to server
// We'll handle upgrades manually to avoid interfering with Next.js HMR
this.wss = new WebSocketServer({ this.wss = new WebSocketServer({
server, noServer: true
path: '/ws/script-execution'
}); });
this.activeExecutions = new Map(); this.activeExecutions = new Map();
this.db = getDatabase(); this.db = getDatabase();
this.setupWebSocket(); this.setupWebSocket();
} }
/**
* Handle WebSocket upgrade for our endpoint
* @param {import('http').IncomingMessage} request
* @param {import('stream').Duplex} socket
* @param {Buffer} head
*/
handleUpgrade(request, socket, head) {
this.wss.handleUpgrade(request, socket, head, (ws) => {
this.wss.emit('connection', ws, request);
});
}
/** /**
* Parse Container ID from terminal output * Parse Container ID from terminal output
@@ -276,13 +299,17 @@ class ScriptExecutionHandler {
* @param {WebSocketMessage} message * @param {WebSocketMessage} message
*/ */
async handleMessage(ws, message) { async handleMessage(ws, message) {
const { action, scriptPath, executionId, input, mode, server, isUpdate, isShell, containerId } = message; const { action, scriptPath, executionId, input, mode, server, isUpdate, isShell, isBackup, isClone, containerId, storage, backupStorage, cloneCount, hostnames, containerType } = message;
switch (action) { switch (action) {
case 'start': case 'start':
if (scriptPath && executionId) { if (scriptPath && executionId) {
if (isUpdate && containerId) { if (isClone && containerId && storage && server && cloneCount && hostnames && containerType) {
await this.startUpdateExecution(ws, containerId, executionId, mode, server); await this.startSSHCloneExecution(ws, containerId, executionId, storage, server, containerType, cloneCount, hostnames);
} else if (isBackup && containerId && storage) {
await this.startBackupExecution(ws, containerId, executionId, storage, mode, server);
} else if (isUpdate && containerId) {
await this.startUpdateExecution(ws, containerId, executionId, mode, server, backupStorage);
} else if (isShell && containerId) { } else if (isShell && containerId) {
await this.startShellExecution(ws, containerId, executionId, mode, server); await this.startShellExecution(ws, containerId, executionId, mode, server);
} else { } else {
@@ -660,18 +687,636 @@ class ScriptExecutionHandler {
} }
} }
/**
* Start backup execution
* @param {ExtendedWebSocket} ws
* @param {string} containerId
* @param {string} executionId
* @param {string} storage
* @param {string} mode
* @param {ServerInfo|null} server
*/
async startBackupExecution(ws, containerId, executionId, storage, mode = 'local', server = null) {
try {
// Send start message
this.sendMessage(ws, {
type: 'start',
data: `Starting backup for container ${containerId} to storage ${storage}...`,
timestamp: Date.now()
});
if (mode === 'ssh' && server) {
await this.startSSHBackupExecution(ws, containerId, executionId, storage, server);
} else {
this.sendMessage(ws, {
type: 'error',
data: 'Backup is only supported via SSH',
timestamp: Date.now()
});
}
} catch (error) {
this.sendMessage(ws, {
type: 'error',
data: `Failed to start backup: ${error instanceof Error ? error.message : String(error)}`,
timestamp: Date.now()
});
}
}
/**
* Start SSH backup execution
* @param {ExtendedWebSocket} ws
* @param {string} containerId
* @param {string} executionId
* @param {string} storage
* @param {ServerInfo} server
* @param {Function} [onComplete] - Optional callback when backup completes
*/
startSSHBackupExecution(ws, containerId, executionId, storage, server, onComplete = undefined) {
const sshService = getSSHExecutionService();
return new Promise((resolve, reject) => {
try {
const backupCommand = `vzdump ${containerId} --storage ${storage} --mode snapshot`;
// Wrap the onExit callback to resolve our promise
let promiseResolved = false;
sshService.executeCommand(
server,
backupCommand,
/** @param {string} data */
(data) => {
this.sendMessage(ws, {
type: 'output',
data: data,
timestamp: Date.now()
});
},
/** @param {string} error */
(error) => {
this.sendMessage(ws, {
type: 'error',
data: error,
timestamp: Date.now()
});
},
/** @param {number} code */
(code) => {
// Don't send 'end' message here if this is part of a backup+update flow
// The update flow will handle completion messages
const success = code === 0;
if (!success) {
this.sendMessage(ws, {
type: 'error',
data: `Backup failed with exit code: ${code}`,
timestamp: Date.now()
});
}
// Send a completion message (but not 'end' type to avoid stopping terminal)
this.sendMessage(ws, {
type: 'output',
data: `\n[Backup ${success ? 'completed' : 'failed'} with exit code: ${code}]\n`,
timestamp: Date.now()
});
if (onComplete) onComplete(success);
// Resolve the promise when backup completes
// Use setImmediate to ensure resolution happens in the right execution context
if (!promiseResolved) {
promiseResolved = true;
const result = { success, code };
// Use setImmediate to ensure promise resolution happens in the next tick
// This ensures the await in startUpdateExecution can properly resume
setImmediate(() => {
try {
resolve(result);
} catch (resolveError) {
console.error('Error resolving backup promise:', resolveError);
reject(resolveError);
}
});
}
this.activeExecutions.delete(executionId);
}
).then((execution) => {
// Store the execution
this.activeExecutions.set(executionId, {
process: /** @type {any} */ (execution).process,
ws
});
// Note: Don't resolve here - wait for onExit callback
}).catch((error) => {
console.error('Error starting backup execution:', error);
this.sendMessage(ws, {
type: 'error',
data: `SSH backup execution failed: ${error instanceof Error ? error.message : String(error)}`,
timestamp: Date.now()
});
if (onComplete) onComplete(false);
if (!promiseResolved) {
promiseResolved = true;
reject(error);
}
});
} catch (error) {
console.error('Error in startSSHBackupExecution:', error);
this.sendMessage(ws, {
type: 'error',
data: `SSH backup execution failed: ${error instanceof Error ? error.message : String(error)}`,
timestamp: Date.now()
});
if (onComplete) onComplete(false);
reject(error);
}
});
}
/**
* Start SSH clone execution
* Gets next IDs sequentially: get next ID → clone → get next ID → clone, etc.
* @param {ExtendedWebSocket} ws
* @param {string} containerId
* @param {string} executionId
* @param {string} storage
* @param {ServerInfo} server
* @param {'lxc'|'vm'} containerType
* @param {number} cloneCount
* @param {string[]} hostnames
*/
async startSSHCloneExecution(ws, containerId, executionId, storage, server, containerType, cloneCount, hostnames) {
const sshService = getSSHExecutionService();
this.sendMessage(ws, {
type: 'start',
data: `Starting clone operation: Creating ${cloneCount} clone(s) of ${containerType.toUpperCase()} ${containerId}...`,
timestamp: Date.now()
});
try {
// Step 1: Stop source container/VM
this.sendMessage(ws, {
type: 'output',
data: `\n[Step 1/${4 + cloneCount}] Stopping source ${containerType.toUpperCase()} ${containerId}...\n`,
timestamp: Date.now()
});
const stopCommand = containerType === 'lxc' ? `pct stop ${containerId}` : `qm stop ${containerId}`;
await new Promise(/** @type {(resolve: (value?: void) => void, reject: (error?: any) => void) => void} */ ((resolve, reject) => {
sshService.executeCommand(
server,
stopCommand,
/** @param {string} data */
(data) => {
this.sendMessage(ws, {
type: 'output',
data: data,
timestamp: Date.now()
});
},
/** @param {string} error */
(error) => {
this.sendMessage(ws, {
type: 'error',
data: error,
timestamp: Date.now()
});
},
/** @param {number} code */
(code) => {
if (code === 0) {
this.sendMessage(ws, {
type: 'output',
data: `\n[Step 1/${4 + cloneCount}] Source ${containerType.toUpperCase()} stopped successfully.\n`,
timestamp: Date.now()
});
resolve();
} else {
// Continue even if stop fails (might already be stopped)
this.sendMessage(ws, {
type: 'output',
data: `\n[Step 1/${4 + cloneCount}] Stop command completed with exit code ${code} (container may already be stopped).\n`,
timestamp: Date.now()
});
resolve();
}
}
);
}));
// Step 2: Clone for each clone count (get next ID sequentially before each clone)
const clonedIds = [];
for (let i = 0; i < cloneCount; i++) {
const cloneNumber = i + 1;
const hostname = hostnames[i];
// Get next ID for this clone
this.sendMessage(ws, {
type: 'output',
data: `\n[Step ${2 + i}/${4 + cloneCount}] Getting next available ID for clone ${cloneNumber}...\n`,
timestamp: Date.now()
});
let nextId = '';
try {
let output = '';
await new Promise(/** @type {(resolve: (value?: void) => void, reject: (error?: any) => void) => void} */ ((resolve, reject) => {
sshService.executeCommand(
server,
'pvesh get /cluster/nextid',
/** @param {string} data */
(data) => {
output += data;
},
/** @param {string} error */
(error) => {
reject(new Error(`Failed to get next ID: ${error}`));
},
/** @param {number} exitCode */
(exitCode) => {
if (exitCode === 0) {
resolve();
} else {
reject(new Error(`pvesh command failed with exit code ${exitCode}`));
}
}
);
}));
nextId = output.trim();
if (!nextId || !/^\d+$/.test(nextId)) {
throw new Error('Invalid next ID received');
}
this.sendMessage(ws, {
type: 'output',
data: `\n[Step ${2 + i}/${4 + cloneCount}] Got next ID: ${nextId}\n`,
timestamp: Date.now()
});
} catch (error) {
this.sendMessage(ws, {
type: 'error',
data: `\n[Step ${2 + i}/${4 + cloneCount}] Failed to get next ID: ${error instanceof Error ? error.message : String(error)}\n`,
timestamp: Date.now()
});
throw error;
}
clonedIds.push(nextId);
// Clone the container/VM
this.sendMessage(ws, {
type: 'output',
data: `\n[Step ${2 + i}/${4 + cloneCount}] Cloning ${containerType.toUpperCase()} ${containerId} to ${nextId} with hostname ${hostname}...\n`,
timestamp: Date.now()
});
const cloneCommand = containerType === 'lxc'
? `pct clone ${containerId} ${nextId} --hostname ${hostname} --storage ${storage}`
: `qm clone ${containerId} ${nextId} --name ${hostname} --storage ${storage}`;
await new Promise(/** @type {(resolve: (value?: void) => void, reject: (error?: any) => void) => void} */ ((resolve, reject) => {
sshService.executeCommand(
server,
cloneCommand,
/** @param {string} data */
(data) => {
this.sendMessage(ws, {
type: 'output',
data: data,
timestamp: Date.now()
});
},
/** @param {string} error */
(error) => {
this.sendMessage(ws, {
type: 'error',
data: error,
timestamp: Date.now()
});
},
/** @param {number} code */
(code) => {
if (code === 0) {
this.sendMessage(ws, {
type: 'output',
data: `\n[Step ${2 + i}/${4 + cloneCount}] Clone ${cloneNumber} created successfully.\n`,
timestamp: Date.now()
});
resolve();
} else {
this.sendMessage(ws, {
type: 'error',
data: `\nClone ${cloneNumber} failed with exit code: ${code}\n`,
timestamp: Date.now()
});
reject(new Error(`Clone ${cloneNumber} failed with exit code ${code}`));
}
}
);
}));
}
// Step 3: Start source container/VM
this.sendMessage(ws, {
type: 'output',
data: `\n[Step ${2 + cloneCount + 1}/${4 + cloneCount}] Starting source ${containerType.toUpperCase()} ${containerId}...\n`,
timestamp: Date.now()
});
const startSourceCommand = containerType === 'lxc' ? `pct start ${containerId}` : `qm start ${containerId}`;
await new Promise(/** @type {(resolve: (value?: void) => void, reject: (error?: any) => void) => void} */ ((resolve) => {
sshService.executeCommand(
server,
startSourceCommand,
/** @param {string} data */
(data) => {
this.sendMessage(ws, {
type: 'output',
data: data,
timestamp: Date.now()
});
},
/** @param {string} error */
(error) => {
this.sendMessage(ws, {
type: 'error',
data: error,
timestamp: Date.now()
});
},
/** @param {number} code */
(code) => {
if (code === 0) {
this.sendMessage(ws, {
type: 'output',
data: `\n[Step ${2 + cloneCount + 1}/${4 + cloneCount}] Source ${containerType.toUpperCase()} started successfully.\n`,
timestamp: Date.now()
});
} else {
this.sendMessage(ws, {
type: 'output',
data: `\n[Step ${2 + cloneCount + 1}/${4 + cloneCount}] Start command completed with exit code ${code}.\n`,
timestamp: Date.now()
});
}
resolve();
}
);
}));
// Step 4: Start target containers/VMs
this.sendMessage(ws, {
type: 'output',
data: `\n[Step ${2 + cloneCount + 2}/${4 + cloneCount}] Starting cloned ${containerType.toUpperCase()}(s)...\n`,
timestamp: Date.now()
});
for (let i = 0; i < cloneCount; i++) {
const cloneNumber = i + 1;
const nextId = clonedIds[i];
const startTargetCommand = containerType === 'lxc' ? `pct start ${nextId}` : `qm start ${nextId}`;
await new Promise(/** @type {(resolve: (value?: void) => void, reject: (error?: any) => void) => void} */ ((resolve) => {
sshService.executeCommand(
server,
startTargetCommand,
/** @param {string} data */
(data) => {
this.sendMessage(ws, {
type: 'output',
data: data,
timestamp: Date.now()
});
},
/** @param {string} error */
(error) => {
this.sendMessage(ws, {
type: 'error',
data: error,
timestamp: Date.now()
});
},
/** @param {number} code */
(code) => {
if (code === 0) {
this.sendMessage(ws, {
type: 'output',
data: `\nClone ${cloneNumber} (ID: ${nextId}) started successfully.\n`,
timestamp: Date.now()
});
} else {
this.sendMessage(ws, {
type: 'output',
data: `\nClone ${cloneNumber} (ID: ${nextId}) start completed with exit code ${code}.\n`,
timestamp: Date.now()
});
}
resolve();
}
);
}));
}
// Step 5: Add to database
this.sendMessage(ws, {
type: 'output',
data: `\n[Step ${2 + cloneCount + 3}/${4 + cloneCount}] Adding cloned ${containerType.toUpperCase()}(s) to database...\n`,
timestamp: Date.now()
});
for (let i = 0; i < cloneCount; i++) {
const nextId = clonedIds[i];
const hostname = hostnames[i];
try {
// Read config file to get hostname/name
const configPath = containerType === 'lxc'
? `/etc/pve/lxc/${nextId}.conf`
: `/etc/pve/qemu-server/${nextId}.conf`;
let configContent = '';
await new Promise(/** @type {(resolve: (value?: void) => void) => void} */ ((resolve) => {
sshService.executeCommand(
server,
`cat "${configPath}" 2>/dev/null || echo ""`,
/** @param {string} data */
(data) => {
configContent += data;
},
() => resolve(),
() => resolve()
);
}));
// Parse config for hostname/name
let finalHostname = hostname;
if (configContent.trim()) {
const lines = configContent.split('\n');
for (const line of lines) {
const trimmed = line.trim();
if (containerType === 'lxc' && trimmed.startsWith('hostname:')) {
finalHostname = trimmed.substring(9).trim();
break;
} else if (containerType === 'vm' && trimmed.startsWith('name:')) {
finalHostname = trimmed.substring(5).trim();
break;
}
}
}
if (!finalHostname) {
finalHostname = `${containerType}-${nextId}`;
}
// Create installed script record
const script = await this.db.createInstalledScript({
script_name: finalHostname,
script_path: `cloned/${finalHostname}`,
container_id: nextId,
server_id: server.id,
execution_mode: 'ssh',
status: 'success',
output_log: `Cloned ${containerType.toUpperCase()}`
});
// For LXC, store config in database
if (containerType === 'lxc' && configContent.trim()) {
// Simple config parser
/** @type {any} */
const configData = {};
const lines = configContent.split('\n');
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;
const [key, ...valueParts] = trimmed.split(':');
const value = valueParts.join(':').trim();
if (key === 'hostname') configData.hostname = value;
else if (key === 'arch') configData.arch = value;
else if (key === 'cores') configData.cores = parseInt(value) || null;
else if (key === 'memory') configData.memory = parseInt(value) || null;
else if (key === 'swap') configData.swap = parseInt(value) || null;
else if (key === 'onboot') configData.onboot = parseInt(value) || null;
else if (key === 'ostype') configData.ostype = value;
else if (key === 'unprivileged') configData.unprivileged = parseInt(value) || null;
else if (key === 'tags') configData.tags = value;
else if (key === 'rootfs') {
const match = value.match(/^([^:]+):([^,]+)/);
if (match) {
configData.rootfs_storage = match[1];
const sizeMatch = value.match(/size=([^,]+)/);
if (sizeMatch) {
configData.rootfs_size = sizeMatch[1];
}
}
}
}
await this.db.createLXCConfig(script.id, configData);
}
this.sendMessage(ws, {
type: 'output',
data: `\nClone ${i + 1} (ID: ${nextId}, Hostname: ${finalHostname}) added to database successfully.\n`,
timestamp: Date.now()
});
} catch (error) {
this.sendMessage(ws, {
type: 'error',
data: `\nError adding clone ${i + 1} (ID: ${nextId}) to database: ${error instanceof Error ? error.message : String(error)}\n`,
timestamp: Date.now()
});
}
}
this.sendMessage(ws, {
type: 'output',
data: `\n\n[Clone operation completed successfully!]\nCreated ${cloneCount} clone(s) of ${containerType.toUpperCase()} ${containerId}.\n`,
timestamp: Date.now()
});
this.activeExecutions.delete(executionId);
} catch (error) {
this.sendMessage(ws, {
type: 'error',
data: `\n\n[Clone operation failed!]\nError: ${error instanceof Error ? error.message : String(error)}\n`,
timestamp: Date.now()
});
this.activeExecutions.delete(executionId);
}
}
/** /**
* Start update execution (pct enter + update command) * Start update execution (pct enter + update command)
* @param {ExtendedWebSocket} ws * @param {ExtendedWebSocket} ws
* @param {string} containerId * @param {string} containerId
* @param {string} executionId * @param {string} executionId
* @param {string} mode * @param {string} mode
* @param {ServerInfo|null} server * @param {ServerInfo|undefined} server
* @param {string} [backupStorage] - Optional storage to backup to before update
*/ */
async startUpdateExecution(ws, containerId, executionId, mode = 'local', server = null) { async startUpdateExecution(ws, containerId, executionId, mode = 'local', server = undefined, backupStorage = undefined) {
try { try {
// If backup storage is provided, run backup first
if (backupStorage && mode === 'ssh' && server) {
this.sendMessage(ws, {
type: 'start',
data: `Starting backup before update for container ${containerId}...`,
timestamp: Date.now()
});
// Create a separate execution ID for backup
const backupExecutionId = `backup_${executionId}`;
// Run backup and wait for it to complete
try {
const backupResult = await this.startSSHBackupExecution(
ws,
containerId,
backupExecutionId,
backupStorage,
server
);
// Backup completed (successfully or not)
if (!backupResult || !backupResult.success) {
// Backup failed, but we'll still allow update (per requirement 1b)
this.sendMessage(ws, {
type: 'output',
data: '\n⚠ Backup failed, but proceeding with update as requested...\n',
timestamp: Date.now()
});
} else {
// Backup succeeded
this.sendMessage(ws, {
type: 'output',
data: '\n✅ Backup completed successfully. Starting update...\n',
timestamp: Date.now()
});
}
} catch (error) {
console.error('Backup error before update:', error);
// Backup failed to start, but allow update to proceed
this.sendMessage(ws, {
type: 'output',
data: `\n⚠️ Backup error: ${error instanceof Error ? error.message : String(error)}. Proceeding with update...\n`,
timestamp: Date.now()
});
}
// Small delay before starting update
await new Promise(resolve => setTimeout(resolve, 1000));
}
// Send start message // Send start message for update (only if we're actually starting an update)
this.sendMessage(ws, { this.sendMessage(ws, {
type: 'start', type: 'start',
data: `Starting update for container ${containerId}...`, data: `Starting update for container ${containerId}...`,
@@ -955,12 +1600,22 @@ app.prepare().then(() => {
const parsedUrl = parse(req.url || '', true); const parsedUrl = parse(req.url || '', true);
const { pathname, query } = parsedUrl; const { pathname, query } = parsedUrl;
if (pathname === '/ws/script-execution') { // Check if this is a WebSocket upgrade request
const isWebSocketUpgrade = req.headers.upgrade === 'websocket';
// Only intercept WebSocket upgrades for /ws/script-execution
// Let Next.js handle all other WebSocket upgrades (like HMR) and all HTTP requests
if (isWebSocketUpgrade && pathname === '/ws/script-execution') {
// WebSocket upgrade will be handled by the WebSocket server // WebSocket upgrade will be handled by the WebSocket server
// Don't call handle() for this path - let WebSocketServer handle it
return; return;
} }
// Let Next.js handle all other requests including HMR // Let Next.js handle all other requests including:
// - HTTP requests to /ws/script-execution (non-WebSocket)
// - WebSocket upgrades to other paths (like /_next/webpack-hmr)
// - All static assets (_next routes)
// - All other routes
await handle(req, res, parsedUrl); await handle(req, res, parsedUrl);
} catch (err) { } catch (err) {
console.error('Error occurred handling', req.url, err); console.error('Error occurred handling', req.url, err);
@@ -971,6 +1626,33 @@ app.prepare().then(() => {
// Create WebSocket handlers // Create WebSocket handlers
const scriptHandler = new ScriptExecutionHandler(httpServer); const scriptHandler = new ScriptExecutionHandler(httpServer);
// Handle WebSocket upgrades manually to avoid interfering with Next.js HMR
// We need to preserve Next.js's upgrade handlers and call them for non-matching paths
// Save any existing upgrade listeners (Next.js might have set them up)
const existingUpgradeListeners = httpServer.listeners('upgrade').slice();
httpServer.removeAllListeners('upgrade');
// Add our upgrade handler that routes based on path
httpServer.on('upgrade', (request, socket, head) => {
const parsedUrl = parse(request.url || '', true);
const { pathname } = parsedUrl;
if (pathname === '/ws/script-execution') {
// Handle our custom WebSocket endpoint
scriptHandler.handleUpgrade(request, socket, head);
} else {
// For all other paths (including Next.js HMR), call existing listeners
// This allows Next.js to handle its own WebSocket upgrades
for (const listener of existingUpgradeListeners) {
try {
listener.call(httpServer, request, socket, head);
} catch (err) {
console.error('Error in upgrade listener:', err);
}
}
}
});
// Note: TerminalHandler removed as it's not being used by the current application // Note: TerminalHandler removed as it's not being used by the current application
httpServer httpServer
@@ -982,13 +1664,38 @@ app.prepare().then(() => {
console.log(`> Ready on http://${hostname}:${port}`); console.log(`> Ready on http://${hostname}:${port}`);
console.log(`> WebSocket server running on ws://${hostname}:${port}/ws/script-execution`); console.log(`> WebSocket server running on ws://${hostname}:${port}/ws/script-execution`);
// Initialize auto sync module and run initialization
if (!autoSyncModule) {
try {
console.log('Dynamically importing autoSyncInit...');
autoSyncModule = await import('./src/server/lib/autoSyncInit.js');
console.log('autoSyncModule loaded, exports:', Object.keys(autoSyncModule));
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
console.error('Failed to import autoSyncInit:', err.message);
console.error('Stack:', err.stack);
throw error;
}
}
// Initialize default repositories // Initialize default repositories
await initializeRepositories(); if (typeof autoSyncModule.initializeRepositories === 'function') {
console.log('Calling initializeRepositories...');
await autoSyncModule.initializeRepositories();
} else {
console.warn('initializeRepositories is not a function, type:', typeof autoSyncModule.initializeRepositories);
}
// Initialize auto-sync service // Initialize auto-sync service
initializeAutoSync(); if (typeof autoSyncModule.initializeAutoSync === 'function') {
console.log('Calling initializeAutoSync...');
autoSyncModule.initializeAutoSync();
}
// Setup graceful shutdown handlers // Setup graceful shutdown handlers
setupGracefulShutdown(); if (typeof autoSyncModule.setupGracefulShutdown === 'function') {
console.log('Setting up graceful shutdown...');
autoSyncModule.setupGracefulShutdown();
}
}); });
}); });

View File

@@ -1,6 +1,13 @@
'use client'; "use client";
import { createContext, useContext, useEffect, useState, useCallback, type ReactNode } from 'react'; import {
createContext,
useContext,
useEffect,
useState,
useCallback,
type ReactNode,
} from "react";
interface AuthContextType { interface AuthContextType {
isAuthenticated: boolean; isAuthenticated: boolean;
@@ -27,10 +34,13 @@ export function AuthProvider({ children }: AuthProviderProps) {
const checkAuthInternal = async (retryCount = 0) => { const checkAuthInternal = async (retryCount = 0) => {
try { try {
// First check if setup is completed // First check if setup is completed
const setupResponse = await fetch('/api/settings/auth-credentials'); const setupResponse = await fetch("/api/settings/auth-credentials");
if (setupResponse.ok) { if (setupResponse.ok) {
const setupData = await setupResponse.json() as { setupCompleted: boolean; enabled: boolean }; const setupData = (await setupResponse.json()) as {
setupCompleted: boolean;
enabled: boolean;
};
// If setup is not completed or auth is disabled, don't verify // If setup is not completed or auth is disabled, don't verify
if (!setupData.setupCompleted || !setupData.enabled) { if (!setupData.setupCompleted || !setupData.enabled) {
setIsAuthenticated(false); setIsAuthenticated(false);
@@ -42,12 +52,12 @@ export function AuthProvider({ children }: AuthProviderProps) {
} }
// Only verify authentication if setup is completed and auth is enabled // Only verify authentication if setup is completed and auth is enabled
const response = await fetch('/api/auth/verify', { const response = await fetch("/api/auth/verify", {
credentials: 'include', // Ensure cookies are sent credentials: "include", // Ensure cookies are sent
}); });
if (response.ok) { if (response.ok) {
const data = await response.json() as { const data = (await response.json()) as {
username: string; username: string;
expirationTime?: number | null; expirationTime?: number | null;
timeUntilExpiration?: number | null; timeUntilExpiration?: number | null;
}; };
@@ -58,7 +68,7 @@ export function AuthProvider({ children }: AuthProviderProps) {
setIsAuthenticated(false); setIsAuthenticated(false);
setUsername(null); setUsername(null);
setExpirationTime(null); setExpirationTime(null);
// Retry logic for failed auth checks (max 2 retries) // Retry logic for failed auth checks (max 2 retries)
if (retryCount < 2) { if (retryCount < 2) {
setTimeout(() => { setTimeout(() => {
@@ -68,11 +78,11 @@ export function AuthProvider({ children }: AuthProviderProps) {
} }
} }
} catch (error) { } catch (error) {
console.error('Error checking auth:', error); console.error("Error checking auth:", error);
setIsAuthenticated(false); setIsAuthenticated(false);
setUsername(null); setUsername(null);
setExpirationTime(null); setExpirationTime(null);
// Retry logic for network errors (max 2 retries) // Retry logic for network errors (max 2 retries)
if (retryCount < 2) { if (retryCount < 2) {
setTimeout(() => { setTimeout(() => {
@@ -89,44 +99,49 @@ export function AuthProvider({ children }: AuthProviderProps) {
return checkAuthInternal(0); return checkAuthInternal(0);
}, []); }, []);
const login = async (username: string, password: string): Promise<boolean> => { const login = async (
username: string,
password: string,
): Promise<boolean> => {
try { try {
const response = await fetch('/api/auth/login', { const response = await fetch("/api/auth/login", {
method: 'POST', method: "POST",
headers: { headers: {
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
body: JSON.stringify({ username, password }), body: JSON.stringify({ username, password }),
credentials: 'include', // Ensure cookies are received credentials: "include", // Ensure cookies are received
}); });
if (response.ok) { if (response.ok) {
const data = await response.json() as { username: string }; const data = (await response.json()) as {
username: string;
expirationTime?: number;
};
setIsAuthenticated(true); setIsAuthenticated(true);
setUsername(data.username); setUsername(data.username);
// Set expiration time from login response if available
// Check auth again to get expiration time if (data.expirationTime) {
// Add a small delay to ensure the httpOnly cookie is available setExpirationTime(data.expirationTime);
await new Promise<void>((resolve) => { }
setTimeout(() => { // Don't call checkAuth after login - we already know we're authenticated
void checkAuth().then(() => resolve()); // The cookie is set by the server response
}, 150);
});
return true; return true;
} else { } else {
const errorData = await response.json(); const errorData = await response.json();
console.error('Login failed:', errorData.error); console.error("Login failed:", errorData.error);
return false; return false;
} }
} catch (error) { } catch (error) {
console.error('Login error:', error); console.error("Login error:", error);
return false; return false;
} }
}; };
const logout = () => { const logout = () => {
// Clear the auth cookie by setting it to expire // Clear the auth cookie by setting it to expire
document.cookie = 'auth-token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'; document.cookie =
"auth-token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
setIsAuthenticated(false); setIsAuthenticated(false);
setUsername(null); setUsername(null);
setExpirationTime(null); setExpirationTime(null);
@@ -156,7 +171,7 @@ export function AuthProvider({ children }: AuthProviderProps) {
export function useAuth() { export function useAuth() {
const context = useContext(AuthContext); const context = useContext(AuthContext);
if (context === undefined) { if (context === undefined) {
throw new Error('useAuth must be used within an AuthProvider'); throw new Error("useAuth must be used within an AuthProvider");
} }
return context; return context;
} }

View File

@@ -0,0 +1,74 @@
"use client";
import { Button } from "./ui/button";
import { AlertTriangle } from "lucide-react";
import { useRegisterModal } from "./modal/ModalStackProvider";
interface BackupWarningModalProps {
isOpen: boolean;
onClose: () => void;
onProceed: () => void;
}
export function BackupWarningModal({
isOpen,
onClose,
onProceed,
}: BackupWarningModalProps) {
useRegisterModal(isOpen, {
id: "backup-warning-modal",
allowEscape: true,
onClose,
});
if (!isOpen) return null;
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4 backdrop-blur-sm">
<div className="bg-card border-border w-full max-w-md rounded-lg border shadow-xl">
{/* Header */}
<div className="border-border flex items-center justify-center border-b p-6">
<div className="flex items-center gap-3">
<AlertTriangle className="text-warning h-8 w-8" />
<h2 className="text-card-foreground text-2xl font-bold">
Backup Failed
</h2>
</div>
</div>
{/* Content */}
<div className="p-6">
<p className="text-muted-foreground mb-6 text-sm">
The backup failed, but you can still proceed with the update if you
wish.
<br />
<br />
<strong className="text-foreground">Warning:</strong> Proceeding
without a backup means you won&apos;t be able to restore the
container if something goes wrong during the update.
</p>
{/* Action Buttons */}
<div className="flex flex-col justify-end gap-3 sm:flex-row">
<Button
onClick={onClose}
variant="outline"
size="default"
className="w-full sm:w-auto"
>
Cancel
</Button>
<Button
onClick={onProceed}
variant="default"
size="default"
className="bg-warning hover:bg-warning/90 w-full sm:w-auto"
>
Proceed Anyway
</Button>
</div>
</div>
</div>
</div>
);
}

View File

@@ -0,0 +1,551 @@
"use client";
import { useState, useEffect } from "react";
import { api } from "~/trpc/react";
import { Button } from "./ui/button";
import { Badge } from "./ui/badge";
import {
RefreshCw,
ChevronDown,
ChevronRight,
HardDrive,
Database,
Server,
CheckCircle,
AlertCircle,
} from "lucide-react";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "./ui/dropdown-menu";
import { ConfirmationModal } from "./ConfirmationModal";
import { LoadingModal } from "./LoadingModal";
interface Backup {
id: number;
backup_name: string;
backup_path: string;
size: bigint | null;
created_at: Date | null;
storage_name: string;
storage_type: string;
discovered_at: Date;
server_id?: number;
server_name: string | null;
server_color: string | null;
}
interface ContainerBackups {
container_id: string;
hostname: string;
backups: Backup[];
}
export function BackupsTab() {
const [expandedContainers, setExpandedContainers] = useState<Set<string>>(
new Set(),
);
const [hasAutoDiscovered, setHasAutoDiscovered] = useState(false);
const [restoreConfirmOpen, setRestoreConfirmOpen] = useState(false);
const [selectedBackup, setSelectedBackup] = useState<{
backup: Backup;
containerId: string;
} | null>(null);
const [restoreProgress, setRestoreProgress] = useState<string[]>([]);
const [restoreSuccess, setRestoreSuccess] = useState(false);
const [restoreError, setRestoreError] = useState<string | null>(null);
const [shouldPollRestore, setShouldPollRestore] = useState(false);
const {
data: backupsData,
refetch: refetchBackups,
isLoading,
} = api.backups.getAllBackupsGrouped.useQuery();
const discoverMutation = api.backups.discoverBackups.useMutation({
onSuccess: () => {
void refetchBackups();
},
});
// Poll for restore progress
const { data: restoreLogsData } = api.backups.getRestoreProgress.useQuery(
undefined,
{
enabled: shouldPollRestore,
refetchInterval: 1000, // Poll every second
refetchIntervalInBackground: true,
},
);
// Update restore progress when log data changes
useEffect(() => {
if (restoreLogsData?.success && restoreLogsData.logs) {
setRestoreProgress(restoreLogsData.logs);
// Stop polling when restore is complete
if (restoreLogsData.isComplete) {
setShouldPollRestore(false);
// Check if restore was successful or failed
const lastLog =
restoreLogsData.logs[restoreLogsData.logs.length - 1] ?? "";
if (lastLog.includes("Restore completed successfully")) {
setRestoreSuccess(true);
setRestoreError(null);
} else if (lastLog.includes("Error:") || lastLog.includes("failed")) {
setRestoreError(lastLog);
setRestoreSuccess(false);
}
}
}
}, [restoreLogsData]);
const restoreMutation = api.backups.restoreBackup.useMutation({
onMutate: () => {
// Start polling for progress
setShouldPollRestore(true);
setRestoreProgress(["Starting restore..."]);
setRestoreError(null);
setRestoreSuccess(false);
},
onSuccess: (result) => {
// Stop polling - progress will be updated from logs
setShouldPollRestore(false);
if (result.success) {
// Update progress with all messages from backend (fallback if polling didn't work)
const progressMessages =
restoreProgress.length > 0
? restoreProgress
: (result.progress?.map((p) => p.message) ?? [
"Restore completed successfully",
]);
setRestoreProgress(progressMessages);
setRestoreSuccess(true);
setRestoreError(null);
setRestoreConfirmOpen(false);
setSelectedBackup(null);
// Keep success message visible - user can dismiss manually
} else {
setRestoreError(result.error ?? "Restore failed");
setRestoreProgress(
result.progress?.map((p) => p.message) ?? restoreProgress,
);
setRestoreSuccess(false);
setRestoreConfirmOpen(false);
setSelectedBackup(null);
// Keep error message visible - user can dismiss manually
}
},
onError: (error) => {
// Stop polling on error
setShouldPollRestore(false);
setRestoreError(error.message ?? "Restore failed");
setRestoreConfirmOpen(false);
setSelectedBackup(null);
setRestoreProgress([]);
},
});
// Update progress text in modal based on current progress
const currentProgressText =
restoreProgress.length > 0
? restoreProgress[restoreProgress.length - 1]
: "Restoring backup...";
// Auto-discover backups when tab is first opened
useEffect(() => {
if (!hasAutoDiscovered && !isLoading && backupsData) {
// Only auto-discover if there are no backups yet
if (!backupsData.backups?.length) {
void handleDiscoverBackups();
}
setHasAutoDiscovered(true);
}
}, [hasAutoDiscovered, isLoading, backupsData]);
const handleDiscoverBackups = () => {
discoverMutation.mutate();
};
const handleRestoreClick = (backup: Backup, containerId: string) => {
setSelectedBackup({ backup, containerId });
setRestoreConfirmOpen(true);
setRestoreError(null);
setRestoreSuccess(false);
setRestoreProgress([]);
};
const handleRestoreConfirm = () => {
if (!selectedBackup) return;
setRestoreConfirmOpen(false);
setRestoreError(null);
setRestoreSuccess(false);
restoreMutation.mutate({
backupId: selectedBackup.backup.id,
containerId: selectedBackup.containerId,
serverId: selectedBackup.backup.server_id ?? 0,
});
};
const toggleContainer = (containerId: string) => {
const newExpanded = new Set(expandedContainers);
if (newExpanded.has(containerId)) {
newExpanded.delete(containerId);
} else {
newExpanded.add(containerId);
}
setExpandedContainers(newExpanded);
};
const formatFileSize = (bytes: bigint | null): string => {
if (!bytes) return "Unknown size";
const b = Number(bytes);
if (b === 0) return "0 B";
const k = 1024;
const sizes = ["B", "KB", "MB", "GB", "TB"];
const i = Math.floor(Math.log(b) / Math.log(k));
return `${(b / Math.pow(k, i)).toFixed(2)} ${sizes[i]}`;
};
const formatDate = (date: Date | null): string => {
if (!date) return "Unknown date";
return new Date(date).toLocaleString();
};
const getStorageTypeIcon = (type: string) => {
switch (type) {
case "pbs":
return <Database className="h-4 w-4" />;
case "local":
return <HardDrive className="h-4 w-4" />;
default:
return <Server className="h-4 w-4" />;
}
};
const getStorageTypeBadgeVariant = (
type: string,
): "default" | "secondary" | "outline" => {
switch (type) {
case "pbs":
return "default";
case "local":
return "secondary";
default:
return "outline";
}
};
const backups = backupsData?.success ? backupsData.backups : [];
const isDiscovering = discoverMutation.isPending;
return (
<div className="space-y-6">
{/* Header with refresh button */}
<div className="flex items-center justify-between">
<div>
<h2 className="text-foreground text-2xl font-bold">Backups</h2>
<p className="text-muted-foreground mt-1 text-sm">
Discovered backups grouped by container ID
</p>
</div>
<Button
onClick={handleDiscoverBackups}
disabled={isDiscovering}
className="flex items-center gap-2"
>
<RefreshCw
className={`h-4 w-4 ${isDiscovering ? "animate-spin" : ""}`}
/>
{isDiscovering ? "Discovering..." : "Discover Backups"}
</Button>
</div>
{/* Loading state */}
{(isLoading || isDiscovering) && backups.length === 0 && (
<div className="bg-card border-border rounded-lg border p-8 text-center">
<RefreshCw className="text-muted-foreground mx-auto mb-4 h-8 w-8 animate-spin" />
<p className="text-muted-foreground">
{isDiscovering ? "Discovering backups..." : "Loading backups..."}
</p>
</div>
)}
{/* Empty state */}
{!isLoading && !isDiscovering && backups.length === 0 && (
<div className="bg-card border-border rounded-lg border p-8 text-center">
<HardDrive className="text-muted-foreground mx-auto mb-4 h-12 w-12" />
<h3 className="text-foreground mb-2 text-lg font-semibold">
No backups found
</h3>
<p className="text-muted-foreground mb-4">
Click &quot;Discover Backups&quot; to scan for backups on your
servers.
</p>
<Button onClick={handleDiscoverBackups} disabled={isDiscovering}>
<RefreshCw
className={`mr-2 h-4 w-4 ${isDiscovering ? "animate-spin" : ""}`}
/>
Discover Backups
</Button>
</div>
)}
{/* Backups list */}
{!isLoading && backups.length > 0 && (
<div className="space-y-4">
{backups.map((container: ContainerBackups) => {
const isExpanded = expandedContainers.has(container.container_id);
const backupCount = container.backups.length;
return (
<div
key={container.container_id}
className="bg-card border-border overflow-hidden rounded-lg border shadow-sm"
>
{/* Container header - collapsible */}
<button
onClick={() => toggleContainer(container.container_id)}
className="hover:bg-accent/50 flex w-full items-center justify-between p-4 text-left transition-colors"
>
<div className="flex min-w-0 flex-1 items-center gap-3">
{isExpanded ? (
<ChevronDown className="text-muted-foreground h-5 w-5 flex-shrink-0" />
) : (
<ChevronRight className="text-muted-foreground h-5 w-5 flex-shrink-0" />
)}
<div className="min-w-0 flex-1">
<div className="flex flex-wrap items-center gap-2">
<span className="text-foreground font-semibold">
CT {container.container_id}
</span>
{container.hostname && (
<>
<span className="text-muted-foreground"></span>
<span className="text-muted-foreground">
{container.hostname}
</span>
</>
)}
</div>
<p className="text-muted-foreground mt-1 text-sm">
{backupCount} {backupCount === 1 ? "backup" : "backups"}
</p>
</div>
</div>
</button>
{/* Container content - backups list */}
{isExpanded && (
<div className="border-border border-t">
<div className="space-y-3 p-4">
{container.backups.map((backup) => (
<div
key={backup.id}
className="bg-muted/50 border-border/50 rounded-lg border p-4"
>
<div className="flex items-start justify-between gap-4">
<div className="min-w-0 flex-1">
<div className="mb-2 flex flex-wrap items-center gap-2">
<span className="text-foreground font-medium break-all">
{backup.backup_name}
</span>
<Badge
variant={getStorageTypeBadgeVariant(
backup.storage_type,
)}
className="flex items-center gap-1"
>
{getStorageTypeIcon(backup.storage_type)}
{backup.storage_name}
</Badge>
</div>
<div className="text-muted-foreground flex flex-wrap items-center gap-4 text-sm">
{backup.size && (
<span className="flex items-center gap-1">
<HardDrive className="h-3 w-3" />
{formatFileSize(backup.size)}
</span>
)}
{backup.created_at && (
<span>{formatDate(backup.created_at)}</span>
)}
{backup.server_name && (
<span className="flex items-center gap-1">
<Server className="h-3 w-3" />
{backup.server_name}
</span>
)}
</div>
<div className="mt-2">
<code className="text-muted-foreground text-xs break-all">
{backup.backup_path}
</code>
</div>
</div>
<div className="flex-shrink-0">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="outline"
size="sm"
className="bg-muted/20 hover:bg-muted/30 border-muted text-muted-foreground hover:text-foreground hover:border-muted-foreground border transition-all duration-200 hover:scale-105 hover:shadow-md"
>
Actions
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="bg-card border-border w-48">
<DropdownMenuItem
onClick={() =>
handleRestoreClick(
backup,
container.container_id,
)
}
disabled={restoreMutation.isPending}
className="text-muted-foreground hover:text-foreground hover:bg-muted/20 focus:bg-muted/20"
>
Restore
</DropdownMenuItem>
<DropdownMenuItem
disabled
className="text-muted-foreground opacity-50"
>
Delete
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
</div>
</div>
))}
</div>
</div>
)}
</div>
);
})}
</div>
)}
{/* Error state */}
{backupsData && !backupsData.success && (
<div className="bg-destructive/10 border-destructive rounded-lg border p-4">
<p className="text-destructive">
Error loading backups: {backupsData.error ?? "Unknown error"}
</p>
</div>
)}
{/* Restore Confirmation Modal */}
{selectedBackup && (
<ConfirmationModal
isOpen={restoreConfirmOpen}
onClose={() => {
setRestoreConfirmOpen(false);
setSelectedBackup(null);
}}
onConfirm={handleRestoreConfirm}
title="Restore Backup"
message={`This will destroy the existing container and restore from backup. The container will be stopped during restore. This action cannot be undone and may result in data loss.`}
variant="danger"
confirmText={selectedBackup.containerId}
confirmButtonText="Restore"
cancelButtonText="Cancel"
/>
)}
{/* Restore Progress Modal */}
{(restoreMutation.isPending ||
(restoreSuccess && restoreProgress.length > 0)) && (
<LoadingModal
isOpen={true}
action={currentProgressText}
logs={restoreProgress}
isComplete={restoreSuccess}
title="Restore in progress"
onClose={() => {
setRestoreSuccess(false);
setRestoreProgress([]);
}}
/>
)}
{/* Restore Success */}
{restoreSuccess && (
<div className="bg-success/10 border-success/20 rounded-lg border p-4">
<div className="mb-2 flex items-center justify-between">
<div className="flex items-center gap-2">
<CheckCircle className="text-success h-5 w-5" />
<span className="text-success font-medium">
Restore Completed Successfully
</span>
</div>
<Button
variant="ghost"
size="sm"
onClick={() => {
setRestoreSuccess(false);
setRestoreProgress([]);
}}
className="h-6 w-6 p-0"
>
×
</Button>
</div>
<p className="text-muted-foreground text-sm">
The container has been restored from backup.
</p>
</div>
)}
{/* Restore Error */}
{restoreError && (
<div className="bg-error/10 border-error/20 rounded-lg border p-4">
<div className="mb-2 flex items-center justify-between">
<div className="flex items-center gap-2">
<AlertCircle className="text-error h-5 w-5" />
<span className="text-error font-medium">Restore Failed</span>
</div>
<Button
variant="ghost"
size="sm"
onClick={() => {
setRestoreError(null);
setRestoreProgress([]);
}}
className="h-6 w-6 p-0"
>
×
</Button>
</div>
<p className="text-muted-foreground text-sm">{restoreError}</p>
{restoreProgress.length > 0 && (
<div className="mt-2 space-y-1">
{restoreProgress.map((message, index) => (
<p key={index} className="text-muted-foreground text-sm">
{message}
</p>
))}
</div>
)}
<Button
onClick={() => {
setRestoreError(null);
setRestoreProgress([]);
}}
variant="outline"
size="sm"
className="mt-3"
>
Dismiss
</Button>
</div>
)}
</div>
);
}

View File

@@ -187,9 +187,10 @@ export function CategorySidebar({
'Miscellaneous': 'box' 'Miscellaneous': 'box'
}; };
// Sort categories by count (descending) and then alphabetically // Filter categories to only show those with scripts, then sort by count (descending) and alphabetically
const sortedCategories = categories const sortedCategories = categories
.map(category => [category, categoryCounts[category] ?? 0] as const) .map(category => [category, categoryCounts[category] ?? 0] as const)
.filter(([, count]) => count > 0) // Only show categories with at least one script
.sort(([a, countA], [b, countB]) => { .sort(([a, countA], [b, countB]) => {
if (countB !== countA) return countB - countA; if (countB !== countA) return countB - countA;
return a.localeCompare(b); return a.localeCompare(b);

View File

@@ -0,0 +1,129 @@
'use client';
import { useState, useEffect } from 'react';
import { Button } from './ui/button';
import { Input } from './ui/input';
import { Copy, X } from 'lucide-react';
import { useRegisterModal } from './modal/ModalStackProvider';
interface CloneCountInputModalProps {
isOpen: boolean;
onClose: () => void;
onSubmit: (count: number) => void;
storageName: string;
}
export function CloneCountInputModal({
isOpen,
onClose,
onSubmit,
storageName
}: CloneCountInputModalProps) {
const [cloneCount, setCloneCount] = useState<number>(1);
useRegisterModal(isOpen, { id: 'clone-count-input-modal', allowEscape: true, onClose });
useEffect(() => {
if (isOpen) {
setCloneCount(1); // Reset to default when modal opens
}
}, [isOpen]);
if (!isOpen) return null;
const handleSubmit = () => {
if (cloneCount >= 1) {
onSubmit(cloneCount);
setCloneCount(1); // Reset after submit
}
};
const handleClose = () => {
setCloneCount(1); // Reset on close
onClose();
};
return (
<div className="fixed inset-0 backdrop-blur-sm bg-black/50 flex items-center justify-center z-50 p-4">
<div className="bg-card rounded-lg shadow-xl max-w-md w-full border border-border">
{/* Header */}
<div className="flex items-center justify-between p-6 border-b border-border">
<div className="flex items-center gap-3">
<Copy className="h-6 w-6 text-primary" />
<h2 className="text-2xl font-bold text-card-foreground">Clone Count</h2>
</div>
<Button
onClick={handleClose}
variant="ghost"
size="icon"
className="text-muted-foreground hover:text-foreground"
>
<X className="h-5 w-5" />
</Button>
</div>
{/* Content */}
<div className="p-6">
<p className="text-sm text-muted-foreground mb-4">
How many clones would you like to create?
</p>
{storageName && (
<div className="mb-4 p-3 bg-muted/50 rounded-lg">
<p className="text-sm text-muted-foreground">Storage:</p>
<p className="text-sm font-medium text-foreground">{storageName}</p>
</div>
)}
<div className="space-y-2 mb-6">
<label htmlFor="cloneCount" className="block text-sm font-medium text-foreground">
Number of Clones
</label>
<Input
id="cloneCount"
type="number"
min="1"
max="100"
value={cloneCount}
onChange={(e) => {
const value = parseInt(e.target.value, 10);
if (!isNaN(value) && value >= 1 && value <= 100) {
setCloneCount(value);
} else if (e.target.value === '') {
setCloneCount(1);
}
}}
className="w-full"
placeholder="1"
/>
<p className="text-xs text-muted-foreground">
Enter a number between 1 and 100
</p>
</div>
{/* Action Buttons */}
<div className="flex flex-col sm:flex-row justify-end gap-3">
<Button
onClick={handleClose}
variant="outline"
size="default"
className="w-full sm:w-auto"
>
Cancel
</Button>
<Button
onClick={handleSubmit}
disabled={cloneCount < 1 || cloneCount > 100}
variant="default"
size="default"
className="w-full sm:w-auto"
>
Continue
</Button>
</div>
</div>
</div>
</div>
);
}

View File

@@ -1,47 +1,53 @@
'use client'; "use client";
import React, { useState, useRef, useEffect } from 'react'; import React, { useState, useRef, useEffect } from "react";
import { api } from '~/trpc/react'; import { api } from "~/trpc/react";
import { ScriptCard } from './ScriptCard'; import { ScriptCard } from "./ScriptCard";
import { ScriptCardList } from './ScriptCardList'; import { ScriptCardList } from "./ScriptCardList";
import { ScriptDetailModal } from './ScriptDetailModal'; import { ScriptDetailModal } from "./ScriptDetailModal";
import { CategorySidebar } from './CategorySidebar'; import { CategorySidebar } from "./CategorySidebar";
import { FilterBar, type FilterState } from './FilterBar'; import { FilterBar, type FilterState } from "./FilterBar";
import { ViewToggle } from './ViewToggle'; import { ViewToggle } from "./ViewToggle";
import { Button } from './ui/button'; import { Button } from "./ui/button";
import type { ScriptCard as ScriptCardType } from '~/types/script'; import type { ScriptCard as ScriptCardType } from "~/types/script";
import type { Server } from "~/types/server";
import { getDefaultFilters, mergeFiltersWithDefaults } from "./filterUtils";
interface DownloadedScriptsTabProps { interface DownloadedScriptsTabProps {
onInstallScript?: ( onInstallScript?: (
scriptPath: string, scriptPath: string,
scriptName: string, scriptName: string,
mode?: "local" | "ssh", mode?: "local" | "ssh",
server?: any, server?: Server,
) => void; ) => void;
} }
export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabProps) { export function DownloadedScriptsTab({
onInstallScript,
}: DownloadedScriptsTabProps) {
const [selectedSlug, setSelectedSlug] = useState<string | null>(null); const [selectedSlug, setSelectedSlug] = useState<string | null>(null);
const [isModalOpen, setIsModalOpen] = useState(false); const [isModalOpen, setIsModalOpen] = useState(false);
const [selectedCategory, setSelectedCategory] = useState<string | null>(null); const [selectedCategory, setSelectedCategory] = useState<string | null>(null);
const [viewMode, setViewMode] = useState<'card' | 'list'>('card'); const [viewMode, setViewMode] = useState<"card" | "list">("card");
const [filters, setFilters] = useState<FilterState>({ const [filters, setFilters] = useState<FilterState>(getDefaultFilters());
searchQuery: '',
showUpdatable: null,
selectedTypes: [],
selectedRepositories: [],
sortBy: 'name',
sortOrder: 'asc',
});
const [saveFiltersEnabled, setSaveFiltersEnabled] = useState(false); const [saveFiltersEnabled, setSaveFiltersEnabled] = useState(false);
const [isLoadingFilters, setIsLoadingFilters] = useState(true); const [isLoadingFilters, setIsLoadingFilters] = useState(true);
const gridRef = useRef<HTMLDivElement>(null); const gridRef = useRef<HTMLDivElement>(null);
const { data: scriptCardsData, isLoading: githubLoading, error: githubError, refetch } = api.scripts.getScriptCardsWithCategories.useQuery(); const {
const { data: localScriptsData, isLoading: localLoading, error: localError } = api.scripts.getAllDownloadedScripts.useQuery(); data: scriptCardsData,
isLoading: githubLoading,
error: githubError,
refetch,
} = api.scripts.getScriptCardsWithCategories.useQuery();
const {
data: localScriptsData,
isLoading: localLoading,
error: localError,
} = api.scripts.getAllDownloadedScripts.useQuery();
const { data: scriptData } = api.scripts.getScriptBySlug.useQuery( const { data: scriptData } = api.scripts.getScriptBySlug.useQuery(
{ slug: selectedSlug ?? '' }, { slug: selectedSlug ?? "" },
{ enabled: !!selectedSlug } { enabled: !!selectedSlug },
); );
// Load SAVE_FILTER setting, saved filters, and view mode on component mount // Load SAVE_FILTER setting, saved filters, and view mode on component mount
@@ -49,7 +55,7 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
const loadSettings = async () => { const loadSettings = async () => {
try { try {
// Load SAVE_FILTER setting // Load SAVE_FILTER setting
const saveFilterResponse = await fetch('/api/settings/save-filter'); const saveFilterResponse = await fetch("/api/settings/save-filter");
let saveFilterEnabled = false; let saveFilterEnabled = false;
if (saveFilterResponse.ok) { if (saveFilterResponse.ok) {
const saveFilterData = await saveFilterResponse.json(); const saveFilterData = await saveFilterResponse.json();
@@ -59,26 +65,32 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
// Load saved filters if SAVE_FILTER is enabled // Load saved filters if SAVE_FILTER is enabled
if (saveFilterEnabled) { if (saveFilterEnabled) {
const filtersResponse = await fetch('/api/settings/filters'); const filtersResponse = await fetch("/api/settings/filters");
if (filtersResponse.ok) { if (filtersResponse.ok) {
const filtersData = await filtersResponse.json(); const filtersData = (await filtersResponse.json()) as {
filters?: Partial<FilterState>;
};
if (filtersData.filters) { if (filtersData.filters) {
setFilters(filtersData.filters as FilterState); setFilters(mergeFiltersWithDefaults(filtersData.filters));
} }
} }
} }
// Load view mode // Load view mode
const viewModeResponse = await fetch('/api/settings/view-mode'); const viewModeResponse = await fetch("/api/settings/view-mode");
if (viewModeResponse.ok) { if (viewModeResponse.ok) {
const viewModeData = await viewModeResponse.json(); const viewModeData = await viewModeResponse.json();
const viewMode = viewModeData.viewMode; const viewMode = viewModeData.viewMode;
if (viewMode && typeof viewMode === 'string' && (viewMode === 'card' || viewMode === 'list')) { if (
viewMode &&
typeof viewMode === "string" &&
(viewMode === "card" || viewMode === "list")
) {
setViewMode(viewMode); setViewMode(viewMode);
} }
} }
} catch (error) { } catch (error) {
console.error('Error loading settings:', error); console.error("Error loading settings:", error);
} finally { } finally {
setIsLoadingFilters(false); setIsLoadingFilters(false);
} }
@@ -93,15 +105,15 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
const saveFilters = async () => { const saveFilters = async () => {
try { try {
await fetch('/api/settings/filters', { await fetch("/api/settings/filters", {
method: 'POST', method: "POST",
headers: { headers: {
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
body: JSON.stringify({ filters }), body: JSON.stringify({ filters }),
}); });
} catch (error) { } catch (error) {
console.error('Error saving filters:', error); console.error("Error saving filters:", error);
} }
}; };
@@ -116,15 +128,15 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
const saveViewMode = async () => { const saveViewMode = async () => {
try { try {
await fetch('/api/settings/view-mode', { await fetch("/api/settings/view-mode", {
method: 'POST', method: "POST",
headers: { headers: {
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
body: JSON.stringify({ viewMode }), body: JSON.stringify({ viewMode }),
}); });
} catch (error) { } catch (error) {
console.error('Error saving view mode:', error); console.error("Error saving view mode:", error);
} }
}; };
@@ -135,31 +147,32 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
// Extract categories from metadata // Extract categories from metadata
const categories = React.useMemo((): string[] => { const categories = React.useMemo((): string[] => {
if (!scriptCardsData?.success || !scriptCardsData.metadata?.categories) return []; if (!scriptCardsData?.success || !scriptCardsData.metadata?.categories)
return [];
return (scriptCardsData.metadata.categories as any[]) return (scriptCardsData.metadata.categories as any[])
.filter((cat) => cat.id !== 0) // Exclude Miscellaneous for main list .filter((cat) => cat.id !== 0) // Exclude Miscellaneous for main list
.sort((a, b) => a.sort_order - b.sort_order) .sort((a, b) => a.sort_order - b.sort_order)
.map((cat) => cat.name as string) .map((cat) => cat.name as string)
.filter((name): name is string => typeof name === 'string'); .filter((name): name is string => typeof name === "string");
}, [scriptCardsData]); }, [scriptCardsData]);
// Get GitHub scripts with download status (deduplicated) // Get GitHub scripts with download status (deduplicated)
const combinedScripts = React.useMemo((): ScriptCardType[] => { const combinedScripts = React.useMemo((): ScriptCardType[] => {
if (!scriptCardsData?.success) return []; if (!scriptCardsData?.success) return [];
// Use Map to deduplicate by slug/name // Use Map to deduplicate by slug/name
const scriptMap = new Map<string, ScriptCardType>(); const scriptMap = new Map<string, ScriptCardType>();
scriptCardsData.cards?.forEach(script => { scriptCardsData.cards?.forEach((script: ScriptCardType) => {
if (script?.name && script?.slug) { if (script?.name && script?.slug) {
// Use slug as unique identifier, only keep first occurrence // Use slug as unique identifier, only keep first occurrence
if (!scriptMap.has(script.slug)) { if (!scriptMap.has(script.slug)) {
scriptMap.set(script.slug, { scriptMap.set(script.slug, {
...script, ...script,
source: 'github' as const, source: "github" as const,
isDownloaded: false, // Will be updated by status check isDownloaded: false, // Will be updated by status check
isUpToDate: false, // Will be updated by status check isUpToDate: false, // Will be updated by status check
}); });
} }
} }
@@ -171,68 +184,77 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
// Update scripts with download status and filter to only downloaded scripts // Update scripts with download status and filter to only downloaded scripts
const downloadedScripts = React.useMemo((): ScriptCardType[] => { const downloadedScripts = React.useMemo((): ScriptCardType[] => {
// Helper to normalize identifiers so underscores vs hyphens don't break matches // Helper to normalize identifiers so underscores vs hyphens don't break matches
const normalizeId = (s?: string): string => (s ?? '') const normalizeId = (s?: string): string =>
.toLowerCase() (s ?? "")
.replace(/\.(sh|bash|py|js|ts)$/g, '') .toLowerCase()
.replace(/[^a-z0-9]+/g, '-') .replace(/\.(sh|bash|py|js|ts)$/g, "")
.replace(/^-+|-+$/g, ''); .replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
return combinedScripts return combinedScripts
.map(script => { .map((script) => {
if (!script?.name) { if (!script?.name) {
return script; // Return as-is if invalid return script; // Return as-is if invalid
} }
// Check if there's a corresponding local script // Check if there's a corresponding local script
const hasLocalVersion = localScriptsData?.scripts?.some(local => { const hasLocalVersion =
if (!local?.name) return false; localScriptsData?.scripts?.some((local) => {
if (!local?.name) return false;
// Primary: Exact slug-to-slug matching (most reliable, prevents false positives)
if (local.slug && script.slug) { // Primary: Exact slug-to-slug matching (most reliable, prevents false positives)
if (local.slug.toLowerCase() === script.slug.toLowerCase()) { if (local.slug && script.slug) {
return true; if (local.slug.toLowerCase() === script.slug.toLowerCase()) {
return true;
}
} }
}
// Secondary: Check install basenames (for edge cases where install script names differ from slugs)
// Secondary: Check install basenames (for edge cases where install script names differ from slugs) // Only use normalized matching for install basenames, not for slug/name matching
// Only use normalized matching for install basenames, not for slug/name matching const normalizedLocal = normalizeId(local.name);
const normalizedLocal = normalizeId(local.name); const matchesInstallBasename =
const matchesInstallBasename = (script as any)?.install_basenames?.some((base: string) => normalizeId(base) === normalizedLocal) ?? false; (script as any)?.install_basenames?.some(
return matchesInstallBasename; (base: string) => normalizeId(base) === normalizedLocal,
}) ?? false; ) ?? false;
return matchesInstallBasename;
}) ?? false;
return { return {
...script, ...script,
isDownloaded: hasLocalVersion, isDownloaded: hasLocalVersion,
}; };
}) })
.filter(script => script.isDownloaded); // Only show downloaded scripts .filter((script) => script.isDownloaded); // Only show downloaded scripts
}, [combinedScripts, localScriptsData]); }, [combinedScripts, localScriptsData]);
// Count scripts per category (using downloaded scripts only) // Count scripts per category (using downloaded scripts only)
const categoryCounts = React.useMemo((): Record<string, number> => { const categoryCounts = React.useMemo((): Record<string, number> => {
if (!scriptCardsData?.success) return {}; if (!scriptCardsData?.success) return {};
const counts: Record<string, number> = {}; const counts: Record<string, number> = {};
// Initialize all categories with 0 // Initialize all categories with 0
categories.forEach((categoryName: string) => { categories.forEach((categoryName: string) => {
counts[categoryName] = 0; counts[categoryName] = 0;
}); });
// Count each unique downloaded script only once per category // Count each unique downloaded script only once per category
downloadedScripts.forEach(script => { downloadedScripts.forEach((script) => {
if (script.categoryNames && script.slug) { if (script.categoryNames && script.slug) {
const countedCategories = new Set<string>(); const countedCategories = new Set<string>();
script.categoryNames.forEach((categoryName: unknown) => { script.categoryNames.forEach((categoryName: unknown) => {
if (typeof categoryName === 'string' && counts[categoryName] !== undefined && !countedCategories.has(categoryName)) { if (
typeof categoryName === "string" &&
counts[categoryName] !== undefined &&
!countedCategories.has(categoryName)
) {
countedCategories.add(categoryName); countedCategories.add(categoryName);
counts[categoryName]++; counts[categoryName]++;
} }
}); });
} }
}); });
return counts; return counts;
}, [categories, downloadedScripts, scriptCardsData?.success]); }, [categories, downloadedScripts, scriptCardsData?.success]);
@@ -243,15 +265,15 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
// Filter by search query // Filter by search query
if (filters.searchQuery?.trim()) { if (filters.searchQuery?.trim()) {
const query = filters.searchQuery.toLowerCase().trim(); const query = filters.searchQuery.toLowerCase().trim();
if (query.length >= 1) { if (query.length >= 1) {
scripts = scripts.filter(script => { scripts = scripts.filter((script) => {
if (!script || typeof script !== 'object') { if (!script || typeof script !== "object") {
return false; return false;
} }
const name = (script.name ?? '').toLowerCase(); const name = (script.name ?? "").toLowerCase();
const slug = (script.slug ?? '').toLowerCase(); const slug = (script.slug ?? "").toLowerCase();
return name.includes(query) ?? slug.includes(query); return name.includes(query) ?? slug.includes(query);
}); });
@@ -260,9 +282,9 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
// Filter by category using real category data from downloaded scripts // Filter by category using real category data from downloaded scripts
if (selectedCategory) { if (selectedCategory) {
scripts = scripts.filter(script => { scripts = scripts.filter((script) => {
if (!script) return false; if (!script) return false;
// Check if the downloaded script has categoryNames that include the selected category // Check if the downloaded script has categoryNames that include the selected category
return script.categoryNames?.includes(selectedCategory) ?? false; return script.categoryNames?.includes(selectedCategory) ?? false;
}); });
@@ -270,7 +292,7 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
// Filter by updateable status // Filter by updateable status
if (filters.showUpdatable !== null) { if (filters.showUpdatable !== null) {
scripts = scripts.filter(script => { scripts = scripts.filter((script) => {
if (!script) return false; if (!script) return false;
const isUpdatable = script.updateable ?? false; const isUpdatable = script.updateable ?? false;
return filters.showUpdatable ? isUpdatable : !isUpdatable; return filters.showUpdatable ? isUpdatable : !isUpdatable;
@@ -279,28 +301,30 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
// Filter by script types // Filter by script types
if (filters.selectedTypes.length > 0) { if (filters.selectedTypes.length > 0) {
scripts = scripts.filter(script => { scripts = scripts.filter((script) => {
if (!script) return false; if (!script) return false;
const scriptType = (script.type ?? '').toLowerCase(); const scriptType = (script.type ?? "").toLowerCase();
// Map non-standard types to standard categories // Map non-standard types to standard categories
const mappedType = scriptType === 'turnkey' ? 'ct' : scriptType; const mappedType = scriptType === "turnkey" ? "ct" : scriptType;
return filters.selectedTypes.some(type => type.toLowerCase() === mappedType); return filters.selectedTypes.some(
(type) => type.toLowerCase() === mappedType,
);
}); });
} }
// Filter by repositories // Filter by repositories
if (filters.selectedRepositories.length > 0) { if (filters.selectedRepositories.length > 0) {
scripts = scripts.filter(script => { scripts = scripts.filter((script) => {
if (!script) return false; if (!script) return false;
const repoUrl = script.repository_url; const repoUrl = script.repository_url;
// If script has no repository_url, exclude it when filtering by repositories // If script has no repository_url, exclude it when filtering by repositories
if (!repoUrl) { if (!repoUrl) {
return false; return false;
} }
// Only include scripts from selected repositories // Only include scripts from selected repositories
return filters.selectedRepositories.includes(repoUrl); return filters.selectedRepositories.includes(repoUrl);
}); });
@@ -309,18 +333,18 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
// Apply sorting // Apply sorting
scripts.sort((a, b) => { scripts.sort((a, b) => {
if (!a || !b) return 0; if (!a || !b) return 0;
let compareValue = 0; let compareValue = 0;
switch (filters.sortBy) { switch (filters.sortBy) {
case 'name': case "name":
compareValue = (a.name ?? '').localeCompare(b.name ?? ''); compareValue = (a.name ?? "").localeCompare(b.name ?? "");
break; break;
case 'created': case "created":
// Get creation date from script metadata in JSON format (date_created: "YYYY-MM-DD") // Get creation date from script metadata in JSON format (date_created: "YYYY-MM-DD")
const aCreated = a?.date_created ?? ''; const aCreated = a?.date_created ?? "";
const bCreated = b?.date_created ?? ''; const bCreated = b?.date_created ?? "";
// If both have dates, compare them directly // If both have dates, compare them directly
if (aCreated && bCreated) { if (aCreated && bCreated) {
// For dates: asc = oldest first (2020 before 2024), desc = newest first (2024 before 2020) // For dates: asc = oldest first (2020 before 2024), desc = newest first (2024 before 2020)
@@ -333,15 +357,15 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
compareValue = 1; compareValue = 1;
} else { } else {
// Both have no dates, fallback to name comparison // Both have no dates, fallback to name comparison
compareValue = (a.name ?? '').localeCompare(b.name ?? ''); compareValue = (a.name ?? "").localeCompare(b.name ?? "");
} }
break; break;
default: default:
compareValue = (a.name ?? '').localeCompare(b.name ?? ''); compareValue = (a.name ?? "").localeCompare(b.name ?? "");
} }
// Apply sort order // Apply sort order
return filters.sortOrder === 'asc' ? compareValue : -compareValue; return filters.sortOrder === "asc" ? compareValue : -compareValue;
}); });
return scripts; return scripts;
@@ -349,8 +373,10 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
// Calculate filter counts for FilterBar // Calculate filter counts for FilterBar
const filterCounts = React.useMemo(() => { const filterCounts = React.useMemo(() => {
const updatableCount = downloadedScripts.filter(script => script?.updateable).length; const updatableCount = downloadedScripts.filter(
(script) => script?.updateable,
).length;
return { installedCount: downloadedScripts.length, updatableCount }; return { installedCount: downloadedScripts.length, updatableCount };
}, [downloadedScripts]); }, [downloadedScripts]);
@@ -368,13 +394,13 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
useEffect(() => { useEffect(() => {
if (selectedCategory && gridRef.current) { if (selectedCategory && gridRef.current) {
const timeoutId = setTimeout(() => { const timeoutId = setTimeout(() => {
gridRef.current?.scrollIntoView({ gridRef.current?.scrollIntoView({
behavior: 'smooth', behavior: "smooth",
block: 'start', block: "start",
inline: 'nearest' inline: "nearest",
}); });
}, 100); }, 100);
return () => clearTimeout(timeoutId); return () => clearTimeout(timeoutId);
} }
}, [selectedCategory]); }, [selectedCategory]);
@@ -393,22 +419,38 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
if (githubLoading || localLoading) { if (githubLoading || localLoading) {
return ( return (
<div className="flex items-center justify-center py-12"> <div className="flex items-center justify-center py-12">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary"></div> <div className="border-primary h-8 w-8 animate-spin rounded-full border-b-2"></div>
<span className="ml-2 text-muted-foreground">Loading downloaded scripts...</span> <span className="text-muted-foreground ml-2">
Loading downloaded scripts...
</span>
</div> </div>
); );
} }
if (githubError || localError) { if (githubError || localError) {
return ( return (
<div className="text-center py-12"> <div className="py-12 text-center">
<div className="text-error mb-4"> <div className="text-error mb-4">
<svg className="w-12 h-12 mx-auto mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <svg
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 16.5c-.77.833.192 2.5 1.732 2.5z" /> className="mx-auto mb-2 h-12 w-12"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 16.5c-.77.833.192 2.5 1.732 2.5z"
/>
</svg> </svg>
<p className="text-lg font-medium">Failed to load downloaded scripts</p> <p className="text-lg font-medium">
<p className="text-sm text-muted-foreground mt-1"> Failed to load downloaded scripts
{githubError?.message ?? localError?.message ?? 'Unknown error occurred'} </p>
<p className="text-muted-foreground mt-1 text-sm">
{githubError?.message ??
localError?.message ??
"Unknown error occurred"}
</p> </p>
</div> </div>
<Button <Button
@@ -425,14 +467,25 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
if (!downloadedScripts?.length) { if (!downloadedScripts?.length) {
return ( return (
<div className="text-center py-12"> <div className="py-12 text-center">
<div className="text-muted-foreground"> <div className="text-muted-foreground">
<svg className="w-12 h-12 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <svg
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" /> className="mx-auto mb-4 h-12 w-12"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
/>
</svg> </svg>
<p className="text-lg font-medium">No downloaded scripts found</p> <p className="text-lg font-medium">No downloaded scripts found</p>
<p className="text-sm text-muted-foreground mt-1"> <p className="text-muted-foreground mt-1 text-sm">
You haven&apos;t downloaded any scripts yet. Visit the Available Scripts tab to download some scripts. You haven&apos;t downloaded any scripts yet. Visit the Available
Scripts tab to download some scripts.
</p> </p>
</div> </div>
</div> </div>
@@ -441,12 +494,9 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
return ( return (
<div className="space-y-6"> <div className="space-y-6">
<div className="flex flex-col gap-4 lg:flex-row lg:gap-6">
<div className="flex flex-col lg:flex-row gap-4 lg:gap-6">
{/* Category Sidebar */} {/* Category Sidebar */}
<div className="flex-shrink-0 order-2 lg:order-1"> <div className="order-2 flex-shrink-0 lg:order-1">
<CategorySidebar <CategorySidebar
categories={categories} categories={categories}
categoryCounts={categoryCounts} categoryCounts={categoryCounts}
@@ -457,7 +507,7 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
</div> </div>
{/* Main Content */} {/* Main Content */}
<div className="flex-1 min-w-0 order-1 lg:order-2" ref={gridRef}> <div className="order-1 min-w-0 flex-1 lg:order-2" ref={gridRef}>
{/* Enhanced Filter Bar */} {/* Enhanced Filter Bar */}
<FilterBar <FilterBar
filters={filters} filters={filters}
@@ -470,26 +520,41 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
/> />
{/* View Toggle */} {/* View Toggle */}
<ViewToggle <ViewToggle viewMode={viewMode} onViewModeChange={setViewMode} />
viewMode={viewMode}
onViewModeChange={setViewMode}
/>
{/* Scripts Grid */} {/* Scripts Grid */}
{filteredScripts.length === 0 && (filters.searchQuery || selectedCategory || filters.showUpdatable !== null || filters.selectedTypes.length > 0) ? ( {filteredScripts.length === 0 &&
<div className="text-center py-12"> (filters.searchQuery ||
selectedCategory ||
filters.showUpdatable !== null ||
filters.selectedTypes.length > 0) ? (
<div className="py-12 text-center">
<div className="text-muted-foreground"> <div className="text-muted-foreground">
<svg className="w-12 h-12 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <svg
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" /> className="mx-auto mb-4 h-12 w-12"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
/>
</svg> </svg>
<p className="text-lg font-medium">No matching downloaded scripts found</p> <p className="text-lg font-medium">
<p className="text-sm text-muted-foreground mt-1"> No matching downloaded scripts found
</p>
<p className="text-muted-foreground mt-1 text-sm">
Try different filter settings or clear all filters. Try different filter settings or clear all filters.
</p> </p>
<div className="flex justify-center gap-2 mt-4"> <div className="mt-4 flex justify-center gap-2">
{filters.searchQuery && ( {filters.searchQuery && (
<Button <Button
onClick={() => handleFiltersChange({ ...filters, searchQuery: '' })} onClick={() =>
handleFiltersChange({ ...filters, searchQuery: "" })
}
variant="default" variant="default"
size="default" size="default"
> >
@@ -508,18 +573,17 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
</div> </div>
</div> </div>
</div> </div>
) : ( ) : viewMode === "card" ? (
viewMode === 'card' ? ( <div className="grid grid-cols-1 gap-6 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6"> {filteredScripts.map((script, index) => {
{filteredScripts.map((script, index) => {
// Add validation to ensure script has required properties // Add validation to ensure script has required properties
if (!script || typeof script !== 'object') { if (!script || typeof script !== "object") {
return null; return null;
} }
// Create a unique key by combining slug, name, and index to handle duplicates // Create a unique key by combining slug, name, and index to handle duplicates
const uniqueKey = `${script.slug ?? 'unknown'}-${script.name ?? 'unnamed'}-${index}`; const uniqueKey = `${script.slug ?? "unknown"}-${script.name ?? "unnamed"}-${index}`;
return ( return (
<ScriptCard <ScriptCard
key={uniqueKey} key={uniqueKey}
@@ -528,18 +592,18 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
/> />
); );
})} })}
</div> </div>
) : ( ) : (
<div className="space-y-3"> <div className="space-y-3">
{filteredScripts.map((script, index) => { {filteredScripts.map((script, index) => {
// Add validation to ensure script has required properties // Add validation to ensure script has required properties
if (!script || typeof script !== 'object') { if (!script || typeof script !== "object") {
return null; return null;
} }
// Create a unique key by combining slug, name, and index to handle duplicates // Create a unique key by combining slug, name, and index to handle duplicates
const uniqueKey = `${script.slug ?? 'unknown'}-${script.name ?? 'unnamed'}-${index}`; const uniqueKey = `${script.slug ?? "unknown"}-${script.name ?? "unnamed"}-${index}`;
return ( return (
<ScriptCardList <ScriptCardList
key={uniqueKey} key={uniqueKey}
@@ -548,8 +612,7 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
/> />
); );
})} })}
</div> </div>
)
)} )}
<ScriptDetailModal <ScriptDetailModal

View File

@@ -3,8 +3,19 @@
import React, { useState } from "react"; import React, { useState } from "react";
import { Button } from "./ui/button"; import { Button } from "./ui/button";
import { ContextualHelpIcon } from "./ContextualHelpIcon"; import { ContextualHelpIcon } from "./ContextualHelpIcon";
import { Package, Monitor, Wrench, Server, FileText, Calendar, RefreshCw, Filter, GitBranch } from "lucide-react"; import {
Package,
Monitor,
Wrench,
Server,
FileText,
Calendar,
RefreshCw,
Filter,
GitBranch,
} from "lucide-react";
import { api } from "~/trpc/react"; import { api } from "~/trpc/react";
import { getDefaultFilters } from "./filterUtils";
export interface FilterState { export interface FilterState {
searchQuery: string; searchQuery: string;
@@ -48,11 +59,11 @@ export function FilterBar({
// Fetch enabled repositories // Fetch enabled repositories
const { data: enabledReposData } = api.repositories.getEnabled.useQuery(); const { data: enabledReposData } = api.repositories.getEnabled.useQuery();
const enabledRepos = enabledReposData?.repositories ?? []; const enabledRepos = enabledReposData?.repositories ?? [];
// Helper function to extract repository name from URL // Helper function to extract repository name from URL
const getRepoName = (url: string): string => { const getRepoName = (url: string): string => {
try { try {
const match = url.match(/github\.com\/([^\/]+)\/([^\/]+)/); const match = /github\.com\/([^\/]+)\/([^\/]+)/.exec(url);
if (match) { if (match) {
return `${match[1]}/${match[2]}`; return `${match[1]}/${match[2]}`;
} }
@@ -67,14 +78,7 @@ export function FilterBar({
}; };
const clearAllFilters = () => { const clearAllFilters = () => {
onFiltersChange({ onFiltersChange(getDefaultFilters());
searchQuery: "",
showUpdatable: null,
selectedTypes: [],
selectedRepositories: [],
sortBy: "name",
sortOrder: "asc",
});
}; };
const hasActiveFilters = const hasActiveFilters =
@@ -104,29 +108,33 @@ export function FilterBar({
}; };
return ( return (
<div className="mb-6 rounded-lg border border-border bg-card p-4 sm:p-6 shadow-sm"> <div className="border-border bg-card mb-6 rounded-lg border p-4 shadow-sm sm:p-6">
{/* Loading State */} {/* Loading State */}
{isLoadingFilters && ( {isLoadingFilters && (
<div className="mb-4 flex items-center justify-center py-2"> <div className="mb-4 flex items-center justify-center py-2">
<div className="flex items-center space-x-2 text-sm text-muted-foreground"> <div className="text-muted-foreground flex items-center space-x-2 text-sm">
<div className="h-4 w-4 animate-spin rounded-full border-2 border-primary border-t-transparent"></div> <div className="border-primary h-4 w-4 animate-spin rounded-full border-2 border-t-transparent"></div>
<span>Loading saved filters...</span> <span>Loading saved filters...</span>
</div> </div>
</div> </div>
)} )}
{/* Filter Header */} {/* Filter Header */}
{!isLoadingFilters && ( {!isLoadingFilters && (
<div className="mb-4 flex items-center justify-between"> <div className="mb-4 flex items-center justify-between">
<h3 className="text-lg font-medium text-foreground">Filter Scripts</h3> <h3 className="text-foreground text-lg font-medium">
Filter Scripts
</h3>
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<ContextualHelpIcon section="available-scripts" tooltip="Help with filtering and searching" /> <ContextualHelpIcon
section="available-scripts"
tooltip="Help with filtering and searching"
/>
<Button <Button
onClick={() => setIsMinimized(!isMinimized)} onClick={() => setIsMinimized(!isMinimized)}
variant="ghost" variant="ghost"
size="icon" size="icon"
className="h-8 w-8 text-muted-foreground hover:text-foreground" className="text-muted-foreground hover:text-foreground h-8 w-8"
title={isMinimized ? "Expand filters" : "Minimize filters"} title={isMinimized ? "Expand filters" : "Minimize filters"}
> >
<svg <svg
@@ -152,10 +160,10 @@ export function FilterBar({
<> <>
{/* Search Bar */} {/* Search Bar */}
<div className="mb-4"> <div className="mb-4">
<div className="relative max-w-md w-full"> <div className="relative w-full max-w-md">
<div className="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3"> <div className="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
<svg <svg
className="h-5 w-5 text-muted-foreground" className="text-muted-foreground h-5 w-5"
fill="none" fill="none"
stroke="currentColor" stroke="currentColor"
viewBox="0 0 24 24" viewBox="0 0 24 24"
@@ -173,13 +181,13 @@ export function FilterBar({
placeholder="Search scripts..." placeholder="Search scripts..."
value={filters.searchQuery} value={filters.searchQuery}
onChange={(e) => updateFilters({ searchQuery: e.target.value })} onChange={(e) => updateFilters({ searchQuery: e.target.value })}
className="block w-full rounded-lg border border-input bg-background py-3 pr-10 pl-10 text-sm leading-5 text-foreground placeholder-muted-foreground focus:border-primary focus:placeholder-muted-foreground focus:ring-2 focus:ring-primary focus:outline-none" className="border-input bg-background text-foreground placeholder-muted-foreground focus:border-primary focus:placeholder-muted-foreground focus:ring-primary block w-full rounded-lg border py-3 pr-10 pl-10 text-sm leading-5 focus:ring-2 focus:outline-none"
/> />
{filters.searchQuery && ( {filters.searchQuery && (
<Button <Button
onClick={() => updateFilters({ searchQuery: "" })} onClick={() => updateFilters({ searchQuery: "" })}
variant="ghost" variant="ghost"
className="absolute inset-y-0 right-0 flex items-center justify-center pr-3 h-full text-muted-foreground hover:text-foreground" className="text-muted-foreground hover:text-foreground absolute inset-y-0 right-0 flex h-full items-center justify-center pr-3"
> >
<svg <svg
className="h-5 w-5" className="h-5 w-5"
@@ -200,318 +208,335 @@ export function FilterBar({
</div> </div>
{/* Filter Buttons */} {/* Filter Buttons */}
<div className="mb-4 flex flex-col sm:flex-row flex-wrap gap-2 sm:gap-3"> <div className="mb-4 flex flex-col flex-wrap gap-2 sm:flex-row sm:gap-3">
{/* Updateable Filter */} {/* Updateable Filter */}
<Button
onClick={() => {
const next =
filters.showUpdatable === null
? true
: filters.showUpdatable === true
? false
: null;
updateFilters({ showUpdatable: next });
}}
variant="outline"
size="default"
className={`w-full sm:w-auto flex items-center justify-center space-x-2 ${
filters.showUpdatable === null
? "bg-muted text-muted-foreground hover:bg-accent hover:text-accent-foreground"
: filters.showUpdatable === true
? "border border-success/20 bg-success/10 text-success"
: "border border-destructive/20 bg-destructive/10 text-destructive"
}`}
>
<RefreshCw className="h-4 w-4" />
<span>{getUpdatableButtonText()}</span>
</Button>
{/* Type Dropdown */}
<div className="relative w-full sm:w-auto">
<Button
onClick={() => setIsTypeDropdownOpen(!isTypeDropdownOpen)}
variant="outline"
size="default"
className={`w-full flex items-center justify-center space-x-2 ${
filters.selectedTypes.length === 0
? "bg-muted text-muted-foreground hover:bg-accent hover:text-accent-foreground"
: "border border-primary/20 bg-primary/10 text-primary"
}`}
>
<Filter className="h-4 w-4" />
<span>{getTypeButtonText()}</span>
<svg
className={`h-4 w-4 transition-transform ${isTypeDropdownOpen ? "rotate-180" : ""}`}
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M19 9l-7 7-7-7"
/>
</svg>
</Button>
{isTypeDropdownOpen && (
<div className="absolute top-full left-0 z-10 mt-1 w-48 rounded-lg border border-border bg-card shadow-lg">
<div className="p-2">
{SCRIPT_TYPES.map((type) => {
const IconComponent = type.Icon;
return (
<label
key={type.value}
className="flex cursor-pointer items-center space-x-3 rounded-md px-3 py-2 hover:bg-accent"
>
<input
type="checkbox"
checked={filters.selectedTypes.includes(type.value)}
onChange={(e) => {
if (e.target.checked) {
updateFilters({
selectedTypes: [
...filters.selectedTypes,
type.value,
],
});
} else {
updateFilters({
selectedTypes: filters.selectedTypes.filter(
(t) => t !== type.value,
),
});
}
}}
className="rounded border-input text-primary focus:ring-primary"
/>
<IconComponent className="h-4 w-4" />
<span className="text-sm text-muted-foreground">
{type.label}
</span>
</label>
);
})}
</div>
<div className="border-t border-border p-2">
<Button
onClick={() => {
updateFilters({ selectedTypes: [] });
setIsTypeDropdownOpen(false);
}}
variant="ghost"
size="sm"
className="w-full justify-start text-muted-foreground hover:bg-accent hover:text-foreground"
>
Clear all
</Button>
</div>
</div>
)}
</div>
{/* Repository Filter Buttons - Only show if more than one enabled repo */}
{enabledRepos.length > 1 && enabledRepos.map((repo) => {
const isSelected = filters.selectedRepositories.includes(repo.url);
return (
<Button <Button
key={repo.id}
onClick={() => { onClick={() => {
const currentSelected = filters.selectedRepositories; const next =
if (isSelected) { filters.showUpdatable === null
// Remove repository from selection ? true
updateFilters({ : filters.showUpdatable === true
selectedRepositories: currentSelected.filter(url => url !== repo.url) ? false
}); : null;
} else { updateFilters({ showUpdatable: next });
// Add repository to selection
updateFilters({
selectedRepositories: [...currentSelected, repo.url]
});
}
}} }}
variant="outline" variant="outline"
size="default" size="default"
className={`w-full sm:w-auto flex items-center justify-center space-x-2 ${ className={`flex w-full items-center justify-center space-x-2 sm:w-auto ${
isSelected filters.showUpdatable === null
? "border border-primary/20 bg-primary/10 text-primary" ? "bg-muted text-muted-foreground hover:bg-accent hover:text-accent-foreground"
: "bg-muted text-muted-foreground hover:bg-accent hover:text-accent-foreground" : filters.showUpdatable === true
? "border-success/20 bg-success/10 text-success border"
: "border-destructive/20 bg-destructive/10 text-destructive border"
}`} }`}
> >
<GitBranch className="h-4 w-4" /> <RefreshCw className="h-4 w-4" />
<span>{getRepoName(repo.url)}</span> <span>{getUpdatableButtonText()}</span>
</Button> </Button>
);
})}
{/* Sort By Dropdown */} {/* Type Dropdown */}
<div className="relative w-full sm:w-auto"> <div className="relative w-full sm:w-auto">
<Button <Button
onClick={() => setIsSortDropdownOpen(!isSortDropdownOpen)} onClick={() => setIsTypeDropdownOpen(!isTypeDropdownOpen)}
variant="outline" variant="outline"
size="default" size="default"
className="w-full sm:w-auto flex items-center justify-center space-x-2 bg-muted text-muted-foreground hover:bg-accent hover:text-accent-foreground" className={`flex w-full items-center justify-center space-x-2 ${
> filters.selectedTypes.length === 0
{filters.sortBy === "name" ? ( ? "bg-muted text-muted-foreground hover:bg-accent hover:text-accent-foreground"
<FileText className="h-4 w-4" /> : "border-primary/20 bg-primary/10 text-primary border"
) : ( }`}
<Calendar className="h-4 w-4" /> >
)} <Filter className="h-4 w-4" />
<span>{filters.sortBy === "name" ? "By Name" : "By Created Date"}</span> <span>{getTypeButtonText()}</span>
<svg <svg
className={`h-4 w-4 transition-transform ${isSortDropdownOpen ? "rotate-180" : ""}`} className={`h-4 w-4 transition-transform ${isTypeDropdownOpen ? "rotate-180" : ""}`}
fill="none" fill="none"
stroke="currentColor" stroke="currentColor"
viewBox="0 0 24 24" viewBox="0 0 24 24"
> >
<path <path
strokeLinecap="round" strokeLinecap="round"
strokeLinejoin="round" strokeLinejoin="round"
strokeWidth={2} strokeWidth={2}
d="M19 9l-7 7-7-7" d="M19 9l-7 7-7-7"
/> />
</svg> </svg>
</Button> </Button>
{isSortDropdownOpen && ( {isTypeDropdownOpen && (
<div className="absolute top-full left-0 z-10 mt-1 w-full sm:w-48 rounded-lg border border-border bg-card shadow-lg"> <div className="border-border bg-card absolute top-full left-0 z-10 mt-1 w-48 rounded-lg border shadow-lg">
<div className="p-2"> <div className="p-2">
<button {SCRIPT_TYPES.map((type) => {
onClick={() => { const IconComponent = type.Icon;
updateFilters({ sortBy: "name" }); return (
setIsSortDropdownOpen(false); <label
}} key={type.value}
className={`w-full flex items-center space-x-3 rounded-md px-3 py-2 text-left hover:bg-accent ${ className="hover:bg-accent flex cursor-pointer items-center space-x-3 rounded-md px-3 py-2"
filters.sortBy === "name" ? "bg-primary/10 text-primary" : "text-muted-foreground" >
}`} <input
> type="checkbox"
<FileText className="h-4 w-4" /> checked={filters.selectedTypes.includes(type.value)}
<span className="text-sm">By Name</span> onChange={(e) => {
</button> if (e.target.checked) {
<button updateFilters({
onClick={() => { selectedTypes: [
updateFilters({ sortBy: "created" }); ...filters.selectedTypes,
setIsSortDropdownOpen(false); type.value,
}} ],
className={`w-full flex items-center space-x-3 rounded-md px-3 py-2 text-left hover:bg-accent ${ });
filters.sortBy === "created" ? "bg-primary/10 text-primary" : "text-muted-foreground" } else {
}`} updateFilters({
> selectedTypes: filters.selectedTypes.filter(
<Calendar className="h-4 w-4" /> (t) => t !== type.value,
<span className="text-sm">By Created Date</span> ),
</button> });
</div> }
}}
className="border-input text-primary focus:ring-primary rounded"
/>
<IconComponent className="h-4 w-4" />
<span className="text-muted-foreground text-sm">
{type.label}
</span>
</label>
);
})}
</div>
<div className="border-border border-t p-2">
<Button
onClick={() => {
updateFilters({ selectedTypes: [] });
setIsTypeDropdownOpen(false);
}}
variant="ghost"
size="sm"
className="text-muted-foreground hover:bg-accent hover:text-foreground w-full justify-start"
>
Clear all
</Button>
</div>
</div>
)}
</div> </div>
)}
</div>
{/* Sort Order Button */} {/* Repository Filter Buttons - Only show if more than one enabled repo */}
<Button {enabledRepos.length > 1 &&
onClick={() => enabledRepos.map((repo: { id: number; url: string }) => {
updateFilters({ const repoUrl = String(repo.url);
sortOrder: filters.sortOrder === "asc" ? "desc" : "asc", const isSelected =
}) filters.selectedRepositories.includes(repoUrl);
} return (
variant="outline" <Button
size="default" key={repo.id}
className="w-full sm:w-auto flex items-center justify-center space-x-1 bg-muted text-muted-foreground hover:bg-accent hover:text-accent-foreground" onClick={() => {
> const currentSelected = filters.selectedRepositories;
{filters.sortOrder === "asc" ? ( if (isSelected) {
<> // Remove repository from selection
<svg updateFilters({
className="h-4 w-4" selectedRepositories: currentSelected.filter(
fill="none" (url) => url !== repoUrl,
stroke="currentColor" ),
viewBox="0 0 24 24" });
> } else {
<path // Add repository to selection
strokeLinecap="round" updateFilters({
strokeLinejoin="round" selectedRepositories: [...currentSelected, repoUrl],
strokeWidth={2} });
d="M7 11l5-5m0 0l5 5m-5-5v12" }
/> }}
</svg> variant="outline"
<span> size="default"
{filters.sortBy === "created" ? "Oldest First" : "A-Z"} className={`flex w-full items-center justify-center space-x-2 sm:w-auto ${
</span> isSelected
</> ? "border-primary/20 bg-primary/10 text-primary border"
) : ( : "bg-muted text-muted-foreground hover:bg-accent hover:text-accent-foreground"
<> }`}
<svg >
className="h-4 w-4" <GitBranch className="h-4 w-4" />
fill="none" <span>{getRepoName(repoUrl)}</span>
stroke="currentColor" </Button>
viewBox="0 0 24 24" );
> })}
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M17 13l-5 5m0 0l-5-5m5 5V6"
/>
</svg>
<span>
{filters.sortBy === "created" ? "Newest First" : "Z-A"}
</span>
</>
)}
</Button>
</div>
{/* Filter Summary and Clear All */} {/* Sort By Dropdown */}
<div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-2"> <div className="relative w-full sm:w-auto">
<div className="flex items-center gap-4"> <Button
<div className="text-sm text-muted-foreground"> onClick={() => setIsSortDropdownOpen(!isSortDropdownOpen)}
{filteredCount === totalScripts ? ( variant="outline"
<span>Showing all {totalScripts} scripts</span> size="default"
) : ( className="bg-muted text-muted-foreground hover:bg-accent hover:text-accent-foreground flex w-full items-center justify-center space-x-2 sm:w-auto"
<span> >
{filteredCount} of {totalScripts} scripts{" "} {filters.sortBy === "name" ? (
{hasActiveFilters && ( <FileText className="h-4 w-4" />
<span className="font-medium text-info"> ) : (
(filtered) <Calendar className="h-4 w-4" />
)}
<span>
{filters.sortBy === "name" ? "By Name" : "By Created Date"}
</span>
<svg
className={`h-4 w-4 transition-transform ${isSortDropdownOpen ? "rotate-180" : ""}`}
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M19 9l-7 7-7-7"
/>
</svg>
</Button>
{isSortDropdownOpen && (
<div className="border-border bg-card absolute top-full left-0 z-10 mt-1 w-full rounded-lg border shadow-lg sm:w-48">
<div className="p-2">
<button
onClick={() => {
updateFilters({ sortBy: "name" });
setIsSortDropdownOpen(false);
}}
className={`hover:bg-accent flex w-full items-center space-x-3 rounded-md px-3 py-2 text-left ${
filters.sortBy === "name"
? "bg-primary/10 text-primary"
: "text-muted-foreground"
}`}
>
<FileText className="h-4 w-4" />
<span className="text-sm">By Name</span>
</button>
<button
onClick={() => {
updateFilters({ sortBy: "created" });
setIsSortDropdownOpen(false);
}}
className={`hover:bg-accent flex w-full items-center space-x-3 rounded-md px-3 py-2 text-left ${
filters.sortBy === "created"
? "bg-primary/10 text-primary"
: "text-muted-foreground"
}`}
>
<Calendar className="h-4 w-4" />
<span className="text-sm">By Created Date</span>
</button>
</div>
</div>
)}
</div>
{/* Sort Order Button */}
<Button
onClick={() =>
updateFilters({
sortOrder: filters.sortOrder === "asc" ? "desc" : "asc",
})
}
variant="outline"
size="default"
className="bg-muted text-muted-foreground hover:bg-accent hover:text-accent-foreground flex w-full items-center justify-center space-x-1 sm:w-auto"
>
{filters.sortOrder === "asc" ? (
<>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M7 11l5-5m0 0l5 5m-5-5v12"
/>
</svg>
<span>
{filters.sortBy === "created" ? "Oldest First" : "A-Z"}
</span>
</>
) : (
<>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M17 13l-5 5m0 0l-5-5m5 5V6"
/>
</svg>
<span>
{filters.sortBy === "created" ? "Newest First" : "Z-A"}
</span>
</>
)}
</Button>
</div>
{/* Filter Summary and Clear All */}
<div className="flex flex-col items-start justify-between gap-2 sm:flex-row sm:items-center">
<div className="flex items-center gap-4">
<div className="text-muted-foreground text-sm">
{filteredCount === totalScripts ? (
<span>Showing all {totalScripts} scripts</span>
) : (
<span>
{filteredCount} of {totalScripts} scripts{" "}
{hasActiveFilters && (
<span className="text-info font-medium">(filtered)</span>
)}
</span> </span>
)} )}
</span> </div>
{/* Filter Persistence Status */}
{!isLoadingFilters && saveFiltersEnabled && (
<div className="text-success flex items-center space-x-1 text-xs">
<svg
className="h-3 w-3"
fill="currentColor"
viewBox="0 0 20 20"
>
<path
fillRule="evenodd"
d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
clipRule="evenodd"
/>
</svg>
<span>Filters are being saved automatically</span>
</div>
)}
</div>
{hasActiveFilters && (
<Button
onClick={clearAllFilters}
variant="ghost"
size="sm"
className="text-error hover:bg-error/10 hover:text-error-foreground flex w-full items-center justify-center space-x-1 sm:w-auto sm:justify-start"
>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M6 18L18 6M6 6l12 12"
/>
</svg>
<span>Clear all filters</span>
</Button>
)} )}
</div> </div>
{/* Filter Persistence Status */}
{!isLoadingFilters && saveFiltersEnabled && (
<div className="flex items-center space-x-1 text-xs text-success">
<svg className="h-3 w-3" fill="currentColor" viewBox="0 0 20 20">
<path fillRule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clipRule="evenodd" />
</svg>
<span>Filters are being saved automatically</span>
</div>
)}
</div>
{hasActiveFilters && (
<Button
onClick={clearAllFilters}
variant="ghost"
size="sm"
className="flex items-center space-x-1 text-error hover:bg-error/10 hover:text-error-foreground w-full sm:w-auto justify-center sm:justify-start"
>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M6 18L18 6M6 6l12 12"
/>
</svg>
<span>Clear all filters</span>
</Button>
)}
</div>
</> </>
)} )}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
'use client'; 'use client';
import { useState, useEffect } from 'react'; import { useState, useEffect, startTransition } from 'react';
import { api } from '~/trpc/react'; import { api } from '~/trpc/react';
import { Button } from './ui/button'; import { Button } from './ui/button';
import { Input } from './ui/input'; import { Input } from './ui/input';
@@ -159,9 +159,13 @@ export function LXCSettingsModal({ isOpen, script, onClose, onSave: _onSave }: L
useEffect(() => { useEffect(() => {
if (configData?.success) { if (configData?.success) {
populateFormData(configData); populateFormData(configData);
setHasChanges(false); startTransition(() => {
setHasChanges(false);
});
} else if (configData && !configData.success) { } else if (configData && !configData.success) {
setError(String(configData.error ?? 'Failed to load configuration')); startTransition(() => {
setError(String(configData.error ?? 'Failed to load configuration'));
});
} }
}, [configData]); }, [configData]);

View File

@@ -1,39 +1,107 @@
'use client'; "use client";
import { Loader2 } from 'lucide-react'; import { Loader2, CheckCircle, X } from "lucide-react";
import { useRegisterModal } from './modal/ModalStackProvider'; import { useRegisterModal } from "./modal/ModalStackProvider";
import { useEffect, useRef } from "react";
import { Button } from "./ui/button";
interface LoadingModalProps { interface LoadingModalProps {
isOpen: boolean; isOpen: boolean;
action: string; action?: string;
logs?: string[];
isComplete?: boolean;
title?: string;
onClose?: () => void;
} }
export function LoadingModal({ isOpen, action }: LoadingModalProps) { export function LoadingModal({
useRegisterModal(isOpen, { id: 'loading-modal', allowEscape: false, onClose: () => null }); isOpen,
action,
logs = [],
isComplete = false,
title,
onClose,
}: LoadingModalProps) {
// Allow dismissing with ESC only when complete, prevent during running
useRegisterModal(isOpen, {
id: "loading-modal",
allowEscape: isComplete,
onClose: onClose ?? (() => null),
});
const logsEndRef = useRef<HTMLDivElement>(null);
// Auto-scroll to bottom when new logs arrive
useEffect(() => {
logsEndRef.current?.scrollIntoView({ behavior: "smooth" });
}, [logs]);
if (!isOpen) return null; if (!isOpen) return null;
return ( return (
<div className="fixed inset-0 backdrop-blur-sm bg-black/50 flex items-center justify-center z-50 p-4"> <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4 backdrop-blur-sm">
<div className="bg-card rounded-lg shadow-xl max-w-md w-full border border-border p-8"> <div className="bg-card border-border relative flex max-h-[80vh] w-full max-w-2xl flex-col rounded-lg border p-8 shadow-xl">
{/* Close button - only show when complete */}
{isComplete && onClose && (
<Button
variant="ghost"
size="sm"
onClick={onClose}
className="absolute top-4 right-4 h-6 w-6 p-0"
>
<X className="h-4 w-4" />
</Button>
)}
<div className="flex flex-col items-center space-y-4"> <div className="flex flex-col items-center space-y-4">
<div className="relative"> <div className="relative">
<Loader2 className="h-12 w-12 animate-spin text-primary" /> {isComplete ? (
<div className="absolute inset-0 rounded-full border-2 border-primary/20 animate-pulse"></div> <CheckCircle className="text-success h-12 w-12" />
</div> ) : (
<div className="text-center"> <>
<h3 className="text-lg font-semibold text-card-foreground mb-2"> <Loader2 className="text-primary h-12 w-12 animate-spin" />
Processing <div className="border-primary/20 absolute inset-0 animate-pulse rounded-full border-2"></div>
</h3> </>
<p className="text-sm text-muted-foreground"> )}
{action}
</p>
<p className="text-xs text-muted-foreground mt-2">
Please wait...
</p>
</div> </div>
{/* Action text - displayed prominently */}
{action && (
<p className="text-foreground text-base font-medium">{action}</p>
)}
{/* Static title text */}
{title && <p className="text-muted-foreground text-sm">{title}</p>}
{/* Log output */}
{logs.length > 0 && (
<div className="bg-card border-border text-chart-2 terminal-output max-h-[60vh] w-full overflow-y-auto rounded-lg border p-4 font-mono text-xs">
{logs.map((log, index) => (
<div
key={index}
className="mb-1 break-words whitespace-pre-wrap"
>
{log}
</div>
))}
<div ref={logsEndRef} />
</div>
)}
{!isComplete && (
<div className="flex space-x-1">
<div className="bg-primary h-2 w-2 animate-bounce rounded-full"></div>
<div
className="bg-primary h-2 w-2 animate-bounce rounded-full"
style={{ animationDelay: "0.1s" }}
></div>
<div
className="bg-primary h-2 w-2 animate-bounce rounded-full"
style={{ animationDelay: "0.2s" }}
></div>
</div>
)}
</div> </div>
</div> </div>
</div> </div>
); );
} }

View File

@@ -0,0 +1,343 @@
"use client";
import { useState, useEffect } from "react";
import { Button } from "./ui/button";
import { Lock, CheckCircle, AlertCircle } from "lucide-react";
import { useRegisterModal } from "./modal/ModalStackProvider";
import { api } from "~/trpc/react";
import type { Storage } from "~/server/services/storageService";
interface PBSCredentialsModalProps {
isOpen: boolean;
onClose: () => void;
serverId: number;
serverName: string;
storage: Storage;
}
export function PBSCredentialsModal({
isOpen,
onClose,
serverId,
serverName: _serverName,
storage,
}: PBSCredentialsModalProps) {
const [pbsIp, setPbsIp] = useState("");
const [pbsDatastore, setPbsDatastore] = useState("");
const [pbsPassword, setPbsPassword] = useState("");
const [pbsFingerprint, setPbsFingerprint] = useState("");
const [isLoading, setIsLoading] = useState(false);
// Extract PBS info from storage object
const pbsIpFromStorage = (storage as { server?: string }).server ?? null;
const pbsDatastoreFromStorage =
(storage as { datastore?: string }).datastore ?? null;
// Fetch existing credentials
const { data: credentialData, refetch } =
api.pbsCredentials.getCredentialsForStorage.useQuery(
{ serverId, storageName: storage.name },
{ enabled: isOpen },
);
// Initialize form with storage config values or existing credentials
useEffect(() => {
if (isOpen) {
if (credentialData?.success && credentialData.credential) {
// Load existing credentials
setPbsIp(String(credentialData.credential.pbs_ip));
setPbsDatastore(String(credentialData.credential.pbs_datastore));
setPbsPassword(""); // Don't show password
setPbsFingerprint(
String(credentialData.credential.pbs_fingerprint ?? ""),
);
} else {
// Initialize with storage config values
setPbsIp(pbsIpFromStorage ?? "");
setPbsDatastore(pbsDatastoreFromStorage ?? "");
setPbsPassword("");
setPbsFingerprint("");
}
}
}, [isOpen, credentialData, pbsIpFromStorage, pbsDatastoreFromStorage]);
const saveCredentials = api.pbsCredentials.saveCredentials.useMutation({
onSuccess: () => {
void refetch();
onClose();
},
onError: (error) => {
console.error("Failed to save PBS credentials:", error);
alert(`Failed to save credentials: ${error.message}`);
},
});
const deleteCredentials = api.pbsCredentials.deleteCredentials.useMutation({
onSuccess: () => {
void refetch();
onClose();
},
onError: (error) => {
console.error("Failed to delete PBS credentials:", error);
alert(`Failed to delete credentials: ${error.message}`);
},
});
useRegisterModal(isOpen, {
id: "pbs-credentials-modal",
allowEscape: true,
onClose,
});
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!pbsIp || !pbsDatastore || !pbsFingerprint) {
alert("Please fill in all required fields (IP, Datastore, Fingerprint)");
return;
}
// Password is optional when updating existing credentials
setIsLoading(true);
try {
await saveCredentials.mutateAsync({
serverId,
storageName: storage.name,
pbs_ip: pbsIp,
pbs_datastore: pbsDatastore,
pbs_password: pbsPassword || undefined, // Undefined means keep existing password
pbs_fingerprint: pbsFingerprint,
});
} finally {
setIsLoading(false);
}
};
const handleDelete = async () => {
if (
!confirm(
"Are you sure you want to delete the PBS credentials for this storage?",
)
) {
return;
}
setIsLoading(true);
try {
await deleteCredentials.mutateAsync({
serverId,
storageName: storage.name,
});
} finally {
setIsLoading(false);
}
};
if (!isOpen) return null;
const hasCredentials = credentialData?.success && credentialData.credential;
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4 backdrop-blur-sm">
<div className="bg-card border-border flex max-h-[90vh] w-full max-w-2xl flex-col rounded-lg border shadow-xl">
{/* Header */}
<div className="border-border flex items-center justify-between border-b p-6">
<div className="flex items-center gap-3">
<Lock className="text-primary h-6 w-6" />
<h2 className="text-card-foreground text-2xl font-bold">
PBS Credentials - {storage.name}
</h2>
</div>
<Button
onClick={onClose}
variant="ghost"
size="icon"
className="text-muted-foreground hover:text-foreground"
>
<svg
className="h-5 w-5"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</Button>
</div>
{/* Content */}
<div className="flex-1 overflow-y-auto p-6">
<form onSubmit={handleSubmit} className="space-y-4">
{/* Storage Name (read-only) */}
<div>
<label
htmlFor="storage-name"
className="text-foreground mb-1 block text-sm font-medium"
>
Storage Name
</label>
<input
type="text"
id="storage-name"
value={storage.name}
disabled
className="bg-muted text-muted-foreground border-border w-full cursor-not-allowed rounded-md border px-3 py-2 shadow-sm"
/>
</div>
{/* PBS IP */}
<div>
<label
htmlFor="pbs-ip"
className="text-foreground mb-1 block text-sm font-medium"
>
PBS Server IP <span className="text-error">*</span>
</label>
<input
type="text"
id="pbs-ip"
value={pbsIp}
onChange={(e) => setPbsIp(e.target.value)}
required
disabled={isLoading}
className="bg-card text-foreground placeholder-muted-foreground focus:ring-ring focus:border-ring border-border w-full rounded-md border px-3 py-2 shadow-sm focus:ring-2 focus:outline-none"
placeholder="e.g., 10.10.10.226"
/>
<p className="text-muted-foreground mt-1 text-xs">
IP address of the Proxmox Backup Server
</p>
</div>
{/* PBS Datastore */}
<div>
<label
htmlFor="pbs-datastore"
className="text-foreground mb-1 block text-sm font-medium"
>
PBS Datastore <span className="text-error">*</span>
</label>
<input
type="text"
id="pbs-datastore"
value={pbsDatastore}
onChange={(e) => setPbsDatastore(e.target.value)}
required
disabled={isLoading}
className="bg-card text-foreground placeholder-muted-foreground focus:ring-ring focus:border-ring border-border w-full rounded-md border px-3 py-2 shadow-sm focus:ring-2 focus:outline-none"
placeholder="e.g., NAS03-ISCSI-BACKUP"
/>
<p className="text-muted-foreground mt-1 text-xs">
Name of the datastore on the PBS server
</p>
</div>
{/* PBS Password */}
<div>
<label
htmlFor="pbs-password"
className="text-foreground mb-1 block text-sm font-medium"
>
Password{" "}
{!hasCredentials && <span className="text-error">*</span>}
</label>
<input
type="password"
id="pbs-password"
value={pbsPassword}
onChange={(e) => setPbsPassword(e.target.value)}
required={!hasCredentials}
disabled={isLoading}
className="bg-card text-foreground placeholder-muted-foreground focus:ring-ring focus:border-ring border-border w-full rounded-md border px-3 py-2 shadow-sm focus:ring-2 focus:outline-none"
placeholder={
hasCredentials
? "Enter new password (leave empty to keep existing)"
: "Enter PBS password"
}
/>
<p className="text-muted-foreground mt-1 text-xs">
Password for root@pam user on PBS server
</p>
</div>
{/* PBS Fingerprint */}
<div>
<label
htmlFor="pbs-fingerprint"
className="text-foreground mb-1 block text-sm font-medium"
>
Fingerprint <span className="text-error">*</span>
</label>
<input
type="text"
id="pbs-fingerprint"
value={pbsFingerprint}
onChange={(e) => setPbsFingerprint(e.target.value)}
required
disabled={isLoading}
className="bg-card text-foreground placeholder-muted-foreground focus:ring-ring focus:border-ring border-border w-full rounded-md border px-3 py-2 shadow-sm focus:ring-2 focus:outline-none"
placeholder="e.g., 7b:e5:87:38:5e:16:05:d1:12:22:7f:73:d2:e2:d0:cf:8c:cb:28:e2:74:0c:78:91:1a:71:74:2e:79:20:5a:02"
/>
<p className="text-muted-foreground mt-1 text-xs">
Server fingerprint for auto-acceptance. You can find this on
your PBS dashboard by clicking the &quot;Show Fingerprint&quot;
button.
</p>
</div>
{/* Status indicator */}
{hasCredentials && (
<div className="bg-success/10 border-success/20 flex items-center gap-2 rounded-lg border p-3">
<CheckCircle className="text-success h-4 w-4" />
<span className="text-success text-sm font-medium">
Credentials are configured for this storage
</span>
</div>
)}
{/* Action Buttons */}
<div className="flex flex-col justify-end gap-3 pt-4 sm:flex-row">
{hasCredentials && (
<Button
type="button"
onClick={handleDelete}
variant="outline"
disabled={isLoading}
className="order-3 w-full sm:w-auto"
>
<AlertCircle className="mr-2 h-4 w-4" />
Delete Credentials
</Button>
)}
<Button
type="button"
onClick={onClose}
variant="outline"
disabled={isLoading}
className="order-2 w-full sm:w-auto"
>
Cancel
</Button>
<Button
type="submit"
variant="default"
disabled={isLoading}
className="order-1 w-full sm:w-auto"
>
{isLoading
? "Saving..."
: hasCredentials
? "Update Credentials"
: "Save Credentials"}
</Button>
</div>
</form>
</div>
</div>
</div>
);
}

View File

@@ -1,6 +1,6 @@
'use client'; 'use client';
import { useState, useEffect } from 'react'; import { useState, useEffect, startTransition } from 'react';
import { api } from '~/trpc/react'; import { api } from '~/trpc/react';
import { Button } from './ui/button'; import { Button } from './ui/button';
import { Badge } from './ui/badge'; import { Badge } from './ui/badge';
@@ -47,7 +47,9 @@ export function ReleaseNotesModal({ isOpen, onClose, highlightVersion }: Release
// Get current version when modal opens // Get current version when modal opens
useEffect(() => { useEffect(() => {
if (isOpen && versionData?.success && versionData.version) { if (isOpen && versionData?.success && versionData.version) {
setCurrentVersion(versionData.version); startTransition(() => {
setCurrentVersion(versionData.version);
});
} }
}, [isOpen, versionData]); }, [isOpen, versionData]);

View File

@@ -1,6 +1,6 @@
'use client'; 'use client';
import { useState } from 'react'; import { useState, useRef, useEffect } from 'react';
import { api } from '~/trpc/react'; import { api } from '~/trpc/react';
import { Button } from './ui/button'; import { Button } from './ui/button';
import { ContextualHelpIcon } from './ContextualHelpIcon'; import { ContextualHelpIcon } from './ContextualHelpIcon';
@@ -9,6 +9,10 @@ export function ResyncButton() {
const [isResyncing, setIsResyncing] = useState(false); const [isResyncing, setIsResyncing] = useState(false);
const [lastSync, setLastSync] = useState<Date | null>(null); const [lastSync, setLastSync] = useState<Date | null>(null);
const [syncMessage, setSyncMessage] = useState<string | null>(null); const [syncMessage, setSyncMessage] = useState<string | null>(null);
const hasReloadedRef = useRef<boolean>(false);
const isUserInitiatedRef = useRef<boolean>(false);
const reloadTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const messageTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const resyncMutation = api.scripts.resyncScripts.useMutation({ const resyncMutation = api.scripts.resyncScripts.useMutation({
onSuccess: (data) => { onSuccess: (data) => {
@@ -16,29 +20,87 @@ export function ResyncButton() {
setLastSync(new Date()); setLastSync(new Date());
if (data.success) { if (data.success) {
setSyncMessage(data.message ?? 'Scripts synced successfully'); setSyncMessage(data.message ?? 'Scripts synced successfully');
// Reload the page after successful sync // Only reload if this was triggered by user action
setTimeout(() => { if (isUserInitiatedRef.current && !hasReloadedRef.current) {
window.location.reload(); hasReloadedRef.current = true;
}, 2000); // Wait 2 seconds to show the success message
// Clear any existing reload timeout
if (reloadTimeoutRef.current) {
clearTimeout(reloadTimeoutRef.current);
reloadTimeoutRef.current = null;
}
// Set new reload timeout
reloadTimeoutRef.current = setTimeout(() => {
reloadTimeoutRef.current = null;
window.location.reload();
}, 2000); // Wait 2 seconds to show the success message
} else {
// Reset flag if reload didn't happen
isUserInitiatedRef.current = false;
}
} else { } else {
setSyncMessage(data.error ?? 'Failed to sync scripts'); setSyncMessage(data.error ?? 'Failed to sync scripts');
// Clear message after 3 seconds for errors // Clear message after 3 seconds for errors
setTimeout(() => setSyncMessage(null), 3000); if (messageTimeoutRef.current) {
clearTimeout(messageTimeoutRef.current);
}
messageTimeoutRef.current = setTimeout(() => {
setSyncMessage(null);
messageTimeoutRef.current = null;
}, 3000);
isUserInitiatedRef.current = false;
} }
}, },
onError: (error) => { onError: (error) => {
setIsResyncing(false); setIsResyncing(false);
setSyncMessage(`Error: ${error.message}`); setSyncMessage(`Error: ${error.message}`);
setTimeout(() => setSyncMessage(null), 3000); if (messageTimeoutRef.current) {
clearTimeout(messageTimeoutRef.current);
}
messageTimeoutRef.current = setTimeout(() => {
setSyncMessage(null);
messageTimeoutRef.current = null;
}, 3000);
isUserInitiatedRef.current = false;
}, },
}); });
const handleResync = async () => { const handleResync = async () => {
// Prevent multiple simultaneous sync operations
if (isResyncing) return;
// Clear any pending reload timeout
if (reloadTimeoutRef.current) {
clearTimeout(reloadTimeoutRef.current);
reloadTimeoutRef.current = null;
}
// Mark as user-initiated before starting
isUserInitiatedRef.current = true;
hasReloadedRef.current = false;
setIsResyncing(true); setIsResyncing(true);
setSyncMessage(null); setSyncMessage(null);
resyncMutation.mutate(); resyncMutation.mutate();
}; };
// Cleanup on unmount - clear any pending timeouts
useEffect(() => {
return () => {
if (reloadTimeoutRef.current) {
clearTimeout(reloadTimeoutRef.current);
reloadTimeoutRef.current = null;
}
if (messageTimeoutRef.current) {
clearTimeout(messageTimeoutRef.current);
messageTimeoutRef.current = null;
}
// Reset refs on unmount
hasReloadedRef.current = false;
isUserInitiatedRef.current = false;
};
}, []);
return ( return (
<div className="flex flex-col sm:flex-row sm:items-center gap-3"> <div className="flex flex-col sm:flex-row sm:items-center gap-3">
<div className="text-sm text-muted-foreground font-medium"> <div className="text-sm text-muted-foreground font-medium">

View File

@@ -1,9 +1,9 @@
'use client'; "use client";
import { useState } from 'react'; import { useState } from "react";
import Image from 'next/image'; import Image from "next/image";
import type { ScriptCard } from '~/types/script'; import type { ScriptCard } from "~/types/script";
import { TypeBadge, UpdateableBadge } from './Badge'; import { TypeBadge, UpdateableBadge } from "./Badge";
interface ScriptCardProps { interface ScriptCardProps {
script: ScriptCard; script: ScriptCard;
@@ -12,7 +12,12 @@ interface ScriptCardProps {
onToggleSelect?: (slug: string) => void; onToggleSelect?: (slug: string) => void;
} }
export function ScriptCard({ script, onClick, isSelected = false, onToggleSelect }: ScriptCardProps) { export function ScriptCard({
script,
onClick,
isSelected = false,
onToggleSelect,
}: ScriptCardProps) {
const [imageError, setImageError] = useState(false); const [imageError, setImageError] = useState(false);
const handleImageError = () => { const handleImageError = () => {
@@ -27,8 +32,8 @@ export function ScriptCard({ script, onClick, isSelected = false, onToggleSelect
}; };
const getRepoName = (url?: string): string => { const getRepoName = (url?: string): string => {
if (!url) return ''; if (!url) return "";
const match = url.match(/github\.com\/([^\/]+)\/([^\/]+)/); const match = /github\.com\/([^\/]+)\/([^\/]+)/.exec(url);
if (match) { if (match) {
return `${match[1]}/${match[2]}`; return `${match[1]}/${match[2]}`;
} }
@@ -37,32 +42,36 @@ export function ScriptCard({ script, onClick, isSelected = false, onToggleSelect
return ( return (
<div <div
className="bg-card rounded-lg shadow-md hover:shadow-lg transition-shadow duration-200 cursor-pointer border border-border hover:border-primary h-full flex flex-col relative" className="bg-card border-border hover:border-primary relative flex h-full cursor-pointer flex-col rounded-lg border shadow-md transition-shadow duration-200 hover:shadow-lg"
onClick={() => onClick(script)} onClick={() => onClick(script)}
> >
{/* Checkbox in top-left corner */} {/* Checkbox in top-left corner */}
{onToggleSelect && ( {onToggleSelect && (
<div className="absolute top-2 left-2 z-10"> <div className="absolute top-2 left-2 z-10">
<div <div
className={`w-4 h-4 border-2 rounded cursor-pointer transition-all duration-200 flex items-center justify-center ${ className={`flex h-4 w-4 cursor-pointer items-center justify-center rounded border-2 transition-all duration-200 ${
isSelected isSelected
? 'bg-primary border-primary text-primary-foreground' ? "bg-primary border-primary text-primary-foreground"
: 'bg-card border-border hover:border-primary/60 hover:bg-accent' : "bg-card border-border hover:border-primary/60 hover:bg-accent"
}`} }`}
onClick={handleCheckboxClick} onClick={handleCheckboxClick}
> >
{isSelected && ( {isSelected && (
<svg className="w-3 h-3" fill="currentColor" viewBox="0 0 20 20"> <svg className="h-3 w-3" fill="currentColor" viewBox="0 0 20 20">
<path fillRule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clipRule="evenodd" /> <path
fillRule="evenodd"
d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
clipRule="evenodd"
/>
</svg> </svg>
)} )}
</div> </div>
</div> </div>
)} )}
<div className="p-6 flex-1 flex flex-col"> <div className="flex flex-1 flex-col p-6">
{/* Header with logo and name */} {/* Header with logo and name */}
<div className="flex items-start space-x-4 mb-4"> <div className="mb-4 flex items-start space-x-4">
<div className="flex-shrink-0"> <div className="flex-shrink-0">
{script.logo && !imageError ? ( {script.logo && !imageError ? (
<Image <Image
@@ -70,42 +79,49 @@ export function ScriptCard({ script, onClick, isSelected = false, onToggleSelect
alt={`${script.name} logo`} alt={`${script.name} logo`}
width={48} width={48}
height={48} height={48}
className="w-12 h-12 rounded-lg object-contain" className="h-12 w-12 rounded-lg object-contain"
onError={handleImageError} onError={handleImageError}
/> />
) : ( ) : (
<div className="w-12 h-12 bg-muted rounded-lg flex items-center justify-center"> <div className="bg-muted flex h-12 w-12 items-center justify-center rounded-lg">
<span className="text-muted-foreground text-lg font-semibold"> <span className="text-muted-foreground text-lg font-semibold">
{script.name?.charAt(0)?.toUpperCase() || '?'} {script.name?.charAt(0)?.toUpperCase() || "?"}
</span> </span>
</div> </div>
)} )}
</div> </div>
<div className="flex-1 min-w-0"> <div className="min-w-0 flex-1">
<h3 className="text-lg font-semibold text-foreground truncate"> <h3 className="text-foreground truncate text-lg font-semibold">
{script.name || 'Unnamed Script'} {script.name || "Unnamed Script"}
</h3> </h3>
<div className="mt-2 space-y-2"> <div className="mt-2 space-y-2">
{/* Type and Updateable status on first row */} {/* Type and Updateable status on first row */}
<div className="flex items-center space-x-2 flex-wrap gap-1"> <div className="flex flex-wrap items-center gap-1 space-x-2">
<TypeBadge type={script.type ?? 'unknown'} /> <TypeBadge type={script.type ?? "unknown"} />
{script.updateable && <UpdateableBadge />} {script.updateable && <UpdateableBadge />}
{script.repository_url && ( {script.repository_url && (
<span className="text-xs px-2 py-0.5 bg-muted text-muted-foreground rounded border border-border" title={script.repository_url}> <span
className="bg-muted text-muted-foreground border-border rounded border px-2 py-0.5 text-xs"
title={script.repository_url}
>
{getRepoName(script.repository_url)} {getRepoName(script.repository_url)}
</span> </span>
)} )}
</div> </div>
{/* Download Status */} {/* Download Status */}
<div className="flex items-center space-x-1"> <div className="flex items-center space-x-1">
<div className={`w-2 h-2 rounded-full ${ <div
script.isDownloaded ? 'bg-success' : 'bg-error' className={`h-2 w-2 rounded-full ${
}`}></div> script.isDownloaded ? "bg-success" : "bg-error"
<span className={`text-xs font-medium ${ }`}
script.isDownloaded ? 'text-success' : 'text-error' ></div>
}`}> <span
{script.isDownloaded ? 'Downloaded' : 'Not Downloaded'} className={`text-xs font-medium ${
script.isDownloaded ? "text-success" : "text-error"
}`}
>
{script.isDownloaded ? "Downloaded" : "Not Downloaded"}
</span> </span>
</div> </div>
</div> </div>
@@ -113,8 +129,8 @@ export function ScriptCard({ script, onClick, isSelected = false, onToggleSelect
</div> </div>
{/* Description */} {/* Description */}
<p className="text-muted-foreground text-sm line-clamp-3 mb-4 flex-1"> <p className="text-muted-foreground mb-4 line-clamp-3 flex-1 text-sm">
{script.description || 'No description available'} {script.description || "No description available"}
</p> </p>
{/* Footer with website link */} {/* Footer with website link */}
@@ -124,12 +140,22 @@ export function ScriptCard({ script, onClick, isSelected = false, onToggleSelect
href={script.website} href={script.website}
target="_blank" target="_blank"
rel="noopener noreferrer" rel="noopener noreferrer"
className="text-info hover:text-info/80 text-sm font-medium flex items-center space-x-1" className="text-info hover:text-info/80 flex items-center space-x-1 text-sm font-medium"
onClick={(e) => e.stopPropagation()} onClick={(e) => e.stopPropagation()}
> >
<span>Website</span> <span>Website</span>
<svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <svg
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14" /> className="h-3 w-3"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"
/>
</svg> </svg>
</a> </a>
</div> </div>

View File

@@ -1,9 +1,9 @@
'use client'; "use client";
import { useState } from 'react'; import { useState } from "react";
import Image from 'next/image'; import Image from "next/image";
import type { ScriptCard } from '~/types/script'; import type { ScriptCard } from "~/types/script";
import { TypeBadge, UpdateableBadge } from './Badge'; import { TypeBadge, UpdateableBadge } from "./Badge";
interface ScriptCardListProps { interface ScriptCardListProps {
script: ScriptCard; script: ScriptCard;
@@ -12,7 +12,12 @@ interface ScriptCardListProps {
onToggleSelect?: (slug: string) => void; onToggleSelect?: (slug: string) => void;
} }
export function ScriptCardList({ script, onClick, isSelected = false, onToggleSelect }: ScriptCardListProps) { export function ScriptCardList({
script,
onClick,
isSelected = false,
onToggleSelect,
}: ScriptCardListProps) {
const [imageError, setImageError] = useState(false); const [imageError, setImageError] = useState(false);
const handleImageError = () => { const handleImageError = () => {
@@ -27,26 +32,27 @@ export function ScriptCardList({ script, onClick, isSelected = false, onToggleSe
}; };
const formatDate = (dateString?: string) => { const formatDate = (dateString?: string) => {
if (!dateString) return 'Unknown'; if (!dateString) return "Unknown";
try { try {
return new Date(dateString).toLocaleDateString('en-US', { return new Date(dateString).toLocaleDateString("en-US", {
year: 'numeric', year: "numeric",
month: 'short', month: "short",
day: 'numeric' day: "numeric",
}); });
} catch { } catch {
return 'Unknown'; return "Unknown";
} }
}; };
const getCategoryNames = () => { const getCategoryNames = () => {
if (!script.categoryNames || script.categoryNames.length === 0) return 'Uncategorized'; if (!script.categoryNames || script.categoryNames.length === 0)
return script.categoryNames.join(', '); return "Uncategorized";
return script.categoryNames.join(", ");
}; };
const getRepoName = (url?: string): string => { const getRepoName = (url?: string): string => {
if (!url) return ''; if (!url) return "";
const match = url.match(/github\.com\/([^\/]+)\/([^\/]+)/); const match = /github\.com\/([^\/]+)\/([^\/]+)/.exec(url);
if (match) { if (match) {
return `${match[1]}/${match[2]}`; return `${match[1]}/${match[2]}`;
} }
@@ -55,30 +61,34 @@ export function ScriptCardList({ script, onClick, isSelected = false, onToggleSe
return ( return (
<div <div
className="bg-card rounded-lg shadow-sm hover:shadow-md transition-shadow duration-200 cursor-pointer border border-border hover:border-primary relative" className="bg-card border-border hover:border-primary relative cursor-pointer rounded-lg border shadow-sm transition-shadow duration-200 hover:shadow-md"
onClick={() => onClick(script)} onClick={() => onClick(script)}
> >
{/* Checkbox */} {/* Checkbox */}
{onToggleSelect && ( {onToggleSelect && (
<div className="absolute top-4 left-4 z-10"> <div className="absolute top-4 left-4 z-10">
<div <div
className={`w-4 h-4 border-2 rounded cursor-pointer transition-all duration-200 flex items-center justify-center ${ className={`flex h-4 w-4 cursor-pointer items-center justify-center rounded border-2 transition-all duration-200 ${
isSelected isSelected
? 'bg-primary border-primary text-primary-foreground' ? "bg-primary border-primary text-primary-foreground"
: 'bg-card border-border hover:border-primary/60 hover:bg-accent' : "bg-card border-border hover:border-primary/60 hover:bg-accent"
}`} }`}
onClick={handleCheckboxClick} onClick={handleCheckboxClick}
> >
{isSelected && ( {isSelected && (
<svg className="w-3 h-3" fill="currentColor" viewBox="0 0 20 20"> <svg className="h-3 w-3" fill="currentColor" viewBox="0 0 20 20">
<path fillRule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clipRule="evenodd" /> <path
fillRule="evenodd"
d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
clipRule="evenodd"
/>
</svg> </svg>
)} )}
</div> </div>
</div> </div>
)} )}
<div className={`p-6 ${onToggleSelect ? 'pl-12' : ''}`}> <div className={`p-6 ${onToggleSelect ? "pl-12" : ""}`}>
<div className="flex items-start space-x-4"> <div className="flex items-start space-x-4">
{/* Logo */} {/* Logo */}
<div className="flex-shrink-0"> <div className="flex-shrink-0">
@@ -88,42 +98,49 @@ export function ScriptCardList({ script, onClick, isSelected = false, onToggleSe
alt={`${script.name} logo`} alt={`${script.name} logo`}
width={56} width={56}
height={56} height={56}
className="w-14 h-14 rounded-lg object-contain" className="h-14 w-14 rounded-lg object-contain"
onError={handleImageError} onError={handleImageError}
/> />
) : ( ) : (
<div className="w-14 h-14 bg-muted rounded-lg flex items-center justify-center"> <div className="bg-muted flex h-14 w-14 items-center justify-center rounded-lg">
<span className="text-muted-foreground text-lg font-semibold"> <span className="text-muted-foreground text-lg font-semibold">
{script.name?.charAt(0)?.toUpperCase() || '?'} {script.name?.charAt(0)?.toUpperCase() || "?"}
</span> </span>
</div> </div>
)} )}
</div> </div>
{/* Main Content */} {/* Main Content */}
<div className="flex-1 min-w-0"> <div className="min-w-0 flex-1">
{/* Header Row */} {/* Header Row */}
<div className="flex items-start justify-between mb-3"> <div className="mb-3 flex items-start justify-between">
<div className="flex-1 min-w-0"> <div className="min-w-0 flex-1">
<h3 className="text-xl font-semibold text-foreground truncate mb-2"> <h3 className="text-foreground mb-2 truncate text-xl font-semibold">
{script.name || 'Unnamed Script'} {script.name || "Unnamed Script"}
</h3> </h3>
<div className="flex items-center space-x-3 flex-wrap gap-2"> <div className="flex flex-wrap items-center gap-2 space-x-3">
<TypeBadge type={script.type ?? 'unknown'} /> <TypeBadge type={script.type ?? "unknown"} />
{script.updateable && <UpdateableBadge />} {script.updateable && <UpdateableBadge />}
{script.repository_url && ( {script.repository_url && (
<span className="text-xs px-2 py-0.5 bg-muted text-muted-foreground rounded border border-border" title={script.repository_url}> <span
className="bg-muted text-muted-foreground border-border rounded border px-2 py-0.5 text-xs"
title={script.repository_url}
>
{getRepoName(script.repository_url)} {getRepoName(script.repository_url)}
</span> </span>
)} )}
<div className="flex items-center space-x-1"> <div className="flex items-center space-x-1">
<div className={`w-2 h-2 rounded-full ${ <div
script.isDownloaded ? 'bg-success' : 'bg-error' className={`h-2 w-2 rounded-full ${
}`}></div> script.isDownloaded ? "bg-success" : "bg-error"
<span className={`text-sm font-medium ${ }`}
script.isDownloaded ? 'text-success' : 'text-error' ></div>
}`}> <span
{script.isDownloaded ? 'Downloaded' : 'Not Downloaded'} className={`text-sm font-medium ${
script.isDownloaded ? "text-success" : "text-error"
}`}
>
{script.isDownloaded ? "Downloaded" : "Not Downloaded"}
</span> </span>
</div> </div>
</div> </div>
@@ -135,68 +152,128 @@ export function ScriptCardList({ script, onClick, isSelected = false, onToggleSe
href={script.website} href={script.website}
target="_blank" target="_blank"
rel="noopener noreferrer" rel="noopener noreferrer"
className="text-info hover:text-info/80 text-sm font-medium flex items-center space-x-1 ml-4" className="text-info hover:text-info/80 ml-4 flex items-center space-x-1 text-sm font-medium"
onClick={(e) => e.stopPropagation()} onClick={(e) => e.stopPropagation()}
> >
<span>Website</span> <span>Website</span>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <svg
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14" /> className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"
/>
</svg> </svg>
</a> </a>
)} )}
</div> </div>
{/* Description */} {/* Description */}
<p className="text-muted-foreground text-sm mb-4 line-clamp-2"> <p className="text-muted-foreground mb-4 line-clamp-2 text-sm">
{script.description || 'No description available'} {script.description || "No description available"}
</p> </p>
{/* Metadata Row */} {/* Metadata Row */}
<div className="flex items-center justify-between text-xs text-muted-foreground"> <div className="text-muted-foreground flex items-center justify-between text-xs">
<div className="flex items-center space-x-4"> <div className="flex items-center space-x-4">
<div className="flex items-center space-x-1"> <div className="flex items-center space-x-1">
<svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <svg
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z" /> className="h-3 w-3"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z"
/>
</svg> </svg>
<span>Categories: {getCategoryNames()}</span> <span>Categories: {getCategoryNames()}</span>
</div> </div>
<div className="flex items-center space-x-1"> <div className="flex items-center space-x-1">
<svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <svg
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" /> className="h-3 w-3"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"
/>
</svg> </svg>
<span>Created: {formatDate(script.date_created)}</span> <span>Created: {formatDate(script.date_created)}</span>
</div> </div>
{(script.os ?? script.version) && ( {(script.os ?? script.version) && (
<div className="flex items-center space-x-1"> <div className="flex items-center space-x-1">
<svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <svg
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 3v2m6-2v2M9 19v2m6-2v2M5 9H3m2 6H3m18-6h-2m2 6h-2M7 19h10a2 2 0 002-2V7a2 2 0 00-2-2H7a2 2 0 00-2 2v10a2 2 0 002 2zM9 9h6v6H9V9z" /> className="h-3 w-3"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M9 3v2m6-2v2M9 19v2m6-2v2M5 9H3m2 6H3m18-6h-2m2 6h-2M7 19h10a2 2 0 002-2V7a2 2 0 00-2-2H7a2 2 0 00-2 2v10a2 2 0 002 2zM9 9h6v6H9V9z"
/>
</svg> </svg>
<span> <span>
{script.os && script.version {script.os && script.version
? `${script.os.charAt(0).toUpperCase() + script.os.slice(1)} ${script.version}` ? `${script.os.charAt(0).toUpperCase() + script.os.slice(1)} ${script.version}`
: script.os : script.os
? script.os.charAt(0).toUpperCase() + script.os.slice(1) ? script.os.charAt(0).toUpperCase() +
script.os.slice(1)
: script.version : script.version
? `Version ${script.version}` ? `Version ${script.version}`
: '' : ""}
}
</span> </span>
</div> </div>
)} )}
{script.interface_port && ( {script.interface_port && (
<div className="flex items-center space-x-1"> <div className="flex items-center space-x-1">
<svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <svg
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 9l3 3-3 3m5 0h3M5 20h14a2 2 0 002-2V6a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" /> className="h-3 w-3"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M8 9l3 3-3 3m5 0h3M5 20h14a2 2 0 002-2V6a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"
/>
</svg> </svg>
<span>Port: {script.interface_port}</span> <span>Port: {script.interface_port}</span>
</div> </div>
)} )}
</div> </div>
<div className="flex items-center space-x-1"> <div className="flex items-center space-x-1">
<svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <svg
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /> className="h-3 w-3"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg> </svg>
<span>ID: {script.slug || 'unknown'}</span> <span>ID: {script.slug || "unknown"}</span>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -4,14 +4,20 @@ import { useState } from "react";
import Image from "next/image"; import Image from "next/image";
import { api } from "~/trpc/react"; import { api } from "~/trpc/react";
import type { Script } from "~/types/script"; import type { Script } from "~/types/script";
import type { Server } from "~/types/server";
import { DiffViewer } from "./DiffViewer"; import { DiffViewer } from "./DiffViewer";
import { TextViewer } from "./TextViewer"; import { TextViewer } from "./TextViewer";
import { ExecutionModeModal } from "./ExecutionModeModal"; import { ExecutionModeModal } from "./ExecutionModeModal";
import { ConfirmationModal } from "./ConfirmationModal"; import { ConfirmationModal } from "./ConfirmationModal";
import { ScriptVersionModal } from "./ScriptVersionModal"; import { ScriptVersionModal } from "./ScriptVersionModal";
import { TypeBadge, UpdateableBadge, PrivilegedBadge, NoteBadge } from "./Badge"; import {
TypeBadge,
UpdateableBadge,
PrivilegedBadge,
NoteBadge,
} from "./Badge";
import { Button } from "./ui/button"; import { Button } from "./ui/button";
import { useRegisterModal } from './modal/ModalStackProvider'; import { useRegisterModal } from "./modal/ModalStackProvider";
interface ScriptDetailModalProps { interface ScriptDetailModalProps {
script: Script | null; script: Script | null;
@@ -21,7 +27,7 @@ interface ScriptDetailModalProps {
scriptPath: string, scriptPath: string,
scriptName: string, scriptName: string,
mode?: "local" | "ssh", mode?: "local" | "ssh",
server?: any, server?: Server,
) => void; ) => void;
} }
@@ -31,7 +37,11 @@ export function ScriptDetailModal({
onClose, onClose,
onInstallScript, onInstallScript,
}: ScriptDetailModalProps) { }: ScriptDetailModalProps) {
useRegisterModal(isOpen, { id: 'script-detail-modal', allowEscape: true, onClose }); useRegisterModal(isOpen, {
id: "script-detail-modal",
allowEscape: true,
onClose,
});
const [imageError, setImageError] = useState(false); const [imageError, setImageError] = useState(false);
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [loadMessage, setLoadMessage] = useState<string | null>(null); const [loadMessage, setLoadMessage] = useState<string | null>(null);
@@ -40,7 +50,9 @@ export function ScriptDetailModal({
const [textViewerOpen, setTextViewerOpen] = useState(false); const [textViewerOpen, setTextViewerOpen] = useState(false);
const [executionModeOpen, setExecutionModeOpen] = useState(false); const [executionModeOpen, setExecutionModeOpen] = useState(false);
const [versionModalOpen, setVersionModalOpen] = useState(false); const [versionModalOpen, setVersionModalOpen] = useState(false);
const [selectedVersionType, setSelectedVersionType] = useState<string | null>(null); const [selectedVersionType, setSelectedVersionType] = useState<string | null>(
null,
);
const [isDeleting, setIsDeleting] = useState(false); const [isDeleting, setIsDeleting] = useState(false);
const [deleteConfirmOpen, setDeleteConfirmOpen] = useState(false); const [deleteConfirmOpen, setDeleteConfirmOpen] = useState(false);
@@ -61,7 +73,11 @@ export function ScriptDetailModal({
isLoading: comparisonLoading, isLoading: comparisonLoading,
} = api.scripts.compareScriptContent.useQuery( } = api.scripts.compareScriptContent.useQuery(
{ slug: script?.slug ?? "" }, { slug: script?.slug ?? "" },
{ enabled: !!script && isOpen }, {
enabled: !!script && isOpen,
refetchOnMount: true,
staleTime: 0,
},
); );
// Load script mutation // Load script mutation
@@ -136,22 +152,27 @@ export function ScriptDetailModal({
const handleInstallScript = () => { const handleInstallScript = () => {
if (!script) return; if (!script) return;
// Check if script has multiple variants (default and alpine) // Check if script has multiple variants (default and alpine)
const installMethods = script.install_methods || []; const installMethods = script.install_methods || [];
const hasMultipleVariants = installMethods.filter(method => const hasMultipleVariants =
method.type === 'default' || method.type === 'alpine' installMethods.filter(
).length > 1; (method) => method.type === "default" || method.type === "alpine",
).length > 1;
if (hasMultipleVariants) { if (hasMultipleVariants) {
// Show version selection modal first // Show version selection modal first
setVersionModalOpen(true); setVersionModalOpen(true);
} else { } else {
// Only one variant, proceed directly to execution mode // Only one variant, proceed directly to execution mode
// Use the first available method or default to 'default' type // Use the first available method or default to 'default' type
const defaultMethod = installMethods.find(method => method.type === 'default'); const defaultMethod = installMethods.find(
(method) => method.type === "default",
);
const firstMethod = installMethods[0]; const firstMethod = installMethods[0];
setSelectedVersionType(defaultMethod?.type || firstMethod?.type || 'default'); setSelectedVersionType(
defaultMethod?.type ?? firstMethod?.type ?? "default",
);
setExecutionModeOpen(true); setExecutionModeOpen(true);
} }
}; };
@@ -162,17 +183,16 @@ export function ScriptDetailModal({
setExecutionModeOpen(true); setExecutionModeOpen(true);
}; };
const handleExecuteScript = (mode: "local" | "ssh", server?: any) => { const handleExecuteScript = (mode: "local" | "ssh", server?: Server) => {
if (!script || !onInstallScript) return; if (!script || !onInstallScript) return;
// Find the script path based on selected version type // Find the script path based on selected version type
const versionType = selectedVersionType || 'default'; const versionType = selectedVersionType ?? "default";
const scriptMethod = script.install_methods?.find( const scriptMethod =
(method) => method.type === versionType && method.script, script.install_methods?.find(
) || script.install_methods?.find( (method) => method.type === versionType && method.script,
(method) => method.script, ) ?? script.install_methods?.find((method) => method.script);
);
if (scriptMethod?.script) { if (scriptMethod?.script) {
const scriptPath = `scripts/${scriptMethod.script}`; const scriptPath = `scripts/${scriptMethod.script}`;
const scriptName = script.name; const scriptName = script.name;
@@ -203,31 +223,31 @@ export function ScriptDetailModal({
return ( return (
<div <div
className="fixed inset-0 z-50 flex items-center justify-center p-4 backdrop-blur-sm bg-black/50" className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4 backdrop-blur-sm"
onClick={handleBackdropClick} onClick={handleBackdropClick}
> >
<div className="bg-card rounded-lg shadow-xl max-w-6xl w-full max-h-[95vh] min-h-[80vh] overflow-y-auto border border-border mx-2 sm:mx-4 lg:mx-0"> <div className="bg-card border-border mx-2 max-h-[95vh] min-h-[80vh] w-full max-w-6xl overflow-y-auto rounded-lg border shadow-xl sm:mx-4 lg:mx-0">
{/* Header */} {/* Header */}
<div className="flex items-center justify-between border-b border-border p-4 sm:p-6"> <div className="border-border flex items-center justify-between border-b p-4 sm:p-6">
<div className="flex items-center space-x-3 sm:space-x-4 min-w-0 flex-1"> <div className="flex min-w-0 flex-1 items-center space-x-3 sm:space-x-4">
{script.logo && !imageError ? ( {script.logo && !imageError ? (
<Image <Image
src={script.logo} src={script.logo}
alt={`${script.name} logo`} alt={`${script.name} logo`}
width={64} width={64}
height={64} height={64}
className="h-12 w-12 sm:h-16 sm:w-16 rounded-lg object-contain flex-shrink-0" className="h-12 w-12 flex-shrink-0 rounded-lg object-contain sm:h-16 sm:w-16"
onError={handleImageError} onError={handleImageError}
/> />
) : ( ) : (
<div className="flex h-12 w-12 sm:h-16 sm:w-16 items-center justify-center rounded-lg bg-muted flex-shrink-0"> <div className="bg-muted flex h-12 w-12 flex-shrink-0 items-center justify-center rounded-lg sm:h-16 sm:w-16">
<span className="text-lg sm:text-2xl font-semibold text-muted-foreground"> <span className="text-muted-foreground text-lg font-semibold sm:text-2xl">
{script.name.charAt(0).toUpperCase()} {script.name.charAt(0).toUpperCase()}
</span> </span>
</div> </div>
)} )}
<div className="min-w-0 flex-1"> <div className="min-w-0 flex-1">
<h2 className="text-xl sm:text-2xl font-bold text-foreground truncate"> <h2 className="text-foreground truncate text-xl font-bold sm:text-2xl">
{script.name} {script.name}
</h2> </h2>
<div className="mt-1 flex flex-wrap items-center gap-1 sm:gap-2"> <div className="mt-1 flex flex-wrap items-center gap-1 sm:gap-2">
@@ -239,37 +259,39 @@ export function ScriptDetailModal({
href={script.repository_url} href={script.repository_url}
target="_blank" target="_blank"
rel="noopener noreferrer" rel="noopener noreferrer"
className="text-xs px-2 py-0.5 bg-muted text-muted-foreground rounded border border-border hover:bg-accent hover:text-foreground transition-colors" className="bg-muted text-muted-foreground border-border hover:bg-accent hover:text-foreground rounded border px-2 py-0.5 text-xs transition-colors"
onClick={(e) => e.stopPropagation()} onClick={(e) => e.stopPropagation()}
title={`Source: ${script.repository_url}`} title={`Source: ${script.repository_url}`}
> >
{script.repository_url.match(/github\.com\/([^\/]+)\/([^\/]+)/)?.[0]?.replace('https://', '') ?? script.repository_url} {/github\.com\/([^\/]+)\/([^\/]+)/
.exec(script.repository_url)?.[0]
?.replace("https://", "") ?? script.repository_url}
</a> </a>
)} )}
</div> </div>
</div> </div>
{/* Interface Port*/} {/* Interface Port*/}
{script.interface_port && ( {script.interface_port && (
<div className="ml-3 sm:ml-4 flex-shrink-0"> <div className="ml-3 flex-shrink-0 sm:ml-4">
<div className="bg-primary/10 border border-primary/30 rounded-lg px-3 py-1.5 sm:px-4 sm:py-2"> <div className="bg-primary/10 border-primary/30 rounded-lg border px-3 py-1.5 sm:px-4 sm:py-2">
<span className="text-xs sm:text-sm font-medium text-muted-foreground mr-2"> <span className="text-muted-foreground mr-2 text-xs font-medium sm:text-sm">
Port: Port:
</span> </span>
<span className="text-sm sm:text-base font-semibold text-foreground font-mono"> <span className="text-foreground font-mono text-sm font-semibold sm:text-base">
{script.interface_port} {script.interface_port}
</span> </span>
</div> </div>
</div> </div>
)} )}
</div> </div>
{/* Close Button */} {/* Close Button */}
<Button <Button
onClick={onClose} onClick={onClose}
variant="ghost" variant="ghost"
size="icon" size="icon"
className="text-muted-foreground hover:text-foreground flex-shrink-0 ml-4" className="text-muted-foreground hover:text-foreground ml-4 flex-shrink-0"
> >
<svg <svg
className="h-5 w-5 sm:h-6 sm:w-6" className="h-5 w-5 sm:h-6 sm:w-6"
@@ -288,189 +310,91 @@ export function ScriptDetailModal({
</div> </div>
{/* Action Buttons */} {/* Action Buttons */}
<div className="flex flex-col sm:flex-row items-stretch sm:items-center space-y-2 sm:space-y-0 sm:space-x-2 p-4 sm:p-6 border-b border-border"> <div className="border-border flex flex-col items-stretch space-y-2 border-b p-4 sm:flex-row sm:items-center sm:space-y-0 sm:space-x-2 sm:p-6">
{/* Install Button - only show if script files exist */} {/* Install Button - only show if script files exist */}
{scriptFilesData?.success && {scriptFilesData?.success &&
scriptFilesData.ctExists && scriptFilesData.ctExists &&
onInstallScript && ( onInstallScript && (
<Button <Button
onClick={handleInstallScript} onClick={handleInstallScript}
variant="outline" variant="outline"
size="default" size="default"
className="w-full sm:w-auto flex items-center justify-center space-x-2" className="flex w-full items-center justify-center space-x-2 sm:w-auto"
>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
> >
<svg <path
className="h-4 w-4" strokeLinecap="round"
fill="none" strokeLinejoin="round"
stroke="currentColor" strokeWidth={2}
viewBox="0 0 24 24" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"
> />
<path </svg>
strokeLinecap="round" <span>Install</span>
strokeLinejoin="round" </Button>
strokeWidth={2} )}
d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"
/>
</svg>
<span>Install</span>
</Button>
)}
{/* View Button - only show if script files exist */} {/* View Button - only show if script files exist */}
{scriptFilesData?.success && {scriptFilesData?.success &&
(scriptFilesData.ctExists || scriptFilesData.installExists) && ( (scriptFilesData.ctExists || scriptFilesData.installExists) && (
<Button <Button
onClick={handleViewScript} onClick={handleViewScript}
variant="outline" variant="outline"
size="default" size="default"
className="w-full sm:w-auto flex items-center justify-center space-x-2" className="flex w-full items-center justify-center space-x-2 sm:w-auto"
>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
> >
<svg <path
className="h-4 w-4" strokeLinecap="round"
fill="none" strokeLinejoin="round"
stroke="currentColor" strokeWidth={2}
viewBox="0 0 24 24" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
> />
<path <path
strokeLinecap="round" strokeLinecap="round"
strokeLinejoin="round" strokeLinejoin="round"
strokeWidth={2} strokeWidth={2}
d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"
/> />
<path </svg>
strokeLinecap="round" <span>View</span>
strokeLinejoin="round" </Button>
strokeWidth={2} )}
d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"
/>
</svg>
<span>View</span>
</Button>
)}
{/* Load/Update Script Button */} {/* Load/Update Script Button */}
{(() => { {(() => {
const hasLocalFiles = const hasLocalFiles =
scriptFilesData?.success && scriptFilesData?.success &&
(scriptFilesData.ctExists || scriptFilesData.installExists); (scriptFilesData.ctExists || scriptFilesData.installExists);
const hasDifferences = const hasDifferences =
comparisonData?.success && comparisonData.hasDifferences; comparisonData?.success && comparisonData.hasDifferences;
const isUpToDate = hasLocalFiles && !hasDifferences; const isUpToDate = hasLocalFiles && !hasDifferences;
if (!hasLocalFiles) { if (!hasLocalFiles) {
// No local files - show Load Script button // No local files - show Load Script button
return ( return (
<button <button
onClick={handleLoadScript} onClick={handleLoadScript}
disabled={isLoading} disabled={isLoading}
className={`flex items-center space-x-2 rounded-lg px-4 py-2 font-medium transition-colors ${ className={`flex items-center space-x-2 rounded-lg px-4 py-2 font-medium transition-colors ${
isLoading isLoading
? "cursor-not-allowed bg-muted text-muted-foreground" ? "bg-muted text-muted-foreground cursor-not-allowed"
: "bg-success text-success-foreground hover:bg-success/90" : "bg-success text-success-foreground hover:bg-success/90"
}`} }`}
>
{isLoading ? (
<>
<div className="h-4 w-4 animate-spin rounded-full border-b-2 border-white"></div>
<span>Loading...</span>
</>
) : (
<>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
/>
</svg>
<span>Load Script</span>
</>
)}
</button>
);
} else if (isUpToDate) {
// Local files exist and are up to date - show disabled Update button
return (
<button
disabled
className="flex cursor-not-allowed items-center space-x-2 rounded-lg bg-muted px-4 py-2 font-medium text-muted-foreground transition-colors"
>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
/>
</svg>
<span>Up to Date</span>
</button>
);
} else {
// Local files exist but have differences - show Update button
return (
<button
onClick={handleLoadScript}
disabled={isLoading}
className={`flex items-center space-x-2 rounded-lg px-4 py-2 font-medium transition-colors ${
isLoading
? "cursor-not-allowed bg-muted text-muted-foreground"
: "bg-warning text-warning-foreground hover:bg-warning/90"
}`}
>
{isLoading ? (
<>
<div className="h-4 w-4 animate-spin rounded-full border-b-2 border-white"></div>
<span>Updating...</span>
</>
) : (
<>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
/>
</svg>
<span>Update Script</span>
</>
)}
</button>
);
}
})()}
{/* Delete Button - only show if script files exist */}
{scriptFilesData?.success &&
(scriptFilesData.ctExists || scriptFilesData.installExists) && (
<Button
onClick={handleDeleteScript}
disabled={isDeleting}
variant="destructive"
size="default"
className="w-full sm:w-auto flex items-center justify-center space-x-2"
> >
{isDeleting ? ( {isLoading ? (
<> <>
<div className="h-4 w-4 animate-spin rounded-full border-b-2 border-white"></div> <div className="h-4 w-4 animate-spin rounded-full border-b-2 border-white"></div>
<span>Deleting...</span> <span>Loading...</span>
</> </>
) : ( ) : (
<> <>
@@ -484,23 +408,121 @@ export function ScriptDetailModal({
strokeLinecap="round" strokeLinecap="round"
strokeLinejoin="round" strokeLinejoin="round"
strokeWidth={2} strokeWidth={2}
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
/> />
</svg> </svg>
<span>Delete Script</span> <span>Load Script</span>
</> </>
)} )}
</Button> </button>
)} );
} else if (isUpToDate) {
// Local files exist and are up to date - show disabled Update button
return (
<button
disabled
className="bg-muted text-muted-foreground flex cursor-not-allowed items-center space-x-2 rounded-lg px-4 py-2 font-medium transition-colors"
>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
/>
</svg>
<span>Up to Date</span>
</button>
);
} else {
// Local files exist but have differences - show Update button
return (
<button
onClick={handleLoadScript}
disabled={isLoading}
className={`flex items-center space-x-2 rounded-lg px-4 py-2 font-medium transition-colors ${
isLoading
? "bg-muted text-muted-foreground cursor-not-allowed"
: "bg-warning text-warning-foreground hover:bg-warning/90"
}`}
>
{isLoading ? (
<>
<div className="h-4 w-4 animate-spin rounded-full border-b-2 border-white"></div>
<span>Updating...</span>
</>
) : (
<>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
/>
</svg>
<span>Update Script</span>
</>
)}
</button>
);
}
})()}
{/* Delete Button - only show if script files exist */}
{scriptFilesData?.success &&
(scriptFilesData.ctExists || scriptFilesData.installExists) && (
<Button
onClick={handleDeleteScript}
disabled={isDeleting}
variant="destructive"
size="default"
className="flex w-full items-center justify-center space-x-2 sm:w-auto"
>
{isDeleting ? (
<>
<div className="h-4 w-4 animate-spin rounded-full border-b-2 border-white"></div>
<span>Deleting...</span>
</>
) : (
<>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
/>
</svg>
<span>Delete Script</span>
</>
)}
</Button>
)}
</div> </div>
{/* Content */} {/* Content */}
<div className="space-y-4 sm:space-y-6 p-4 sm:p-6"> <div className="space-y-4 p-4 sm:space-y-6 sm:p-6">
{/* Script Files Status */} {/* Script Files Status */}
{(scriptFilesLoading || comparisonLoading) && ( {(scriptFilesLoading || comparisonLoading) && (
<div className="mb-4 rounded-lg bg-primary/10 p-3 text-sm text-primary"> <div className="bg-primary/10 text-primary mb-4 rounded-lg p-3 text-sm">
<div className="flex items-center space-x-2"> <div className="flex items-center space-x-2">
<div className="h-4 w-4 animate-spin rounded-full border-b-2 border-primary"></div> <div className="border-primary h-4 w-4 animate-spin rounded-full border-b-2"></div>
<span>Loading script status...</span> <span>Loading script status...</span>
</div> </div>
</div> </div>
@@ -523,8 +545,8 @@ export function ScriptDetailModal({
} }
return ( return (
<div className="mb-4 rounded-lg bg-muted p-3 text-sm text-muted-foreground"> <div className="bg-muted text-muted-foreground mb-4 rounded-lg p-3 text-sm">
<div className="flex flex-col sm:flex-row sm:items-center space-y-2 sm:space-y-0 sm:space-x-4"> <div className="flex flex-col space-y-2 sm:flex-row sm:items-center sm:space-y-0 sm:space-x-4">
<div className="flex items-center space-x-2"> <div className="flex items-center space-x-2">
<div <div
className={`h-2 w-2 rounded-full ${scriptFilesData.ctExists ? "bg-success" : "bg-muted"}`} className={`h-2 w-2 rounded-full ${scriptFilesData.ctExists ? "bg-success" : "bg-muted"}`}
@@ -547,24 +569,67 @@ export function ScriptDetailModal({
</div> </div>
{scriptFilesData?.success && {scriptFilesData?.success &&
(scriptFilesData.ctExists || (scriptFilesData.ctExists ||
scriptFilesData.installExists) && scriptFilesData.installExists) && (
comparisonData?.success &&
!comparisonLoading && (
<div className="flex items-center space-x-2"> <div className="flex items-center space-x-2">
<div {comparisonData?.success ? (
className={`h-2 w-2 rounded-full ${comparisonData.hasDifferences ? "bg-warning" : "bg-success"}`} <>
></div> <div
<span> className={`h-2 w-2 rounded-full ${comparisonData.hasDifferences ? "bg-warning" : "bg-success"}`}
Status:{" "} ></div>
{comparisonData.hasDifferences <span>
? "Update available" Status:{" "}
: "Up to date"} {comparisonData.hasDifferences
</span> ? "Update available"
: "Up to date"}
</span>
</>
) : comparisonLoading ? (
<>
<div className="bg-muted h-2 w-2 animate-pulse rounded-full"></div>
<span>Checking for updates...</span>
</>
) : comparisonData?.error ? (
<>
<div className="bg-destructive h-2 w-2 rounded-full"></div>
<span className="text-destructive">
Error: {comparisonData.error}
</span>
</>
) : (
<>
<div className="bg-muted h-2 w-2 rounded-full"></div>
<span>Status: Unknown</span>
</>
)}
<button
onClick={() => void refetchComparison()}
disabled={comparisonLoading}
className="hover:bg-accent ml-2 flex items-center justify-center rounded-md p-1.5 transition-colors disabled:cursor-not-allowed disabled:opacity-50"
title="Refresh comparison"
>
{comparisonLoading ? (
<div className="h-4 w-4 animate-spin rounded-full border-2 border-current border-t-transparent"></div>
) : (
<svg
className="text-muted-foreground hover:text-foreground h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
/>
</svg>
)}
</button>
</div> </div>
)} )}
</div> </div>
{scriptFilesData.files.length > 0 && ( {scriptFilesData.files.length > 0 && (
<div className="mt-2 text-xs text-muted-foreground break-words"> <div className="text-muted-foreground mt-2 text-xs break-words">
Files: {scriptFilesData.files.join(", ")} Files: {scriptFilesData.files.join(", ")}
</div> </div>
)} )}
@@ -574,17 +639,17 @@ export function ScriptDetailModal({
{/* Load Message */} {/* Load Message */}
{loadMessage && ( {loadMessage && (
<div className="mb-4 rounded-lg bg-primary/10 p-3 text-sm text-primary"> <div className="bg-primary/10 text-primary mb-4 rounded-lg p-3 text-sm">
{loadMessage} {loadMessage}
</div> </div>
)} )}
{/* Description */} {/* Description */}
<div> <div>
<h3 className="mb-2 text-base sm:text-lg font-semibold text-foreground"> <h3 className="text-foreground mb-2 text-base font-semibold sm:text-lg">
Description Description
</h3> </h3>
<p className="text-sm sm:text-base text-muted-foreground"> <p className="text-muted-foreground text-sm sm:text-base">
{script.description} {script.description}
</p> </p>
</div> </div>
@@ -592,50 +657,50 @@ export function ScriptDetailModal({
{/* Basic Information */} {/* Basic Information */}
<div className="grid grid-cols-1 gap-4 sm:gap-6 lg:grid-cols-2"> <div className="grid grid-cols-1 gap-4 sm:gap-6 lg:grid-cols-2">
<div> <div>
<h3 className="mb-3 text-base sm:text-lg font-semibold text-foreground"> <h3 className="text-foreground mb-3 text-base font-semibold sm:text-lg">
Basic Information Basic Information
</h3> </h3>
<dl className="space-y-2"> <dl className="space-y-2">
<div> <div>
<dt className="text-sm font-medium text-muted-foreground"> <dt className="text-muted-foreground text-sm font-medium">
Slug Slug
</dt> </dt>
<dd className="font-mono text-sm text-foreground"> <dd className="text-foreground font-mono text-sm">
{script.slug} {script.slug}
</dd> </dd>
</div> </div>
<div> <div>
<dt className="text-sm font-medium text-muted-foreground"> <dt className="text-muted-foreground text-sm font-medium">
Date Created Date Created
</dt> </dt>
<dd className="text-sm text-foreground"> <dd className="text-foreground text-sm">
{script.date_created} {script.date_created}
</dd> </dd>
</div> </div>
<div> <div>
<dt className="text-sm font-medium text-muted-foreground"> <dt className="text-muted-foreground text-sm font-medium">
Categories Categories
</dt> </dt>
<dd className="text-sm text-foreground"> <dd className="text-foreground text-sm">
{script.categories.join(", ")} {script.categories.join(", ")}
</dd> </dd>
</div> </div>
{script.interface_port && ( {script.interface_port && (
<div> <div>
<dt className="text-sm font-medium text-muted-foreground"> <dt className="text-muted-foreground text-sm font-medium">
Interface Port Interface Port
</dt> </dt>
<dd className="text-sm text-foreground"> <dd className="text-foreground text-sm">
{script.interface_port} {script.interface_port}
</dd> </dd>
</div> </div>
)} )}
{script.config_path && ( {script.config_path && (
<div> <div>
<dt className="text-sm font-medium text-muted-foreground"> <dt className="text-muted-foreground text-sm font-medium">
Config Path Config Path
</dt> </dt>
<dd className="font-mono text-sm text-foreground"> <dd className="text-foreground font-mono text-sm">
{script.config_path} {script.config_path}
</dd> </dd>
</div> </div>
@@ -644,13 +709,13 @@ export function ScriptDetailModal({
</div> </div>
<div> <div>
<h3 className="mb-3 text-base sm:text-lg font-semibold text-foreground"> <h3 className="text-foreground mb-3 text-base font-semibold sm:text-lg">
Links Links
</h3> </h3>
<dl className="space-y-2"> <dl className="space-y-2">
{script.website && ( {script.website && (
<div> <div>
<dt className="text-sm font-medium text-muted-foreground"> <dt className="text-muted-foreground text-sm font-medium">
Website Website
</dt> </dt>
<dd className="text-sm"> <dd className="text-sm">
@@ -658,7 +723,7 @@ export function ScriptDetailModal({
href={script.website} href={script.website}
target="_blank" target="_blank"
rel="noopener noreferrer" rel="noopener noreferrer"
className="break-all text-primary hover:text-primary/80" className="text-primary hover:text-primary/80 break-all"
> >
{script.website} {script.website}
</a> </a>
@@ -667,7 +732,7 @@ export function ScriptDetailModal({
)} )}
{script.documentation && ( {script.documentation && (
<div> <div>
<dt className="text-sm font-medium text-muted-foreground"> <dt className="text-muted-foreground text-sm font-medium">
Documentation Documentation
</dt> </dt>
<dd className="text-sm"> <dd className="text-sm">
@@ -675,7 +740,7 @@ export function ScriptDetailModal({
href={script.documentation} href={script.documentation}
target="_blank" target="_blank"
rel="noopener noreferrer" rel="noopener noreferrer"
className="break-all text-primary hover:text-primary/80" className="text-primary hover:text-primary/80 break-all"
> >
{script.documentation} {script.documentation}
</a> </a>
@@ -691,26 +756,26 @@ export function ScriptDetailModal({
script.type !== "pve" && script.type !== "pve" &&
script.type !== "addon" && ( script.type !== "addon" && (
<div> <div>
<h3 className="mb-3 text-base sm:text-lg font-semibold text-foreground"> <h3 className="text-foreground mb-3 text-base font-semibold sm:text-lg">
Install Methods Install Methods
</h3> </h3>
<div className="space-y-4"> <div className="space-y-4">
{script.install_methods.map((method, index) => ( {script.install_methods.map((method, index) => (
<div <div
key={index} key={index}
className="rounded-lg border border-border bg-card p-3 sm:p-4" className="border-border bg-card rounded-lg border p-3 sm:p-4"
> >
<div className="mb-3 flex flex-col sm:flex-row sm:items-center justify-between space-y-1 sm:space-y-0"> <div className="mb-3 flex flex-col justify-between space-y-1 sm:flex-row sm:items-center sm:space-y-0">
<h4 className="text-sm sm:text-base font-medium text-foreground capitalize"> <h4 className="text-foreground text-sm font-medium capitalize sm:text-base">
{method.type} {method.type}
</h4> </h4>
<span className="font-mono text-xs sm:text-sm text-muted-foreground break-all"> <span className="text-muted-foreground font-mono text-xs break-all sm:text-sm">
{method.script} {method.script}
</span> </span>
</div> </div>
<div className="grid grid-cols-2 gap-2 sm:gap-4 text-xs sm:text-sm lg:grid-cols-4"> <div className="grid grid-cols-2 gap-2 text-xs sm:gap-4 sm:text-sm lg:grid-cols-4">
<div> <div>
<dt className="font-medium text-muted-foreground"> <dt className="text-muted-foreground font-medium">
CPU CPU
</dt> </dt>
<dd className="text-foreground"> <dd className="text-foreground">
@@ -718,7 +783,7 @@ export function ScriptDetailModal({
</dd> </dd>
</div> </div>
<div> <div>
<dt className="font-medium text-muted-foreground"> <dt className="text-muted-foreground font-medium">
RAM RAM
</dt> </dt>
<dd className="text-foreground"> <dd className="text-foreground">
@@ -726,7 +791,7 @@ export function ScriptDetailModal({
</dd> </dd>
</div> </div>
<div> <div>
<dt className="font-medium text-muted-foreground"> <dt className="text-muted-foreground font-medium">
HDD HDD
</dt> </dt>
<dd className="text-foreground"> <dd className="text-foreground">
@@ -734,7 +799,7 @@ export function ScriptDetailModal({
</dd> </dd>
</div> </div>
<div> <div>
<dt className="font-medium text-muted-foreground"> <dt className="text-muted-foreground font-medium">
OS OS
</dt> </dt>
<dd className="text-foreground"> <dd className="text-foreground">
@@ -752,26 +817,26 @@ export function ScriptDetailModal({
{(script.default_credentials.username ?? {(script.default_credentials.username ??
script.default_credentials.password) && ( script.default_credentials.password) && (
<div> <div>
<h3 className="mb-3 text-base sm:text-lg font-semibold text-foreground"> <h3 className="text-foreground mb-3 text-base font-semibold sm:text-lg">
Default Credentials Default Credentials
</h3> </h3>
<dl className="space-y-2"> <dl className="space-y-2">
{script.default_credentials.username && ( {script.default_credentials.username && (
<div> <div>
<dt className="text-sm font-medium text-muted-foreground"> <dt className="text-muted-foreground text-sm font-medium">
Username Username
</dt> </dt>
<dd className="font-mono text-sm text-foreground"> <dd className="text-foreground font-mono text-sm">
{script.default_credentials.username} {script.default_credentials.username}
</dd> </dd>
</div> </div>
)} )}
{script.default_credentials.password && ( {script.default_credentials.password && (
<div> <div>
<dt className="text-sm font-medium text-muted-foreground"> <dt className="text-muted-foreground text-sm font-medium">
Password Password
</dt> </dt>
<dd className="font-mono text-sm text-foreground"> <dd className="text-foreground font-mono text-sm">
{script.default_credentials.password} {script.default_credentials.password}
</dd> </dd>
</div> </div>
@@ -783,7 +848,7 @@ export function ScriptDetailModal({
{/* Notes */} {/* Notes */}
{script.notes.length > 0 && ( {script.notes.length > 0 && (
<div> <div>
<h3 className="mb-3 text-lg font-semibold text-foreground"> <h3 className="text-foreground mb-3 text-lg font-semibold">
Notes Notes
</h3> </h3>
<ul className="space-y-2"> <ul className="space-y-2">
@@ -798,14 +863,17 @@ export function ScriptDetailModal({
key={index} key={index}
className={`rounded-lg p-3 text-sm ${ className={`rounded-lg p-3 text-sm ${
noteType === "warning" noteType === "warning"
? "border-l-4 border-warning bg-warning/10 text-warning" ? "border-warning bg-warning/10 text-warning border-l-4"
: noteType === "error" : noteType === "error"
? "border-l-4 border-destructive bg-destructive/10 text-destructive" ? "border-destructive bg-destructive/10 text-destructive border-l-4"
: "bg-muted text-muted-foreground" : "bg-muted text-muted-foreground"
}`} }`}
> >
<div className="flex items-start"> <div className="flex items-start">
<NoteBadge noteType={noteType as 'info' | 'warning' | 'error'} className="mr-2 flex-shrink-0"> <NoteBadge
noteType={noteType as "info" | "warning" | "error"}
className="mr-2 flex-shrink-0"
>
{noteType} {noteType}
</NoteBadge> </NoteBadge>
<span>{noteText}</span> <span>{noteText}</span>
@@ -837,7 +905,13 @@ export function ScriptDetailModal({
<TextViewer <TextViewer
scriptName={ scriptName={
script.install_methods script.install_methods
?.find((method) => method.script?.startsWith("ct/")) ?.find(
(method) =>
method.script &&
(method.script.startsWith("ct/") ||
method.script.startsWith("vm/") ||
method.script.startsWith("tools/")),
)
?.script?.split("/") ?.script?.split("/")
.pop() ?? `${script.slug}.sh` .pop() ?? `${script.slug}.sh`
} }

View File

@@ -33,6 +33,7 @@ interface InstalledScript {
container_status?: 'running' | 'stopped' | 'unknown'; container_status?: 'running' | 'stopped' | 'unknown';
web_ui_ip: string | null; web_ui_ip: string | null;
web_ui_port: number | null; web_ui_port: number | null;
is_vm?: boolean;
} }
interface ScriptInstallationCardProps { interface ScriptInstallationCardProps {
@@ -44,6 +45,8 @@ interface ScriptInstallationCardProps {
onSave: () => void; onSave: () => void;
onCancel: () => void; onCancel: () => void;
onUpdate: () => void; onUpdate: () => void;
onBackup?: () => void;
onClone?: () => void;
onShell: () => void; onShell: () => void;
onDelete: () => void; onDelete: () => void;
isUpdating: boolean; isUpdating: boolean;
@@ -68,6 +71,8 @@ export function ScriptInstallationCard({
onSave, onSave,
onCancel, onCancel,
onUpdate, onUpdate,
onBackup,
onClone,
onShell, onShell,
onDelete, onDelete,
isUpdating, isUpdating,
@@ -298,7 +303,7 @@ export function ScriptInstallationCard({
</Button> </Button>
</DropdownMenuTrigger> </DropdownMenuTrigger>
<DropdownMenuContent className="w-48 bg-card border-border"> <DropdownMenuContent className="w-48 bg-card border-border">
{script.container_id && ( {script.container_id && !script.is_vm && (
<DropdownMenuItem <DropdownMenuItem
onClick={onUpdate} onClick={onUpdate}
disabled={containerStatus === 'stopped'} disabled={containerStatus === 'stopped'}
@@ -307,6 +312,24 @@ export function ScriptInstallationCard({
Update Update
</DropdownMenuItem> </DropdownMenuItem>
)} )}
{script.container_id && script.execution_mode === 'ssh' && onBackup && (
<DropdownMenuItem
onClick={onBackup}
disabled={containerStatus === 'stopped'}
className="text-muted-foreground hover:text-foreground hover:bg-muted/20 focus:bg-muted/20"
>
Backup
</DropdownMenuItem>
)}
{script.container_id && script.execution_mode === 'ssh' && onClone && (
<DropdownMenuItem
onClick={onClone}
disabled={containerStatus === 'stopped'}
className="text-muted-foreground hover:text-foreground hover:bg-muted/20 focus:bg-muted/20"
>
Clone
</DropdownMenuItem>
)}
{script.container_id && script.execution_mode === 'ssh' && ( {script.container_id && script.execution_mode === 'ssh' && (
<DropdownMenuItem <DropdownMenuItem
onClick={onShell} onClick={onShell}

View File

@@ -1,9 +1,9 @@
'use client'; "use client";
import { useState } from 'react'; import { useState } from "react";
import type { Script, ScriptInstallMethod } from '../../types/script'; import type { Script } from "../../types/script";
import { Button } from './ui/button'; import { Button } from "./ui/button";
import { useRegisterModal } from './modal/ModalStackProvider'; import { useRegisterModal } from "./modal/ModalStackProvider";
interface ScriptVersionModalProps { interface ScriptVersionModalProps {
isOpen: boolean; isOpen: boolean;
@@ -12,16 +12,29 @@ interface ScriptVersionModalProps {
script: Script | null; script: Script | null;
} }
export function ScriptVersionModal({ isOpen, onClose, onSelectVersion, script }: ScriptVersionModalProps) { export function ScriptVersionModal({
useRegisterModal(isOpen, { id: 'script-version-modal', allowEscape: true, onClose }); isOpen,
onClose,
onSelectVersion,
script,
}: ScriptVersionModalProps) {
useRegisterModal(isOpen, {
id: "script-version-modal",
allowEscape: true,
onClose,
});
const [selectedVersion, setSelectedVersion] = useState<string | null>(null); const [selectedVersion, setSelectedVersion] = useState<string | null>(null);
if (!isOpen || !script) return null; if (!isOpen || !script) return null;
// Get available install methods // Get available install methods
const installMethods = script.install_methods || []; const installMethods = script.install_methods || [];
const defaultMethod = installMethods.find(method => method.type === 'default'); const defaultMethod = installMethods.find(
const alpineMethod = installMethods.find(method => method.type === 'alpine'); (method) => method.type === "default",
);
const alpineMethod = installMethods.find(
(method) => method.type === "alpine",
);
const handleConfirm = () => { const handleConfirm = () => {
if (selectedVersion) { if (selectedVersion) {
@@ -35,19 +48,29 @@ export function ScriptVersionModal({ isOpen, onClose, onSelectVersion, script }:
}; };
return ( return (
<div className="fixed inset-0 backdrop-blur-sm bg-black/50 flex items-center justify-center z-50 p-4"> <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4 backdrop-blur-sm">
<div className="bg-card rounded-lg shadow-xl max-w-2xl w-full border border-border"> <div className="bg-card border-border w-full max-w-2xl rounded-lg border shadow-xl">
{/* Header */} {/* Header */}
<div className="flex items-center justify-between p-6 border-b border-border"> <div className="border-border flex items-center justify-between border-b p-6">
<h2 className="text-xl font-bold text-foreground">Select Version</h2> <h2 className="text-foreground text-xl font-bold">Select Version</h2>
<Button <Button
onClick={onClose} onClick={onClose}
variant="ghost" variant="ghost"
size="icon" size="icon"
className="text-muted-foreground hover:text-foreground" className="text-muted-foreground hover:text-foreground"
> >
<svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <svg
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" /> className="h-6 w-6"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M6 18L18 6M6 6l12 12"
/>
</svg> </svg>
</Button> </Button>
</div> </div>
@@ -55,11 +78,12 @@ export function ScriptVersionModal({ isOpen, onClose, onSelectVersion, script }:
{/* Content */} {/* Content */}
<div className="p-6"> <div className="p-6">
<div className="mb-6"> <div className="mb-6">
<h3 className="text-lg font-medium text-foreground mb-2"> <h3 className="text-foreground mb-2 text-lg font-medium">
Choose a version for &quot;{script.name}&quot; Choose a version for &quot;{script.name}&quot;
</h3> </h3>
<p className="text-sm text-muted-foreground"> <p className="text-muted-foreground text-sm">
Select the version you want to install. Each version has different resource requirements. Select the version you want to install. Each version has different
resource requirements.
</p> </p>
</div> </div>
@@ -67,25 +91,29 @@ export function ScriptVersionModal({ isOpen, onClose, onSelectVersion, script }:
{/* Default Version */} {/* Default Version */}
{defaultMethod && ( {defaultMethod && (
<div <div
onClick={() => handleVersionSelect('default')} onClick={() => handleVersionSelect("default")}
className={`cursor-pointer rounded-lg border-2 p-4 transition-all ${ className={`cursor-pointer rounded-lg border-2 p-4 transition-all ${
selectedVersion === 'default' selectedVersion === "default"
? 'border-primary bg-primary/10' ? "border-primary bg-primary/10"
: 'border-border bg-card hover:border-primary/50' : "border-border bg-card hover:border-primary/50"
}`} }`}
> >
<div className="flex items-start justify-between"> <div className="flex items-start justify-between">
<div className="flex-1"> <div className="flex-1">
<div className="flex items-center space-x-3 mb-3"> <div className="mb-3 flex items-center space-x-3">
<div <div
className={`w-5 h-5 rounded-full border-2 flex items-center justify-center ${ className={`flex h-5 w-5 items-center justify-center rounded-full border-2 ${
selectedVersion === 'default' selectedVersion === "default"
? 'border-primary bg-primary' ? "border-primary bg-primary"
: 'border-border' : "border-border"
}`} }`}
> >
{selectedVersion === 'default' && ( {selectedVersion === "default" && (
<svg className="w-3 h-3 text-white" fill="currentColor" viewBox="0 0 20 20"> <svg
className="h-3 w-3 text-white"
fill="currentColor"
viewBox="0 0 20 20"
>
<path <path
fillRule="evenodd" fillRule="evenodd"
d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
@@ -94,27 +122,34 @@ export function ScriptVersionModal({ isOpen, onClose, onSelectVersion, script }:
</svg> </svg>
)} )}
</div> </div>
<h4 className="text-base font-semibold text-foreground capitalize"> <h4 className="text-foreground text-base font-semibold capitalize">
{defaultMethod.type} {defaultMethod.type}
</h4> </h4>
</div> </div>
<div className="grid grid-cols-2 gap-3 text-sm ml-8"> <div className="ml-8 grid grid-cols-2 gap-3 text-sm">
<div> <div>
<span className="text-muted-foreground">CPU: </span> <span className="text-muted-foreground">CPU: </span>
<span className="text-foreground font-medium">{defaultMethod.resources.cpu} cores</span> <span className="text-foreground font-medium">
{defaultMethod.resources.cpu} cores
</span>
</div> </div>
<div> <div>
<span className="text-muted-foreground">RAM: </span> <span className="text-muted-foreground">RAM: </span>
<span className="text-foreground font-medium">{defaultMethod.resources.ram} MB</span> <span className="text-foreground font-medium">
{defaultMethod.resources.ram} MB
</span>
</div> </div>
<div> <div>
<span className="text-muted-foreground">HDD: </span> <span className="text-muted-foreground">HDD: </span>
<span className="text-foreground font-medium">{defaultMethod.resources.hdd} GB</span> <span className="text-foreground font-medium">
{defaultMethod.resources.hdd} GB
</span>
</div> </div>
<div> <div>
<span className="text-muted-foreground">OS: </span> <span className="text-muted-foreground">OS: </span>
<span className="text-foreground font-medium"> <span className="text-foreground font-medium">
{defaultMethod.resources.os} {defaultMethod.resources.version} {defaultMethod.resources.os}{" "}
{defaultMethod.resources.version}
</span> </span>
</div> </div>
</div> </div>
@@ -126,25 +161,29 @@ export function ScriptVersionModal({ isOpen, onClose, onSelectVersion, script }:
{/* Alpine Version */} {/* Alpine Version */}
{alpineMethod && ( {alpineMethod && (
<div <div
onClick={() => handleVersionSelect('alpine')} onClick={() => handleVersionSelect("alpine")}
className={`cursor-pointer rounded-lg border-2 p-4 transition-all ${ className={`cursor-pointer rounded-lg border-2 p-4 transition-all ${
selectedVersion === 'alpine' selectedVersion === "alpine"
? 'border-primary bg-primary/10' ? "border-primary bg-primary/10"
: 'border-border bg-card hover:border-primary/50' : "border-border bg-card hover:border-primary/50"
}`} }`}
> >
<div className="flex items-start justify-between"> <div className="flex items-start justify-between">
<div className="flex-1"> <div className="flex-1">
<div className="flex items-center space-x-3 mb-3"> <div className="mb-3 flex items-center space-x-3">
<div <div
className={`w-5 h-5 rounded-full border-2 flex items-center justify-center ${ className={`flex h-5 w-5 items-center justify-center rounded-full border-2 ${
selectedVersion === 'alpine' selectedVersion === "alpine"
? 'border-primary bg-primary' ? "border-primary bg-primary"
: 'border-border' : "border-border"
}`} }`}
> >
{selectedVersion === 'alpine' && ( {selectedVersion === "alpine" && (
<svg className="w-3 h-3 text-white" fill="currentColor" viewBox="0 0 20 20"> <svg
className="h-3 w-3 text-white"
fill="currentColor"
viewBox="0 0 20 20"
>
<path <path
fillRule="evenodd" fillRule="evenodd"
d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
@@ -153,27 +192,34 @@ export function ScriptVersionModal({ isOpen, onClose, onSelectVersion, script }:
</svg> </svg>
)} )}
</div> </div>
<h4 className="text-base font-semibold text-foreground capitalize"> <h4 className="text-foreground text-base font-semibold capitalize">
{alpineMethod.type} {alpineMethod.type}
</h4> </h4>
</div> </div>
<div className="grid grid-cols-2 gap-3 text-sm ml-8"> <div className="ml-8 grid grid-cols-2 gap-3 text-sm">
<div> <div>
<span className="text-muted-foreground">CPU: </span> <span className="text-muted-foreground">CPU: </span>
<span className="text-foreground font-medium">{alpineMethod.resources.cpu} cores</span> <span className="text-foreground font-medium">
{alpineMethod.resources.cpu} cores
</span>
</div> </div>
<div> <div>
<span className="text-muted-foreground">RAM: </span> <span className="text-muted-foreground">RAM: </span>
<span className="text-foreground font-medium">{alpineMethod.resources.ram} MB</span> <span className="text-foreground font-medium">
{alpineMethod.resources.ram} MB
</span>
</div> </div>
<div> <div>
<span className="text-muted-foreground">HDD: </span> <span className="text-muted-foreground">HDD: </span>
<span className="text-foreground font-medium">{alpineMethod.resources.hdd} GB</span> <span className="text-foreground font-medium">
{alpineMethod.resources.hdd} GB
</span>
</div> </div>
<div> <div>
<span className="text-muted-foreground">OS: </span> <span className="text-muted-foreground">OS: </span>
<span className="text-foreground font-medium"> <span className="text-foreground font-medium">
{alpineMethod.resources.os} {alpineMethod.resources.version} {alpineMethod.resources.os}{" "}
{alpineMethod.resources.version}
</span> </span>
</div> </div>
</div> </div>
@@ -184,12 +230,8 @@ export function ScriptVersionModal({ isOpen, onClose, onSelectVersion, script }:
</div> </div>
{/* Action Buttons */} {/* Action Buttons */}
<div className="flex justify-end space-x-3 mt-6"> <div className="mt-6 flex justify-end space-x-3">
<Button <Button onClick={onClose} variant="outline" size="default">
onClick={onClose}
variant="outline"
size="default"
>
Cancel Cancel
</Button> </Button>
<Button <Button
@@ -197,7 +239,9 @@ export function ScriptVersionModal({ isOpen, onClose, onSelectVersion, script }:
disabled={!selectedVersion} disabled={!selectedVersion}
variant="default" variant="default"
size="default" size="default"
className={!selectedVersion ? 'bg-muted-foreground cursor-not-allowed' : ''} className={
!selectedVersion ? "bg-muted-foreground cursor-not-allowed" : ""
}
> >
Continue Continue
</Button> </Button>
@@ -207,4 +251,3 @@ export function ScriptVersionModal({ isOpen, onClose, onSelectVersion, script }:
</div> </div>
); );
} }

File diff suppressed because it is too large Load Diff

View File

@@ -1,11 +1,11 @@
'use client'; "use client";
import { useState, useEffect } from 'react'; import { useState, useEffect } from "react";
import type { CreateServerData } from '../../types/server'; import type { CreateServerData } from "../../types/server";
import { Button } from './ui/button'; import { Button } from "./ui/button";
import { SSHKeyInput } from './SSHKeyInput'; import { SSHKeyInput } from "./SSHKeyInput";
import { PublicKeyModal } from './PublicKeyModal'; import { PublicKeyModal } from "./PublicKeyModal";
import { Key } from 'lucide-react'; import { Key } from "lucide-react";
interface ServerFormProps { interface ServerFormProps {
onSubmit: (data: CreateServerData) => void; onSubmit: (data: CreateServerData) => void;
@@ -14,40 +14,47 @@ interface ServerFormProps {
onCancel?: () => void; onCancel?: () => void;
} }
export function ServerForm({ onSubmit, initialData, isEditing = false, onCancel }: ServerFormProps) { export function ServerForm({
onSubmit,
initialData,
isEditing = false,
onCancel,
}: ServerFormProps) {
const [formData, setFormData] = useState<CreateServerData>( const [formData, setFormData] = useState<CreateServerData>(
initialData ?? { initialData ?? {
name: '', name: "",
ip: '', ip: "",
user: '', user: "",
password: '', password: "",
auth_type: 'password', auth_type: "password",
ssh_key: '', ssh_key: "",
ssh_key_passphrase: '', ssh_key_passphrase: "",
ssh_port: 22, ssh_port: 22,
color: '#3b82f6', color: "#3b82f6",
} },
); );
const [errors, setErrors] = useState<Partial<Record<keyof CreateServerData, string>>>({}); const [errors, setErrors] = useState<
const [sshKeyError, setSshKeyError] = useState<string>(''); Partial<Record<keyof CreateServerData, string>>
>({});
const [sshKeyError, setSshKeyError] = useState<string>("");
const [colorCodingEnabled, setColorCodingEnabled] = useState(false); const [colorCodingEnabled, setColorCodingEnabled] = useState(false);
const [isGeneratingKey, setIsGeneratingKey] = useState(false); const [isGeneratingKey, setIsGeneratingKey] = useState(false);
const [showPublicKeyModal, setShowPublicKeyModal] = useState(false); const [showPublicKeyModal, setShowPublicKeyModal] = useState(false);
const [generatedPublicKey, setGeneratedPublicKey] = useState(''); const [generatedPublicKey, setGeneratedPublicKey] = useState("");
const [, setIsGeneratedKey] = useState(false); const [, setIsGeneratedKey] = useState(false);
const [, setGeneratedServerId] = useState<number | null>(null); const [, setGeneratedServerId] = useState<number | null>(null);
useEffect(() => { useEffect(() => {
const loadColorCodingSetting = async () => { const loadColorCodingSetting = async () => {
try { try {
const response = await fetch('/api/settings/color-coding'); const response = await fetch("/api/settings/color-coding");
if (response.ok) { if (response.ok) {
const data = await response.json(); const data = await response.json();
setColorCodingEnabled(Boolean(data.enabled)); setColorCodingEnabled(Boolean(data.enabled));
} }
} catch (error) { } catch (error) {
console.error('Error loading color coding setting:', error); console.error("Error loading color coding setting:", error);
} }
}; };
void loadColorCodingSetting(); void loadColorCodingSetting();
@@ -58,15 +65,16 @@ export function ServerForm({ onSubmit, initialData, isEditing = false, onCancel
if (!trimmed) return false; if (!trimmed) return false;
// IPv4 validation // IPv4 validation
const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; const ipv4Regex =
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
if (ipv4Regex.test(trimmed)) { if (ipv4Regex.test(trimmed)) {
return true; return true;
} }
// Check for IPv6 with zone identifier (link-local addresses like fe80::...%eth0) // Check for IPv6 with zone identifier (link-local addresses like fe80::...%eth0)
let ipv6Address = trimmed; let ipv6Address = trimmed;
const zoneIdMatch = trimmed.match(/^(.+)%([a-zA-Z0-9_\-]+)$/); const zoneIdMatch = /^(.+)%([a-zA-Z0-9_\-]+)$/.exec(trimmed);
if (zoneIdMatch) { if (zoneIdMatch?.[1] && zoneIdMatch[2]) {
ipv6Address = zoneIdMatch[1]; ipv6Address = zoneIdMatch[1];
// Zone identifier should be a valid interface name (alphanumeric, underscore, hyphen) // Zone identifier should be a valid interface name (alphanumeric, underscore, hyphen)
const zoneId = zoneIdMatch[2]; const zoneId = zoneIdMatch[2];
@@ -79,10 +87,11 @@ export function ServerForm({ onSubmit, initialData, isEditing = false, onCancel
// Matches: 2001:0db8:85a3:0000:0000:8a2e:0370:7334, ::1, 2001:db8::1, etc. // Matches: 2001:0db8:85a3:0000:0000:8a2e:0370:7334, ::1, 2001:db8::1, etc.
// Also supports IPv4-mapped IPv6 addresses like ::ffff:192.168.1.1 // Also supports IPv4-mapped IPv6 addresses like ::ffff:192.168.1.1
// Simplified validation: check for valid hex segments separated by colons // Simplified validation: check for valid hex segments separated by colons
const ipv6Pattern = /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::1$|^::$|^(?:[0-9a-fA-F]{1,4}:)*::(?:[0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4}$|^(?:[0-9a-fA-F]{1,4}:)*::[0-9a-fA-F]{1,4}$|^::(?:[0-9a-fA-F]{1,4}:)+[0-9a-fA-F]{1,4}$|^::ffff:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^(?:[0-9a-fA-F]{1,4}:){1,4}:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; const ipv6Pattern =
/^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::1$|^::$|^(?:[0-9a-fA-F]{1,4}:)*::(?:[0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4}$|^(?:[0-9a-fA-F]{1,4}:)*::[0-9a-fA-F]{1,4}$|^::(?:[0-9a-fA-F]{1,4}:)+[0-9a-fA-F]{1,4}$|^::ffff:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^(?:[0-9a-fA-F]{1,4}:){1,4}:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
if (ipv6Pattern.test(ipv6Address)) { if (ipv6Pattern.test(ipv6Address)) {
// Additional validation: ensure only one :: compression exists // Additional validation: ensure only one :: compression exists
const compressionCount = (ipv6Address.match(/::/g) || []).length; const compressionCount = (ipv6Address.match(/::/g) ?? []).length;
if (compressionCount <= 1) { if (compressionCount <= 1) {
return true; return true;
} }
@@ -91,17 +100,19 @@ export function ServerForm({ onSubmit, initialData, isEditing = false, onCancel
// FQDN/hostname validation (RFC 1123 compliant) // FQDN/hostname validation (RFC 1123 compliant)
// Allows letters, numbers, hyphens, dots; must start and end with alphanumeric // Allows letters, numbers, hyphens, dots; must start and end with alphanumeric
// Max length 253 characters, each label max 63 characters // Max length 253 characters, each label max 63 characters
const hostnameRegex = /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?$/; const hostnameRegex =
/^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?$/;
if (hostnameRegex.test(trimmed) && trimmed.length <= 253) { if (hostnameRegex.test(trimmed) && trimmed.length <= 253) {
// Additional check: each label (between dots) must be max 63 chars // Additional check: each label (between dots) must be max 63 chars
const labels = trimmed.split('.'); const labels = trimmed.split(".");
if (labels.every(label => label.length > 0 && label.length <= 63)) { if (labels.every((label) => label.length > 0 && label.length <= 63)) {
return true; return true;
} }
} }
// Also allow simple hostnames without dots (like 'localhost') // Also allow simple hostnames without dots (like 'localhost')
const simpleHostnameRegex = /^[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?$/; const simpleHostnameRegex =
/^[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?$/;
if (simpleHostnameRegex.test(trimmed) && trimmed.length <= 63) { if (simpleHostnameRegex.test(trimmed) && trimmed.length <= 63) {
return true; return true;
} }
@@ -113,41 +124,44 @@ export function ServerForm({ onSubmit, initialData, isEditing = false, onCancel
const newErrors: Partial<Record<keyof CreateServerData, string>> = {}; const newErrors: Partial<Record<keyof CreateServerData, string>> = {};
if (!formData.name.trim()) { if (!formData.name.trim()) {
newErrors.name = 'Server name is required'; newErrors.name = "Server name is required";
} }
if (!formData.ip.trim()) { if (!formData.ip.trim()) {
newErrors.ip = 'Server address is required'; newErrors.ip = "Server address is required";
} else { } else {
if (!validateServerAddress(formData.ip)) { if (!validateServerAddress(formData.ip)) {
newErrors.ip = 'Please enter a valid IP address (IPv4/IPv6) or hostname'; newErrors.ip =
"Please enter a valid IP address (IPv4/IPv6) or hostname";
} }
} }
if (!formData.user.trim()) { if (!formData.user.trim()) {
newErrors.user = 'Username is required'; newErrors.user = "Username is required";
} }
// Validate SSH port // Validate SSH port
if (formData.ssh_port !== undefined && (formData.ssh_port < 1 || formData.ssh_port > 65535)) { if (
newErrors.ssh_port = 'SSH port must be between 1 and 65535'; formData.ssh_port !== undefined &&
(formData.ssh_port < 1 || formData.ssh_port > 65535)
) {
newErrors.ssh_port = "SSH port must be between 1 and 65535";
} }
// Validate authentication based on auth_type // Validate authentication based on auth_type
const authType = formData.auth_type ?? 'password'; const authType = formData.auth_type ?? "password";
if (authType === 'password') { if (authType === "password") {
if (!formData.password?.trim()) { if (!formData.password?.trim()) {
newErrors.password = 'Password is required for password authentication'; newErrors.password = "Password is required for password authentication";
}
}
if (authType === 'key') {
if (!formData.ssh_key?.trim()) {
newErrors.ssh_key = 'SSH key is required for key authentication';
} }
} }
if (authType === "key") {
if (!formData.ssh_key?.trim()) {
newErrors.ssh_key = "SSH key is required for key authentication";
}
}
setErrors(newErrors); setErrors(newErrors);
return Object.keys(newErrors).length === 0 && !sshKeyError; return Object.keys(newErrors).length === 0 && !sshKeyError;
@@ -158,348 +172,411 @@ export function ServerForm({ onSubmit, initialData, isEditing = false, onCancel
if (validateForm()) { if (validateForm()) {
onSubmit(formData); onSubmit(formData);
if (!isEditing) { if (!isEditing) {
setFormData({ setFormData({
name: '', name: "",
ip: '', ip: "",
user: '', user: "",
password: '', password: "",
auth_type: 'password', auth_type: "password",
ssh_key: '', ssh_key: "",
ssh_key_passphrase: '', ssh_key_passphrase: "",
ssh_port: 22, ssh_port: 22,
color: '#3b82f6' color: "#3b82f6",
}); });
} }
} }
}; };
const handleChange = (field: keyof CreateServerData) => ( const handleChange =
e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement> (field: keyof CreateServerData) =>
) => { (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => {
// Special handling for numeric ssh_port: keep it strictly numeric // Special handling for numeric ssh_port: keep it strictly numeric
if (field === 'ssh_port') { if (field === "ssh_port") {
const raw = (e.target as HTMLInputElement).value ?? ''; const raw = (e.target as HTMLInputElement).value ?? "";
const digitsOnly = raw.replace(/\D+/g, ''); const digitsOnly = raw.replace(/\D+/g, "");
setFormData(prev => ({ setFormData((prev) => ({
...prev, ...prev,
ssh_port: digitsOnly ? parseInt(digitsOnly, 10) : undefined, ssh_port: digitsOnly ? parseInt(digitsOnly, 10) : undefined,
})); }));
if (errors.ssh_port) { if (errors.ssh_port) {
setErrors(prev => ({ ...prev, ssh_port: undefined })); setErrors((prev) => ({ ...prev, ssh_port: undefined }));
}
return;
} }
return;
}
setFormData(prev => ({ ...prev, [field]: (e.target as HTMLInputElement).value })); setFormData((prev) => ({
// Clear error when user starts typing ...prev,
if (errors[field]) { [field]: (e.target as HTMLInputElement).value,
setErrors(prev => ({ ...prev, [field]: undefined })); }));
} // Clear error when user starts typing
if (errors[field]) {
// Reset generated key state when switching auth types setErrors((prev) => ({ ...prev, [field]: undefined }));
if (field === 'auth_type') { }
setIsGeneratedKey(false);
setGeneratedPublicKey(''); // Reset generated key state when switching auth types
} if (field === "auth_type") {
}; setIsGeneratedKey(false);
setGeneratedPublicKey("");
}
};
const handleGenerateKeyPair = async () => { const handleGenerateKeyPair = async () => {
setIsGeneratingKey(true); setIsGeneratingKey(true);
try { try {
const response = await fetch('/api/servers/generate-keypair', { const response = await fetch("/api/servers/generate-keypair", {
method: 'POST', method: "POST",
headers: { headers: {
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
}); });
if (!response.ok) { if (!response.ok) {
throw new Error('Failed to generate key pair'); throw new Error("Failed to generate key pair");
} }
const data = await response.json() as { success: boolean; privateKey?: string; publicKey?: string; serverId?: number; error?: string }; const data = (await response.json()) as {
success: boolean;
privateKey?: string;
publicKey?: string;
serverId?: number;
error?: string;
};
if (data.success) { if (data.success) {
const serverId = data.serverId ?? 0; const serverId = data.serverId ?? 0;
const keyPath = `data/ssh-keys/server_${serverId}_key`; const keyPath = `data/ssh-keys/server_${serverId}_key`;
setFormData(prev => ({ setFormData((prev) => ({
...prev, ...prev,
ssh_key: data.privateKey ?? '', ssh_key: data.privateKey ?? "",
ssh_key_path: keyPath, ssh_key_path: keyPath,
key_generated: true key_generated: true,
})); }));
setGeneratedPublicKey(data.publicKey ?? ''); setGeneratedPublicKey(data.publicKey ?? "");
setGeneratedServerId(serverId); setGeneratedServerId(serverId);
setIsGeneratedKey(true); setIsGeneratedKey(true);
setShowPublicKeyModal(true); setShowPublicKeyModal(true);
setSshKeyError(''); setSshKeyError("");
} else { } else {
throw new Error(data.error ?? 'Failed to generate key pair'); throw new Error(data.error ?? "Failed to generate key pair");
} }
} catch (error) { } catch (error) {
console.error('Error generating key pair:', error); console.error("Error generating key pair:", error);
setSshKeyError(error instanceof Error ? error.message : 'Failed to generate key pair'); setSshKeyError(
error instanceof Error ? error.message : "Failed to generate key pair",
);
} finally { } finally {
setIsGeneratingKey(false); setIsGeneratingKey(false);
} }
}; };
const handleSSHKeyChange = (value: string) => { const handleSSHKeyChange = (value: string) => {
setFormData(prev => ({ ...prev, ssh_key: value })); setFormData((prev) => ({ ...prev, ssh_key: value }));
if (errors.ssh_key) { if (errors.ssh_key) {
setErrors(prev => ({ ...prev, ssh_key: undefined })); setErrors((prev) => ({ ...prev, ssh_key: undefined }));
} }
}; };
return ( return (
<> <>
<form onSubmit={handleSubmit} className="space-y-6"> <form onSubmit={handleSubmit} className="space-y-6">
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4"> <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
<div>
<label htmlFor="name" className="block text-sm font-medium text-muted-foreground mb-1">
Server Name *
</label>
<input
type="text"
id="name"
value={formData.name}
onChange={handleChange('name')}
className={`w-full px-3 py-2 border rounded-md shadow-sm bg-card text-foreground placeholder-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:border-ring ${
errors.name ? 'border-destructive' : 'border-border'
}`}
placeholder="e.g., Production Server"
/>
{errors.name && <p className="mt-1 text-sm text-destructive">{errors.name}</p>}
</div>
<div>
<label htmlFor="ip" className="block text-sm font-medium text-muted-foreground mb-1">
Host/IP Address *
</label>
<input
type="text"
id="ip"
value={formData.ip}
onChange={handleChange('ip')}
className={`w-full px-3 py-2 border rounded-md shadow-sm bg-card text-foreground placeholder-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:border-ring ${
errors.ip ? 'border-destructive' : 'border-border'
}`}
placeholder="e.g., 192.168.1.100, server.example.com, 2001:db8::1, or fe80::...%eth0"
/>
{errors.ip && <p className="mt-1 text-sm text-destructive">{errors.ip}</p>}
</div>
<div>
<label htmlFor="user" className="block text-sm font-medium text-muted-foreground mb-1">
Username *
</label>
<input
type="text"
id="user"
value={formData.user}
onChange={handleChange('user')}
className={`w-full px-3 py-2 border rounded-md shadow-sm bg-card text-foreground placeholder-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:border-ring ${
errors.user ? 'border-destructive' : 'border-border'
}`}
placeholder="e.g., root"
/>
{errors.user && <p className="mt-1 text-sm text-destructive">{errors.user}</p>}
</div>
<div>
<label htmlFor="ssh_port" className="block text-sm font-medium text-muted-foreground mb-1">
SSH Port
</label>
<input
type="number"
id="ssh_port"
inputMode="numeric"
pattern="[0-9]*"
autoComplete="off"
value={formData.ssh_port ?? 22}
onChange={handleChange('ssh_port')}
className={`w-full px-3 py-2 border rounded-md shadow-sm bg-card text-foreground placeholder-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:border-ring ${
errors.ssh_port ? 'border-destructive' : 'border-border'
}`}
placeholder="22"
min={1}
max={65535}
/>
{errors.ssh_port && <p className="mt-1 text-sm text-destructive">{errors.ssh_port}</p>}
</div>
<div>
<label htmlFor="auth_type" className="block text-sm font-medium text-muted-foreground mb-1">
Authentication Type *
</label>
<select
id="auth_type"
value={formData.auth_type ?? 'password'}
onChange={handleChange('auth_type')}
className="w-full px-3 py-2 border rounded-md shadow-sm bg-card text-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:border-ring border-border"
>
<option value="password">Password Only</option>
<option value="key">SSH Key Only</option>
</select>
</div>
{colorCodingEnabled && (
<div> <div>
<label htmlFor="color" className="block text-sm font-medium text-muted-foreground mb-1"> <label
Server Color htmlFor="name"
className="text-muted-foreground mb-1 block text-sm font-medium"
>
Server Name *
</label> </label>
<div className="flex items-center gap-3"> <input
<input type="text"
type="color" id="name"
id="color" value={formData.name}
value={formData.color ?? '#3b82f6'} onChange={handleChange("name")}
onChange={handleChange('color')} className={`bg-card text-foreground placeholder-muted-foreground focus:ring-ring focus:border-ring w-full rounded-md border px-3 py-2 shadow-sm focus:ring-2 focus:outline-none ${
className="w-20 h-10 rounded cursor-pointer border border-border" errors.name ? "border-destructive" : "border-border"
/> }`}
<span className="text-sm text-muted-foreground"> placeholder="e.g., Production Server"
Choose a color to identify this server />
</span> {errors.name && (
</div> <p className="text-destructive mt-1 text-sm">{errors.name}</p>
)}
</div> </div>
)}
</div>
{/* Password Authentication */}
{formData.auth_type === 'password' && (
<div>
<label htmlFor="password" className="block text-sm font-medium text-muted-foreground mb-1">
Password *
</label>
<input
type="password"
id="password"
value={formData.password ?? ''}
onChange={handleChange('password')}
className={`w-full px-3 py-2 border rounded-md shadow-sm bg-card text-foreground placeholder-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:border-ring ${
errors.password ? 'border-destructive' : 'border-border'
}`}
placeholder="Enter password"
/>
{errors.password && <p className="mt-1 text-sm text-destructive">{errors.password}</p>}
</div>
)}
{/* SSH Key Authentication */}
{formData.auth_type === 'key' && (
<div className="space-y-4">
<div> <div>
<div className="flex items-center justify-between mb-1"> <label
<label className="block text-sm font-medium text-muted-foreground"> htmlFor="ip"
SSH Private Key * className="text-muted-foreground mb-1 block text-sm font-medium"
</label> >
<Button Host/IP Address *
type="button" </label>
variant="outline" <input
size="sm" type="text"
onClick={handleGenerateKeyPair} id="ip"
disabled={isGeneratingKey} value={formData.ip}
className="gap-2" onChange={handleChange("ip")}
className={`bg-card text-foreground placeholder-muted-foreground focus:ring-ring focus:border-ring w-full rounded-md border px-3 py-2 shadow-sm focus:ring-2 focus:outline-none ${
errors.ip ? "border-destructive" : "border-border"
}`}
placeholder="e.g., 192.168.1.100, server.example.com, 2001:db8::1, or fe80::...%eth0"
/>
{errors.ip && (
<p className="text-destructive mt-1 text-sm">{errors.ip}</p>
)}
</div>
<div>
<label
htmlFor="user"
className="text-muted-foreground mb-1 block text-sm font-medium"
>
Username *
</label>
<input
type="text"
id="user"
value={formData.user}
onChange={handleChange("user")}
className={`bg-card text-foreground placeholder-muted-foreground focus:ring-ring focus:border-ring w-full rounded-md border px-3 py-2 shadow-sm focus:ring-2 focus:outline-none ${
errors.user ? "border-destructive" : "border-border"
}`}
placeholder="e.g., root"
/>
{errors.user && (
<p className="text-destructive mt-1 text-sm">{errors.user}</p>
)}
</div>
<div>
<label
htmlFor="ssh_port"
className="text-muted-foreground mb-1 block text-sm font-medium"
>
SSH Port
</label>
<input
type="number"
id="ssh_port"
inputMode="numeric"
pattern="[0-9]*"
autoComplete="off"
value={formData.ssh_port ?? 22}
onChange={handleChange("ssh_port")}
className={`bg-card text-foreground placeholder-muted-foreground focus:ring-ring focus:border-ring w-full rounded-md border px-3 py-2 shadow-sm focus:ring-2 focus:outline-none ${
errors.ssh_port ? "border-destructive" : "border-border"
}`}
placeholder="22"
min={1}
max={65535}
/>
{errors.ssh_port && (
<p className="text-destructive mt-1 text-sm">{errors.ssh_port}</p>
)}
</div>
<div>
<label
htmlFor="auth_type"
className="text-muted-foreground mb-1 block text-sm font-medium"
>
Authentication Type *
</label>
<select
id="auth_type"
value={formData.auth_type ?? "password"}
onChange={handleChange("auth_type")}
className="bg-card text-foreground focus:ring-ring focus:border-ring border-border w-full rounded-md border px-3 py-2 shadow-sm focus:ring-2 focus:outline-none"
>
<option value="password">Password Only</option>
<option value="key">SSH Key Only</option>
</select>
</div>
{colorCodingEnabled && (
<div>
<label
htmlFor="color"
className="text-muted-foreground mb-1 block text-sm font-medium"
> >
<Key className="h-4 w-4" /> Server Color
{isGeneratingKey ? 'Generating...' : 'Generate Key Pair'} </label>
</Button> <div className="flex items-center gap-3">
</div> <input
type="color"
{/* Show manual key input only if no key has been generated */} id="color"
{!formData.key_generated && ( value={formData.color ?? "#3b82f6"}
<> onChange={handleChange("color")}
<SSHKeyInput className="border-border h-10 w-20 cursor-pointer rounded border"
value={formData.ssh_key ?? ''}
onChange={handleSSHKeyChange}
onError={setSshKeyError}
/> />
{errors.ssh_key && <p className="mt-1 text-sm text-destructive">{errors.ssh_key}</p>} <span className="text-muted-foreground text-sm">
{sshKeyError && <p className="mt-1 text-sm text-destructive">{sshKeyError}</p>} Choose a color to identify this server
</> </span>
)}
{/* Show generated key status */}
{formData.key_generated && (
<div className="p-3 bg-success/10 border border-success/20 rounded-md">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<svg className="w-4 h-4 text-success" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
</svg>
<span className="text-sm font-medium text-success-foreground">
SSH key pair generated successfully
</span>
</div>
<Button
type="button"
variant="outline"
size="sm"
onClick={() => setShowPublicKeyModal(true)}
className="gap-2 border-info/20 text-info bg-info/10 hover:bg-info/20"
>
<Key className="h-4 w-4" />
View Public Key
</Button>
</div>
<p className="text-xs text-success/80 mt-1">
The private key has been generated and will be saved with the server.
</p>
</div> </div>
)} </div>
</div> )}
</div>
{/* Password Authentication */}
{formData.auth_type === "password" && (
<div> <div>
<label htmlFor="ssh_key_passphrase" className="block text-sm font-medium text-muted-foreground mb-1"> <label
SSH Key Passphrase (Optional) htmlFor="password"
className="text-muted-foreground mb-1 block text-sm font-medium"
>
Password *
</label> </label>
<input <input
type="password" type="password"
id="ssh_key_passphrase" id="password"
value={formData.ssh_key_passphrase ?? ''} value={formData.password ?? ""}
onChange={handleChange('ssh_key_passphrase')} onChange={handleChange("password")}
className="w-full px-3 py-2 border rounded-md shadow-sm bg-card text-foreground placeholder-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:border-ring border-border" className={`bg-card text-foreground placeholder-muted-foreground focus:ring-ring focus:border-ring w-full rounded-md border px-3 py-2 shadow-sm focus:ring-2 focus:outline-none ${
placeholder="Enter passphrase for encrypted key" errors.password ? "border-destructive" : "border-border"
}`}
placeholder="Enter password"
/> />
<p className="mt-1 text-xs text-muted-foreground"> {errors.password && (
Only required if your SSH key is encrypted with a passphrase <p className="text-destructive mt-1 text-sm">{errors.password}</p>
</p> )}
</div> </div>
</div>
)}
<div className="flex flex-col sm:flex-row justify-end space-y-2 sm:space-y-0 sm:space-x-3 pt-4">
{isEditing && onCancel && (
<Button
type="button"
onClick={onCancel}
variant="outline"
size="default"
className="w-full sm:w-auto order-2 sm:order-1"
>
Cancel
</Button>
)} )}
<Button
type="submit" {/* SSH Key Authentication */}
variant="default" {formData.auth_type === "key" && (
size="default" <div className="space-y-4">
className="w-full sm:w-auto order-1 sm:order-2" <div>
> <div className="mb-1 flex items-center justify-between">
{isEditing ? 'Update Server' : 'Add Server'} <label className="text-muted-foreground block text-sm font-medium">
</Button> SSH Private Key *
</div> </label>
</form> <Button
type="button"
{/* Public Key Modal */} variant="outline"
<PublicKeyModal size="sm"
isOpen={showPublicKeyModal} onClick={handleGenerateKeyPair}
onClose={() => setShowPublicKeyModal(false)} disabled={isGeneratingKey}
publicKey={generatedPublicKey} className="gap-2"
serverName={formData.name || 'New Server'} >
serverIp={formData.ip} <Key className="h-4 w-4" />
/> {isGeneratingKey ? "Generating..." : "Generate Key Pair"}
</Button>
</div>
{/* Show manual key input only if no key has been generated */}
{!formData.key_generated && (
<>
<SSHKeyInput
value={formData.ssh_key ?? ""}
onChange={handleSSHKeyChange}
onError={setSshKeyError}
/>
{errors.ssh_key && (
<p className="text-destructive mt-1 text-sm">
{errors.ssh_key}
</p>
)}
{sshKeyError && (
<p className="text-destructive mt-1 text-sm">
{sshKeyError}
</p>
)}
</>
)}
{/* Show generated key status */}
{formData.key_generated && (
<div className="bg-success/10 border-success/20 rounded-md border p-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<svg
className="text-success h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M5 13l4 4L19 7"
/>
</svg>
<span className="text-success-foreground text-sm font-medium">
SSH key pair generated successfully
</span>
</div>
<Button
type="button"
variant="outline"
size="sm"
onClick={() => setShowPublicKeyModal(true)}
className="border-info/20 text-info bg-info/10 hover:bg-info/20 gap-2"
>
<Key className="h-4 w-4" />
View Public Key
</Button>
</div>
<p className="text-success/80 mt-1 text-xs">
The private key has been generated and will be saved with
the server.
</p>
</div>
)}
</div>
<div>
<label
htmlFor="ssh_key_passphrase"
className="text-muted-foreground mb-1 block text-sm font-medium"
>
SSH Key Passphrase (Optional)
</label>
<input
type="password"
id="ssh_key_passphrase"
value={formData.ssh_key_passphrase ?? ""}
onChange={handleChange("ssh_key_passphrase")}
className="bg-card text-foreground placeholder-muted-foreground focus:ring-ring focus:border-ring border-border w-full rounded-md border px-3 py-2 shadow-sm focus:ring-2 focus:outline-none"
placeholder="Enter passphrase for encrypted key"
/>
<p className="text-muted-foreground mt-1 text-xs">
Only required if your SSH key is encrypted with a passphrase
</p>
</div>
</div>
)}
<div className="flex flex-col justify-end space-y-2 pt-4 sm:flex-row sm:space-y-0 sm:space-x-3">
{isEditing && onCancel && (
<Button
type="button"
onClick={onCancel}
variant="outline"
size="default"
className="order-2 w-full sm:order-1 sm:w-auto"
>
Cancel
</Button>
)}
<Button
type="submit"
variant="default"
size="default"
className="order-1 w-full sm:order-2 sm:w-auto"
>
{isEditing ? "Update Server" : "Add Server"}
</Button>
</div>
</form>
{/* Public Key Modal */}
<PublicKeyModal
isOpen={showPublicKeyModal}
onClose={() => setShowPublicKeyModal(false)}
publicKey={generatedPublicKey}
serverName={formData.name || "New Server"}
serverIp={formData.ip}
/>
</> </>
); );
} }

View File

@@ -6,7 +6,8 @@ import { ServerForm } from './ServerForm';
import { Button } from './ui/button'; import { Button } from './ui/button';
import { ConfirmationModal } from './ConfirmationModal'; import { ConfirmationModal } from './ConfirmationModal';
import { PublicKeyModal } from './PublicKeyModal'; import { PublicKeyModal } from './PublicKeyModal';
import { Key } from 'lucide-react'; import { ServerStoragesModal } from './ServerStoragesModal';
import { Key, Database } from 'lucide-react';
interface ServerListProps { interface ServerListProps {
servers: Server[]; servers: Server[];
@@ -32,6 +33,8 @@ export function ServerList({ servers, onUpdate, onDelete }: ServerListProps) {
serverName: string; serverName: string;
serverIp: string; serverIp: string;
} | null>(null); } | null>(null);
const [showStoragesModal, setShowStoragesModal] = useState(false);
const [selectedServerForStorages, setSelectedServerForStorages] = useState<{ id: number; name: string } | null>(null);
const handleEdit = (server: Server) => { const handleEdit = (server: Server) => {
setEditingId(server.id); setEditingId(server.id);
@@ -251,6 +254,19 @@ export function ServerList({ servers, onUpdate, onDelete }: ServerListProps) {
</> </>
)} )}
</Button> </Button>
<Button
onClick={() => {
setSelectedServerForStorages({ id: server.id, name: server.name });
setShowStoragesModal(true);
}}
variant="outline"
size="sm"
className="w-full sm:w-auto border-info/20 text-info bg-info/10 hover:bg-info/20"
>
<Database className="w-4 h-4 mr-1" />
<span className="hidden sm:inline">View Storages</span>
<span className="sm:hidden">Storages</span>
</Button>
<div className="flex space-x-2"> <div className="flex space-x-2">
{/* View Public Key button - only show for generated keys */} {/* View Public Key button - only show for generated keys */}
{server.key_generated === true && ( {server.key_generated === true && (
@@ -324,6 +340,19 @@ export function ServerList({ servers, onUpdate, onDelete }: ServerListProps) {
serverIp={publicKeyData.serverIp} serverIp={publicKeyData.serverIp}
/> />
)} )}
{/* Server Storages Modal */}
{selectedServerForStorages && (
<ServerStoragesModal
isOpen={showStoragesModal}
onClose={() => {
setShowStoragesModal(false);
setSelectedServerForStorages(null);
}}
serverId={selectedServerForStorages.id}
serverName={selectedServerForStorages.name}
/>
)}
</div> </div>
); );
} }

View File

@@ -0,0 +1,273 @@
"use client";
import { useState } from "react";
import { Button } from "./ui/button";
import {
Database,
RefreshCw,
CheckCircle,
Lock,
AlertCircle,
} from "lucide-react";
import { useRegisterModal } from "./modal/ModalStackProvider";
import { api } from "~/trpc/react";
import { PBSCredentialsModal } from "./PBSCredentialsModal";
import type { Storage } from "~/server/services/storageService";
interface ServerStoragesModalProps {
isOpen: boolean;
onClose: () => void;
serverId: number;
serverName: string;
}
export function ServerStoragesModal({
isOpen,
onClose,
serverId,
serverName,
}: ServerStoragesModalProps) {
const [forceRefresh, setForceRefresh] = useState(false);
const [selectedPBSStorage, setSelectedPBSStorage] = useState<Storage | null>(
null,
);
const { data, isLoading, refetch } =
api.installedScripts.getBackupStorages.useQuery(
{ serverId, forceRefresh },
{ enabled: isOpen },
);
// Fetch all PBS credentials for this server to show status indicators
const { data: allCredentials } =
api.pbsCredentials.getAllCredentialsForServer.useQuery(
{ serverId },
{ enabled: isOpen },
);
const credentialsMap = new Map<string, boolean>();
if (allCredentials?.success) {
allCredentials.credentials.forEach((c: { storage_name: string }) => {
credentialsMap.set(String(c.storage_name), true);
});
}
useRegisterModal(isOpen, {
id: "server-storages-modal",
allowEscape: true,
onClose,
});
const handleRefresh = () => {
setForceRefresh(true);
void refetch();
setTimeout(() => setForceRefresh(false), 1000);
};
if (!isOpen) return null;
const storages = data?.success ? data.storages : [];
const backupStorages = storages.filter((s) => s.supportsBackup);
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4 backdrop-blur-sm">
<div className="bg-card border-border flex max-h-[90vh] w-full max-w-3xl flex-col rounded-lg border shadow-xl">
{/* Header */}
<div className="border-border flex items-center justify-between border-b p-6">
<div className="flex items-center gap-3">
<Database className="text-primary h-6 w-6" />
<h2 className="text-card-foreground text-2xl font-bold">
Storages for {serverName}
</h2>
</div>
<div className="flex items-center gap-2">
<Button
onClick={handleRefresh}
variant="outline"
size="sm"
disabled={isLoading}
>
<RefreshCw
className={`mr-2 h-4 w-4 ${isLoading ? "animate-spin" : ""}`}
/>
Refresh
</Button>
<Button
onClick={onClose}
variant="ghost"
size="icon"
className="text-muted-foreground hover:text-foreground"
>
<svg
className="h-5 w-5"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</Button>
</div>
</div>
{/* Content */}
<div className="flex-1 overflow-y-auto p-6">
{isLoading ? (
<div className="py-8 text-center">
<div className="border-primary mb-4 inline-block h-8 w-8 animate-spin rounded-full border-b-2"></div>
<p className="text-muted-foreground">Loading storages...</p>
</div>
) : !data?.success ? (
<div className="py-8 text-center">
<Database className="text-muted-foreground mx-auto mb-4 h-12 w-12" />
<p className="text-foreground mb-2">Failed to load storages</p>
<p className="text-muted-foreground mb-4 text-sm">
{data?.error ?? "Unknown error occurred"}
</p>
<Button onClick={handleRefresh} variant="outline" size="sm">
<RefreshCw className="mr-2 h-4 w-4" />
Try Again
</Button>
</div>
) : storages.length === 0 ? (
<div className="py-8 text-center">
<Database className="text-muted-foreground mx-auto mb-4 h-12 w-12" />
<p className="text-foreground mb-2">No storages found</p>
<p className="text-muted-foreground text-sm">
Make sure your server has storages configured.
</p>
</div>
) : (
<>
{data.cached && (
<div className="bg-muted/50 text-muted-foreground mb-4 rounded-lg p-3 text-sm">
Showing cached data. Click Refresh to fetch latest from
server.
</div>
)}
<div className="space-y-3">
{storages.map((storage) => {
const isBackupCapable = storage.supportsBackup;
return (
<div
key={storage.name}
className={`rounded-lg border p-4 ${
isBackupCapable
? "border-success/50 bg-success/5"
: "border-border bg-card"
}`}
>
<div className="flex-1">
<div className="mb-2 flex flex-wrap items-center gap-2">
<h3 className="text-foreground font-medium">
{storage.name}
</h3>
{isBackupCapable && (
<span className="bg-success/20 text-success border-success/30 flex items-center gap-1 rounded border px-2 py-0.5 text-xs font-medium">
<CheckCircle className="h-3 w-3" />
Backup
</span>
)}
<span className="bg-muted text-muted-foreground rounded px-2 py-0.5 text-xs font-medium">
{storage.type}
</span>
{storage.type === "pbs" &&
(credentialsMap.has(storage.name) ? (
<span className="bg-success/20 text-success border-success/30 flex items-center gap-1 rounded border px-2 py-0.5 text-xs font-medium">
<CheckCircle className="h-3 w-3" />
Credentials Configured
</span>
) : (
<span className="bg-warning/20 text-warning border-warning/30 flex items-center gap-1 rounded border px-2 py-0.5 text-xs font-medium">
<AlertCircle className="h-3 w-3" />
Credentials Needed
</span>
))}
</div>
<div className="text-muted-foreground space-y-1 text-sm">
<div>
<span className="font-medium">Content:</span>{" "}
{storage.content.join(", ")}
</div>
{storage.nodes && storage.nodes.length > 0 && (
<div>
<span className="font-medium">Nodes:</span>{" "}
{storage.nodes.join(", ")}
</div>
)}
{Object.entries(storage)
.filter(
([key]) =>
![
"name",
"type",
"content",
"supportsBackup",
"nodes",
].includes(key),
)
.map(([key, value]) => (
<div key={key}>
<span className="font-medium capitalize">
{key.replace(/_/g, " ")}:
</span>{" "}
{String(value)}
</div>
))}
</div>
{storage.type === "pbs" && (
<div className="border-border mt-3 border-t pt-3">
<Button
onClick={() => setSelectedPBSStorage(storage)}
variant="outline"
size="sm"
className="flex items-center gap-2"
>
<Lock className="h-4 w-4" />
{credentialsMap.has(storage.name)
? "Edit"
: "Configure"}{" "}
Credentials
</Button>
</div>
)}
</div>
</div>
);
})}
</div>
{backupStorages.length > 0 && (
<div className="bg-success/10 border-success/20 mt-6 rounded-lg border p-4">
<p className="text-success text-sm font-medium">
{backupStorages.length} storage
{backupStorages.length !== 1 ? "s" : ""} available for
backups
</p>
</div>
)}
</>
)}
</div>
</div>
{/* PBS Credentials Modal */}
{selectedPBSStorage && (
<PBSCredentialsModal
isOpen={!!selectedPBSStorage}
onClose={() => setSelectedPBSStorage(null)}
serverId={serverId}
serverName={serverName}
storage={selectedPBSStorage}
/>
)}
</div>
);
}

View File

@@ -0,0 +1,178 @@
'use client';
import { useState } from 'react';
import { Button } from './ui/button';
import { Database, RefreshCw, CheckCircle } from 'lucide-react';
import { useRegisterModal } from './modal/ModalStackProvider';
import type { Storage } from '~/server/services/storageService';
interface StorageSelectionModalProps {
isOpen: boolean;
onClose: () => void;
onSelect: (storage: Storage) => void;
storages: Storage[];
isLoading: boolean;
onRefresh: () => void;
title?: string;
description?: string;
filterFn?: (storage: Storage) => boolean;
showBackupTag?: boolean;
}
export function StorageSelectionModal({
isOpen,
onClose,
onSelect,
storages,
isLoading,
onRefresh,
title = 'Select Storage',
description = 'Select a storage to use.',
filterFn,
showBackupTag = true
}: StorageSelectionModalProps) {
const [selectedStorage, setSelectedStorage] = useState<Storage | null>(null);
useRegisterModal(isOpen, { id: 'storage-selection-modal', allowEscape: true, onClose });
if (!isOpen) return null;
const handleSelect = () => {
if (selectedStorage) {
onSelect(selectedStorage);
setSelectedStorage(null);
}
};
const handleClose = () => {
setSelectedStorage(null);
onClose();
};
// Filter storages using filterFn if provided, otherwise filter to show only backup-capable storages
const filteredStorages = filterFn ? storages.filter(filterFn) : storages.filter(s => s.supportsBackup);
return (
<div className="fixed inset-0 backdrop-blur-sm bg-black/50 flex items-center justify-center z-50 p-4">
<div className="bg-card rounded-lg shadow-xl max-w-2xl w-full border border-border">
{/* Header */}
<div className="flex items-center justify-between p-6 border-b border-border">
<div className="flex items-center gap-3">
<Database className="h-6 w-6 text-primary" />
<h2 className="text-2xl font-bold text-card-foreground">{title}</h2>
</div>
<Button
onClick={handleClose}
variant="ghost"
size="icon"
className="text-muted-foreground hover:text-foreground"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</Button>
</div>
{/* Content */}
<div className="p-6">
{isLoading ? (
<div className="text-center py-8">
<div className="inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-primary mb-4"></div>
<p className="text-muted-foreground">Loading storages...</p>
</div>
) : filteredStorages.length === 0 ? (
<div className="text-center py-8">
<Database className="h-12 w-12 text-muted-foreground mx-auto mb-4" />
<p className="text-foreground mb-2">No backup-capable storages found</p>
<p className="text-sm text-muted-foreground mb-4">
Make sure your server has storages configured with backup content type.
</p>
<Button onClick={onRefresh} variant="outline" size="sm">
<RefreshCw className="h-4 w-4 mr-2" />
Refresh Storages
</Button>
</div>
) : (
<>
<p className="text-sm text-muted-foreground mb-4">
{description}
</p>
{/* Storage List */}
<div className="space-y-2 max-h-96 overflow-y-auto mb-4">
{filteredStorages.map((storage) => (
<div
key={storage.name}
onClick={() => setSelectedStorage(storage)}
className={`p-4 border rounded-lg cursor-pointer transition-all ${
selectedStorage?.name === storage.name
? 'border-primary bg-primary/10'
: 'border-border hover:border-primary/50 hover:bg-accent/50'
}`}
>
<div className="flex items-center justify-between">
<div className="flex-1">
<div className="flex items-center gap-2 mb-1">
<h3 className="font-medium text-foreground">{storage.name}</h3>
{showBackupTag && (
<span className="px-2 py-0.5 text-xs font-medium rounded bg-success/20 text-success border border-success/30">
Backup
</span>
)}
<span className="px-2 py-0.5 text-xs font-medium rounded bg-muted text-muted-foreground">
{storage.type}
</span>
</div>
<div className="text-sm text-muted-foreground">
<span>Content: {storage.content.join(', ')}</span>
{storage.nodes && storage.nodes.length > 0 && (
<span className="ml-2"> Nodes: {storage.nodes.join(', ')}</span>
)}
</div>
</div>
{selectedStorage?.name === storage.name && (
<CheckCircle className="h-5 w-5 text-primary flex-shrink-0 ml-2" />
)}
</div>
</div>
))}
</div>
{/* Refresh Button */}
<div className="flex justify-end mb-4">
<Button onClick={onRefresh} variant="outline" size="sm">
<RefreshCw className="h-4 w-4 mr-2" />
Fetch Storages
</Button>
</div>
</>
)}
{/* Action Buttons */}
<div className="flex flex-col sm:flex-row justify-end gap-3">
<Button
onClick={handleClose}
variant="outline"
size="default"
className="w-full sm:w-auto"
>
Cancel
</Button>
<Button
onClick={handleSelect}
disabled={!selectedStorage}
variant="default"
size="default"
className="w-full sm:w-auto"
>
Select Storage
</Button>
</div>
</div>
</div>
</div>
);
}

View File

@@ -12,7 +12,15 @@ interface TerminalProps {
server?: any; server?: any;
isUpdate?: boolean; isUpdate?: boolean;
isShell?: boolean; isShell?: boolean;
isBackup?: boolean;
isClone?: boolean;
containerId?: string; containerId?: string;
storage?: string;
backupStorage?: string;
executionId?: string;
cloneCount?: number;
hostnames?: string[];
containerType?: 'lxc' | 'vm';
} }
interface TerminalMessage { interface TerminalMessage {
@@ -21,7 +29,7 @@ interface TerminalMessage {
timestamp: number; timestamp: number;
} }
export function Terminal({ scriptPath, onClose, mode = 'local', server, isUpdate = false, isShell = false, containerId }: TerminalProps) { export function Terminal({ scriptPath, onClose, mode = 'local', server, isUpdate = false, isShell = false, isBackup = false, isClone = false, containerId, storage, backupStorage, executionId: propExecutionId, cloneCount, hostnames, containerType }: TerminalProps) {
const [isConnected, setIsConnected] = useState(false); const [isConnected, setIsConnected] = useState(false);
const [isRunning, setIsRunning] = useState(false); const [isRunning, setIsRunning] = useState(false);
const [isClient, setIsClient] = useState(false); const [isClient, setIsClient] = useState(false);
@@ -36,7 +44,16 @@ export function Terminal({ scriptPath, onClose, mode = 'local', server, isUpdate
const fitAddonRef = useRef<any>(null); const fitAddonRef = useRef<any>(null);
const wsRef = useRef<WebSocket | null>(null); const wsRef = useRef<WebSocket | null>(null);
const inputHandlerRef = useRef<((data: string) => void) | null>(null); const inputHandlerRef = useRef<((data: string) => void) | null>(null);
const [executionId, setExecutionId] = useState(() => `exec_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`); const [executionId, setExecutionId] = useState(() => propExecutionId ?? `exec_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`);
// Update executionId when propExecutionId changes
useEffect(() => {
if (propExecutionId) {
setExecutionId(propExecutionId);
}
}, [propExecutionId]);
const effectiveExecutionId = propExecutionId ?? executionId;
const isConnectingRef = useRef<boolean>(false); const isConnectingRef = useRef<boolean>(false);
const hasConnectedRef = useRef<boolean>(false); const hasConnectedRef = useRef<boolean>(false);
@@ -274,7 +291,7 @@ export function Terminal({ scriptPath, onClose, mode = 'local', server, isUpdate
if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) { if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) {
const message = { const message = {
action: 'input', action: 'input',
executionId, executionId: effectiveExecutionId,
input: data input: data
}; };
wsRef.current.send(JSON.stringify(message)); wsRef.current.send(JSON.stringify(message));
@@ -322,9 +339,11 @@ export function Terminal({ scriptPath, onClose, mode = 'local', server, isUpdate
// Only auto-start on initial connection, not on reconnections // Only auto-start on initial connection, not on reconnections
if (isInitialConnection && !isRunning) { if (isInitialConnection && !isRunning) {
// Generate a new execution ID for the initial run // Use propExecutionId if provided, otherwise generate a new one
const newExecutionId = `exec_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; const newExecutionId = propExecutionId ?? `exec_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
setExecutionId(newExecutionId); if (!propExecutionId) {
setExecutionId(newExecutionId);
}
const message = { const message = {
action: 'start', action: 'start',
@@ -334,7 +353,14 @@ export function Terminal({ scriptPath, onClose, mode = 'local', server, isUpdate
server, server,
isUpdate, isUpdate,
isShell, isShell,
containerId isBackup,
isClone,
containerId,
storage,
backupStorage,
cloneCount,
hostnames,
containerType
}; };
ws.send(JSON.stringify(message)); ws.send(JSON.stringify(message));
} }
@@ -374,13 +400,15 @@ export function Terminal({ scriptPath, onClose, mode = 'local', server, isUpdate
wsRef.current.close(); wsRef.current.close();
} }
}; };
}, [scriptPath, mode, server, isUpdate, isShell, containerId, isMobile]); // eslint-disable-line react-hooks/exhaustive-deps }, [scriptPath, mode, server, isUpdate, isShell, containerId, isMobile]);
const startScript = () => { const startScript = () => {
if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN && !isRunning) { if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN && !isRunning) {
// Generate a new execution ID for each script run // Generate a new execution ID for each script run (unless propExecutionId is provided)
const newExecutionId = `exec_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; const newExecutionId = propExecutionId ?? `exec_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
setExecutionId(newExecutionId); if (!propExecutionId) {
setExecutionId(newExecutionId);
}
setIsStopped(false); setIsStopped(false);
wsRef.current.send(JSON.stringify({ wsRef.current.send(JSON.stringify({
@@ -391,7 +419,14 @@ export function Terminal({ scriptPath, onClose, mode = 'local', server, isUpdate
server, server,
isUpdate, isUpdate,
isShell, isShell,
containerId isBackup,
isClone,
containerId,
storage,
backupStorage,
cloneCount,
hostnames,
containerType
})); }));
} }
}; };

View File

@@ -1,10 +1,10 @@
'use client'; "use client";
import { useState, useEffect, useCallback } from 'react'; import { useState, useEffect, useCallback } from "react";
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { tomorrow } from 'react-syntax-highlighter/dist/esm/styles/prism'; import { tomorrow } from "react-syntax-highlighter/dist/esm/styles/prism";
import { Button } from './ui/button'; import { Button } from "./ui/button";
import type { Script } from '../../types/script'; import type { Script } from "../../types/script";
interface TextViewerProps { interface TextViewerProps {
scriptName: string; scriptName: string;
@@ -14,154 +14,161 @@ interface TextViewerProps {
} }
interface ScriptContent { interface ScriptContent {
ctScript?: string; mainScript?: string;
installScript?: string; installScript?: string;
alpineCtScript?: string; alpineMainScript?: string;
alpineInstallScript?: string; alpineInstallScript?: string;
} }
export function TextViewer({ scriptName, isOpen, onClose, script }: TextViewerProps) { export function TextViewer({
scriptName,
isOpen,
onClose,
script,
}: TextViewerProps) {
const [scriptContent, setScriptContent] = useState<ScriptContent>({}); const [scriptContent, setScriptContent] = useState<ScriptContent>({});
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [activeTab, setActiveTab] = useState<'ct' | 'install'>('ct'); const [activeTab, setActiveTab] = useState<"main" | "install">("main");
const [selectedVersion, setSelectedVersion] = useState<'default' | 'alpine'>('default'); const [selectedVersion, setSelectedVersion] = useState<"default" | "alpine">(
"default",
);
// Extract slug from script name (remove .sh extension) // Extract slug from script name (remove .sh extension)
const slug = scriptName.replace(/\.sh$/, '').replace(/^alpine-/, ''); const slug = scriptName.replace(/\.sh$/, "").replace(/^alpine-/, "");
// Check if alpine variant exists // Get default and alpine install methods
const hasAlpineVariant = script?.install_methods?.some( const defaultMethod = script?.install_methods?.find(
method => method.type === 'alpine' && method.script?.startsWith('ct/') (method) => method.type === "default",
); );
const alpineMethod = script?.install_methods?.find(
// Get script names for default and alpine versions (method) => method.type === "alpine",
const defaultScriptName = scriptName.replace(/^alpine-/, ''); );
const alpineScriptName = scriptName.startsWith('alpine-') ? scriptName : `alpine-${scriptName}`;
// Check if alpine variant exists
const hasAlpineVariant = !!alpineMethod;
// Get script paths from install_methods
const defaultScriptPath = defaultMethod?.script;
const alpineScriptPath = alpineMethod?.script;
// Determine if install scripts exist (only for ct/ scripts typically)
const hasInstallScript =
defaultScriptPath?.startsWith("ct/") ?? alpineScriptPath?.startsWith("ct/");
// Get script names for display
const defaultScriptName = scriptName.replace(/^alpine-/, "");
const loadScriptContent = useCallback(async () => { const loadScriptContent = useCallback(async () => {
setIsLoading(true); setIsLoading(true);
setError(null); setError(null);
try { try {
// Build fetch requests for default version // Build fetch requests based on actual script paths from install_methods
const requests: Promise<Response>[] = []; const requests: Promise<Response>[] = [];
const requestTypes: Array<
// Default CT script "default-main" | "default-install" | "alpine-main" | "alpine-install"
requests.push( > = [];
fetch(`/api/trpc/scripts.getScriptContent?input=${encodeURIComponent(JSON.stringify({ json: { path: `ct/${defaultScriptName}` } }))}`)
); // Default main script (ct/, vm/, tools/, etc.)
if (defaultScriptPath) {
// Tools, VM, VW scripts
requests.push(
fetch(`/api/trpc/scripts.getScriptContent?input=${encodeURIComponent(JSON.stringify({ json: { path: `tools/pve/${defaultScriptName}` } }))}`)
);
requests.push(
fetch(`/api/trpc/scripts.getScriptContent?input=${encodeURIComponent(JSON.stringify({ json: { path: `vm/${defaultScriptName}` } }))}`)
);
requests.push(
fetch(`/api/trpc/scripts.getScriptContent?input=${encodeURIComponent(JSON.stringify({ json: { path: `vw/${defaultScriptName}` } }))}`)
);
// Default install script
requests.push(
fetch(`/api/trpc/scripts.getScriptContent?input=${encodeURIComponent(JSON.stringify({ json: { path: `install/${slug}-install.sh` } }))}`)
);
// Alpine versions if variant exists
if (hasAlpineVariant) {
requests.push( requests.push(
fetch(`/api/trpc/scripts.getScriptContent?input=${encodeURIComponent(JSON.stringify({ json: { path: `ct/${alpineScriptName}` } }))}`) fetch(
); `/api/trpc/scripts.getScriptContent?input=${encodeURIComponent(JSON.stringify({ json: { path: defaultScriptPath } }))}`,
requests.push( ),
fetch(`/api/trpc/scripts.getScriptContent?input=${encodeURIComponent(JSON.stringify({ json: { path: `install/alpine-${slug}-install.sh` } }))}`)
); );
requestTypes.push("default-main");
} }
// Default install script (only for ct/ scripts)
if (hasInstallScript && defaultScriptPath?.startsWith("ct/")) {
requests.push(
fetch(
`/api/trpc/scripts.getScriptContent?input=${encodeURIComponent(JSON.stringify({ json: { path: `install/${slug}-install.sh` } }))}`,
),
);
requestTypes.push("default-install");
}
// Alpine main script
if (hasAlpineVariant && alpineScriptPath) {
requests.push(
fetch(
`/api/trpc/scripts.getScriptContent?input=${encodeURIComponent(JSON.stringify({ json: { path: alpineScriptPath } }))}`,
),
);
requestTypes.push("alpine-main");
}
// Alpine install script (only for ct/ scripts)
if (
hasAlpineVariant &&
hasInstallScript &&
alpineScriptPath?.startsWith("ct/")
) {
requests.push(
fetch(
`/api/trpc/scripts.getScriptContent?input=${encodeURIComponent(JSON.stringify({ json: { path: `install/alpine-${slug}-install.sh` } }))}`,
),
);
requestTypes.push("alpine-install");
}
const responses = await Promise.allSettled(requests); const responses = await Promise.allSettled(requests);
const content: ScriptContent = {}; const content: ScriptContent = {};
let responseIndex = 0;
// Default CT script // Process responses based on their types
const ctResponse = responses[responseIndex]; await Promise.all(
if (ctResponse?.status === 'fulfilled' && ctResponse.value.ok) { responses.map(async (response, index) => {
const ctData = await ctResponse.value.json() as { result?: { data?: { json?: { success?: boolean; content?: string } } } }; if (response.status === "fulfilled" && response.value.ok) {
if (ctData.result?.data?.json?.success) { try {
content.ctScript = ctData.result.data.json.content; const data = (await response.value.json()) as {
} result?: {
} data?: { json?: { success?: boolean; content?: string } };
};
responseIndex++; };
// Tools script const type = requestTypes[index];
const toolsResponse = responses[responseIndex]; if (
if (toolsResponse?.status === 'fulfilled' && toolsResponse.value.ok) { data.result?.data?.json?.success &&
const toolsData = await toolsResponse.value.json() as { result?: { data?: { json?: { success?: boolean; content?: string } } } }; data.result.data.json.content
if (toolsData.result?.data?.json?.success) { ) {
content.ctScript = toolsData.result.data.json.content; // Use ctScript field for tools scripts too switch (type) {
} case "default-main":
} content.mainScript = data.result.data.json.content;
break;
responseIndex++; case "default-install":
// VM script content.installScript = data.result.data.json.content;
const vmResponse = responses[responseIndex]; break;
if (vmResponse?.status === 'fulfilled' && vmResponse.value.ok) { case "alpine-main":
const vmData = await vmResponse.value.json() as { result?: { data?: { json?: { success?: boolean; content?: string } } } }; content.alpineMainScript = data.result.data.json.content;
if (vmData.result?.data?.json?.success) { break;
content.ctScript = vmData.result.data.json.content; // Use ctScript field for VM scripts too case "alpine-install":
} content.alpineInstallScript = data.result.data.json.content;
} break;
}
responseIndex++; }
// VW script } catch {
const vwResponse = responses[responseIndex]; // Ignore errors
if (vwResponse?.status === 'fulfilled' && vwResponse.value.ok) { }
const vwData = await vwResponse.value.json() as { result?: { data?: { json?: { success?: boolean; content?: string } } } };
if (vwData.result?.data?.json?.success) {
content.ctScript = vwData.result.data.json.content; // Use ctScript field for VW scripts too
}
}
responseIndex++;
// Default install script
const installResponse = responses[responseIndex];
if (installResponse?.status === 'fulfilled' && installResponse.value.ok) {
const installData = await installResponse.value.json() as { result?: { data?: { json?: { success?: boolean; content?: string } } } };
if (installData.result?.data?.json?.success) {
content.installScript = installData.result.data.json.content;
}
}
responseIndex++;
// Alpine CT script
if (hasAlpineVariant) {
const alpineCtResponse = responses[responseIndex];
if (alpineCtResponse?.status === 'fulfilled' && alpineCtResponse.value.ok) {
const alpineCtData = await alpineCtResponse.value.json() as { result?: { data?: { json?: { success?: boolean; content?: string } } } };
if (alpineCtData.result?.data?.json?.success) {
content.alpineCtScript = alpineCtData.result.data.json.content;
} }
} }),
responseIndex++; );
}
// Alpine install script
if (hasAlpineVariant) {
const alpineInstallResponse = responses[responseIndex];
if (alpineInstallResponse?.status === 'fulfilled' && alpineInstallResponse.value.ok) {
const alpineInstallData = await alpineInstallResponse.value.json() as { result?: { data?: { json?: { success?: boolean; content?: string } } } };
if (alpineInstallData.result?.data?.json?.success) {
content.alpineInstallScript = alpineInstallData.result.data.json.content;
}
}
}
setScriptContent(content); setScriptContent(content);
} catch (err) { } catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load script content'); setError(
err instanceof Error ? err.message : "Failed to load script content",
);
} finally { } finally {
setIsLoading(false); setIsLoading(false);
} }
}, [defaultScriptName, alpineScriptName, slug, hasAlpineVariant]); }, [
defaultScriptPath,
alpineScriptPath,
slug,
hasAlpineVariant,
hasInstallScript,
]);
useEffect(() => { useEffect(() => {
if (isOpen && scriptName) { if (isOpen && scriptName) {
@@ -179,51 +186,63 @@ export function TextViewer({ scriptName, isOpen, onClose, script }: TextViewerPr
return ( return (
<div <div
className="fixed inset-0 backdrop-blur-sm bg-black/50 flex items-center justify-center p-4 z-50" className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4 backdrop-blur-sm"
onClick={handleBackdropClick} onClick={handleBackdropClick}
> >
<div className="bg-card rounded-lg shadow-xl max-w-6xl w-full max-h-[90vh] flex flex-col border border-border mx-4 sm:mx-0"> <div className="bg-card border-border mx-4 flex max-h-[90vh] w-full max-w-6xl flex-col rounded-lg border shadow-xl sm:mx-0">
{/* Header */} {/* Header */}
<div className="flex items-center justify-between p-6 border-b border-border"> <div className="border-border flex items-center justify-between border-b p-6">
<div className="flex items-center space-x-4 flex-1"> <div className="flex flex-1 items-center space-x-4">
<h2 className="text-2xl font-bold text-foreground"> <h2 className="text-foreground text-2xl font-bold">
Script Viewer: {defaultScriptName} Script Viewer: {defaultScriptName}
</h2> </h2>
{hasAlpineVariant && ( {hasAlpineVariant && (
<div className="flex space-x-2"> <div className="flex space-x-2">
<Button <Button
variant={selectedVersion === 'default' ? 'default' : 'outline'} variant={
onClick={() => setSelectedVersion('default')} selectedVersion === "default" ? "default" : "outline"
}
onClick={() => setSelectedVersion("default")}
className="px-3 py-1 text-sm" className="px-3 py-1 text-sm"
> >
Default Default
</Button> </Button>
<Button <Button
variant={selectedVersion === 'alpine' ? 'default' : 'outline'} variant={selectedVersion === "alpine" ? "default" : "outline"}
onClick={() => setSelectedVersion('alpine')} onClick={() => setSelectedVersion("alpine")}
className="px-3 py-1 text-sm" className="px-3 py-1 text-sm"
> >
Alpine Alpine
</Button> </Button>
</div> </div>
)} )}
{((selectedVersion === 'default' && (scriptContent.ctScript || scriptContent.installScript)) || {/* Boolean logic intentionally uses || for truthiness checks - eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing */}
(selectedVersion === 'alpine' && (scriptContent.alpineCtScript || scriptContent.alpineInstallScript))) && ( {((selectedVersion === "default" &&
Boolean(
scriptContent.mainScript ?? scriptContent.installScript,
)) ||
(selectedVersion === "alpine" &&
Boolean(
scriptContent.alpineMainScript ??
scriptContent.alpineInstallScript,
))) && (
<div className="flex space-x-2"> <div className="flex space-x-2">
<Button <Button
variant={activeTab === 'ct' ? 'outline' : 'ghost'} variant={activeTab === "main" ? "outline" : "ghost"}
onClick={() => setActiveTab('ct')} onClick={() => setActiveTab("main")}
className="px-3 py-1 text-sm" className="px-3 py-1 text-sm"
> >
CT Script Script
</Button>
<Button
variant={activeTab === 'install' ? 'outline' : 'ghost'}
onClick={() => setActiveTab('install')}
className="px-3 py-1 text-sm"
>
Install Script
</Button> </Button>
{hasInstallScript && (
<Button
variant={activeTab === "install" ? "outline" : "ghost"}
onClick={() => setActiveTab("install")}
className="px-3 py-1 text-sm"
>
Install Script
</Button>
)}
</div> </div>
)} )}
</div> </div>
@@ -231,92 +250,108 @@ export function TextViewer({ scriptName, isOpen, onClose, script }: TextViewerPr
onClick={onClose} onClick={onClose}
className="text-muted-foreground hover:text-foreground transition-colors" className="text-muted-foreground hover:text-foreground transition-colors"
> >
<svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <svg
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" /> className="h-6 w-6"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M6 18L18 6M6 6l12 12"
/>
</svg> </svg>
</button> </button>
</div> </div>
{/* Content */} {/* Content */}
<div className="flex-1 overflow-hidden flex flex-col"> <div className="flex flex-1 flex-col overflow-hidden">
{isLoading ? ( {isLoading ? (
<div className="flex items-center justify-center h-full"> <div className="flex h-full items-center justify-center">
<div className="text-lg text-muted-foreground">Loading script content...</div> <div className="text-muted-foreground text-lg">
Loading script content...
</div>
</div> </div>
) : error ? ( ) : error ? (
<div className="flex items-center justify-center h-full"> <div className="flex h-full items-center justify-center">
<div className="text-lg text-destructive">Error: {error}</div> <div className="text-destructive text-lg">Error: {error}</div>
</div> </div>
) : ( ) : (
<div className="flex-1 overflow-auto"> <div className="flex-1 overflow-auto">
{activeTab === 'ct' && ( {activeTab === "main" &&
selectedVersion === 'default' && scriptContent.ctScript ? ( (selectedVersion === "default" && scriptContent.mainScript ? (
<SyntaxHighlighter <SyntaxHighlighter
language="bash" language="bash"
style={tomorrow} style={tomorrow}
customStyle={{ customStyle={{
margin: 0, margin: 0,
padding: '1rem', padding: "1rem",
fontSize: '14px', fontSize: "14px",
lineHeight: '1.5', lineHeight: "1.5",
minHeight: '100%' minHeight: "100%",
}} }}
showLineNumbers={true} showLineNumbers={true}
wrapLines={true} wrapLines={true}
> >
{scriptContent.ctScript} {scriptContent.mainScript}
</SyntaxHighlighter> </SyntaxHighlighter>
) : selectedVersion === 'alpine' && scriptContent.alpineCtScript ? ( ) : selectedVersion === "alpine" &&
scriptContent.alpineMainScript ? (
<SyntaxHighlighter <SyntaxHighlighter
language="bash" language="bash"
style={tomorrow} style={tomorrow}
customStyle={{ customStyle={{
margin: 0, margin: 0,
padding: '1rem', padding: "1rem",
fontSize: '14px', fontSize: "14px",
lineHeight: '1.5', lineHeight: "1.5",
minHeight: '100%' minHeight: "100%",
}} }}
showLineNumbers={true} showLineNumbers={true}
wrapLines={true} wrapLines={true}
> >
{scriptContent.alpineCtScript} {scriptContent.alpineMainScript}
</SyntaxHighlighter> </SyntaxHighlighter>
) : ( ) : (
<div className="flex items-center justify-center h-full"> <div className="flex h-full items-center justify-center">
<div className="text-lg text-muted-foreground"> <div className="text-muted-foreground text-lg">
{selectedVersion === 'default' ? 'Default CT script not found' : 'Alpine CT script not found'} {selectedVersion === "default"
? "Default script not found"
: "Alpine script not found"}
</div> </div>
</div> </div>
) ))}
)} {activeTab === "install" &&
{activeTab === 'install' && ( (selectedVersion === "default" &&
selectedVersion === 'default' && scriptContent.installScript ? ( scriptContent.installScript ? (
<SyntaxHighlighter <SyntaxHighlighter
language="bash" language="bash"
style={tomorrow} style={tomorrow}
customStyle={{ customStyle={{
margin: 0, margin: 0,
padding: '1rem', padding: "1rem",
fontSize: '14px', fontSize: "14px",
lineHeight: '1.5', lineHeight: "1.5",
minHeight: '100%' minHeight: "100%",
}} }}
showLineNumbers={true} showLineNumbers={true}
wrapLines={true} wrapLines={true}
> >
{scriptContent.installScript} {scriptContent.installScript}
</SyntaxHighlighter> </SyntaxHighlighter>
) : selectedVersion === 'alpine' && scriptContent.alpineInstallScript ? ( ) : selectedVersion === "alpine" &&
scriptContent.alpineInstallScript ? (
<SyntaxHighlighter <SyntaxHighlighter
language="bash" language="bash"
style={tomorrow} style={tomorrow}
customStyle={{ customStyle={{
margin: 0, margin: 0,
padding: '1rem', padding: "1rem",
fontSize: '14px', fontSize: "14px",
lineHeight: '1.5', lineHeight: "1.5",
minHeight: '100%' minHeight: "100%",
}} }}
showLineNumbers={true} showLineNumbers={true}
wrapLines={true} wrapLines={true}
@@ -324,13 +359,14 @@ export function TextViewer({ scriptName, isOpen, onClose, script }: TextViewerPr
{scriptContent.alpineInstallScript} {scriptContent.alpineInstallScript}
</SyntaxHighlighter> </SyntaxHighlighter>
) : ( ) : (
<div className="flex items-center justify-center h-full"> <div className="flex h-full items-center justify-center">
<div className="text-lg text-muted-foreground"> <div className="text-muted-foreground text-lg">
{selectedVersion === 'default' ? 'Default install script not found' : 'Alpine install script not found'} {selectedVersion === "default"
? "Default install script not found"
: "Alpine install script not found"}
</div> </div>
</div> </div>
) ))}
)}
</div> </div>
)} )}
</div> </div>

View File

@@ -1,6 +1,6 @@
'use client'; 'use client';
import { createContext, useContext, useEffect, useState } from 'react'; import { createContext, useContext, useEffect, useState, startTransition } from 'react';
type Theme = 'light' | 'dark'; type Theme = 'light' | 'dark';
@@ -31,9 +31,13 @@ export function ThemeProvider({ children }: ThemeProviderProps) {
useEffect(() => { useEffect(() => {
const savedTheme = localStorage.getItem('theme') as Theme; const savedTheme = localStorage.getItem('theme') as Theme;
if (savedTheme && (savedTheme === 'light' || savedTheme === 'dark')) { if (savedTheme && (savedTheme === 'light' || savedTheme === 'dark')) {
setThemeState(savedTheme); startTransition(() => {
setThemeState(savedTheme);
});
} }
setMounted(true); startTransition(() => {
setMounted(true);
});
}, []); }, []);
// Apply theme to document element // Apply theme to document element

View File

@@ -0,0 +1,234 @@
"use client";
import { Button } from "./ui/button";
import { Badge } from "./ui/badge";
import { X, ExternalLink, Calendar, Tag, AlertTriangle } from "lucide-react";
import { useRegisterModal } from "./modal/ModalStackProvider";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
interface UpdateConfirmationModalProps {
isOpen: boolean;
onClose: () => void;
onConfirm: () => void;
releaseInfo: {
tagName: string;
name: string;
publishedAt: string;
htmlUrl: string;
body?: string;
} | null;
currentVersion: string;
latestVersion: string;
}
export function UpdateConfirmationModal({
isOpen,
onClose,
onConfirm,
releaseInfo,
currentVersion,
latestVersion,
}: UpdateConfirmationModalProps) {
useRegisterModal(isOpen, {
id: "update-confirmation-modal",
allowEscape: true,
onClose,
});
if (!isOpen || !releaseInfo) return null;
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4 backdrop-blur-sm">
<div className="bg-card border-border flex max-h-[90vh] w-full max-w-4xl flex-col rounded-lg border shadow-xl">
{/* Header */}
<div className="border-border flex items-center justify-between border-b p-6">
<div className="flex items-center gap-3">
<AlertTriangle className="text-warning h-6 w-6" />
<div>
<h2 className="text-card-foreground text-2xl font-bold">
Confirm Update
</h2>
<p className="text-muted-foreground mt-1 text-sm">
Review the changelog before proceeding with the update
</p>
</div>
</div>
<Button
variant="ghost"
size="sm"
onClick={onClose}
className="h-8 w-8 p-0"
>
<X className="h-4 w-4" />
</Button>
</div>
{/* Content */}
<div className="flex flex-1 flex-col overflow-hidden">
<div className="flex-1 space-y-4 overflow-y-auto p-6">
{/* Version Info */}
<div className="bg-muted/50 border-border rounded-lg border p-4">
<div className="mb-3 flex items-center justify-between">
<div className="flex items-center gap-3">
<h3 className="text-card-foreground text-lg font-semibold">
{releaseInfo.name || releaseInfo.tagName}
</h3>
<Badge variant="default" className="text-xs">
Latest
</Badge>
</div>
<Button
variant="ghost"
size="sm"
asChild
className="h-8 w-8 p-0"
>
<a
href={releaseInfo.htmlUrl}
target="_blank"
rel="noopener noreferrer"
title="View on GitHub"
>
<ExternalLink className="h-4 w-4" />
</a>
</Button>
</div>
<div className="text-muted-foreground mb-3 flex items-center gap-4 text-sm">
<div className="flex items-center gap-1">
<Tag className="h-4 w-4" />
<span>{releaseInfo.tagName}</span>
</div>
<div className="flex items-center gap-1">
<Calendar className="h-4 w-4" />
<span>
{new Date(releaseInfo.publishedAt).toLocaleDateString(
"en-US",
{
year: "numeric",
month: "long",
day: "numeric",
},
)}
</span>
</div>
</div>
<div className="text-muted-foreground text-sm">
<span>Updating from </span>
<span className="text-card-foreground font-medium">
v{currentVersion}
</span>
<span> to </span>
<span className="text-card-foreground font-medium">
v{latestVersion}
</span>
</div>
</div>
{/* Changelog */}
{releaseInfo.body ? (
<div className="border-border bg-card rounded-lg border p-6">
<h4 className="text-md text-card-foreground mb-4 font-semibold">
Changelog
</h4>
<div className="prose prose-sm dark:prose-invert max-w-none">
<ReactMarkdown
remarkPlugins={[remarkGfm]}
components={{
h1: ({ children }) => (
<h1 className="text-card-foreground mt-6 mb-4 text-2xl font-bold">
{children}
</h1>
),
h2: ({ children }) => (
<h2 className="text-card-foreground mt-5 mb-3 text-xl font-semibold">
{children}
</h2>
),
h3: ({ children }) => (
<h3 className="text-card-foreground mt-4 mb-2 text-lg font-medium">
{children}
</h3>
),
p: ({ children }) => (
<p className="text-card-foreground mb-3 leading-relaxed">
{children}
</p>
),
ul: ({ children }) => (
<ul className="text-card-foreground mb-3 list-inside list-disc space-y-1">
{children}
</ul>
),
ol: ({ children }) => (
<ol className="text-card-foreground mb-3 list-inside list-decimal space-y-1">
{children}
</ol>
),
li: ({ children }) => (
<li className="text-card-foreground">{children}</li>
),
a: ({ href, children }) => (
<a
href={href}
className="text-info hover:text-info/80 underline"
target="_blank"
rel="noopener noreferrer"
>
{children}
</a>
),
strong: ({ children }) => (
<strong className="text-card-foreground font-semibold">
{children}
</strong>
),
em: ({ children }) => (
<em className="text-card-foreground italic">
{children}
</em>
),
}}
>
{releaseInfo.body}
</ReactMarkdown>
</div>
</div>
) : (
<div className="border-border bg-card rounded-lg border p-6">
<p className="text-muted-foreground">
No changelog available for this release.
</p>
</div>
)}
{/* Warning */}
<div className="bg-warning/10 border-warning/30 rounded-lg border p-4">
<div className="flex items-start gap-3">
<AlertTriangle className="text-warning mt-0.5 h-5 w-5 flex-shrink-0" />
<div className="text-card-foreground text-sm">
<p className="mb-1 font-medium">Important:</p>
<p className="text-muted-foreground">
Please review the changelog above for any breaking changes
or important updates before proceeding. The server will
restart automatically after the update completes.
</p>
</div>
</div>
</div>
</div>
</div>
{/* Footer */}
<div className="border-border bg-muted/30 flex items-center justify-between border-t p-6">
<Button onClick={onClose} variant="ghost">
Cancel
</Button>
<Button onClick={onConfirm} variant="destructive" className="gap-2">
<span>Proceed with Update</span>
</Button>
</div>
</div>
</div>
);
}

View File

@@ -4,9 +4,10 @@ import { api } from "~/trpc/react";
import { Badge } from "./ui/badge"; import { Badge } from "./ui/badge";
import { Button } from "./ui/button"; import { Button } from "./ui/button";
import { ContextualHelpIcon } from "./ContextualHelpIcon"; import { ContextualHelpIcon } from "./ContextualHelpIcon";
import { UpdateConfirmationModal } from "./UpdateConfirmationModal";
import { ExternalLink, Download, RefreshCw, Loader2 } from "lucide-react"; import { ExternalLink, Download, RefreshCw, Loader2 } from "lucide-react";
import { useState, useEffect, useRef } from "react"; import { useState, useEffect, useRef, useCallback } from "react";
interface VersionDisplayProps { interface VersionDisplayProps {
onOpenReleaseNotes?: () => void; onOpenReleaseNotes?: () => void;
@@ -85,55 +86,233 @@ export function VersionDisplay({ onOpenReleaseNotes }: VersionDisplayProps = {})
const [updateLogs, setUpdateLogs] = useState<string[]>([]); const [updateLogs, setUpdateLogs] = useState<string[]>([]);
const [shouldSubscribe, setShouldSubscribe] = useState(false); const [shouldSubscribe, setShouldSubscribe] = useState(false);
const [updateStartTime, setUpdateStartTime] = useState<number | null>(null); const [updateStartTime, setUpdateStartTime] = useState<number | null>(null);
const lastLogTimeRef = useRef<number>(Date.now()); const [showUpdateConfirmation, setShowUpdateConfirmation] = useState(false);
const lastLogTimeRef = useRef<number>(0);
// Initialize lastLogTimeRef in useEffect to avoid calling Date.now() during render
useEffect(() => {
if (lastLogTimeRef.current === 0) {
lastLogTimeRef.current = Date.now();
}
}, []);
const reconnectIntervalRef = useRef<NodeJS.Timeout | null>(null); const reconnectIntervalRef = useRef<NodeJS.Timeout | null>(null);
const reloadTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const hasReloadedRef = useRef<boolean>(false);
const isUpdatingRef = useRef<boolean>(false);
const isNetworkErrorRef = useRef<boolean>(false);
const updateSessionIdRef = useRef<string | null>(null);
const updateStartTimeRef = useRef<number | null>(null);
const logFileModifiedTimeRef = useRef<number | null>(null);
const isCompleteProcessedRef = useRef<boolean>(false);
const executeUpdate = api.version.executeUpdate.useMutation({ const executeUpdate = api.version.executeUpdate.useMutation({
onSuccess: (result) => { onSuccess: (result) => {
setUpdateResult({ success: result.success, message: result.message }); setUpdateResult({ success: result.success, message: result.message });
if (result.success) { if (result.success) {
// Start subscribing to update logs // Start subscribing to update logs only if we're actually updating
setShouldSubscribe(true); if (isUpdatingRef.current) {
setUpdateLogs(['Update started...']); setShouldSubscribe(true);
setUpdateLogs(['Update started...']);
}
} else { } else {
setIsUpdating(false); setIsUpdating(false);
setShouldSubscribe(false); // Reset subscription on failure
updateSessionIdRef.current = null;
updateStartTimeRef.current = null;
logFileModifiedTimeRef.current = null;
isCompleteProcessedRef.current = false;
} }
}, },
onError: (error) => { onError: (error) => {
setUpdateResult({ success: false, message: error.message }); setUpdateResult({ success: false, message: error.message });
setIsUpdating(false); setIsUpdating(false);
setShouldSubscribe(false); // Reset subscription on error
updateSessionIdRef.current = null;
updateStartTimeRef.current = null;
logFileModifiedTimeRef.current = null;
isCompleteProcessedRef.current = false;
} }
}); });
// Poll for update logs // Poll for update logs - only enabled when shouldSubscribe is true AND we're updating
const { data: updateLogsData } = api.version.getUpdateLogs.useQuery(undefined, { const { data: updateLogsData } = api.version.getUpdateLogs.useQuery(undefined, {
enabled: shouldSubscribe, enabled: shouldSubscribe && isUpdating,
refetchInterval: 1000, // Poll every second refetchInterval: shouldSubscribe && isUpdating ? 1000 : false, // Poll every second only when updating
refetchIntervalInBackground: true, refetchIntervalInBackground: false, // Don't poll in background to prevent stale data
}); });
// Attempt to reconnect and reload page when server is back
// Memoized with useCallback to prevent recreation on every render
// Only depends on refs to avoid stale closures
const startReconnectAttempts = useCallback(() => {
// CRITICAL: Stricter guard - check refs BEFORE starting reconnect attempts
// Only start if we're actually updating and haven't already started
// Double-check isUpdating state and session validity to prevent false triggers from stale data
if (reconnectIntervalRef.current || !isUpdatingRef.current || hasReloadedRef.current || !updateStartTimeRef.current) {
return;
}
// Validate session age before starting reconnection attempts
const sessionAge = Date.now() - updateStartTimeRef.current;
const MAX_SESSION_AGE = 30 * 60 * 1000; // 30 minutes
if (sessionAge > MAX_SESSION_AGE) {
// Session is stale, don't start reconnection
return;
}
setUpdateLogs(prev => [...prev, 'Attempting to reconnect...']);
reconnectIntervalRef.current = setInterval(() => {
void (async () => {
// Guard: Only proceed if we're still updating and in network error state
// Check refs directly to avoid stale closures
if (!isUpdatingRef.current || !isNetworkErrorRef.current || hasReloadedRef.current || !updateStartTimeRef.current) {
// Clear interval if we're no longer updating
if (!isUpdatingRef.current && reconnectIntervalRef.current) {
clearInterval(reconnectIntervalRef.current);
reconnectIntervalRef.current = null;
}
return;
}
// Validate session is still valid
const currentSessionAge = Date.now() - updateStartTimeRef.current;
if (currentSessionAge > MAX_SESSION_AGE) {
// Session expired, stop reconnection attempts
if (reconnectIntervalRef.current) {
clearInterval(reconnectIntervalRef.current);
reconnectIntervalRef.current = null;
}
return;
}
try {
// Try to fetch the root path to check if server is back
const response = await fetch('/', { method: 'HEAD' });
if (response.ok || response.status === 200) {
// Double-check we're still updating and session is valid before reloading
if (!isUpdatingRef.current || hasReloadedRef.current || !updateStartTimeRef.current) {
return;
}
// Final session validation
const finalSessionAge = Date.now() - updateStartTimeRef.current;
if (finalSessionAge > MAX_SESSION_AGE) {
return;
}
// Mark that we're about to reload to prevent multiple reloads
hasReloadedRef.current = true;
setUpdateLogs(prev => [...prev, 'Server is back online! Reloading...']);
// Clear interval
if (reconnectIntervalRef.current) {
clearInterval(reconnectIntervalRef.current);
reconnectIntervalRef.current = null;
}
// Clear any existing reload timeout
if (reloadTimeoutRef.current) {
clearTimeout(reloadTimeoutRef.current);
reloadTimeoutRef.current = null;
}
// Set reload timeout
reloadTimeoutRef.current = setTimeout(() => {
reloadTimeoutRef.current = null;
window.location.reload();
}, 1000);
}
} catch {
// Server still down, keep trying
}
})();
}, 2000);
}, []); // Empty deps - only uses refs which are stable
// Update logs when data changes // Update logs when data changes
useEffect(() => { useEffect(() => {
// CRITICAL: Only process update logs if we're actually updating
// This prevents stale isComplete data from triggering reloads when not updating
if (!isUpdating || !updateStartTimeRef.current) {
return;
}
// CRITICAL: Validate session - only process logs from current update session
// Check that update started within last 30 minutes (reasonable window for update)
const sessionAge = Date.now() - updateStartTimeRef.current;
const MAX_SESSION_AGE = 30 * 60 * 1000; // 30 minutes
if (sessionAge > MAX_SESSION_AGE) {
// Session is stale, reset everything
setTimeout(() => {
setIsUpdating(false);
setShouldSubscribe(false);
}, 0);
updateSessionIdRef.current = null;
updateStartTimeRef.current = null;
logFileModifiedTimeRef.current = null;
isCompleteProcessedRef.current = false;
return;
}
if (updateLogsData?.success && updateLogsData.logs) { if (updateLogsData?.success && updateLogsData.logs) {
lastLogTimeRef.current = Date.now();
setUpdateLogs(updateLogsData.logs);
if (updateLogsData.isComplete) { if (updateLogsData.logFileModifiedTime !== null && logFileModifiedTimeRef.current !== null) {
setUpdateLogs(prev => [...prev, 'Update complete! Server restarting...']);
setIsNetworkError(true); if (updateLogsData.logFileModifiedTime < logFileModifiedTimeRef.current) {
return;
}
} else if (updateLogsData.logFileModifiedTime !== null && updateStartTimeRef.current) {
const timeDiff = updateLogsData.logFileModifiedTime - updateStartTimeRef.current;
if (timeDiff < -5000) {
}
logFileModifiedTimeRef.current = updateLogsData.logFileModifiedTime;
}
lastLogTimeRef.current = Date.now();
setTimeout(() => setUpdateLogs(updateLogsData.logs), 0);
if (
updateLogsData.isComplete &&
isUpdating &&
updateStartTimeRef.current &&
sessionAge < MAX_SESSION_AGE &&
!isCompleteProcessedRef.current
) {
// Mark as processed immediately to prevent multiple triggers
isCompleteProcessedRef.current = true;
// Stop polling immediately to prevent further stale data processing
setTimeout(() => setShouldSubscribe(false), 0);
setTimeout(() => {
setUpdateLogs(prev => [...prev, 'Update complete! Server restarting...']);
setIsNetworkError(true);
}, 0);
// Start reconnection attempts when we know update is complete // Start reconnection attempts when we know update is complete
startReconnectAttempts(); setTimeout(() => startReconnectAttempts(), 0);
} }
} }
}, [updateLogsData]); }, [updateLogsData, startReconnectAttempts, isUpdating]);
// Monitor for server connection loss and auto-reload (fallback only) // Monitor for server connection loss and auto-reload (fallback only)
useEffect(() => { useEffect(() => {
if (!shouldSubscribe) return; // Early return: only run if we're actually updating
if (!shouldSubscribe || !isUpdating) return;
// Only use this as a fallback - the main trigger should be completion detection // Only use this as a fallback - the main trigger should be completion detection
const checkInterval = setInterval(() => { const checkInterval = setInterval(() => {
// Check refs first to ensure we're still updating
if (!isUpdatingRef.current || hasReloadedRef.current) {
return;
}
const timeSinceLastLog = Date.now() - lastLogTimeRef.current; const timeSinceLastLog = Date.now() - lastLogTimeRef.current;
// Only start reconnection if we've been updating for at least 3 minutes // Only start reconnection if we've been updating for at least 3 minutes
@@ -141,7 +320,10 @@ export function VersionDisplay({ onOpenReleaseNotes }: VersionDisplayProps = {})
const hasBeenUpdatingLongEnough = updateStartTime && (Date.now() - updateStartTime) > 180000; // 3 minutes const hasBeenUpdatingLongEnough = updateStartTime && (Date.now() - updateStartTime) > 180000; // 3 minutes
const noLogsForAWhile = timeSinceLastLog > 60000; // 60 seconds const noLogsForAWhile = timeSinceLastLog > 60000; // 60 seconds
if (hasBeenUpdatingLongEnough && noLogsForAWhile && isUpdating && !isNetworkError) { // Additional guard: check refs again before triggering and validate session
const sessionAge = updateStartTimeRef.current ? Date.now() - updateStartTimeRef.current : Infinity;
const MAX_SESSION_AGE = 30 * 60 * 1000; // 30 minutes
if (hasBeenUpdatingLongEnough && noLogsForAWhile && isUpdatingRef.current && !isNetworkErrorRef.current && updateStartTimeRef.current && sessionAge < MAX_SESSION_AGE) {
setIsNetworkError(true); setIsNetworkError(true);
setUpdateLogs(prev => [...prev, 'Server restarting... waiting for reconnection...']); setUpdateLogs(prev => [...prev, 'Server restarting... waiting for reconnection...']);
@@ -151,55 +333,121 @@ export function VersionDisplay({ onOpenReleaseNotes }: VersionDisplayProps = {})
}, 10000); // Check every 10 seconds }, 10000); // Check every 10 seconds
return () => clearInterval(checkInterval); return () => clearInterval(checkInterval);
}, [shouldSubscribe, isUpdating, updateStartTime, isNetworkError]); }, [shouldSubscribe, isUpdating, updateStartTime, startReconnectAttempts]);
// Attempt to reconnect and reload page when server is back // Keep refs in sync with state
const startReconnectAttempts = () => {
if (reconnectIntervalRef.current) return;
setUpdateLogs(prev => [...prev, 'Attempting to reconnect...']);
reconnectIntervalRef.current = setInterval(() => {
void (async () => {
try {
// Try to fetch the root path to check if server is back
const response = await fetch('/', { method: 'HEAD' });
if (response.ok || response.status === 200) {
setUpdateLogs(prev => [...prev, 'Server is back online! Reloading...']);
// Clear interval and reload
if (reconnectIntervalRef.current) {
clearInterval(reconnectIntervalRef.current);
}
setTimeout(() => {
window.location.reload();
}, 1000);
}
} catch {
// Server still down, keep trying
}
})();
}, 2000);
};
// Cleanup reconnect interval on unmount
useEffect(() => { useEffect(() => {
isUpdatingRef.current = isUpdating;
// CRITICAL: Reset shouldSubscribe immediately when isUpdating becomes false
// This prevents stale polling from continuing
if (!isUpdating) {
setTimeout(() => {
setShouldSubscribe(false);
}, 0);
// Reset completion processing flag when update stops
isCompleteProcessedRef.current = false;
}
}, [isUpdating]);
useEffect(() => {
isNetworkErrorRef.current = isNetworkError;
}, [isNetworkError]);
// Keep updateStartTime ref in sync
useEffect(() => {
updateStartTimeRef.current = updateStartTime;
}, [updateStartTime]);
// Clear reconnect interval when update completes or component unmounts
useEffect(() => {
// If we're no longer updating, clear the reconnect interval and reset subscription
if (!isUpdating) {
if (reconnectIntervalRef.current) {
clearInterval(reconnectIntervalRef.current);
reconnectIntervalRef.current = null;
}
// Clear reload timeout if update stops
if (reloadTimeoutRef.current) {
clearTimeout(reloadTimeoutRef.current);
reloadTimeoutRef.current = null;
}
// Reset subscription to prevent stale polling
setTimeout(() => {
setShouldSubscribe(false);
}, 0);
// Reset completion processing flag
isCompleteProcessedRef.current = false;
// Don't clear session refs here - they're cleared explicitly on unmount or new update
}
return () => { return () => {
if (reconnectIntervalRef.current) { if (reconnectIntervalRef.current) {
clearInterval(reconnectIntervalRef.current); clearInterval(reconnectIntervalRef.current);
reconnectIntervalRef.current = null;
} }
if (reloadTimeoutRef.current) {
clearTimeout(reloadTimeoutRef.current);
reloadTimeoutRef.current = null;
}
};
}, [isUpdating]);
// Cleanup on component unmount - reset all update-related state
useEffect(() => {
return () => {
// Clear all intervals
if (reconnectIntervalRef.current) {
clearInterval(reconnectIntervalRef.current);
reconnectIntervalRef.current = null;
}
// Reset all refs and state
updateSessionIdRef.current = null;
updateStartTimeRef.current = null;
logFileModifiedTimeRef.current = null;
isCompleteProcessedRef.current = false;
hasReloadedRef.current = false;
isUpdatingRef.current = false;
isNetworkErrorRef.current = false;
}; };
}, []); }, []);
const handleUpdate = () => { const handleUpdate = () => {
// Show confirmation modal instead of starting update directly
setShowUpdateConfirmation(true);
};
const handleConfirmUpdate = () => {
// Close the confirmation modal
setShowUpdateConfirmation(false);
// Start the actual update process
const sessionId = `update_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const startTime = Date.now();
setIsUpdating(true); setIsUpdating(true);
setUpdateResult(null); setUpdateResult(null);
setIsNetworkError(false); setIsNetworkError(false);
setUpdateLogs([]); setUpdateLogs([]);
setShouldSubscribe(false); setShouldSubscribe(false); // Will be set to true in mutation onSuccess
setUpdateStartTime(Date.now()); setUpdateStartTime(startTime);
lastLogTimeRef.current = Date.now();
// Set refs for session tracking
updateSessionIdRef.current = sessionId;
updateStartTimeRef.current = startTime;
lastLogTimeRef.current = startTime;
logFileModifiedTimeRef.current = null; // Will be set when we first see log file
isCompleteProcessedRef.current = false; // Reset completion flag
hasReloadedRef.current = false; // Reset reload flag when starting new update
// Clear any existing reconnect interval and reload timeout
if (reconnectIntervalRef.current) {
clearInterval(reconnectIntervalRef.current);
reconnectIntervalRef.current = null;
}
if (reloadTimeoutRef.current) {
clearTimeout(reloadTimeoutRef.current);
reloadTimeoutRef.current = null;
}
executeUpdate.mutate(); executeUpdate.mutate();
}; };
@@ -233,6 +481,18 @@ export function VersionDisplay({ onOpenReleaseNotes }: VersionDisplayProps = {})
{/* Loading overlay */} {/* Loading overlay */}
{isUpdating && <LoadingOverlay isNetworkError={isNetworkError} logs={updateLogs} />} {isUpdating && <LoadingOverlay isNetworkError={isNetworkError} logs={updateLogs} />}
{/* Update Confirmation Modal */}
{versionStatus?.releaseInfo && (
<UpdateConfirmationModal
isOpen={showUpdateConfirmation}
onClose={() => setShowUpdateConfirmation(false)}
onConfirm={handleConfirmUpdate}
releaseInfo={versionStatus.releaseInfo}
currentVersion={versionStatus.currentVersion}
latestVersion={versionStatus.latestVersion}
/>
)}
<div className="flex flex-col sm:flex-row items-center gap-2 sm:gap-2"> <div className="flex flex-col sm:flex-row items-center gap-2 sm:gap-2">
<Badge <Badge
variant={isUpToDate ? "default" : "secondary"} variant={isUpToDate ? "default" : "secondary"}

View File

@@ -0,0 +1,44 @@
import type { FilterState } from "./FilterBar";
/**
* Returns the default FilterState with all properties initialized.
* This serves as the single source of truth for default filter values.
*/
export function getDefaultFilters(): FilterState {
return {
searchQuery: "",
showUpdatable: null,
selectedTypes: [],
selectedRepositories: [],
sortBy: "name",
sortOrder: "asc",
};
}
/**
* Merges saved filters with defaults, ensuring all FilterState properties exist.
* This prevents crashes when loading old saved filters that are missing new properties.
*
* @param savedFilters - Partial or undefined saved filters from storage
* @returns Complete FilterState with all properties guaranteed to exist
*/
export function mergeFiltersWithDefaults(
savedFilters: Partial<FilterState> | undefined
): FilterState {
const defaults = getDefaultFilters();
if (!savedFilters) {
return defaults;
}
// Merge saved filters with defaults, ensuring all properties exist
return {
searchQuery: savedFilters.searchQuery ?? defaults.searchQuery,
showUpdatable: savedFilters.showUpdatable ?? defaults.showUpdatable,
selectedTypes: savedFilters.selectedTypes ?? defaults.selectedTypes,
selectedRepositories: savedFilters.selectedRepositories ?? defaults.selectedRepositories,
sortBy: savedFilters.sortBy ?? defaults.sortBy,
sortOrder: savedFilters.sortOrder ?? defaults.sortOrder,
};
}

View File

@@ -6,30 +6,40 @@ export interface ToggleProps
checked?: boolean; checked?: boolean;
onCheckedChange?: (checked: boolean) => void; onCheckedChange?: (checked: boolean) => void;
label?: string; label?: string;
labelPosition?: 'left' | 'right';
} }
const Toggle = React.forwardRef<HTMLInputElement, ToggleProps>( const Toggle = React.forwardRef<HTMLInputElement, ToggleProps>(
({ className, checked, onCheckedChange, label, ...props }, ref) => { ({ className, checked, onCheckedChange, label, labelPosition = 'right', ...props }, ref) => {
const toggleSwitch = (
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
className="sr-only"
checked={checked}
onChange={(e) => onCheckedChange?.(e.target.checked)}
ref={ref}
{...props}
/>
<div className={cn(
"w-11 h-6 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-primary/20 rounded-full peer after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 dark:after:border-gray-500 after:border after:rounded-full after:h-5 after:w-5 after:transition-transform after:duration-300 after:ease-in-out after:shadow-md transition-colors duration-300 ease-in-out border-2 border-gray-300 dark:border-gray-600",
checked
? "bg-blue-500 dark:bg-blue-600 after:translate-x-full"
: "bg-gray-300 dark:bg-gray-700",
className
)} />
</label>
);
return ( return (
<div className="flex items-center space-x-3"> <div className="flex items-center space-x-3">
<label className="relative inline-flex items-center cursor-pointer"> {label && labelPosition === 'left' && (
<input <span className="text-sm font-medium text-foreground">
type="checkbox" {label}
className="sr-only" </span>
checked={checked} )}
onChange={(e) => onCheckedChange?.(e.target.checked)} {toggleSwitch}
ref={ref} {label && labelPosition === 'right' && (
{...props}
/>
<div className={cn(
"w-11 h-6 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-primary/20 rounded-full peer after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 dark:after:border-gray-500 after:border after:rounded-full after:h-5 after:w-5 after:transition-transform after:duration-300 after:ease-in-out after:shadow-md transition-colors duration-300 ease-in-out border-2 border-gray-300 dark:border-gray-600",
checked
? "bg-blue-500 dark:bg-blue-600 after:translate-x-full"
: "bg-gray-300 dark:bg-gray-700",
className
)} />
</label>
{label && (
<span className="text-sm font-medium text-foreground"> <span className="text-sm font-medium text-foreground">
{label} {label}
</span> </span>

View File

@@ -41,10 +41,14 @@ export async function POST(request: NextRequest) {
const sessionDurationDays = authConfig.sessionDurationDays; const sessionDurationDays = authConfig.sessionDurationDays;
const token = generateToken(username, sessionDurationDays); const token = generateToken(username, sessionDurationDays);
// Calculate expiration time for client
const expirationTime = Date.now() + (sessionDurationDays * 24 * 60 * 60 * 1000);
const response = NextResponse.json({ const response = NextResponse.json({
success: true, success: true,
message: 'Login successful', message: 'Login successful',
username username,
expirationTime
}); });
// Determine if request is over HTTPS // Determine if request is over HTTPS
@@ -54,7 +58,7 @@ export async function POST(request: NextRequest) {
response.cookies.set('auth-token', token, { response.cookies.set('auth-token', token, {
httpOnly: true, httpOnly: true,
secure: isSecure, // Only secure if actually over HTTPS secure: isSecure, // Only secure if actually over HTTPS
sameSite: 'strict', sameSite: 'lax', // Use lax for cross-origin navigation support
maxAge: sessionDurationDays * 24 * 60 * 60, // Use configured duration maxAge: sessionDurationDays * 24 * 60 * 60, // Use configured duration
path: '/', path: '/',
}); });

View File

@@ -3,6 +3,14 @@ import { NextResponse } from 'next/server';
import { getDatabase } from '../../../../../server/database-prisma'; import { getDatabase } from '../../../../../server/database-prisma';
import { getSSHService } from '../../../../../server/ssh-service'; import { getSSHService } from '../../../../../server/ssh-service';
interface ServerData {
id: number;
name: string;
ip: string;
ssh_key_path?: string | null;
key_generated?: boolean;
}
export async function GET( export async function GET(
request: NextRequest, request: NextRequest,
{ params }: { params: Promise<{ id: string }> } { params }: { params: Promise<{ id: string }> }
@@ -18,7 +26,7 @@ export async function GET(
} }
const db = getDatabase(); const db = getDatabase();
const server = await db.getServerById(id); const server = await db.getServerById(id) as ServerData | null;
if (!server) { if (!server) {
return NextResponse.json( return NextResponse.json(
@@ -28,14 +36,14 @@ export async function GET(
} }
// Only allow viewing public key if it was generated by the system // Only allow viewing public key if it was generated by the system
if (!(server as any).key_generated) { if (!server.key_generated) {
return NextResponse.json( return NextResponse.json(
{ error: 'Public key not available for user-provided keys' }, { error: 'Public key not available for user-provided keys' },
{ status: 403 } { status: 403 }
); );
} }
if (!(server as any).ssh_key_path) { if (!server.ssh_key_path) {
return NextResponse.json( return NextResponse.json(
{ error: 'SSH key path not found' }, { error: 'SSH key path not found' },
{ status: 404 } { status: 404 }
@@ -43,13 +51,13 @@ export async function GET(
} }
const sshService = getSSHService(); const sshService = getSSHService();
const publicKey = sshService.getPublicKey((server as any).ssh_key_path as string); const publicKey = sshService.getPublicKey(server.ssh_key_path);
return NextResponse.json({ return NextResponse.json({
success: true, success: true,
publicKey, publicKey,
serverName: (server as any).name, serverName: server.name,
serverIp: (server as any).ip serverIp: server.ip
}); });
} catch (error) { } catch (error) {
console.error('Error retrieving public key:', error); console.error('Error retrieving public key:', error);

View File

@@ -12,7 +12,7 @@ export const POST = withApiLogging(async function POST(_request: NextRequest) {
// Get the next available server ID for key file naming // Get the next available server ID for key file naming
const serverId = await db.getNextServerId(); const serverId = await db.getNextServerId();
const keyPair = await sshService.generateKeyPair(serverId); const keyPair = await sshService.generateKeyPair(Number(serverId));
return NextResponse.json({ return NextResponse.json({
success: true, success: true,

View File

@@ -4,9 +4,25 @@ import fs from 'fs';
import path from 'path'; import path from 'path';
import { isValidCron } from 'cron-validator'; import { isValidCron } from 'cron-validator';
interface AutoSyncSettings {
autoSyncEnabled: boolean;
syncIntervalType: string;
syncIntervalPredefined?: string;
syncIntervalCron?: string;
autoDownloadNew: boolean;
autoUpdateExisting: boolean;
notificationEnabled: boolean;
appriseUrls?: string[] | string;
lastAutoSync?: string;
lastAutoSyncError?: string;
lastAutoSyncErrorTime?: string;
testNotification?: boolean;
triggerManualSync?: boolean;
}
export async function POST(request: NextRequest) { export async function POST(request: NextRequest) {
try { try {
const settings = await request.json(); const settings = await request.json() as AutoSyncSettings;
if (!settings || typeof settings !== 'object') { if (!settings || typeof settings !== 'object') {
return NextResponse.json( return NextResponse.json(
@@ -54,7 +70,7 @@ export async function POST(request: NextRequest) {
// Validate predefined interval // Validate predefined interval
if (settings.syncIntervalType === 'predefined') { if (settings.syncIntervalType === 'predefined') {
const validIntervals = ['15min', '30min', '1hour', '6hours', '12hours', '24hours']; const validIntervals = ['15min', '30min', '1hour', '6hours', '12hours', '24hours'];
if (!validIntervals.includes(settings.syncIntervalPredefined)) { if (!settings.syncIntervalPredefined || !validIntervals.includes(settings.syncIntervalPredefined)) {
return NextResponse.json( return NextResponse.json(
{ error: 'Invalid predefined interval' }, { error: 'Invalid predefined interval' },
{ status: 400 } { status: 400 }
@@ -67,7 +83,7 @@ export async function POST(request: NextRequest) {
if (!settings.syncIntervalCron || typeof settings.syncIntervalCron !== 'string' || settings.syncIntervalCron.trim() === '') { if (!settings.syncIntervalCron || typeof settings.syncIntervalCron !== 'string' || settings.syncIntervalCron.trim() === '') {
// Fallback to predefined if custom is selected but no cron expression // Fallback to predefined if custom is selected but no cron expression
settings.syncIntervalType = 'predefined'; settings.syncIntervalType = 'predefined';
settings.syncIntervalPredefined = settings.syncIntervalPredefined || '1hour'; settings.syncIntervalPredefined = settings.syncIntervalPredefined ?? '1hour';
settings.syncIntervalCron = ''; settings.syncIntervalCron = '';
} else if (!isValidCron(settings.syncIntervalCron, { seconds: false })) { } else if (!isValidCron(settings.syncIntervalCron, { seconds: false })) {
return NextResponse.json( return NextResponse.json(
@@ -109,7 +125,7 @@ export async function POST(request: NextRequest) {
); );
} }
} }
} catch (parseError) { } catch {
return NextResponse.json( return NextResponse.json(
{ error: 'Invalid JSON format for Apprise URLs' }, { error: 'Invalid JSON format for Apprise URLs' },
{ status: 400 } { status: 400 }
@@ -130,15 +146,15 @@ export async function POST(request: NextRequest) {
const autoSyncSettings = { const autoSyncSettings = {
'AUTO_SYNC_ENABLED': settings.autoSyncEnabled ? 'true' : 'false', 'AUTO_SYNC_ENABLED': settings.autoSyncEnabled ? 'true' : 'false',
'SYNC_INTERVAL_TYPE': settings.syncIntervalType, 'SYNC_INTERVAL_TYPE': settings.syncIntervalType,
'SYNC_INTERVAL_PREDEFINED': settings.syncIntervalPredefined || '', 'SYNC_INTERVAL_PREDEFINED': settings.syncIntervalPredefined ?? '',
'SYNC_INTERVAL_CRON': settings.syncIntervalCron || '', 'SYNC_INTERVAL_CRON': settings.syncIntervalCron ?? '',
'AUTO_DOWNLOAD_NEW': settings.autoDownloadNew ? 'true' : 'false', 'AUTO_DOWNLOAD_NEW': settings.autoDownloadNew ? 'true' : 'false',
'AUTO_UPDATE_EXISTING': settings.autoUpdateExisting ? 'true' : 'false', 'AUTO_UPDATE_EXISTING': settings.autoUpdateExisting ? 'true' : 'false',
'NOTIFICATION_ENABLED': settings.notificationEnabled ? 'true' : 'false', 'NOTIFICATION_ENABLED': settings.notificationEnabled ? 'true' : 'false',
'APPRISE_URLS': Array.isArray(settings.appriseUrls) ? JSON.stringify(settings.appriseUrls) : (settings.appriseUrls || '[]'), 'APPRISE_URLS': Array.isArray(settings.appriseUrls) ? JSON.stringify(settings.appriseUrls) : (settings.appriseUrls ?? '[]'),
'LAST_AUTO_SYNC': settings.lastAutoSync || '', 'LAST_AUTO_SYNC': settings.lastAutoSync ?? '',
'LAST_AUTO_SYNC_ERROR': settings.lastAutoSyncError || '', 'LAST_AUTO_SYNC_ERROR': settings.lastAutoSyncError ?? '',
'LAST_AUTO_SYNC_ERROR_TIME': settings.lastAutoSyncErrorTime || '' 'LAST_AUTO_SYNC_ERROR_TIME': settings.lastAutoSyncErrorTime ?? ''
}; };
// Update or add each setting // Update or add each setting
@@ -160,18 +176,27 @@ export async function POST(request: NextRequest) {
// Reschedule auto-sync service with new settings // Reschedule auto-sync service with new settings
try { try {
const { getAutoSyncService, setAutoSyncService } = await import('../../../../server/lib/autoSyncInit.js'); const { getAutoSyncService, setAutoSyncService } = await import('../../../../server/lib/autoSyncInit');
let autoSyncService = getAutoSyncService(); let autoSyncService = getAutoSyncService();
// If no global instance exists, create one // If no global instance exists, create one
if (!autoSyncService) { if (!autoSyncService) {
const { AutoSyncService } = await import('../../../../server/services/autoSyncService.js'); const { AutoSyncService } = await import('../../../../server/services/autoSyncService');
autoSyncService = new AutoSyncService(); autoSyncService = new AutoSyncService();
setAutoSyncService(autoSyncService); setAutoSyncService(autoSyncService);
} }
// Update the global service instance with new settings // Update the global service instance with new settings
autoSyncService.saveSettings(settings); // Normalize appriseUrls to always be an array
const normalizedSettings = {
...settings,
appriseUrls: Array.isArray(settings.appriseUrls)
? settings.appriseUrls
: settings.appriseUrls
? [settings.appriseUrls]
: undefined
};
autoSyncService.saveSettings(normalizedSettings);
if (settings.autoSyncEnabled) { if (settings.autoSyncEnabled) {
autoSyncService.scheduleAutoSync(); autoSyncService.scheduleAutoSync();
@@ -180,7 +205,7 @@ export async function POST(request: NextRequest) {
// Ensure the service is completely stopped and won't restart // Ensure the service is completely stopped and won't restart
autoSyncService.isRunning = false; autoSyncService.isRunning = false;
// Also stop the global service instance if it exists // Also stop the global service instance if it exists
const { stopAutoSync: stopGlobalAutoSync } = await import('../../../../server/lib/autoSyncInit.js'); const { stopAutoSync: stopGlobalAutoSync } = await import('../../../../server/lib/autoSyncInit');
stopGlobalAutoSync(); stopGlobalAutoSync();
} }
} catch (error) { } catch (error) {
@@ -231,21 +256,21 @@ export async function GET() {
autoSyncEnabled: getEnvValue(envContent, 'AUTO_SYNC_ENABLED') === 'true', autoSyncEnabled: getEnvValue(envContent, 'AUTO_SYNC_ENABLED') === 'true',
syncIntervalType: getEnvValue(envContent, 'SYNC_INTERVAL_TYPE') || 'predefined', syncIntervalType: getEnvValue(envContent, 'SYNC_INTERVAL_TYPE') || 'predefined',
syncIntervalPredefined: getEnvValue(envContent, 'SYNC_INTERVAL_PREDEFINED') || '1hour', syncIntervalPredefined: getEnvValue(envContent, 'SYNC_INTERVAL_PREDEFINED') || '1hour',
syncIntervalCron: getEnvValue(envContent, 'SYNC_INTERVAL_CRON') || '', syncIntervalCron: getEnvValue(envContent, 'SYNC_INTERVAL_CRON') ?? '',
autoDownloadNew: getEnvValue(envContent, 'AUTO_DOWNLOAD_NEW') === 'true', autoDownloadNew: getEnvValue(envContent, 'AUTO_DOWNLOAD_NEW') === 'true',
autoUpdateExisting: getEnvValue(envContent, 'AUTO_UPDATE_EXISTING') === 'true', autoUpdateExisting: getEnvValue(envContent, 'AUTO_UPDATE_EXISTING') === 'true',
notificationEnabled: getEnvValue(envContent, 'NOTIFICATION_ENABLED') === 'true', notificationEnabled: getEnvValue(envContent, 'NOTIFICATION_ENABLED') === 'true',
appriseUrls: (() => { appriseUrls: (() => {
try { try {
const urlsValue = getEnvValue(envContent, 'APPRISE_URLS') || '[]'; const urlsValue = getEnvValue(envContent, 'APPRISE_URLS') ?? '[]';
return JSON.parse(urlsValue); return JSON.parse(urlsValue) as string[];
} catch { } catch {
return []; return [];
} }
})(), })(),
lastAutoSync: getEnvValue(envContent, 'LAST_AUTO_SYNC') || '', lastAutoSync: getEnvValue(envContent, 'LAST_AUTO_SYNC') ?? '',
lastAutoSyncError: getEnvValue(envContent, 'LAST_AUTO_SYNC_ERROR') || null, lastAutoSyncError: getEnvValue(envContent, 'LAST_AUTO_SYNC_ERROR') ?? null,
lastAutoSyncErrorTime: getEnvValue(envContent, 'LAST_AUTO_SYNC_ERROR_TIME') || null lastAutoSyncErrorTime: getEnvValue(envContent, 'LAST_AUTO_SYNC_ERROR_TIME') ?? null
}; };
return NextResponse.json({ settings }); return NextResponse.json({ settings });
@@ -275,8 +300,8 @@ async function handleTestNotification() {
const notificationEnabled = getEnvValue(envContent, 'NOTIFICATION_ENABLED') === 'true'; const notificationEnabled = getEnvValue(envContent, 'NOTIFICATION_ENABLED') === 'true';
const appriseUrls = (() => { const appriseUrls = (() => {
try { try {
const urlsValue = getEnvValue(envContent, 'APPRISE_URLS') || '[]'; const urlsValue = getEnvValue(envContent, 'APPRISE_URLS') ?? '[]';
return JSON.parse(urlsValue); return JSON.parse(urlsValue) as string[];
} catch { } catch {
return []; return [];
} }
@@ -289,7 +314,7 @@ async function handleTestNotification() {
); );
} }
if (!appriseUrls || appriseUrls.length === 0) { if (!appriseUrls?.length) {
return NextResponse.json( return NextResponse.json(
{ error: 'No Apprise URLs configured' }, { error: 'No Apprise URLs configured' },
{ status: 400 } { status: 400 }
@@ -297,7 +322,7 @@ async function handleTestNotification() {
} }
// Send test notification using the auto-sync service // Send test notification using the auto-sync service
const { AutoSyncService } = await import('../../../../server/services/autoSyncService.js'); const { AutoSyncService } = await import('../../../../server/services/autoSyncService');
const autoSyncService = new AutoSyncService(); const autoSyncService = new AutoSyncService();
const result = await autoSyncService.testNotification(); const result = await autoSyncService.testNotification();
@@ -345,11 +370,11 @@ async function handleManualSync() {
} }
// Trigger manual sync using the auto-sync service // Trigger manual sync using the auto-sync service
const { AutoSyncService } = await import('../../../../server/services/autoSyncService.js'); const { AutoSyncService } = await import('../../../../server/services/autoSyncService');
const autoSyncService = new AutoSyncService(); const autoSyncService = new AutoSyncService();
const result = await autoSyncService.executeAutoSync() as any; const result = await autoSyncService.executeAutoSync() as { success: boolean; message?: string } | null;
if (result && result.success) { if (result?.success) {
return NextResponse.json({ return NextResponse.json({
success: true, success: true,
message: 'Manual sync completed successfully', message: 'Manual sync completed successfully',
@@ -357,7 +382,7 @@ async function handleManualSync() {
}); });
} else { } else {
return NextResponse.json( return NextResponse.json(
{ error: result.message }, { error: result?.message ?? 'Unknown error' },
{ status: 500 } { status: 500 }
); );
} }
@@ -376,7 +401,7 @@ function getEnvValue(envContent: string, key: string): string {
const regex = new RegExp(`^${key}="(.+)"$`, 'm'); const regex = new RegExp(`^${key}="(.+)"$`, 'm');
let match = regex.exec(envContent); let match = regex.exec(envContent);
if (match && match[1]) { if (match?.[1]) {
let value = match[1]; let value = match[1];
// Remove extra quotes that might be around JSON values // Remove extra quotes that might be around JSON values
if (value.startsWith('"') && value.endsWith('"')) { if (value.startsWith('"') && value.endsWith('"')) {
@@ -388,7 +413,7 @@ function getEnvValue(envContent: string, key: string): string {
// Try to match without quotes (fallback) // Try to match without quotes (fallback)
const regexNoQuotes = new RegExp(`^${key}=([^\\s]*)$`, 'm'); const regexNoQuotes = new RegExp(`^${key}=([^\\s]*)$`, 'm');
match = regexNoQuotes.exec(envContent); match = regexNoQuotes.exec(envContent);
if (match && match[1]) { if (match?.[1]) {
return match[1]; return match[1];
} }

View File

@@ -1,49 +1,71 @@
"use client";
'use client'; import { useState, useRef, useEffect } from "react";
import { ScriptsGrid } from "./_components/ScriptsGrid";
import { useState, useRef, useEffect } from 'react'; import { DownloadedScriptsTab } from "./_components/DownloadedScriptsTab";
import { ScriptsGrid } from './_components/ScriptsGrid'; import { InstalledScriptsTab } from "./_components/InstalledScriptsTab";
import { DownloadedScriptsTab } from './_components/DownloadedScriptsTab'; import { BackupsTab } from "./_components/BackupsTab";
import { InstalledScriptsTab } from './_components/InstalledScriptsTab'; import { ResyncButton } from "./_components/ResyncButton";
import { ResyncButton } from './_components/ResyncButton'; import { Terminal } from "./_components/Terminal";
import { Terminal } from './_components/Terminal'; import { ServerSettingsButton } from "./_components/ServerSettingsButton";
import { ServerSettingsButton } from './_components/ServerSettingsButton'; import { SettingsButton } from "./_components/SettingsButton";
import { SettingsButton } from './_components/SettingsButton'; import { HelpButton } from "./_components/HelpButton";
import { HelpButton } from './_components/HelpButton'; import { VersionDisplay } from "./_components/VersionDisplay";
import { VersionDisplay } from './_components/VersionDisplay'; import { ThemeToggle } from "./_components/ThemeToggle";
import { ThemeToggle } from './_components/ThemeToggle'; import { Button } from "./_components/ui/button";
import { Button } from './_components/ui/button'; import { ContextualHelpIcon } from "./_components/ContextualHelpIcon";
import { ContextualHelpIcon } from './_components/ContextualHelpIcon'; import {
import { ReleaseNotesModal, getLastSeenVersion } from './_components/ReleaseNotesModal'; ReleaseNotesModal,
import { Footer } from './_components/Footer'; getLastSeenVersion,
import { Package, HardDrive, FolderOpen, LogOut } from 'lucide-react'; } from "./_components/ReleaseNotesModal";
import { api } from '~/trpc/react'; import { Footer } from "./_components/Footer";
import { useAuth } from './_components/AuthProvider'; import { Package, HardDrive, FolderOpen, LogOut, Archive } from "lucide-react";
import { api } from "~/trpc/react";
import { useAuth } from "./_components/AuthProvider";
import type { Server } from "~/types/server";
import type { ScriptCard } from "~/types/script";
export default function Home() { export default function Home() {
const { isAuthenticated, logout } = useAuth(); const { isAuthenticated, logout } = useAuth();
const [runningScript, setRunningScript] = useState<{ path: string; name: string; mode?: 'local' | 'ssh'; server?: any } | null>(null); const [runningScript, setRunningScript] = useState<{
const [activeTab, setActiveTab] = useState<'scripts' | 'downloaded' | 'installed'>(() => { path: string;
if (typeof window !== 'undefined') { name: string;
const savedTab = localStorage.getItem('activeTab') as 'scripts' | 'downloaded' | 'installed'; mode?: "local" | "ssh";
return savedTab || 'scripts'; server?: Server;
} | null>(null);
const [activeTab, setActiveTab] = useState<
"scripts" | "downloaded" | "installed" | "backups"
>(() => {
if (typeof window !== "undefined") {
const savedTab = localStorage.getItem("activeTab") as
| "scripts"
| "downloaded"
| "installed"
| "backups";
return savedTab || "scripts";
} }
return 'scripts'; return "scripts";
}); });
const [releaseNotesOpen, setReleaseNotesOpen] = useState(false); const [releaseNotesOpen, setReleaseNotesOpen] = useState(false);
const [highlightVersion, setHighlightVersion] = useState<string | undefined>(undefined); const [highlightVersion, setHighlightVersion] = useState<string | undefined>(
undefined,
);
const terminalRef = useRef<HTMLDivElement>(null); const terminalRef = useRef<HTMLDivElement>(null);
// Fetch data for script counts // Fetch data for script counts
const { data: scriptCardsData } = api.scripts.getScriptCardsWithCategories.useQuery(); const { data: scriptCardsData } =
const { data: localScriptsData } = api.scripts.getAllDownloadedScripts.useQuery(); api.scripts.getScriptCardsWithCategories.useQuery();
const { data: installedScriptsData } = api.installedScripts.getAllInstalledScripts.useQuery(); const { data: localScriptsData } =
api.scripts.getAllDownloadedScripts.useQuery();
const { data: installedScriptsData } =
api.installedScripts.getAllInstalledScripts.useQuery();
const { data: backupsData } = api.backups.getAllBackupsGrouped.useQuery();
const { data: versionData } = api.version.getCurrentVersion.useQuery(); const { data: versionData } = api.version.getCurrentVersion.useQuery();
// Save active tab to localStorage whenever it changes // Save active tab to localStorage whenever it changes
useEffect(() => { useEffect(() => {
if (typeof window !== 'undefined') { if (typeof window !== "undefined") {
localStorage.setItem('activeTab', activeTab); localStorage.setItem("activeTab", activeTab);
} }
}, [activeTab]); }, [activeTab]);
@@ -52,9 +74,12 @@ export default function Home() {
if (versionData?.success && versionData.version) { if (versionData?.success && versionData.version) {
const currentVersion = versionData.version; const currentVersion = versionData.version;
const lastSeenVersion = getLastSeenVersion(); const lastSeenVersion = getLastSeenVersion();
// If we have a current version and either no last seen version or versions don't match // If we have a current version and either no last seen version or versions don't match
if (currentVersion && (!lastSeenVersion || currentVersion !== lastSeenVersion)) { if (
currentVersion &&
(!lastSeenVersion || currentVersion !== lastSeenVersion)
) {
setHighlightVersion(currentVersion); setHighlightVersion(currentVersion);
setReleaseNotesOpen(true); setReleaseNotesOpen(true);
} }
@@ -75,11 +100,11 @@ export default function Home() {
const scriptCounts = { const scriptCounts = {
available: (() => { available: (() => {
if (!scriptCardsData?.success) return 0; if (!scriptCardsData?.success) return 0;
// Deduplicate scripts using Map by slug (same logic as ScriptsGrid.tsx) // Deduplicate scripts using Map by slug (same logic as ScriptsGrid.tsx)
const scriptMap = new Map<string, any>(); const scriptMap = new Map<string, ScriptCard>();
scriptCardsData.cards?.forEach(script => { scriptCardsData.cards?.forEach((script: ScriptCard) => {
if (script?.name && script?.slug) { if (script?.name && script?.slug) {
// Use slug as unique identifier, only keep first occurrence // Use slug as unique identifier, only keep first occurrence
if (!scriptMap.has(script.slug)) { if (!scriptMap.has(script.slug)) {
@@ -87,38 +112,83 @@ export default function Home() {
} }
} }
}); });
return scriptMap.size; return scriptMap.size;
})(), })(),
downloaded: (() => { downloaded: (() => {
if (!scriptCardsData?.success || !localScriptsData?.scripts) return 0; if (!scriptCardsData?.success || !localScriptsData?.scripts) return 0;
// Helper to normalize identifiers for robust matching
const normalizeId = (s?: string): string =>
(s ?? "")
.toLowerCase()
.replace(/\.(sh|bash|py|js|ts)$/g, "")
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
// First deduplicate GitHub scripts using Map by slug // First deduplicate GitHub scripts using Map by slug
const scriptMap = new Map<string, any>(); const scriptMap = new Map<string, ScriptCard>();
scriptCardsData.cards?.forEach(script => { scriptCardsData.cards?.forEach((script: ScriptCard) => {
if (script?.name && script?.slug) { if (script?.name && script?.slug) {
if (!scriptMap.has(script.slug)) { if (!scriptMap.has(script.slug)) {
scriptMap.set(script.slug, script); scriptMap.set(script.slug, script);
} }
} }
}); });
const deduplicatedGithubScripts = Array.from(scriptMap.values()); const deduplicatedGithubScripts = Array.from(scriptMap.values());
const localScripts = localScriptsData.scripts ?? []; const localScripts = (localScriptsData.scripts ?? []) as Array<{
name?: string;
slug?: string;
}>;
// Count scripts that are both in deduplicated GitHub data and have local versions // Count scripts that are both in deduplicated GitHub data and have local versions
return deduplicatedGithubScripts.filter(script => { // Use the same matching logic as DownloadedScriptsTab and ScriptsGrid
return deduplicatedGithubScripts.filter((script) => {
if (!script?.name) return false; if (!script?.name) return false;
return localScripts.some(local => {
// Check if there's a corresponding local script
return localScripts.some((local) => {
if (!local?.name) return false; if (!local?.name) return false;
const localName = local.name.replace(/\.sh$/, '');
return localName.toLowerCase() === script.name.toLowerCase() || // Primary: Exact slug-to-slug matching (most reliable)
localName.toLowerCase() === (script.slug ?? '').toLowerCase(); if (local.slug && script.slug) {
if (local.slug.toLowerCase() === script.slug.toLowerCase()) {
return true;
}
// Also try normalized slug matching (handles filename-based slugs vs JSON slugs)
if (
normalizeId(local.slug ?? undefined) ===
normalizeId(script.slug ?? undefined)
) {
return true;
}
}
// Secondary: Check install basenames (for edge cases where install script names differ from slugs)
const normalizedLocal = normalizeId(local.name ?? undefined);
const matchesInstallBasename =
script.install_basenames?.some(
(base) => normalizeId(String(base)) === normalizedLocal,
) ?? false;
if (matchesInstallBasename) return true;
// Tertiary: Normalized filename to normalized slug matching
if (
script.slug &&
normalizeId(local.name ?? undefined) ===
normalizeId(script.slug ?? undefined)
) {
return true;
}
return false;
}); });
}).length; }).length;
})(), })(),
installed: installedScriptsData?.scripts?.length ?? 0 installed: installedScriptsData?.scripts?.length ?? 0,
backups: backupsData?.success ? backupsData.backups.length : 0,
}; };
const scrollToTerminal = () => { const scrollToTerminal = () => {
@@ -126,15 +196,20 @@ export default function Home() {
// Get the element's position and scroll with a small offset for better mobile experience // Get the element's position and scroll with a small offset for better mobile experience
const elementTop = terminalRef.current.offsetTop; const elementTop = terminalRef.current.offsetTop;
const offset = window.innerWidth < 768 ? 20 : 0; // Small offset on mobile const offset = window.innerWidth < 768 ? 20 : 0; // Small offset on mobile
window.scrollTo({ window.scrollTo({
top: elementTop - offset, top: elementTop - offset,
behavior: 'smooth' behavior: "smooth",
}); });
} }
}; };
const handleRunScript = (scriptPath: string, scriptName: string, mode?: 'local' | 'ssh', server?: any) => { const handleRunScript = (
scriptPath: string,
scriptName: string,
mode?: "local" | "ssh",
server?: Server,
) => {
setRunningScript({ path: scriptPath, name: scriptName, mode, server }); setRunningScript({ path: scriptPath, name: scriptName, mode, server });
// Scroll to terminal after a short delay to ensure it's rendered // Scroll to terminal after a short delay to ensure it's rendered
setTimeout(scrollToTerminal, 100); setTimeout(scrollToTerminal, 100);
@@ -145,16 +220,16 @@ export default function Home() {
}; };
return ( return (
<main className="min-h-screen bg-background"> <main className="bg-background min-h-screen">
<div className="container mx-auto px-2 sm:px-4 py-4 sm:py-8"> <div className="container mx-auto px-2 py-4 sm:px-4 sm:py-8">
{/* Header */} {/* Header */}
<div className="text-center mb-6 sm:mb-8"> <div className="mb-6 text-center sm:mb-8">
<div className="flex justify-between items-start mb-2"> <div className="mb-2 flex items-start justify-between">
<div className="flex-1"></div> <div className="flex-1"></div>
<h1 className="text-2xl sm:text-3xl lg:text-4xl font-bold text-foreground flex items-center justify-center gap-2 sm:gap-3 flex-1"> <h1 className="text-foreground flex flex-1 items-center justify-center gap-2 text-2xl font-bold sm:gap-3 sm:text-3xl lg:text-4xl">
<span className="break-words">PVE Scripts Management</span> <span className="break-words">PVE Scripts Management</span>
</h1> </h1>
<div className="flex-1 flex justify-end items-center gap-2"> <div className="flex flex-1 items-center justify-end gap-2">
{isAuthenticated && ( {isAuthenticated && (
<Button <Button
variant="ghost" variant="ghost"
@@ -170,8 +245,9 @@ export default function Home() {
<ThemeToggle /> <ThemeToggle />
</div> </div>
</div> </div>
<p className="text-sm sm:text-base text-muted-foreground mb-4 px-2"> <p className="text-muted-foreground mb-4 px-2 text-sm sm:text-base">
Manage and execute Proxmox helper scripts locally with live output streaming Manage and execute Proxmox helper scripts locally with live output
streaming
</p> </p>
<div className="flex justify-center px-2"> <div className="flex justify-center px-2">
<VersionDisplay onOpenReleaseNotes={handleOpenReleaseNotes} /> <VersionDisplay onOpenReleaseNotes={handleOpenReleaseNotes} />
@@ -180,7 +256,7 @@ export default function Home() {
{/* Controls */} {/* Controls */}
<div className="mb-6 sm:mb-8"> <div className="mb-6 sm:mb-8">
<div className="flex flex-col sm:flex-row sm:flex-wrap sm:items-center gap-4 p-4 sm:p-6 bg-card rounded-lg shadow-sm border border-border"> <div className="bg-card border-border flex flex-col gap-4 rounded-lg border p-4 shadow-sm sm:flex-row sm:flex-wrap sm:items-center sm:p-6">
<ServerSettingsButton /> <ServerSettingsButton />
<SettingsButton /> <SettingsButton />
<ResyncButton /> <ResyncButton />
@@ -190,65 +266,92 @@ export default function Home() {
{/* Tab Navigation */} {/* Tab Navigation */}
<div className="mb-6 sm:mb-8"> <div className="mb-6 sm:mb-8">
<div className="border-b border-border"> <div className="border-border border-b">
<nav className="-mb-px flex flex-col sm:flex-row space-y-2 sm:space-y-0 sm:space-x-1"> <nav className="-mb-px flex flex-col space-y-2 sm:flex-row sm:space-y-0 sm:space-x-1">
<Button <Button
variant="ghost" variant="ghost"
size="null" size="null"
onClick={() => setActiveTab('scripts')} onClick={() => setActiveTab("scripts")}
className={`px-3 py-2 text-sm flex items-center justify-center sm:justify-start gap-2 w-full sm:w-auto ${ className={`flex w-full items-center justify-center gap-2 px-3 py-2 text-sm sm:w-auto sm:justify-start ${
activeTab === 'scripts' activeTab === "scripts"
? 'bg-accent text-accent-foreground rounded-t-md rounded-b-none' ? "bg-accent text-accent-foreground rounded-t-md rounded-b-none"
: 'hover:bg-accent hover:text-accent-foreground hover:rounded-t-md hover:rounded-b-none' : "hover:bg-accent hover:text-accent-foreground hover:rounded-t-md hover:rounded-b-none"
}`}> }`}
>
<Package className="h-4 w-4" /> <Package className="h-4 w-4" />
<span className="hidden sm:inline">Available Scripts</span> <span className="hidden sm:inline">Available Scripts</span>
<span className="sm:hidden">Available</span> <span className="sm:hidden">Available</span>
<span className="ml-1 px-2 py-0.5 text-xs bg-muted text-muted-foreground rounded-full"> <span className="bg-muted text-muted-foreground ml-1 rounded-full px-2 py-0.5 text-xs">
{scriptCounts.available} {scriptCounts.available}
</span> </span>
<ContextualHelpIcon section="available-scripts" tooltip="Help with Available Scripts" /> <ContextualHelpIcon
section="available-scripts"
tooltip="Help with Available Scripts"
/>
</Button> </Button>
<Button <Button
variant="ghost" variant="ghost"
size="null" size="null"
onClick={() => setActiveTab('downloaded')} onClick={() => setActiveTab("downloaded")}
className={`px-3 py-2 text-sm flex items-center justify-center sm:justify-start gap-2 w-full sm:w-auto ${ className={`flex w-full items-center justify-center gap-2 px-3 py-2 text-sm sm:w-auto sm:justify-start ${
activeTab === 'downloaded' activeTab === "downloaded"
? 'bg-accent text-accent-foreground rounded-t-md rounded-b-none' ? "bg-accent text-accent-foreground rounded-t-md rounded-b-none"
: 'hover:bg-accent hover:text-accent-foreground hover:rounded-t-md hover:rounded-b-none' : "hover:bg-accent hover:text-accent-foreground hover:rounded-t-md hover:rounded-b-none"
}`}> }`}
>
<HardDrive className="h-4 w-4" /> <HardDrive className="h-4 w-4" />
<span className="hidden sm:inline">Downloaded Scripts</span> <span className="hidden sm:inline">Downloaded Scripts</span>
<span className="sm:hidden">Downloaded</span> <span className="sm:hidden">Downloaded</span>
<span className="ml-1 px-2 py-0.5 text-xs bg-muted text-muted-foreground rounded-full"> <span className="bg-muted text-muted-foreground ml-1 rounded-full px-2 py-0.5 text-xs">
{scriptCounts.downloaded} {scriptCounts.downloaded}
</span> </span>
<ContextualHelpIcon section="downloaded-scripts" tooltip="Help with Downloaded Scripts" /> <ContextualHelpIcon
section="downloaded-scripts"
tooltip="Help with Downloaded Scripts"
/>
</Button> </Button>
<Button <Button
variant="ghost" variant="ghost"
size="null" size="null"
onClick={() => setActiveTab('installed')} onClick={() => setActiveTab("installed")}
className={`px-3 py-2 text-sm flex items-center justify-center sm:justify-start gap-2 w-full sm:w-auto ${ className={`flex w-full items-center justify-center gap-2 px-3 py-2 text-sm sm:w-auto sm:justify-start ${
activeTab === 'installed' activeTab === "installed"
? 'bg-accent text-accent-foreground rounded-t-md rounded-b-none' ? "bg-accent text-accent-foreground rounded-t-md rounded-b-none"
: 'hover:bg-accent hover:text-accent-foreground hover:rounded-t-md hover:rounded-b-none' : "hover:bg-accent hover:text-accent-foreground hover:rounded-t-md hover:rounded-b-none"
}`}> }`}
>
<FolderOpen className="h-4 w-4" /> <FolderOpen className="h-4 w-4" />
<span className="hidden sm:inline">Installed Scripts</span> <span className="hidden sm:inline">Installed Scripts</span>
<span className="sm:hidden">Installed</span> <span className="sm:hidden">Installed</span>
<span className="ml-1 px-2 py-0.5 text-xs bg-muted text-muted-foreground rounded-full"> <span className="bg-muted text-muted-foreground ml-1 rounded-full px-2 py-0.5 text-xs">
{scriptCounts.installed} {scriptCounts.installed}
</span> </span>
<ContextualHelpIcon section="installed-scripts" tooltip="Help with Installed Scripts" /> <ContextualHelpIcon
section="installed-scripts"
tooltip="Help with Installed Scripts"
/>
</Button>
<Button
variant="ghost"
size="null"
onClick={() => setActiveTab("backups")}
className={`flex w-full items-center justify-center gap-2 px-3 py-2 text-sm sm:w-auto sm:justify-start ${
activeTab === "backups"
? "bg-accent text-accent-foreground rounded-t-md rounded-b-none"
: "hover:bg-accent hover:text-accent-foreground hover:rounded-t-md hover:rounded-b-none"
}`}
>
<Archive className="h-4 w-4" />
<span className="hidden sm:inline">Backups</span>
<span className="sm:hidden">Backups</span>
<span className="bg-muted text-muted-foreground ml-1 rounded-full px-2 py-0.5 text-xs">
{scriptCounts.backups}
</span>
</Button> </Button>
</nav> </nav>
</div> </div>
</div> </div>
{/* Running Script Terminal */} {/* Running Script Terminal */}
{runningScript && ( {runningScript && (
<div ref={terminalRef} className="mb-8"> <div ref={terminalRef} className="mb-8">
@@ -262,17 +365,17 @@ export default function Home() {
)} )}
{/* Tab Content */} {/* Tab Content */}
{activeTab === 'scripts' && ( {activeTab === "scripts" && (
<ScriptsGrid onInstallScript={handleRunScript} /> <ScriptsGrid onInstallScript={handleRunScript} />
)} )}
{activeTab === 'downloaded' && ( {activeTab === "downloaded" && (
<DownloadedScriptsTab onInstallScript={handleRunScript} /> <DownloadedScriptsTab onInstallScript={handleRunScript} />
)} )}
{activeTab === 'installed' && ( {activeTab === "installed" && <InstalledScriptsTab />}
<InstalledScriptsTab />
)} {activeTab === "backups" && <BackupsTab />}
</div> </div>
{/* Footer */} {/* Footer */}

View File

@@ -147,7 +147,7 @@ export function getAuthConfig(): {
const sessionDurationRegex = /^AUTH_SESSION_DURATION_DAYS=(.*)$/m; const sessionDurationRegex = /^AUTH_SESSION_DURATION_DAYS=(.*)$/m;
const sessionDurationMatch = sessionDurationRegex.exec(envContent); const sessionDurationMatch = sessionDurationRegex.exec(envContent);
const sessionDurationDays = sessionDurationMatch const sessionDurationDays = sessionDurationMatch
? parseInt(sessionDurationMatch[1]?.trim() || String(DEFAULT_JWT_EXPIRY_DAYS), 10) || DEFAULT_JWT_EXPIRY_DAYS ? parseInt(sessionDurationMatch[1]?.trim() ?? String(DEFAULT_JWT_EXPIRY_DAYS), 10) || DEFAULT_JWT_EXPIRY_DAYS
: DEFAULT_JWT_EXPIRY_DAYS; : DEFAULT_JWT_EXPIRY_DAYS;
const hasCredentials = !!(username && passwordHash); const hasCredentials = !!(username && passwordHash);

View File

@@ -2,6 +2,8 @@ import { scriptsRouter } from "~/server/api/routers/scripts";
import { installedScriptsRouter } from "~/server/api/routers/installedScripts"; import { installedScriptsRouter } from "~/server/api/routers/installedScripts";
import { serversRouter } from "~/server/api/routers/servers"; import { serversRouter } from "~/server/api/routers/servers";
import { versionRouter } from "~/server/api/routers/version"; import { versionRouter } from "~/server/api/routers/version";
import { backupsRouter } from "~/server/api/routers/backups";
import { pbsCredentialsRouter } from "~/server/api/routers/pbsCredentials";
import { repositoriesRouter } from "~/server/api/routers/repositories"; import { repositoriesRouter } from "~/server/api/routers/repositories";
import { createCallerFactory, createTRPCRouter } from "~/server/api/trpc"; import { createCallerFactory, createTRPCRouter } from "~/server/api/trpc";
@@ -15,6 +17,8 @@ export const appRouter = createTRPCRouter({
installedScripts: installedScriptsRouter, installedScripts: installedScriptsRouter,
servers: serversRouter, servers: serversRouter,
version: versionRouter, version: versionRouter,
backups: backupsRouter,
pbsCredentials: pbsCredentialsRouter,
repositories: repositoriesRouter, repositories: repositoriesRouter,
}); });

View File

@@ -0,0 +1,171 @@
import { z } from 'zod';
import { createTRPCRouter, publicProcedure } from '~/server/api/trpc';
import { getDatabase } from '~/server/database-prisma';
import { getBackupService } from '~/server/services/backupService';
import { getRestoreService } from '~/server/services/restoreService';
import { readFile } from 'fs/promises';
import { join } from 'path';
import { existsSync } from 'fs';
import stripAnsi from 'strip-ansi';
export const backupsRouter = createTRPCRouter({
// Get all backups grouped by container ID
getAllBackupsGrouped: publicProcedure
.query(async () => {
try {
const db = getDatabase();
const groupedBackups = await db.getBackupsGroupedByContainer();
// Convert Map to array format for frontend
const result: Array<{
container_id: string;
hostname: string;
backups: Array<{
id: number;
backup_name: string;
backup_path: string;
size: bigint | null;
created_at: Date | null;
storage_name: string;
storage_type: string;
discovered_at: Date;
server_id?: number;
server_name: string | null;
server_color: string | null;
}>;
}> = [];
for (const [containerId, backups] of groupedBackups.entries()) {
if (backups.length === 0) continue;
// Get hostname from first backup (all backups for same container should have same hostname)
const hostname = backups[0]?.hostname ?? '';
result.push({
container_id: containerId,
hostname,
backups: backups.map(backup => ({
id: backup.id,
backup_name: backup.backup_name,
backup_path: backup.backup_path,
size: backup.size,
created_at: backup.created_at,
storage_name: backup.storage_name,
storage_type: backup.storage_type,
discovered_at: backup.discovered_at,
server_id: backup.server_id,
server_name: backup.server?.name ?? null,
server_color: backup.server?.color ?? null,
})),
});
}
return {
success: true,
backups: result,
};
} catch (error) {
console.error('Error in getAllBackupsGrouped:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to fetch backups',
backups: [],
};
}
}),
// Discover backups for all containers
discoverBackups: publicProcedure
.mutation(async () => {
try {
const backupService = getBackupService();
await backupService.discoverAllBackups();
return {
success: true,
message: 'Backup discovery completed successfully',
};
} catch (error) {
console.error('Error in discoverBackups:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to discover backups',
};
}
}),
// Get restore progress from log file
getRestoreProgress: publicProcedure
.query(async () => {
try {
const logPath = join(process.cwd(), 'restore.log');
if (!existsSync(logPath)) {
return {
success: true,
logs: [],
isComplete: false
};
}
const logs = await readFile(logPath, 'utf-8');
const logLines = logs.split('\n')
.filter(line => line.trim())
.map(line => stripAnsi(line)); // Strip ANSI color codes
// Check if restore is complete by looking for completion indicators
const isComplete = logLines.some(line =>
line.includes('complete: Restore completed successfully') ||
line.includes('error: Error:') ||
line.includes('Restore completed successfully') ||
line.includes('Restore failed')
);
return {
success: true,
logs: logLines,
isComplete
};
} catch (error) {
console.error('Error reading restore logs:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to read restore logs',
logs: [],
isComplete: false
};
}
}),
// Restore backup
restoreBackup: publicProcedure
.input(z.object({
backupId: z.number(),
containerId: z.string(),
serverId: z.number(),
}))
.mutation(async ({ input }) => {
try {
const restoreService = getRestoreService();
const result = await restoreService.executeRestore(
input.backupId,
input.containerId,
input.serverId
);
return {
success: result.success,
error: result.error,
progress: result.progress,
};
} catch (error) {
console.error('Error in restoreBackup:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to restore backup',
progress: [],
};
}
}),
});

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,153 @@
import { z } from 'zod';
import { createTRPCRouter, publicProcedure } from '~/server/api/trpc';
import { getDatabase } from '~/server/database-prisma';
export const pbsCredentialsRouter = createTRPCRouter({
// Get credentials for a specific storage
getCredentialsForStorage: publicProcedure
.input(z.object({
serverId: z.number(),
storageName: z.string(),
}))
.query(async ({ input }) => {
try {
const db = getDatabase();
const credential = await db.getPBSCredential(input.serverId, input.storageName);
if (!credential) {
return {
success: false,
error: 'PBS credentials not found',
credential: null,
};
}
return {
success: true,
credential: {
id: credential.id,
server_id: credential.server_id,
storage_name: credential.storage_name,
pbs_ip: credential.pbs_ip,
pbs_datastore: credential.pbs_datastore,
pbs_fingerprint: credential.pbs_fingerprint,
// Don't return password for security
},
};
} catch (error) {
console.error('Error in getCredentialsForStorage:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to fetch PBS credentials',
credential: null,
};
}
}),
// Get all PBS credentials for a server
getAllCredentialsForServer: publicProcedure
.input(z.object({
serverId: z.number(),
}))
.query(async ({ input }) => {
try {
const db = getDatabase();
const credentials = await db.getPBSCredentialsByServer(input.serverId);
return {
success: true,
credentials: credentials.map((c: { id: number; server_id: number; storage_name: string; pbs_ip: string; pbs_datastore: string; pbs_fingerprint: string; pbs_password: string }) => ({
id: c.id,
server_id: c.server_id,
storage_name: c.storage_name,
pbs_ip: c.pbs_ip,
pbs_datastore: c.pbs_datastore,
pbs_fingerprint: c.pbs_fingerprint,
// Don't return password for security
})),
};
} catch (error) {
console.error('Error in getAllCredentialsForServer:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to fetch PBS credentials',
credentials: [],
};
}
}),
// Save/update PBS credentials
saveCredentials: publicProcedure
.input(z.object({
serverId: z.number(),
storageName: z.string(),
pbs_ip: z.string(),
pbs_datastore: z.string(),
pbs_password: z.string().optional(), // Optional to allow updating without changing password
pbs_fingerprint: z.string(),
}))
.mutation(async ({ input }) => {
try {
const db = getDatabase();
// If password is not provided, fetch existing credential to preserve password
let passwordToSave = input.pbs_password;
if (!passwordToSave) {
const existing = await db.getPBSCredential(input.serverId, input.storageName);
if (existing) {
passwordToSave = existing.pbs_password;
} else {
return {
success: false,
error: 'Password is required for new credentials',
};
}
}
await db.createOrUpdatePBSCredential({
server_id: input.serverId,
storage_name: input.storageName,
pbs_ip: input.pbs_ip,
pbs_datastore: input.pbs_datastore,
pbs_password: passwordToSave ?? '',
pbs_fingerprint: input.pbs_fingerprint,
});
return {
success: true,
message: 'PBS credentials saved successfully',
};
} catch (error) {
console.error('Error in saveCredentials:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to save PBS credentials',
};
}
}),
// Delete PBS credentials
deleteCredentials: publicProcedure
.input(z.object({
serverId: z.number(),
storageName: z.string(),
}))
.mutation(async ({ input }) => {
try {
const db = getDatabase();
await db.deletePBSCredential(input.serverId, input.storageName);
return {
success: true,
message: 'PBS credentials deleted successfully',
};
} catch (error) {
console.error('Error in deleteCredentials:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to delete PBS credentials',
};
}
}),
});

View File

@@ -1,3 +1,4 @@
import { z } from "zod"; import { z } from "zod";
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc"; import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import { scriptManager } from "~/server/lib/scripts"; import { scriptManager } from "~/server/lib/scripts";
@@ -100,7 +101,7 @@ export const scriptsRouter = createTRPCRouter({
getAllScripts: publicProcedure getAllScripts: publicProcedure
.query(async () => { .query(async () => {
try { try {
const scripts = await githubJsonService.getAllScripts(); const scripts = await localScriptsService.getAllScripts();
return { success: true, scripts }; return { success: true, scripts };
} catch (error) { } catch (error) {
return { return {
@@ -177,7 +178,7 @@ export const scriptsRouter = createTRPCRouter({
const scripts = await localScriptsService.getAllScripts(); const scripts = await localScriptsService.getAllScripts();
// Create a set of enabled repository URLs for fast lookup // Create a set of enabled repository URLs for fast lookup
const enabledRepoUrls = new Set(enabledRepos.map(repo => repo.url)); const enabledRepoUrls = new Set(enabledRepos.map((repo: { url: string }) => repo.url));
// Create category ID to name mapping // Create category ID to name mapping
const categoryMap: Record<number, string> = {}; const categoryMap: Record<number, string> = {};
@@ -188,7 +189,7 @@ export const scriptsRouter = createTRPCRouter({
} }
// Enhance cards with category information and additional script data // Enhance cards with category information and additional script data
const cardsWithCategories = cards.map(card => { const cardsWithCategories = cards.map((card: ScriptCard) => {
const script = scripts.find(s => s.slug === card.slug); const script = scripts.find(s => s.slug === card.slug);
const categoryNames: string[] = script?.categories?.map(id => categoryMap[id]).filter((name): name is string => typeof name === 'string') ?? []; const categoryNames: string[] = script?.categories?.map(id => categoryMap[id]).filter((name): name is string => typeof name === 'string') ?? [];
@@ -225,7 +226,7 @@ export const scriptsRouter = createTRPCRouter({
// Filter cards to only include scripts from enabled repositories // Filter cards to only include scripts from enabled repositories
// For backward compatibility, include scripts without repository_url // For backward compatibility, include scripts without repository_url
const filteredCards = cardsWithCategories.filter(card => { const filteredCards = cardsWithCategories.filter((card: ScriptCard) => {
const repoUrl = card.repository_url; const repoUrl = card.repository_url;
// If script has no repository_url, include it for backward compatibility // If script has no repository_url, include it for backward compatibility

View File

@@ -1,5 +1,5 @@
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc"; import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import { readFile, writeFile } from "fs/promises"; import { readFile, writeFile, stat } from "fs/promises";
import { join } from "path"; import { join } from "path";
import { spawn } from "child_process"; import { spawn } from "child_process";
import { env } from "~/env"; import { env } from "~/env";
@@ -111,7 +111,8 @@ export const versionRouter = createTRPCRouter({
tagName: release.tag_name, tagName: release.tag_name,
name: release.name, name: release.name,
publishedAt: release.published_at, publishedAt: release.published_at,
htmlUrl: release.html_url htmlUrl: release.html_url,
body: release.body
} }
}; };
} catch (error) { } catch (error) {
@@ -175,10 +176,21 @@ export const versionRouter = createTRPCRouter({
return { return {
success: true, success: true,
logs: [], logs: [],
isComplete: false isComplete: false,
logFileModifiedTime: null
}; };
} }
// Get log file modification time for session validation
let logFileModifiedTime: number | null = null;
try {
const stats = await stat(logPath);
logFileModifiedTime = stats.mtimeMs;
} catch (statError) {
// If we can't get stats, continue without timestamp
console.warn('Could not get log file stats:', statError);
}
const logs = await readFile(logPath, 'utf-8'); const logs = await readFile(logPath, 'utf-8');
const logLines = logs.split('\n') const logLines = logs.split('\n')
.filter(line => line.trim()) .filter(line => line.trim())
@@ -201,7 +213,8 @@ export const versionRouter = createTRPCRouter({
return { return {
success: true, success: true,
logs: logLines, logs: logLines,
isComplete isComplete,
logFileModifiedTime
}; };
} catch (error) { } catch (error) {
console.error('Error reading update logs:', error); console.error('Error reading update logs:', error);
@@ -209,7 +222,8 @@ export const versionRouter = createTRPCRouter({
success: false, success: false,
error: error instanceof Error ? error.message : 'Failed to read update logs', error: error instanceof Error ? error.message : 'Failed to read update logs',
logs: [], logs: [],
isComplete: false isComplete: false,
logFileModifiedTime: null
}; };
} }
}), }),

View File

@@ -9,10 +9,10 @@ class DatabaseServicePrisma {
} }
init() { init() {
// Ensure data/ssh-keys directory exists // Ensure data/ssh-keys directory exists (recursive to create parent dirs)
const sshKeysDir = join(process.cwd(), 'data', 'ssh-keys'); const sshKeysDir = join(process.cwd(), 'data', 'ssh-keys');
if (!existsSync(sshKeysDir)) { if (!existsSync(sshKeysDir)) {
mkdirSync(sshKeysDir, { mode: 0o700 }); mkdirSync(sshKeysDir, { recursive: true, mode: 0o700 });
} }
} }
@@ -271,6 +271,161 @@ class DatabaseServicePrisma {
}); });
} }
// Backup CRUD operations
async createOrUpdateBackup(backupData) {
// Find existing backup by container_id, server_id, and backup_path
const existing = await prisma.backup.findFirst({
where: {
container_id: backupData.container_id,
server_id: backupData.server_id,
backup_path: backupData.backup_path,
},
});
if (existing) {
// Update existing backup
return await prisma.backup.update({
where: { id: existing.id },
data: {
hostname: backupData.hostname,
backup_name: backupData.backup_name,
size: backupData.size,
created_at: backupData.created_at,
storage_name: backupData.storage_name,
storage_type: backupData.storage_type,
discovered_at: new Date(),
},
});
} else {
// Create new backup
return await prisma.backup.create({
data: {
container_id: backupData.container_id,
server_id: backupData.server_id,
hostname: backupData.hostname,
backup_name: backupData.backup_name,
backup_path: backupData.backup_path,
size: backupData.size,
created_at: backupData.created_at,
storage_name: backupData.storage_name,
storage_type: backupData.storage_type,
discovered_at: new Date(),
},
});
}
}
async getAllBackups() {
return await prisma.backup.findMany({
include: {
server: true,
},
orderBy: [
{ container_id: 'asc' },
{ created_at: 'desc' },
],
});
}
async getBackupById(id) {
return await prisma.backup.findUnique({
where: { id },
include: {
server: true,
},
});
}
async getBackupsByContainerId(containerId) {
return await prisma.backup.findMany({
where: { container_id: containerId },
include: {
server: true,
},
orderBy: { created_at: 'desc' },
});
}
async deleteBackupsForContainer(containerId, serverId) {
return await prisma.backup.deleteMany({
where: {
container_id: containerId,
server_id: serverId,
},
});
}
async getBackupsGroupedByContainer() {
const backups = await this.getAllBackups();
const grouped = new Map();
for (const backup of backups) {
const key = backup.container_id;
if (!grouped.has(key)) {
grouped.set(key, []);
}
grouped.get(key).push(backup);
}
return grouped;
}
// PBS Credentials CRUD operations
async createOrUpdatePBSCredential(credentialData) {
return await prisma.pBSStorageCredential.upsert({
where: {
server_id_storage_name: {
server_id: credentialData.server_id,
storage_name: credentialData.storage_name,
},
},
update: {
pbs_ip: credentialData.pbs_ip,
pbs_datastore: credentialData.pbs_datastore,
pbs_password: credentialData.pbs_password,
pbs_fingerprint: credentialData.pbs_fingerprint,
updated_at: new Date(),
},
create: {
server_id: credentialData.server_id,
storage_name: credentialData.storage_name,
pbs_ip: credentialData.pbs_ip,
pbs_datastore: credentialData.pbs_datastore,
pbs_password: credentialData.pbs_password,
pbs_fingerprint: credentialData.pbs_fingerprint,
},
});
}
async getPBSCredential(serverId, storageName) {
return await prisma.pBSStorageCredential.findUnique({
where: {
server_id_storage_name: {
server_id: serverId,
storage_name: storageName,
},
},
});
}
async getPBSCredentialsByServer(serverId) {
return await prisma.pBSStorageCredential.findMany({
where: { server_id: serverId },
orderBy: { storage_name: 'asc' },
});
}
async deletePBSCredential(serverId, storageName) {
return await prisma.pBSStorageCredential.delete({
where: {
server_id_storage_name: {
server_id: serverId,
storage_name: storageName,
},
},
});
}
async close() { async close() {
await prisma.$disconnect(); await prisma.$disconnect();
} }

View File

@@ -3,26 +3,128 @@ import { join } from 'path';
import { writeFileSync, unlinkSync, chmodSync, mkdirSync } from 'fs'; import { writeFileSync, unlinkSync, chmodSync, mkdirSync } from 'fs';
import { existsSync } from 'fs'; import { existsSync } from 'fs';
import type { CreateServerData } from '../types/server'; import type { CreateServerData } from '../types/server';
import type { Prisma } from '../../prisma/generated/prisma/client';
// Type definitions based on Prisma schema
type Server = {
id: number;
name: string;
ip: string;
user: string;
password: string | null;
auth_type: string | null;
ssh_key: string | null;
ssh_key_passphrase: string | null;
ssh_port: number | null;
color: string | null;
created_at: Date | null;
updated_at: Date | null;
ssh_key_path: string | null;
key_generated: boolean | null;
};
type InstalledScript = {
id: number;
script_name: string;
script_path: string;
container_id: string | null;
server_id: number | null;
execution_mode: string;
installation_date: Date | null;
status: string;
output_log: string | null;
web_ui_ip: string | null;
web_ui_port: number | null;
};
type InstalledScriptWithServer = InstalledScript & {
server: Server | null;
};
type LXCConfig = {
id: number;
installed_script_id: number;
arch: string | null;
cores: number | null;
memory: number | null;
hostname: string | null;
swap: number | null;
onboot: number | null;
ostype: string | null;
unprivileged: number | null;
net_name: string | null;
net_bridge: string | null;
net_hwaddr: string | null;
net_ip_type: string | null;
net_ip: string | null;
net_gateway: string | null;
net_type: string | null;
net_vlan: number | null;
rootfs_storage: string | null;
rootfs_size: string | null;
feature_keyctl: number | null;
feature_nesting: number | null;
feature_fuse: number | null;
feature_mount: string | null;
tags: string | null;
advanced_config: string | null;
synced_at: Date | null;
config_hash: string | null;
created_at: Date;
updated_at: Date;
};
type Backup = {
id: number;
container_id: string;
server_id: number;
hostname: string;
backup_name: string;
backup_path: string;
size: bigint | null;
created_at: Date | null;
storage_name: string;
storage_type: string;
discovered_at: Date;
};
type BackupWithServer = Backup & {
server: Server | null;
};
type PBSStorageCredential = {
id: number;
server_id: number;
storage_name: string;
pbs_ip: string;
pbs_datastore: string;
pbs_password: string;
pbs_fingerprint: string;
created_at: Date;
updated_at: Date;
};
type LXCConfigInput = Partial<Omit<LXCConfig, 'id' | 'installed_script_id' | 'created_at' | 'updated_at'>>;
class DatabaseServicePrisma { class DatabaseServicePrisma {
constructor() { constructor() {
this.init(); this.init();
} }
init() { init(): void {
// Ensure data/ssh-keys directory exists // Ensure data/ssh-keys directory exists (recursive to create parent dirs)
const sshKeysDir = join(process.cwd(), 'data', 'ssh-keys'); const sshKeysDir = join(process.cwd(), 'data', 'ssh-keys');
if (!existsSync(sshKeysDir)) { if (!existsSync(sshKeysDir)) {
mkdirSync(sshKeysDir, { mode: 0o700 }); mkdirSync(sshKeysDir, { recursive: true, mode: 0o700 });
} }
} }
// Server CRUD operations // Server CRUD operations
async createServer(serverData: CreateServerData) { async createServer(serverData: CreateServerData): Promise<Server> {
const { name, ip, user, password, auth_type, ssh_key, ssh_key_passphrase, ssh_port, color, key_generated } = serverData; const { name, ip, user, password, auth_type, ssh_key, ssh_key_passphrase, ssh_port, color, key_generated } = serverData;
const normalizedPort = ssh_port !== undefined ? parseInt(String(ssh_port), 10) : 22; const normalizedPort = ssh_port !== undefined ? parseInt(String(ssh_port), 10) : 22;
let ssh_key_path = null; let ssh_key_path: string | null = null;
// If using SSH key authentication, create persistent key file // If using SSH key authentication, create persistent key file
if (auth_type === 'key' && ssh_key) { if (auth_type === 'key' && ssh_key) {
@@ -30,7 +132,7 @@ class DatabaseServicePrisma {
ssh_key_path = this.createSSHKeyFile(serverId, ssh_key); ssh_key_path = this.createSSHKeyFile(serverId, ssh_key);
} }
return await prisma.server.create({ const result = await prisma.server.create({
data: { data: {
name, name,
ip, ip,
@@ -45,27 +147,30 @@ class DatabaseServicePrisma {
color, color,
} }
}); });
return result as Server;
} }
async getAllServers() { async getAllServers(): Promise<Server[]> {
return await prisma.server.findMany({ const result = await prisma.server.findMany({
orderBy: { created_at: 'desc' } orderBy: { created_at: 'desc' }
}); });
return result as Server[];
} }
async getServerById(id: number) { async getServerById(id: number): Promise<Server | null> {
return await prisma.server.findUnique({ const result = await prisma.server.findUnique({
where: { id } where: { id }
}); });
return result as Server | null;
} }
async updateServer(id: number, serverData: CreateServerData) { async updateServer(id: number, serverData: CreateServerData): Promise<Server> {
const { name, ip, user, password, auth_type, ssh_key, ssh_key_passphrase, ssh_port, color, key_generated } = serverData; const { name, ip, user, password, auth_type, ssh_key, ssh_key_passphrase, ssh_port, color, key_generated } = serverData;
const normalizedPort = ssh_port !== undefined ? parseInt(String(ssh_port), 10) : undefined; const normalizedPort = ssh_port !== undefined ? parseInt(String(ssh_port), 10) : undefined;
// Get existing server to check for key changes // Get existing server to check for key changes
const existingServer = await this.getServerById(id); const existingServer = await this.getServerById(id);
let ssh_key_path = existingServer?.ssh_key_path; let ssh_key_path = existingServer?.ssh_key_path ?? null;
// Handle SSH key changes // Handle SSH key changes
if (auth_type === 'key' && ssh_key) { if (auth_type === 'key' && ssh_key) {
@@ -101,7 +206,7 @@ class DatabaseServicePrisma {
ssh_key_path = null; ssh_key_path = null;
} }
return await prisma.server.update({ const result = await prisma.server.update({
where: { id }, where: { id },
data: { data: {
name, name,
@@ -117,9 +222,10 @@ class DatabaseServicePrisma {
color, color,
} }
}); });
return result as Server;
} }
async deleteServer(id: number) { async deleteServer(id: number): Promise<Server> {
// Get server info before deletion to clean up key files // Get server info before deletion to clean up key files
const server = await this.getServerById(id); const server = await this.getServerById(id);
@@ -136,9 +242,10 @@ class DatabaseServicePrisma {
} }
} }
return await prisma.server.delete({ const result = await prisma.server.delete({
where: { id } where: { id }
}); });
return result as Server;
} }
// Installed Scripts CRUD operations // Installed Scripts CRUD operations
@@ -152,10 +259,10 @@ class DatabaseServicePrisma {
output_log?: string; output_log?: string;
web_ui_ip?: string; web_ui_ip?: string;
web_ui_port?: number; web_ui_port?: number;
}) { }): Promise<InstalledScript> {
const { script_name, script_path, container_id, server_id, execution_mode, status, output_log, web_ui_ip, web_ui_port } = scriptData; const { script_name, script_path, container_id, server_id, execution_mode, status, output_log, web_ui_ip, web_ui_port } = scriptData;
return await prisma.installedScript.create({ const result = await prisma.installedScript.create({
data: { data: {
script_name, script_name,
script_path, script_path,
@@ -168,34 +275,40 @@ class DatabaseServicePrisma {
web_ui_port: web_ui_port ?? null, web_ui_port: web_ui_port ?? null,
} }
}); });
return result as InstalledScript;
} }
async getAllInstalledScripts() { async getAllInstalledScripts(): Promise<InstalledScriptWithServer[]> {
return await prisma.installedScript.findMany({ const result = await prisma.installedScript.findMany({
include: { include: {
server: true server: true,
lxc_config: true
}, },
orderBy: { installation_date: 'desc' } orderBy: { installation_date: 'desc' }
}); });
return result as InstalledScriptWithServer[];
} }
async getInstalledScriptById(id: number) { async getInstalledScriptById(id: number): Promise<InstalledScriptWithServer | null> {
return await prisma.installedScript.findUnique({ const result = await prisma.installedScript.findUnique({
where: { id }, where: { id },
include: { include: {
server: true server: true
} }
}); });
return result as InstalledScriptWithServer | null;
} }
async getInstalledScriptsByServer(server_id: number) { async getInstalledScriptsByServer(server_id: number): Promise<InstalledScriptWithServer[]> {
return await prisma.installedScript.findMany({ const result = await prisma.installedScript.findMany({
where: { server_id }, where: { server_id },
include: { include: {
server: true server: true,
lxc_config: true
}, },
orderBy: { installation_date: 'desc' } orderBy: { installation_date: 'desc' }
}); });
return result as InstalledScriptWithServer[];
} }
async updateInstalledScript(id: number, updateData: { async updateInstalledScript(id: number, updateData: {
@@ -205,17 +318,10 @@ class DatabaseServicePrisma {
output_log?: string; output_log?: string;
web_ui_ip?: string; web_ui_ip?: string;
web_ui_port?: number; web_ui_port?: number;
}) { }): Promise<InstalledScript | { changes: number }> {
const { script_name, container_id, status, output_log, web_ui_ip, web_ui_port } = updateData; const { script_name, container_id, status, output_log, web_ui_ip, web_ui_port } = updateData;
const updateFields: { const updateFields: Prisma.InstalledScriptUpdateInput = {};
script_name?: string;
container_id?: string;
status?: 'in_progress' | 'success' | 'failed';
output_log?: string;
web_ui_ip?: string;
web_ui_port?: number;
} = {};
if (script_name !== undefined) updateFields.script_name = script_name; if (script_name !== undefined) updateFields.script_name = script_name;
if (container_id !== undefined) updateFields.container_id = container_id; if (container_id !== undefined) updateFields.container_id = container_id;
if (status !== undefined) updateFields.status = status; if (status !== undefined) updateFields.status = status;
@@ -227,33 +333,36 @@ class DatabaseServicePrisma {
return { changes: 0 }; return { changes: 0 };
} }
return await prisma.installedScript.update({ const result = await prisma.installedScript.update({
where: { id }, where: { id },
data: updateFields data: updateFields
}); });
return result as InstalledScript;
} }
async deleteInstalledScript(id: number) { async deleteInstalledScript(id: number): Promise<InstalledScript> {
return await prisma.installedScript.delete({ const result = await prisma.installedScript.delete({
where: { id } where: { id }
}); });
return result as InstalledScript;
} }
async deleteInstalledScriptsByServer(server_id: number) { async deleteInstalledScriptsByServer(server_id: number): Promise<{ count: number }> {
return await prisma.installedScript.deleteMany({ const result = await prisma.installedScript.deleteMany({
where: { server_id } where: { server_id }
}); });
return result as { count: number };
} }
async getNextServerId() { async getNextServerId(): Promise<number> {
const result = await prisma.server.findFirst({ const result = await prisma.server.findFirst({
orderBy: { id: 'desc' }, orderBy: { id: 'desc' },
select: { id: true } select: { id: true }
}); });
return (result?.id ?? 0) + 1; return ((result as { id: number } | null)?.id ?? 0) + 1;
} }
createSSHKeyFile(serverId: number, sshKey: string) { createSSHKeyFile(serverId: number, sshKey: string): string {
const sshKeysDir = join(process.cwd(), 'data', 'ssh-keys'); const sshKeysDir = join(process.cwd(), 'data', 'ssh-keys');
const keyPath = join(sshKeysDir, `server_${serverId}_key`); const keyPath = join(sshKeysDir, `server_${serverId}_key`);
@@ -266,17 +375,18 @@ class DatabaseServicePrisma {
} }
// LXC Config CRUD operations // LXC Config CRUD operations
async createLXCConfig(scriptId: number, configData: any) { async createLXCConfig(scriptId: number, configData: LXCConfigInput): Promise<LXCConfig> {
return await prisma.lXCConfig.create({ const result = await prisma.lXCConfig.create({
data: { data: {
installed_script_id: scriptId, installed_script_id: scriptId,
...configData ...configData
} }
}); });
return result as LXCConfig;
} }
async updateLXCConfig(scriptId: number, configData: any) { async updateLXCConfig(scriptId: number, configData: LXCConfigInput): Promise<LXCConfig> {
return await prisma.lXCConfig.upsert({ const result = await prisma.lXCConfig.upsert({
where: { installed_script_id: scriptId }, where: { installed_script_id: scriptId },
update: configData, update: configData,
create: { create: {
@@ -284,21 +394,205 @@ class DatabaseServicePrisma {
...configData ...configData
} }
}); });
return result as LXCConfig;
} }
async getLXCConfigByScriptId(scriptId: number) { async getLXCConfigByScriptId(scriptId: number): Promise<LXCConfig | null> {
return await prisma.lXCConfig.findUnique({ const result = await prisma.lXCConfig.findUnique({
where: { installed_script_id: scriptId }
});
return result as LXCConfig | null;
}
async deleteLXCConfig(scriptId: number): Promise<void> {
await prisma.lXCConfig.delete({
where: { installed_script_id: scriptId } where: { installed_script_id: scriptId }
}); });
} }
async deleteLXCConfig(scriptId: number) { // Backup CRUD operations
return await prisma.lXCConfig.delete({ async createOrUpdateBackup(backupData: {
where: { installed_script_id: scriptId } container_id: string;
}); server_id: number;
hostname: string;
backup_name: string;
backup_path: string;
size?: bigint;
created_at?: Date;
storage_name: string;
storage_type: 'local' | 'storage' | 'pbs';
}): Promise<Backup> {
// Find existing backup by container_id, server_id, and backup_path
const existing = await prisma.backup.findFirst({
where: {
container_id: backupData.container_id,
server_id: backupData.server_id,
backup_path: backupData.backup_path,
},
}) as Backup | null;
if (existing) {
// Update existing backup
const result = await prisma.backup.update({
where: { id: existing.id },
data: {
hostname: backupData.hostname,
backup_name: backupData.backup_name,
size: backupData.size,
created_at: backupData.created_at,
storage_name: backupData.storage_name,
storage_type: backupData.storage_type,
discovered_at: new Date(),
},
});
return result as Backup;
} else {
// Create new backup
const result = await prisma.backup.create({
data: {
container_id: backupData.container_id,
server_id: backupData.server_id,
hostname: backupData.hostname,
backup_name: backupData.backup_name,
backup_path: backupData.backup_path,
size: backupData.size,
created_at: backupData.created_at,
storage_name: backupData.storage_name,
storage_type: backupData.storage_type,
discovered_at: new Date(),
},
});
return result as Backup;
}
} }
async close() { async getAllBackups(): Promise<BackupWithServer[]> {
const result = await prisma.backup.findMany({
include: {
server: true,
},
orderBy: [
{ container_id: 'asc' },
{ created_at: 'desc' },
],
});
return result as BackupWithServer[];
}
async getBackupById(id: number): Promise<BackupWithServer | null> {
const result = await prisma.backup.findUnique({
where: { id },
include: {
server: true,
},
});
return result as BackupWithServer | null;
}
async getBackupsByContainerId(containerId: string): Promise<BackupWithServer[]> {
const result = await prisma.backup.findMany({
where: { container_id: containerId },
include: {
server: true,
},
orderBy: { created_at: 'desc' },
});
return result as BackupWithServer[];
}
async deleteBackupsForContainer(containerId: string, serverId: number): Promise<{ count: number }> {
const result = await prisma.backup.deleteMany({
where: {
container_id: containerId,
server_id: serverId,
},
});
return result as { count: number };
}
async getBackupsGroupedByContainer(): Promise<Map<string, BackupWithServer[]>> {
const backups = await this.getAllBackups();
const grouped = new Map<string, BackupWithServer[]>();
for (const backup of backups) {
const key = backup.container_id;
if (!grouped.has(key)) {
grouped.set(key, []);
}
grouped.get(key)!.push(backup);
}
return grouped;
}
// PBS Credentials CRUD operations
async createOrUpdatePBSCredential(credentialData: {
server_id: number;
storage_name: string;
pbs_ip: string;
pbs_datastore: string;
pbs_password: string;
pbs_fingerprint: string;
}): Promise<PBSStorageCredential> {
const result = await prisma.pBSStorageCredential.upsert({
where: {
server_id_storage_name: {
server_id: credentialData.server_id,
storage_name: credentialData.storage_name,
},
},
update: {
pbs_ip: credentialData.pbs_ip,
pbs_datastore: credentialData.pbs_datastore,
pbs_password: credentialData.pbs_password,
pbs_fingerprint: credentialData.pbs_fingerprint,
updated_at: new Date(),
},
create: {
server_id: credentialData.server_id,
storage_name: credentialData.storage_name,
pbs_ip: credentialData.pbs_ip,
pbs_datastore: credentialData.pbs_datastore,
pbs_password: credentialData.pbs_password,
pbs_fingerprint: credentialData.pbs_fingerprint,
},
});
return result as PBSStorageCredential;
}
async getPBSCredential(serverId: number, storageName: string): Promise<PBSStorageCredential | null> {
const result = await prisma.pBSStorageCredential.findUnique({
where: {
server_id_storage_name: {
server_id: serverId,
storage_name: storageName,
},
},
});
return result as PBSStorageCredential | null;
}
async getPBSCredentialsByServer(serverId: number): Promise<PBSStorageCredential[]> {
const result = await prisma.pBSStorageCredential.findMany({
where: { server_id: serverId },
orderBy: { storage_name: 'asc' },
});
return result as PBSStorageCredential[];
}
async deletePBSCredential(serverId: number, storageName: string): Promise<PBSStorageCredential> {
const result = await prisma.pBSStorageCredential.delete({
where: {
server_id_storage_name: {
server_id: serverId,
storage_name: storageName,
},
},
});
return result as PBSStorageCredential;
}
async close(): Promise<void> {
await prisma.$disconnect(); await prisma.$disconnect();
} }
} }
@@ -306,7 +600,7 @@ class DatabaseServicePrisma {
// Singleton instance // Singleton instance
let dbInstance: DatabaseServicePrisma | null = null; let dbInstance: DatabaseServicePrisma | null = null;
export function getDatabase() { export function getDatabase(): DatabaseServicePrisma {
dbInstance ??= new DatabaseServicePrisma(); dbInstance ??= new DatabaseServicePrisma();
return dbInstance; return dbInstance;
} }

View File

@@ -1,7 +1,11 @@
import { PrismaClient } from '@prisma/client'; import 'dotenv/config'
import { PrismaClient } from '../../prisma/generated/prisma/client.ts'
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3'
const globalForPrisma = globalThis; const globalForPrisma = globalThis;
export const prisma = globalForPrisma.prisma ?? new PrismaClient(); const adapter = new PrismaBetterSqlite3({ url: process.env.DATABASE_URL });
export const prisma = globalForPrisma.prisma ?? new PrismaClient({ adapter });
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma; if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma;

View File

@@ -1,10 +1,13 @@
import { PrismaClient } from '@prisma/client'; import 'dotenv/config'
import { PrismaClient } from '../../prisma/generated/prisma/client'
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3'
const globalForPrisma = globalThis as unknown as { const globalForPrisma = globalThis as { prisma?: PrismaClient };
prisma: PrismaClient | undefined;
};
export const prisma = globalForPrisma.prisma ?? new PrismaClient({ const adapter = new PrismaBetterSqlite3({ url: process.env.DATABASE_URL! });
export const prisma: PrismaClient = globalForPrisma.prisma ?? new PrismaClient({
adapter,
log: ['warn', 'error'] log: ['warn', 'error']
}); });

View File

@@ -1,17 +1,23 @@
import { AutoSyncService } from '../services/autoSyncService.js'; import { AutoSyncService } from '../services/autoSyncService.js';
import { repositoryService } from '../services/repositoryService.ts'; import { repositoryService } from '../services/repositoryService.js';
/** @type {AutoSyncService | null} */
let autoSyncService = null; let autoSyncService = null;
let isInitialized = false; let isInitialized = false;
/** /**
* Initialize default repositories * Initialize default repositories
* @returns {Promise<void>}
*/ */
export async function initializeRepositories() { export async function initializeRepositories() {
try { try {
console.log('Initializing default repositories...'); console.log('Initializing default repositories...');
await repositoryService.initializeDefaultRepositories(); if (repositoryService && repositoryService.initializeDefaultRepositories) {
console.log('Default repositories initialized successfully'); await repositoryService.initializeDefaultRepositories();
console.log('Default repositories initialized successfully');
} else {
console.warn('Repository service not available, skipping repository initialization');
}
} catch (error) { } catch (error) {
console.error('Failed to initialize repositories:', error); console.error('Failed to initialize repositories:', error);
console.error('Error stack:', error.stack); console.error('Error stack:', error.stack);

View File

@@ -1,7 +1,22 @@
import { AutoSyncService } from '~/server/services/autoSyncService'; import { AutoSyncService } from '~/server/services/autoSyncService';
import { repositoryService } from '~/server/services/repositoryService';
let autoSyncService: AutoSyncService | null = null; let autoSyncService: AutoSyncService | null = null;
/**
* Initialize default repositories
*/
export async function initializeRepositories(): Promise<void> {
try {
console.log('Initializing default repositories...');
await repositoryService.initializeDefaultRepositories();
console.log('Default repositories initialized successfully');
} catch (error) {
console.error('Failed to initialize repositories:', error);
console.error('Error stack:', (error as Error).stack);
}
}
/** /**
* Initialize auto-sync service and schedule cron job if enabled * Initialize auto-sync service and schedule cron job if enabled
*/ */

View File

@@ -272,6 +272,12 @@ export class AutoSyncService {
console.log(`Scheduling auto-sync with cron expression: ${cronExpression}`); console.log(`Scheduling auto-sync with cron expression: ${cronExpression}`);
/** @type {any} */
const cronOptions = {
scheduled: true,
timezone: 'UTC'
};
this.cronJob = cron.schedule(cronExpression, async () => { this.cronJob = cron.schedule(cronExpression, async () => {
// Check global lock first // Check global lock first
if (globalAutoSyncLock) { if (globalAutoSyncLock) {
@@ -300,10 +306,7 @@ export class AutoSyncService {
console.log('Starting scheduled auto-sync...'); console.log('Starting scheduled auto-sync...');
await this.executeAutoSync(); await this.executeAutoSync();
}, { }, cronOptions);
scheduled: true,
timezone: 'UTC'
});
console.log('Auto-sync cron job scheduled successfully'); console.log('Auto-sync cron job scheduled successfully');
} }
@@ -373,7 +376,7 @@ export class AutoSyncService {
console.log(`Processing ${syncResult.syncedFiles.length} synced JSON files for script downloads...`); console.log(`Processing ${syncResult.syncedFiles.length} synced JSON files for script downloads...`);
// Get scripts only for the synced files // Get scripts only for the synced files
const localScriptsService = await import('./localScripts.js'); const localScriptsService = await import('./localScripts');
const syncedScripts = []; const syncedScripts = [];
for (const filename of syncResult.syncedFiles) { for (const filename of syncResult.syncedFiles) {

View File

@@ -0,0 +1,693 @@
import { getSSHExecutionService } from '../ssh-execution-service';
import { getStorageService } from './storageService';
import { getDatabase } from '../database-prisma';
import type { Server } from '~/types/server';
import type { Storage } from './storageService';
export interface BackupData {
container_id: string;
server_id: number;
hostname: string;
backup_name: string;
backup_path: string;
size?: bigint;
created_at?: Date;
storage_name: string;
storage_type: 'local' | 'storage' | 'pbs';
}
class BackupService {
/**
* Get server hostname via SSH
*/
async getServerHostname(server: Server): Promise<string> {
const sshService = getSSHExecutionService();
let hostname = '';
await new Promise<void>((resolve, reject) => {
void sshService.executeCommand(
server,
'hostname',
(data: string) => {
hostname += data;
},
(_error: string) => {
reject(new Error(`Failed to get hostname: ${_error}`));
},
(_exitCode: number) => {
if (_exitCode === 0) {
resolve();
} else {
reject(new Error(`hostname command failed with exit code ${_exitCode}`));
}
}
);
});
return hostname.trim();
}
/**
* Discover local backups in /var/lib/vz/dump/
*/
async discoverLocalBackups(server: Server, ctId: string, hostname: string): Promise<BackupData[]> {
const sshService = getSSHExecutionService();
const backups: BackupData[] = [];
// Find backup files matching pattern (with timeout)
const findCommand = `timeout 10 find /var/lib/vz/dump/ -type f -name "vzdump-lxc-${ctId}-*.tar*" 2>/dev/null`;
let findOutput = '';
try {
await Promise.race([
new Promise<void>((resolve) => {
void sshService.executeCommand(
server,
findCommand,
(data: string) => {
findOutput += data;
},
(error: string) => {
console.error('Error getting hostname:', error);
// Ignore errors - directory might not exist
resolve();
},
(exitCode: number) => {
console.error('Error getting find command:', exitCode);
resolve();
}
);
}),
new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, 15000); // 15 second timeout
})
]);
const backupPaths = findOutput.trim().split('\n').filter(path => path.trim());
// Get detailed info for each backup file
for (const backupPath of backupPaths) {
if (!backupPath.trim()) continue;
try {
// Get file size and modification time
const statCommand = `stat -c "%s|%Y|%n" "${backupPath}" 2>/dev/null || stat -f "%z|%m|%N" "${backupPath}" 2>/dev/null || echo ""`;
let statOutput = '';
await Promise.race([
new Promise<void>((resolve) => {
void sshService.executeCommand(
server,
statCommand,
(data: string) => {
statOutput += data;
},
() => resolve(),
() => resolve()
);
}),
new Promise<void>((resolve) => {
setTimeout(() => resolve(), 5000); // 5 second timeout for stat
})
]);
const statParts = statOutput.trim().split('|');
const fileName = backupPath.split('/').pop() ?? backupPath;
if (statParts.length >= 2 && statParts[0] && statParts[1]) {
const size = BigInt(statParts[0] ?? '0');
const mtime = parseInt(statParts[1] ?? '0', 10);
backups.push({
container_id: ctId,
server_id: server.id,
hostname,
backup_name: fileName,
backup_path: backupPath,
size,
created_at: mtime > 0 ? new Date(mtime * 1000) : undefined,
storage_name: 'local',
storage_type: 'local',
});
} else {
// If stat fails, still add the backup with minimal info
backups.push({
container_id: ctId,
server_id: server.id,
hostname,
backup_name: fileName,
backup_path: backupPath,
size: undefined,
created_at: undefined,
storage_name: 'local',
storage_type: 'local',
});
}
} catch (error) {
console.error('Error processing backup:', error);
// Still try to add the backup even if stat fails
const fileName = backupPath.split('/').pop() ?? backupPath;
backups.push({
container_id: ctId,
server_id: server.id,
hostname,
backup_name: fileName,
backup_path: backupPath,
size: undefined,
created_at: undefined,
storage_name: 'local',
storage_type: 'local',
});
}
}
} catch (error) {
console.error(`Error discovering local backups for CT ${ctId}:`, error);
}
return backups;
}
/**
* Discover backups in mounted storage (/mnt/pve/<storage>/dump/)
*/
async discoverStorageBackups(server: Server, storage: Storage, ctId: string, hostname: string): Promise<BackupData[]> {
const sshService = getSSHExecutionService();
const backups: BackupData[] = [];
const dumpPath = `/mnt/pve/${storage.name}/dump/`;
const findCommand = `timeout 10 find "${dumpPath}" -type f -name "vzdump-lxc-${ctId}-*.tar*" 2>/dev/null`;
let findOutput = '';
console.log(`[BackupService] Discovering storage backups for CT ${ctId} on ${storage.name}`);
try {
await Promise.race([
new Promise<void>((resolve) => {
void sshService.executeCommand(
server,
findCommand,
(data: string) => {
findOutput += data;
},
(error: string) => {
console.error('Error getting stat command:', error);
resolve();
},
(exitCode: number) => {
console.error('Error getting stat command:', exitCode);
resolve();
}
);
}),
new Promise<void>((resolve) => {
setTimeout(() => {
console.log(`[BackupService] Storage backup discovery timeout for ${storage.name}`);
resolve();
}, 15000); // 15 second timeout
})
]);
const backupPaths = findOutput.trim().split('\n').filter(path => path.trim());
console.log(`[BackupService] Found ${backupPaths.length} backup files for CT ${ctId} on storage ${storage.name}`);
// Get detailed info for each backup file
for (const backupPath of backupPaths) {
if (!backupPath.trim()) continue;
try {
const statCommand = `stat -c "%s|%Y|%n" "${backupPath}" 2>/dev/null || stat -f "%z|%m|%N" "${backupPath}" 2>/dev/null || echo ""`;
let statOutput = '';
await Promise.race([
new Promise<void>((resolve) => {
void sshService.executeCommand(
server,
statCommand,
(data: string) => {
statOutput += data;
},
() => resolve(),
() => resolve()
);
}),
new Promise<void>((resolve) => {
setTimeout(() => resolve(), 5000); // 5 second timeout for stat
})
]);
const statParts = statOutput.trim().split('|');
const fileName = backupPath.split('/').pop() ?? backupPath;
if (statParts.length >= 2 && statParts[0] && statParts[1]) {
const size = BigInt(statParts[0] ?? '0');
const mtime = parseInt(statParts[1] ?? '0', 10);
backups.push({
container_id: ctId,
server_id: server.id,
hostname,
backup_name: fileName,
backup_path: backupPath,
size,
created_at: mtime > 0 ? new Date(mtime * 1000) : undefined,
storage_name: storage.name,
storage_type: 'storage',
});
console.log(`[BackupService] Added storage backup: ${fileName} from ${storage.name}`);
} else {
// If stat fails, still add the backup with minimal info
console.log(`[BackupService] Stat failed for ${fileName}, adding backup without size/date`);
backups.push({
container_id: ctId,
server_id: server.id,
hostname,
backup_name: fileName,
backup_path: backupPath,
size: undefined,
created_at: undefined,
storage_name: storage.name,
storage_type: 'storage',
});
}
} catch (error) {
console.error(`Error processing backup ${backupPath}:`, error);
// Still try to add the backup even if stat fails
const fileName = backupPath.split('/').pop() ?? backupPath;
backups.push({
container_id: ctId,
server_id: server.id,
hostname,
backup_name: fileName,
backup_path: backupPath,
size: undefined,
created_at: undefined,
storage_name: storage.name,
storage_type: 'storage',
});
}
}
console.log(`[BackupService] Total storage backups found for CT ${ctId} on ${storage.name}: ${backups.length}`);
} catch (error) {
console.error(`Error discovering storage backups for CT ${ctId} on ${storage.name}:`, error);
}
return backups;
}
/**
* Login to PBS using stored credentials
*/
async loginToPBS(server: Server, storage: Storage): Promise<boolean> {
const db = getDatabase();
const credential = await db.getPBSCredential(server.id, storage.name);
if (!credential) {
console.log(`[BackupService] No PBS credentials found for storage ${storage.name}, skipping PBS discovery`);
return false;
}
const sshService = getSSHExecutionService();
const storageService = getStorageService();
const pbsInfo = storageService.getPBSStorageInfo(storage);
// Use IP and datastore from credentials (they override config if different)
const pbsIp = credential.pbs_ip ?? pbsInfo.pbs_ip;
const pbsDatastore = credential.pbs_datastore ?? pbsInfo.pbs_datastore;
if (!pbsIp || !pbsDatastore) {
console.log(`[BackupService] Missing PBS IP or datastore for storage ${storage.name}`);
return false;
}
// Build login command
// Format: proxmox-backup-client login --repository root@pam@<IP>:<DATASTORE>
// PBS supports PBS_PASSWORD and PBS_REPOSITORY environment variables for non-interactive login
const repository = `root@pam@${pbsIp}:${pbsDatastore}`;
// Escape password for shell safety (single quotes)
const escapedPassword = credential.pbs_password.replace(/'/g, "'\\''");
// Use PBS_PASSWORD environment variable for non-interactive authentication
// Auto-accept fingerprint by piping "y" to stdin
// PBS will use PBS_PASSWORD env var if available, avoiding interactive prompt
const fullCommand = `echo "y" | PBS_PASSWORD='${escapedPassword}' PBS_REPOSITORY='${repository}' timeout 10 proxmox-backup-client login --repository ${repository} 2>&1`;
console.log(`[BackupService] Logging into PBS: ${repository}`);
let loginOutput = '';
let loginSuccess = false;
try {
await Promise.race([
new Promise<void>((resolve) => {
void sshService.executeCommand(
server,
fullCommand,
(data: string) => {
loginOutput += data;
},
(error: string) => {
console.log(`[BackupService] PBS login error: ${error}`);
resolve();
},
(exitCode: number) => {
loginSuccess = exitCode === 0;
if (loginSuccess) {
console.log(`[BackupService] Successfully logged into PBS: ${repository}`);
} else {
console.log(`[BackupService] PBS login failed with exit code ${exitCode}`);
console.log(`[BackupService] Login output: ${loginOutput}`);
}
resolve();
}
);
}),
new Promise<void>((resolve) => {
setTimeout(() => {
console.log(`[BackupService] PBS login timeout`);
resolve();
}, 15000); // 15 second timeout
})
]);
// Check if login was successful (look for success indicators in output)
if (loginSuccess || loginOutput.includes('successfully') || loginOutput.includes('logged in')) {
return true;
}
return false;
} catch (error) {
console.error(`[BackupService] Error during PBS login:`, error);
return false;
}
}
/**
* Discover PBS backups using proxmox-backup-client
*/
async discoverPBSBackups(server: Server, storage: Storage, ctId: string, hostname: string): Promise<BackupData[]> {
const sshService = getSSHExecutionService();
const backups: BackupData[] = [];
// Login to PBS first
const loggedIn = await this.loginToPBS(server, storage);
if (!loggedIn) {
console.log(`[BackupService] Failed to login to PBS for storage ${storage.name}, skipping backup discovery`);
return backups;
}
// Get PBS credentials to build full repository string
const db = getDatabase();
const credential = await db.getPBSCredential(server.id, storage.name);
if (!credential) {
console.log(`[BackupService] No PBS credentials found for storage ${storage.name}`);
return backups;
}
const storageService = getStorageService();
const pbsInfo = storageService.getPBSStorageInfo(storage);
const pbsIp = credential.pbs_ip ?? pbsInfo.pbs_ip;
const pbsDatastore = credential.pbs_datastore ?? pbsInfo.pbs_datastore;
if (!pbsIp || !pbsDatastore) {
console.log(`[BackupService] Missing PBS IP or datastore for storage ${storage.name}`);
return backups;
}
// Build full repository string: root@pam@<IP>:<DATASTORE>
const repository = `root@pam@${pbsIp}:${pbsDatastore}`;
// Use correct command: snapshot list ct/<CT_ID> --repository <full_repo_string>
const command = `timeout 30 proxmox-backup-client snapshot list ct/${ctId} --repository ${repository} 2>&1 || echo "PBS_ERROR"`;
let output = '';
console.log(`[BackupService] Discovering PBS backups for CT ${ctId} on repository ${repository}`);
try {
// Add timeout to prevent hanging
await Promise.race([
new Promise<void>((resolve) => {
void sshService.executeCommand(
server,
command,
(data: string) => {
output += data;
},
(error: string) => {
console.log(`[BackupService] PBS command error: ${error}`);
resolve();
},
(exitCode: number) => {
console.log(`[BackupService] PBS command completed with exit code ${exitCode}`);
resolve();
}
);
}),
new Promise<void>((resolve) => {
setTimeout(() => {
console.log(`[BackupService] PBS discovery timeout, continuing...`);
resolve();
}, 35000); // 35 second timeout (command has 30s timeout, so this is a safety net)
})
]);
// Check if PBS command failed
if (output.includes('PBS_ERROR') || output.includes('error') || output.includes('Error')) {
console.log(`[BackupService] PBS discovery failed or no backups found for CT ${ctId}`);
return backups;
}
// Parse PBS snapshot list output (table format)
// Format: snapshot | size | files
// Example: ct/148/2025-10-21T19:14:55Z | 994.944 MiB | catalog.pcat1 client.log ...
const lines = output.trim().split('\n').filter(line => line.trim());
console.log(`[BackupService] Parsing ${lines.length} lines from PBS output`);
for (const line of lines) {
// Skip header lines, separators, or error messages
if (line.includes('snapshot') && line.includes('size') && line.includes('files')) {
continue; // Skip header row
}
if (line.includes('═') || line.includes('─') || line.includes('│') && (/^[│═─╞╪╡├┼┤└┴┘]+$/.exec(line))) {
continue; // Skip table separator lines
}
if (line.includes('repository') || line.includes('error') || line.includes('Error') || line.includes('PBS_ERROR')) {
continue;
}
// Parse table row - format: snapshot | size | files
// Example: │ ct/148/2025-10-21T19:14:55Z │ 994.944 MiB │ catalog.pcat1 client.log index.json pct.conf root.pxar │
const parts = line.split('│').map(p => p.trim()).filter(p => p);
if (parts.length >= 2) {
const snapshotPath = parts[0]; // e.g., "ct/148/2025-10-21T19:14:55Z"
const sizeStr = parts[1]; // e.g., "994.944 MiB"
if (!snapshotPath) {
continue; // Skip if no snapshot path
}
// Extract snapshot name (last part after /)
const snapshotParts = snapshotPath.split('/');
const snapshotName = snapshotParts[snapshotParts.length - 1] ?? snapshotPath;
if (!snapshotName) {
continue; // Skip if no snapshot name
}
// Parse date from snapshot name (format: 2025-10-21T19:14:55Z)
let createdAt: Date | undefined;
const dateMatch = /(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)/.exec(snapshotName);
if (dateMatch?.[1]) {
try {
createdAt = new Date(dateMatch[1]);
} catch (e) {
console.error('Error parsing date:', e);
// Invalid date, leave undefined
}
}
// Parse size (convert MiB/GiB to bytes)
let size: bigint | undefined;
if (sizeStr) {
const sizeMatch = /([\d.]+)\s*(MiB|GiB|KiB|B)/i.exec(sizeStr);
if (sizeMatch?.[1] && sizeMatch[2]) {
const sizeValue = parseFloat(sizeMatch[1]);
const unit = sizeMatch[2].toUpperCase();
let bytes = sizeValue;
if (unit === 'KIB') bytes = sizeValue * 1024;
else if (unit === 'MIB') bytes = sizeValue * 1024 * 1024;
else if (unit === 'GIB') bytes = sizeValue * 1024 * 1024 * 1024;
size = BigInt(Math.floor(bytes));
}
}
backups.push({
container_id: ctId,
server_id: server.id,
hostname,
backup_name: snapshotName,
backup_path: `pbs://${repository}/${snapshotPath}`,
size,
created_at: createdAt,
storage_name: storage.name,
storage_type: 'pbs',
});
}
}
console.log(`[BackupService] Found ${backups.length} PBS backups for CT ${ctId}`);
} catch (error) {
console.error(`Error discovering PBS backups for CT ${ctId}:`, error);
}
return backups;
}
/**
* Discover all backups for a container across all backup-capable storages
*/
async discoverAllBackupsForContainer(server: Server, ctId: string, hostname: string): Promise<BackupData[]> {
const allBackups: BackupData[] = [];
try {
// Get server hostname to filter storages
const serverHostname = await this.getServerHostname(server);
const normalizedHostname = serverHostname.trim().toLowerCase();
console.log(`[BackupService] Discovering backups for server ${server.name} (hostname: ${serverHostname}, normalized: ${normalizedHostname})`);
// Get all backup-capable storages (force refresh to get latest node assignments)
const storageService = getStorageService();
const allStorages = await storageService.getBackupStorages(server, true); // Force refresh
console.log(`[BackupService] Found ${allStorages.length} backup-capable storages total`);
// Filter storages by node hostname matching
const applicableStorages = allStorages.filter(storage => {
// If storage has no nodes specified, it's available on all nodes
if (!storage.nodes || storage.nodes.length === 0) {
console.log(`[BackupService] Storage ${storage.name} has no nodes specified, including it`);
return true;
}
// Normalize all nodes for comparison
const normalizedNodes = storage.nodes.map(node => node.trim().toLowerCase());
const isApplicable = normalizedNodes.includes(normalizedHostname);
if (!isApplicable) {
console.log(`[BackupService] EXCLUDING Storage ${storage.name} (nodes: ${storage.nodes.join(', ')}) - not applicable for hostname: ${serverHostname}`);
} else {
console.log(`[BackupService] INCLUDING Storage ${storage.name} (nodes: ${storage.nodes.join(', ')}) - applicable for hostname: ${serverHostname}`);
}
return isApplicable;
});
console.log(`[BackupService] Filtered to ${applicableStorages.length} applicable storages for ${serverHostname}`);
// Discover local backups
const localBackups = await this.discoverLocalBackups(server, ctId, hostname);
allBackups.push(...localBackups);
// Discover backups from each applicable storage
for (const storage of applicableStorages) {
try {
if (storage.type === 'pbs') {
// PBS storage
const pbsBackups = await this.discoverPBSBackups(server, storage, ctId, hostname);
allBackups.push(...pbsBackups);
} else {
// Regular storage (dir, nfs, etc.)
const storageBackups = await this.discoverStorageBackups(server, storage, ctId, hostname);
allBackups.push(...storageBackups);
}
} catch (error) {
console.error(`[BackupService] Error discovering backups from storage ${storage.name}:`, error);
// Continue with other storages
}
}
console.log(`[BackupService] Total backups discovered for CT ${ctId}: ${allBackups.length}`);
} catch (error) {
console.error(`Error discovering backups for container ${ctId}:`, error);
}
return allBackups;
}
/**
* Discover backups for all installed scripts with container_id
*/
async discoverAllBackups(): Promise<void> {
const db = getDatabase();
const scripts = await db.getAllInstalledScripts();
// Filter scripts that have container_id and server_id
const scriptsWithContainers = scripts.filter(
(script: any) => script.container_id && script.server_id && script.server
);
// Clear all existing backups first to ensure we start fresh
console.log('[BackupService] Clearing all existing backups before rediscovery...');
const allBackups = await db.getAllBackups();
for (const backup of allBackups) {
await db.deleteBackupsForContainer(backup.container_id, backup.server_id);
}
console.log('[BackupService] Cleared all existing backups');
for (const script of scriptsWithContainers) {
if (!script.container_id || !script.server_id || !script.server) continue;
const containerId = script.container_id;
const server = script.server as Server;
try {
// Get hostname from LXC config if available, otherwise use script name
let hostname = script.script_name ?? `CT-${script.container_id}`;
try {
const lxcConfig = await db.getLXCConfigByScriptId(script.id);
if (lxcConfig?.hostname) {
hostname = lxcConfig.hostname;
}
} catch (error) {
console.error('Error getting LXC config:', error);
// LXC config might not exist, use script name
console.debug(`No LXC config found for script ${script.id}, using script name as hostname`);
}
console.log(`[BackupService] Discovering backups for script ${script.id}, CT ${containerId} on server ${server.name}`);
// Discover backups for this container
const backups = await this.discoverAllBackupsForContainer(
server,
containerId,
hostname
);
console.log(`[BackupService] Found ${backups.length} backups for CT ${containerId} on server ${server.name}`);
// Save discovered backups
for (const backup of backups) {
await db.createOrUpdateBackup(backup);
}
} catch (error) {
console.error(`Error discovering backups for script ${script.id} (CT ${script.container_id}):`, error);
}
}
}
}
// Singleton instance
let backupServiceInstance: BackupService | null = null;
export function getBackupService(): BackupService {
backupServiceInstance ??= new BackupService();
return backupServiceInstance;
}

View File

@@ -1,6 +1,428 @@
// JavaScript wrapper for githubJsonService.ts // JavaScript wrapper for githubJsonService (for use with node server.js)
// This allows the JavaScript autoSyncService.js to import the TypeScript service import { writeFile, mkdir, readdir, readFile } from 'fs/promises';
import { join } from 'path';
import { repositoryService } from './repositoryService.js';
import { githubJsonService } from './githubJsonService.ts'; // Get environment variables
const getEnv = () => ({
REPO_BRANCH: process.env.REPO_BRANCH || 'main',
JSON_FOLDER: process.env.JSON_FOLDER || 'json',
REPO_URL: process.env.REPO_URL || 'https://github.com/community-scripts/ProxmoxVE',
GITHUB_TOKEN: process.env.GITHUB_TOKEN
});
export { githubJsonService }; class GitHubJsonService {
constructor() {
this.branch = null;
this.jsonFolder = null;
this.localJsonDirectory = null;
this.scriptCache = new Map();
}
initializeConfig() {
if (this.branch === null) {
const env = getEnv();
this.branch = env.REPO_BRANCH;
this.jsonFolder = env.JSON_FOLDER;
this.localJsonDirectory = join(process.cwd(), 'scripts', 'json');
}
}
getBaseUrl(repoUrl) {
const urlMatch = /github\.com\/([^\/]+)\/([^\/]+)/.exec(repoUrl);
if (!urlMatch) {
throw new Error(`Invalid GitHub repository URL: ${repoUrl}`);
}
const [, owner, repo] = urlMatch;
return `https://api.github.com/repos/${owner}/${repo}`;
}
extractRepoPath(repoUrl) {
const match = /github\.com\/([^\/]+)\/([^\/]+)/.exec(repoUrl);
if (!match) {
throw new Error('Invalid GitHub repository URL');
}
return `${match[1]}/${match[2]}`;
}
async fetchFromGitHub(repoUrl, endpoint) {
const baseUrl = this.getBaseUrl(repoUrl);
const env = getEnv();
const headers = {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'PVEScripts-Local/1.0',
};
if (env.GITHUB_TOKEN) {
headers.Authorization = `token ${env.GITHUB_TOKEN}`;
}
const response = await fetch(`${baseUrl}${endpoint}`, { headers });
if (!response.ok) {
if (response.status === 403) {
const error = new Error(`GitHub API rate limit exceeded. Consider setting GITHUB_TOKEN for higher limits. Status: ${response.status} ${response.statusText}`);
error.name = 'RateLimitError';
throw error;
}
throw new Error(`GitHub API error: ${response.status} ${response.statusText}`);
}
return response.json();
}
async downloadJsonFile(repoUrl, filePath) {
this.initializeConfig();
const repoPath = this.extractRepoPath(repoUrl);
const rawUrl = `https://raw.githubusercontent.com/${repoPath}/${this.branch}/${filePath}`;
const env = getEnv();
const headers = {
'User-Agent': 'PVEScripts-Local/1.0',
};
if (env.GITHUB_TOKEN) {
headers.Authorization = `token ${env.GITHUB_TOKEN}`;
}
const response = await fetch(rawUrl, { headers });
if (!response.ok) {
if (response.status === 403) {
const error = new Error(`GitHub rate limit exceeded while downloading ${filePath}. Consider setting GITHUB_TOKEN for higher limits.`);
error.name = 'RateLimitError';
throw error;
}
throw new Error(`Failed to download ${filePath}: ${response.status} ${response.statusText}`);
}
const content = await response.text();
const script = JSON.parse(content);
script.repository_url = repoUrl;
return script;
}
async getJsonFiles(repoUrl) {
this.initializeConfig();
try {
const files = await this.fetchFromGitHub(
repoUrl,
`/contents/${this.jsonFolder}?ref=${this.branch}`
);
return files.filter(file => file.name.endsWith('.json'));
} catch (error) {
console.error(`Error fetching JSON files from GitHub (${repoUrl}):`, error);
throw new Error(`Failed to fetch script files from repository: ${repoUrl}`);
}
}
async getAllScripts(repoUrl) {
try {
const jsonFiles = await this.getJsonFiles(repoUrl);
const scripts = [];
for (const file of jsonFiles) {
try {
const script = await this.downloadJsonFile(repoUrl, file.path);
scripts.push(script);
} catch (error) {
console.error(`Failed to download script ${file.name} from ${repoUrl}:`, error);
}
}
return scripts;
} catch (error) {
console.error(`Error fetching all scripts from ${repoUrl}:`, error);
throw new Error(`Failed to fetch scripts from repository: ${repoUrl}`);
}
}
async getScriptCards(repoUrl) {
try {
const scripts = await this.getAllScripts(repoUrl);
return scripts.map(script => ({
name: script.name,
slug: script.slug,
description: script.description,
logo: script.logo,
type: script.type,
updateable: script.updateable,
website: script.website,
repository_url: script.repository_url,
}));
} catch (error) {
console.error(`Error creating script cards from ${repoUrl}:`, error);
throw new Error(`Failed to create script cards from repository: ${repoUrl}`);
}
}
async getScriptBySlug(slug, repoUrl) {
try {
const localScript = await this.getScriptFromLocal(slug);
if (localScript) {
if (repoUrl && localScript.repository_url !== repoUrl) {
return null;
}
return localScript;
}
if (repoUrl) {
try {
this.initializeConfig();
const script = await this.downloadJsonFile(repoUrl, `${this.jsonFolder}/${slug}.json`);
return script;
} catch {
return null;
}
}
const enabledRepos = await repositoryService.getEnabledRepositories();
for (const repo of enabledRepos) {
try {
this.initializeConfig();
const script = await this.downloadJsonFile(repo.url, `${this.jsonFolder}/${slug}.json`);
return script;
} catch {
// Continue to next repo
}
}
return null;
} catch (error) {
console.error('Error fetching script by slug:', error);
throw new Error(`Failed to fetch script: ${slug}`);
}
}
async getScriptFromLocal(slug) {
try {
if (this.scriptCache.has(slug)) {
return this.scriptCache.get(slug);
}
this.initializeConfig();
const filePath = join(this.localJsonDirectory, `${slug}.json`);
const content = await readFile(filePath, 'utf-8');
const script = JSON.parse(content);
if (!script.repository_url) {
const env = getEnv();
script.repository_url = env.REPO_URL;
}
this.scriptCache.set(slug, script);
return script;
} catch {
return null;
}
}
async syncJsonFilesForRepo(repoUrl) {
try {
console.log(`Starting JSON sync from repository: ${repoUrl}`);
const githubFiles = await this.getJsonFiles(repoUrl);
console.log(`Found ${githubFiles.length} JSON files in repository ${repoUrl}`);
const localFiles = await this.getLocalJsonFiles();
console.log(`Found ${localFiles.length} local JSON files`);
const filesToSync = await this.findFilesToSyncForRepo(repoUrl, githubFiles, localFiles);
console.log(`Found ${filesToSync.length} files that need syncing from ${repoUrl}`);
if (filesToSync.length === 0) {
return {
success: true,
message: `All JSON files are up to date for repository: ${repoUrl}`,
count: 0,
syncedFiles: []
};
}
const syncedFiles = await this.syncSpecificFiles(repoUrl, filesToSync);
return {
success: true,
message: `Successfully synced ${syncedFiles.length} JSON files from ${repoUrl}`,
count: syncedFiles.length,
syncedFiles
};
} catch (error) {
console.error(`JSON sync failed for ${repoUrl}:`, error);
return {
success: false,
message: `Failed to sync JSON files from ${repoUrl}: ${error instanceof Error ? error.message : 'Unknown error'}`,
count: 0,
syncedFiles: []
};
}
}
async syncJsonFiles() {
try {
console.log('Starting multi-repository JSON sync...');
const enabledRepos = await repositoryService.getEnabledRepositories();
if (enabledRepos.length === 0) {
return {
success: false,
message: 'No enabled repositories found',
count: 0,
syncedFiles: []
};
}
console.log(`Found ${enabledRepos.length} enabled repositories`);
const allSyncedFiles = [];
const processedSlugs = new Set();
let totalSynced = 0;
for (const repo of enabledRepos) {
try {
console.log(`Syncing from repository: ${repo.url} (priority: ${repo.priority})`);
const result = await this.syncJsonFilesForRepo(repo.url);
if (result.success) {
const newFiles = result.syncedFiles.filter(file => {
const slug = file.replace('.json', '');
if (processedSlugs.has(slug)) {
return false;
}
processedSlugs.add(slug);
return true;
});
allSyncedFiles.push(...newFiles);
totalSynced += newFiles.length;
} else {
console.error(`Failed to sync from ${repo.url}: ${result.message}`);
}
} catch (error) {
console.error(`Error syncing from ${repo.url}:`, error);
}
}
await this.updateExistingFilesWithRepositoryUrl();
return {
success: true,
message: `Successfully synced ${totalSynced} JSON files from ${enabledRepos.length} repositories`,
count: totalSynced,
syncedFiles: allSyncedFiles
};
} catch (error) {
console.error('Multi-repository JSON sync failed:', error);
return {
success: false,
message: `Failed to sync JSON files: ${error instanceof Error ? error.message : 'Unknown error'}`,
count: 0,
syncedFiles: []
};
}
}
async updateExistingFilesWithRepositoryUrl() {
try {
this.initializeConfig();
const files = await this.getLocalJsonFiles();
const env = getEnv();
const mainRepoUrl = env.REPO_URL;
for (const file of files) {
try {
const filePath = join(this.localJsonDirectory, file);
const content = await readFile(filePath, 'utf-8');
const script = JSON.parse(content);
if (!script.repository_url) {
script.repository_url = mainRepoUrl;
await writeFile(filePath, JSON.stringify(script, null, 2), 'utf-8');
console.log(`Updated ${file} with repository_url: ${mainRepoUrl}`);
}
} catch (error) {
console.error(`Error updating ${file}:`, error);
}
}
} catch (error) {
console.error('Error updating existing files with repository_url:', error);
}
}
async getLocalJsonFiles() {
this.initializeConfig();
try {
const files = await readdir(this.localJsonDirectory);
return files.filter(f => f.endsWith('.json'));
} catch {
return [];
}
}
async findFilesToSyncForRepo(repoUrl, githubFiles, localFiles) {
const filesToSync = [];
for (const ghFile of githubFiles) {
const localFilePath = join(this.localJsonDirectory, ghFile.name);
let needsSync = false;
if (!localFiles.includes(ghFile.name)) {
needsSync = true;
} else {
try {
const content = await readFile(localFilePath, 'utf-8');
const script = JSON.parse(content);
if (!script.repository_url || script.repository_url !== repoUrl) {
needsSync = true;
}
} catch {
needsSync = true;
}
}
if (needsSync) {
filesToSync.push(ghFile);
}
}
return filesToSync;
}
async syncSpecificFiles(repoUrl, filesToSync) {
this.initializeConfig();
const syncedFiles = [];
await mkdir(this.localJsonDirectory, { recursive: true });
for (const file of filesToSync) {
try {
const script = await this.downloadJsonFile(repoUrl, file.path);
const filename = `${script.slug}.json`;
const filePath = join(this.localJsonDirectory, filename);
script.repository_url = repoUrl;
await writeFile(filePath, JSON.stringify(script, null, 2), 'utf-8');
syncedFiles.push(filename);
this.scriptCache.delete(script.slug);
} catch (error) {
console.error(`Failed to sync ${file.name} from ${repoUrl}:`, error);
}
}
return syncedFiles;
}
}
// Singleton instance
export const githubJsonService = new GitHubJsonService();

View File

@@ -2,7 +2,7 @@ import { writeFile, mkdir, readdir, readFile } from 'fs/promises';
import { join } from 'path'; import { join } from 'path';
import { env } from '../../env.js'; import { env } from '../../env.js';
import type { Script, ScriptCard, GitHubFile } from '../../types/script'; import type { Script, ScriptCard, GitHubFile } from '../../types/script';
import { repositoryService } from './repositoryService.ts'; import { repositoryService } from './repositoryService';
export class GitHubJsonService { export class GitHubJsonService {
private branch: string | null = null; private branch: string | null = null;
@@ -64,7 +64,8 @@ export class GitHubJsonService {
throw new Error(`GitHub API error: ${response.status} ${response.statusText}`); throw new Error(`GitHub API error: ${response.status} ${response.statusText}`);
} }
return response.json() as Promise<T>; const data = await response.json();
return data as T;
} }
private async downloadJsonFile(repoUrl: string, filePath: string): Promise<Script> { private async downloadJsonFile(repoUrl: string, filePath: string): Promise<Script> {
@@ -214,9 +215,7 @@ export class GitHubJsonService {
const script = JSON.parse(content) as Script; const script = JSON.parse(content) as Script;
// If script doesn't have repository_url, set it to main repo (for backward compatibility) // If script doesn't have repository_url, set it to main repo (for backward compatibility)
if (!script.repository_url) { script.repository_url ??= env.REPO_URL ?? 'https://github.com/community-scripts/ProxmoxVE';
script.repository_url = env.REPO_URL ?? 'https://github.com/community-scripts/ProxmoxVE';
}
// Cache the script // Cache the script
this.scriptCache.set(slug, script); this.scriptCache.set(slug, script);
@@ -397,7 +396,6 @@ export class GitHubJsonService {
const filesToSync: GitHubFile[] = []; const filesToSync: GitHubFile[] = [];
for (const ghFile of githubFiles) { for (const ghFile of githubFiles) {
const slug = ghFile.name.replace('.json', '');
const localFilePath = join(this.localJsonDirectory!, ghFile.name); const localFilePath = join(this.localJsonDirectory!, ghFile.name);
let needsSync = false; let needsSync = false;

View File

@@ -1,6 +0,0 @@
// JavaScript wrapper for localScripts.ts
// This allows the JavaScript autoSyncService.js to import the TypeScript service
import { localScriptsService } from './localScripts.ts';
export { localScriptsService };

View File

@@ -1,3 +1,4 @@
import { readFile, readdir, writeFile, mkdir } from 'fs/promises'; import { readFile, readdir, writeFile, mkdir } from 'fs/promises';
import { join } from 'path'; import { join } from 'path';
import type { Script, ScriptCard } from '~/types/script'; import type { Script, ScriptCard } from '~/types/script';
@@ -95,7 +96,7 @@ export class LocalScriptsService {
let foundRepo: string | null = null; let foundRepo: string | null = null;
for (const repo of enabledRepos) { for (const repo of enabledRepos) {
try { try {
const { githubJsonService } = await import('./githubJsonService.js'); const { githubJsonService } = await import('./githubJsonService');
const repoScript = await githubJsonService.getScriptBySlug(slug, repo.url); const repoScript = await githubJsonService.getScriptBySlug(slug, repo.url);
if (repoScript) { if (repoScript) {
foundRepo = repo.url; foundRepo = repo.url;

View File

@@ -0,0 +1,216 @@
// JavaScript wrapper for repositoryService (for use with node server.js)
import { prisma } from '../db.js';
class RepositoryService {
/**
* Initialize default repositories if they don't exist
*/
async initializeDefaultRepositories() {
const mainRepoUrl = 'https://github.com/community-scripts/ProxmoxVE';
const devRepoUrl = 'https://github.com/community-scripts/ProxmoxVED';
// Check if repositories already exist
const existingRepos = await prisma.repository.findMany({
where: {
url: {
in: [mainRepoUrl, devRepoUrl]
}
}
});
const existingUrls = new Set(existingRepos.map((r) => r.url));
// Create main repo if it doesn't exist
if (!existingUrls.has(mainRepoUrl)) {
await prisma.repository.create({
data: {
url: mainRepoUrl,
enabled: true,
is_default: true,
is_removable: false,
priority: 1
}
});
console.log('Initialized main repository:', mainRepoUrl);
}
// Create dev repo if it doesn't exist
if (!existingUrls.has(devRepoUrl)) {
await prisma.repository.create({
data: {
url: devRepoUrl,
enabled: false,
is_default: true,
is_removable: false,
priority: 2
}
});
console.log('Initialized dev repository:', devRepoUrl);
}
}
/**
* Get all repositories, sorted by priority
*/
async getAllRepositories() {
return await prisma.repository.findMany({
orderBy: [
{ priority: 'asc' },
{ created_at: 'asc' }
]
});
}
/**
* Get enabled repositories, sorted by priority
*/
async getEnabledRepositories() {
return await prisma.repository.findMany({
where: {
enabled: true
},
orderBy: [
{ priority: 'asc' },
{ created_at: 'asc' }
]
});
}
/**
* Get repository by URL
*/
async getRepositoryByUrl(url) {
return await prisma.repository.findUnique({
where: { url }
});
}
/**
* Create a new repository
*/
async createRepository(data) {
// Validate GitHub URL
if (!data.url.match(/^https:\/\/github\.com\/[^\/]+\/[^\/]+$/)) {
throw new Error('Invalid GitHub repository URL. Format: https://github.com/owner/repo');
}
// Check for duplicates
const existing = await this.getRepositoryByUrl(data.url);
if (existing) {
throw new Error('Repository already exists');
}
// Get max priority for user-added repos
const maxPriority = await prisma.repository.aggregate({
_max: {
priority: true
}
});
return await prisma.repository.create({
data: {
url: data.url,
enabled: data.enabled ?? true,
is_default: false,
is_removable: true,
priority: data.priority ?? (maxPriority._max.priority ?? 0) + 1
}
});
}
/**
* Update repository
*/
async updateRepository(id, data) {
// If updating URL, validate it
if (data.url) {
if (!data.url.match(/^https:\/\/github\.com\/[^\/]+\/[^\/]+$/)) {
throw new Error('Invalid GitHub repository URL. Format: https://github.com/owner/repo');
}
// Check for duplicates (excluding current repo)
const existing = await prisma.repository.findFirst({
where: {
url: data.url,
id: { not: id }
}
});
if (existing) {
throw new Error('Repository URL already exists');
}
}
return await prisma.repository.update({
where: { id },
data
});
}
/**
* Delete repository and associated JSON files
*/
async deleteRepository(id) {
const repo = await prisma.repository.findUnique({
where: { id }
});
if (!repo) {
throw new Error('Repository not found');
}
if (!repo.is_removable) {
throw new Error('Cannot delete default repository');
}
// Delete associated JSON files
await this.deleteRepositoryJsonFiles(repo.url);
// Delete repository
await prisma.repository.delete({
where: { id }
});
return { success: true };
}
/**
* Delete all JSON files associated with a repository
*/
async deleteRepositoryJsonFiles(repoUrl) {
const { readdir, unlink, readFile } = await import('fs/promises');
const { join } = await import('path');
const jsonDirectory = join(process.cwd(), 'scripts', 'json');
try {
const files = await readdir(jsonDirectory);
for (const file of files) {
if (!file.endsWith('.json')) continue;
try {
const filePath = join(jsonDirectory, file);
const content = await readFile(filePath, 'utf-8');
const script = JSON.parse(content);
// If script has repository_url matching the repo, delete it
if (script.repository_url === repoUrl) {
await unlink(filePath);
console.log(`Deleted JSON file: ${file} (from repository: ${repoUrl})`);
}
} catch (error) {
// Skip files that can't be read or parsed
console.error(`Error processing file ${file}:`, error);
}
}
} catch (error) {
// Directory might not exist, which is fine
if (error.code !== 'ENOENT') {
console.error('Error deleting repository JSON files:', error);
}
}
}
}
// Singleton instance
export const repositoryService = new RepositoryService();

View File

@@ -1,4 +1,5 @@
import { prisma } from '../db.ts'; /* eslint-disable @typescript-eslint/prefer-regexp-exec */
import { prisma } from '../db';
export class RepositoryService { export class RepositoryService {
/** /**
@@ -17,7 +18,7 @@ export class RepositoryService {
} }
}); });
const existingUrls = new Set(existingRepos.map(r => r.url)); const existingUrls = new Set(existingRepos.map((r: { url: string }) => r.url));
// Create main repo if it doesn't exist // Create main repo if it doesn't exist
if (!existingUrls.has(mainRepoUrl)) { if (!existingUrls.has(mainRepoUrl)) {

View File

@@ -0,0 +1,563 @@
/* eslint-disable @typescript-eslint/no-floating-promises, @typescript-eslint/prefer-optional-chain, @typescript-eslint/prefer-nullish-coalescing, @typescript-eslint/prefer-regexp-exec, @typescript-eslint/no-empty-function */
import { getSSHExecutionService } from '../ssh-execution-service';
import { getBackupService } from './backupService';
import { getStorageService } from './storageService';
import { getDatabase } from '../database-prisma';
import type { Server } from '~/types/server';
import type { Storage } from './storageService';
import { writeFile } from 'fs/promises';
import { join } from 'path';
export interface RestoreProgress {
step: string;
message: string;
}
export interface RestoreResult {
success: boolean;
error?: string;
progress?: RestoreProgress[];
}
class RestoreService {
/**
* Get rootfs storage from LXC config or installed scripts database
*/
async getRootfsStorage(server: Server, ctId: string): Promise<string | null> {
const sshService = getSSHExecutionService();
const db = getDatabase();
const configPath = `/etc/pve/lxc/${ctId}.conf`;
const readCommand = `cat "${configPath}" 2>/dev/null || echo ""`;
let rawConfig = '';
try {
// Try to read config file (container might not exist, so don't fail on error)
await new Promise<void>((resolve) => {
sshService.executeCommand(
server,
readCommand,
(data: string) => {
rawConfig += data;
},
() => resolve(), // Don't fail on error
() => resolve() // Always resolve
);
});
// If we got config content, parse it
if (rawConfig.trim()) {
// Parse rootfs line: rootfs: PROX2-STORAGE2:vm-148-disk-0,size=4G
const lines = rawConfig.split('\n');
for (const line of lines) {
const trimmed = line.trim();
if (trimmed.startsWith('rootfs:')) {
const match = trimmed.match(/^rootfs:\s*([^:]+):/);
if (match && match[1]) {
return match[1].trim();
}
}
}
}
// If config file doesn't exist or doesn't have rootfs, try to get from installed scripts database
const installedScripts = await db.getAllInstalledScripts();
const script = installedScripts.find((s: any) => s.container_id === ctId && s.server_id === server.id);
if (script) {
// Try to get LXC config from database
const lxcConfig = await db.getLXCConfigByScriptId(script.id);
if (lxcConfig?.rootfs_storage) {
// Extract storage from rootfs_storage format: "STORAGE:vm-148-disk-0"
const match = lxcConfig.rootfs_storage.match(/^([^:]+):/);
if (match && match[1]) {
return match[1].trim();
}
}
}
return null;
} catch {
// Try fallback to database
try {
const installedScripts = await db.getAllInstalledScripts();
const script = installedScripts.find((s: any) => s.container_id === ctId && s.server_id === server.id);
if (script) {
const lxcConfig = await db.getLXCConfigByScriptId(script.id);
if (lxcConfig?.rootfs_storage) {
const match = lxcConfig.rootfs_storage.match(/^([^:]+):/);
if (match && match[1]) {
return match[1].trim();
}
}
}
} catch {
// Ignore database error
}
return null;
}
}
/**
* Stop container (continue if already stopped)
*/
async stopContainer(server: Server, ctId: string): Promise<void> {
const sshService = getSSHExecutionService();
const command = `pct stop ${ctId} 2>&1 || true`; // Continue even if already stopped
await new Promise<void>((resolve) => {
sshService.executeCommand(
server,
command,
() => {},
() => resolve(),
() => resolve() // Always resolve, don't fail if already stopped
);
});
}
/**
* Destroy container
*/
async destroyContainer(server: Server, ctId: string): Promise<void> {
const sshService = getSSHExecutionService();
const command = `pct destroy ${ctId} 2>&1`;
let output = '';
let exitCode = 0;
await new Promise<void>((resolve, reject) => {
sshService.executeCommand(
server,
command,
(data: string) => {
output += data;
},
(error: string) => {
// Check if error is about container not existing
if (error.includes('does not exist') || error.includes('not found')) {
resolve(); // Container doesn't exist, that's fine
} else {
reject(new Error(`Destroy failed: ${error}`));
}
},
(code: number) => {
exitCode = code;
if (exitCode === 0) {
resolve();
} else {
// Check if error is about container not existing
if (output.includes('does not exist') || output.includes('not found') || output.includes('No such file')) {
resolve(); // Container doesn't exist, that's fine
} else {
reject(new Error(`Destroy failed with exit code ${exitCode}: ${output}`));
}
}
}
);
});
}
/**
* Restore from local/storage backup
*/
async restoreLocalBackup(
server: Server,
ctId: string,
backupPath: string,
storage: string
): Promise<void> {
const sshService = getSSHExecutionService();
const command = `pct restore ${ctId} "${backupPath}" --storage=${storage}`;
let output = '';
let exitCode = 0;
await new Promise<void>((resolve, reject) => {
sshService.executeCommand(
server,
command,
(data: string) => {
output += data;
},
(error: string) => {
reject(new Error(`Restore failed: ${error}`));
},
(code: number) => {
exitCode = code;
if (exitCode === 0) {
resolve();
} else {
reject(new Error(`Restore failed with exit code ${exitCode}: ${output}`));
}
}
);
});
}
/**
* Restore from PBS backup
*/
async restorePBSBackup(
server: Server,
storage: Storage,
ctId: string,
snapshotPath: string,
storageName: string,
onProgress?: (step: string, message: string) => Promise<void>
): Promise<void> {
const backupService = getBackupService();
const sshService = getSSHExecutionService();
const db = getDatabase();
// Get PBS credentials
const credential = await db.getPBSCredential(server.id, storage.name);
if (!credential) {
throw new Error(`No PBS credentials found for storage ${storage.name}`);
}
const storageService = getStorageService();
const pbsInfo = storageService.getPBSStorageInfo(storage);
const pbsIp = credential.pbs_ip || pbsInfo.pbs_ip;
const pbsDatastore = credential.pbs_datastore || pbsInfo.pbs_datastore;
if (!pbsIp || !pbsDatastore) {
throw new Error(`Missing PBS IP or datastore for storage ${storage.name}`);
}
const repository = `root@pam@${pbsIp}:${pbsDatastore}`;
// Extract snapshot name from path (e.g., "2025-10-21T19:14:55Z" from "ct/148/2025-10-21T19:14:55Z")
const snapshotParts = snapshotPath.split('/');
const snapshotName = snapshotParts[snapshotParts.length - 1] || snapshotPath;
// Replace colons with underscores for file paths (tar doesn't like colons in filenames)
const snapshotNameForPath = snapshotName.replace(/:/g, '_');
// Determine file extension - try common extensions
let downloadedPath = '';
let downloadSuccess = false;
// Login to PBS first
if (onProgress) await onProgress('pbs_login', 'Logging into PBS...');
const loggedIn = await backupService.loginToPBS(server, storage);
if (!loggedIn) {
throw new Error(`Failed to login to PBS for storage ${storage.name}`);
}
// Download backup from PBS
// proxmox-backup-client restore outputs a folder, not a file
if (onProgress) await onProgress('pbs_download', 'Downloading backup from PBS...');
// Target folder for PBS restore (without extension)
// Use sanitized snapshot name (colons replaced with underscores) for file paths
const targetFolder = `/var/lib/vz/dump/vzdump-lxc-${ctId}-${snapshotNameForPath}`;
const targetTar = `${targetFolder}.tar`;
// Use PBS_PASSWORD env var and add timeout for long downloads
const escapedPassword = credential.pbs_password.replace(/'/g, "'\\''");
const restoreCommand = `PBS_PASSWORD='${escapedPassword}' PBS_REPOSITORY='${repository}' timeout 300 proxmox-backup-client restore "${snapshotPath}" root.pxar "${targetFolder}" --repository '${repository}' 2>&1`;
let output = '';
let exitCode = 0;
try {
// Download from PBS (creates a folder)
await Promise.race([
new Promise<void>((resolve, reject) => {
sshService.executeCommand(
server,
restoreCommand,
(data: string) => {
output += data;
},
(error: string) => {
reject(new Error(`Download failed: ${error}`));
},
(code: number) => {
exitCode = code;
if (exitCode === 0) {
resolve();
} else {
reject(new Error(`Download failed with exit code ${exitCode}: ${output}`));
}
}
);
}),
new Promise<void>((resolve, reject) => {
setTimeout(() => {
reject(new Error('Download timeout after 5 minutes'));
}, 300000); // 5 minute timeout
})
]);
// Check if folder exists
const checkCommand = `test -d "${targetFolder}" && echo "exists" || echo "notfound"`;
let checkOutput = '';
await new Promise<void>((resolve) => {
sshService.executeCommand(
server,
checkCommand,
(data: string) => {
checkOutput += data;
},
() => resolve(),
() => resolve()
);
});
if (!checkOutput.includes('exists')) {
throw new Error(`Downloaded folder ${targetFolder} does not exist`);
}
// Pack the folder into a tar file
if (onProgress) await onProgress('pbs_pack', 'Packing backup folder...');
// Use -C to change to the folder directory, then pack all contents (.) into the tar file
const packCommand = `tar -cf "${targetTar}" -C "${targetFolder}" . 2>&1`;
let packOutput = '';
let packExitCode = 0;
await Promise.race([
new Promise<void>((resolve, reject) => {
sshService.executeCommand(
server,
packCommand,
(data: string) => {
packOutput += data;
},
(error: string) => {
reject(new Error(`Pack failed: ${error}`));
},
(code: number) => {
packExitCode = code;
if (packExitCode === 0) {
resolve();
} else {
reject(new Error(`Pack failed with exit code ${packExitCode}: ${packOutput}`));
}
}
);
}),
new Promise<void>((resolve, reject) => {
setTimeout(() => {
reject(new Error('Pack timeout after 2 minutes'));
}, 120000); // 2 minute timeout for packing
})
]);
// Check if tar file exists
const checkTarCommand = `test -f "${targetTar}" && echo "exists" || echo "notfound"`;
let checkTarOutput = '';
await new Promise<void>((resolve) => {
sshService.executeCommand(
server,
checkTarCommand,
(data: string) => {
checkTarOutput += data;
},
() => resolve(),
() => resolve()
);
});
if (!checkTarOutput.includes('exists')) {
throw new Error(`Packed tar file ${targetTar} does not exist`);
}
downloadedPath = targetTar;
downloadSuccess = true;
} catch (error) {
throw error;
}
if (!downloadSuccess || !downloadedPath) {
throw new Error(`Failed to download and pack backup from PBS`);
}
// Restore from packed tar file
if (onProgress) await onProgress('restoring', 'Restoring container...');
try {
await this.restoreLocalBackup(server, ctId, downloadedPath, storageName);
} finally {
// Cleanup: delete downloaded folder and tar file
if (onProgress) await onProgress('cleanup', 'Cleaning up temporary files...');
const cleanupCommand = `rm -rf "${targetFolder}" "${targetTar}" 2>&1 || true`;
sshService.executeCommand(
server,
cleanupCommand,
() => {},
() => {},
() => {}
);
}
}
/**
* Execute full restore flow
*/
async executeRestore(
backupId: number,
containerId: string,
serverId: number,
onProgress?: (progress: RestoreProgress) => void
): Promise<RestoreResult> {
const progress: RestoreProgress[] = [];
const logPath = join(process.cwd(), 'restore.log');
// Clear log file at start of restore
const clearLogFile = async () => {
try {
await writeFile(logPath, '', 'utf-8');
} catch {
// Ignore log file errors
}
};
// Write progress to log file
const writeProgressToLog = async (message: string) => {
try {
const logLine = `${message}\n`;
await writeFile(logPath, logLine, { flag: 'a', encoding: 'utf-8' });
} catch {
// Ignore log file errors
}
};
const addProgress = async (step: string, message: string) => {
const p = { step, message };
progress.push(p);
// Write to log file (just the message, without step prefix)
await writeProgressToLog(message);
// Call callback if provided
if (onProgress) {
onProgress(p);
}
};
try {
// Clear log file at start
await clearLogFile();
const db = getDatabase();
const sshService = getSSHExecutionService();
await addProgress('starting', 'Starting restore...');
// Get backup details
const backup = await db.getBackupById(backupId);
if (!backup) {
throw new Error(`Backup with ID ${backupId} not found`);
}
// Get server details
const serverData = await db.getServerById(serverId);
if (!serverData) {
throw new Error(`Server with ID ${serverId} not found`);
}
// Cast to Server type (Prisma returns nullable fields as null, Server uses undefined)
const server = serverData as unknown as Server;
// Get rootfs storage
await addProgress('reading_config', 'Reading container configuration...');
const rootfsStorage = await this.getRootfsStorage(server, containerId);
if (!rootfsStorage) {
// Try to check if container exists, if not we can proceed without stopping/destroying
const checkCommand = `pct list ${containerId} 2>&1 | grep -q "^${containerId}" && echo "exists" || echo "notfound"`;
let checkOutput = '';
await new Promise<void>((resolve) => {
sshService.executeCommand(
server,
checkCommand,
(data: string) => {
checkOutput += data;
},
() => resolve(),
() => resolve()
);
});
if (checkOutput.includes('notfound')) {
// Container doesn't exist, we can't determine storage - need user input or use default
throw new Error(`Container ${containerId} does not exist and storage could not be determined. Please ensure the container exists or specify the storage manually.`);
}
throw new Error(`Could not determine rootfs storage for container ${containerId}. Please ensure the container exists and has a valid configuration.`);
}
// Try to stop and destroy container - if it doesn't exist, continue anyway
await addProgress('stopping', 'Stopping container...');
try {
await this.stopContainer(server, containerId);
} catch {
// Continue even if stop fails
}
// Try to destroy container - if it doesn't exist, continue anyway
await addProgress('destroying', 'Destroying container...');
try {
await this.destroyContainer(server, containerId);
} catch {
// Container might not exist, which is fine - continue with restore
await addProgress('skipping', 'Container does not exist or already destroyed, continuing...');
}
// Restore based on backup type
if (backup.storage_type === 'pbs') {
// Get storage info for PBS
const storageService = getStorageService();
const storages = await storageService.getStorages(server, false);
const storage = storages.find(s => s.name === backup.storage_name);
if (!storage) {
throw new Error(`Storage ${backup.storage_name} not found`);
}
// Parse snapshot path from backup_path (format: pbs://root@pam@IP:DATASTORE/ct/148/2025-10-21T19:14:55Z)
const snapshotPathMatch = backup.backup_path.match(/pbs:\/\/[^/]+\/(.+)$/);
if (!snapshotPathMatch || !snapshotPathMatch[1]) {
throw new Error(`Invalid PBS backup path format: ${backup.backup_path}`);
}
const snapshotPath = snapshotPathMatch[1];
await this.restorePBSBackup(server, storage, containerId, snapshotPath, rootfsStorage, async (step, message) => {
await addProgress(step, message);
});
} else {
// Local or storage backup
await addProgress('restoring', 'Restoring container...');
await this.restoreLocalBackup(server, containerId, backup.backup_path, rootfsStorage);
}
await addProgress('complete', 'Restore completed successfully');
return {
success: true,
progress,
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
await addProgress('error', `Error: ${errorMessage}`);
return {
success: false,
error: errorMessage,
progress,
};
}
}
}
// Singleton instance
let restoreServiceInstance: RestoreService | null = null;
export function getRestoreService(): RestoreService {
if (!restoreServiceInstance) {
restoreServiceInstance = new RestoreService();
}
return restoreServiceInstance;
}

View File

@@ -4,21 +4,23 @@ import { writeFile, mkdir, access, readFile, unlink } from 'fs/promises';
export class ScriptDownloaderService { export class ScriptDownloaderService {
constructor() { constructor() {
this.scriptsDirectory = null; /** @type {string} */
this.repoUrl = null; this.scriptsDirectory = join(process.cwd(), 'scripts');
/** @type {string} */
this.repoUrl = process.env.REPO_URL || 'https://github.com/community-scripts/ProxmoxVE';
} }
initializeConfig() { initializeConfig() {
if (this.scriptsDirectory === null) { // Re-initialize if needed (for environment changes)
this.scriptsDirectory = join(process.cwd(), 'scripts'); this.scriptsDirectory = join(process.cwd(), 'scripts');
// Get REPO_URL from environment or use default this.repoUrl = process.env.REPO_URL || 'https://github.com/community-scripts/ProxmoxVE';
this.repoUrl = process.env.REPO_URL || 'https://github.com/community-scripts/ProxmoxVE';
}
} }
/** /**
* Validates that a directory path doesn't contain nested directories with the same name * Validates that a directory path doesn't contain nested directories with the same name
* (e.g., prevents ct/ct or install/install) * (e.g., prevents ct/ct or install/install)
* @param {string} dirPath - The directory path to validate
* @returns {boolean}
*/ */
validateDirectoryPath(dirPath) { validateDirectoryPath(dirPath) {
const normalizedPath = dirPath.replace(/\\/g, '/'); const normalizedPath = dirPath.replace(/\\/g, '/');
@@ -36,6 +38,9 @@ export class ScriptDownloaderService {
/** /**
* Validates that finalTargetDir doesn't contain nested directory names like ct/ct or install/install * Validates that finalTargetDir doesn't contain nested directory names like ct/ct or install/install
* @param {string} targetDir - The base target directory
* @param {string} finalTargetDir - The final target directory to validate
* @returns {string}
*/ */
validateTargetDir(targetDir, finalTargetDir) { validateTargetDir(targetDir, finalTargetDir) {
// Check if finalTargetDir contains nested directory names // Check if finalTargetDir contains nested directory names
@@ -53,6 +58,11 @@ export class ScriptDownloaderService {
return finalTargetDir; return finalTargetDir;
} }
/**
* Ensure a directory exists, creating it if necessary
* @param {string} dirPath - The directory path to ensure exists
* @returns {Promise<void>}
*/
async ensureDirectoryExists(dirPath) { async ensureDirectoryExists(dirPath) {
// Validate the directory path to prevent nested directories with the same name // Validate the directory path to prevent nested directories with the same name
this.validateDirectoryPath(dirPath); this.validateDirectoryPath(dirPath);
@@ -61,7 +71,7 @@ export class ScriptDownloaderService {
console.log(`[Directory Creation] Ensuring directory exists: ${dirPath}`); console.log(`[Directory Creation] Ensuring directory exists: ${dirPath}`);
await mkdir(dirPath, { recursive: true }); await mkdir(dirPath, { recursive: true });
console.log(`[Directory Creation] Directory created/verified: ${dirPath}`); console.log(`[Directory Creation] Directory created/verified: ${dirPath}`);
} catch (error) { } catch (/** @type {any} */ error) {
if (error.code !== 'EEXIST') { if (error.code !== 'EEXIST') {
console.error(`[Directory Creation] Error creating directory ${dirPath}:`, error.message); console.error(`[Directory Creation] Error creating directory ${dirPath}:`, error.message);
throw error; throw error;
@@ -71,6 +81,11 @@ export class ScriptDownloaderService {
} }
} }
/**
* Extract repository path from GitHub URL
* @param {string} repoUrl - The GitHub repository URL
* @returns {string}
*/
extractRepoPath(repoUrl) { extractRepoPath(repoUrl) {
const match = /github\.com\/([^\/]+)\/([^\/]+)/.exec(repoUrl); const match = /github\.com\/([^\/]+)\/([^\/]+)/.exec(repoUrl);
if (!match) { if (!match) {
@@ -79,6 +94,13 @@ export class ScriptDownloaderService {
return `${match[1]}/${match[2]}`; return `${match[1]}/${match[2]}`;
} }
/**
* Download a file from GitHub
* @param {string} repoUrl - The GitHub repository URL
* @param {string} filePath - The file path within the repository
* @param {string} [branch] - The branch to download from
* @returns {Promise<string>}
*/
async downloadFileFromGitHub(repoUrl, filePath, branch = 'main') { async downloadFileFromGitHub(repoUrl, filePath, branch = 'main') {
this.initializeConfig(); this.initializeConfig();
if (!repoUrl) { if (!repoUrl) {
@@ -88,6 +110,7 @@ export class ScriptDownloaderService {
const repoPath = this.extractRepoPath(repoUrl); const repoPath = this.extractRepoPath(repoUrl);
const url = `https://raw.githubusercontent.com/${repoPath}/${branch}/${filePath}`; const url = `https://raw.githubusercontent.com/${repoPath}/${branch}/${filePath}`;
/** @type {Record<string, string>} */
const headers = { const headers = {
'User-Agent': 'PVEScripts-Local/1.0', 'User-Agent': 'PVEScripts-Local/1.0',
}; };
@@ -106,6 +129,11 @@ export class ScriptDownloaderService {
return response.text(); return response.text();
} }
/**
* Get repository URL for a script
* @param {import('~/types/script').Script} script - The script object
* @returns {string}
*/
getRepoUrlForScript(script) { getRepoUrlForScript(script) {
// Use repository_url from script if available, otherwise fallback to env or default // Use repository_url from script if available, otherwise fallback to env or default
if (script.repository_url) { if (script.repository_url) {
@@ -115,6 +143,11 @@ export class ScriptDownloaderService {
return this.repoUrl; return this.repoUrl;
} }
/**
* Modify script content to use local paths
* @param {string} content - The script content
* @returns {string}
*/
modifyScriptContent(content) { modifyScriptContent(content) {
// Replace the build.func source line // Replace the build.func source line
const oldPattern = /source <\(curl -fsSL https:\/\/raw\.githubusercontent\.com\/community-scripts\/ProxmoxVE\/main\/misc\/build\.func\)/g; const oldPattern = /source <\(curl -fsSL https:\/\/raw\.githubusercontent\.com\/community-scripts\/ProxmoxVE\/main\/misc\/build\.func\)/g;
@@ -123,9 +156,15 @@ export class ScriptDownloaderService {
return content.replace(oldPattern, newPattern); return content.replace(oldPattern, newPattern);
} }
/**
* Load a script by downloading its files
* @param {import('~/types/script').Script} script - The script to load
* @returns {Promise<{success: boolean, message: string, files: string[], error?: string}>}
*/
async loadScript(script) { async loadScript(script) {
this.initializeConfig(); this.initializeConfig();
try { try {
/** @type {string[]} */
const files = []; const files = [];
const repoUrl = this.getRepoUrlForScript(script); const repoUrl = this.getRepoUrlForScript(script);
const branch = process.env.REPO_BRANCH || 'main'; const branch = process.env.REPO_BRANCH || 'main';
@@ -266,6 +305,11 @@ export class ScriptDownloaderService {
} }
} }
/**
* Check if a script is downloaded
* @param {import('~/types/script').Script} script - The script to check
* @returns {Promise<boolean>}
*/
async isScriptDownloaded(script) { async isScriptDownloaded(script) {
if (!script.install_methods?.length) return false; if (!script.install_methods?.length) return false;
@@ -318,6 +362,11 @@ export class ScriptDownloaderService {
return true; return true;
} }
/**
* Check which script files exist locally
* @param {import('~/types/script').Script} script - The script to check
* @returns {Promise<{ctExists: boolean, installExists: boolean, files: string[]}>}
*/
async checkScriptExists(script) { async checkScriptExists(script) {
this.initializeConfig(); this.initializeConfig();
const files = []; const files = [];
@@ -416,6 +465,11 @@ export class ScriptDownloaderService {
} }
} }
/**
* Delete a script's local files
* @param {import('~/types/script').Script} script - The script to delete
* @returns {Promise<{success: boolean, message: string, deletedFiles: string[]}>}
*/
async deleteScript(script) { async deleteScript(script) {
this.initializeConfig(); this.initializeConfig();
const deletedFiles = []; const deletedFiles = [];
@@ -467,8 +521,14 @@ export class ScriptDownloaderService {
} }
} }
/**
* Compare local script content with remote
* @param {import('~/types/script').Script} script - The script to compare
* @returns {Promise<{hasDifferences: boolean, differences: string[], error?: string}>}
*/
async compareScriptContent(script) { async compareScriptContent(script) {
this.initializeConfig(); this.initializeConfig();
/** @type {string[]} */
const differences = []; const differences = [];
let hasDifferences = false; let hasDifferences = false;
const repoUrl = this.getRepoUrlForScript(script); const repoUrl = this.getRepoUrlForScript(script);
@@ -519,13 +579,16 @@ export class ScriptDownloaderService {
comparisonPromises.push( comparisonPromises.push(
this.compareSingleFile(script, scriptPath, `${finalTargetDir}/${fileName}`) this.compareSingleFile(script, scriptPath, `${finalTargetDir}/${fileName}`)
.then(result => { .then(result => {
if (result.error) {
console.error(`[Comparison] Error comparing ${result.filePath}: ${result.error}`);
}
if (result.hasDifferences) { if (result.hasDifferences) {
hasDifferences = true; hasDifferences = true;
differences.push(result.filePath); differences.push(result.filePath);
} }
}) })
.catch(() => { .catch((error) => {
// Don't add to differences if there's an error reading files console.error(`[Comparison] Promise error for ${scriptPath}:`, error);
}) })
); );
} }
@@ -541,13 +604,16 @@ export class ScriptDownloaderService {
comparisonPromises.push( comparisonPromises.push(
this.compareSingleFile(script, installScriptPath, installScriptPath) this.compareSingleFile(script, installScriptPath, installScriptPath)
.then(result => { .then(result => {
if (result.error) {
console.error(`[Comparison] Error comparing ${result.filePath}: ${result.error}`);
}
if (result.hasDifferences) { if (result.hasDifferences) {
hasDifferences = true; hasDifferences = true;
differences.push(result.filePath); differences.push(result.filePath);
} }
}) })
.catch(() => { .catch((error) => {
// Don't add to differences if there's an error reading files console.error(`[Comparison] Promise error for ${installScriptPath}:`, error);
}) })
); );
} }
@@ -567,13 +633,16 @@ export class ScriptDownloaderService {
comparisonPromises.push( comparisonPromises.push(
this.compareSingleFile(script, alpineInstallScriptPath, alpineInstallScriptPath) this.compareSingleFile(script, alpineInstallScriptPath, alpineInstallScriptPath)
.then(result => { .then(result => {
if (result.error) {
console.error(`[Comparison] Error comparing ${result.filePath}: ${result.error}`);
}
if (result.hasDifferences) { if (result.hasDifferences) {
hasDifferences = true; hasDifferences = true;
differences.push(result.filePath); differences.push(result.filePath);
} }
}) })
.catch(() => { .catch((error) => {
// Don't add to differences if there's an error reading files console.error(`[Comparison] Promise error for ${alpineInstallScriptPath}:`, error);
}) })
); );
} catch { } catch {
@@ -584,29 +653,42 @@ export class ScriptDownloaderService {
// Wait for all comparisons to complete // Wait for all comparisons to complete
await Promise.all(comparisonPromises); await Promise.all(comparisonPromises);
console.log(`[Comparison] Completed comparison for ${script.slug}: hasDifferences=${hasDifferences}, differences=${differences.length}`);
return { hasDifferences, differences }; return { hasDifferences, differences };
} catch (error) { } catch (/** @type {any} */ error) {
console.error('Error comparing script content:', error); console.error(`[Comparison] Error comparing script content for ${script.slug}:`, error);
return { hasDifferences: false, differences: [] }; return { hasDifferences: false, differences: [], error: error.message };
} }
} }
/**
* Compare a single file with remote
* @param {import('~/types/script').Script} script - The script object
* @param {string} remotePath - The remote file path
* @param {string} filePath - The local file path
* @returns {Promise<{hasDifferences: boolean, filePath: string, error?: string}>}
*/
async compareSingleFile(script, remotePath, filePath) { async compareSingleFile(script, remotePath, filePath) {
try { try {
const localPath = join(this.scriptsDirectory, filePath); const localPath = join(this.scriptsDirectory, filePath);
const repoUrl = this.getRepoUrlForScript(script); const repoUrl = this.getRepoUrlForScript(script);
const branch = process.env.REPO_BRANCH || 'main'; const branch = process.env.REPO_BRANCH || 'main';
console.log(`[Comparison] Comparing ${filePath} from ${repoUrl} (branch: ${branch})`);
// Read local content // Read local content
const localContent = await readFile(localPath, 'utf-8'); const localContent = await readFile(localPath, 'utf-8');
console.log(`[Comparison] Local file size: ${localContent.length} bytes`);
// Download remote content from the script's repository // Download remote content from the script's repository
const remoteContent = await this.downloadFileFromGitHub(repoUrl, remotePath, branch); const remoteContent = await this.downloadFileFromGitHub(repoUrl, remotePath, branch);
console.log(`[Comparison] Remote file size: ${remoteContent.length} bytes`);
// Apply modification only for CT scripts, not for other script types // Apply modification only for CT scripts, not for other script types
let modifiedRemoteContent; let modifiedRemoteContent;
if (remotePath.startsWith('ct/')) { if (remotePath.startsWith('ct/')) {
modifiedRemoteContent = this.modifyScriptContent(remoteContent); modifiedRemoteContent = this.modifyScriptContent(remoteContent);
console.log(`[Comparison] Applied CT script modifications`);
} else { } else {
modifiedRemoteContent = remoteContent; // Don't modify tools or vm scripts modifiedRemoteContent = remoteContent; // Don't modify tools or vm scripts
} }
@@ -614,13 +696,26 @@ export class ScriptDownloaderService {
// Compare content // Compare content
const hasDifferences = localContent !== modifiedRemoteContent; const hasDifferences = localContent !== modifiedRemoteContent;
if (hasDifferences) {
console.log(`[Comparison] Differences found in ${filePath}`);
} else {
console.log(`[Comparison] No differences in ${filePath}`);
}
return { hasDifferences, filePath }; return { hasDifferences, filePath };
} catch (error) { } catch (/** @type {any} */ error) {
console.error(`Error comparing file ${filePath}:`, error); console.error(`[Comparison] Error comparing file ${filePath}:`, error.message);
return { hasDifferences: false, filePath }; // Return error information so it can be handled upstream
return { hasDifferences: false, filePath, error: error.message };
} }
} }
/**
* Get diff between local and remote script
* @param {import('~/types/script').Script} script - The script object
* @param {string} filePath - The file path to diff
* @returns {Promise<{diff: string|null, localContent: string|null, remoteContent: string|null}>}
*/
async getScriptDiff(script, filePath) { async getScriptDiff(script, filePath) {
this.initializeConfig(); this.initializeConfig();
try { try {
@@ -680,6 +775,12 @@ export class ScriptDownloaderService {
} }
} }
/**
* Generate a simple line-by-line diff
* @param {string} localContent - The local file content
* @param {string} remoteContent - The remote file content
* @returns {string}
*/
generateDiff(localContent, remoteContent) { generateDiff(localContent, remoteContent) {
const localLines = localContent.split('\n'); const localLines = localContent.split('\n');
const remoteLines = remoteContent.split('\n'); const remoteLines = remoteContent.split('\n');

View File

@@ -0,0 +1,220 @@
import { getSSHExecutionService } from '../ssh-execution-service';
import type { Server } from '~/types/server';
export interface Storage {
name: string;
type: string;
content: string[];
supportsBackup: boolean;
nodes?: string[];
[key: string]: any; // For additional storage-specific properties
}
interface CachedStorageData {
storages: Storage[];
lastFetched: Date;
}
class StorageService {
private cache: Map<number, CachedStorageData> = new Map();
private readonly CACHE_TTL_MS = 60 * 60 * 1000; // 1 hour
/**
* Parse storage.cfg content and extract storage information
*/
private parseStorageConfig(configContent: string): Storage[] {
const storages: Storage[] = [];
const lines = configContent.split('\n');
let currentStorage: Partial<Storage> | null = null;
for (const rawLine of lines) {
if (!rawLine) continue;
// Check if line is indented (has leading whitespace/tabs) BEFORE trimming
const isIndented = /^[\s\t]/.test(rawLine);
const line = rawLine.trim();
// Skip empty lines and comments
if (!line || line.startsWith('#')) {
continue;
}
// Check if this is a storage definition line (format: "type: name")
// Storage definitions are NOT indented
if (!isIndented) {
const storageMatch = /^(\w+):\s*(.+)$/.exec(line);
if (storageMatch?.[1] && storageMatch[2]) {
// Save previous storage if exists
if (currentStorage?.name) {
storages.push(this.finalizeStorage(currentStorage));
}
// Start new storage
currentStorage = {
type: storageMatch[1],
name: storageMatch[2],
content: [],
supportsBackup: false,
};
continue;
}
}
// Parse storage properties (indented lines - can be tabs or spaces)
if (currentStorage && isIndented) {
// Split on first whitespace (space or tab) to separate key and value
const match = /^(\S+)\s+(.+)$/.exec(line);
if (match?.[1] && match[2]) {
const key = match[1];
const value = match[2].trim();
switch (key) {
case 'content':
// Content can be comma-separated: "images,rootdir" or "backup"
currentStorage.content = value.split(',').map(c => c.trim());
currentStorage.supportsBackup = currentStorage.content.includes('backup');
break;
case 'nodes':
// Nodes can be comma-separated: "prox5" or "prox5,prox6"
currentStorage.nodes = value.split(',').map(n => n.trim());
break;
default:
// Store other properties
if (key) {
(currentStorage as any)[key] = value;
}
}
}
}
}
// Don't forget the last storage
if (currentStorage?.name) {
storages.push(this.finalizeStorage(currentStorage));
}
return storages;
}
/**
* Finalize storage object with proper typing
*/
private finalizeStorage(storage: Partial<Storage>): Storage {
return {
name: storage.name!,
type: storage.type!,
content: storage.content ?? [],
supportsBackup: storage.supportsBackup ?? false,
nodes: storage.nodes,
...Object.fromEntries(
Object.entries(storage).filter(([key]) =>
!['name', 'type', 'content', 'supportsBackup', 'nodes'].includes(key)
)
),
};
}
/**
* Fetch storage configuration from server via SSH
*/
async fetchStoragesFromServer(server: Server, forceRefresh = false): Promise<Storage[]> {
const serverId = server.id;
// Check cache first (unless force refresh)
if (!forceRefresh && this.cache.has(serverId)) {
const cached = this.cache.get(serverId)!;
const age = Date.now() - cached.lastFetched.getTime();
if (age < this.CACHE_TTL_MS) {
return cached.storages;
}
}
// Fetch from server
const sshService = getSSHExecutionService();
let configContent = '';
await new Promise<void>((resolve, reject) => {
void sshService.executeCommand(
server,
'cat /etc/pve/storage.cfg',
(data: string) => {
configContent += data;
},
(error: string) => {
reject(new Error(`Failed to read storage config: ${error}`));
},
(exitCode: number) => {
if (exitCode === 0) {
resolve();
} else {
reject(new Error(`Command failed with exit code ${exitCode}`));
}
}
);
});
// Parse and cache
const storages = this.parseStorageConfig(configContent);
this.cache.set(serverId, {
storages,
lastFetched: new Date(),
});
return storages;
}
/**
* Get all storages for a server (cached or fresh)
*/
async getStorages(server: Server, forceRefresh = false): Promise<Storage[]> {
return this.fetchStoragesFromServer(server, forceRefresh);
}
/**
* Get only backup-capable storages
*/
async getBackupStorages(server: Server, forceRefresh = false): Promise<Storage[]> {
const allStorages = await this.getStorages(server, forceRefresh);
return allStorages.filter(s => s.supportsBackup);
}
/**
* Get PBS storage information (IP and datastore) from storage config
*/
getPBSStorageInfo(storage: Storage): { pbs_ip: string | null; pbs_datastore: string | null } {
if (storage.type !== 'pbs') {
return { pbs_ip: null, pbs_datastore: null };
}
return {
pbs_ip: (storage as any).server ?? null,
pbs_datastore: (storage as any).datastore ?? null,
};
}
/**
* Clear cache for a specific server
*/
clearCache(serverId: number): void {
this.cache.delete(serverId);
}
/**
* Clear all caches
*/
clearAllCaches(): void {
this.cache.clear();
}
}
// Singleton instance
let storageServiceInstance: StorageService | null = null;
export function getStorageService(): StorageService {
storageServiceInstance ??= new StorageService();
return storageServiceInstance;
}

View File

@@ -22,7 +22,7 @@
"noEmit": true, "noEmit": true,
"module": "ESNext", "module": "ESNext",
"moduleResolution": "Bundler", "moduleResolution": "Bundler",
"jsx": "preserve", "jsx": "react-jsx",
"plugins": [ "plugins": [
{ {
"name": "next" "name": "next"

View File

@@ -851,6 +851,59 @@ rollback() {
exit 1 exit 1
} }
# Check installed Node.js version and upgrade if needed
check_node_version() {
if ! command -v node &>/dev/null; then
log_error "Node.js is not installed"
exit 1
fi
local current major_version
current=$(node -v 2>/dev/null | tr -d 'v')
major_version=${current%%.*}
log "Detected Node.js version: $current"
if (( major_version < 24 )); then
log_warning "Node.js < 24 detected → upgrading to Node.js 24 LTS..."
upgrade_node_to_24
elif (( major_version > 24 )); then
log_warning "Node.js > 24 detected → script tested only up to Node 24"
log "Continuing anyway…"
else
log_success "Node.js 24 already installed"
fi
}
# Upgrade Node.js to version 24
upgrade_node_to_24() {
log "Preparing Node.js 24 upgrade…"
# Remove old nodesource repo if it exists
if [ -f /etc/apt/sources.list.d/nodesource.list ]; then
rm -f /etc/apt/sources.list.d/nodesource.list
fi
# Install NodeSource repo for Node.js 24
curl -fsSL https://deb.nodesource.com/setup_24.x -o /tmp/node24_setup.sh
if ! bash /tmp/node24_setup.sh > /tmp/node24_setup.log 2>&1; then
log_error "Failed to configure Node.js 24 repository"
tail -20 /tmp/node24_setup.log | while read -r line; do log_error "$line"; done
exit 1
fi
log "Installing Node.js 24…"
if ! apt-get install -y nodejs >> "$LOG_FILE" 2>&1; then
log_error "Failed to install Node.js 24"
exit 1
fi
local new_ver
new_ver=$(node -v 2>/dev/null || true)
log_success "Node.js successfully upgraded to $new_ver"
}
# Main update process # Main update process
main() { main() {
# Check if this is the relocated/detached version first # Check if this is the relocated/detached version first
@@ -913,6 +966,10 @@ main() {
# Stop the application before updating # Stop the application before updating
stop_application stop_application
# Check Node.js version
check_node_version
# Download and extract release # Download and extract release
local source_dir local source_dir