@underpostnet/underpost 2.90.4 → 2.95.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/.github/workflows/pwa-microservices-template-page.cd.yml +5 -4
  2. package/.github/workflows/release.cd.yml +7 -7
  3. package/README.md +7 -8
  4. package/bin/build.js +6 -1
  5. package/bin/deploy.js +2 -196
  6. package/cli.md +154 -80
  7. package/manifests/deployment/dd-default-development/deployment.yaml +4 -4
  8. package/manifests/deployment/dd-test-development/deployment.yaml +2 -2
  9. package/package.json +1 -1
  10. package/scripts/disk-clean.sh +216 -0
  11. package/scripts/rocky-setup.sh +1 -0
  12. package/scripts/ssh-cluster-info.sh +4 -3
  13. package/src/cli/cluster.js +1 -1
  14. package/src/cli/db.js +1143 -201
  15. package/src/cli/deploy.js +93 -24
  16. package/src/cli/env.js +2 -2
  17. package/src/cli/image.js +198 -133
  18. package/src/cli/index.js +111 -44
  19. package/src/cli/lxd.js +73 -74
  20. package/src/cli/monitor.js +20 -9
  21. package/src/cli/repository.js +212 -5
  22. package/src/cli/run.js +207 -74
  23. package/src/cli/ssh.js +642 -14
  24. package/src/client/components/core/CommonJs.js +0 -1
  25. package/src/db/mongo/MongooseDB.js +5 -1
  26. package/src/index.js +1 -1
  27. package/src/monitor.js +11 -1
  28. package/src/server/backup.js +1 -1
  29. package/src/server/conf.js +1 -1
  30. package/src/server/dns.js +242 -1
  31. package/src/server/process.js +6 -1
  32. package/src/server/start.js +2 -0
  33. package/scripts/snap-clean.sh +0 -26
  34. package/src/client/public/default/plantuml/client-conf.svg +0 -1
  35. package/src/client/public/default/plantuml/client-schema.svg +0 -1
  36. package/src/client/public/default/plantuml/cron-conf.svg +0 -1
  37. package/src/client/public/default/plantuml/cron-schema.svg +0 -1
  38. package/src/client/public/default/plantuml/server-conf.svg +0 -1
  39. package/src/client/public/default/plantuml/server-schema.svg +0 -1
  40. package/src/client/public/default/plantuml/ssr-conf.svg +0 -1
  41. package/src/client/public/default/plantuml/ssr-schema.svg +0 -1
package/cli.md CHANGED
@@ -1,4 +1,4 @@
1
- ## underpost ci/cd cli v2.90.4
1
+ ## underpost ci/cd cli v2.95.0
2
2
 
3
3
  ### Usage: `underpost [options] [command]`
