124 lines
5.3 KiB
PowerShell
124 lines
5.3 KiB
PowerShell
###Gitlab: riscrive i mirror verso gitea###
|
|
|
|
#Questo script passa in rassegna tutti i progetti esistenti sul gitlab aziendale e per ognuno riscrive il mirror verso gitea (cancella mirror e ricrea)
|
|
|
|
#importo file contenente configurazioni
|
|
. .\ApiGit\ApiGitResources\ApiScriptsConfig.ps1
|
|
#importo file contenente funzioni
|
|
. .\ApiGit\ApiGitResources\ApiScriptsFunctions.ps1
|
|
|
|
#rilevo time per log inizio analisi
|
|
$startTime = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
|
|
# avvio stopwatch
|
|
$mainStopWatch = [system.diagnostics.stopwatch]::StartNew()
|
|
#contatore ciclo do while
|
|
$projectCount = 1
|
|
#numero massimo di progetti
|
|
$projectNumber = 250
|
|
#inizializzo variabili vuote
|
|
$mirrorId = ""
|
|
$mirrorUrl = ""
|
|
$body = ""
|
|
$jsonBody = ""
|
|
|
|
#nome file di log
|
|
$logFile = "GitlabMirroringGitea.log"
|
|
|
|
#creazione folder di Log se non già esistente
|
|
if (Test-Path $logFolder) {
|
|
}
|
|
else {
|
|
New-Item $logFolder -ItemType Directory
|
|
}
|
|
|
|
#scrivo intestazione e inizio analisi
|
|
WriteLogOutput $logFile 0 "--------------------"
|
|
Switch ($logLevel) {
|
|
0 { WriteLogOutput $logFile 0 "LOG SINTETICO GITLAB MIRRORING" }
|
|
1 { WriteLogOutput $logFile 0 "LOG ERRORI GITLAB MIRRORING" }
|
|
2 { WriteLogOutput $logFile 0 "LOG FULL GITLAB MIRRORING" }
|
|
3 { WriteLogOutput $logFile 0 "LOG AMPOLLOSO GITLAB MIRRORING" }
|
|
}
|
|
WriteLogOutput $logFile 0 ""
|
|
WriteLogOutput $logFile 0 "Percorso log: $logFolder"
|
|
WriteLogOutput $logFile 0 ""
|
|
WriteLogOutput $logFile 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 $gitlabHead -ContentType "application/json" -UseBasicParsing
|
|
$parsedResponse = $Response.Content | ConvertFrom-Json
|
|
#scrivo il numero del progetto
|
|
WriteLogOutput $logFile 1 ""
|
|
WriteLogOutput $logFile 1 "--------------------"
|
|
WriteLogOutput $logFile 1 "**PROGETTO $projectCount**"
|
|
foreach ($item in $parsedResponse) {
|
|
$mirrorUrl = ""
|
|
$trunkedPath = ""
|
|
#controllo se l'url del mirror contiene gitea
|
|
if ($item.url.Contains("gitea")) {
|
|
$mirrorDestination = "@gitea.steamware.net"
|
|
#salvo id mirror e url mirror
|
|
$mirrorId = $($item.id)
|
|
$mirrorUrl = $($item.url)
|
|
WriteLogOutput $logFile 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 $gitlabHead -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 pass relativi a gitlab
|
|
$newMirror = "https://" + $giteaUser + ":" + $giteaPass + $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
|
|
$rebuildResponse = Invoke-WebRequest -Method Post -URI $callUrlCreateMirror -Headers $gitlabHead -ContentType "application/json" -Body $jsonBody -UseBasicParsing
|
|
$parsedRebuild = $rebuildResponse.Content | ConvertFrom-Json
|
|
foreach ($item in $parsedRebuild) {
|
|
$mirrorId = $($item.id)
|
|
$mirrorUrl = $($item.url)
|
|
}
|
|
WriteLogOutput $logFile 1 "NEW ID: $mirrorId - URL: $mirrorUrl - Mirror ricostruito con successo"
|
|
}
|
|
}
|
|
}
|
|
#scrivo se trovo un errore durante il try/catch
|
|
catch {
|
|
$parsedError = $_ | ConvertFrom-Json
|
|
WriteLogOutput $logFile 3 ""
|
|
WriteLogOutput $logFile 3 "--------------------"
|
|
WriteLogOutput $logFile 3 "**ERRORE NEL PROGETTO $projectCount**"
|
|
WriteLogOutput $logFile 3 $($parsedError.message)
|
|
}
|
|
$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 $logFile 0 ""
|
|
WriteLogOutput $logFile 0 "Fine Esecuzione Script: $endTime"
|
|
WriteLogOutput $logFile 0 ""
|
|
WriteLogOutput $logFile 0 "Durata Esecuzione Script: $durataScript secondi"
|
|
WriteLogOutput $logFile 0 "" |