about summary refs log tree commit diff
path: root/pkgs/servers/etcd/3.5/update.sh
blob: 329161188e3972432e40918f0f6e4e0c93b510fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p curl gnugrep gnused jq nix-prefetch

set -x -eu -o pipefail

ETCD_VERSION_MAJOR_MINOR=`basename "$PWD"`

ETCD_PKG_NAME=etcd_$(echo $ETCD_VERSION_MAJOR_MINOR | sed 's/[.]/_/g')
NIXPKGS_PATH="$(git rev-parse --show-toplevel)"
ETCD_PATH="$(dirname "$0")"

OLD_VERSION="$(nix-instantiate --eval -E "with import $NIXPKGS_PATH {}; \
    $ETCD_PKG_NAME.version or (builtins.parseDrvName $ETCD_PKG_NAME.name).version" | tr -d '"')"

LATEST_TAG=$(curl ${GITHUB_TOKEN:+" -u \":$GITHUB_TOKEN\""} --silent https://api.github.com/repos/etcd-io/etcd/releases \
    | jq -r 'map(.tag_name)' | grep $ETCD_VERSION_MAJOR_MINOR | sed 's|[", ]||g' | sort -rV | head -n1)

LATEST_VERSION=$(echo ${LATEST_TAG} | sed 's/^v//')

if [ ! "$OLD_VERSION" = "$LATEST_VERSION" ]; then
    echo "Attempting to update etcd from $OLD_VERSION to $LATEST_VERSION"
    ETCD_SRC_HASH=$(nix-prefetch-url --quiet --unpack https://github.com/etcd-io/etcd/archive/refs/tags/${LATEST_TAG}.tar.gz)
    ETCD_SRC_HASH=$(nix hash to-sri --type sha256 $ETCD_SRC_HASH)

    setKV () {
        sed -i "s|$1 = \".*\"|$1 = \"${2:-}\"|" "$ETCD_PATH/default.nix"
    }

    setKV version $LATEST_VERSION
    setKV etcdSrcHash $ETCD_SRC_HASH

    getAndSetVendorHash () {
        local EMPTY_HASH="sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" # Hash from lib.fakeHash
        local VENDOR_HASH=$EMPTY_HASH
        local PKG_KEY=$1
        local INNER_PKG=$2

        setKV $PKG_KEY $EMPTY_HASH

        set +e
        VENDOR_HASH=$(nix-prefetch -I nixpkgs=$NIXPKGS_PATH "{ sha256 }: \
            (import $NIXPKGS_PATH/. {}).$ETCD_PKG_NAME.passthru.$INNER_PKG.goModules.overrideAttrs (_: { vendorHash = sha256; })")
        set -e

        if [ -n "${VENDOR_HASH:-}" ]; then
            setKV $PKG_KEY $VENDOR_HASH
        else
            echo "Update failed. $PKG_KEY is empty."
            exit 1
        fi
    }

    getAndSetVendorHash etcdServerVendorHash etcdserver
    getAndSetVendorHash etcdUtlVendorHash etcdutl
    getAndSetVendorHash etcdCtlVendorHash etcdctl

    # `git` flag here is to be used by local maintainers to speed up the bump process
    if [ $# -eq 1 ] && [ "$1" = "git" ]; then
        git switch -c "package-$ETCD_PKG_NAME-$LATEST_VERSION"
        git add "$ETCD_PATH"/default.nix
        git commit -m "$ETCD_PKG_NAME: $OLD_VERSION -> $LATEST_VERSION

Release: https://github.com/etcd-io/etcd/releases/tag/v$LATEST_VERSION"
    fi

else
    echo "etcd is already up-to-date at $OLD_VERSION"
fi