4
4
  ```
@@ -17,22 +17,21 @@ Commands:
17
17
  static [options] Manages static build of page, bundles, and documentation with comprehensive customization options.
18
18
  config [options] <operator> [key] [value] Manages Underpost configurations using various operators.
19
19
  root Displays the root path of the npm installation.
20
- ip [options] Displays the current public machine IP addresses.
20
+ ip [options] [ips] Displays the current public machine IP addresses.
21
21
  cluster [options] [pod-name] Manages Kubernetes clusters, defaulting to Kind cluster initialization.
22
22
  deploy [options] [deploy-list] [env] Manages application deployments, defaulting to deploying development pods.
23
23
  secret [options] <platform> Manages secrets for various platforms.
24
- dockerfile-image-build [options] Builds a Docker image from a specified Dockerfile with various options for naming, saving, and loading.
25
- dockerfile-pull-base-images [options] Pulls required Underpost Dockerfile base images and optionally loads them into clusters.
24
+ image [options] Manages Docker images, including building, saving, and loading into Kubernetes clusters.
26
25
  install Quickly imports Underpost npm dependencies by copying them.
27
- db [options] <deploy-list> Manages database operations, including import, export, and collection management.
26
+ db [options] <deploy-list> Manages database operations with support for MariaDB and MongoDB, including import/export, multi-pod targeting, and Git integration.
28
27
  metadata [options] [deploy-id] [host] [path] Manages cluster metadata operations, including import and export.
29
28
  script [options] <operator> <script-name> [script-value] Supports a variety of built-in Underpost global scripts, their preset lifecycle events, and arbitrary custom scripts.
30
29
  cron [options] [deploy-list] [job-list] Manages cron jobs, including initialization, execution, and configuration updates.
31
30
  fs [options] [path] Manages file storage, defaulting to file upload operations.
32
31
  test [options] [deploy-list] Manages and runs tests, defaulting to the current Underpost default test suite.
33
32
  monitor [options] <deploy-id> [env] Manages health server monitoring for specified deployments.
34
- ssh [options] Import and start ssh server and client based on current default deployment ID.
35
- run [options] <runner-id> [path] Runs a script from the specified path.
33
+ ssh [options]
34
+ run [options] <runner-id> [path] Runs specified scripts using various runners.
36
35
  lxd [options] Manages LXD containers and virtual machines.
37
36
  baremetal [options] [workflow-id] [hostname] [ip-address] Manages baremetal server operations, including installation, database setup, commissioning, and user management.
38
37
  help [command] display help for command
@@ -49,15 +48,28 @@ Commands:
49
48
  Initializes a new Underpost project, service, or configuration.
50
49
 
51
50
  Arguments:
52
- app-name The name of the new project.
51
+ app-name The name of the new project.
53
52
 
54
53
  Options:
55
- --deploy-id <deploy-id> Crete deploy ID conf env files
56
- --sub-conf <sub-conf> Create sub conf env files
57
- --cluster Create deploy ID cluster files and sync to current
58
- cluster
59
- --dev Sets the development cli context
60
- -h, --help display help for command
54
+ --deploy-id <deploy-id> Crete deploy ID conf env files
55
+ --sub-conf <sub-conf> Create sub conf env files
56
+ --cluster Create deploy ID cluster files and sync to
57
+ current cluster
58
+ --build-repos Create deploy ID repositories
59
+ --build Build the deployment to
60
+ pwa-microservices-template (requires
61
+ --deploy-id)
62
+ --clean-template Clean the build directory
63
+ (pwa-microservices-template)
64
+ --sync-conf Sync configuration to private repositories
65
+ (requires --deploy-id)
66
+ --purge Remove deploy ID conf and all related
67
+ repositories (requires --deploy-id)
68
+ --dev Sets the development cli context
69
+ --default-conf Create default deploy ID conf env files
70
+ --conf-workflow-id <workflow-id> Set custom configuration workflow ID for
71
+ conf generation
72
+ -h, --help display help for command
61
73
 
62
74
  ```
63
75
 
@@ -282,13 +294,28 @@ Options:
282
294
 
283
295
  ### `ip` :
284
296
  ```
285
- Usage: underpost ip [options]
297
+ Usage: underpost ip [options] [ips]
286
298
 
287
299
  Displays the current public machine IP addresses.
288
300
 
301
+ Arguments:
302
+ ips Optional args comma-separated list of IP to process.
303
+
289
304
  Options:
290
- --copy Copies the IP addresses to the clipboard.
291
- -h, --help display help for command
305
+ --copy Copies the IP addresses to the clipboard.
306
+ --ban-ingress-add Adds IP addresses to banned ingress list.
307
+ --ban-ingress-remove Removes IP addresses from banned ingress list.
308
+ --ban-ingress-list Lists all banned ingress IP addresses.
309
+ --ban-ingress-clear Clears all banned ingress IP addresses.
310
+ --ban-egress-add Adds IP addresses to banned egress list.
311
+ --ban-egress-remove Removes IP addresses from banned egress list.
312
+ --ban-egress-list Lists all banned egress IP addresses.
313
+ --ban-egress-clear Clears all banned egress IP addresses.
314
+ --ban-both-add Adds IP addresses to both banned ingress and egress
315
+ lists.
316
+ --ban-both-remove Removes IP addresses from both banned ingress and
317
+ egress lists.
318
+ -h, --help display help for command
292
319
 
293
320
  ```
294
321
 
@@ -428,6 +455,10 @@ Options:
428
455
  configuration during deployment.
429
456
  --namespace <namespace> Kubernetes namespace for deployment
430
457
  operations (defaults to "default").
458
+ --kind-type <kind-type> Specifies the Kind cluster type for
459
+ deployment operations.
460
+ --port <port> Sets up port forwarding from local to
461
+ remote ports.
431
462
  -h, --help display help for command
