-
Notifications
You must be signed in to change notification settings - Fork 0
/
phoenix_deployer.bat
262 lines (238 loc) · 7.73 KB
/
phoenix_deployer.bat
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
@echo off
setlocal EnableDelayedExpansion
:: Initialize variables with passed arguments
set "PROJECT_NAME="
set "SERVER_ADDRESS="
set "SSH_KEY="
set "SSH_USERNAME="
set "COMMAND="
set "DB="
set "LOG_FILE="
:: Function to log messages with timestamp
:Log
if defined LOG_FILE (
echo [%date% %time%] %~1 >> "%LOG_FILE%"
) else (
echo [%date% %time%] %~1
)
goto :eof
:: Parse input arguments
:parse
if "%~1"=="" goto endparse
if /I "%~1"=="-pn" (
set "PROJECT_NAME=%~2"
shift
shift
goto parse
)
if /I "%~1"=="-sa" (
set "SERVER_ADDRESS=%~2"
shift
shift
goto parse
)
if /I "%~1"=="-sk" (
set "SSH_KEY=%~2"
shift
shift
goto parse
)
if /I "%~1"=="-su" (
set "SSH_USERNAME=%~2"
shift
shift
goto parse
)
if /I "%~1"=="-cmd" (
set "COMMAND=%~2"
shift
shift
goto parse
)
if /I "%~1"=="-db" (
set "DB=%~2"
shift
shift
goto parse
)
if /I "%~1"=="-log" (
set "LOG_FILE=%~2"
shift
shift
goto parse
)
echo "Unknown argument: %~1" | call :Log
echo "Unknown argument: %~1"
exit /b 1
:endparse
:: Check if necessary arguments are provided
if "%PROJECT_NAME%"=="" (
echo "Error: Project name (-pn) is required." | call :Log
exit /b 1
)
if "%SERVER_ADDRESS%"=="" (
echo "Error: Server address (-sa) is required." | call :Log
exit /b 1
)
if "%SSH_KEY%"=="" (
echo "Error: SSH key (-sk) is required." | call :Log
exit /b 1
)
if "%SSH_USERNAME%"=="" (
echo "Error: SSH username (-su) is required." | call :Log
exit /b 1
)
if "%COMMAND%"=="" (
echo "Error: Command (-cmd) is required." | call :Log
exit /b 1
)
if "%DB%"=="" (
echo "Error: DB flag (-db) is required." | call :Log
exit /b 1
)
if "%LOG_FILE%"=="" (
echo "Error: Log file path (-log) is required." | call :Log
exit /b 1
)
:: Validate 'DB' parameter
if /I "%DB%" NEQ "true" if /I "%DB%" NEQ "false" (
echo "Error: 'DB' flag must be either 'true' or 'false'." | call :Log
exit /b 1
)
:: Log start of deployer script
call :Log "Starting deployment for project '%PROJECT_NAME%' to server '%SERVER_ADDRESS%'."
call :Log "DB Setup Required: %DB%"
:: Define project-specific variables
set "MAIN_SERVER_DIR=/root"
set "APP_DIR=%MAIN_SERVER_DIR%/%PROJECT_NAME%"
set "SERVICE_FILE=%PROJECT_NAME%.service"
set "SERVICE_PATH=/etc/systemd/system/%SERVICE_FILE%"
set "TEMP_DIR=%PROJECT_NAME%_temp"
set "ZIP_FILE=%PROJECT_NAME%.tar.gz"
:: Create the service file locally
call :Log "Creating service file '%SERVICE_FILE%' locally..."
echo [Unit]> "%SERVICE_FILE%"
echo Description=Phoenix Service for %PROJECT_NAME%>> "%SERVICE_FILE%"
echo After=network.target>> "%SERVICE_FILE%"
echo.>> "%SERVICE_FILE%"
echo [Service]>> "%SERVICE_FILE%"
echo User=root>> "%SERVICE_FILE%"
echo WorkingDirectory=%APP_DIR%>> "%SERVICE_FILE%"
echo ExecStart=/bin/bash -c \"%COMMAND%\" >> "%SERVICE_FILE%"
echo Restart=always>> "%SERVICE_FILE%"
echo RestartSec=5>> "%SERVICE_FILE%"
echo SyslogIdentifier=%PROJECT_NAME%>> "%SERVICE_FILE%"
echo.>> "%SERVICE_FILE%"
echo [Install]>> "%SERVICE_FILE%"
echo WantedBy=multi-user.target>> "%SERVICE_FILE%"
if errorlevel 1 (
echo "Error: Failed to create service file '%SERVICE_FILE%' locally." | call :Log
exit /b 1
)
call :Log "Service file created successfully."
:: Create temporary directory
call :Log "Creating temporary directory '%TEMP_DIR%'..."
mkdir "%TEMP_DIR%"
if errorlevel 1 (
echo "Error: Failed to create temporary directory '%TEMP_DIR%'." | call :Log
exit /b 1
)
call :Log "Temporary directory created successfully."
:: Compile files into temporary directory
call :Log "Compiling files into temporary directory..."
robocopy . "%TEMP_DIR%" /E /XD "deps" "_build" "node_modules" ".git" ".elixir_ls" ".github" >nul
if errorlevel 8 (
echo "Error: Robocopy encountered an issue while copying files." | call :Log
exit /b 1
)
call :Log "Files compiled successfully."
:: Pack files
call :Log "Packing files into '%ZIP_FILE%'..."
tar -czvf "%ZIP_FILE%" -C "%TEMP_DIR%" . >nul
if errorlevel 1 (
echo "Error: Failed to create archive '%ZIP_FILE%'." | call :Log
exit /b 1
)
call :Log "Files packed successfully."
:: Download setup.sh from GitHub
call :Log "Downloading 'phoenix_setup.sh' from GitHub..."
ssh -i "%SSH_KEY%" "%SSH_USERNAME%@%SERVER_ADDRESS%" "sudo wget -O /root/phoenix_setup.sh https://raw.githubusercontent.com/royokello/phoenix-setup/main/phoenix_setup.sh && sudo chmod +x /root/phoenix_setup.sh"
if errorlevel 1 (
echo "Error: Failed to download or set permissions for 'phoenix_setup.sh'." | call :Log
exit /b 1
)
call :Log "'phoenix_server_setup.sh' downloaded and permissions set."
:: Run setup script
call :Log "Running setup script on server..."
ssh -i "%SSH_KEY%" "%SSH_USERNAME%@%SERVER_ADDRESS%" "sudo bash /root/phoenix_server_setup.sh -pn %PROJECT_NAME% -db %DB%"
if errorlevel 1 (
echo "Error: Setup script execution failed on server." | call :Log
exit /b 1
)
call :Log "Setup script executed successfully on server."
:: Stop existing service on server
call :Log "Stopping existing service '%SERVICE_FILE%' on server..."
ssh -i "%SSH_KEY%" "%SSH_USERNAME%@%SERVER_ADDRESS%" "sudo systemctl stop %SERVICE_FILE%"
if errorlevel 1 (
echo "Error: Failed to stop existing service '%SERVICE_FILE%' on server." | call :Log
exit /b 1
)
call :Log "Existing service stopped."
:: Upload files and service file
call :Log "Uploading archive '%ZIP_FILE%' to server..."
scp -i "%SSH_KEY%" "%ZIP_FILE%" "%SSH_USERNAME%@%SERVER_ADDRESS%:%MAIN_SERVER_DIR%"
if errorlevel 1 (
echo "Error: Failed to upload '%ZIP_FILE%' to server." | call :Log
exit /b 1
)
call :Log "Archive uploaded successfully."
call :Log "Uploading service file '%SERVICE_FILE%' to server..."
scp -i "%SSH_KEY%" "%SERVICE_FILE%" "%SSH_USERNAME%@%SERVER_ADDRESS%:%SERVICE_PATH%"
if errorlevel 1 (
echo "Error: Failed to upload service file '%SERVICE_FILE%' to server." | call :Log
exit /b 1
)
call :Log "Service file uploaded successfully."
:: Unpack project archive on server
call :Log "Unpacking archive on server to '%APP_DIR%'..."
ssh -i "%SSH_KEY%" "%SSH_USERNAME%@%SERVER_ADDRESS%" "mkdir -p %APP_DIR% && tar -xzvf %MAIN_SERVER_DIR%/%ZIP_FILE% -C %APP_DIR%"
if errorlevel 1 (
echo "Error: Failed to unpack archive on server." | call :Log
exit /b 1
)
call :Log "Archive unpacked successfully on server."
:: Clean up uploaded archive on server
call :Log "Cleaning up uploaded archive on server..."
ssh -i "%SSH_KEY%" "%SSH_USERNAME%@%SERVER_ADDRESS%" "rm %MAIN_SERVER_DIR%/%ZIP_FILE%"
if errorlevel 1 (
echo "Warning: Failed to remove archive '%ZIP_FILE%' from server." | call :Log
) else (
call :Log "Uploaded archive removed from server."
)
:: Deploy files and enable service
call :Log "Deploying files and enabling service '%SERVICE_FILE%'..."
ssh -i "%SSH_KEY%" "%SSH_USERNAME%@%SERVER_ADDRESS%" "sudo systemctl daemon-reload && sudo systemctl enable %SERVICE_FILE% && sudo systemctl start %SERVICE_FILE%"
if errorlevel 1 (
echo "Error: Failed to deploy service '%SERVICE_FILE%' on server." | call :Log
exit /b 1
)
call :Log "Service '%SERVICE_FILE%' deployed and started successfully."
:: Delete temporary files on the development machine
call :Log "Deleting temporary files on the development machine..."
rmdir /S /Q "%TEMP_DIR%"
if errorlevel 1 (
echo "Warning: Failed to delete temporary directory '%TEMP_DIR%'." | call :Log
) else (
call :Log "Temporary directory deleted successfully."
)
del "%ZIP_FILE%"
if errorlevel 1 (
echo "Warning: Failed to delete archive '%ZIP_FILE%'." | call :Log
) else (
call :Log "Archive '%ZIP_FILE%' deleted successfully."
)
:: Log completion
call :Log "Deployment process completed successfully for project '%PROJECT_NAME%'."
echo "Process completed successfully. Check the log file '%LOG_FILE%' for details."
pause
endlocal