diff --git a/.travis.yml b/.travis.yml index 6db423e..c0cacff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,15 +1,42 @@ -language: go -go_import_path: github.com/cloudflare/goflow -go: - - 1.12.x - -script: - - GO111MODULE=on make - -notifications: - email: - recipients: - - louis@cloudflare.com - on_success: never - on_failure: change - +jobs: + include: + # Test + - stage: test + os: linux + language: go + env: + GO111MODULE=on + script: + - make test-race vet test + # Compile + - stage: compile + os: linux + language: go + env: + GO111MODULE=on + BUILDINFOSDET=-travis + before_install: + - sudo apt-get update + - sudo apt-get install -y rpm ruby ruby-dev + - sudo gem install fpm + script: + - GOOS=linux make build-goflow-light + - GOOS=linux make build-goflow + - GOOS=darwin make build-goflow + - GOOS=windows EXTENSION=.exe make build-goflow + - make package-deb-goflow package-rpm-goflow + deploy: + provider: releases + api_key: + secure: eg1OSNzXVSVsCx/n7xSJAtAw7NlgtnK57EyJmrwGgvcs5OUm5cvsnK3isuWwsAFanW6b69UoyyZDayIj72poiTVGo5705lL1sN39LxypmlkpmOFJaMggIdbPAN4fB6anRHp+MBGMvxGjeJP/97JKnPXcyK+QevqxRl2sMFRjLthTBManET7ahAhD5HqsdT/MeFORCymlJ+sIRXkLHrtBdiW/KXLLzsKn3C4/OPP3Z08ggqDix7I3zLaHW7nAvug3h5V5I84FiedEgO+w7McMjX8ri2Fz/sXNz3AaQIgBUxkmnIEvv4b9nFkd3HjIHRyS6iPpcdrqGXcMqW2SVHOJ668t140MLKrZyoCj4yi0UzqjY5F6iBCy5GSz8TBbz1Mo7TF6ieVeAaC0WZImO1aRHQeBNY/5NjvmwCXLDq7sUyxcHbfSa39/Pn6sD5yZkNsSEpTJ9AHxo2/os4NxQJ6l4nV/vseNDUnhcLf3irCBpsv1k1q6EgAO4kCdELSDMaYasZm2p4U9PDiGP1tyxWoglQKzma0sR1FGnOpUQB1Wl6ZWeW4IotHLb6QQRLfERPueWgENi2etDs88lLY1EuCamFoY19nWXROCiUEYFthK6csapgQw7y4hIcup2/gB0eNVoWbGB16MYQD2W47gj6LUGDSQMAjXffymugde71R46JQ= + file_glob: true + file: dist/* + skip_cleanup: true + on: + tags: true + repo: cloudflare/goflow + - dist: trusty + services: + - docker + script: + - make docker-goflow diff --git a/Dockerfile b/Dockerfile index e1718b8..bf7a03f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,12 @@ FROM golang:alpine as builder -ARG VERSION="" +ARG LDFLAGS="" RUN apk --update --no-cache add git build-base gcc COPY . /build WORKDIR /build -RUN go build -ldflags "-X main.version=${VERSION}" -o goflow cmd/goflow/goflow.go +RUN go build -ldflags "${LDFLAGS}" -o goflow cmd/goflow/goflow.go FROM alpine:latest ARG src_dir diff --git a/Makefile b/Makefile index 984b28f..5166b99 100644 --- a/Makefile +++ b/Makefile @@ -1,31 +1,28 @@ -IMAGE ?= cloudflare/goflow -VERSION ?= $(shell git describe --tags --always --dirty) -VERSION_DOCKER ?= $(shell git describe --tags --abbrev=0 --always --dirty) - +EXTENSION ?= +DIST_DIR ?= dist/ GOOS ?= linux ARCH ?= $(shell uname -m) +BUILDINFOSDET ?= -.PHONY: all -all: test-race vet test +DOCKER_REPO := cloudflare/ +GOFLOW_NAME := goflow +GOFLOW_VERSION := $(shell git describe --tags $(git rev-list --tags --max-count=1)) +VERSION_PKG := $(shell echo $(GOFLOW_VERSION) | sed 's/^v//g') +ARCH := x86_64 +LICENSE := BSD-3 +URL := https://github.com/cloudflare/goflow +DESCRIPTION := GoFlow: an sFlow/IPFIX/NetFlow v9/v5 collector to Kafka +BUILDINFOS := ($(shell date +%FT%T%z)$(BUILDINFOSDET)) +LDFLAGS := '-X main.version=$(GOFLOW_VERSION) -X main.buildinfos=$(BUILDINFOS)' -.PHONY: clean -clean: - rm -rf bin +OUTPUT_GOFLOW := $(DIST_DIR)goflow-$(GOFLOW_VERSION)-$(GOOS)-$(ARCH)$(EXTENSION) -.PHONY: build -build: - @echo compiling code - mkdir bin - GOOS=$(GOOS) go build -ldflags '-X main.version=$(VERSION)' -o bin/goflow-$(GOOS)-$(ARCH) cmd/goflow/goflow.go - GOOS=$(GOOS) go build -ldflags '-X main.version=$(VERSION)' -o bin/goflow-sflow-$(GOOS)-$(ARCH) cmd/csflow/csflow.go - GOOS=$(GOOS) go build -ldflags '-X main.version=$(VERSION)' -o bin/goflow-netflow-$(GOOS)-$(ARCH) cmd/cnetflow/cnetflow.go - GOOS=$(GOOS) go build -ldflags '-X main.version=$(VERSION)' -o bin/goflow-nflegacy-$(GOOS)-$(ARCH) cmd/cnflegacy/cnflegacy.go +OUTPUT_GOFLOW_LIGHT_SFLOW := $(DIST_DIR)goflow-sflow-$(GOFLOW_VERSION)-$(GOOS)-$(ARCH)$(EXTENSION) +OUTPUT_GOFLOW_LIGHT_NF := $(DIST_DIR)goflow-netflow-$(GOFLOW_VERSION)-$(GOOS)-$(ARCH)$(EXTENSION) +OUTPUT_GOFLOW_LIGHT_NFV5 := $(DIST_DIR)goflow-nflegacy-$(GOFLOW_VERSION)-$(GOOS)-$(ARCH)$(EXTENSION) - -.PHONY: container -container: - @echo build docker container - docker build --build-arg VERSION=$(VERSION) -t $(IMAGE):$(VERSION_DOCKER) . +.PHONY: all +all: test-race vet test .PHONY: proto proto: @@ -46,3 +43,50 @@ vet: test-race: @echo testing code for races go test -race ./... + +.PHONY: prepare +prepare: + mkdir -p $(DIST_DIR) + +.PHONY: clean +clean: + rm -rf $(DIST_DIR) + +.PHONY: build-goflow +build-goflow: prepare + go build -ldflags $(LDFLAGS) -o $(OUTPUT_GOFLOW) cmd/goflow/goflow.go + +.PHONY: build-goflow-light +build-goflow-light: prepare + go build -ldflags $(LDFLAGS) -o $(OUTPUT_GOFLOW_LIGHT_SFLOW) cmd/csflow/csflow.go + go build -ldflags $(LDFLAGS) -o $(OUTPUT_GOFLOW_LIGHT_NF) cmd/cnetflow/cnetflow.go + go build -ldflags $(LDFLAGS) -o $(OUTPUT_GOFLOW_LIGHT_NFV5) cmd/cnflegacy/cnflegacy.go + +.PHONY: docker-goflow +docker-goflow: + docker build -t $(DOCKER_REPO)$(GOFLOW_NAME):$(GOFLOW_VERSION) --build-arg LDFLAGS=$(LDFLAGS) -f Dockerfile . + +.PHONY: package-deb-goflow +package-deb-goflow: prepare + fpm -s dir -t deb -n $(GOFLOW_NAME) -v $(VERSION_PKG) \ + --description "$(DESCRIPTION)" \ + --url "$(URL)" \ + --architecture $(ARCH) \ + --license "$(LICENSE)" \ + --deb-no-default-config-files \ + --package $(DIST_DIR) \ + $(OUTPUT_GOFLOW)=/usr/bin/goflow \ + package/goflow.service=/lib/systemd/system/goflow.service \ + package/goflow.env=/etc/default/goflow + +.PHONY: package-rpm-goflow +package-rpm-goflow: prepare + fpm -s dir -t rpm -n $(GOFLOW_NAME) -v $(VERSION_PKG) \ + --description "$(DESCRIPTION)" \ + --url "$(URL)" \ + --architecture $(ARCH) \ + --license "$(LICENSE) "\ + --package $(DIST_DIR) \ + $(OUTPUT_GOFLOW)=/usr/bin/goflow \ + package/goflow.service=/lib/systemd/system/goflow.service \ + package/goflow.env=/etc/default/goflow diff --git a/cmd/cnetflow/cnetflow.go b/cmd/cnetflow/cnetflow.go index c2ef8cb..7ce8a48 100644 --- a/cmd/cnetflow/cnetflow.go +++ b/cmd/cnetflow/cnetflow.go @@ -15,7 +15,8 @@ import ( var ( version = "" - AppVersion = "GoFlow NetFlow " + version + buildinfos = "" + AppVersion = "GoFlow NetFlow " + version + " " + buildinfos Addr = flag.String("addr", "", "NetFlow/IPFIX listening address") Port = flag.Int("port", 2055, "NetFlow/IPFIX listening port") diff --git a/cmd/cnflegacy/cnflegacy.go b/cmd/cnflegacy/cnflegacy.go index 30f1d18..60ea52a 100644 --- a/cmd/cnflegacy/cnflegacy.go +++ b/cmd/cnflegacy/cnflegacy.go @@ -15,7 +15,8 @@ import ( var ( version = "" - AppVersion = "GoFlow NetFlowv5 " + version + buildinfos = "" + AppVersion = "GoFlow NetFlowV5 " + version + " " + buildinfos Addr = flag.String("addr", "", "NetFlow v5 listening address") Port = flag.Int("port", 2055, "NetFlow v5 listening port") diff --git a/cmd/csflow/csflow.go b/cmd/csflow/csflow.go index bde0ad0..2b29808 100644 --- a/cmd/csflow/csflow.go +++ b/cmd/csflow/csflow.go @@ -15,7 +15,8 @@ import ( var ( version = "" - AppVersion = "GoFlow sFlow " + version + buildinfos = "" + AppVersion = "GoFlow sFlow " + version + " " + buildinfos Addr = flag.String("addr", "", "sFlow listening address") Port = flag.Int("port", 6343, "sFlow listening port") diff --git a/cmd/goflow/goflow.go b/cmd/goflow/goflow.go index aba776c..0f7ece6 100644 --- a/cmd/goflow/goflow.go +++ b/cmd/goflow/goflow.go @@ -17,7 +17,8 @@ import ( var ( version = "" - AppVersion = "GoFlow " + version + buildinfos = "" + AppVersion = "GoFlow " + version + " " + buildinfos SFlowEnable = flag.Bool("sflow", true, "Enable sFlow") SFlowAddr = flag.String("sflow.addr", "", "sFlow listening address") diff --git a/docker-compose-pkg.yml b/docker-compose-pkg.yml new file mode 100644 index 0000000..ab0788d --- /dev/null +++ b/docker-compose-pkg.yml @@ -0,0 +1,11 @@ +version: '3' +services: + packager: + build: package + entrypoint: make + command: + - build-goflow + - package-deb-goflow + - package-rpm-goflow + volumes: + - ./:/work/ \ No newline at end of file diff --git a/package/Dockerfile b/package/Dockerfile new file mode 100644 index 0000000..1b42a3f --- /dev/null +++ b/package/Dockerfile @@ -0,0 +1,9 @@ +FROM ruby + +RUN apt-get update && \ + apt-get install -y git make rpm golang && \ + gem install fpm + +WORKDIR /work + +ENTRYPOINT [ "/bin/bash" ] \ No newline at end of file diff --git a/package/goflow.env b/package/goflow.env new file mode 100644 index 0000000..256fca3 --- /dev/null +++ b/package/goflow.env @@ -0,0 +1 @@ +GOFLOW_ARGS= \ No newline at end of file diff --git a/package/goflow.service b/package/goflow.service new file mode 100644 index 0000000..0d55bbc --- /dev/null +++ b/package/goflow.service @@ -0,0 +1,11 @@ +[Unit] +Description=GoFlow +After=network.target + +[Service] +Type=simple +EnvironmentFile=/etc/default/goflow +ExecStart=/usr/bin/goflow $GOFLOW_ARGS + +[Install] +WantedBy=multi-user.target \ No newline at end of file