432
463
 
433
464
  ```
@@ -455,14 +486,22 @@ Options:
455
486
  ```
456
487
 
457
488
 
458
- ### `dockerfile-image-build` :
489
+ ### `image` :
459
490
  ```
460
- Usage: underpost dockerfile-image-build [options]
491
+ Usage: underpost image [options]
461
492
 
462
- Builds a Docker image from a specified Dockerfile with various options for
463
- naming, saving, and loading.
493
+ Manages Docker images, including building, saving, and loading into Kubernetes
494
+ clusters.
464
495
 
465
496
  Options:
497
+ --build Builds a Docker image using Podman,
498
+ optionally saves it as a tar archive,
499
+ and loads it into a specified Kubernetes
500
+ cluster (Kind, Kubeadm, or K3s).
501
+ --ls Lists all available Underpost Dockerfile
502
+ images.
503
+ --rm <image-id> Removes specified Underpost Dockerfile
504
+ images.
466
505
  --path [path] The path to the Dockerfile directory.
467
506
  --image-name [image-name] Sets a custom name for the Docker image.
468
507
  --image-path [image-path] Sets the output path for the tar image
@@ -470,10 +509,20 @@ Options:
470
509
  --dockerfile-name [dockerfile-name] Sets a custom name for the Dockerfile.
471
510
  --podman-save Exports the built image as a tar file
472
511
  using Podman.
473
- --kind-load Imports the tar image into a Kind
474
- cluster.
475
- --kubeadm-load Imports the tar image into a Kubeadm
476
- cluster.
512
+ --pull-base Pulls base images and builds a
513
+ "rockylinux9-underpost" image.
514
+ --spec Get current cached list of container
515
+ images used by all pods
516
+ --namespace <namespace> Kubernetes namespace for image
517
+ operations (defaults to "default").
518
+ --kind Set kind cluster env image context
519
+ management.
520
+ --kubeadm Set kubeadm cluster env image context
521
+ management.
522
+ --k3s Set k3s cluster env image context
523
+ management.
524
+ --node-name Set node name for kubeadm or k3s cluster
525
+ env image context management.
477
526
  --secrets Includes Dockerfile environment secrets
478
527
  during the build.
479
528
  --secrets-path [secrets-path] Specifies a custom path for Dockerfile
@@ -481,31 +530,13 @@ Options:
481
530
  --reset Performs a build without using the
482
531
  cache.
483
532
  --dev Use development mode.
484
- --k3s-load Loads the image into a K3s cluster.
533
+ --pull-dockerhub <dockerhub-image> Sets a custom Docker Hub image for base
534
+ image pulls.
485
535
  -h, --help display help for command
486
536
 
487
537
  ```
488
538
 
489
539
 
490
- ### `dockerfile-pull-base-images` :
491
- ```
492
- Usage: underpost dockerfile-pull-base-images [options]
493
-
494
- Pulls required Underpost Dockerfile base images and optionally loads them into
495
- clusters.
496
-
497
- Options:
498
- --path [path] The path to the Dockerfile directory.
499
- --kind-load Imports the pulled image into a Kind cluster.
500
- --kubeadm-load Imports the pulled image into a Kubeadm cluster.
501
- --version Sets a custom version for the base images.
502
- --k3s-load Loads the image into a K3s cluster.
503
- --dev Use development mode.
504
- -h, --help display help for command
505
-
506
- ```
507
-
508
-
509
540
  ### `install` :
510
541
  ```
511
542
  Usage: underpost install [options]
@@ -522,32 +553,31 @@ Options:
522
553
  ```
523
554
  Usage: underpost db [options] <deploy-list>
524
555
 
525
- Manages database operations, including import, export, and collection
526
- management.
556
+ Manages database operations with support for MariaDB and MongoDB, including
557
+ import/export, multi-pod targeting, and Git integration.
527
558
 
528
559
  Arguments:
529
- deploy-list A comma-separated list of deployment IDs (e.g.,
530
- "default-a,default-b").
560
+ deploy-list A comma-separated list of deployment IDs (e.g., "default-a,default-b").
531
561
 
532
562
  Options:
