From 4e4ccf3c53d4fd0b29c8801c75d0756a55a9bf58 Mon Sep 17 00:00:00 2001 From: Sverker Abrahamsson Date: Mon, 10 Apr 2023 13:43:07 +0200 Subject: [PATCH] Added .gitattributes to make clones on Windows use lf as eol Added scripts for building and running on Windows Added generated files to .gitignore --- .gitattributes | 1 + .gitignore | 5 +++ check-dependencies.ps1 | 71 ++++++++++++++++++++++++++++++++++++++++++ create-certificate.ps1 | 39 +++++++++++++++++++++++ docker-run.ps1 | 29 +++++++++++++++++ run-checks.ps1 | 8 +++++ server/build-image.ps1 | 6 ++++ web/build-image.ps1 | 3 ++ 8 files changed, 162 insertions(+) create mode 100644 .gitattributes create mode 100644 check-dependencies.ps1 create mode 100644 create-certificate.ps1 create mode 100644 docker-run.ps1 create mode 100644 run-checks.ps1 create mode 100644 server/build-image.ps1 create mode 100644 web/build-image.ps1 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/.gitignore b/.gitignore index 937d518..81fc1ac 100644 --- a/.gitignore +++ b/.gitignore @@ -14,5 +14,10 @@ .env.test.local .env.production.local .cert +-cert-file +localhost.pem server/.cert +server/app/bin web/.cert +node_modules +yarn.lock diff --git a/check-dependencies.ps1 b/check-dependencies.ps1 new file mode 100644 index 0000000..9b88674 --- /dev/null +++ b/check-dependencies.ps1 @@ -0,0 +1,71 @@ +# Check if the needed tools are installed +$oldPreference = $ErrorActionPreference +$ErrorActionPreference = ‘stop’ + +$DockerExists=$false +try {if(Get-Command docker) { + $DockerExists=$true +}} Catch {“docker not installed”} + +$NpmExists=$false +try {if(Get-Command npm) { + $NpmExists=$true +}} Catch {“npm not installed”} + +$YarnExists=$false +try {if(Get-Command yarn) { + $YarnExists=$true +}} Catch {“yarn not installed”} + +$ChocoExists=$false +try {if(Get-Command choco) { + $ChocoExists=$true +}} Catch {“choco not installed”} + +$MkcertExists=$false +try {if(Get-Command mkcert) { + $MkcertExists=$true +}} Catch {“mkcert not installed”} + +$OpensslExists=$false +try {if(Get-Command openssl) { + $OpensslExists=$true +}} Catch {“openssl not installed”} +$ErrorActionPreference=$oldPreference + +if($DockerExists -and $NpmExists -and $YarnExists -and $ChocoExists -and $MkcertExists -and $OpensslExists) { + Write-Output "Dependencies docker, npm, yarn, choco, mkcert and openssl are present" + exit 0 +} + +if(!$DockerExists -or !$NpmExists -or !$ChocoExists) { + if(!$DockerExists) { + Write-Output "Please download and install Docker from https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe"; + } + + if(!$NpmExists -or !$ChocoExists) { + Write-Output "Please download and install NodeJS package including chocolade from https://nodejs.org/en/download"; + } + exit 1; +} + +if (!([Security.Principal.WindowsPrincipal] ` + [Security.Principal.WindowsIdentity]::GetCurrent() ` + ).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { + Write-Output "Installing dependencies requires elevated shell"; + exit 1; +} + +if(!$YarnExists) { + npm install --global yarn +} + +if(!$MkcertExists) { + choco install -y mkcert +} + +if(!$OpensslExists) { + choco install -y openssl +} + +RefreshEnv diff --git a/create-certificate.ps1 b/create-certificate.ps1 new file mode 100644 index 0000000..d2e2f72 --- /dev/null +++ b/create-certificate.ps1 @@ -0,0 +1,39 @@ +$KEY=".cert/localhost.key" +$CERT=".cert/localhost.pem" +$KEYSTORE="server/.cert/keystore.jks" + +if ((Test-Path "$KEY") -and (Test-Path "$CERT") -and (Test-Path "$KEYSTORE") -and (Test-Path "web/$CERT")) { + Write-Output "Key and cert exist and will not be recreated"; + exit 0; +} + +if (!([Security.Principal.WindowsPrincipal] ` + [Security.Principal.WindowsIdentity]::GetCurrent() ` + ).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { + Write-Output "Generating certificate requires elevated shell"; + exit 1; +} + +Write-Output "Generating new TLS certificate for localhost" + +mkcert -install +if (!(Test-Path ".cert")) { + mkdir -p .cert +} +mkcert -key-file $KEY -cert-file $CERT "localhost" + +if (!(Test-Path "web/.cert")) { + mkdir -p web/.cert +} +Copy-Item $KEY web/.cert/localhost.key +Copy-Item $CERT web/.cert/localhost.pem + +Write-Output "Converting generated certificate for use in server project" + +if (!(Test-Path "server/.cert")) { + mkdir -p server/.cert +} + +$openssl = 'C:\Program Files\Git\usr\bin\openssl' +& $openssl pkcs12 -export -in "$CERT" -inkey "$KEY" -out server/.cert/keystore.p12 -name "testCert" -password "pass:example-password" +keytool -importkeystore -noprompt -srckeystore server/.cert/keystore.p12 -srcstoretype pkcs12 -srcstorepass "example-password" -destkeystore server/.cert/keystore.jks -deststorepass "example-password" -destkeypass "example-password" diff --git a/docker-run.ps1 b/docker-run.ps1 new file mode 100644 index 0000000..7d5a0ee --- /dev/null +++ b/docker-run.ps1 @@ -0,0 +1,29 @@ +if (!(Test-Path "server\.env") -and !(Test-Path "web\.env")) { + Write-Warning "Please populate .env files as described in README.md" + exit 1 +} + +.\check-dependencies.ps1 +if ($LASTEXITCODE -ne 0) { + exit $LASTEXITCODE +} + +.\create-certificate.ps1 +if ($LASTEXITCODE -ne 0) { + exit $LASTEXITCODE +} + +Set-Location -Path web +.\build-image.ps1 +if ($LASTEXITCODE -ne 0) { + exit $LASTEXITCODE +} + +Set-Location -Path ..\server +.\build-image.ps1 +if ($LASTEXITCODE -ne 0) { + exit $LASTEXITCODE +} + +Set-Location -Path .. +docker compose up -d diff --git a/run-checks.ps1 b/run-checks.ps1 new file mode 100644 index 0000000..9f2cbde --- /dev/null +++ b/run-checks.ps1 @@ -0,0 +1,8 @@ +Set-Location -Path web +yarn lint +yarn test + +Set-Location -Path ..\server +.\gradlew check + +Set-Location -Path .. diff --git a/server/build-image.ps1 b/server/build-image.ps1 new file mode 100644 index 0000000..48b150c --- /dev/null +++ b/server/build-image.ps1 @@ -0,0 +1,6 @@ +./gradlew clean shadowJar + +docker build -t example-integration-server:test ` + --build-arg jar=app/build/libs/server-all.jar ` + --build-arg keystore=.cert/keystore.jks ` + . diff --git a/web/build-image.ps1 b/web/build-image.ps1 new file mode 100644 index 0000000..12405fc --- /dev/null +++ b/web/build-image.ps1 @@ -0,0 +1,3 @@ +yarn && yarn build + +docker build --no-cache -t example-integration-web:test .