summaryrefslogtreecommitdiffhomepage
path: root/tools/unitctl/build
diff options
context:
space:
mode:
Diffstat (limited to 'tools/unitctl/build')
-rw-r--r--tools/unitctl/build/container.mk67
-rw-r--r--tools/unitctl/build/github.mk22
-rwxr-xr-xtools/unitctl/build/openapi-generator-cli.sh77
-rw-r--r--tools/unitctl/build/package.mk139
-rw-r--r--tools/unitctl/build/release.mk57
5 files changed, 362 insertions, 0 deletions
diff --git a/tools/unitctl/build/container.mk b/tools/unitctl/build/container.mk
new file mode 100644
index 00000000..c892db2e
--- /dev/null
+++ b/tools/unitctl/build/container.mk
@@ -0,0 +1,67 @@
+ ## Builds a container image for building on Debian Linux
+.PHONY: container-debian-build-image
+.ONESHELL: container-debian-build-image
+container-debian-build-image:
+container-debian-build-image:
+ $Q echo "$(M) building debian linux docker build image: $(@)"
+ $(DOCKER) buildx build $(DOCKER_BUILD_FLAGS)\
+ -t debian_builder -f Dockerfile $(CURDIR);
+
+ ## Builds deb packages using a container image
+.PHONY: container-deb-packages
+container-deb-packages: container-debian-build-image
+ $Q $(DOCKER) run --rm --volume "$(CURDIR):/project" \
+ --workdir /project debian_builder make deb-packages
+ # Reset permissions on the target directory to the current user
+ if command -v id > /dev/null; then \
+ $(DOCKER) run --rm --volume "$(CURDIR):/project" \
+ --workdir /project debian_builder \
+ chown --recursive "$(shell id -u):$(shell id -g)" /project/target
+ fi
+
+ ## Builds a rpm packages using a container image
+.PHONY: container-rpm-packages
+container-rpm-packages: container-debian-build-image
+ $Q $(DOCKER) run --rm --volume "$(CURDIR):/project" \
+ --workdir /project debian_builder make rpm-packages
+ # Reset permissions on the target directory to the current user
+ if command -v id > /dev/null; then \
+ $(DOCKER) run --rm --volume "$(CURDIR):/project" \
+ --workdir /project debian_builder chown --recursive \
+ "$(shell id -u):$(shell id -g)" /project/target
+ fi
+
+## Builds all packages using a container image
+.PHONY: container-all-packages
+container-all-packages: container-debian-build-image
+ $Q $(DOCKER) run --rm --volume "$(CURDIR):/project" \
+ --workdir /project debian_builder make all-packages
+ # Reset permissions on the target directory to the current user
+ if command -v id > /dev/null; then \
+ $(DOCKER) run --rm --volume "$(CURDIR):/project" \
+ --workdir /project debian_builder \
+ chown --recursive "$(shell id -u):$(shell id -g)" /project/target
+ fi
+
+## Run tests inside container
+.PHONY: container-test
+container-test: container-debian-build-image
+ $Q $(DOCKER) run --rm --volume "$(CURDIR):/project" \
+ --workdir /project debian_builder make test
+ # Reset permissions on the target directory to the current user
+ if command -v id > /dev/null; then \
+ $(DOCKER) run --rm --volume "$(CURDIR):/project" \
+ --workdir /project debian_builder \
+ chown --recursive "$(shell id -u):$(shell id -g)" /project/target
+ fi
+
+.PHONY: container-shell
+container-shell: container-debian-build-image ## Run tests inside container
+ $Q $(DOCKER) run -it --rm --volume "$(CURDIR):/project" \
+ --workdir /project debian_builder bash
+ # Reset permissions on the target directory to the current user
+ if command -v id > /dev/null; then \
+ $(DOCKER) run --rm --volume "$(CURDIR):/project" \
+ --workdir /project debian_builder \
+ chown --recursive "$(shell id -u):$(shell id -g)" /project/target
+ fi
diff --git a/tools/unitctl/build/github.mk b/tools/unitctl/build/github.mk
new file mode 100644
index 00000000..4d31546f
--- /dev/null
+++ b/tools/unitctl/build/github.mk
@@ -0,0 +1,22 @@
+.PHONY: gh-make-release
+.ONESHELL: gh-make-release
+gh-make-release:
+ifndef CI
+ $(error must be running in CI)
+endif
+ifneq ($(shell git rev-parse --abbrev-ref HEAD),release-v$(VERSION))
+ $(error must be running on release-v$(VERSION) branch)
+endif
+ $(info $(M) updating files with release version [$(GIT_BRANCH)]) @
+ git commit -m "ci: update files to version $(VERSION)" \
+ Cargo.toml pkg/brew/$(PACKAGE_NAME).rb
+ git push origin "release-v$(VERSION)"
+ git tag -a "v$(VERSION)" -m "ci: tagging v$(VERSION)"
+ git push origin --tags
+ gh release create "v$(VERSION)" \
+ --title "v$(VERSION)" \
+ --notes-file $(CURDIR)/target/dist/release_notes.md \
+ $(CURDIR)/target/dist/*.gz \
+ $(CURDIR)/target/dist/*.deb \
+ $(CURDIR)/target/dist/*.rpm \
+ $(CURDIR)/target/dist/SHA256SUMS
diff --git a/tools/unitctl/build/openapi-generator-cli.sh b/tools/unitctl/build/openapi-generator-cli.sh
new file mode 100755
index 00000000..3a65e5ce
--- /dev/null
+++ b/tools/unitctl/build/openapi-generator-cli.sh
@@ -0,0 +1,77 @@
+#!/usr/bin/env bash
+
+# Source: https://github.com/OpenAPITools/openapi-generator/blob/master/bin/utils/openapi-generator-cli.sh
+# License: Apache 2.0
+
+####
+# Save as openapi-generator-cli on your PATH. chmod u+x. Enjoy.
+#
+# This script will query github on every invocation to pull the latest released
+# version of openapi-generator.
+#
+# If you want repeatable executions, you can explicitly set a version via
+# OPENAPI_GENERATOR_VERSION
+# e.g. (in Bash)
+# export OPENAPI_GENERATOR_VERSION=3.1.0
+# openapi-generator-cli.sh
+# or
+# OPENAPI_GENERATOR_VERSION=3.1.0 openapi-generator-cli.sh
+#
+# This is also helpful, for example, if you want to evaluate a SNAPSHOT version.
+#
+# NOTE: Jars are downloaded on demand from maven into the same directory as this
+# script for every 'latest' version pulled from github. Consider putting this
+# under its own directory.
+####
+set -o pipefail
+
+for cmd in {mvn,jq,curl}; do
+ if ! command -v ${cmd} > /dev/null; then
+ >&2 echo "This script requires '${cmd}' to be installed."
+ exit 1
+ fi
+done
+
+function latest.tag {
+ local uri="https://api.github.com/repos/${1}/releases"
+ local ver=$(curl -s ${uri} | jq -r 'first(.[]|select(.prerelease==false)).tag_name')
+ if [[ $ver == v* ]]; then
+ ver=${ver:1}
+ fi
+ echo $ver
+}
+
+ghrepo=openapitools/openapi-generator
+groupid=org.openapitools
+artifactid=openapi-generator-cli
+ver=${OPENAPI_GENERATOR_VERSION:-$(latest.tag $ghrepo)}
+
+echo "Using OpenAPI Generator version: ${ver}"
+
+jar=${artifactid}-${ver}.jar
+cachedir=${OPENAPI_GENERATOR_DOWNLOAD_CACHE_DIR}
+
+DIR=${cachedir:-"$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"}
+
+if [ ! -d "${DIR}" ]; then
+ mkdir -p "${DIR}"
+fi
+
+if [ ! -f ${DIR}/${jar} ]; then
+ repo="central::default::https://repo1.maven.org/maven2/"
+ if [[ ${ver} =~ ^.*-SNAPSHOT$ ]]; then
+ repo="central::default::https://oss.sonatype.org/content/repositories/snapshots"
+ fi
+ mvn org.apache.maven.plugins:maven-dependency-plugin:2.9:get \
+ -DremoteRepositories=${repo} \
+ -Dartifact=${groupid}:${artifactid}:${ver} \
+ -Dtransitive=false \
+ -Ddest=${DIR}/${jar}
+fi
+
+java -ea \
+ ${JAVA_OPTS} \
+ -Xms512M \
+ -Xmx1024M \
+ -server \
+ -jar ${DIR}/${jar} "$@" \ No newline at end of file
diff --git a/tools/unitctl/build/package.mk b/tools/unitctl/build/package.mk
new file mode 100644
index 00000000..7009e2b1
--- /dev/null
+++ b/tools/unitctl/build/package.mk
@@ -0,0 +1,139 @@
+.PHONY: install-packaging-deb
+install-packaging-deb:
+ $Q if ! command -v cargo-deb > /dev/null; then \
+ $(CARGO) install --quiet cargo-deb; \
+ fi
+
+.PHONY: install-packaging-rpm
+install-packaging-rpm:
+ $Q if ! command -v cargo-generate-rpm > /dev/null; then \
+ $(CARGO) install --quiet cargo-generate-rpm; \
+ fi
+
+## Installs tools needed for building distributable packages
+.PHONY: install-packaging-tools
+install-packaging-tools:
+ $Q $(CARGO) install --quiet cargo-deb cargo-generate-rpm
+
+target/dist:
+ $Q mkdir -p $@
+
+## Builds all packages for all targets
+.PHONY: all-packages
+all-packages: deb-packages rpm-packages gz-packages
+
+target/dist/SHA256SUMS: target/dist
+ $Q cd target/dist && $(CHECKSUM) * > SHA256SUMS
+
+.PHONY: checksums
+checksums: target/dist/SHA256SUMS ## Generates checksums for all packages
+
+################################################################################
+### Debian Packages
+################################################################################
+
+to_debian_arch = $(shell echo $(1) | \
+ $(SED) -e 's/x86_64/amd64/' -e 's/aarch64/arm64/' -e 's/armv7/armhf/')
+DEBIAN_PACKAGE_TARGETS := \
+ $(foreach t, $(TARGETS), target/$(t)/debian/$(PACKAGE_NAME)_$(VERSION)_$(call to_debian_arch, $(firstword $(subst -, , $(t)))).deb)
+
+.ONESHELL: $(DEBIAN_PACKAGE_TARGETS)
+.NOTPARALLEL: $(DEBIAN_PACKAGE_TARGETS)
+$(DEBIAN_PACKAGE_TARGETS): $(TARGETS) target/man/$(OUTPUT_BINARY).1.gz target/dist
+ $Q TARGET="$(word 2, $(subst /, , $(dir $@)))"
+ # Skip building debs for musl targets
+ if echo "$(@)" | $(GREP) -q 'musl\|apple'; then \
+ exit 0
+ fi
+ if [ ! -f "$(CURDIR)/$(@)" ]; then
+ if [ -d "$(CURDIR)/target/release" ]; then \
+ echo "$(M) removing existing release directory: $(CURDIR)/target/release"
+ rm -rf "$(CURDIR)/target/release"
+ fi
+ echo "$(M) copying target architecture [$${TARGET}] build to target/release directory"
+ cp -r "$(CURDIR)/target/$${TARGET}/release" "$(CURDIR)/target/release"
+ echo "$(M) building debian package for target [$${TARGET}]: $(@)"
+ $(CARGO) deb --package unitctl --no-build --target "$${TARGET}" --output "$(CURDIR)/$(@)"
+ ln -f "$(CURDIR)/$(@)" "$(CURDIR)/target/dist/"
+ fi
+
+## Creates a debian package for the current platform
+.PHONY: deb-packages
+deb-packages: install-packaging-deb $(TARGETS) manpage $(DEBIAN_PACKAGE_TARGETS)
+
+################################################################################
+### RPM Packages
+################################################################################
+
+RPM_PACKAGE_TARGETS := $(foreach t, $(TARGETS), target/$(t)/generate-rpm/$(PACKAGE_NAME)_$(VERSION)_$(firstword $(subst -, , $(t))).rpm)
+
+.ONESHELL: $(RPM_PACKAGE_TARGETS)
+.NOTPARALLEL: $(RPM_PACKAGE_TARGETS)
+$(RPM_PACKAGE_TARGETS): $(TARGETS) target/man/$(OUTPUT_BINARY).1.gz target/dist
+ $Q TARGET="$(word 2, $(subst /, , $(dir $@)))"
+ ARCH="$(firstword $(subst -, , $(word 2, $(subst /, , $(dir $@)))))"
+ # Skip building rpms for musl targets
+ if echo "$(@)" | $(GREP) -q 'musl\|apple'; then \
+ exit 0
+ fi
+ if [ ! -f "$(CURDIR)/$(@)" ]; then
+ if [ -d "$(CURDIR)/target/release" ]; then \
+ echo "$(M) removing existing release directory: $(CURDIR)/target/release"
+ rm -rf "$(CURDIR)/target/release"
+ fi
+ echo "$(M) copying target architecture [$${ARCH}] build to target/release directory"
+ cp -r "$(CURDIR)/target/$${TARGET}/release" "$(CURDIR)/target/release"
+ echo "$(M) building rpm package: $(@)"
+ $(CARGO) generate-rpm --package unitctl --arch "$${ARCH}" --target "$${TARGET}" --output "$(CURDIR)/$(@)"
+ rm -rf "$(CURDIR)/target/release"
+ ln -f "$(CURDIR)/$(@)" "$(CURDIR)/target/dist/"
+ fi
+
+## Creates a rpm package for the current platform
+.PHONY: rpm-packages
+rpm-packages: install-packaging-rpm $(TARGETS) manpage $(RPM_PACKAGE_TARGETS)
+
+################################################################################
+### Homebrew Packages
+################################################################################
+
+## Modifies the homebrew formula to point to the latest release
+.PHONY: homebrew-packages
+.ONESHELL: homebrew-packages
+homebrew-packages: target/dist/SHA256SUMS
+ifdef NEW_VERSION
+ VERSION=$(NEW_VERSION)
+endif
+ $Q \
+ VERSION="$(VERSION)" \
+ PACKAGE_NAME="$(PACKAGE_NAME)" \
+ SRC_REPO="$(SRC_REPO)" \
+ AARCH64_UNKNOWN_LINUX_GNU_SHA256="$$($(GREP) $(PACKAGE_NAME)_v$(VERSION)_aarch64-unknown-linux-gnu.tar.gz $(CURDIR)/target/dist/SHA256SUMS | cut -d ' ' -f 1)" \
+ X86_64_UNKNOWN_LINUX_GNU_SHA256="$$($(GREP) $(PACKAGE_NAME)_v$(VERSION)_x86_64-unknown-linux-gnu.tar.gz $(CURDIR)/target/dist/SHA256SUMS | cut -d ' ' -f 1)" \
+ X86_64_APPLE_DARWIN_SHA256="$$($(GREP) $(PACKAGE_NAME)_v$(VERSION)_x86_64-apple-darwin.tar.gz $(CURDIR)/target/dist/SHA256SUMS | cut -d ' ' -f 1)" \
+ AARCH64_APPLE_DARWIN_SHA256="$$($(GREP) $(PACKAGE_NAME)_v$(VERSION)_aarch64-apple-darwin.tar.gz $(CURDIR)/target/dist/SHA256SUMS | cut -d ' ' -f 1)" \
+ envsubst < $(CURDIR)/pkg/brew/$(PACKAGE_NAME).rb.template > $(CURDIR)/pkg/brew/$(PACKAGE_NAME).rb
+
+
+################################################################################
+### Tarball Packages
+################################################################################
+
+GZ_PACKAGE_TARGETS = $(foreach t, $(TARGETS), target/gz/$(t)/$(PACKAGE_NAME)_$(VERSION)_$(firstword $(subst -, , $(t))).tar.gz)
+
+.ONESHELL: $(GZ_PACKAGE_TARGETS)
+$(GZ_PACKAGE_TARGETS): $(TARGETS) target/man/$(PACKAGE_NAME).1.gz target/dist
+ $Q mkdir -p "$(CURDIR)/target/gz"
+ TARGET="$(word 3, $(subst /, , $(dir $@)))"
+ PACKAGE="$(CURDIR)/target/gz/$(PACKAGE_NAME)_v$(VERSION)_$${TARGET}.tar.gz"
+ if [ ! -f "$${PACKAGE}}" ]; then
+ tar -cz -f $${PACKAGE} \
+ -C $(CURDIR)/target/man $(PACKAGE_NAME).1.gz \
+ -C $(CURDIR)/target/$${TARGET}/release $(PACKAGE_NAME) \
+ -C $(CURDIR) LICENSE.txt
+ ln -f "$${PACKAGE}" "$(CURDIR)/target/dist/"
+ fi
+
+## Creates a gzipped tarball all target platforms
+.PHONE: gz-packages
+gz-packages: $(GZ_PACKAGE_TARGETS)
diff --git a/tools/unitctl/build/release.mk b/tools/unitctl/build/release.mk
new file mode 100644
index 00000000..949e9301
--- /dev/null
+++ b/tools/unitctl/build/release.mk
@@ -0,0 +1,57 @@
+.ONESHELL: target/dist/release_notes.md
+target/dist/release_notes.md: target/dist target/dist/SHA256SUMS
+ $(info $(M) building release notes) @
+ $Q echo "# Release Notes" > target/dist/release_notes.md
+ echo '## SHA256 Checksums' >> target/dist/release_notes.md
+ echo '```' >> target/dist/release_notes.md
+ cat target/dist/SHA256SUMS >> target/dist/release_notes.md
+ echo '```' >> target/dist/release_notes.md
+
+.PHONY: release-notes
+release-notes: target/dist/release_notes.md ## Build release notes
+
+.PHONY: version
+version: ## Outputs the current version
+ $Q echo "Version: $(VERSION)"
+
+.PHONY: version-update
+.ONESHELL: version-update
+version-update: ## Prompts for a new version
+ $(info $(M) updating repository to new version) @
+ $Q echo " last committed version: $(LAST_VERSION)"
+ $Q echo " Cargo.toml file version : $(VERSION)"
+ read -p " Enter new version in the format (MAJOR.MINOR.PATCH): " version
+ $Q echo "$$version" | $(GREP) -qE '^[0-9]+\.[0-9]+\.[0-9]+-?.*$$' || \
+ (echo "invalid version identifier: $$version" && exit 1) && \
+ $(SED) -i "s/^version\s*=.*$$/version = \"$$version\"/" \
+ $(CURDIR)/unit-client-rs/Cargo.toml
+ $(SED) -i "s/^version\s*=.*$$/version = \"$$version\"/" \
+ $(CURDIR)/unitctl/Cargo.toml
+ $(SED) -i "s/^version\s*=.*$$/version = \"$$version\"/" \
+ $(CURDIR)/unit-openapi/Cargo.toml
+ $(SED) -i "s/^\s*\"packageVersion\":\s*.*$$/ \"packageVersion\": \"$$version\",/" \
+ $(CURDIR)/openapi-config.json
+ @ VERSION=$(shell $(GREP) -Po '^version\s+=\s+"\K.*?(?=")' \
+ $(CURDIR)/unitctl/Cargo.toml)
+
+.PHONY: version-release
+.ONESHELL: version-release
+version-release: ## Change from a pre-release to full release version
+ $Q echo "$(VERSION)" | $(GREP) -qE '^[0-9]+\.[0-9]+\.[0-9]+-beta$$' || \
+ (echo "invalid version identifier - must contain suffix -beta: $(VERSION)" && exit 1)
+ export NEW_VERSION="$(shell echo $(VERSION) | $(SED) -e 's/-beta$$//')"
+ $(SED) -i "s/^version\s*=.*$$/version = \"$$NEW_VERSION\"/" \
+ $(CURDIR)/unit-client-rs/Cargo.toml
+ $(SED) -i "s/^version\s*=.*$$/version = \"$$NEW_VERSION\"/" \
+ $(CURDIR)/unitctl/Cargo.toml
+ $(SED) -i "s/^version\s*=.*$$/version = \"$$NEW_VERSION\"/" \
+ $(CURDIR)/unit-openapi/Cargo.toml
+ $(SED) -i "s/^\s*\"packageVersion\":\s*.*$$/ \"packageVersion\": \"$$NEW_VERSION\",/" \
+ $(CURDIR)/openapi-config.json
+ @ VERSION=$(shell $(GREP) -Po '^version\s+=\s+"\K.*?(?=")' \
+ $(CURDIR)/unitctl/Cargo.toml)
+
+.PHONY: cargo-release
+cargo-release: ## Releases a new version to crates.io
+ $(info $(M) releasing version $(VERSION) to crates.io) @
+ $Q $(CARGO) publish