533
- --import Imports container backups from specified
534
- repositories.
535
- --export Exports container backups to specified
536
- repositories.
537
- --pod-name <pod-name> Optional: Specifies the pod context for database
538
- operations.
539
- --collections <collections> A comma-separated list of database collections
540
- to operate on.
541
- --out-path <out-path> Specifies a custom output path for backups.
542
- --drop Drops the specified databases or collections.
543
- --preserveUUID Preserves UUIDs during database operations.
544
- --git Uploads database backups to GitHub.
545
- --hosts <hosts> A comma-separated list of database hosts.
546
- --paths <paths> A comma-separated list of paths for database
547
- files.
548
- --ns <ns-name> Optional: Specifies the namespace context for
549
- database operations.
550
- -h, --help display help for command
563
+ --import Imports container backups from specified repositories.
564
+ --export Exports container backups to specified repositories.
565
+ --pod-name <pod-name> Comma-separated list of pod names or patterns (supports wildcards like "mariadb-*").
566
+ --node-name <node-name> Comma-separated list of node names to filter pods by their node placement.
567
+ --label-selector <selector> Kubernetes label selector for filtering pods (e.g., "app=mariadb").
568
+ --all-pods Target all matching pods instead of just the first one.
569
+ --primary-pod Automatically detect and use MongoDB primary pod (MongoDB only).
570
+ --stats Display database statistics (collection/table names with document/row counts).
571
+ --collections <collections> Comma-separated list of database collections to operate on.
572
+ --out-path <out-path> Specifies a custom output path for backups.
573
+ --drop Drops the specified databases or collections before importing.
574
+ --preserveUUID Preserves UUIDs during database import operations.
575
+ --git Enables Git integration for backup version control (clone, pull, commit, push to GitHub).
576
+ --hosts <hosts> Comma-separated list of database hosts to filter operations.
577
+ --paths <paths> Comma-separated list of paths to filter database operations.
578
+ --ns <ns-name> Kubernetes namespace context for database operations (defaults to "default").
579
+ --macro-rollback-export <n-commits-reset> Exports a macro rollback script that reverts the last n commits (Git integration required).
580
+ -h, --help display help for command
551
581
 
552
582
  ```
553
583
 
@@ -690,9 +720,12 @@ Options:
690
720
  --single Disables recurrence, running the monitor script
691
721
  only once.
692
722
  --replicas <replicas> Sets a custom number of replicas for monitoring.
723
+ Defaults to 1.
693
724
  --type <type> Sets a custom monitor type.
694
725
  --sync Synchronizes with current proxy deployments and
695
726
  traffic configurations.
727
+ --namespace <namespace> Sets the Kubernetes namespace for the
728
+ deployment. Defaults to "default".
696
729
  -h, --help display help for command
697
730
 
698
731
  ```
@@ -702,12 +735,40 @@ Options:
702
735
  ```
703
736
  Usage: underpost ssh [options]
704
737
 
705
- Import and start ssh server and client based on current default deployment ID.
706
-
707
738
  Options:
708
- --generate Generates new ssh credential and stores it in current private
709
- keys file storage.
710
- -h, --help display help for command
739
+ --deploy-id <deploy-id> Sets deploy id context for ssh operations.
740
+ --generate Generates new ssh credential and stores it in
741
+ current private keys file storage.
742
+ --user <user> Sets custom ssh user
743
+ --password <password> Sets custom ssh password
744
+ --host <host> Sets custom ssh host
745
+ --port <port> Sets custom ssh port
746
+ --filter <filter> Filters ssh user credentials from current private
747
+ keys file storage.
748
+ --groups <groups> Sets comma-separated ssh user groups for the ssh
749
+ user credential.
750
+ --user-add Adds a new ssh user credential to current private
751
+ keys file storage.
752
+ --user-remove Removes an existing ssh user credential from current
753
+ private keys file storage.
754
+ --user-ls Lists all ssh user credentials from current private
755
+ keys file storage.
756
+ --start Starts an SSH session with the specified
757
+ credentials.
758
+ --reset Resets ssh configuration and deletes all stored
759
+ credentials.
760
+ --keys-list Lists all ssh keys from current private keys file
761
+ storage.
762
+ --hosts-list Lists all ssh hosts from current private keys file
763
+ storage.
764
+ --disable-password Disables password authentication for the SSH
765
+ session.
766
+ --key-test Tests the SSH key using ssh-keygen.
767
+ --stop Stops the SSH service.
768
+ --status Checks the status of the SSH service.
769
+ --connect-uri Displays the connection URI.
770
+ --copy Copies the connection URI to clipboard.
771
+ -h, --help display help for command
711
772
 
712
773
  ```
