-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_devsforge.ps1
More file actions
200 lines (176 loc) · 7.96 KB
/
run_devsforge.ps1
File metadata and controls
200 lines (176 loc) · 7.96 KB
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
195
196
197
198
199
200
# ============================================================================
# Script to build and run Docker - devsforge
# Platform: Windows PowerShell 5.1+
#
# NOTE: Run with: powershell -ExecutionPolicy RemoteSigned -File run_devsforge.ps1
# Or use: run_devsforge_init.cmd (recommended)
# ============================================================================
# CONFIGURATION - Modify these variables as needed
$CONTAINER_NAME = "devsforge-container"
$IMAGE_NAME = "devsforge-apache"
$DOCKER_PORT = "8090"
$HOST_PORT = "80"
$SHARED_FOLDER = "C:\devsforge\code" # Change this path if necessary
$CONTAINER_PATH = "/var/www/html/devsforge/code"
# ============================================================================
# Function for logging with timestamp
function Log-Message {
param(
[string]$Message,
[string]$Type = "INFO"
)
$timestamp = Get-Date -Format "HH:mm:ss"
switch ($Type) {
"SUCCESS" { Write-Host "[$timestamp] OK: $Message" -ForegroundColor Green }
"ERROR" { Write-Host "[$timestamp] ERROR: $Message" -ForegroundColor Red }
"WARNING" { Write-Host "[$timestamp] WARNING: $Message" -ForegroundColor Yellow }
"INFO" { Write-Host "[$timestamp] INFO: $Message" -ForegroundColor Cyan }
default { Write-Host "[$timestamp] $Message" -ForegroundColor White }
}
}
Log-Message "Starting DEVSFORGE - Docker Build & Run Script" "INFO"
Write-Host ""
Write-Host "============================================================" -ForegroundColor Cyan
Write-Host " DEVSFORGE - Docker Build & Run Script" -ForegroundColor Cyan
Write-Host "============================================================" -ForegroundColor Cyan
Write-Host ""
# Verify that Docker is installed
Log-Message "Verifying that Docker is installed..." "INFO"
if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
Log-Message "Docker is not installed or is not in the PATH" "ERROR"
Write-Host " Download Docker from: https://www.docker.com/products/docker-desktop" -ForegroundColor Red
Write-Host ""
exit 1
}
Log-Message "Docker found correctly" "SUCCESS"
Write-Host ""
# ============================================================================
# STEP 1: Navigate to project root folder
# ============================================================================
Write-Host "============================================================" -ForegroundColor Cyan
Log-Message "STEP 1: Navigating to project root folder..." "INFO"
Write-Host "============================================================" -ForegroundColor Cyan
try {
$scriptDir = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
Set-Location -Path $scriptDir
Log-Message "Current location: $(Get-Location)" "SUCCESS"
}
catch {
Log-Message "Error navigating to folder: $_" "ERROR"
exit 1
}
Write-Host ""
# ============================================================================
# STEP 2: Build Docker image
# ============================================================================
Write-Host "============================================================" -ForegroundColor Cyan
Log-Message "STEP 2: Building Docker image..." "INFO"
Write-Host "============================================================" -ForegroundColor Cyan
Write-Host " Name: $IMAGE_NAME" -ForegroundColor Yellow
Write-Host " Dockerfile: docker/Dockerfile" -ForegroundColor Yellow
Write-Host " Context: $(Get-Location)" -ForegroundColor Yellow
Write-Host " Note: Build may take several minutes..." -ForegroundColor Yellow
Write-Host ""
try {
docker build -t $IMAGE_NAME -f docker/Dockerfile .
if ($LASTEXITCODE -ne 0) {
Log-Message "Error building Docker image" "ERROR"
exit 1
}
Log-Message "Docker image built successfully" "SUCCESS"
}
catch {
Log-Message "Error during build: $_" "ERROR"
exit 1
}
Write-Host ""
# ============================================================================
# STEP 3: Clean devsforge folder
# ============================================================================
Write-Host "============================================================" -ForegroundColor Cyan
Log-Message "STEP 3: Preparing shared folder..." "INFO"
Write-Host "============================================================" -ForegroundColor Cyan
Log-Message "Removing previous folder (if exists)..." "INFO"
try {
$parentFolder = Split-Path -Parent -Path $SHARED_FOLDER
Remove-Item -Path $parentFolder -Recurse -Force -ErrorAction SilentlyContinue
Log-Message "Folder removed" "SUCCESS"
}
catch {
Log-Message "Error removing (continuing): $_" "WARNING"
}
Log-Message "Creating folder $SHARED_FOLDER..." "INFO"
try {
New-Item -ItemType Directory -Path $SHARED_FOLDER -Force | Out-Null
Log-Message "Folder created successfully" "SUCCESS"
}
catch {
Log-Message "Error creating folder: $_" "ERROR"
exit 1
}
Write-Host ""
# ============================================================================
# STEP 4: Run Docker container
# ============================================================================
Write-Host "============================================================" -ForegroundColor Cyan
Log-Message "STEP 4: Starting Docker container..." "INFO"
Write-Host "============================================================" -ForegroundColor Cyan
Write-Host " Name: $CONTAINER_NAME" -ForegroundColor Yellow
Write-Host " Port: $DOCKER_PORT (access: http://localhost:$DOCKER_PORT/devsforge)" -ForegroundColor Yellow
Write-Host " Volume: $SHARED_FOLDER -> $CONTAINER_PATH" -ForegroundColor Yellow
Write-Host ""
try {
docker run -d -p ${DOCKER_PORT}:${HOST_PORT} --name $CONTAINER_NAME -v "${SHARED_FOLDER}:${CONTAINER_PATH}" $IMAGE_NAME
if ($LASTEXITCODE -ne 0) {
Log-Message "Error running container" "ERROR"
exit 1
}
Log-Message "Container started successfully" "SUCCESS"
}
catch {
Log-Message "Error running container: $_" "ERROR"
exit 1
}
Write-Host ""
# ============================================================================
# STEP 5: Final verification
# ============================================================================
Write-Host "============================================================" -ForegroundColor Cyan
Log-Message "STEP 5: Verifying container status..." "INFO"
Write-Host "============================================================" -ForegroundColor Cyan
Start-Sleep -Seconds 2
try {
$containerStatus = docker ps --filter "name=$CONTAINER_NAME" --format "{{.Status}}"
if ($containerStatus) {
Log-Message "Container status: $containerStatus" "SUCCESS"
}
else {
Log-Message "Container is not running. Checking logs..." "WARNING"
docker logs $CONTAINER_NAME
exit 1
}
}
catch {
Log-Message "Error verifying status: $_" "WARNING"
}
Write-Host ""
# ============================================================================
# Final Summary
# ============================================================================
Write-Host "============================================================" -ForegroundColor Green
Write-Host " PROCESS COMPLETED SUCCESSFULLY" -ForegroundColor Green
Write-Host "============================================================" -ForegroundColor Green
Write-Host ""
Write-Host "Access URL:" -ForegroundColor Cyan
Write-Host " http://localhost:$DOCKER_PORT/devsforge" -ForegroundColor Yellow
Write-Host ""
Write-Host "Shared folder:" -ForegroundColor Cyan
Write-Host " $SHARED_FOLDER" -ForegroundColor Yellow
Write-Host ""
Write-Host "Useful commands:" -ForegroundColor Cyan
Write-Host " View logs: docker logs -f $CONTAINER_NAME" -ForegroundColor Gray
Write-Host " Stop: docker stop $CONTAINER_NAME" -ForegroundColor Gray
Write-Host " Restart: docker restart $CONTAINER_NAME" -ForegroundColor Gray
Write-Host " Remove: docker stop $CONTAINER_NAME && docker rm $CONTAINER_NAME" -ForegroundColor Gray
Write-Host ""
Log-Message "Finished successfully" "SUCCESS"