Skip to content

Commit 6db2cd0

Browse files
dsimog01alexgonmad
andauthored
Improve resilience (#32)
* Adding aliases to improve code resiliance (#31) * Adding more aliases to improve code resiliance * Removed hardcoded dappmanager IP * Improve getLocalIp * Add log for retrieving domain * Improve iteration * Add retries for internal IP retrieval * Add dappmanager IP as fallback --------- Co-authored-by: Alex Gonzalez Matias <104903362+alexgonmad@users.noreply.github.com>
1 parent 64dad16 commit 6db2cd0

5 files changed

Lines changed: 96 additions & 24 deletions

File tree

api/src/createLocalConfigFile.ts

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import { params } from "./params";
77
export async function createLocalConfigFile(device: string): Promise<string> {
88
try {
99
const remoteFilePath = getRemoteConfigFilePath(device, "conf");
10-
const localIp = await getLocalIp();
10+
const localIp = await getLocalIpWithRetries(30);
1111
const localEndpoint = `${localIp}:${params.SERVER_PORT}`;
1212

1313
const remoteConfigFile = fs.readFileSync(remoteFilePath, "utf8");
1414
const localConfigFile = setLocalEndpoint(remoteConfigFile, localEndpoint);
1515

16-
if (localConfigFile === remoteConfigFile) throw Error("Error generating localConfigFile");
16+
if (localConfigFile === remoteConfigFile)
17+
throw Error("Error generating localConfigFile");
1718
return localConfigFile;
1819
} catch (e) {
1920
e.message = `Error creating localConfigFile: ${e.message}`;
@@ -23,22 +24,55 @@ export async function createLocalConfigFile(device: string): Promise<string> {
2324

2425
// Utils
2526

27+
async function getLocalIpWithRetries(retries: number): Promise<string> {
28+
// An integer n >= 1
29+
retries = Math.max(Math.floor(retries), 1);
30+
31+
for (let i = 0; i < retries - 1; i++) {
32+
try {
33+
return await getLocalIp();
34+
} catch (e) {
35+
console.log(`Error getting local IP: ${e.message}`);
36+
console.log(`Retrying... (${i + 1}/${retries})`);
37+
}
38+
}
39+
40+
return await getLocalIp();
41+
}
42+
2643
async function getLocalIp(): Promise<string> {
27-
try {
28-
const localIp = await got(params.DAPPNODE_API_URL_GET_INTERNAL_IP).text();
29-
if (!localIp) throw Error("localIp is empty");
30-
if (!ipRegex({ exact: true }).test(localIp)) throw Error("Invalida localIp");
31-
return localIp;
32-
} catch (e) {
33-
e.message = `Error fetching localIp: ${e.message}`;
34-
throw e;
44+
const dappmanagerHostnames = params.DAPPMANAGER_HOSTNAMES;
45+
const getLocalIpUrls = dappmanagerHostnames.map(
46+
(hostname) => `http://${hostname}${params.GET_INTERNAL_IP_ENDPOINT}`
47+
);
48+
49+
let errorMessages: string[] = [];
50+
51+
for (const url of getLocalIpUrls) {
52+
try {
53+
const localIp = await got(url).text();
54+
if (!localIp) throw Error("Local IP is empty");
55+
if (!ipRegex({ exact: true }).test(localIp))
56+
throw Error("Invalid local IP");
57+
return localIp;
58+
} catch (e) {
59+
errorMessages.push(
60+
`Local IP could not be fetched from ${url}: ${e.message}`
61+
);
62+
}
3563
}
64+
throw Error(errorMessages.join("\n"));
3665
}
3766

38-
export function setLocalEndpoint(configFile: string, localEndpoint: string): string {
67+
export function setLocalEndpoint(
68+
configFile: string,
69+
localEndpoint: string
70+
): string {
3971
return configFile
4072
.split("\n")
41-
.map((row) => (row.startsWith("Endpoint =") ? `Endpoint = ${localEndpoint}` : row))
73+
.map((row) =>
74+
row.startsWith("Endpoint =") ? `Endpoint = ${localEndpoint}` : row
75+
)
4276
.join("\n");
4377
}
4478

api/src/params.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const params = {
22
MASTER_ADMIN: "dappnode_admin",
3-
DAPPNODE_API_URL_GET_INTERNAL_IP: "http://my.dappnode/global-envs/INTERNAL_IP",
3+
DAPPMANAGER_HOSTNAMES: ["my.dappnode", "dappmanager.dappnode", "172.33.1.7"], // TODO: Remove dappmanager IP when IP migration is performed
4+
GET_INTERNAL_IP_ENDPOINT: "/global-envs/INTERNAL_IP",
45
SERVER_PORT: 51820,
56
DATA_DIR: process.env.DATA_DIR || "/config",
67
HELP_MESSAGE:

releases.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
"dappnode": "Thu, 28 Jan 2021 02:25:25 GMT"
66
}
77
}
8-
}
8+
}

root/app/add-peer

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,34 @@ fi
99

1010
INTERNAL_SUBNET=${INTERNAL_SUBNET:-10.13.13.0}
1111
INTERFACE=$(echo "$INTERNAL_SUBNET" | awk 'BEGIN{FS=OFS="."} NF--')
12-
if [ -z "$SERVERURL" ] || [ "$SERVERURL" = "auto" ]; then
13-
SERVERURL=$(curl my.dappnode/global-envs/HOSTNAME)
14-
fi
1512
SERVERPORT=${SERVERPORT:-51820}
13+
1614
if [ -z "$PEERDNS" ] || [ "$PEERDNS" = "auto" ]; then
1715
PEERDNS="${INTERFACE}.1"
1816
fi
1917

18+
if [ -z "$SERVERURL" ] || [ "$SERVERURL" = "auto" ]; then
19+
20+
function fetchDappnodeDomain {
21+
for i in {1..10}; do
22+
for domain in "$@"; do
23+
echo "**** Fetching DAppNode domain from $domain..."
24+
SERVERURL=$(curl -s "$domain/global-envs/HOSTNAME")
25+
if [ ! -z "$SERVERURL" ]; then
26+
break
27+
fi
28+
sleep 2
29+
done
30+
done
31+
}
32+
33+
# TODO: Remove IP when the IP migration is performed
34+
domains=("my.dappnode" "dappmanager.dappnode" "172.33.1.7")
35+
36+
fetchDappnodeDomain "${domains[@]}"
37+
38+
fi
39+
2040
for i in {1..254}; do
2141
if grep -q "AllowedIPs = ${INTERFACE}.$(( $i + 1 ))/32" /config/wg0.conf; then
2242
echo "Peer $i exists"

root/etc/cont-init.d/30-config

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,30 @@ if [ -n "$PEERS" ]; then
244244
fi
245245
PEERS_COUNT=$(echo "${#PEERS_ARRAY[@]}")
246246
if [ -z "$SERVERURL" ] || [ "$SERVERURL" = "auto" ]; then
247-
for i in {1..90}; do
248-
echo "**** Fetching DAppNode domain..."
249-
SERVERURL=$(curl -s my.dappnode/global-envs/HOSTNAME)
250-
if [ ! -z $SERVERURL ]; then break; fi
251-
sleep 2
252-
done
253-
echo "**** SERVERURL var is either not set or is set to \"auto\", setting external IP to auto detected value of $SERVERURL ****"
247+
248+
function fetchDappnodeDomain {
249+
for i in {1..30}; do
250+
for domain in "$@"; do
251+
echo "**** Fetching DAppNode domain from $domain..."
252+
SERVERURL=$(curl -s "$domain/global-envs/HOSTNAME")
253+
if [ ! -z "$SERVERURL" ]; then
254+
break 2
255+
fi
256+
sleep 2
257+
done
258+
done
259+
}
260+
261+
# TODO: Remove IP when the IP migration is performed
262+
domains=("my.dappnode" "dappmanager.dappnode" "172.33.1.7")
263+
264+
fetchDappnodeDomain "${domains[@]}"
265+
266+
if [ -z "$SERVERURL" ]; then
267+
echo "**** SERVERURL var is either not set or is set to \"auto\", setting external IP to auto detected value of $SERVERURL ****"
268+
else
269+
echo "**** SERVERURL var is set to $SERVERURL ****"
270+
fi
254271
else
255272
echo "**** External server address is set to $SERVERURL ****"
256273
fi

0 commit comments

Comments
 (0)