713
774
 
@@ -716,11 +777,11 @@ Options:
716
777
  ```
717
778
  Usage: underpost run [options] <runner-id> [path]
718
779
 
719
- Runs a script from the specified path.
780
+ Runs specified scripts using various runners.
720
781
 
721
782
  Arguments:
722
- runner-id The runner ID to run. Options: spark-template, rmi, kill, secret, underpost-config, gpu-env, tf-gpu-test, dev-cluster, metadata, svc-ls, svc-rm, ssh-cluster-info, dev-hosts-expose, dev-hosts-restore, cluster-build, template-deploy, template-deploy-image, clean, pull, release-deploy, ssh-deploy, ide, sync, tz, cron, get-proxy, instance-promote, instance, ls-deployments, ls-images, host-update, dd-container, ip-info, monitor, db-client, git-conf, promote, metrics, cluster, deploy, dev, service, sh, log, release-cmt, sync-replica, tf-vae-test, deploy-job.
723
- path The absolute or relative directory path where the script is located.
783
+ runner-id The runner ID to run. Options: spark-template, rmi, kill, secret, underpost-config, gpu-env, tf-gpu-test, dev-cluster, metadata, svc-ls, svc-rm, ssh-cluster-info, dev-hosts-expose, dev-hosts-restore, cluster-build, template-deploy, template-deploy-image, clean, pull, release-deploy, ssh-deploy, ide, sync, stop, ssh-deploy-stop, tz, cron, get-proxy, instance-promote, instance, ls-deployments, host-update, dd-container, ip-info, monitor, db-client, git-conf, promote, metrics, cluster, deploy, disk-clean, disk-usage, dev, service, sh, log, release-cmt, sync-replica, tf-vae-test, deploy-job.
784
+ path The input value, identifier, or path for the operation.
724
785
 
725
786
  Options:
726
787
  --command <command-array> Array of commands to run.
@@ -746,9 +807,7 @@ Options:
746
807
  --api-version <version> Sets the API version for the job manifest in deploy-job.
747
808
  --labels <labels> Optional: Specifies a comma-separated list of key-value pairs for labels (e.g., "app=my-app,env=prod").
748
809
  --claim-name <name> Optional: Specifies the claim name for volume mounting in deploy-job.
749
- --kind <kind-type> Specifies the kind of Kubernetes resource (e.g., Job, Deployment) for deploy-job.
750
- --kubeadm Flag to indicate Kubeadm cluster type context
751
- --k3s Flag to indicate K3s cluster type context
810
+ --kind-type <kind-type> Specifies the kind of Kubernetes resource (e.g., Job, Deployment) for deploy-job.
752
811
  --force Forces operation, overriding any warnings or conflicts.
753
812
  --tls Enables TLS for the runner execution.
754
813
  --reset Resets the runner state before execution.
@@ -763,6 +822,16 @@ Options:
763
822
  --expose Enables service exposure for the runner execution.
764
823
  --conf-server-path <conf-server-path> Sets a custom configuration server path.
765
824
  --underpost-root <underpost-root> Sets a custom Underpost root path.
825
+ --cron-jobs <jobs> Comma-separated list of cron jobs to run before executing the script.
826
+ --timezone <timezone> Sets the timezone for the runner execution.
827
+ --kubeadm Sets the kubeadm cluster context for the runner execution.
828
+ --k3s Sets the k3s cluster context for the runner execution.
829
+ --kind Sets the kind cluster context for the runner execution.
830
+ --log-type <log-type> Sets the log type for the runner execution.
831
+ --deploy-id <deploy-id> Sets deploy id context for the runner execution.
832
+ --user <user> Sets user context for the runner execution.
833
+ --hosts <hosts> Comma-separated list of hosts for the runner execution.
834
+ --instance-id <instance-id> Sets instance id context for the runner execution.
766
835
  -h, --help display help for command
767
836
 
768
837
  ```
