Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
(GH-263) Add helper scripts
Browse files Browse the repository at this point in the history
This commit adds the build/install scripts and makefile
to the project, ensuring that the app can be built/installed
without specialized knowledge or steps.
  • Loading branch information
michaeltlombardi committed Oct 27, 2021
1 parent ac35405 commit d7cb9b1
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
quality: format lint sec tidy

# Run go mod tidy and check go.sum is unchanged
PHONY+= tidy
tidy:
@echo "🔘 Checking that go mod tidy does not make a change..."
@cp go.sum go.sum.bak
@go mod tidy
@diff go.sum go.sum.bak && rm go.sum.bak || (echo "🔴 go mod tidy would make a change, exiting"; exit 1)
@echo "✅ Checking go mod tidy complete"

# Format go code and error if any changes are made
PHONY+= format
format:
@echo "🔘 Checking that go fmt does not make any changes..."
@test -z $$(go fmt ./...) || (echo "🔴 go fmt would make a change, exiting"; exit 1)
@echo "✅ Checking go fmt complete"

PHONY+= lint
lint: $(GOPATH)/bin/golangci-lint
@echo "🔘 Linting $(1) (`date '+%H:%M:%S'`)"
@lint=`golint ./...`; \
if [ "$$lint" != "" ]; \
then echo "🔴 Lint found by golint"; echo "$$lint"; exit 1;\
fi
@lint=`go vet ./...`; \
if [ "$$lint" != "" ]; \
then echo "🔴 Lint found by go vet"; echo "$$lint"; exit 1;\
fi
@lint=`golangci-lint run`; \
if [ "$$lint" != "" ]; \
then echo "🔴 Lint found by golangci-lint"; echo "$$lint"; exit 1;\
fi
@echo "✅ Lint-free (`date '+%H:%M:%S'`)"

PHONY+= sec
sec: $(GOPATH)/bin/gosec
@echo "🔘 Checking for security problems ... (`date '+%H:%M:%S'`)"
@sec=`gosec -exclude-dir=testutils -quiet ./...`; \
if [ "$$sec" != "" ]; \
then echo "🔴 Problems found"; echo "$$sec"; exit 1;\
else echo "✅ No problems found (`date '+%H:%M:%S'`)"; \
fi

default: quality

.PHONY: $(PHONY)
44 changes: 44 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env pwsh

[CmdletBinding()]
param (
[Parameter()]
[ValidateSet('build', 'quick', 'package')]
[string]
$Target = 'build'
)
$Env:WORKINGDIR = $PSScriptRoot

$arch = go env GOHOSTARCH
$platform = go env GOHOSTOS
$binPath = Join-Path $PSScriptRoot 'dist' "prm_${platform}_${arch}"
$binPath2 = Join-Path $PSScriptRoot 'dist' "notel_prm_${platform}_${arch}"

switch ($Target) {
'build' {
# Set goreleaser to build for current platform only
# Add environment variables for honeycomb if not already loaded
if (!(Test-Path ENV:\HONEYCOMB_API_KEY)) {
$ENV:HONEYCOMB_API_KEY = 'not_set'
}
if (!(Test-Path ENV:\HONEYCOMB_DATASET)) {
$ENV:HONEYCOMB_DATASET = 'not_set'
}
goreleaser build --snapshot --rm-dist --single-target
# git clone -b main --depth 1 --single-branch https://github.com/puppetlabs/baker-round (Join-Path $binPath 'templates')
# Copy-Item (Join-Path $binPath 'templates') -Destination (Join-Path $binPath2 'templates') -Recurse
}
'quick' {
If ($Env:OS -match '^Windows') {
go build -o "$binPath/prm.exe" -tags telemetry
go build -o "$binPath2/prm.exe"
} else {
go build -o "$binPath/prm" -tags telemetry
go build -o "$binPath2/prm"
}
}
'package' {
# git clone -b main --depth 1 --single-branch https://github.com/puppetlabs/baker-round 'templates'
goreleaser --skip-publish --snapshot --rm-dist
}
}
28 changes: 28 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

export WORKINGDIR=$(pwd)

target=${1:-build}
arch=$(go env GOHOSTARCH)
platform=$(go env GOHOSTOS)
binPath="$(pwd)/dist/prm_${platform}_${arch}"
binPath2="$(pwd)/dist/notel_prm_${platform}_${arch}"

if [ "$target" == "build" ]; then
# Set goreleaser to build for current platform only
if [ -z "${HONEYCOMB_API_KEY}" ]; then
export HONEYCOMB_API_KEY="not_set"
fi
if [ -z "${HONEYCOMB_DATASET}" ]; then
export HONEYCOMB_DATASET="not_set"
fi
goreleaser build --snapshot --rm-dist --single-target
# git clone -b main --depth 1 --single-branch https://github.com/puppetlabs/baker-round "$binPath/templates"
# cp -r "$binPath/templates" "$binPath2/templates"
elif [ "$target" == "quick" ]; then
go build -o ${binPath}/prm -tags telemetry
go build -o ${binPath2}/prm
elif [ "$target" == "package" ]; then
# git clone -b main --depth 1 --single-branch https://github.com/puppetlabs/baker-round "templates"
goreleaser --skip-publish --snapshot --rm-dist
fi
59 changes: 59 additions & 0 deletions tools/install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
function Install-Prm {
[CmdletBinding()]
param (
[switch]$NoTelemetry
)

Set-StrictMode -Version 3.0
$ErrorActionPreference = 'Stop'

$org = 'puppetlabs'
$repo = 'prm'

$app = 'prm'

$appPkgName = 'prm'
if ($NoTelemetry) {
$appPkgName = 'notel_prm'
}

$arch = 'x86_64'
$os = 'windows'
$ext = '.zip'

$file = "${appPkgName}_${os}_${arch}${ext}"
$downloadURL = "https://github.com/${org}/${repo}/releases/latest/download/$file"

$Destination = '~/.puppetlabs/prm'
$Destination = $PSCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Destination)

$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())
$null = New-Item -ItemType Directory -Path $tempDir -Force -ErrorAction SilentlyContinue
$packagePath = Join-Path -Path $tempDir -ChildPath $file

if (!$PSVersionTable.ContainsKey('PSEdition') -or $PSVersionTable.PSEdition -eq 'Desktop') {
$oldProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
}

try {
if ($NoTelemetry) {
Write-Host "Downloading and extracting ${app} (TELEMETRY DISABLED VERSION) to ${Destination}"
} else {
Write-Host "Downloading and extracting ${app} to ${Destination}"
}
Invoke-WebRequest -Uri $downloadURL -OutFile $packagePath
} finally {
if (!$PSVersionTable.ContainsKey('PSEdition') -or $PSVersionTable.PSEdition -eq 'Desktop') {
$ProgressPreference = $oldProgressPreference
}
}

if (Test-Path -Path $Destination) {
Remove-Item -Path $Destination -Force -Recurse
}
Expand-Archive -Path $packagePath -DestinationPath $Destination

Write-Host 'Remember to add the prm app to your path:'
Write-Host "`$env:Path += `"`$env:PATH;${Destination}`""
}
98 changes: 98 additions & 0 deletions tools/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/bin/sh
set -e

ARCH="x86_64"
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
EXT=".tar.gz"

ORG="puppetlabs"
REPO="prm"
APP="prm"
APP_PKG_NAME="prm"

NO_TEL=${1:-false}

if [ ${NO_TEL} = "--no-telemetry" ]; then
APP_PKG_NAME="notel_prm"
fi

RELEASES=""
FILE=""
CHECKSUM=""

logDebug() {
if [ ! -z $PRM_INSTALL_DEBUG ]; then
echo $1
fi
}

getChecksums() {
for i in {1..5}; do
FILE="${APP_PKG_NAME}_${OS}_${ARCH}${EXT}"
checksumURL="https://github.com/${ORG}/${REPO}/releases/latest/download/checksums.txt"
resp=$(curl -Ls "${checksumURL}" -o /tmp/prm_checksums.txt --write-out "%{http_code}")
respCode=$(echo ${resp} | tail -n 1)
logDebug "GET ${checksumURL} | Resp: ${resp}"
if [ ${respCode} -ne 200 ]; then
echo "Fetching checksums.txt failed on attempt ${i}, retrying..."
sleep 5
else
CHECKSUM=$(grep " ${FILE}" /tmp/prm_checksums.txt | cut -d ' ' -f 1)
return 0
fi
done
echo "Fetching checksums.txt failed after max retry attempts"
exit 1
}

downloadLatestRelease() {
destination="${HOME}/.puppetlabs/prm"

[ -d ${destination} ] || mkdir -p ${destination} ]

if [ "${noTel}" = "--no-telemetry" ]; then
echo "Downloading and extracting ${APP_PKG_NAME} (TELEMETRY DISABLED VERSION) to ${destination}"
else
echo "Downloading and extracting ${APP_PKG_NAME} to ${destination}"
fi

downloadURL="https://github.com/${ORG}/${REPO}/releases/latest/download/${FILE}"

for i in {1..5}; do
resp=$(curl -Ls ${downloadURL} -o /tmp/${FILE} --write-out "%{http_code}")
respCode=$(echo ${resp} | tail -n 1)
logDebug "GET ${downloadURL} | Resp: ${resp}"
if [ ${respCode} -ne 200 ]; then
echo "Fetching PRM package failed on attempt ${i}, retrying..."
sleep 5
else
downloadChecksumRaw=$(shasum -a 256 /tmp/${FILE} || sha256sum /tmp/${FILE})
downloadChecksum=$(echo ${downloadChecksumRaw} | cut -d ' ' -f 1)
logDebug "Checksum calc for ${FILE}:"
logDebug " - Expect checksum: ${CHECKSUM}"
logDebug " - Actual checksum: ${downloadChecksum}"
if [ ${downloadChecksum} = ${CHECKSUM} ]; then
logDebug "Extracting /tmp/${FILE} to ${destination}"
tar -zxf "/tmp/${FILE}" -C ${destination}
tarStatus=$(echo $?)
logDebug "Removing /tmp/${FILE}"
rm "/tmp/${FILE}"
if [ ${tarStatus} -eq 0 ]; then
echo "Remember to add the prm app to your path:"
echo 'export PATH=$PATH:'${destination}
exit 0
else
echo "Untar unsuccessful (status code: $?)"
exit 1
fi
else
echo "Checksum verification failed for ${FILE}"
exit 1
fi
return 0
fi
done
}

getChecksums
downloadLatestRelease

0 comments on commit d7cb9b1

Please sign in to comment.