From d695e9c0156f1cc1d9f54ed3b621c4695d198633 Mon Sep 17 00:00:00 2001 From: "marco.locatelli@steamware.net" Date: Mon, 8 Jan 2024 13:22:04 +0100 Subject: [PATCH] Script per creazione mirror quando assenti --- CheckGitlabMirroring.ps1 | 2 +- GitlabMirroringToGitea.ps1 | 13 ++- GitlabNewMirrorCreation.ps1 | 163 ++++++++++++++++++++++++++++++++++++ RenewGitlabMirroring.ps1 | 25 ++++-- 4 files changed, 190 insertions(+), 13 deletions(-) create mode 100644 GitlabNewMirrorCreation.ps1 diff --git a/CheckGitlabMirroring.ps1 b/CheckGitlabMirroring.ps1 index 6e79fe4..93a1a44 100644 --- a/CheckGitlabMirroring.ps1 +++ b/CheckGitlabMirroring.ps1 @@ -27,7 +27,7 @@ $mirrorErrorsCount = 0 $mirrorSuccessCount = 0 #livello di log: 0=log sintetico, 1=log errori, 2=log full, 3=log ampolloso -$logLevel = 2 +$logLevel = 3 #output a terminale: 0=disattivo, 1=abilitato $terminalOutput = 1 diff --git a/GitlabMirroringToGitea.ps1 b/GitlabMirroringToGitea.ps1 index 3107fa2..d641b8f 100644 --- a/GitlabMirroringToGitea.ps1 +++ b/GitlabMirroringToGitea.ps1 @@ -23,7 +23,7 @@ $userName = "replica" $projectCount = 1 #numero massimo di progetti -$projectNumber = 2 +$projectNumber = 200 #livello di log: 0=log sintetico, 1=log errori, 2=log full, 3=log ampolloso $logLevel = 3 @@ -41,7 +41,7 @@ $jsonBody = "" $GitLogFolder="c:\Steamware\Logs\Gitlab\" #nome file di log -$logFile = Join-Path $GitLogFolder "GitlabRebuildMirroring.log" +$logFile = Join-Path $GitLogFolder "GitlabMirroringGitea.log" #dichiaro funzione per scrittura output Function WriteLogOutput @@ -135,8 +135,13 @@ do #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 $gitlabHead -ContentType "application/json" -Body $jsonBody -UseBasicParsing - WriteLogOutput 1 "ID: $mirrorId - URL: $mirrorUrl - Mirror ricostruito con successo" + $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 1 "NEW ID: $mirrorId - URL: $mirrorUrl - Mirror ricostruito con successo" } } } diff --git a/GitlabNewMirrorCreation.ps1 b/GitlabNewMirrorCreation.ps1 new file mode 100644 index 0000000..46f82b1 --- /dev/null +++ b/GitlabNewMirrorCreation.ps1 @@ -0,0 +1,163 @@ +################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() + +#access token per autenticazione +#creato da profilo marco.locatelli@egalware.com su gitlab.steamware.net +#creazione 04 gennaio 2024, scadenza 31 dicembre 2024 +$gitlabHead = @{"PRIVATE-TOKEN"="glpat-VjT_SAsBk3s-yWE1LDUF"} + +#access token per autenticazione +#creato da profilo replica su gitea2.steamware.net +#$giteaHead = @{"Authorization"="token 21c3e674ae369eda349d0e1fadaddd526f90419d"} +$giteaPass = "ajejebrazorf92!" + +#nome utente gitea che effettua i mirror push +$giteaUser = "replica" + +#password replcia gogs +$gogsPass = "viaDante16!" + +#nome utente gogs che effettua i mirror push +$gogsUser = "replica" + +#contatore ciclo do while +$projectCount = 140 + +#numero massimo di progetti da analizzare +$projectNumber = 1 + +#livello di log: 0=log sintetico, 1=log errori, 2=log full, 3=log ampolloso +$logLevel = 2 + +#output a terminale: 0=disattivo, 1=abilitato +$terminalOutput = 1 + +#cartella file di log +$GitLogFolder="C:\Steamware\Logs\Gitlab\" + +#nome file di log +$logFile = Join-Path $GitLogFolder "GiteaNewMirrors.log" + +#dichiaro funzione per scrittura output +Function WriteLogOutput +{ + Param ($logType, [string]$logString) + #scrivo su file la stringa se il tipo di log è > o = al livello di log richieeso + if($logType -le $logLevel) + { + Add-content $logFile -value "$logString" + #scrivo su terminale la stringa se $terminalOutput=1 + if($terminalOutput -eq 1) + { + Write-Output($logString) + } + } +} + +Function FreshMirrorCreation +{ + Param ($projectNumber, $user, $auth, $destination, $path) + #compongo url da chiamare per creazione nuovo mirror + $callUrlCreateMirror = "https://gitlab.steamware.net/api/v4/projects/" + $projectNumber + "/remote_mirrors" + #creo url del nuovo mirror con username e token relativi a gitlab + $newMirror = "https://" + $user + ":" + $auth + $destination + "/" + $path + #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 1 "NEW ID: $mirrorId - URL: $mirrorUrl - Mirror creato con successo" +} + +#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 NUOVI MIRROR"} + 1 {WriteLogOutput 0 "LOG ERRORI NUOVI MIRROR"} + 2 {WriteLogOutput 0 "LOG FULL NUOVI MIRROR"} + 3 {WriteLogOutput 0 "LOG AMPOLLOSO NUOVI MIRROR"} +} +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 +{ + $callUrl = "https://gitlab.steamware.net/api/v4/projects/" + $projectCount + "/remote_mirrors" + try + { + $gitlabResponse = Invoke-WebRequest -URI $callUrl -Headers $gitlabHead -ContentType "application/json" -UseBasicParsing + #$parsedGitlabResponse = $gitlabResponse.Content | ConvertFrom-Json + #scrivo il numero del progetto nel terminale e su file + WriteLogOutput 1 "" + WriteLogOutput 1 "--------------------" + WriteLogOutput 1 "**PROGETTO $projectCount**" + if($gitlabResponse.Content -eq "[]") + { + $callUrl2 = "https://gitlab.steamware.net/api/v4/projects/" + $projectCount + $gitlabResponse2 = Invoke-WebRequest -URI $callUrl2 -Headers $gitlabHead -ContentType "application/json" -UseBasicParsing + $parsedResponse2 = $gitlabResponse2.Content | ConvertFrom-Json + $repoPath = $($parsedResponse2.path) + $path = "Egalware/" + $repoPath + ".git" + try + { + FreshMirrorCreation $projectCount $giteaUser $giteaPass "@gitea2.steamware.net" $path + FreshMirrorCreation $projectCount $gogsUser $gogsPass "@gogs.steamware.net" $path + } + catch + { + } + } + } + #scrivo se trovo un errore (in particolare progetto non trovato) durante il try/catch (solo se loglevel è = 3) + catch + { + WriteLogOutput 3 "" + WriteLogOutput 3 "--------------------" + WriteLogOutput 3 "**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 "" \ No newline at end of file diff --git a/RenewGitlabMirroring.ps1 b/RenewGitlabMirroring.ps1 index 062ae97..bf0bb54 100644 --- a/RenewGitlabMirroring.ps1 +++ b/RenewGitlabMirroring.ps1 @@ -6,17 +6,20 @@ $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"} +#access token per autenticazione creazione 04 gennaio 2024, scadenza 31 dicembre 2024 +#creato da profilo marco.locatelli@egalware.com su gitlab.steamware.net +$head = @{"PRIVATE-TOKEN"="glpat-VjT_SAsBk3s-yWE1LDUF"} #nome utente che effettua i mirror push $userName = "replica" -#token autenticazione utente replica azzano -$tokenAzzano = "glpat-gADkrEhkEsvQxGxKez4y" +#token autenticazione utente replica azzano creato a gennaio 2024, scadenza 31 dicembere 2024 +#$tokenAzzano = "glpat-gADkrEhkEsvQxGxKez4y" <<< TOKEN 2023 (IN DISUSO) +$tokenAzzano = "glpat-UHLqByZZr8tns6jwc4_-" -#token autenticazione utente replica nembro -$tokenNembro = "glpat-TzZkRUoYAdKikgwFXW_E" +#token autenticazione utente replica nembro creato a gennaio 2024, scadenza 31 dicembere 2024 +#$tokenNembro = "glpat-TzZkRUoYAdKikgwFXW_E" <<< TOKEN 2023 (IN DISUSO) +$tokenNembro = "glpat-puycZnyztFioe5tmkaso" #contatore ciclo do while $projectCount = 1 @@ -99,6 +102,7 @@ do $doExecute = "false" $token = "" $mirrorUrl = "" + $mirrorId = "" $trunkedPath = "" #controllo se l'url del mirror contiene azzano if($item.url.Contains("azzano")) @@ -141,8 +145,13 @@ do #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 "ID: $mirrorId - URL: $mirrorUrl - Mirror ricostruito con successo" + $rebuildResponse = Invoke-WebRequest -Method Post -URI $callUrlCreateMirror -Headers $head -ContentType "application/json" -Body $jsonBody -UseBasicParsing + $parsedRebuild = $rebuildResponse.Content | ConvertFrom-Json + foreach($item in $parsedRebuild){ + $mirrorId = $($item.id) + $mirrorUrl = $($item.url) + } + WriteLogOutput 1 "NEW ID: $mirrorId - URL: $mirrorUrl - Mirror ricostruito con successo" } } }