@@ -806,8 +875,13 @@ Options:
806
875
  --delete-expose <vm-name-ports> Removes exposed ports on a VM (e.g.,
807
876
  "k8s-control:80,443"). Multiple VM-port
808
877
  pairs can be comma-separated.
809
- --auto-expose-k8s-ports <vm-id> Automatically exposes common Kubernetes
810
- ports for the specified VM.
878
+ --workflow-id <workflow-id> Sets the workflow ID context for LXD
879
+ operations.
880
+ --vm-id <vm-id> Sets the VM ID context for LXD operations.
881
+ --deploy-id <deploy-id> Sets the deployment ID context for LXD
882
+ operations.
883
+ --namespace <namespace> Kubernetes namespace for LXD operations
884
+ (defaults to "default").
811
885
  -h, --help display help for command
812
886
 
813
887
  ```
@@ -17,7 +17,7 @@ spec:
17
17
  spec:
18
18
  containers:
19
19
  - name: dd-default-development-blue
20
- image: localhost/rockylinux9-underpost:v2.90.4
20
+ image: localhost/rockylinux9-underpost:v2.95.0
21
21
  # resources:
22
22
  # requests:
23
23
  # memory: "124Ki"
@@ -32,7 +32,7 @@ spec:
32
32
  npm install -g npm@11.2.0 &&
33
33
  npm install -g underpost &&
34
34
  cd $(underpost root)/underpost &&
35
- node bin/deploy update-default-conf template &&
35
+ node bin new --default-conf --conf-workflow-id template &&
36
36
  mkdir -p /home/dd &&
37
37
  cd /home/dd &&
38
38
  underpost new engine
@@ -100,7 +100,7 @@ spec:
100
100
  spec:
101
101
  containers:
102
102
  - name: dd-default-development-green
103
- image: localhost/rockylinux9-underpost:v2.90.4
103
+ image: localhost/rockylinux9-underpost:v2.95.0
104
104
  # resources:
105
105
  # requests:
106
106
  # memory: "124Ki"
@@ -115,7 +115,7 @@ spec:
115
115
  npm install -g npm@11.2.0 &&
116
116
  npm install -g underpost &&
117
117
  cd $(underpost root)/underpost &&
118
- node bin/deploy update-default-conf template &&
118
+ node bin new --default-conf --conf-workflow-id template &&
119
119
  mkdir -p /home/dd &&
120
120
  cd /home/dd &&
121
121
  underpost new engine
@@ -18,7 +18,7 @@ spec:
18
18
  spec:
19
19
  containers:
20
20
  - name: dd-test-development-blue
21
- image: localhost/rockylinux9-underpost:v2.90.4
21
+ image: localhost/rockylinux9-underpost:v2.95.0
22
22
 
23
23
  command:
24
24
  - /bin/sh
@@ -103,7 +103,7 @@ spec:
103
103
  spec:
104
104
  containers:
105
105
  - name: dd-test-development-green
106
- image: localhost/rockylinux9-underpost:v2.90.4
106
+ image: localhost/rockylinux9-underpost:v2.95.0
107
107
 
108
108
  command:
109
109
  - /bin/sh
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "type": "module",
3
3
  "main": "src/index.js",
4
4
  "name": "@underpostnet/underpost",
5
- "version": "2.90.4",
5
+ "version": "2.95.0",
6
6
  "description": "pwa api rest template",
7
7
  "scripts": {
8
8
  "start": "env-cmd -f .env.production node --max-old-space-size=8192 src/server",
@@ -0,0 +1,216 @@
1
+ #!/usr/bin/env bash
2
+ # disk-clean.sh
3
+ # Safe, interactive disk cleanup for Rocky/RHEL-like systems.
4
+
5
+ set -u # Detect undefined variables (removed -e to handle errors manually where it matters)
6
+ IFS=$'\n\t'
7
+
8
+ AUTO_YES=0
9
+ LXD_FLAG=0
10
+ VACUUM_SIZE="500M"
11
+ TMP_AGE_DAYS=7
12
+ LOG_GZ_AGE_DAYS=90
13
+ ROOT_CACHE_AGE_DAYS=30
14
+ AGGRESSIVE=0
15
+
16
+ # Colors for better readability
17
+ RED='\033[0;31m'
18
+ GREEN='\033[0;32m'
19
+ YELLOW='\033[1;33m'
20
+ NC='\033[0m' # No Color
21
+
22
+ usage() {
23
+ cat <<EOF
24
+ Usage: $0 [--yes] [--aggressive] [--lxd] [--vacuum-size SIZE]
25
+
26
+ Options:
27
+ --yes run destructive actions without asking
28
+ --aggressive clean user caches (npm, pip, conda, root .cache)
29
+ --lxd enable lxc image prune
30
+ --vacuum-size X set journalctl --vacuum-size (default: $VACUUM_SIZE)
31
+ -h, --help show this help
32
+ EOF
33
+ }
34
+
35
+ # Parse args
36
+ while [[ $# -gt 0 ]]; do
37
+ case "$1" in
38
+ --yes) AUTO_YES=1; shift;;
39
+ --aggressive) AGGRESSIVE=1; shift;;
40
+ --lxd) LXD_FLAG=1; shift;;
41
+ --vacuum-size) VACUUM_SIZE="$2"; shift 2;;
42
+ -h|--help) usage; exit 0;;
43
+ *) echo "Unknown argument: $1"; usage; exit 2;;
44
+ esac
45
+ done
46
+
47
+ log() { echo -e "${GREEN}[INFO]${NC} $*"; }
48
+ warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
49
+ error() { echo -e "${RED}[ERROR]${NC} $*"; }
50
+
51
+ run() {
52
+ # Runs the command safely
53
+ echo "+ $*"
54
+ # Execute the command passed as arguments, preserving quotes/spaces
55
+ "$@" || {
56
+ warn "Command failed (non-critical): $*"
57
+ return 0
58
+ }
59
+ }
60
+
61
+ confirm() {
62
+ if [[ $AUTO_YES -eq 1 ]]; then
63
+ return 0
64
+ fi
65
+ # Use </dev/tty to ensure we read from user even inside a pipe loop
66
+ read -r -p "$1 [y/N]: " ans </dev/tty
67
+ case "$ans" in
68
+ [Yy]|[Yy][Ee][Ss]) return 0;;
69
+ *) return 1;;
70
+ esac
71
+ }
72
+
73
+ require_root() {
74
+ if [[ $EUID -ne 0 ]]; then
75
+ error "This script must be run as root."
76
+ exit 1
77
+ fi
78
+ }
79
+
80
+ command_exists() {
81
+ command -v "$1" >/dev/null 2>&1
82
+ }
83
+
84
+ require_root
85
+
86
+ log "Starting cleanup (aggressive=$AGGRESSIVE)"
87
+
88
+ # 1) Package Manager (DNF)
89
+ if command_exists dnf; then
90
+ log "Cleaning DNF caches"
91
+ run dnf clean all
92
+ run rm -rf /var/cache/dnf
93
+ if confirm "Run 'dnf autoremove -y' for orphan packages?"; then
94
+ run dnf autoremove -y
95
+ else
96
+ log "Skipped dnf autoremove"
97
+ fi
98
+ else
99
+ warn "dnf not found"
100
+ fi
101
+
102
+ # 2) Journal logs
103
+ if command_exists journalctl; then
104
+ log "Current Journal disk usage:"
105
+ journalctl --disk-usage || true
106
+
107
+ if confirm "Run 'journalctl --vacuum-size=$VACUUM_SIZE'?"; then
108
+ run journalctl --vacuum-size="$VACUUM_SIZE"
109
+ else
110
+ log "Skipped journal vacuum"
111
+ fi
112
+ fi
113
+
114
+ # 3) /var/tmp
115
+ if [[ -d /var/tmp ]]; then
116
+ if confirm "Delete files in /var/tmp older than $TMP_AGE_DAYS days?"; then
117
+ find /var/tmp -mindepth 1 -mtime +$TMP_AGE_DAYS -delete
118
+ fi
119
+ fi
120
+
121
+ # 4) Old compressed logs
122
+ if [[ -d /var/log ]]; then
123
+ if confirm "Delete compressed logs (.gz) in /var/log older than $LOG_GZ_AGE_DAYS days?"; then
124
+ find /var/log -type f -name '*.gz' -mtime +$LOG_GZ_AGE_DAYS -delete
125
+ fi
126
+ fi
127
+
128
+ # 5) Snap: disabled revisions
129
+ if command_exists snap; then
130
+ log "Searching for old Snap revisions"
131
+ # Save to variable only if successful
132
+ disabled_snaps=$(snap list --all 2>/dev/null | awk '/disabled/ {print $1, $3}') || disabled_snaps=""
133
+
134
+ if [[ -n "$disabled_snaps" ]]; then
135
+ echo "Disabled snap revisions found:"
136
+ echo "$disabled_snaps"
137
+ if confirm "Remove all disabled revisions?"; then
138
+ while read -r pkg rev; do
139
+ [[ -z "$pkg" ]] && continue
140
+ log "Removing snap $pkg (revision $rev)"
141
+ run snap remove "$pkg" --revision="$rev"
142
+ done <<< "$disabled_snaps"
143
+
144
+ log "Setting snap retention to 2"
145
+ run snap set system refresh.retain=2
146
+ fi
147
+ else
148
+ log "No disabled snap revisions found."
149
+ fi
150
+ fi
151
+
152
+ # 6) LXD
153
+ if command_exists lxc; then
154
+ if [[ $LXD_FLAG -eq 1 ]]; then
155
+ if confirm "Run 'lxc image prune'?"; then
156
+ run lxc image prune -f
157
+ fi
158
+ else
159
+ log "Skipping LXD (use --lxd to enable)"
160
+ fi
161
+ fi
162
+
163
+ # 7) Docker / Containerd
164
+ if command_exists docker; then
165
+ if confirm "Run 'docker system prune -a --volumes'? (WARNING: Removes stopped containers)"; then
166
+ run docker system prune -a --volumes -f
167
+ fi
168
+ elif command_exists crictl; then
169
+ if confirm "Attempt to remove images with crictl?"; then
170
+ run crictl rmi --prune
171
+ fi
172
+ fi
173
+
174
+ # 8) Large Files (>1G) - Completely rewritten logic
175
+ log "Scanning for files larger than 1G (this may take a while)..."
176
+
177
+ # Use while loop with find -print0 to handle filenames with spaces safely
178
+ FOUND_LARGE=0
179
+ # Note: find does not modify filesystem here, safe to run always
180
+ while IFS= read -r -d '' file; do
181
+ FOUND_LARGE=1
182
+ # Get readable size to show user
183
+ filesize=$(du -h "$file" | cut -f1)
184
+
185
+ echo -e "Large file found: ${YELLOW}$file${NC} (Size: $filesize)"
186
+
187
+ if confirm " -> Delete this file?"; then
188
+ run rm -vf "$file"
189
+ else
190
+ log "Skipped: $file"
191
+ fi
192
+ done < <(find / -xdev -type f -size +1G -print0 2>/dev/null)
193
+
194
+ if [[ $FOUND_LARGE -eq 0 ]]; then
195
+ log "No files >1G found in /"
196
+ fi
197
+
198
+ # 9) Aggressive Caches
199
+ if [[ $AGGRESSIVE -eq 1 ]]; then
200
+ log "Aggressive mode enabled"
201
+ command_exists npm && confirm "Run 'npm cache clean --force'?" && run npm cache clean --force
202
+ command_exists pip && confirm "Run 'pip cache purge'?" && run pip cache purge
203
+ command_exists conda && confirm "Run 'conda clean --all -y'?" && run conda clean --all -y
204
+
205
+ if [[ -d /root/.cache ]]; then
206
+ if confirm "Delete /root/.cache (> $ROOT_CACHE_AGE_DAYS days)?"; then
207
+ find /root/.cache -type f -mtime +$ROOT_CACHE_AGE_DAYS -delete
208
+ fi
209
+ fi
210
+ fi
211
+
212
+ # 10) Final
213
+ log "Final disk usage:"
214
+ df -h --total | grep total || df -h /
215
+
216
+ log "Cleanup finished."
@@ -34,6 +34,7 @@ PACKAGES=(
34
34
  which
35
35
  net-tools
36
36
  bind-utils
37
+ tcpdump
37
38
  )
38
39
 
39
40
  # Defaults