Skip to content

Commit 5fbca38

Browse files
authored
remove console download links, improve nexus url in install_minion.sh (#78)
1 parent 44acfdc commit 5fbca38

4 files changed

Lines changed: 291 additions & 21 deletions

File tree

internal/web/handlers.go

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"html/template"
88
"net/http"
9+
"os"
910
"strings"
1011
"time"
1112

@@ -106,21 +107,47 @@ func (ws *WebServer) serveDownloadIndex(w http.ResponseWriter, _ *http.Request)
106107
<a href="/download/minion/darwin-amd64" class="download-link">macOS x64</a>
107108
<a href="/download/minion/darwin-arm64" class="download-link">macOS ARM64</a>
108109
</div>
109-
<div class="download-section">
110-
<h2>Console Binaries</h2>
111-
<a href="/download/console/linux-amd64" class="download-link">Linux x64</a>
112-
<a href="/download/console/linux-arm64" class="download-link">Linux ARM64</a>
113-
<a href="/download/console/windows-amd64.exe" class="download-link">Windows x64</a>
114-
<a href="/download/console/windows-arm64.exe" class="download-link">Windows ARM64</a>
115-
<a href="/download/console/darwin-amd64" class="download-link">macOS x64</a>
116-
<a href="/download/console/darwin-arm64" class="download-link">macOS ARM64</a>
117-
</div>
118110
<p><a href="/">← Back to Dashboard</a></p>
119111
</body>
120112
</html>`
121113
w.Write([]byte(html))
122114
}
123115

116+
// handleInstallScript serves the dynamically generated install script
117+
func (ws *WebServer) handleInstallScript(w http.ResponseWriter, r *http.Request) {
118+
ws.setSecurityHeaders(w)
119+
120+
if r.Method != http.MethodGet {
121+
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
122+
return
123+
}
124+
125+
// Build the base URL using NEXUS_SERVER environment variable
126+
nexusServer := os.Getenv("NEXUS_SERVER")
127+
if nexusServer == "" {
128+
nexusServer = "localhost"
129+
}
130+
baseURL := fmt.Sprintf("http://%s:%d", nexusServer, ws.config.WebPort)
131+
132+
// Template data
133+
data := struct {
134+
BaseURL string
135+
}{
136+
BaseURL: baseURL,
137+
}
138+
139+
// Set appropriate headers for shell script
140+
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
141+
w.Header().Set("Content-Disposition", "inline; filename=install_minion.sh")
142+
143+
// Execute template
144+
if err := ws.templates.ExecuteTemplate(w, "install_minion.sh", data); err != nil {
145+
ws.logger.Error("Failed to execute install script template", zap.Error(err))
146+
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
147+
return
148+
}
149+
}
150+
124151
// handleAPIStatus serves the /api/status endpoint
125152
func (ws *WebServer) handleAPIStatus(w http.ResponseWriter, r *http.Request) {
126153
ws.setJSONHeaders(w)
@@ -373,12 +400,12 @@ func (ws *WebServer) serveBinaryFile(w http.ResponseWriter, r *http.Request, pat
373400
return
374401
}
375402

376-
component := parts[0] // minion or console
403+
component := parts[0] // minion
377404
platform := parts[1] // linux-amd64, windows-amd64.exe, etc.
378405

379406
// Validate component
380-
if component != "minion" && component != "console" {
381-
http.Error(w, "Invalid component. Must be 'minion' or 'console'", http.StatusBadRequest)
407+
if component != "minion" {
408+
http.Error(w, "Invalid component. Must be 'minion'", http.StatusBadRequest)
382409
return
383410
}
384411

@@ -406,6 +433,12 @@ func (ws *WebServer) serveBinaryFile(w http.ResponseWriter, r *http.Request, pat
406433
zap.String("path", binaryPath),
407434
zap.String("remote_addr", r.RemoteAddr))
408435

436+
// Check if file exists before serving
437+
if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
438+
http.Error(w, fmt.Sprintf("Binary for %s/%s not available", component, platform), http.StatusNotFound)
439+
return
440+
}
441+
409442
// Set appropriate headers for binary download
410443
filename := component
411444
if strings.HasSuffix(platform, ".exe") {

internal/web/handlers_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ func TestHandleDownloadBinary(t *testing.T) {
256256
expectedStatus int
257257
expectedHeader string
258258
}{
259-
{"/download/minion/linux-amd64", http.StatusNotFound, "application/octet-stream"},
260-
{"/download/console/windows-amd64.exe", http.StatusNotFound, "application/octet-stream"},
259+
{"/download/minion/linux-amd64", http.StatusNotFound, ""},
260+
{"/download/console/windows-amd64.exe", http.StatusBadRequest, ""},
261261
{"/download/invalid/linux-amd64", http.StatusBadRequest, ""},
262262
{"/download/minion/invalid-platform", http.StatusBadRequest, ""},
263263
{"/download/minion", http.StatusBadRequest, ""},
@@ -299,10 +299,10 @@ func TestServeBinaryFileValidation(t *testing.T) {
299299
expectedBody: "Binary for minion/linux-amd64 not available",
300300
},
301301
{
302-
name: "Valid console binary",
302+
name: "Invalid console binary",
303303
path: "console/windows-amd64.exe",
304-
expectedStatus: http.StatusNotFound,
305-
expectedBody: "Binary for console/windows-amd64.exe not available",
304+
expectedStatus: http.StatusBadRequest,
305+
expectedBody: "Invalid component. Must be 'minion'",
306306
},
307307
{
308308
name: "Invalid component",

internal/web/server.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ func StartWebServer(cfg *config.NexusConfig, nexusServer *nexus.Server, logger *
3737
// Binary downloads
3838
mux.HandleFunc("/download/", webServer.loggingMiddleware(webServer.handleDownload))
3939

40-
// Installation script (redirect to static file)
41-
mux.HandleFunc("/install_minion.sh", webServer.loggingMiddleware(func(w http.ResponseWriter, r *http.Request) {
42-
http.Redirect(w, r, "/static/install_minion.sh", http.StatusMovedPermanently)
43-
}))
40+
// Installation script (dynamically generated)
41+
mux.HandleFunc("/install_minion.sh", webServer.loggingMiddleware(webServer.handleInstallScript))
4442

4543
// API endpoints
4644
mux.HandleFunc("/api/status", webServer.loggingMiddleware(webServer.handleAPIStatus))
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
#!/bin/bash
2+
3+
# Script d'installation de Minion
4+
# Usage: curl {{.BaseURL}}/install_minion.sh | sh
5+
6+
set -e
7+
8+
# Couleurs pour l'affichage
9+
RED='\033[0;31m'
10+
GREEN='\033[0;32m'
11+
YELLOW='\033[1;33m'
12+
NC='\033[0m' # No Color
13+
14+
# Configuration
15+
BASE_URL="{{.BaseURL}}"
16+
BINARY_NAME="minion"
17+
18+
# Fonction pour afficher les messages
19+
log_info() {
20+
echo -e "${GREEN}[INFO]${NC} $1"
21+
}
22+
23+
log_warn() {
24+
echo -e "${YELLOW}[WARN]${NC} $1"
25+
}
26+
27+
log_error() {
28+
echo -e "${RED}[ERROR]${NC} $1"
29+
}
30+
31+
# Fonction pour détecter l'OS
32+
detect_os() {
33+
case "$(uname -s)" in
34+
Linux*) OS="linux" ;;
35+
Darwin*) OS="darwin" ;;
36+
CYGWIN*|MINGW*|MSYS*) OS="windows" ;;
37+
*) OS="unknown" ;;
38+
esac
39+
40+
if [ "$OS" = "unknown" ]; then
41+
log_error "OS non supporté: $(uname -s)"
42+
exit 1
43+
fi
44+
45+
log_info "OS détecté: $OS"
46+
}
47+
48+
# Fonction pour détecter l'architecture
49+
detect_arch() {
50+
ARCH=$(uname -m)
51+
52+
case "$ARCH" in
53+
x86_64|amd64) ARCH="amd64" ;;
54+
aarch64|arm64) ARCH="arm64" ;;
55+
armv7l) ARCH="arm" ;;
56+
i386|i686) ARCH="386" ;;
57+
*)
58+
log_error "Architecture non supportée: $ARCH"
59+
exit 1
60+
;;
61+
esac
62+
63+
log_info "Architecture détectée: $ARCH"
64+
}
65+
66+
# Fonction pour construire le nom du binaire
67+
build_binary_name() {
68+
local suffix=""
69+
70+
# Déterminer le suffixe selon la plateforme
71+
case "$OS" in
72+
linux)
73+
if [ "$ARCH" = "amd64" ]; then
74+
suffix="-linux"
75+
fi
76+
;;
77+
windows)
78+
suffix=".exe"
79+
if [ "$ARCH" = "amd64" ]; then
80+
suffix="-windows.exe"
81+
fi
82+
;;
83+
darwin)
84+
if [ "$ARCH" = "amd64" ]; then
85+
suffix="-darwin"
86+
fi
87+
;;
88+
esac
89+
90+
BINARY_FILE="${BINARY_NAME}${suffix}"
91+
DOWNLOAD_URL="${BASE_URL}/download/minion/${OS}-${ARCH}${suffix}"
92+
93+
log_info "Binaire cible: $BINARY_FILE"
94+
log_info "URL de téléchargement: $DOWNLOAD_URL"
95+
}
96+
97+
# Fonction pour télécharger le binaire
98+
download_binary() {
99+
local temp_file="/tmp/${BINARY_FILE}"
100+
101+
log_info "Téléchargement du binaire..."
102+
103+
# Utiliser curl ou wget selon ce qui est disponible
104+
if command -v curl >/dev/null 2>&1; then
105+
if ! curl -L -o "$temp_file" "$DOWNLOAD_URL"; then
106+
log_error "Échec du téléchargement avec curl"
107+
exit 1
108+
fi
109+
elif command -v wget >/dev/null 2>&1; then
110+
if ! wget -O "$temp_file" "$DOWNLOAD_URL"; then
111+
log_error "Échec du téléchargement avec wget"
112+
exit 1
113+
fi
114+
else
115+
log_error "curl ou wget requis pour le téléchargement"
116+
exit 1
117+
fi
118+
119+
# Vérifier que le fichier a été téléchargé
120+
if [ ! -f "$temp_file" ]; then
121+
log_error "Le fichier n'a pas été téléchargé"
122+
exit 1
123+
fi
124+
125+
# Vérifier la taille du fichier
126+
if [ ! -s "$temp_file" ]; then
127+
log_error "Le fichier téléchargé est vide"
128+
rm -f "$temp_file"
129+
exit 1
130+
fi
131+
132+
log_info "Téléchargement terminé avec succès"
133+
134+
# Déplacer le binaire vers le répertoire de destination
135+
local install_dir="/usr/local/bin"
136+
local final_path="${install_dir}/${BINARY_NAME}"
137+
138+
# Créer le répertoire s'il n'existe pas
139+
if [ ! -d "$install_dir" ]; then
140+
log_warn "Création du répertoire $install_dir"
141+
sudo mkdir -p "$install_dir"
142+
fi
143+
144+
# Copier et rendre exécutable
145+
log_info "Installation du binaire vers $final_path"
146+
sudo cp "$temp_file" "$final_path"
147+
sudo chmod +x "$final_path"
148+
149+
# Nettoyer le fichier temporaire
150+
rm -f "$temp_file"
151+
152+
log_info "Installation terminée avec succès"
153+
}
154+
155+
# Fonction pour vérifier l'installation
156+
verify_installation() {
157+
if command -v "$BINARY_NAME" >/dev/null 2>&1; then
158+
log_info "Vérification de l'installation..."
159+
local version_output
160+
if version_output=$("$BINARY_NAME" --version 2>/dev/null); then
161+
log_info "Version installée: $version_output"
162+
else
163+
log_warn "Impossible d'obtenir la version, mais le binaire est installé"
164+
fi
165+
return 0
166+
else
167+
log_error "Le binaire n'est pas accessible dans le PATH"
168+
return 1
169+
fi
170+
}
171+
172+
# Fonction pour exécuter le binaire
173+
run_minion() {
174+
log_info "Démarrage de Minion..."
175+
176+
# Vérifier si des paramètres ont été passés via des variables d'environnement
177+
local minion_args=""
178+
179+
if [ -n "$NEXUS_SERVER" ]; then
180+
minion_args="$minion_args --server $NEXUS_SERVER"
181+
fi
182+
183+
if [ -n "$MINION_NAME" ]; then
184+
minion_args="$minion_args --name $MINION_NAME"
185+
fi
186+
187+
if [ -n "$MINION_TAGS" ]; then
188+
minion_args="$minion_args --tags $MINION_TAGS"
189+
fi
190+
191+
if [ -z "$minion_args" ]; then
192+
log_info "Aucune configuration fournie. Utilisation des paramètres par défaut."
193+
log_info "Variables d'environnement disponibles:"
194+
log_info " NEXUS_SERVER - Adresse du serveur Nexus"
195+
log_info " MINION_NAME - Nom du minion"
196+
log_info " MINION_TAGS - Tags du minion"
197+
log_info ""
198+
log_info "Exemple:"
199+
log_info " NEXUS_SERVER=nexus.example.com:8080 MINION_NAME=web-server-01 $0"
200+
fi
201+
202+
# Exécuter le minion
203+
log_info "Commande exécutée: $BINARY_NAME $minion_args"
204+
exec "$BINARY_NAME" $minion_args
205+
}
206+
207+
# Fonction principale
208+
main() {
209+
log_info "=== Installation de Minion ==="
210+
211+
# Vérifier les prérequis
212+
if [ "$EUID" -eq 0 ]; then
213+
log_warn "Attention: exécution en tant que root"
214+
fi
215+
216+
# Détecter l'environnement
217+
detect_os
218+
detect_arch
219+
220+
# Construire les informations de téléchargement
221+
build_binary_name
222+
223+
# Télécharger et installer
224+
download_binary
225+
226+
# Vérifier l'installation
227+
if verify_installation; then
228+
log_info "=== Installation réussie ==="
229+
230+
# Exécuter le minion
231+
run_minion
232+
else
233+
log_error "=== Échec de l'installation ==="
234+
exit 1
235+
fi
236+
}
237+
238+
# Exécuter le script principal
239+
main "$@"

0 commit comments

Comments
 (0)