Skip to content
Snippets Groups Projects
Commit 45e7f3c1 authored by Rick Carback's avatar Rick Carback
Browse files

Initial setup for user-discovery-bot

parents
Branches
Tags
No related merge requests found
# From: https://about.gitlab.com/2017/09/21/how-to-create-ci-cd-pipeline-with-autodeploy-to-kubernetes-using-gitlab-and-helm/
cache:
untracked: true
key: "$CI_BUILD_REF_NAME"
paths:
- vendor/
variables:
REPO_DIR: gitlab.com/privategrity
REPO_NAME: server
DOCKER_IMAGE: carback1/golang-glide:1.9-4a49ace8cac8
MIN_CODE_COVERAGE: "80.0"
before_script:
##
## Go Setup
##
- go version || echo "Go executable not found."
- echo $CI_BUILD_REF
- echo $CI_PROJECT_DIR
- mkdir -p $GOPATH/src/$REPO_DIR
- ln -s $CI_PROJECT_DIR $GOPATH/src/$REPO_DIR/$REPO_NAME
- cd $GOPATH/src/$REPO_DIR/$REPO_NAME
- echo $PWD
stages:
- setup
- test
- build
- trigger_integration
setup:
stage: setup
image: $DOCKER_IMAGE
script:
# https://docs.gitlab.com/ee/ci/ssh_keys/README.html
##
## Run ssh-agent (inside the build environment)
##
- eval $(ssh-agent -s)
##
## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
## We're using tr to fix line endings which makes ed25519 keys work
## without extra base64 encoding.
## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
##
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
##
## Create the SSH directory and give it the right permissions
##
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
##
## Add SSH key for gitlab.com
##
- ssh-keyscan -t rsa gitlab.com > ~/.ssh/known_hosts
- glide cc
- glide up
artifacts:
paths:
- vendor/
test:
stage: test
image: $DOCKER_IMAGE
coverage: "^total:\s+\(statements\)\s+\d+\.\d+\%$"
script:
- mkdir -p testdata
# Test coverage
- goverage -coverprofile=testdata/coverage.out -v ./... 2>&1 | grep -v "no packages being tested depend on"
# Exclude some specific packages and files
# - grep -v cmd testdata/coverage.out > testdata/coverage-real.out
- go tool cover -func=testdata/coverage.out
- go tool cover -html=testdata/coverage.out -o testdata/coverage.html
# Benchmarking
- go test -bench=BenchmarkPrecomp -cpuprofile=testdata/precomp.cpu -memprofile=testdata/precomp.mem -short main_benchmarks_test.go
- go tool pprof -png server.test testdata/precomp.cpu > testdata/precomp-cpu.png
- go tool pprof -png server.test testdata/precomp.mem > testdata/precomp-mem.png
- go test -bench=BenchmarkRealtime -cpuprofile=testdata/realtime.cpu -memprofile=testdata/realtime.mem -short main_benchmarks_test.go
- go tool pprof -png server.test testdata/realtime.cpu > testdata/realtime-cpu.png
- go tool pprof -png server.test testdata/realtime.mem > testdata/realtime-mem.png
# Test Coverage Check
- go tool cover -func=testdata/coverage-real.out | grep "total:" | awk '{print $3}' | sed 's/\%//g' > testdata/coverage-percentage.txt
- export CODE_CHECK=$(echo "$(cat testdata/coverage-percentage.txt) >= $MIN_CODE_COVERAGE" | bc -l)
- (if [ "$CODE_CHECK" == "1" ]; then echo "Minimum coverage of $MIN_CODE_COVERAGE succeeded"; else echo "Minimum coverage of $MIN_CODE_COVERAGE failed"; exit 1; fi);
artifacts:
paths:
- testdata/
build:
stage: build
image: $DOCKER_IMAGE
script:
- mkdir -p release
- go generate cmd/version.go
- GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags '-w -s' ./...
- GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags '-w -s' -o release/udb.linux64 main.go
- GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -ldflags '-w -s' -o release/udb.win64 main.go
- GOOS=windows GOARCH=386 CGO_ENABLED=0 go build -ldflags '-w -s' -o release/udb.win32 main.go
- GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -ldflags '-w -s' -o release/udb.darwin64 main.go
- ./release/udb.linux64 --version
artifacts:
paths:
- release/
# trigger_integration:
# stage: trigger_integration
# script:
# - "curl -X POST -F token=3cd593ad56ec017e30254c9ec6c0ab -F ref=master https://gitlab.com/api/v4/projects/5615854/trigger/pipeline"
# only:
# - master
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2018 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
// The following directive is necessary to make the package coherent:
// +build ignore
// This program generates cmd/version_vars.go. It can be invoked by running
// go generate
package main
import (
"bufio"
"log"
"io/ioutil"
"os"
"os/exec"
"text/template"
"time"
"strings"
)
func GenerateGitVersion() string {
cmd := exec.Command("git", "show", "--oneline")
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(strings.NewReader(string(stdoutStderr)))
for scanner.Scan() {
return scanner.Text()
}
return "UNKNOWNVERSION"
}
func ReadGlideLock() string {
r, _ := ioutil.ReadFile("../glide.lock")
return string(r)
}
func main() {
gitversion := GenerateGitVersion()
glidedependencies := ReadGlideLock()
f, err := os.Create("version_vars.go")
die(err)
defer f.Close()
packageTemplate.Execute(f, struct {
Timestamp time.Time
GITVER string
GLIDEDEPS string
}{
Timestamp: time.Now(),
GITVER: gitversion,
GLIDEDEPS: glidedependencies,
})
}
func die(err error) {
if err != nil {
log.Fatal(err)
}
}
var packageTemplate = template.Must(template.New("").Parse(
"// Code generated by go generate; DO NOT EDIT.\n" +
"// This file was generated by robots at\n" +
"// {{ .Timestamp }}\n" +
"package cmd\n\n" +
"const GITVERSION = `{{ .GITVER }}`\n" +
"const SEMVER = \"0.0.0a\"\n" +
"const GLIDEDEPS = `{{ .GLIDEDEPS }}`\n"))
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2018 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
// Package cmd initializes the CLI and config parsers as well as the logger.
package cmd
import (
"os"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
)
var cfgFile string
var verbose bool
var showVer bool
var validConfig bool
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "user-discovery-bot",
Short: "Runs a user discovery bot for cMix",
Long: `This bot provides user lookup and search functions on cMix`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if showVer {
printVersion()
return
}
if !validConfig {
jww.WARN.Println("Invalid Config File")
}
// StartBot()
},
}
// Execute adds all child commands to the root command and sets flags
// appropriately. This is called by main.main(). It only needs to
// happen once to the RootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
jww.ERROR.Println(err)
os.Exit(1)
}
}
// init is the initialization function for Cobra which defines commands
// and flags.
func init() {
// NOTE: The point of init() is to be declarative.
// There is one init in each sub command. Do not put variable declarations
// here, and ensure all the Flags are of the *P variety, unless there's a
// very good reason not to have them as local params to sub command."
cobra.OnInitialize(initConfig, initLog)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
RootCmd.Flags().StringVarP(&cfgFile, "config", "", "",
"config file (default is $HOME/.privategrity/server.yaml)")
RootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false,
"Verbose mode for debugging")
RootCmd.Flags().BoolVarP(&showVer, "version", "V", false,
"Show the server version information.")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
//Use default config location if none is passed
if cfgFile == "" {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
jww.WARN.Println(err)
}
cfgFile = home + "/.privategrity/user-discovery-bot.yaml"
}
f, err := os.Open(cfgFile)
_, err = f.Stat()
validConfig = true
if err != nil {
jww.WARN.Printf("Invalid config file (%s): %s", cfgFile,
err.Error())
validConfig = false
}
f.Close()
viper.SetConfigFile(cfgFile)
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
jww.WARN.Printf("Unable to read config file (%s): %s", cfgFile, err.Error())
validConfig = false
}
}
// initLog initializes logging thresholds and the log path.
func initLog() {
if viper.Get("logPath") != nil {
// If verbose flag set then log more info for debugging
if verbose || viper.GetBool("verbose") {
jww.SetLogThreshold(jww.LevelDebug)
jww.SetStdoutThreshold(jww.LevelDebug)
} else {
jww.SetLogThreshold(jww.LevelInfo)
jww.SetStdoutThreshold(jww.LevelInfo)
}
// Create log file, overwrites if existing
logPath := viper.GetString("logPath")
logFile, err := os.Create(logPath)
if err != nil {
jww.WARN.Println("Invalid or missing log path, default path used.")
} else {
jww.SetLogOutput(logFile)
}
}
}
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
//go:generate go run gen.go
// The above generates: GITVERSION, GLIDEDEPS, and SEMVER
func init() {
RootCmd.AddCommand(versionCmd)
}
func printVersion() {
fmt.Printf("Privategrity User Discovery Bot v%s -- %s\n\n", SEMVER,
GITVERSION)
fmt.Printf("Dependencies:\n\n%s\n", GLIDEDEPS)
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of Privategrity UDB",
Long: `Print the version number of Privategrity User Discovery bot. This
also prints the glide cache versions of all of its dependencies.`,
Run: func(cmd *cobra.Command, args []string) {
printVersion()
},
}
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2018-04-26 21:55:17.4265437 +0000 UTC m=+0.001850439
package cmd
const GITVERSION = `RUN go generate cmd/version.go!`
const SEMVER = "0.0.0a"
const GLIDEDEPS = `You need to run go generate cmd/version.go!`
package: gitlab.com/privategrity/user-discovery-bot
import:
- package: gitlab.com/privategrity/client
version: master
repo: git@gitlab.com:privategrity/client
vcs: git
- package: github.com/spf13/cobra
version: ^0.0.1
subpackages:
- cobra
main.go 0 → 100644
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2018 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package main
import "gitlab.com/privategrity/user-discovery-bot/cmd"
func main() {
cmd.Execute()
}
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2018 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package main
import (
"os/exec"
"testing"
"gitlab.com/privategrity/user-discovery-bot/cmd"
)
// Smoke test for main
func TestMainSmoke(t *testing.T) {
cmd.RootCmd.SetArgs([]string{"--version"})
main()
cmd.RootCmd.SetArgs([]string{"--version", "--config", "sampleconfig.yaml"})
main()
cmd := exec.Command("go", "run", "main.go", "--version")
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
t.Errorf("Smoke test failed with %v", e)
}
}
#No config options yet
logPath: "logfile.log"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment