Skip to content

Commit

Permalink
create build and release scripts
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Wang <wachao@vmware.com>
  • Loading branch information
ahrtr committed Apr 23, 2023
1 parent b26354e commit b7ffe31
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea
etcd-defrag
/bin
/release

20 changes: 20 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

set -euo pipefail

GIT_SHA=$(git rev-parse --short HEAD || echo "GitNotFound")
VERSION_SHA="main.GitSHA"

# use go env if noset
GOOS=${GOOS:-$(go env GOOS)}
GOARCH=${GOARCH:-$(go env GOARCH)}

GO_BUILD_FLAGS=${GO_BUILD_FLAGS:-}

GO_LDFLAGS=(${GO_LDFLAGS:-} "-X=${VERSION_SHA}=${GIT_SHA}")
GO_BUILD_ENV=("CGO_ENABLED=0" "GO_BUILD_FLAGS=${GO_BUILD_FLAGS}" "GOOS=${GOOS}" "GOARCH=${GOARCH}")

rm -f ./bin/etcd-defrag

env "${GO_BUILD_ENV[@]}" go build $GO_BUILD_FLAGS -trimpath -installsuffix=cgo "-ldflags=${GO_LDFLAGS[*]}" -o ./bin/etcd-defrag

2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type globalConfig struct {

dbQuotaBytes int
defragRule string

printVersion bool
}

func clientConfigWithoutEndpoints(gcfg globalConfig) *clientv3.ConfigSpec {
Expand Down
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"runtime"
"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,6 +50,7 @@ func newDefragCommand() *cobra.Command {
defragCmd.Flags().IntVar(&globalCfg.dbQuotaBytes, "etcd-storage-quota-bytes", 2*1024*1024*1024, "etcd storage quota in bytes (the value passed to etcd instance by flag --quota-backend-bytes)")
defragCmd.Flags().StringVar(&globalCfg.defragRule, "defrag-rule", "", "defragmentation rule (etcd-defrag will run defragmentation if the rule is empty or it is evaluated to true)")

defragCmd.Flags().BoolVar(&globalCfg.printVersion, "version", false, "print the version and exit")
return defragCmd
}

Expand All @@ -64,7 +66,19 @@ func main() {
}
}

func printVersion(printVersion bool) {
if printVersion {
fmt.Printf("etcd-defrag Version: %s\n", Version)
fmt.Printf("Git SHA: %s\n", GitSHA)
fmt.Printf("Go Version: %s\n", runtime.Version())
fmt.Printf("Go OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
os.Exit(0)
}
}

func defragCommandFunc(cmd *cobra.Command, args []string) {
printVersion(globalCfg.printVersion)

fmt.Println("Validating configuration.")
if err := validateConfig(cmd, globalCfg); err != nil {
fmt.Fprintf(os.Stderr, "Validating configuration failed: %v\n", err)
Expand Down
91 changes: 91 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env bash

set -euo pipefail

VER=${1:-}
REPOSITORY="${REPOSITORY:-https://github.com/ahrtr/etcd-defrag.git}"

if [ -z "$VER" ]; then
echo "Usage: ${0} VERSION" >> /dev/stderr
exit 255
fi

function setup_env {
local ver=${1}
local proj=${2}

if [ ! -d "${proj}" ]; then
git clone "${REPOSITORY}"
fi

pushd "${proj}" >/dev/null
git fetch --all
git checkout "${ver}"
popd >/dev/null
}

function package {
local target=${1}
local srcdir="${2}/bin"

local ccdir="${srcdir}/${GOOS}_${GOARCH}"
if [ -d "${ccdir}" ]; then
srcdir="${ccdir}"
fi
local ext=""
if [ "${GOOS}" == "windows" ]; then
ext=".exe"
fi
cp "${srcdir}/etcd-defrag" "${target}/etcd-defrag${ext}"

cp etcd/README.md "${target}"/README.md
}

function main {
local proj="etcd-defrag"

mkdir -p release
cd release
setup_env "${VER}" "${proj}"

local tarcmd=tar

for os in darwin windows linux; do
export GOOS=${os}
TARGET_ARCHS=("amd64")

if [ ${GOOS} == "linux" ]; then
TARGET_ARCHS+=("arm64")
TARGET_ARCHS+=("ppc64le")
TARGET_ARCHS+=("s390x")
fi

if [ ${GOOS} == "darwin" ]; then
TARGET_ARCHS+=("arm64")
fi

for TARGET_ARCH in "${TARGET_ARCHS[@]}"; do
export GOARCH=${TARGET_ARCH}

pushd ${proj} >/dev/null
GO_LDFLAGS="-s -w" ./build.sh
popd >/dev/null

TARGET="${proj}-${VER}-${GOOS}-${GOARCH}"
mkdir "${TARGET}"
package "${TARGET}" "${proj}"

if [ ${GOOS} == "linux" ]; then
${tarcmd} cfz "${TARGET}.tar.gz" "${TARGET}"
echo "Wrote release/${TARGET}.tar.gz"
else
zip -qr "${TARGET}.zip" "${TARGET}"
echo "Wrote release/${TARGET}.zip"
fi
done
done
}

main


7 changes: 7 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

var (
Version = "0.1.0"
// GitSHA is the commit SHA value set during build
GitSHA = "Not provided (use ./build.sh)"
)

0 comments on commit b7ffe31

Please sign in to comment.