edit and move trial code from server.go to trial.go and combine release build targets

This commit is contained in:
Maxton Huff 2021-02-09 15:39:32 -06:00
parent c967bc96f4
commit 7ae1599c12
3 changed files with 67 additions and 61 deletions

View file

@ -12,6 +12,7 @@ BUILD_TIME := $(shell date -u +%FT%T%z)
SHARD_WIDTH = 20
COMMIT := $(shell git describe --exact-match >/dev/null 2>&1 || git rev-parse --short HEAD)
LDFLAGS="-X github.com/pilosa/pilosa/v2.Version=$(VERSION) -X github.com/pilosa/pilosa/v2.BuildTime=$(BUILD_TIME) -X github.com/pilosa/pilosa/v2.Variant=$(VARIANT) -X github.com/pilosa/pilosa/v2.Commit=$(COMMIT) -X github.com/pilosa/pilosa/v2.LatticeCommit=$(LATTICE_COMMIT) -X github.com/pilosa/pilosa/v2.TrialDeadline=$(TRIAL_DEADLINE)"
TRIAL_STRING = $(if $(TRIAL_DEADLINE),"-trial-$(TRIAL_DEADLINE)","")
GO_VERSION=1.14.10
RELEASE ?= 0
RELEASE_ENABLED = $(subst 0,,$(RELEASE))
@ -98,19 +99,12 @@ cover-viz: cover
build:
go build -tags='$(BUILD_TAGS)' -ldflags $(LDFLAGS) $(FLAGS) ./cmd/pilosa
# Create a single release trial build under the build directory
release-build-trial:
$(MAKE) $(if $(DOCKER_BUILD),docker-)build FLAGS="-o build/pilosa-trial-$(VERSION_ID)/pilosa" RELEASE=1
cp NOTICE README.md build/pilosa-trial-$(VERSION_ID)
tar -cvz -C build -f build/pilosa-trial-$(VERSION_ID).tar.gz pilosa-trial-$(VERSION_ID)/
@echo Created release trial build: build/pilosa-trial-$(VERSION_ID).tar.gz
# Create a single release build under the build directory
release-build:
$(MAKE) $(if $(DOCKER_BUILD),docker-)build FLAGS="-o build/pilosa-$(VERSION_ID)/pilosa" RELEASE=1
cp NOTICE README.md LICENSE build/pilosa-$(VERSION_ID)
tar -cvz -C build -f build/pilosa-$(VERSION_ID).tar.gz pilosa-$(VERSION_ID)/
@echo Created release build: build/pilosa-$(VERSION_ID).tar.gz
$(MAKE) $(if $(DOCKER_BUILD),docker-)build FLAGS="-o build/pilosa$(TRIAL_STRING)-$(VERSION_ID)/pilosa" RELEASE=1
cp NOTICE README.md LICENSE build/pilosa$(TRIAL_STRING)-$(VERSION_ID)
tar -cvz -C build -f build/pilosa$(TRIAL_STRING)-$(VERSION_ID).tar.gz pilosa$(TRIAL_STRING)-$(VERSION_ID)/
@echo Created release build: build/pilosa$(TRIAL_STRING)-$(VERSION_ID).tar.gz
# Error out if there are untracked changes in Git
check-clean:

View file

@ -39,7 +39,6 @@ import (
"golang.org/x/sync/errgroup"
"github.com/beevik/ntp"
"github.com/pelletier/go-toml"
"github.com/pilosa/pilosa/v2"
"github.com/pilosa/pilosa/v2/boltdb"
@ -286,14 +285,7 @@ func (m *Command) SetupServer() error {
m.logger.Printf("%s", pilosa.VersionInfo())
if pilosa.TrialDeadline != "" {
layout := "2006-01-02"
endTime, err := time.Parse(layout, pilosa.TrialDeadline)
if err != nil {
return errors.Wrap(err, "parsing curTime from make file")
}
go m.dailyCheck(endTime)
}
m.trialVersion()
// If the pilosa command line uses -tx to override the
// PILOSA_TXSRC env variable, then we must also correct
@ -488,47 +480,6 @@ func (m *Command) SetupServer() error {
return errors.Wrap(err, "new handler")
}
// dailyCheck runs in the background while a trial version of Molecula is being run, displaying daily reminders of the remaining days
func (m *Command) dailyCheck(end time.Time) {
ticker := time.NewTicker(5 * time.Second)
var err error
for range ticker.C {
cur, err := m.ntpServerTime()
if err != nil {
m.logger.Printf("reading ntp server time %v", err)
os.Exit(1)
}
m.logger.Printf("Current time remaining in trial: %v", end.Sub(cur))
if end.Sub(cur) <= 0 {
m.logger.Printf("Your free trial has ended")
os.Exit(0)
}
}
m.logger.Printf("reading ntp server time %v", err)
os.Exit(1)
}
// ntpServerTime attempts to reach ntp servers with delays between each attempt
func (m *Command) ntpServerTime() (time.Time, error) {
curTime, err := ntp.Time("0.beevik-ntp.pool.ntp.org")
errCount := 0
if err != nil {
for i := 0; i < 4; i++ {
curTime, err = ntp.Time("0.beevik-ntp.pool.ntp.org")
if err != nil {
errCount++
if errCount >= 4 {
return curTime, err
}
time.Sleep(100 * time.Millisecond)
} else {
break
}
}
}
return curTime, err
}
// setupNetworking sets up internode communication based on the configuration.
func (m *Command) setupNetworking() error {
if m.Config.Cluster.Disabled {

61
server/trial.go Normal file
View file

@ -0,0 +1,61 @@
package server
import (
"os"
"time"
"github.com/beevik/ntp"
"github.com/pilosa/pilosa/v2"
)
func (m *Command) trialVersion() {
if pilosa.TrialDeadline != "" {
endTime, err := time.Parse("2006-01-02", pilosa.TrialDeadline)
if err != nil {
m.logger.Printf("parsing curTime from make file: %v", err)
os.Exit(1)
}
go m.dailyCheck(endTime)
}
}
const hoursPerCheck = 24 * time.Hour
// dailyCheck runs in the background while a trial version of Molecula is being run, displaying daily reminders of the remaining days
func (m *Command) dailyCheck(endTime time.Time) {
ticker := time.NewTicker(hoursPerCheck)
startTime, err := m.ntpServerTime(4)
if err != nil {
m.logger.Printf("reading ntp server time %v", err)
os.Exit(1)
}
runDuration := endTime.Sub(startTime)
if runDuration <= 0 {
m.logger.Printf("Trial edition of Molecula has expired, exiting now!")
os.Exit(1)
}
for range ticker.C {
runningDuration := time.Since(startTime)
m.logger.Printf("Current time remaining in trial: %v", runDuration-runningDuration)
if runningDuration >= runDuration {
m.logger.Printf("Trial edition of Molecula has expired, exiting now!")
os.Exit(1)
}
}
}
const url = "0.beevik-ntp.pool.ntp.org"
const timeRequestDelay = 100 * time.Millisecond
// ntpServerTime attempts to reach ntp servers with delays between each attempt, returning the time value of the first connected server
func (m *Command) ntpServerTime(retries int) (time.Time, error) {
t, err := ntp.Time(url)
if err != nil && retries <= 0 {
return t, err
}
if err != nil {
time.Sleep(timeRequestDelay)
return m.ntpServerTime(retries - 1)
}
return t, nil
}