diff --git a/.dockerignore b/.dockerignore index 95a3db7..9e01640 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,4 @@ -.git +/.idea *.iml -.idea -build/gopath -.gradle -vendor \ No newline at end of file +/out +/var diff --git a/.gitignore b/.gitignore index a7a0c2c..88a8984 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ +/.idea *.iml -.idea -build -.gradle -vendor \ No newline at end of file +/out +/dist +/var diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..cba698f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,42 @@ +language: go +go: + - 1.12.3 +install: skip +os: + - linux +services: + - docker +env: + global: + - GO111MODULE=on + - CGO_ENABLED=0 + - GOOS=linux + - GOARCH=amd64 +cache: + directories: + - $HOME/.cache/go-build + - $HOME/gopath/pkg/mod +script: skip + +jobs: + include: + - stage: test + name: Run Tests + script: + - go run ./build test + - stage: release + name: Release + if: tag =~ ^v\d+\.\d+\.\d+|snapshot-.+$ + before_script: + - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin + script: + - go run ./build build-and-deploy + deploy: + provider: releases + api_key: "$GITHUB_DEPLOY_TOKEN" + file_glob: true + file: dist/* + skip_cleanup: true + name: $TRAVIS_TAG + on: + tags: true diff --git a/Dockerfile b/Dockerfile index 29d02e8..db15446 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,7 @@ FROM scratch MAINTAINER contact@echocat.org -COPY build/out/site24x7_exporter-linux-amd64 /usr/bin/site24x7_exporter +ENV PATH=/usr/bin +COPY dist/site24x7_exporter-linux-amd64 /usr/bin/site24x7_exporter ENTRYPOINT ["/usr/bin/site24x7_exporter"] diff --git a/README.md b/README.md index 38f4bd9..1e0bc6f 100644 --- a/README.md +++ b/README.md @@ -123,51 +123,6 @@ docker run -p9112:9112 -v/etc/certs:/etc/certs:ro echocat/site24x7_exporter \ | ``9`` | Discovery | | ``10`` | Discovery Error | -## Build it - -### Precondition - -For building site24x7_exporter there is only: - -1. a compatible operating system (Linux, Windows or Mac OS X) -2. and a working [Java 8](http://www.oracle.com/technetwork/java/javase/downloads/index.html) installation required. - -There is no need for a working and installed Go installation (or anything else). The build system will download every dependency and build it if necessary. - -> **Hint:** The Go runtime build by the build system will be placed under ``~/.go/sdk``. - -### Run build process - -On Linux and Mac OS X: -```bash -# Build binaries (includes test) -./gradlew build - -# Run tests (but do not build binaries) -./gradlew test - -# Build binaries and release it on GitHub -# Environment variable GITHUB_TOKEN is required -./gradlew build githubRelease -``` - -On Windows: -```bash -# Build binaries (includes test) -gradlew build - -# Run tests (but do not build binaries) -gradlew test - -# Build binaries and release it on GitHub -# Environment variable GITHUB_TOKEN is required -gradlew build githubRelease -``` - -### Build artifacts - -* Compiled and lined binaries can be found under ``./build/out/site24x7_exporter-*`` - ## Contributing site24x7_exporter is an open source project of [echocat](https://echocat.org). diff --git a/build.gradle b/build.gradle deleted file mode 100644 index 3cfeca5..0000000 --- a/build.gradle +++ /dev/null @@ -1,45 +0,0 @@ -plugins { - id "org.echocat.golang" version "0.1.5" - id "co.riiid.gradle" version "0.4.2" -} - -final name = 'site24x7_exporter' -group 'github.com/echocat/site24x7_exporter' -version '0.1.6' - -dependencies { - build 'github.com/prometheus/client_golang' - build 'github.com/beorn7/perks' - build 'github.com/golang/protobuf' - build 'github.com/prometheus/client_model' - build 'github.com/prometheus/common' - build 'github.com/prometheus/procfs' - build 'github.com/matttproud/golang_protobuf_extensions' -} -golang { - platforms = System.getProperty("platforms", "linux-386,linux-amd64,windows-386,windows-amd64,darwin-amd64") - build { - useTemporaryGopath = true - definitions = [ - "main.name" : name, - "main.version" : version, - "main.group" : group, - ] - } -} - -github { - owner = 'echocat' - repo = name - token = "${System.getenv('GITHUB_TOKEN')}" - tagName = "v${version}" - targetCommitish = 'master' - name = version - assets = [ - 'build/out/site24x7_exporter-darwin-amd64', - 'build/out/site24x7_exporter-linux-386', - 'build/out/site24x7_exporter-linux-amd64', - 'build/out/site24x7_exporter-windows-386.exe', - 'build/out/site24x7_exporter-windows-amd64.exe', - ] -} \ No newline at end of file diff --git a/build/build.go b/build/build.go new file mode 100644 index 0000000..7f8d464 --- /dev/null +++ b/build/build.go @@ -0,0 +1,78 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" +) + +func build(branch, commit string) { + buildBinaries(branch, commit) + + if withDocker { + buildDocker(branch, dv, false) + tagDocker(branch, dv, false) + } +} + +func buildBinaries(branch, commit string) { + for _, t := range targets { + buildBinary(branch, commit, t, false) + } +} + +func buildBinary(branch, commit string, t target, forTesting bool) { + ldFlags := buildLdFlagsFor(branch, commit, forTesting) + outputName := t.outputName() + must(os.MkdirAll(filepath.Dir(outputName), 0755)) + executeTo(func(cmd *exec.Cmd) { + cmd.Env = append(os.Environ(), "GOOS="+t.os, "GOARCH="+t.arch) + }, os.Stderr, os.Stdout, "go", "build", "-ldflags", ldFlags, "-o", outputName, ".") +} + +func buildLdFlagsFor(branch, commit string, forTesting bool) string { + testPrefix := "" + testSuffix := "" + if forTesting { + testPrefix = "TEST" + testSuffix = "TEST" + } + return fmt.Sprintf("-X main.version=%s%s%s", testPrefix, branch, testSuffix) + + fmt.Sprintf(" -X main.revision=%s%s%s", testPrefix, commit, testSuffix) + + fmt.Sprintf(" -X main.compiled=%s", startTime.Format("2006-01-02T15:04:05Z")) +} + +func buildDocker(branch string, v dockerVariant, forTesting bool) { + version := branch + if forTesting { + version = "TEST" + version + "TEST" + } + execute("docker", "build", "-t", v.imageName(version), "-f", v.dockerFile, "--build-arg", "image="+imagePrefix, "--build-arg", "version="+version, ".") +} + +func tagDocker(branch string, v dockerVariant, forTesting bool) { + version := branch + if forTesting { + version = "TEST" + version + "TEST" + } + executeForVersionParts(version, func(tagSuffix string) { + tagDockerWith(version, v, v.imageName(tagSuffix)) + }) + if latestVersionPattern != nil && latestVersionPattern.MatchString(version) { + tagDockerWith(version, v, v.baseImageName()) + } + if v.main { + tagDockerWith(version, v, imagePrefix+":"+version) + executeForVersionParts(version, func(tagSuffix string) { + tagDockerWith(version, v, imagePrefix+":"+tagSuffix) + }) + if latestVersionPattern != nil && latestVersionPattern.MatchString(version) { + tagDockerWith(version, v, imagePrefix+":latest") + } + } +} + +func tagDockerWith(branch string, v dockerVariant, tag string) { + execute("docker", "tag", v.imageName(branch), tag) +} diff --git a/build/deploy.go b/build/deploy.go new file mode 100644 index 0000000..2fcfb66 --- /dev/null +++ b/build/deploy.go @@ -0,0 +1,28 @@ +package main + +func deploy(branch string) { + deployDocker(branch, dv) +} + +func deployDocker(branch string, v dockerVariant) { + deployDockerTag(v.imageName(branch)) + executeForVersionParts(branch, func(tagSuffix string) { + deployDockerTag(v.imageName(tagSuffix)) + }) + if latestVersionPattern != nil && latestVersionPattern.MatchString(branch) { + deployDockerTag(v.baseImageName()) + } + if v.main { + deployDockerTag(imagePrefix + ":" + branch) + executeForVersionParts(branch, func(tagSuffix string) { + deployDockerTag(imagePrefix + ":" + tagSuffix) + }) + if latestVersionPattern != nil && latestVersionPattern.MatchString(branch) { + deployDockerTag(imagePrefix + ":latest") + } + } +} + +func deployDockerTag(tag string) { + execute("docker", "push", tag) +} diff --git a/build/main.go b/build/main.go new file mode 100644 index 0000000..dfdfad8 --- /dev/null +++ b/build/main.go @@ -0,0 +1,60 @@ +package main + +import ( + "flag" + "fmt" + "os" + "regexp" +) + +var ( + branch = "snapshot" + commit = "unknown" + withDocker = true + plainLatestVersionPattern string + latestVersionPattern *regexp.Regexp +) + +func init() { + flag.StringVar(&branch, "branch", os.Getenv("TRAVIS_BRANCH"), "something like either main, v1.2.3 or snapshot-feature-foo") + flag.StringVar(&commit, "commit", os.Getenv("TRAVIS_COMMIT"), "something like 463e189796d5e96a7b605ab51985458faf8fd0d4") + flag.BoolVar(&withDocker, "withDocker", true, "enables docker tests and builds") + flag.StringVar(&plainLatestVersionPattern, "latestVersionPattern", os.Getenv("LATEST_VERSION_PATTERN"), "everything what matches here will be a latest tag") +} + +func main() { + flag.Parse() + if branch == "" { + branch = "development" + } + if commit == "" { + commit = "development" + } + if compiled, err := regexp.Compile(plainLatestVersionPattern); err != nil { + panic(err) + } else { + latestVersionPattern = compiled + } + fArgs := flag.Args() + if len(fArgs) != 1 { + usage() + } + switch fArgs[0] { + case "build": + build(branch, commit) + case "test": + test(branch, commit) + case "deploy": + deploy(branch) + case "build-and-deploy": + build(branch, commit) + deploy(branch) + default: + usage() + } +} + +func usage() { + _, _ = fmt.Fprintf(os.Stderr, "Usage: %s [flags] ", os.Args[0]) + os.Exit(1) +} diff --git a/build/model.go b/build/model.go new file mode 100644 index 0000000..a5f46fb --- /dev/null +++ b/build/model.go @@ -0,0 +1,60 @@ +package main + +import ( + "fmt" + "path/filepath" + "runtime" +) + +const imagePrefix = "echocat/site24x7_exporter" + +var ( + dv = dockerVariant{ + dockerFile: "Dockerfile", + main: true, + } + + currentTarget = target{os: runtime.GOOS, arch: runtime.GOARCH} + linuxAmd64 = target{os: "linux", arch: "amd64"} + targets = []target{ + {os: "darwin", arch: "amd64"}, + {os: "darwin", arch: "386"}, + linuxAmd64, + {os: "linux", arch: "386"}, + {os: "windows", arch: "amd64"}, + {os: "windows", arch: "386"}, + } +) + +type dockerVariant struct { + dockerFile string + main bool +} + +func (instance dockerVariant) baseImageName() string { + return imagePrefix + ":latest" +} + +func (instance dockerVariant) imageName(branch string) string { + result := imagePrefix + ":" + branch + if branch == "" { + result += "latest" + } + return result +} + +type target struct { + os string + arch string +} + +func (instance target) outputName() string { + return filepath.Join("dist", fmt.Sprintf("site24x7_exporter-%s-%s%s", instance.os, instance.arch, instance.ext())) +} + +func (instance target) ext() string { + if instance.os == "windows" { + return ".exe" + } + return "" +} diff --git a/build/targets.go b/build/targets.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/build/targets.go @@ -0,0 +1 @@ +package main diff --git a/build/test.go b/build/test.go new file mode 100644 index 0000000..d0e8b17 --- /dev/null +++ b/build/test.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + "strings" +) + +func test(branch, commit string) { + testGoCode(currentTarget) + + buildBinary(branch, commit, currentTarget, true) + testBinary(branch, commit, currentTarget) + + if withDocker { + buildBinary(branch, commit, linuxAmd64, true) + buildDocker(branch, dv, true) + testDocker(branch, commit, dv) + tagDocker(branch, dv, true) + } +} + +func testGoCode(t target) { + executeTo(func(cmd *exec.Cmd) { + cmd.Env = append(os.Environ(), "GOOS="+t.os, "GOARCH="+t.arch) + }, os.Stderr, os.Stdout, "go", "test", "-v", "./...") +} + +func testBinary(branch, commit string, t target) { + testBinaryByExpectingResponse(t, `site24x7_exporter (version: TEST`+branch+`TEST, revision: TEST`+commit+`TEST, build: `, t.outputName(), "-help") +} + +func testBinaryByExpectingResponse(t target, expectedPartOfResponse string, args ...string) { + cmd := append([]string{t.outputName()}, args...) + response := executeAndRecord(args...) + if !strings.Contains(response, expectedPartOfResponse) { + panic(fmt.Sprintf("Command failed [%s]\nResponse should contain: %s\nBut response was: %s", + quoteAllIfNeeded(cmd...), expectedPartOfResponse, response)) + } +} + +func testDocker(branch, commit string, v dockerVariant) { + testDockerByExpectingResponse(branch, v, `site24x7_exporter (version: TEST`+branch+`TEST, revision: TEST`+commit+`TEST, build: `, "-help") +} + +func testDockerByExpectingResponse(branch string, v dockerVariant, expectedPartOfResponse string, command ...string) { + call := append([]string{"docker", "run", "--rm", v.imageName("TEST" + branch + "TEST")}, command...) + response := executeAndRecord(call...) + if !strings.Contains(response, expectedPartOfResponse) { + panic(fmt.Sprintf("Command failed [%s]\nResponse should contain: %s\nBut response was: %s", + quoteAllIfNeeded(call...), expectedPartOfResponse, response)) + } +} diff --git a/build/utils.go b/build/utils.go new file mode 100644 index 0000000..4178265 --- /dev/null +++ b/build/utils.go @@ -0,0 +1,101 @@ +package main + +import ( + "bytes" + "fmt" + "io" + "log" + "os" + "os/exec" + "regexp" + "strconv" + "strings" + "time" +) + +var ( + versionRegexp = regexp.MustCompile(`^v(\d+)\.(\d+)\.(\d+)$`) + startTime = time.Now() +) + +func must(err error) { + if err != nil { + panic(err) + } +} + +func execute(args ...string) { + executeTo(nil, os.Stderr, os.Stdout, args...) +} + +func executeAndRecord(args ...string) string { + buf := new(bytes.Buffer) + executeTo(nil, buf, buf, args...) + return buf.String() +} + +type cmdCustomizer func(*exec.Cmd) + +func executeTo(customizer cmdCustomizer, stderr, stdout io.Writer, args ...string) { + if len(args) <= 0 { + panic("no arguments provided") + } + log.Printf("Execute: %s", quoteAndJoin(args...)) + + cmd := exec.Command(args[0], args[1:]...) + cmd.Stderr = stderr + cmd.Stdout = stdout + if customizer != nil { + customizer(cmd) + } + + if err := cmd.Run(); err != nil { + msg := fmt.Sprintf("command failed [%s]: %v", strings.Join(args, " "), err) + if b, ok := stdout.(fmt.Stringer); ok { + msg += fmt.Sprintf("\nStdout: %s", b.String()) + } + if b, ok := stderr.(fmt.Stringer); ok && stderr != stdout { + msg += fmt.Sprintf("\nStderr: %s", b.String()) + } + panic(msg) + } +} + +func quoteIfNeeded(what string) string { + if strings.ContainsRune(what, '\t') || + strings.ContainsRune(what, '\n') || + strings.ContainsRune(what, ' ') || + strings.ContainsRune(what, '\xFF') || + strings.ContainsRune(what, '\u0100') || + strings.ContainsRune(what, '"') || + strings.ContainsRune(what, '\\') { + return strconv.Quote(what) + } + return what +} + +func quoteAllIfNeeded(in ...string) []string { + out := make([]string, len(in)) + for i, a := range in { + out[i] = quoteIfNeeded(a) + } + return out +} + +func quoteAndJoin(in ...string) string { + out := make([]string, len(in)) + for i, a := range in { + out[i] = quoteIfNeeded(a) + } + return strings.Join(out, " ") +} + +type versionPartAction func(versionPart string) + +func executeForVersionParts(version string, action versionPartAction) { + match := versionRegexp.FindStringSubmatch(version) + if match != nil { + action(fmt.Sprintf("v%s.%s", match[1], match[2])) + action(fmt.Sprintf("v%s", match[1])) + } +} diff --git a/circle.yml b/circle.yml deleted file mode 100644 index 53f1590..0000000 --- a/circle.yml +++ /dev/null @@ -1,37 +0,0 @@ -machine: - java: - version: oraclejdk8 - services: - - docker - environment: - DOCKER_USER: echocatbot - DOCKER_EMAIL: contact@echocat.org - DOCKER_TAG: echocat/site24x7_exporter - -dependencies: - override: - # Remove the gitconfig is required to bring jgit from gradle-golang-plugin to work. - - rm -f ~/.gitconfig - - bash ./gradlew --stacktrace --info -Dplatforms=linux-amd64 --console=plain build - - ls -l build/out - - docker info - - docker build -t $DOCKER_TAG . - -test: - override: - - echo OK. - -deployment: - devel: - branch: /.*/ - commands: - - docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS - - docker tag $DOCKER_TAG $DOCKER_TAG:devel-${CIRCLE_BRANCH} - - docker push $DOCKER_TAG:devel-${CIRCLE_BRANCH} - release: - tag: /v.*/ - commands: - - docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS - - docker tag $DOCKER_TAG $DOCKER_TAG:${CIRCLE_TAG} - - docker push $DOCKER_TAG:${CIRCLE_TAG} - - docker push $DOCKER_TAG diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ab726bb --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/echocat/site24x7_exporter + +go 1.12 + +require ( + github.com/golang/protobuf v1.3.2 + github.com/prometheus/client_golang v1.0.0 + github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 +) diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar deleted file mode 100644 index 30d399d..0000000 Binary files a/gradle/wrapper/gradle-wrapper.jar and /dev/null differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties deleted file mode 100644 index 5e6fb40..0000000 --- a/gradle/wrapper/gradle-wrapper.properties +++ /dev/null @@ -1,6 +0,0 @@ -#Tue Jul 26 13:09:13 CEST 2016 -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-bin.zip diff --git a/gradlew b/gradlew deleted file mode 100755 index 91a7e26..0000000 --- a/gradlew +++ /dev/null @@ -1,164 +0,0 @@ -#!/usr/bin/env bash - -############################################################################## -## -## Gradle start up script for UN*X -## -############################################################################## - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" - -APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` - -# Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" - -warn ( ) { - echo "$*" -} - -die ( ) { - echo - echo "$*" - echo - exit 1 -} - -# OS specific support (must be 'true' or 'false'). -cygwin=false -msys=false -darwin=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MINGW* ) - msys=true - ;; -esac - -# For Cygwin, ensure paths are in UNIX format before anything is touched. -if $cygwin ; then - [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` -fi - -# Attempt to set APP_HOME -# Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi -done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >&- -APP_HOME="`pwd -P`" -cd "$SAVED" >&- - -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar - -# Determine the Java command to use to start the JVM. -if [ -n "$JAVA_HOME" ] ; then - if [ -x "$JAVA_HOME/jre/sh/java" ] ; then - # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" - else - JAVACMD="$JAVA_HOME/bin/java" - fi - if [ ! -x "$JAVACMD" ] ; then - die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." - fi -else - JAVACMD="java" - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." -fi - -# Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi -fi - -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi - -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi - # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" - fi - i=$((i+1)) - done - case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac -fi - -# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules -function splitJvmOpts() { - JVM_OPTS=("$@") -} -eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS -JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" - -exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat deleted file mode 100644 index 8a0b282..0000000 --- a/gradlew.bat +++ /dev/null @@ -1,90 +0,0 @@ -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto init - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:init -@rem Get command-line arguments, handling Windowz variants - -if not "%OS%" == "Windows_NT" goto win9xME_args -if "%@eval[2+2]" == "4" goto 4NT_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* -goto execute - -:4NT_args -@rem Get arguments from the 4NT Shell from JP Software -set CMD_LINE_ARGS=%$ - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega diff --git a/main.go b/main.go index 9cebfd5..8fdbf5f 100644 --- a/main.go +++ b/main.go @@ -16,9 +16,10 @@ const ( ) var ( - name = "site24x7_exporter" - version = "devel" - description = "" + name = "site24x7_exporter" + version = "development" + revision = "development" + compiled = time.Now().Format("2006-01-02T15:04:05Z") listenAddress = flag.String("web.listen-address", ":9112", "Address to listen on for web interface and telemetry.") metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.") @@ -50,8 +51,9 @@ func main() { } func parseUsage() { - flags := flag.CommandLine + flags := flag.NewFlagSet(os.Args[0], flag.ContinueOnError) flags.SetOutput(flagsBuffer) + flags.ErrorHandling() flags.Usage = func() { errorString := flagsBuffer.String() if len(errorString) > 0 { @@ -60,7 +62,11 @@ func parseUsage() { printUsage(nil) } } - flags.Parse(os.Args[1:]) + if err := flags.Parse(os.Args[1:]); err == flag.ErrHelp { + os.Exit(0) + } else if err != nil { + os.Exit(1) + } assertUsage() } @@ -79,10 +85,8 @@ func fail(err interface{}) { } func printUsage(err interface{}) { - fmt.Fprintf(os.Stderr, "%v (version: %v, url: https://github.com/echocat/site24x7_exporter)\n", name, version) - if description != "" { - fmt.Fprintf(os.Stderr, "%v\n", description) - } + fmt.Fprintf(os.Stderr, "%v (version: %v, revision: %v, build: %v)\n", name, version, revision, compiled) + fmt.Fprint(os.Stderr, "URL: https://github.com/echocat/site24x7_exporter\n") fmt.Fprint(os.Stderr, "Author(s): Gregor Noczinski (gregor@noczinski.eu)\n") fmt.Fprint(os.Stderr, "\n") diff --git a/server.go b/server.go index 08b6f72..f1a6a64 100644 --- a/server.go +++ b/server.go @@ -2,10 +2,10 @@ package main import ( "crypto/tls" - "github.com/prometheus/client_golang/prometheus" + "github.com/echocat/site24x7_exporter/utils" + "github.com/prometheus/client_golang/prometheus/promhttp" "log" "net/http" - "github.com/echocat/site24x7_exporter/utils" ) type bufferedLogWriter struct { @@ -25,7 +25,7 @@ func startServer(metricsPath, listenAddress, tlsCert, tlsPrivateKey, tlsClientCa Addr: listenAddress, ErrorLog: createHttpServerLogWrapper(), } - http.Handle(metricsPath, prometheus.Handler()) + http.Handle(metricsPath, promhttp.Handler()) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(` Site24x7 Exporter