Skip to content

Commit

Permalink
refactor(pcs): move to nix-update-script
Browse files Browse the repository at this point in the history
  • Loading branch information
matt1432 committed Dec 18, 2024
1 parent 748560a commit d880d71
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 87 deletions.
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ rec {
nix-prefetch-git
nix-prefetch-github
nix-prefetch-scripts
nix-update
];
};

Expand Down
18 changes: 12 additions & 6 deletions pkgs/pcs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
pcs-web-ui ? null,
...
}: let
inherit (lib) getLib optionalString removePrefix;
inherit (lib) getLib optionalString;

pcs-src = import ./src.nix;
version = removePrefix "v" pcs-src.rev;
pname = "pcs";
version = "0.12.0b1";

rubyEnv = bundlerEnv {
name = "pcs-env-${version}";
Expand All @@ -35,12 +35,16 @@
};
in
python3Packages.buildPythonPackage {
pname = "pcs";
inherit version;
inherit pname version;

pyproject = true;

src = fetchFromGitHub pcs-src;
src = fetchFromGitHub {
owner = "ClusterLabs";
repo = "pcs";
rev = "v${version}";
hash = "sha256-5c1KwXJBvr9yeKYnt04V9M2v+mz7utlbpy820/GzE9o=";
};

# Curl test assumes network access
doCheck = false;
Expand Down Expand Up @@ -154,4 +158,6 @@ in
rm -r $out/lib/pcsd/public/
ln -s ${pcs-web-ui}/lib/pcsd/public $out/lib/pcsd/public
'';

passthru.updateScript = ./update.sh;
}
7 changes: 0 additions & 7 deletions pkgs/pcs/src.nix

This file was deleted.

39 changes: 39 additions & 0 deletions pkgs/pcs/update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env -S nix develop .#update -c bash

ARGS=("$@")

getLatest() {
type="$1"
owner="$2"
repo="$3"

case "$type" in
release)
curl -s "https://api.github.com/repos/$owner/$repo/releases/latest" | jq -r .tag_name
;;

prerelease)
curl -s "https://api.github.com/repos/$owner/$repo/releases" |
jq -r 'map(.tag_name)[]' |
sort -r |
head -n 1
;;
esac
}

updatePackage() {
versionType="$1"
owner="$2"
repo="$3"

current_version=$(nix eval --raw ".#$repo.version")
new_version=$(getLatest "$versionType" "$owner" "$repo")

if [[ "$new_version" != "$current_version" ]]; then
updateGems

nix-update --version "$new_version" --flake pcs "${ARGS[@]}"
fi
}

updatePackage "prerelease" "ClusterLabs" "pcs" # TODO: move to release once 0.12 comes out
92 changes: 18 additions & 74 deletions update.sh
Original file line number Diff line number Diff line change
@@ -1,81 +1,25 @@
#!/usr/bin/env -S nix develop .#update -c bash

COMMIT="$1"
ROOT_DIR="$(pwd)"

git_push() {
if [[ "$COMMIT" == "--commit" ]]; then
(
cd "$ROOT_DIR" || return
git config --global user.name 'Updater'
git config --global user.email 'robot@nowhere.invalid'
git remote update

alejandra .
git add .

git commit -m "$1"
git push
)
else
echo "$1"
fi
}

updateRubyDeps() {
updateGems
git_push "chore: update ruby deps"
updatePackage() {
script="$(nix eval --raw .#"$1".updateScript)"
$script "${@:2}"
}
getLatest() {
type="$1"
owner="$2"
repo="$3"

case "$type" in
release)
curl -s "https://api.github.com/repos/$owner/$repo/releases/latest" | jq -r .tag_name
;;

prerelease)
curl -s "https://api.github.com/repos/$owner/$repo/releases" |
jq -r 'map(.tag_name)[]' |
sort -r |
head -n 1
;;
esac
}
if [[ "$1" == "--commit" ]]; then
git config --global user.name 'github-actions[bot]'
git config --global user.email '41898282+github-actions[bot]@users.noreply.github.com'
updatePackage() {
nix flake update
versionType="$1"
owner="$2"
repo="$3"
file="$ROOT_DIR/pkgs/$repo/src.nix"

current_version=$(nix eval --json --file "$file" | jq -r .rev)
new_version=$(getLatest "$versionType" "$owner" "$repo")

if [[ "$new_version" != "$current_version" ]]; then
hash=$(nix-prefetch-github "$owner" "$repo" --rev "$new_version" |
jq -r .hash)

{
echo '# This file was autogenerated. DO NOT EDIT!'
echo '{'
echo " owner = \"$owner\";"
echo " repo = \"$repo\";"
echo " rev = \"$new_version\";"
echo " hash = \"$hash\";"
echo '}'
} >"$file"

git_push "ci: $repo $current_version -> $new_version"
else
echo "$repo is already up to date"
fi
}

updateRubyDeps
updatePackage "prerelease" "ClusterLabs" "pcs" # TODO: move to release once 0.12 comes out
updatePackage "pacemaker" --commit
updatePackage "pcs" --commit
updatePackage "pcs-web-ui" --commit
updatePackage "resource-agents" --commit
git restore .
else
updatePackage "pacemaker"
updatePackage "pcs"
updatePackage "pcs-web-ui"
updatePackage "resource-agents"
fi

0 comments on commit d880d71

Please sign in to comment.