Skip to content

Commit

Permalink
remove version package and adjust internal dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Jun 4, 2017
1 parent a09f4bb commit 8cf5a6f
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 111 deletions.
31 changes: 12 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
MACKEREL_AGENT_NAME ?= "mackerel-agent"
MACKEREL_API_BASE ?= "https://mackerel.io"
MACKEREL_AGENT_VERSION ?= $(shell git describe --tags --abbrev=0 | sed 's/^v//' | sed 's/[-+].*$$//')
VERSION = $(shell git log --merges --oneline | perl -ne 'if(m/^.+Merge pull request \#[0-9]+ from .+\/bump-version-([0-9\.]+)/){print $$1;exit}')
CURRENT_REVISION = $(shell git rev-parse --short HEAD)
ARGS = "-conf=mackerel-agent.conf"
BUILD_OS_TARGETS = "linux darwin freebsd windows netbsd"
CURRENT_VERSION = $(shell git log --merges --oneline | perl -ne 'if(m/^.+Merge pull request \#[0-9]+ from .+\/bump-version-([0-9\.]+)/){print $$1;exit}')


BUILD_LDFLAGS = "\
-X github.com/mackerelio/mackerel-agent/version.GITCOMMIT=`git rev-parse --short HEAD` \
-X github.com/mackerelio/mackerel-agent/version.VERSION=$(CURRENT_VERSION) \
-X main.version=$(VERSION) \
-X main.gitcommit=$(CURRENT_REVISION) \
-X github.com/mackerelio/mackerel-agent/config.agentName=$(MACKEREL_AGENT_NAME) \
-X github.com/mackerelio/mackerel-agent/config.apibase=$(MACKEREL_API_BASE)"

check-variables:
echo "CURRENT_VERSION: ${CURRENT_VERSION}"
echo "MACKEREL_AGENT_NAME: ${MACKEREL_AGENT_NAME}"
echo "MACKEREL_AGENT_VERSION: ${MACKEREL_AGENT_VERSION}"
echo "MACKEREL_API_BASE: ${MACKEREL_API_BASE}"

all: clean test build

test: lint
Expand Down Expand Up @@ -67,19 +60,19 @@ rpm-v1: crossbuild-package
MACKEREL_AGENT_NAME=$(MACKEREL_AGENT_NAME) _tools/packaging/prepare-rpm-build.sh
docker run --rm -v "$(PWD)":/workspace -v "$(PWD)/rpmbuild":/rpmbuild astj/mackerel-rpm-builder:c5 \
--define "_sourcedir /workspace/packaging/rpm-build/src" --define "_builddir /workspace/build-linux-386" \
--define "_version ${CURRENT_VERSION}" --define "buildarch noarch" \
--define "_version ${VERSION}" --define "buildarch noarch" \
-bb packaging/rpm-build/$(MACKEREL_AGENT_NAME).spec
MACKEREL_AGENT_NAME=$(MACKEREL_AGENT_NAME) _tools/packaging/prepare-rpm-build.sh
docker run --rm -v "$(PWD)":/workspace -v "$(PWD)/rpmbuild":/rpmbuild astj/mackerel-rpm-builder:c5 \
--define "_sourcedir /workspace/packaging/rpm-build/src" --define "_builddir /workspace/build-linux-amd64" \
--define "_version ${CURRENT_VERSION}" --define "buildarch x86_64" \
--define "_version ${VERSION}" --define "buildarch x86_64" \
-bb packaging/rpm-build/$(MACKEREL_AGENT_NAME).spec

rpm-v2: crossbuild-package
BUILD_SYSTEMD=1 MACKEREL_AGENT_NAME=$(MACKEREL_AGENT_NAME) _tools/packaging/prepare-rpm-build.sh
docker run --rm -v "$(PWD)":/workspace -v "$(PWD)/rpmbuild":/rpmbuild astj/mackerel-rpm-builder:c7 \
--define "_sourcedir /workspace/packaging/rpm-build/src" --define "_builddir /workspace/build-linux-amd64" \
--define "_version ${CURRENT_VERSION}" --define "buildarch x86_64" \
--define "_version ${VERSION}" --define "buildarch x86_64" \
-bb packaging/rpm-build/$(MACKEREL_AGENT_NAME).spec

