153 lines
4.7 KiB
PowerShell
153 lines
4.7 KiB
PowerShell
Param(
|
|
[string]$serviceName, #Argomento passato dal chiamante
|
|
[Int32]$httpPort, #Argomento passato dal chiamante
|
|
[string]$updatedArtifactPath #Argomento passato dal chiamante
|
|
)
|
|
|
|
$max_attempts = 48
|
|
$attempt = 0
|
|
$webappsPath = "C:\Program Files\$serviceName\webapps"
|
|
|
|
# Funzione per eseguire la chiamata GET e controllare lo status code
|
|
function Check-Status {
|
|
try {
|
|
$request = Invoke-WebRequest -Uri "http://localhost:$httpPort/ems-api/system/ok" -Method Get -ErrorAction Stop
|
|
$statusCode = $request.StatusCode
|
|
if ($statusCode -eq 200) {
|
|
Write-Host "Status code 200 ricevuto, processo completato."
|
|
exit 0
|
|
} else {
|
|
Write-Host "Status code $statusCode ricevuto, continuo a provare..."
|
|
}
|
|
} catch {
|
|
Write-Host "Errore: $_"
|
|
}
|
|
}
|
|
|
|
# Funzione per cancellare una cartella e un file
|
|
function Delete-FilesAndFolder
|
|
{
|
|
param (
|
|
[string]$folderPath,
|
|
[string]$filePath
|
|
)
|
|
|
|
try {
|
|
# Cancellare la cartella specificata
|
|
if (Test-Path -Path $folderPath) {
|
|
Remove-Item -Path $folderPath -Recurse -Force
|
|
Write-Host "Cartella $folderPath eliminata con successo."
|
|
} else {
|
|
Write-Host "La cartella $folderPath non esiste."
|
|
}
|
|
|
|
# Cancellare il file specificato
|
|
if (Test-Path -Path $filePath) {
|
|
Remove-Item -Path $filePath -Force
|
|
Write-Host "File $filePath eliminato con successo."
|
|
} else {
|
|
Write-Host "Il file $filePath non esiste."
|
|
}
|
|
|
|
} catch {
|
|
Write-Host "Errore nella cancellazione di file o cartella: $_"
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
# Funzione per copiare un file
|
|
function Copy-File {
|
|
param (
|
|
[string]$sourceFilePath,
|
|
[string]$destinationFilePath
|
|
)
|
|
|
|
try {
|
|
if (Test-Path -Path $sourceFilePath) {
|
|
Copy-Item -Path $sourceFilePath -Destination $destinationFilePath -Force
|
|
Write-Host "File copiato da $sourceFilePath a $destinationFilePath con successo."
|
|
} else {
|
|
Write-Host "Il file sorgente $sourceFilePath non esiste."
|
|
}
|
|
} catch {
|
|
Write-Host "Errore nella copia del file: $_"
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
# Funzione per stoppare un servizio e attendere che sia completamente fermato
|
|
function Stop-ServiceAndWait {
|
|
param (
|
|
[string]$serviceName
|
|
)
|
|
|
|
# Tentativo di stoppare il servizio
|
|
try {
|
|
Write-Host "Sto stoppando il servizio $serviceName..."
|
|
Stop-Service -Name $serviceName -Force
|
|
|
|
# Attesa finché il servizio non viene stoppato
|
|
$service = Get-Service -Name $serviceName
|
|
while ($service.Status -ne 'Stopped') {
|
|
Write-Host "Attendo che il servizio $serviceName si fermi..."
|
|
Start-Sleep -Seconds 5
|
|
$service = Get-Service -Name $serviceName
|
|
}
|
|
|
|
Write-Host "Il servizio $serviceName è stato fermato con successo."
|
|
} catch {
|
|
Write-Host "Errore nello stoppare il servizio ${serviceName}: $_"
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
# Funzione per avviare nuovamente un servizio e attendere che sia in esecuzione
|
|
function Start-ServiceAndWait {
|
|
param (
|
|
[string]$serviceName
|
|
)
|
|
|
|
try {
|
|
Write-Host "Sto avviando il servizio $serviceName..."
|
|
Start-Service -Name $serviceName
|
|
|
|
# Attesa finché il servizio non viene avviato
|
|
$service = Get-Service -Name $serviceName
|
|
while ($service.Status -ne 'Running') {
|
|
Write-Host "Attendo che il servizio $serviceName si avvii..."
|
|
Start-Sleep -Seconds 5
|
|
$service = Get-Service -Name $serviceName
|
|
}
|
|
|
|
Write-Host "Il servizio $serviceName è stato avviato con successo."
|
|
|
|
} catch {
|
|
Write-Host "Errore nell'avvio del servizio ${serviceName}: $_"
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
# Chiamare la funzione per stoppare il servizio
|
|
Stop-ServiceAndWait -serviceName $serviceName
|
|
|
|
# Subito dopo lo stop, cancellare la cartella e il file
|
|
$emsApiPath = "$webappsPath\ems-api"
|
|
$emsWarFilePath = "$webappsPath\ems-api.war"
|
|
Delete-FilesAndFolder -folderPath $emsApiPath -filePath $emsWarFilePath
|
|
|
|
# Chiamare la funzione per copiare il file
|
|
Write-Host "Copio $updatedArtifactPath in $emsWarFilePath"
|
|
Copy-File -sourceFilePath $updatedArtifactPath -destinationFilePath $emsWarFilePath
|
|
|
|
# Avvia nuovamente il servizio
|
|
Start-ServiceAndWait -serviceName $serviceName
|
|
|
|
# Ciclo fino a quando lo status code non è 200 o superiamo il numero massimo di tentativi
|
|
while ($attempt -lt $max_attempts) {
|
|
$attempt++
|
|
Check-Status
|
|
Start-Sleep -Seconds 5 # Attendere per 5 secondi prima di fare un altro tentativo
|
|
}
|
|
|
|
Write-Host "Numero massimo di tentativi superato. Nessuna risposta 200 ricevuta."
|
|
exit 1 |