Skip to content

Commit

Permalink
FMO-76: Add OTA update ability
Browse files Browse the repository at this point in the history
- Add storage of current system config at /var/host/FMO-OS via device-sku systemd
- Add registration-agent-laptop-binary and registration-agent-laptop-local to build registration-agent-laptop from a local binary or source code
- Add fmo-ota package that allows:
  + System config modification
  + Update new system version from upstream repository with git tag/commit
  + Ability to choose whether keep the current registration-agent-laptop or build new from given source code

Signed-off-by: Anh Huy Bui <[email protected]>
  • Loading branch information
buianhhuy96 committed Dec 5, 2024
1 parent 30243fc commit 4b30321
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 3 deletions.
1 change: 1 addition & 0 deletions hardware/fmo-os-x86_64.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"vim"
"tcpdump"
"gpsd"
"fmo-ota"
]; # systemPackages

launchers = [
Expand Down
19 changes: 19 additions & 0 deletions modules/hardwareInfo/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ in {
description = "Device Config in JSON format";
default = "";
};
systemConfig = mkOption {
type = types.str;
description = "Folder contains system config in nix format";
default = "/var/host/FMO-OS";
};
systemConfigSymlink = mkOption {
type = types.str;
description = "Folder contains system config in nix format";
default = "/home/ghaf/.sysconf";
};
skuFile = mkOption {
type = types.str;
description = "File contains SKU information generated at runtime";
Expand All @@ -30,6 +40,15 @@ in {
mkdir -p $(dirname ${cfg.skuFile})
echo $system_sku > ${cfg.skuFile}
chmod 444 ${cfg.skuFile}
if [ -d "'${cfg.systemConfig}" ]; then
echo "FMO-OS config exists"
else
mkdir -p ${cfg.systemConfig}
cp -R ${../../.}/* ${cfg.systemConfig}/
fi
chmod 666 ${cfg.systemConfig}/hardware/fmo-os-x86_64.nix
ln -sf ${cfg.systemConfig}/hardware/fmo-os-x86_64.nix ${cfg.systemConfigSymlink}
'';

wantedBy = ["multi-user.target"];
Expand Down
1 change: 1 addition & 0 deletions modules/packages/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#
_: {
nixpkgs.overlays = [
(import ./fmo-ota)
(import ./fmo-tool)
(import ./lisgd)
(import ./nmLauncher)
Expand Down
114 changes: 114 additions & 0 deletions modules/packages/fmo-ota/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Copyright 2022-2024 TII (SSRC) and the Ghaf contributors
# SPDX-License-Identifier: Apache-2.0
(final: _prev: {
fmo-ota = final.writeShellScriptBin "fmo-ota" ''
function usage() {
echo "Usage: $0 [-t <version-tag>/-c <commit-hash>] [-r <absolute-path-to-registration-agent-folder]"; exit 1;
}
while getopts ":c:t:r:s:" o; do
case "''${o}" in
c)
COMMIT=''${OPTARG}
;;
t)
TAGS=''${OPTARG}
;;
r)
REGISTRATION_AGENT=$(realpath ''${OPTARG})
;;
s)
HASH=" ''${OPTARG//\//\\/}"
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
LOCAL_SOURCE=/var/host
if [ -z "''${COMMIT}" ] && [ -z "''${TAGS}" ]; then
if [ ! -d $LOCAL_SOURCE/FMO-OS ]; then
echo "Cannot find current system config"
exit 1
fi
cd $LOCAL_SOURCE/FMO-OS/
if [ -z "''${REGISTRATION_AGENT}" ]; then
rm -rf ./modules/packages/registration-agent/RA-local
cp -R $(registration-agent-laptop -o) ./modules/packages/registration-agent/RA-binary
else
if [ ! -d "''${REGISTRATION_AGENT}" ]; then
echo "Registration Agent path not found"
exit 1
fi
rm -rf ./modules/packages/registration-agent/RA-binary
mkdir -p ./modules/packages/registration-agent/RA-local
cp -R ''${REGISTRATION_AGENT}/* ./modules/packages/registration-agent/RA-local
if [ ! -z "''${HASH}" ]; then
sed -i "11s/.*/$HASH/" ./modules/packages/registration-agent/registration-agent-laptop-local.nix
fi
fi
git init
git add *
sudo nixos-rebuild switch --flake .#fmo-os-x86_64-debug --accept-flake-config
if [ $? -eq 0 ]; then
echo "System update successfully"
else
echo "System update failed"
fi
else
cd /tmp/
git clone https://github.com/tiiuae/FMO-OS.git
cd FMO-OS/
if [ ! -z "''${TAGS}" ]; then
git checkout tags/''${TAGS}
if [ $? -eq 0 ]; then
echo "Tag: ''${TAGS} found"
else
echo "Tag: ''${TAGS} not found"
fi
else
if [ ! -z "''${COMMIT}" ]; then
git checkout ''${COMMIT}
if [ $? -eq 0 ]; then
echo "Commit: ''${TAGS} found"
else
echo "Commit: ''${TAGS} not found"
fi
fi
fi
if [ -z "''${REGISTRATION_AGENT}" ]; then
rm -rf ./modules/packages/registration-agent/RA-local
cp -R $(registration-agent-laptop -o) ./modules/packages/registration-agent/RA-binary
else
if [ ! -d "''${REGISTRATION_AGENT}" ]; then
echo "Registration Agent path not found"
exit 1
fi
rm -rf ./modules/packages/registration-agent/RA-binary
mkdir -p ./modules/packages/registration-agent/RA-local
cp -R ''${REGISTRATION_AGENT}/* ./modules/packages/registration-agent/RA-local
rm -rf ./modules/packages/registration-agent/RA-local/.git
if [ ! -z "''${HASH}" ]; then
sed -i "11s/.*/$HASH/" ./modules/packages/registration-agent/registration-agent-laptop-local.nix
fi
fi
git config --global --add safe.directory $(realpath .)
git add *
sudo nixos-rebuild switch --flake .#fmo-os-x86_64-debug --accept-flake-config
if [ $? -eq 0 ]; then
echo "System update successfully"
rm -rf $LOCAL_SOURCE/FMO-OS
mv /tmp/FMO-OS $LOCAL_SOURCE
else
echo "System update failed"
fi
fi
'';
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{ pkgs,
}:
pkgs.buildPackages.runCommand "registrationAgentOrig"
{
# for `nix run`
meta.mainProgram = "registrationAgentOrig";
} ''
mkdir -p $out/bin
${pkgs.coreutils}/bin/cp -R ${./RA-binary} $out/bin/registration-agent-laptop-orig
''
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2022-2024 TII (SSRC) and the Ghaf contributors
# SPDX-License-Identifier: Apache-2.0

{pkgs,lib}:
pkgs.buildGoModule {
name = "registration-agent-laptop";
src = ./RA-local;
tags = [ "prod" ];
patches = [./remove-test.patch];
vendorHash = ''
sha256-18p7l1otlviZNlM0UlCgW/US5YckBYcY/OEJoJIsIM0=
'';
proxyVendor=true;
postInstall = ''
mv $out/bin/registration-agent-laptop $out/bin/registration-agent-laptop-orig
'';
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@
env_path,
}:
let
registrationAgentOrig = pkgs.callPackage ./registration-agent-laptop.nix {inherit pkgs;};
registrationAgentOrig = if (builtins.pathExists ./RA-local) then
pkgs.callPackage ./registration-agent-laptop-local.nix {inherit pkgs;} else
( if (builtins.pathExists ./RA-binary) then
pkgs.callPackage ./registration-agent-laptop-binary.nix {inherit pkgs;} else
pkgs.callPackage ./registration-agent-laptop-git.nix {inherit pkgs;});
in
pkgs.writeScriptBin "registration-agent-laptop" ''
#${pkgs.bash}/bin/bash
function usage() {
echo "Usage: $0 [-e <absolute-path-to-env-file>]"; exit 1;
}
while getopts ":e:" o; do
while getopts ":e:o" o; do
case "''${o}" in
e)
env=''${OPTARG}
;;
o)
echo -n "${registrationAgentOrig}/bin/registration-agent-laptop-orig"
exit 1
;;
*)
usage
;;
Expand Down

0 comments on commit 4b30321

Please sign in to comment.