deb: deb-v1 deb-v2
Expand All @@ -97,19 +90,19 @@ rpm-kcps-v1: crossbuild-package-kcps
MACKEREL_AGENT_NAME=mackerel-agent-kcps _tools/packaging/prepare-rpm-build.sh
docker run --rm -v "$(PWD)":/workspace -v "$(PWD)/rpmbuild":/rpmbuild astj/mackerel-rpm-builder:c5 \
--define "_sourcedir /workspace/packaging/rpm-build/src" --define "_builddir /workspace/build-linux-386" \
--define "_version ${CURRENT_VERSION}" --define "buildarch noarch" \
--define "_version ${VERSION}" --define "buildarch noarch" \
-bb packaging/rpm-build/mackerel-agent-kcps.spec
MACKEREL_AGENT_NAME=mackerel-agent-kcps _tools/packaging/prepare-rpm-build.sh
docker run --rm -v "$(PWD)":/workspace -v "$(PWD)/rpmbuild":/rpmbuild astj/mackerel-rpm-builder:c5 \
--define "_sourcedir /workspace/packaging/rpm-build/src" --define "_builddir /workspace/build-linux-amd64" \
--define "_version ${CURRENT_VERSION}" --define "buildarch x86_64" \
--define "_version ${VERSION}" --define "buildarch x86_64" \
-bb packaging/rpm-build/mackerel-agent-kcps.spec

rpm-kcps-v2: crossbuild-package-kcps
BUILD_SYSTEMD=1 MACKEREL_AGENT_NAME=mackerel-agent-kcps _tools/packaging/prepare-rpm-build.sh
docker run --rm -v "$(PWD)":/workspace -v "$(PWD)/rpmbuild":/rpmbuild astj/mackerel-rpm-builder:c7 \
--define "_sourcedir /workspace/packaging/rpm-build/src" --define "_builddir /workspace/build-linux-amd64" \
--define "_version ${CURRENT_VERSION}" --define "buildarch x86_64" \
--define "_version ${VERSION}" --define "buildarch x86_64" \
-bb packaging/rpm-build/mackerel-agent-kcps.spec

deb-kcps: deb-kcps-v1 deb-kcps-v2
Expand All @@ -127,14 +120,14 @@ rpm-stage-v1: crossbuild-package-stage
MACKEREL_AGENT_NAME=mackerel-agent-stage _tools/packaging/prepare-rpm-build.sh
docker run --rm -v "$(PWD)":/workspace -v "$(PWD)/rpmbuild":/rpmbuild astj/mackerel-rpm-builder:c5 \
--define "_sourcedir /workspace/packaging/rpm-build/src" --define "_builddir /workspace/build-linux-386" \
--define "_version ${CURRENT_VERSION}" --define "buildarch noarch" \
--define "_version ${VERSION}" --define "buildarch noarch" \
-bb packaging/rpm-build/mackerel-agent-stage.spec

rpm-stage-v2: crossbuild-package-stage
BUILD_SYSTEMD=1 MACKEREL_AGENT_NAME=mackerel-agent-stage _tools/packaging/prepare-rpm-build.sh
docker run --rm -v "$(PWD)":/workspace -v "$(PWD)/rpmbuild":/rpmbuild astj/mackerel-rpm-builder:c7 \
--define "_sourcedir /workspace/packaging/rpm-build/src" --define "_builddir /workspace/build-linux-amd64" \
--define "_version ${CURRENT_VERSION}" --define "buildarch x86_64" \
--define "_version ${VERSION}" --define "buildarch x86_64" \
-bb packaging/rpm-build/mackerel-agent-stage.spec

deb-stage: deb-stage-v1 deb-stage-v2
Expand Down
2 changes: 1 addition & 1 deletion build-k.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ set VERSION=%VERSION:v=%

echo %VERSION%

go build -o build/mackerel-agent-kcps.exe -ldflags="-X github.com/mackerelio/mackerel-agent/version.GITCOMMIT=%COMMIT% -X github.com/mackerelio/mackerel-agent/version.VERSION=%VERSION% -X github.com/mackerelio/mackerel-agent/config.apibase=http://198.18.0.16 " github.com/mackerelio/mackerel-agent
go build -o build/mackerel-agent-kcps.exe -ldflags="-X main.gitcommit=%COMMIT% -X main.version=%VERSION% -X github.com/mackerelio/mackerel-agent/config.apibase=http://198.18.0.16 " github.com/mackerelio/mackerel-agent
8 changes: 2 additions & 6 deletions build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ echo on

FOR /F "usebackq" %%w IN (`git rev-parse --short HEAD`) DO SET COMMIT=%%w

FOR /F "usebackq" %%w IN (`git tag -l --sort=-version:refname "v*"`) DO (
IF NOT DEFINED VERSION (
SET VERSION=%%w
)
)
FOR /F "usebackq" %%w IN (`git describe --tags --abbrev^=0`) DO SET VERSION=%%w

set VERSION=%VERSION:v=%

echo %VERSION%

go build -o build/mackerel-agent.exe -ldflags="-X github.com/mackerelio/mackerel-agent/version.GITCOMMIT=%COMMIT% -X github.com/mackerelio/mackerel-agent/version.VERSION=%VERSION% " github.com/mackerelio/mackerel-agent
go build -o build/mackerel-agent.exe -ldflags="-X main.gitcommit=%COMMIT% -X main.version=%VERSION% " github.com/mackerelio/mackerel-agent
41 changes: 36 additions & 5 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ var metricsInterval = 60 * time.Second
var retryNum uint = 20
var retryInterval = 3 * time.Second

