about summary refs log tree commit diff
path: root/genenetwork-local-container.sh
blob: ca67d4a2f511a3d278fc8d774d334a4c8085942b (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#! /bin/sh -e

# genenetwork-machines --- Guix configuration for genenetwork machines
# Copyright © 2025 Munyoki Kilyungi <me@bonfacemunyoki.com>
#
# This file is part of genenetwork-machines.
#
# genenetwork-machines is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# genenetwork-machines is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with genenetwork-machines.  If not, see
# <https://www.gnu.org/licenses/>.

# Build and install genenetwork container on your local machine.

set -euo pipefail


BASE_DIR="${HOME:-/home/$USER}/genenetwork"
SYSTEM_DIRECTORIES=(
    "$BASE_DIR/var/log"
    "$BASE_DIR/var/genenetwork"
    "$BASE_DIR/etc/genenetwork/conf"
    "$BASE_DIR/etc/genenetwork"
    "$BASE_DIR/var/lib/redis"
    "$BASE_DIR/var/lib/virtuoso"
    "$BASE_DIR/var/lib/xapian"
    "$BASE_DIR/var/lib/genenetwork-sqlite"
    "$BASE_DIR/var/lib/genenetwork-gnqa"
    "/tmp/local-container"
)

GN_PROJECTS=(
    "genenetwork2:git@github.com:genenetwork/genenetwork2"
    "genenetwork3:git@github.com:genenetwork/genenetwork3"
    "gn-auth:https://git.genenetwork.org/gn-auth"
    "gn-docs:https://git.genenetwork.org/gn-docs"
)

CONTAINER_SCM="genenetwork-local-container.scm"
CONTAINER_BIN="/usr/local/bin/genenetwork-local-container"
GC_ROOT="/var/guix/gcroots/genenetwork-local-container"

log() {
    local level="$1"
    shift
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $level: $*" >&2
}

# Check dependencies
for cmd in git guix sudo; do
    if ! command -v "$cmd" &>/dev/null; then
        log "ERROR" "Required command '$cmd' not found"
        exit 1
    fi
done

# Validate HOME is set
if [ -z "${HOME:-}" ]; then
    log "ERROR" "HOME environment variable is not set"
    exit 1
fi

log "INFO" "Creating system directories..."
for dir in "${SYSTEM_DIRECTORIES[@]}"; do
    # Check if directory exists and is accessible
    if [ -d "$dir" ]; then
        if [ -w "$dir" ]; then
            log "DEBUG" "Directory exists and is writable: $dir"
        else
            log "WARNING" "Directory exists but is not writable: $dir"
        fi
        continue
    fi

    # Attempt to create directory
    log "INFO" "Creating directory: $dir"
    if [ -w "$(dirname "$dir")" ]; then
        # Parent directory is writable, try without sudo
        if ! mkdir -p "$dir"; then
            log "ERROR" "Failed to create directory without sudo: $dir"
            exit 1
        fi
    else
        # Parent directory requires root, use sudo
        if ! sudo mkdir -p "$dir"; then
            log "ERROR" "Failed to create directory with sudo: $dir"
            exit 1
        fi
        # Set ownership to current user if created with sudo
        if ! sudo chown "$USER:$USER" "$dir"; then
            log "WARNING" "Failed to set ownership for: $dir"
        fi
    fi
done

is_git_repository() {
    local dir="$1"
    # Check for standard repository
    if [ -d "$dir/.git" ]; then
        if [ -f "$dir/.git/HEAD" ] && [ -d "$dir/.git/refs" ]; then
            log "DEBUG" "Detected standard Git repository: $dir"
            return 0
        else
            log "ERROR" "Directory $dir/.git exists but is not a valid Git repository"
            return 1
        fi
    # Check for bare repository
    elif [ -f "$dir/HEAD" ] && [ -d "$dir/refs" ] && [ -d "$dir/objects" ]; then
        log "DEBUG" "Detected bare Git repository: $dir"
        return 0
    else
        log "ERROR" "$dir exists but is not a Git repository (neither standard nor bare)"
        return 1
    fi
}
# Clone GeneNetwork projects
log "INFO" "Cloning GeneNetwork projects..."
for project_entry in "${GN_PROJECTS[@]}"; do
    IFS=':' read -r project repo_url <<< "$project_entry"
    dir="$BASE_DIR/$project"
    if [ ! -d "$dir" ]; then
        log "INFO" "Cloning $project from $repo_url to $dir"
        if ! git clone "$repo_url" "$dir"; then
            log "ERROR" "Failed to clone $project"
            exit 1
        fi
    else
        log "DEBUG" "Directory exists, skipping clone: $dir"
        if ! is_git_repository "$dir"; then
            log "ERROR" "$dir exists but is not a Git repository"
            exit 1
        fi
    fi
done

# Verify container SCM file exists
if [ ! -f "$CONTAINER_SCM" ]; then
    log "ERROR" "Container SCM file not found: $CONTAINER_SCM"
    exit 1
fi

# Create Guix system container
log "INFO" "Creating Guix system container..."
SHARE_OPTS=(
    "--share=$BASE_DIR/var/log=/var/log"
    "--share=$BASE_DIR/var/genenetwork=/var/genenetwork"
    "--share=$BASE_DIR/etc/genenetwork/conf=/etc/genenetwork/conf"
    "--share=$BASE_DIR/etc/genenetwork=/etc/genenetwork"
    "--share=$BASE_DIR/var/lib/redis=/var/lib/redis"
    "--share=$BASE_DIR/var/lib/virtuoso=/var/lib/virtuoso"
    "--share=$BASE_DIR/genenetwork2=/genenetwork2"
    "--share=$BASE_DIR/genenetwork3=/genenetwork3"
    "--share=$BASE_DIR/gn-auth=/gn-auth"
    "--share=$BASE_DIR/var/lib/xapian=/var/lib/xapian"
    "--share=$BASE_DIR/var/lib/genenetwork-sqlite=/var/lib/genenetwork-sqlite"
    "--share=$BASE_DIR/var/lib/genenetwork-gnqa=/var/lib/genenetwork-gnqa"
    "--share=/tmp/local-container=/tmp"
    "--share=$BASE_DIR/gn-docs=/var/lib/gn-docs"
    "--share=/run/mysqld=/run/mysqld"
)

container_script=$(guix system container \
    --network \
    --load-path=. \
    --verbosity=3 \
    "${SHARE_OPTS[@]}" \
    "$CONTAINER_SCM")

log $container_script

# Create symbolic links
log "INFO" "Creating symbolic links..."
if ! sudo ln -sf "$container_script" "$CONTAINER_BIN"; then
    log "ERROR" "Failed to create symbolic link: $CONTAINER_BIN"
    exit 1
fi

if ! sudo ln -sf "$container_script" "$GC_ROOT"; then
    log "ERROR" "Failed to create GC root link: $GC_ROOT"
    exit 1
fi

log "INFO" "Setup completed successfully!"
log "INFO" "Container script: $container_script"
log "INFO" "Run with: $CONTAINER_BIN"