-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathhostConfigService.nix
More file actions
104 lines (93 loc) · 4.66 KB
/
Copy pathhostConfigService.nix
File metadata and controls
104 lines (93 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
{
lib,
pkgs,
}:
# Helper function to create a systemd service that configures *arr basic settings via API
serviceName: serviceConfig:
with lib; let
mkWaitForApiScript = import ./mkWaitForApiScript.nix {inherit lib pkgs;};
capitalizedName = lib.toUpper (builtins.substring 0 1 serviceName) + builtins.substring 1 (-1) serviceName;
in {
description = "Configure ${serviceName} via API";
after = ["${serviceName}.service"];
wantedBy = ["multi-user.target"];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStartPre = mkWaitForApiScript serviceName serviceConfig;
};
script = ''
set -eu
# Read secrets
API_KEY=$(cat ${serviceConfig.apiKeyPath})
AUTH_PASSWORD=$(cat ${serviceConfig.hostConfig.passwordPath})
BASE_URL="http://127.0.0.1:${builtins.toString serviceConfig.hostConfig.port}${serviceConfig.hostConfig.urlBase}/api/${serviceConfig.apiVersion}"
# Get current host configuration
echo "Fetching current host configuration..."
HOST_CONFIG=$(${pkgs.curl}/bin/curl -s -f -H "X-Api-Key: $API_KEY" "$BASE_URL/config/host" 2>/dev/null)
if [ -z "$HOST_CONFIG" ]; then
echo "Failed to fetch host configuration"
exit 1
fi
# Extract the ID from the host config (needed for PUT request)
CONFIG_ID=$(echo "$HOST_CONFIG" | ${pkgs.jq}/bin/jq -r '.id')
# Build the complete configuration JSON
echo "Building configuration..."
NEW_CONFIG=$(${pkgs.jq}/bin/jq -n \
--arg apiKey "$API_KEY" \
--arg password "$AUTH_PASSWORD" \
--argjson id "$CONFIG_ID" \
'{
id: $id,
bindAddress: "${serviceConfig.hostConfig.bindAddress}",
port: ${builtins.toString serviceConfig.hostConfig.port},
sslPort: ${builtins.toString serviceConfig.hostConfig.sslPort},
enableSsl: ${boolToString serviceConfig.hostConfig.enableSsl},
launchBrowser: ${boolToString serviceConfig.hostConfig.launchBrowser},
authenticationMethod: "${serviceConfig.hostConfig.authenticationMethod}",
authenticationRequired: "${serviceConfig.hostConfig.authenticationRequired}",
analyticsEnabled: ${boolToString serviceConfig.hostConfig.analyticsEnabled},
username: "${serviceConfig.hostConfig.username}",
password: $password,
passwordConfirmation: $password,
logLevel: "${serviceConfig.hostConfig.logLevel}",
logSizeLimit: ${builtins.toString serviceConfig.hostConfig.logSizeLimit},
consoleLogLevel: "${serviceConfig.hostConfig.consoleLogLevel}",
branch: "${serviceConfig.hostConfig.branch}",
apiKey: $apiKey,
sslCertPath: "${serviceConfig.hostConfig.sslCertPath}",
sslCertPassword: "${serviceConfig.hostConfig.sslCertPassword}",
urlBase: "${serviceConfig.hostConfig.urlBase}",
instanceName: "${serviceConfig.hostConfig.instanceName}",
applicationUrl: "${serviceConfig.hostConfig.applicationUrl}",
updateAutomatically: ${boolToString serviceConfig.hostConfig.updateAutomatically},
updateMechanism: "${serviceConfig.hostConfig.updateMechanism}",
updateScriptPath: "${serviceConfig.hostConfig.updateScriptPath}",
proxyEnabled: ${boolToString serviceConfig.hostConfig.proxyEnabled},
proxyType: "${serviceConfig.hostConfig.proxyType}",
proxyHostname: "${serviceConfig.hostConfig.proxyHostname}",
proxyPort: ${builtins.toString serviceConfig.hostConfig.proxyPort},
proxyUsername: "${serviceConfig.hostConfig.proxyUsername}",
proxyPassword: "${serviceConfig.hostConfig.proxyPassword}",
proxyBypassFilter: "${serviceConfig.hostConfig.proxyBypassFilter}",
proxyBypassLocalAddresses: ${boolToString serviceConfig.hostConfig.proxyBypassLocalAddresses},
certificateValidation: "${serviceConfig.hostConfig.certificateValidation}",
backupFolder: "${serviceConfig.hostConfig.backupFolder}",
backupInterval: ${builtins.toString serviceConfig.hostConfig.backupInterval},
backupRetention: ${builtins.toString serviceConfig.hostConfig.backupRetention},
trustCgnatIpAddresses: ${boolToString serviceConfig.hostConfig.trustCgnatIpAddresses}
}')
# Update host configuration
echo "Updating ${capitalizedName} configuration via API..."
${pkgs.curl}/bin/curl -s -f -X PUT \
-H "X-Api-Key: $API_KEY" \
-H "Content-Type: application/json" \
-d "$NEW_CONFIG" \
"$BASE_URL/config/host/$CONFIG_ID" > /dev/null
echo "Configuration updated successfully"
# Restart the service to pick up the new configuration
echo "Restarting ${serviceName} service..."
systemctl restart ${serviceName}.service
echo "${capitalizedName} service restarted"
'';
}