Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the restore procedure #8

Merged
merged 4 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 5 additions & 22 deletions imageroot/actions/restore-module/06copyenv
Original file line number Diff line number Diff line change
@@ -1,39 +1,22 @@
#!/usr/bin/env python3

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

import sys
import json
import agent
import os

request = json.load(sys.stdin)

original_environment = request['environment']

for evar in [
"LOKI_ADDR",
"LOKI_API_AUTH_USERNAME",
"LOKI_API_AUTH_PASSWORD",
"LOKI_LOGS_INGRESS_TOKEN",
"LOKI_HTTP_PORT",
"LOKI_RETENTION_PERIOD",
"LOKI_ACTIVE_FROM",
# NOTE: LOKI_ACTIVE_TO is restored by a later step
]:
agent.set_env(evar, original_environment[evar])
14 changes: 14 additions & 0 deletions imageroot/actions/restore-module/07reinit_data
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env sh

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

exec 1>&2

# Stop the pod
systemctl --user stop loki.service

# At index 20, volume contents are restored by Restic.
podman volume rm loki-server-data
38 changes: 38 additions & 0 deletions imageroot/actions/restore-module/85restore_active_to
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

import sys
import agent
import subprocess
import os
import sys

try:
# Find the biggest modification timestamp among Loki's subdirs,
# and print it in ISO8601 format. Note that the timezone requires
# the ":" separator, otherwise the parsing fails.
find_latest_change_script = """find /loki -type d |
xargs -- stat -c %Y |
awk 'BEGIN { ts = 0 } ; { ts = $1 > ts ? $1 : ts ; } ; END { print ts }' |
xargs -IDATE -- date -u -d @DATE +%Y-%m-%dT%H:%M:%S.0+00:00
"""
proc_active_to = subprocess.run(["podman", "run", "-i",
"--name=loki-server-restore", "--replace", "--rm",
"--network=none", "--volume=loki-server-data:/loki:z",
'--entrypoint=["ash","-s"]',
os.environ["LOKI_IMAGE"],
],
stdout=subprocess.PIPE,
stderr=sys.stderr,
input=find_latest_change_script,
text=True,
check=True,
)
except Exception as ex:
print(agent.SD_WARNING + "Cannot restore LOKI_ACTIVE_TO:", str(ex), file=sys.stderr)
else:
agent.set_env("LOKI_ACTIVE_TO", proc_active_to.stdout.strip())
10 changes: 10 additions & 0 deletions imageroot/actions/restore-module/90start_server
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env sh

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

exec 1>&2

systemctl --user start loki.service
5 changes: 2 additions & 3 deletions imageroot/systemd/user/loki.service
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ ExecStartPre=/usr/bin/podman pod create \
--infra-conmon-pidfile %t/loki.pid \
--pod-id-file %t/loki.pod-id \
--name loki \
--publish ${LOKI_HTTP_PORT}:${LOKI_HTTP_PORT} \
--replace \
--network=slirp4netns:allow_host_loopback=true
--publish ${LOKI_HTTP_PORT}:8080 \
--replace
ExecStart=/usr/bin/podman pod start --pod-id-file %t/loki.pod-id
ExecStop=/usr/bin/podman pod stop --ignore --pod-id-file %t/loki.pod-id -t 10
ExecStopPost=/usr/bin/podman pod rm --ignore -f --pod-id-file %t/loki.pod-id
Expand Down
2 changes: 1 addition & 1 deletion imageroot/systemd/user/traefik.service
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ExecStart=/usr/bin/podman run \
--name=%N \
--volume=./traefik.yaml:/etc/traefik.yaml:Z \
${TRAEFIK_IMAGE} \
--entryPoints.loki.address=:${LOKI_HTTP_PORT} \
--entryPoints.loki.address=:8080 \
--providers.file.filename=/etc/traefik.yaml
ExecStop=/usr/bin/podman stop --ignore --cidfile %t/traefik.ctr-id -t 10
ExecStopPost=/usr/bin/podman rm --ignore -f --cidfile %t/traefik.ctr-id
Expand Down
Loading