173 lines
5.9 KiB
PowerShell
173 lines
5.9 KiB
PowerShell
################START SCRIPT################
|
|
|
|
#rilevo time per log inizio analisi
|
|
$startTime = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
|
|
|
|
# avvio stopwatch
|
|
$mainStopWatch = [system.diagnostics.stopwatch]::StartNew()
|
|
|
|
#token per autenticazione gitlab.steamware.net
|
|
$head = @{"PRIVATE-TOKEN"="glpat-pmkiVSJMhNyAySEKp2nA"}
|
|
|
|
#nome utente che effettua i mirror push
|
|
$userName = "replica"
|
|
|
|
#token autenticazione azzano
|
|
$tokenAzzano = "glpat-gADkrEhkEsvQxGxKez4y"
|
|
|
|
#token autenticazione nembro
|
|
$tokenNembro = "glpat-TzZkRUoYAdKikgwFXW_E"
|
|
|
|
#contatore ciclo do while
|
|
$projectCount = 1
|
|
|
|
#numero massimo di progetti
|
|
$projectNumber = 1
|
|
|
|
#livello di log: 0=log sintetico, 1=log errori, 2=log full, 3=log ampolloso
|
|
$logLevel = 3
|
|
|
|
#output a terminale: 0=disattivo, 1=abilitato
|
|
$terminalOutput = 1
|
|
|
|
#inizializzo variabili vuote
|
|
$mirrorId = ""
|
|
$mirrorUrl = ""
|
|
$body = ""
|
|
$jsonBody = ""
|
|
|
|
#cartella file di log
|
|
$GitLogFolder="c:\Steamware\Logs\Gitlab\"
|
|
|
|
#nome file di log
|
|
$logFile = Join-Path $GitLogFolder "GitlabRebuildMirroring.log"
|
|
|
|
#dichiaro funzione per scrittura output
|
|
Function WriteLogOutput
|
|
{
|
|
Param ($logType, [string]$logString)
|
|
if($logType -le $logLevel)
|
|
{
|
|
#scrivo su file la stringa
|
|
Add-content $logFile -value "$logString"
|
|
#scrivo su terminale la stringa se $terminalOutput=1
|
|
if($terminalOutput -eq 1){
|
|
Write-Output($logString)
|
|
}
|
|
}
|
|
}
|
|
|
|
#creazione folder di Log se non già esistente
|
|
if (Test-Path $GitLogFolder)
|
|
{
|
|
}
|
|
else
|
|
{
|
|
New-Item $GitLogFolder -ItemType Directory
|
|
}
|
|
|
|
#scrivo intestazione e inizio analisi
|
|
WriteLogOutput 0 "--------------------"
|
|
Switch ($logLevel)
|
|
{
|
|
0 {WriteLogOutput 0 "LOG SINTETICO GITLAB MIRRORING"}
|
|
1 {WriteLogOutput 0 "LOG ERRORI GITLAB MIRRORING"}
|
|
2 {WriteLogOutput 0 "LOG FULL GITLAB MIRRORING"}
|
|
3 {WriteLogOutput 0 "LOG AMPOLLOSO GITLAB MIRRORING"}
|
|
}
|
|
WriteLogOutput 0 ""
|
|
WriteLogOutput 0 "Percorso log: $GitLogFolder"
|
|
WriteLogOutput 0 ""
|
|
WriteLogOutput 0 "Inizio Esecuzione Script: $startTime"
|
|
|
|
#ciclo principale do/while che cicla da 1 a N projectNumber
|
|
do
|
|
{
|
|
#chiamata api che restituisce informazioni sullo stato dei mirror
|
|
$callUrl = "https://gitlab.steamware.net/api/v4/projects/" + $projectCount + "/remote_mirrors"
|
|
try
|
|
{
|
|
#parsing della risposta api convertita da json
|
|
$Response = Invoke-WebRequest -URI $callUrl -Headers $head -ContentType "application/json" -UseBasicParsing
|
|
$parsedResponse = $Response.Content | ConvertFrom-Json
|
|
#scrivo il numero del progetto
|
|
WriteLogOutput 1 ""
|
|
WriteLogOutput 1 "--------------------"
|
|
WriteLogOutput 1 "**PROGETTO $projectCount**"
|
|
foreach($item in $parsedResponse)
|
|
{
|
|
$doExecute = "false"
|
|
$token = ""
|
|
$mirrorUrl = ""
|
|
$trunkedPath = ""
|
|
#controllo se l'url del mirror contiene azzano
|
|
if($item.url.Contains("azzano"))
|
|
{
|
|
$doExecute = "true"
|
|
$token = $tokenAzzano
|
|
$mirrorDestination = "@gitlab-azzano.steamware.net"
|
|
}
|
|
#controllo se l'url del mirror contiene nembro
|
|
elseif($item.url.Contains("nembro"))
|
|
{
|
|
$doExecute = "true"
|
|
$token = $tokenNembro
|
|
$mirrorDestination = "@gitlab-nembro.steamware.net"
|
|
}
|
|
#verifico se devo effettuare cancellazione e creazione nuovo mirror
|
|
if($doExecute -eq "true")
|
|
{
|
|
#salvo id mirror e url mirror
|
|
$mirrorId = $($item.id)
|
|
$mirrorUrl = $($item.url)
|
|
WriteLogOutput 1 "ID: $mirrorId - URL: $mirrorUrl"
|
|
#tronco url fino a ".net"
|
|
$splitUrl = $mirrorUrl -split '.steamware.net/'
|
|
$trunkedPath = $splitUrl[1]
|
|
#compongo URL per cancellare mirror con ID trovato
|
|
$callUrlDeleteMirror = "https://gitlab.steamware.net/api/v4/projects/" + $projectCount + "/remote_mirrors/" + $mirrorId
|
|
#chiamata api con method delete che cancella mirror con id specificato
|
|
Invoke-WebRequest -Method Delete -URI $callUrlDeleteMirror -Headers $head -ContentType "application/json" -UseBasicParsing
|
|
#compongo url da chiamare per creazione nuovo mirror
|
|
$callUrlCreateMirror = "https://gitlab.steamware.net/api/v4/projects/" + $projectCount + "/remote_mirrors"
|
|
#creo url del nuovo mirror con username e token relativi a gitlab
|
|
$newMirror = "https://" + $userName + ":" + $token + $mirrorDestination + "/" + $trunkedPath
|
|
#creo body da convertire in json
|
|
$body =
|
|
@{
|
|
url = $newMirror
|
|
enabled = 1
|
|
}
|
|
#converto body in json prima di passarlo alla chiamata POST
|
|
$jsonBody = ConvertTo-Json -InputObject $body
|
|
#chiamata api POST che crea mirror con url e body specificati
|
|
Invoke-WebRequest -Method Post -URI $callUrlCreateMirror -Headers $head -ContentType "application/json" -Body $jsonBody -UseBasicParsing
|
|
WriteLogOutput 1 "Mirror ricostruito con successo"
|
|
}
|
|
}
|
|
}
|
|
#scrivo se trovo un errore (in particolare progetto ID non trovato) durante il try/catch
|
|
catch
|
|
{
|
|
WriteLogOutput 3 ""
|
|
WriteLogOutput 3 "--------------------"
|
|
WriteLogOutput 3 "**ERRORE: PROGETTO $projectCount NON ESISTENTE**"
|
|
}
|
|
$projectCount=$projectCount+1
|
|
}
|
|
#fine ciclo principale
|
|
while($projectCount -le $projectNumber)
|
|
|
|
# fermo stopwatch e calcolo durata script
|
|
$mainStopWatch.Stop()
|
|
$durataScript = $mainStopWatch.Elapsed.TotalSeconds
|
|
|
|
#rilevo time per log fine analisi
|
|
$endTime = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
|
|
|
|
#scrivo a video le statistiche
|
|
WriteLogOutput 0 ""
|
|
WriteLogOutput 0 "Fine Esecuzione Script: $endTime"
|
|
WriteLogOutput 0 ""
|
|
WriteLogOutput 0 "Durata Esecuzione Script: $durataScript secondi"
|
|
WriteLogOutput 0 "" |