From 9552bb7bc7ae15ea65adde84e1a36943cc228022 Mon Sep 17 00:00:00 2001 From: Piper McCorkle Date: Mon, 7 Mar 2022 18:05:48 -0600 Subject: [PATCH] TUN-5853 Add "install" make target and build package manager info into executable --- .gitignore | 1 + Makefile | 30 +++++++++++++++++++++--------- cmd/cloudflared/updater/update.go | 11 ++++++++--- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index d3b17958..c258e58c 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ cscope.* /cloudflared.exe /cloudflared.msi /cloudflared-x86-64* +/cloudflared.1 /packaging .DS_Store *-session.log diff --git a/Makefile b/Makefile index a3fbcf7e..461384d4 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,9 @@ endif DATE := $(shell date -u '+%Y-%m-%d-%H%M UTC') VERSION_FLAGS := -X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)" +ifdef PACKAGE_MANAGER + VERSION_FLAGS := $(VERSION_FLAGS) -X "github.com/cloudflare/cloudflared/cmd/cloudflared/updater.BuiltForPackageManager=$(PACKAGE_MANAGER)" +endif LINK_FLAGS := ifeq ($(FIPS), true) @@ -37,10 +40,11 @@ ifneq ($(GO_BUILD_TAGS),) GO_BUILD_TAGS := -tags "$(GO_BUILD_TAGS)" endif -IMPORT_PATH := github.com/cloudflare/cloudflared -PACKAGE_DIR := $(CURDIR)/packaging -INSTALL_BINDIR := /usr/bin/ -MAN_DIR := /usr/share/man/man1/ +IMPORT_PATH := github.com/cloudflare/cloudflared +PACKAGE_DIR := $(CURDIR)/packaging +PREFIX := /usr +INSTALL_BINDIR := $(PREFIX)/bin/ +INSTALL_MANDIR := $(PREFIX)/share/man/man1/ LOCAL_ARCH ?= $(shell uname -m) ifneq ($(GOARCH),) @@ -141,12 +145,20 @@ publish-deb: cloudflared-deb publish-rpm: cloudflared-rpm $(call publish_package,rpm,yum) +cloudflared.1: cloudflared_man_template + cat cloudflared_man_template | sed -e 's/\$${VERSION}/$(VERSION)/; s/\$${DATE}/$(DATE)/' > cloudflared.1 + +install: cloudflared cloudflared.1 + mkdir -p $(DESTDIR)$(INSTALL_BINDIR) $(DESTDIR)$(INSTALL_MANDIR) + install -m755 cloudflared $(DESTDIR)$(INSTALL_BINDIR)/cloudflared + install -m644 cloudflared.1 $(DESTDIR)$(INSTALL_MANDIR)/cloudflared.1 + # When we build packages, the package name will be FIPS-aware. # But we keep the binary installed by it to be named "cloudflared" regardless. define build_package mkdir -p $(PACKAGE_DIR) cp cloudflared $(PACKAGE_DIR)/cloudflared - cat cloudflared_man_template | sed -e 's/\$${VERSION}/$(VERSION)/; s/\$${DATE}/$(DATE)/' > $(PACKAGE_DIR)/cloudflared.1 + cp cloudflared.1 $(PACKAGE_DIR)/cloudflared.1 fakeroot fpm -C $(PACKAGE_DIR) -s dir -t $(1) \ --description 'Cloudflare Tunnel daemon' \ --vendor 'Cloudflare' \ @@ -154,19 +166,19 @@ define build_package --url 'https://github.com/cloudflare/cloudflared' \ -m 'Cloudflare ' \ -a $(TARGET_ARCH) -v $(VERSION) -n $(DEB_PACKAGE_NAME) $(NIGHTLY_FLAGS) --after-install postinst.sh --after-remove postrm.sh \ - cloudflared=$(INSTALL_BINDIR) cloudflared.1=$(MAN_DIR) + cloudflared=$(INSTALL_BINDIR) cloudflared.1=$(INSTALL_MANDIR) endef .PHONY: cloudflared-deb -cloudflared-deb: cloudflared +cloudflared-deb: cloudflared cloudflared.1 $(call build_package,deb) .PHONY: cloudflared-rpm -cloudflared-rpm: cloudflared +cloudflared-rpm: cloudflared cloudflared.1 $(call build_package,rpm) .PHONY: cloudflared-pkg -cloudflared-pkg: cloudflared +cloudflared-pkg: cloudflared cloudflared.1 $(call build_package,osxpkg) .PHONY: cloudflared-msi diff --git a/cmd/cloudflared/updater/update.go b/cmd/cloudflared/updater/update.go index 23aa327a..5ad40dcb 100644 --- a/cmd/cloudflared/updater/update.go +++ b/cmd/cloudflared/updater/update.go @@ -30,7 +30,8 @@ const ( ) var ( - version string + version string + BuiltForPackageManager = "" ) // BinaryUpdated implements ExitCoder interface, the app will exit with status code 11 @@ -118,7 +119,11 @@ func Update(c *cli.Context) error { log := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog) if wasInstalledFromPackageManager() { - log.Error().Msg("cloudflared was installed by a package manager. Please update using the same method.") + packageManagerName := "a package manager" + if BuiltForPackageManager != "" { + packageManagerName = BuiltForPackageManager + } + log.Error().Msg(fmt.Sprintf("cloudflared was installed by %s. Please update using the same method.", packageManagerName)) return nil } @@ -281,7 +286,7 @@ func supportAutoUpdate(log *zerolog.Logger) bool { func wasInstalledFromPackageManager() bool { ok, _ := config.FileExists(filepath.Join(config.DefaultUnixConfigLocation, isManagedInstallFile)) - return ok + return len(BuiltForPackageManager) != 0 || ok } func isRunningFromTerminal() bool {