167 lines
6.1 KiB
PowerShell
167 lines
6.1 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()
|
|
|
|
#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"}
|
|
|
|
#contatore ciclo do while
|
|
$projectCount = 1
|
|
|
|
#numero massimo di progetti da analizzare
|
|
$projectNumber = 200
|
|
|
|
#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
|
|
|
|
#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)
|
|
}
|
|
}
|
|
}
|
|
|
|
#funzione che crea mirror con chiamata API
|
|
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
|
|
#conversione da Json della risposta alla chiamata POST
|
|
$parsedRebuild = $rebuildResponse.Content | ConvertFrom-Json
|
|
#rilevo ID e URL nuovo mirror per scriverli nel log
|
|
foreach($item in $parsedRebuild){
|
|
$mirrorId = $($item.id)
|
|
$mirrorUrl = $($item.url)
|
|
}
|
|
#scrivo ID e URL nuovo mirror
|
|
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**"
|
|
#ricavo nome del progetto per poter creare mirror
|
|
$nameCall = "https://gitlab.steamware.net/api/v4/projects/" + $projectCount
|
|
$nameResponse = Invoke-WebRequest -URI $nameCall -Headers $gitlabHead -ContentType "application/json" -UseBasicParsing
|
|
$parsedNameResponse = $nameResponse.Content | ConvertFrom-Json
|
|
$repoPath = $($parsedNameResponse.path)
|
|
#compongo path con struttura Egalware/NomeProgetto.git
|
|
$path = "Egalware/" + $repoPath + ".git"
|
|
#se la risposta non contiene "gitea" procedo a creare mirror relativo
|
|
if($gitlabResponse.Content -inotmatch "gitea")
|
|
{
|
|
#autenticazione replica gitea
|
|
$giteaPass = "ajejebrazorf92!"
|
|
#nome utente gitea che effettua i mirror push
|
|
$giteaUser = "replica"
|
|
#destinazione mirror per gitea
|
|
$giteaDestination = "@gitea.steamware.net"
|
|
#chiamo funzione per creare mirror verso gitea
|
|
FreshMirrorCreation $projectCount $giteaUser $giteaPass $giteaDestination $path
|
|
}
|
|
#se la risposta non contiene "gogs" procedo a creare mirror relativo
|
|
if($gitlabResponse.Content -inotmatch "gogs")
|
|
{
|
|
#password replica gogs
|
|
$gogsPass = "viaDante16!"
|
|
#nome utente gogs che effettua i mirror push
|
|
$gogsUser = "replica"
|
|
#destinazione mirror per gogs
|
|
$gogsDestination = "@gogs.steamware.net"
|
|
#chiamo funzione per creare mirror verso gogs
|
|
FreshMirrorCreation $projectCount $gogsUser $gogsPass $gogsDestination $path
|
|
}
|
|
}
|
|
#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 "" |