// AgentMeta contains meta information about mackerel-agent
type AgentMeta struct {
Version string
Revision string
}

// prepareHost collects specs of the host and sends them to Mackerel server.
// A unique host-id is returned by the server if one is not specified.
func prepareHost(conf *config.Config, api *mackerel.API) (*mackerel.Host, error) {
Expand Down Expand Up @@ -149,6 +155,7 @@ type App struct {
Host *mackerel.Host
API *mackerel.API
CustomIdentifierHosts map[string]*mackerel.Host
AgentMeta *AgentMeta
}

type postValue struct {
Expand Down Expand Up @@ -510,6 +517,13 @@ func collectHostSpecs() (string, map[string]interface{}, []spec.NetInterface, st
return hostname, meta, interfaces, customIdentifier, nil
}

func fillUpSpecMeta(meta map[string]interface{}, ver, rev string) map[string]interface{} {
meta["agent-version"] = ver
meta["agent-revision"] = rev
meta["agent-name"] = buildUA(ver, rev)
return meta
}

// UpdateHostSpecs updates the host information that is already registered on Mackerel.
func (app *App) UpdateHostSpecs() {
logger.Debugf("Updating host specs...")
Expand All @@ -519,6 +533,7 @@ func (app *App) UpdateHostSpecs() {
logger.Errorf("While collecting host specs: %s", err)
return
}
meta = fillUpSpecMeta(meta, app.AgentMeta.Version, app.AgentMeta.Revision)

err = app.API.UpdateHost(app.Host.ID, mackerel.HostSpec{
Name: hostname,
Expand All @@ -537,10 +552,24 @@ func (app *App) UpdateHostSpecs() {
}
}

func buildUA(ver, rev string) string {
return fmt.Sprintf("mackerel-agent/%s (Revision %s)", ver, rev)
}

// NewMackerelClient returns Mackerel API client for mackerel-agent
func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool) (*mackerel.API, error) {
api, err := mackerel.NewAPI(apibase, apikey, verbose)
if err != nil {
return nil, err
}
api.UA = buildUA(ver, rev)
return api, nil
}

// Prepare sets up API and registers the host data to the Mackerel server.
// Use returned values to call Run().
func Prepare(conf *config.Config) (*App, error) {
api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, conf.Verbose)
func Prepare(conf *config.Config, ameta *AgentMeta) (*App, error) {
api, err := NewMackerelClient(conf.Apibase, conf.Apikey, ameta.Version, ameta.Revision, conf.Verbose)
if err != nil {
return nil, fmt.Errorf("failed to prepare an api: %s", err.Error())
}
Expand All @@ -556,12 +585,13 @@ func Prepare(conf *config.Config) (*App, error) {
Host: host,
API: api,
CustomIdentifierHosts: prepareCustomIdentiferHosts(conf, api),
AgentMeta: ameta,
}, nil
}

// RunOnce collects specs and metrics, then output them to stdout.
func RunOnce(conf *config.Config) error {
graphdefs, hostSpec, metrics, err := runOncePayload(conf)
func RunOnce(conf *config.Config, ameta *AgentMeta) error {
graphdefs, hostSpec, metrics, err := runOncePayload(conf, ameta)
if err != nil {
return err
}
Expand All @@ -578,12 +608,13 @@ func RunOnce(conf *config.Config) error {
return nil
}

func runOncePayload(conf *config.Config) ([]mackerel.CreateGraphDefsPayload, *mackerel.HostSpec, *agent.MetricsResult, error) {
func runOncePayload(conf *config.Config, ameta *AgentMeta) ([]mackerel.CreateGraphDefsPayload, *mackerel.HostSpec, *agent.MetricsResult, error) {
hostname, meta, interfaces, customIdentifier, err := collectHostSpecs()
if err != nil {
logger.Errorf("While collecting host specs: %s", err)
return nil, nil, nil, err
}
meta = fillUpSpecMeta(meta, ameta.Version, ameta.Revision)

origInterval := metricsInterval
metricsInterval = 1 * time.Second
Expand Down
19 changes: 10 additions & 9 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestPrepareWithCreate(t *testing.T) {
}
}

c, _ := Prepare(&conf)
c, _ := Prepare(&conf, &AgentMeta{})
api := c.API
host := c.Host

Expand Down Expand Up @@ -140,7 +140,7 @@ func TestPrepareWithCreateWithFail(t *testing.T) {
retryNum = origRetryNum
}()

_, err := Prepare(&conf)
_, err := Prepare(&conf, &AgentMeta{})

if err == nil {
t.Errorf("error should be occurred")
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestPrepareWithUpdate(t *testing.T) {
}
}

c, _ := Prepare(&conf)
c, _ := Prepare(&conf, &AgentMeta{})
api := c.API
host := c.Host

Expand Down Expand Up @@ -314,15 +314,16 @@ func TestLoop(t *testing.T) {

termCh := make(chan struct{})
exitCh := make(chan error)
c := &App{
Agent: ag,
Config: &conf,
API: api,
Host: host,
app := &App{
Agent: ag,
Config: &conf,
API: api,
Host: host,
AgentMeta: &AgentMeta{},
}
// Start looping!
go func() {
exitCh <- loop(c, termCh)
exitCh <- loop(app, termCh)
}()

<-done
Expand Down
4 changes: 2 additions & 2 deletions command/command_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestRunOnce(t *testing.T) {
},
},
}
err := RunOnce(conf)
err := RunOnce(conf, &AgentMeta{})
if err != nil {
t.Errorf("RunOnce() should be nomal exit: %s", err)
}
Expand All @@ -61,7 +61,7 @@ func TestRunOncePayload(t *testing.T) {
},
},
}
graphdefs, hostSpec, metrics, err := runOncePayload(conf)
graphdefs, hostSpec, metrics, err := runOncePayload(conf, &AgentMeta{})
if err != nil {
t.Errorf("RunOnce() should be nomal exit: %s", err)
}
Expand Down
11 changes: 6 additions & 5 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import (
"github.com/Songmu/retry"
"github.com/mackerelio/mackerel-agent/command"
"github.com/mackerelio/mackerel-agent/config"
"github.com/mackerelio/mackerel-agent/mackerel"
"github.com/mackerelio/mackerel-agent/pidfile"
"github.com/mackerelio/mackerel-agent/supervisor"
"github.com/mackerelio/mackerel-agent/version"
)

/* +main - mackerel-agent
Expand Down Expand Up @@ -91,7 +89,7 @@ display the version of mackerel-agent
*/
func doVersion(_ *flag.FlagSet, _ []string) error {
fmt.Printf("mackerel-agent version %s (rev %s) [%s %s %s] \n",
version.VERSION, version.GITCOMMIT, runtime.GOOS, runtime.GOARCH, runtime.Version())
version, gitcommit, runtime.GOOS, runtime.GOARCH, runtime.Version())
return nil
}

Expand Down Expand Up @@ -127,7 +125,7 @@ func doRetire(fs *flag.FlagSet, argv []string) error {
return fmt.Errorf("hostID file is not found")
}

api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, conf.Verbose)
api, err := command.NewMackerelClient(conf.Apibase, conf.Apikey, version, gitcommit, conf.Verbose)
if err != nil {
return fmt.Errorf("faild to create api client: %s", err)
}
Expand Down Expand Up @@ -164,6 +162,9 @@ func doOnce(fs *flag.FlagSet, argv []string) error {
logger.Warningf("failed to load config (but `once` must not required conf): %s", err)
conf = &config.Config{}
}
command.RunOnce(conf)
command.RunOnce(conf, &command.AgentMeta{
Version: version,
Revision: gitcommit,
})
return nil
}
15 changes: 10 additions & 5 deletions mackerel/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/mackerelio/mackerel-agent/logging"
"github.com/mackerelio/mackerel-agent/version"
)

