Untitled

mail@pastecode.io avatar
unknown
powershell
a year ago
6.1 kB
3
Indexable
Never
# Script Powershell permettant d'installer OpenJDK 8 depuis Nexus
# @author open|core
# ---------------------------------------------------------------------------
Set-Variable SCRIPT_NAME -option Constant -Scope Script -value $MyInvocation.MyCommand.Name
Set-Variable SCRIPT_VERSION -option Constant -Scope Script -value "2023-01-12"
Set-Variable PDT_BASE_DIR -option Constant -Scope Script -value "D:\Applics\pdt"
Set-Variable PDT_BINARIES_DIR -option Constant -Scope Script -value "D:\Applics\pdt\bin"
Set-Variable PDT_LOGS_DIR -option Constant -Scope Script -value "D:\Applics\pdt\logs"
Set-Variable JDK_FOLDER_NAME -option Constant -Scope Script -value "java-1.8.0-openjdk-1.8.0.212-3.b04.redhat.windows.x86_64"
Set-Variable JDK_DOWNLOAD_URL -option Constant -Scope Script -value "http://registry.beluga.intra.groupama.fr/repository/gtec-binaries/jdk-cert/$JDK_FOLDER_NAME.zip"
Set-Variable JDK_INSTALL_DIR -option Constant -Scope Script -value "D:\Applics"
Set-Variable JDK_LOCAL_ARCHIVE -option Constant -Scope Script -value "$PDT_BINARIES_DIR\openjdk8.zip"

