Skip to content

Commit 377499a

Browse files
Merge pull request #8 from NethServer/fix-restore
Fix the restore procedure
2 parents 1cc8365 + aafad6d commit 377499a

File tree

6 files changed

+70
-26
lines changed

6 files changed

+70
-26
lines changed
Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,22 @@
11
#!/usr/bin/env python3
22

33
#
4-
# Copyright (C) 2022 Nethesis S.r.l.
5-
# http://www.nethesis.it - [email protected]
6-
#
7-
# This script is part of NethServer.
8-
#
9-
# NethServer is free software: you can redistribute it and/or modify
10-
# it under the terms of the GNU General Public License as published by
11-
# the Free Software Foundation, either version 3 of the License,
12-
# or any later version.
13-
#
14-
# NethServer is distributed in the hope that it will be useful,
15-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17-
# GNU General Public License for more details.
18-
#
19-
# You should have received a copy of the GNU General Public License
20-
# along with NethServer. If not, see COPYING.
4+
# Copyright (C) 2024 Nethesis S.r.l.
5+
# SPDX-License-Identifier: GPL-3.0-or-later
216
#
227

238
import sys
249
import json
2510
import agent
11+
import os
2612

2713
request = json.load(sys.stdin)
2814

2915
original_environment = request['environment']
3016

3117
for evar in [
32-
"LOKI_ADDR",
33-
"LOKI_API_AUTH_USERNAME",
34-
"LOKI_API_AUTH_PASSWORD",
35-
"LOKI_LOGS_INGRESS_TOKEN",
36-
"LOKI_HTTP_PORT",
3718
"LOKI_RETENTION_PERIOD",
19+
"LOKI_ACTIVE_FROM",
20+
# NOTE: LOKI_ACTIVE_TO is restored by a later step
3821
]:
3922
agent.set_env(evar, original_environment[evar])
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env sh
2+
3+
#
4+
# Copyright (C) 2024 Nethesis S.r.l.
5+
# SPDX-License-Identifier: GPL-3.0-or-later
6+
#
7+
8+
exec 1>&2
9+
10+
# Stop the pod
11+
systemctl --user stop loki.service
12+
13+
# At index 20, volume contents are restored by Restic.
14+
podman volume rm loki-server-data
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python3
2+
3+
#
4+
# Copyright (C) 2024 Nethesis S.r.l.
5+
# SPDX-License-Identifier: GPL-3.0-or-later
6+
#
7+
8+
import sys
9+
import agent
10+
import subprocess
11+
import os
12+
import sys
13+
14+
try:
15+
# Find the biggest modification timestamp among Loki's subdirs,
16+
# and print it in ISO8601 format. Note that the timezone requires
17+
# the ":" separator, otherwise the parsing fails.
18+
find_latest_change_script = """find /loki -type d |
19+
xargs -- stat -c %Y |
20+
awk 'BEGIN { ts = 0 } ; { ts = $1 > ts ? $1 : ts ; } ; END { print ts }' |
21+
xargs -IDATE -- date -u -d @DATE +%Y-%m-%dT%H:%M:%S.0+00:00
22+
"""
23+
proc_active_to = subprocess.run(["podman", "run", "-i",
24+
"--name=loki-server-restore", "--replace", "--rm",
25+
"--network=none", "--volume=loki-server-data:/loki:z",
26+
'--entrypoint=["ash","-s"]',
27+
os.environ["LOKI_IMAGE"],
28+
],
29+
stdout=subprocess.PIPE,
30+
stderr=sys.stderr,
31+
input=find_latest_change_script,
32+
text=True,
33+
check=True,
34+
)
35+
except Exception as ex:
36+
print(agent.SD_WARNING + "Cannot restore LOKI_ACTIVE_TO:", str(ex), file=sys.stderr)
37+
else:
38+
agent.set_env("LOKI_ACTIVE_TO", proc_active_to.stdout.strip())
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env sh
2+
3+
#
4+
# Copyright (C) 2024 Nethesis S.r.l.
5+
# SPDX-License-Identifier: GPL-3.0-or-later
6+
#
7+
8+
exec 1>&2
9+
10+
systemctl --user start loki.service

imageroot/systemd/user/loki.service

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ ExecStartPre=/usr/bin/podman pod create \
1313
--infra-conmon-pidfile %t/loki.pid \
1414
--pod-id-file %t/loki.pod-id \
1515
--name loki \
16-
--publish ${LOKI_HTTP_PORT}:${LOKI_HTTP_PORT} \
17-
--replace \
18-
--network=slirp4netns:allow_host_loopback=true
16+
--publish ${LOKI_HTTP_PORT}:8080 \
17+
--replace
1918
ExecStart=/usr/bin/podman pod start --pod-id-file %t/loki.pod-id
2019
ExecStop=/usr/bin/podman pod stop --ignore --pod-id-file %t/loki.pod-id -t 10
2120
ExecStopPost=/usr/bin/podman pod rm --ignore -f --pod-id-file %t/loki.pod-id

imageroot/systemd/user/traefik.service

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ExecStart=/usr/bin/podman run \
1919
--name=%N \
2020
--volume=./traefik.yaml:/etc/traefik.yaml:Z \
2121
${TRAEFIK_IMAGE} \
22-
--entryPoints.loki.address=:${LOKI_HTTP_PORT} \
22+
--entryPoints.loki.address=:8080 \
2323
--providers.file.filename=/etc/traefik.yaml
2424
ExecStop=/usr/bin/podman stop --ignore --cidfile %t/traefik.ctr-id -t 10
2525
ExecStopPost=/usr/bin/podman rm --ignore -f --cidfile %t/traefik.ctr-id

0 commit comments

Comments
 (0)