Skip to content

Commit

Permalink
Merge pull request #862 from kernelkit/dagger-mem-usage
Browse files Browse the repository at this point in the history
Dagger memory usage
  • Loading branch information
wkz authored Dec 10, 2024
2 parents 219c610 + 429c4f1 commit ec64c15
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 17 deletions.
71 changes: 54 additions & 17 deletions src/confd/bin/dagger
Original file line number Diff line number Diff line change
Expand Up @@ -84,50 +84,80 @@ EOF
done
}

do_exec()
maybe_get_current()
{
echo $([ -f "$basedir/current" ] && cat "$basedir/current" || true)
[ ! "$current" ] || [ -d "$basedir/$current" ] \
|| abort "Current generation does not exist"
}

get_current()
{
local action="$1"
local current=

current=$([ -f "$basedir/current" ] && cat "$basedir/current")
current=$(maybe_get_current)
[ "$current" ] && [ -d "$basedir/$current" ] \
|| abort "Current generation does not exist"

action_exec "$1" "$basedir/$current"
echo "$current"
}

do_abandon()
get_next()
{
local next=

next=$([ -f "$basedir/next" ] && cat "$basedir/next" || true)
[ "$next" ] && [ -d "$basedir/$next" ] \
|| abort "Next generation does not exist"

mv "$basedir/$next" "$basedir/$next-ABANDONED-$(date +%F-%T)"
rm "$basedir/next"
inform info "Abandoned generation $next"
echo "$next"
}

do_evolve()
do_exec()
{
local current=
local next=
local action="$1"

current=$([ -f "$basedir/current" ] && cat "$basedir/current" || true)
[ ! "$current" ] || [ -d "$basedir/$current" ] \
|| abort "Current generation does not exist"
action_exec "$1" "$basedir/$(get_current)"
}

next=$([ -f "$basedir/next" ] && cat "$basedir/next" || true)
[ "$next" ] && [ -d "$basedir/$next" ] \
|| abort "Next generation does not exist"
do_abandon()
{
local next=$(get_next)

date +%F-%T >"$basedir/$next/ABANDONED"

mv "$basedir/next" "$basedir/current"
inform info "Abandoned generation $next"
}

do_evolve()
{
local current=$(maybe_get_current)
local next=$(get_next)
local ar=

[ "$current" ] && action_exec exit "$basedir/$current"

action_exec init "$basedir/$next"

mv "$basedir/next" "$basedir/current"
inform info "Evolved to generation $next"

if [ "$current" ]; then
ar="$current.tar.gz"
[ -f "$basedir/$current/ABANDONED" ] && ar="$current-ABANDONED.tar.gz"

(cd "$basedir" && tar caf "$ar.tar.gz" "$current")
rm -rf "$basedir/$current"
fi
}

do_prune()
{
local keep=${1:-10}

cd "$basedir"
rm -f old=$(ls -rv *.tar.gz | tail "+$((keep + 1))")
}

usage()
Expand Down Expand Up @@ -158,6 +188,10 @@ Commands:
exec <action>
Run the specified action of the current generation.
prune [<num-generations>]
Remove all but the last <num-generations>, or 10 by default,
archived generations.
help
Show this message.
Expand Down Expand Up @@ -201,6 +235,9 @@ case $cmd in
"exec")
do_exec "$@"
;;
"prune")
do_prune "$@"
;;

*)
usage && exit 1
Expand Down
12 changes: 12 additions & 0 deletions src/confd/src/dagger.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,23 @@ int dagger_evolve(struct dagger *d)
return exitcode;
}

static int dagger_prune(struct dagger *d)
{
int exitcode;

exitcode = systemf("dagger -C %s prune", d->path);
DEBUG("dagger(%d->%d): prune: exitcode=%d\n",
d->current, d->next, exitcode);

return exitcode;
}

int dagger_evolve_or_abandon(struct dagger *d)
{
int exitcode, err;

exitcode = dagger_evolve(d);
dagger_prune(d);
if (!exitcode)
return 0;

Expand Down
5 changes: 5 additions & 0 deletions src/confd/src/ietf-interfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ static int netdag_exit_reload(struct dagger *net)
{
FILE *initctl;

if (systemf("runlevel >/dev/null 2>&1"))
/* If we are still bootstrapping, there is nothing to
* reload. */
return 0;

/* We may end up writing this file multiple times, e.g. if
* multiple services are disabled in the same config cycle,
* but since the contents of the file are static it doesn't
Expand Down

0 comments on commit ec64c15

Please sign in to comment.