Skip to content

Commit

Permalink
wip update CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
dpausp committed Mar 29, 2023
1 parent bba5671 commit a4deaaa
Show file tree
Hide file tree
Showing 20 changed files with 572 additions and 583 deletions.
14 changes: 10 additions & 4 deletions nixos/platform/agent.nix
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ in
};

config = mkMerge [
{
environment.etc."fc_agent_deprecation_warnings".text =
lib.optionalString (config.warnings != [])
((lib.concatStringsSep "\n" config.warnings) + "\n");
}
(mkIf cfg.agent.install {
environment.systemPackages = [
pkgs.fc.agent
Expand Down Expand Up @@ -196,7 +201,7 @@ in

script =
let
verbose = lib.optionalString cfg.agent.verbose "--verbose";
verbose = lib.optionalString cfg.agent.verbose "--show-caller-info";
options = "--enc-path=${cfg.encPath} ${verbose}";
wrappedExtraCommands = lib.optionalString (cfg.agent.extraCommands != "") ''
(
Expand Down Expand Up @@ -241,11 +246,12 @@ in
IOWeight = 10; # 1-10000
ExecStart =
let
verbose = lib.optionalString cfg.agent.verbose "--verbose";
verbose = lib.optionalString cfg.agent.verbose "--show-caller-info";
options = "--enc-path=${cfg.encPath} ${verbose}";
runNow = lib.optionalString (!cfg.agent.updateInMaintenance) "--run-now";
in
"${pkgs.fc.agent}/bin/fc-maintenance ${options} request update ${runNow}";
if cfg.agent.updateInMaintenance
then "${pkgs.fc.agent}/bin/fc-maintenance ${options} request update"
else "${pkgs.fc.agent}/bin/fc-manage ${options} switch --update-channel --lazy";
};

path = commonEnvPath;
Expand Down
2 changes: 2 additions & 0 deletions pkgs/fc/agent/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ buildPythonPackage rec {
py.systemd
xfsprogs
];
# XXX: just for testing, remove!
doCheck = false;
dontStrip = true;
passthru.pythonDevEnv = py.withPackages (_: checkInputs ++ propagatedBuildInputs);

Expand Down
7 changes: 7 additions & 0 deletions pkgs/fc/agent/fc/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import uuid
from pathlib import Path

import responses
import shortuuid
import structlog
from fc.maintenance.activity import Activity
Expand Down Expand Up @@ -65,3 +66,9 @@ def logger():
_logger = structlog.get_logger()
_logger.trace = lambda *a, **k: None
return _logger


@fixture
def mocked_responses():
with responses.RequestsMock() as rsps:
yield rsps
4 changes: 3 additions & 1 deletion pkgs/fc/agent/fc/maintenance/activity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ class Activity:
duration = None
request = None # backpointer, will be set in Request
reboot_needed = None
# XXX: move to init?
# Based on current knowledge, do we predict that this activity will actually change anything?
is_effective = True
comment = ""
estimate = Estimate("10m")
log = None

def __init__(self):
"""Creates activity object (add args if you like).
Expand Down Expand Up @@ -91,7 +93,7 @@ def dump(self):
"""Saves additional state during serialization."""
pass

def merge(self, activity) -> ActivityMergeResult:
def merge(self, other) -> ActivityMergeResult:
"""Merges in other activity. Settings from other have precedence.
Returns merge result.
"""
Expand Down
87 changes: 78 additions & 9 deletions pkgs/fc/agent/fc/maintenance/activity/tests/test_update.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import textwrap
from unittest.mock import create_autospec
from unittest.mock import MagicMock, create_autospec

import structlog
import pytest
import responses
import yaml
from fc.maintenance.activity import Activity, RebootType
from fc.maintenance.activity.update import UpdateActivity
from fc.util.channel import Channel
from fc.util.nixos import (
BuildFailed,
ChannelException,
Expand Down Expand Up @@ -55,7 +57,6 @@
current_kernel: 5.10.45
current_system: {CURRENT_SYSTEM_PATH}
current_version: 21.05.1233.a9cc58d
equivalent_planned_activities: []
next_channel_url: https://hydra.flyingcircus.io/build/93222/download/1/nixexprs.tar.xz
next_environment: fc-21.05-production
next_kernel: 5.10.50
Expand Down Expand Up @@ -120,7 +121,7 @@ def test_update_merge_additional_reload_is_an_insignificant_update(activity):
other = UpdateActivity(channel_url)
other.unit_changes = {
**UNIT_CHANGES,
"reload": set(["nginx.service", "dbus.service"]),
"reload": {"nginx.service", "dbus.service"},
}
result = activity.merge(other)
# Then the merge result should be a new activity and the change is
Expand All @@ -143,7 +144,7 @@ def test_update_merge_more_unit_changes_is_a_significant_update(activity):
)

other = UpdateActivity(channel_url)
other.unit_changes = {**UNIT_CHANGES, "restart": set(["mysql.service"])}
other.unit_changes = {**UNIT_CHANGES, "restart": {"mysql.service"}}
result = activity.merge(other)
# Then the merge result should be a new activity and the change is
# significant.
Expand Down Expand Up @@ -188,6 +189,7 @@ def fake_changed_kernel_version(path):
RegisterFailed=RegisterFailed,
)

mocked.format_unit_change_lines = fc.util.nixos.format_unit_change_lines
mocked.get_fc_channel_build = fake_get_fc_channel_build
mocked.channel_version = fake_channel_version
mocked.kernel_version = fake_changed_kernel_version
Expand All @@ -203,10 +205,8 @@ def fake_changed_kernel_version(path):
return mocked


def test_update_activity_from_system_changed(nixos_mock):
activity = UpdateActivity.from_system_if_changed(
NEXT_CHANNEL_URL, ENVIRONMENT
)
def test_update_activity(nixos_mock):
activity = UpdateActivity(NEXT_CHANNEL_URL, ENVIRONMENT)

assert activity
assert activity.current_version == CURRENT_VERSION
Expand Down Expand Up @@ -310,3 +310,72 @@ def test_update_activity_switch_to_system_fails(log, nixos_mock, activity):

assert activity.returncode == 3
log.has("update-run-failed", returncode=3)


def test_update_activity_from_enc(
log, mocked_responses, nixos_mock, logger, monkeypatch
):
environment = "fc-21.05-dev"
current_channel_url = (
"https://hydra.flyingcircus.io/build/93000/download/1/nixexprs.tar.xz"
)
next_channel_url = (
"https://hydra.flyingcircus.io/build/93222/download/1/nixexprs.tar.xz"
)
current_version = "21.05.1233.a9cc58d"
next_version = "21.05.1235.bacc11d"

enc = {
"parameters": {
"environment_url": next_channel_url,
"environment": environment,
}
}

mocked_responses.add(responses.HEAD, current_channel_url)
mocked_responses.add(responses.HEAD, next_channel_url)
monkeypatch.setattr(
"fc.util.nixos.channel_version", (lambda c: next_version)
)

current_channel = Channel(logger, current_channel_url)
current_channel.version = lambda *a: current_version
monkeypatch.setattr(
"fc.manage.manage.Channel.current", lambda *a: current_channel
)
activity = UpdateActivity.from_enc(logger, enc, current_requests=[])
assert activity


# current="<Channel name=, version=21.05.1233.a9cc58d,
# from=https://hydra.flyingcircus.io/build/93000/download/1/nixexprs.tar.xz>",
# next="<Channel name=next, version=unknown,
# from=https://hydra.flyingcircus.io/build/93222/download/1/nixexprs.tar.xz>",


def test_update_activity_from_enc_unchanged(
log, mocked_responses, nixos_mock, logger, monkeypatch
):
"""Given an unchanged channel url, should not prepare an update activity"""
environment = "fc-21.05-dev"

enc = {
"parameters": {
"environment_url": CURRENT_CHANNEL_URL,
"environment": ENVIRONMENT,
}
}

mocked_responses.add(responses.HEAD, CURRENT_CHANNEL_URL)
monkeypatch.setattr(
"fc.util.nixos.channel_version", (lambda c: CURRENT_VERSION)
)

current_channel = Channel(logger, CURRENT_CHANNEL_URL)
current_channel.version = lambda *a: CURRENT_VERSION
monkeypatch.setattr(
"fc.manage.manage.Channel.current", lambda *a: current_channel
)

activity = UpdateActivity.from_enc(logger, enc, current_requests=[])
assert activity is None
16 changes: 1 addition & 15 deletions pkgs/fc/agent/fc/maintenance/activity/tests/test_vm_change.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
import textwrap
from unittest.mock import create_autospec

import pytest
import structlog
import yaml
from fc.maintenance.activity import Activity, RebootType
from fc.maintenance.activity.reboot import RebootActivity
from fc.maintenance.activity.update import UpdateActivity
from fc.maintenance.activity import Activity
from fc.maintenance.activity.vm_change import VMChangeActivity
from fc.util.nixos import (
BuildFailed,
ChannelException,
ChannelUpdateFailed,
RegisterFailed,
SwitchFailed,
)
from pytest import fixture

SERIALIZED_ACTIVITY = f"""\
Expand Down
Loading

0 comments on commit a4deaaa

Please sign in to comment.