summarylogtreecommitdiffstats
path: root/foundationdb-server.install
blob: 52c064908448c17a930cf5c955a536d26e1caf38 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Define the service name globally for consistency
SERVICE_NAME="foundationdb.service"
FDB_USER="foundationdb"
FDB_GROUP="foundationdb"

post_install() {
    CLUSTER_FILE="/etc/foundationdb/fdb.cluster"

    # 1. Create User & Group
    if ! getent group "$FDB_GROUP" >/dev/null; then
        groupadd -r "$FDB_GROUP"
    fi
    if ! getent passwd "$FDB_USER" >/dev/null; then
        useradd -r -g "$FDB_GROUP" -d /var/lib/foundationdb -s /usr/bin/nologin "$FDB_USER"
    fi

    # 2. Create Persistent Directories (Config & Data)
    if [ ! -d "/etc/foundationdb" ]; then
        mkdir -p "/etc/foundationdb"
    fi
    if [ ! -d "/var/lib/foundationdb" ]; then
        mkdir -p "/var/lib/foundationdb"
    fi

    # 3. Create Runtime Directory (Critical for PID file)
    if [ ! -d "/var/run/foundationdb" ]; then
        mkdir -p "/var/run/foundationdb"
    fi

    # 4. Create Cluster File if missing
    if [ ! -f "$CLUSTER_FILE" ]; then
        echo "Creating new cluster file: $CLUSTER_FILE"
        description=$(LC_CTYPE=C tr -dc A-Za-z0-9 </dev/urandom | head -c 8)
        random_str=$(LC_CTYPE=C tr -dc A-Za-z0-9 </dev/urandom | head -c 8)
        echo "$description:$random_str@127.0.0.1:4500" >"$CLUSTER_FILE"
        chown "$FDB_USER:$FDB_GROUP" "$CLUSTER_FILE"
        chmod 0664 "$CLUSTER_FILE"
        NEWDB=1
    fi

    # 5. Enforce Permissions (Recursive)
    # This ensures everything is owned by foundationdb before starting
    chown -R "$FDB_USER:$FDB_GROUP" /etc/foundationdb
    chown -R "$FDB_USER:$FDB_GROUP" /var/lib/foundationdb
    chown -R "$FDB_USER:$FDB_GROUP" /var/run/foundationdb

    # 6. Start Service
    if [ -x /usr/bin/systemctl ]; then
        echo "Reloading systemd..."
        systemctl daemon-reload

        echo "Enabling and starting $SERVICE_NAME..."
        systemctl enable --now "$SERVICE_NAME" || echo "WARNING: Failed to start $SERVICE_NAME"
    fi

    # 7. Initialize Database (Only for fresh installs)
    if [ "$NEWDB" == "1" ]; then
        if [ -x /usr/bin/fdbcli ]; then
            echo "Waiting for FDB to start..."
            sleep 5
            if systemctl is-active --quiet "$SERVICE_NAME"; then
                echo "Configuring new single-node database..."
                /usr/bin/fdbcli --exec "configure new single memory" || echo "WARNING: Automatic configuration failed. Run 'configure new single memory' manually."
            else
                echo "WARNING: Service is not running. Skipping initialization."
            fi
        fi
    fi
}

pre_upgrade() {
    # Stop the service safely before binaries are replaced
    if [ -x /usr/bin/systemctl ]; then
        systemctl stop "$SERVICE_NAME" || true
    fi
}

post_upgrade() {
    # 1. Re-enforce permissions
    # (Important in case the user messed them up or directories changed)
    chown -R "$FDB_USER:$FDB_GROUP" /etc/foundationdb
    chown -R "$FDB_USER:$FDB_GROUP" /var/lib/foundationdb

    # 2. Re-create PID dir if it was lost (since /var/run is temporary)
    if [ ! -d "/var/run/foundationdb" ]; then
        mkdir -p "/var/run/foundationdb"
        chown "$FDB_USER:$FDB_GROUP" "/var/run/foundationdb"
    fi

    # 3. Handle Systemd
    if [ -x /usr/bin/systemctl ]; then
        systemctl daemon-reload
    fi

    echo "---"
    echo "FoundationDB server has been upgraded."
    echo "To restart the service with the new version, run:"
    echo "  sudo systemctl restart $SERVICE_NAME"
    echo "---"
}

pre_remove() {
    # Stop service before removal
    if [ -x /usr/bin/systemctl ]; then
        systemctl stop "$SERVICE_NAME" || true
        systemctl disable "$SERVICE_NAME" || true
    fi
}