var logger = logging.GetLogger("api")
Expand All @@ -28,6 +27,7 @@ type API struct {
BaseURL *url.URL
APIKey string
Verbose bool
UA string
}

// Error represents API error
Expand Down Expand Up @@ -63,7 +63,7 @@ func NewAPI(rawurl string, apiKey string, verbose bool) (*API, error) {
if err != nil {
return nil, err
}
return &API{u, apiKey, verbose}, nil
return &API{BaseURL: u, APIKey: apiKey, Verbose: verbose}, nil
}

func (api *API) urlFor(path string, query string) *url.URL {
Expand All @@ -73,13 +73,18 @@ func (api *API) urlFor(path string, query string) *url.URL {
return newURL
}

func (api *API) getUA() string {
if api.UA != "" {
return api.UA
}
return "mackerel-agent/0.0.0"
}

var apiRequestTimeout = 30 * time.Second

func (api *API) do(req *http.Request) (resp *http.Response, err error) {
req.Header.Add("X-Api-Key", api.APIKey)
req.Header.Add("X-Agent-Version", version.VERSION)
req.Header.Add("X-Revision", version.GITCOMMIT)
req.Header.Set("User-Agent", version.UserAgent())
req.Header.Set("User-Agent", api.getUA())

if api.Verbose {
dump, err := httputil.DumpRequest(req, true)
Expand Down
Loading

0 comments on commit 8cf5a6f

Please sign in to comment.