Skip to content

Commit ba60ad4

Browse files
committed
chore(doc): document ns8-ci script
1 parent 9924036 commit ba60ad4

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

scripts/Readme.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,82 @@
11
# Scripts Documentation
22

3+
## ns8-ci.sh
4+
5+
### Description
6+
7+
`ns8-ci.sh` is a Bash script that cleans up unused DigitalOcean resources created by the NS8 CI system. It identifies and optionally deletes orphaned tags, droplets, DNS records, and SSH keys to keep the CI environment clean and reduce costs. The script uses direct DigitalOcean API calls via `curl` instead of the `doctl` CLI tool for better compatibility with CI/CD environments.
8+
9+
### Prerequisites
10+
11+
- **curl**: Standard HTTP client tool (typically pre-installed on most systems).
12+
- **jq**: Command-line JSON processor. Install with `apt install jq` or `brew install jq`.
13+
- **DigitalOcean API Token**: A valid DigitalOcean API token with read/write permissions.
14+
15+
### Usage
16+
17+
```bash
18+
./ns8-ci.sh # List mode - only show what would be cleaned up
19+
./ns8-ci.sh --delete # Delete mode - actually remove the identified resources
20+
```
21+
22+
#### Environment Variables
23+
24+
The script requires a DigitalOcean API token to be set in one of these environment variables:
25+
- `DIGITALOCEAN_ACCESS_TOKEN`
26+
27+
#### Example
28+
29+
```bash
30+
# List orphaned resources without deleting
31+
export DIGITALOCEAN_ACCESS_TOKEN="your_token_here"
32+
./ns8-ci.sh
33+
34+
# Actually delete the orphaned resources
35+
./ns8-ci.sh --delete
36+
```
37+
38+
### How It Works
39+
40+
The script performs cleanup in four phases:
41+
42+
1. **Unused Tags**: Identifies tags starting with `NS8-CI-` that have no droplets attached and optionally removes them.
43+
44+
2. **Old Droplets**: Finds active droplets with NS8-CI tags that are older than 3 hours and optionally deletes them.
45+
46+
3. **Orphaned DNS Records**: Locates A and AAAA DNS records in `ci.nethserver.net` that don't correspond to any running droplets and optionally removes them.
47+
48+
4. **Unused SSH Keys**: Identifies SSH keys with names containing `.ci.nethserver.net` that aren't used by any droplets and optionally deletes them.
49+
50+
### Technical Details
51+
52+
- **API Pagination**: Handles DigitalOcean API pagination by setting `per_page=200` and `page=1` parameters.
53+
- **Age Threshold**: Only considers droplets older than 3 hours (10,800 seconds) for deletion.
54+
- **Safe Deletion**: Uses API calls with proper error handling to avoid accidental data loss.
55+
- **Domain Filtering**: Only processes resources in the `ci.nethserver.net` domain.
56+
57+
### Output
58+
59+
The script provides detailed output for each phase:
60+
61+
- Lists all resources that match the cleanup criteria
62+
- Shows droplet ages in human-readable format (hours)
63+
- Confirms each deletion operation when `--delete` mode is used
64+
- Provides summary statistics at completion
65+
66+
### Error Handling
67+
68+
- **Missing Dependencies**: Checks for required tools (`curl`, `jq`) before execution.
69+
- **Authentication**: Validates API token by testing account access.
70+
- **API Failures**: Continues processing other resources if individual API calls fail.
71+
- **Resource Protection**: Only targets resources with specific naming patterns to avoid accidental deletion.
72+
73+
### Notes
74+
75+
- The script is designed for automated execution in CI/CD pipelines.
76+
- Uses the DigitalOcean API v2 with Bearer token authentication.
77+
- Implements pagination to handle accounts with many resources.
78+
- Safe to run multiple times - only processes resources matching specific criteria.
79+
380
## update_issue_status.sh
481

582
### Description

0 commit comments

Comments
 (0)