Function CheckPDT {
	# Admin ?
	if (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
		Write-Warning "(!) Ce script ne devrait pas être exécuté en tant qu'administrateur !"
	}
	# Le dossier racine doit exister, sinon on le crée
	if (-not (Test-Path -Path $PDT_BASE_DIR -PathType Container)) {
		New-Item -Path $PDT_BASE_DIR -ItemType Directory
		Write-Output "(i) Dossier $PDT_BASE_DIR créé"
	}
	# ... et le dossier de logs si besoin
	if (-not (Test-Path -Path $PDT_LOGS_DIR -PathType Container)) {
		New-Item -Path $PDT_LOGS_DIR -ItemType Directory
		Write-Output "(i) Dossier $PDT_LOGS_DIR créé"
	}
	# ... et le dossier de stockage des binaires
	if (-not (Test-Path -Path $PDT_BINARIES_DIR -PathType Container)) {
		New-Item -Path $PDT_BINARIES_DIR -ItemType Directory
		Write-Output "(i) Dossier $PDT_BINARIES_DIR créé"
	}
}
Function InstallJDK {
	if (Test-Path -Path $JDK_LOCAL_ARCHIVE -PathType Leaf) {
		Write-Output "(i) Le fichier $JDK_LOCAL_ARCHIVE existe déjà il ne sera pas écrasé"
	} else {
		Write-Output "(i) Téléchargement de l'archive depuis Nexus (dans $JDK_LOCAL_ARCHIVE), veuillez patienter ..."
		try {
			Invoke-WebRequest -Uri $JDK_DOWNLOAD_URL -OutFile $JDK_LOCAL_ARCHIVE
			Add-Content -Path "$PDT_LOGS_DIR\download.log" -value "$(Get-Date -format 'u');OpenJDK 8;1.8.0.212-3;$JDK_DOWNLOAD_URL;$JDK_LOCAL_ARCHIVE;OK;$SCRIPT_NAME;$SCRIPT_VERSION;$Env:USERNAME"
		} catch {
			Add-Content -Path "$PDT_LOGS_DIR\download.log" -value "$(Get-Date -format 'u');OpenJDK 8;1.8.0.212-3;$JDK_DOWNLOAD_URL;$JDK_LOCAL_ARCHIVE;ECHEC;$SCRIPT_NAME;$SCRIPT_VERSION;$Env:USERNAME"
			Throw "Echec download $JDK_DOWNLOAD_URL, assurez-vous d'être bien connecté au réseau Groupama"
		}
	}

	if (Test-Path -Path "$JDK_INSTALL_DIR\$JDK_FOLDER_NAME" -PathType Container) {
		Write-Output "/!\ L'outil semble déjà installé, extraction non effectuée"
	} else {
		Write-Output "(i) Extraction de l'archive, veuillez patienter ..."
		try {
			Expand-Archive -Path $JDK_LOCAL_ARCHIVE -Force -DestinationPath $JDK_INSTALL_DIR
			Write-Output "(i) Extraction OK dans $JDK_INSTALL_DIR"
			Add-Content -Path "$PDT_LOGS_DIR\install.log" -value "$(Get-Date -format 'u');OpenJDK 8;1.8.0.212-3;$JDK_INSTALL_DIR\$JDK_FOLDER_NAME;$JDK_LOCAL_ARCHIVE;OK;$SCRIPT_NAME;$SCRIPT_VERSION;$Env:USERNAME"
		} catch {
			Add-Content -Path "$PDT_LOGS_DIR\install.log" -value "$(Get-Date -format 'u');OpenJDK 8;1.8.0.212-3;$JDK_INSTALL_DIR\$JDK_FOLDER_NAME;$JDK_LOCAL_ARCHIVE;ECHEC;$SCRIPT_NAME;$SCRIPT_VERSION;$Env:USERNAME"
			Throw "Echec extraction $JDK_LOCAL_ARCHIVE dans $JDK_INSTALL_DIR : $_.Exception.Message"
		}
	}
	# Test avec java -version
	Write-Output "(i) Version Java ..."
	& "$JDK_INSTALL_DIR\$JDK_FOLDER_NAME\bin\java.exe" -version
}
Function UninstallJDK {
	if (Test-Path -Path "$JDK_INSTALL_DIR\$JDK_FOLDER_NAME" -PathType Container) {
		try {
			Remove-Item -Path "$JDK_INSTALL_DIR\$JDK_FOLDER_NAME" -Force -Recurse
			Add-Content -Path "$PDT_LOGS_DIR\uninstall.log" -value "$(Get-Date -format 'u');OpenJDK JDK 8;1.8.0.212-3;$JDK_INSTALL_DIR\$JDK_FOLDER_NAME;$SQLDEV_LOCAL_ARCHIVE;OK;$SCRIPT_NAME;$SCRIPT_VERSION;$Env:USERNAME"
		} catch {
			Write-Warning "(!) Echec suppression de $JDK_INSTALL_DIR\$JDK_FOLDER_NAME : $_.Exception.Message"
			Add-Content -Path "$PDT_LOGS_DIR\uninstall.log" -value "$(Get-Date -format 'u');OpenJDK JDK 8;1.8.0.212-3;$JDK_INSTALL_DIR\$JDK_FOLDER_NAME;$SQLDEV_LOCAL_ARCHIVE;ECHEC;$SCRIPT_NAME;$SCRIPT_VERSION;$Env:USERNAME"
		}
	} else {
		Write-Output "/!\ L'outil n'est pas installé, suppression non effectuée"
	}
}
# ---------------------------------------------------------
# MAIN
# ---------------------------------------------------------
if ($Args[0] -eq '--help') {
	Write-Output "Ce script permet d'installer OpenJDK 8 (incluant les certificats Groupama) depuis notre référentiel interne"
	Write-Output "(i) Usage :"
	Write-Output "  $SCRIPT_NAME --version   # version du script"
	Write-Output "  $SCRIPT_NAME --dry       # mode informatif"
	Write-Output "  $SCRIPT_NAME             # mode réel"
	Write-Output "  $SCRIPT_NAME --uninstall # effectue la désinstallation"
} elseif ($Args[0] -eq '--version') {
	Write-Output "$SCRIPT_VERSION"
} elseif ($Args[0] -eq '--dry') {
	Write-Output "$SCRIPT_NAME : JDK_INSTALL_DIR = $JDK_INSTALL_DIR"
	Write-Output "$SCRIPT_NAME : JDK_DOWNLOAD_URL = $JDK_DOWNLOAD_URL"
	Write-Output "$SCRIPT_NAME : JDK_LOCAL_ARCHIVE = $JDK_LOCAL_ARCHIVE"
	CheckPDT
	if (Test-Path -Path $JDK_LOCAL_ARCHIVE -PathType Leaf) {
		Write-Output "(i) Le fichier $JDK_LOCAL_ARCHIVE existe déjà il ne sera pas écrasé"
	}
	if (Test-Path -Path "$JDK_INSTALL_DIR\$JDK_FOLDER_NAME" -PathType Container) {
		Write-Output "(i) L'outil semble déjà installé sur le poste"
		& "$JDK_INSTALL_DIR\$JDK_FOLDER_NAME\bin\java.exe" -version
	}
} elseif ($Args[0] -eq '--uninstall') {
	CheckPDT
	UninstallJDK
} else {
	CheckPDT
	InstallJDK
}