Skip to content

Commit

Permalink
🎉 Initial commit for samsahai tool
Browse files Browse the repository at this point in the history
  • Loading branch information
phantomnat authored and Pohfy123 committed Mar 17, 2020
0 parents commit fd45a92
Show file tree
Hide file tree
Showing 17 changed files with 427 additions and 0 deletions.
139 changes: 139 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
commands:
early_return_for_forked_pull_requests:
description: >-
If this build is from a fork, stop executing the current job and return success.
This is useful to avoid steps that will fail due to missing credentials.
steps:
- run:
name: Early return if this build is from a forked PR
command: |
if [ -n "$CIRCLE_PR_NUMBER" ]; then
echo "Nothing to do for forked PRs, so marking this step successful"
circleci step halt
fi
version: 2
jobs:
build:
docker:
- image: circleci/golang:1.12

working_directory: /go/src/github.com/agoda-com/samsahai
steps:
# - checkout
# - restore_cache:
# keys:
# - project-{{ checksum "go.sum" }}
# - project-

- checkout
- setup_remote_docker

- run:
name: Prepared Env vars.
command: |
echo 'export docker_user="$QUAY_DOCKER_USER"' >> $BASH_ENV
echo 'export docker_registry="$QUAY_DOCKER_REGISTRY"' >> $BASH_ENV
echo 'export docker_image_repo="phantomnat/samsahai"' >> $BASH_ENV
# echo 'export docker_image_tag="latest"' >> $BASH_ENV
- run:
name: "get required projects"
command: |
go get golang.org/x/tools/cmd/goimports
go get github.com/ahmetb/govvv
go get github.com/jpoles1/gopherbadger
echo 'export GO111MODULE="on"' >> $BASH_ENV
- run:
name: "install dependencies"
command: |
go mod tidy
- run:
name: "run test"
command: |
echo GO111MODULE=${GO111MODULE}
make coverage
- run:
name: "build docker"
command: |
if [ "$CIRCLE_BRANCH" = "master" ]; then
DOCKER_IMAGE_TAG=$(cat ./VERSION)
docker_image_exists() {
local image_full_name="$1"; shift
local wait_time="${1:-5}"
local search_term='Pulling|is up to date|not found'
local result="$((timeout --preserve-status "$wait_time" docker 2>&1 pull "$image_full_name" &) | grep -v 'Pulling repository' | egrep -o "$search_term")"
test "$result" || { echo "Timed out too soon. Try using a wait_time greater than $wait_time..."; return 1 ;}
echo $result | grep -vq 'not found'
}
if docker_image_exists "$docker_registry/$docker_image_repo:$DOCKER_IMAGE_TAG"; then
echo "error: docker image version $DOCKER_IMAGE_TAG already exists!"
exit 1
fi
else
DOCKER_IMAGE_TAG=$(echo "${CIRCLE_PULL_REQUEST}" |awk -F/ '{print $ (NF-1)"-"$NF}')
fi
echo tag: $DOCKER_IMAGE_TAG
echo "export docker_image_tag=\"$DOCKER_IMAGE_TAG\"" >> $BASH_ENV
make build-docker docker_image_tag=$DOCKER_IMAGE_TAG
- run:
name: docker login
command: |
make docker-login docker_password="$QUAY_DOCKER_TOKEN"
- run:
name: docker push
command: |
make docker-push
if [ "$CIRCLE_BRANCH" = "master" ]; then
make docker-tag-n-push-latest
fi
# - save_cache:
# key: project-{{ checksum "project.clj" }}
# paths:
# - /go

# publish-docker:
# docker:
# - image: circleci/golang:1.12
# working_directory: /go/src/github.com/agoda-com/samsahai
# steps:
# - early_return_for_forked_pull_requests
# - checkout
# - run:
# name: Prepared Env vars.
# command: |
# echo 'export docker_user="$QUAY_DOCKER_USER"' >> $BASH_ENV
# echo 'export docker_registry="$QUAY_DOCKER_REGISTRY"' >> $BASH_ENV
# echo 'export docker_registry="$QUAY_DOCKER_REGISTRY"' >> $BASH_ENV
# attach_workspace:
# at: /go
#
# - run:
# name: Docker login
# command: |
# make docker-login docker_password="$QUAY_DOCKER_TOKEN"
# - run:
# name: Docker push
# command: |
# make docker-push

workflows:
version: 2
build-deploy:
jobs:
- build
# - publish-docker:
# requires:
# - build
# filters:
# branches:
# only: master
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea/

out/

coverage.out
coverage.txt
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea/

out/

coverage.out
coverage.txt
73 changes: 73 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
docker_registry ?= "quay.io"
docker_image_repo ?= "agoda-com/samsahai"
docker_image_tag ?= latest
#docker_user := ""
#docker_password := ""

package_name := github.com/agoda-com/samsahai
app_name := samsahai
output_path := ./out
go_ldflags ?= $(shell govvv -flags -pkg $(shell go list ./internal/samsahai))

.PHONY: format
format:
gofmt -w .
goimports -w .

.PHONY: build
build: format
go build -ldflags="$(go_ldflags)" -o $(output_path)/$(app_name) cmd/main.go

.PHONY: install
install: build
cp $(output_path)/$(app_name) $$GOPATH/bin/$(app_name)

.PHONY: print-flag
print-flag:
@echo $(go_ldflags)

.PHONY: build-docker
build-docker:
docker build -t $(docker_registry)/$(docker_image_repo):$(docker_image_tag) \
--build-arg GO_LDFLAGS="$(go_ldflags)" \
-f scripts/Dockerfile .

.PHONY: tidy
tidy:
GO111MODULE=on go mod tidy

.PHONY: coverage
coverage: format
go test `go list ./internal/... | grep -v cmd` -coverprofile=coverage.txt -covermode=atomic

.PHONY: cover-badge
cover-badge: coverage
gopherbadger \
-covercmd "go tool cover -func=coverage.txt"

.PHONY: coverage-html
coverage-html: coverage
go tool cover -html=coverage.txt

.PHONY: docker-login-b64
docker-login-b64:
#echo $(docker_password) | base64 --decode | docker login -u $(docker_user) $(docker_registry) --password-stdin

.PHONY: docker-login
docker-login:
echo $(docker_password) | docker login -u $(docker_user) $(docker_registry) --password-stdin

.PHONY: docker-logout
docker-logout:
docker logout $(docker_registry)

.PHONY: docker-push
docker-push:
docker push $(docker_registry)/$(docker_image_repo):$(docker_image_tag)

.PHONY: docker-tag-n-push-latest
docker-tag-n-push-latest:
@if [ "$(docker_image_tag)" != "latest" ]; then \
docker tag $(docker_registry)/$(docker_image_repo):$(docker_image_tag) $(docker_registry)/$(docker_image_repo):latest; \
docker push $(docker_registry)/$(docker_image_repo):latest; \
fi
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Samsahai
[![CircleCI](https://circleci.com/gh/agoda-com/samsahai.svg?style=svg&circle-token=622dc452d07545e720fc396074cb25a737a204e5)](https://circleci.com/gh/agoda-com/samsahai) [![Docker Repository on Quay](https://quay.io/repository/phantomnat/samsahai/status "Docker Repository on Quay")](https://quay.io/repository/phantomnat/samsahai)

> Creating self-update environment on Kubernetes
---

#### How to contribute

---

#### Related Projects:
- [`go get github.com/ahmetb/govvv`](https://github.com/ahmetb/govvv) - Add version info to Golang app.
- [`go get github.com/jpoles1/gopherbadger`](https://github.com/jpoles1/gopherbadger) - Generate code coverage badge
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
9 changes: 9 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"github.com/agoda-com/samsahai/internal/samsahai/command"
)

func main() {
command.SamsahaiCmd.Execute()
}
Binary file added coverage_badge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/agoda-com/samsahai

go 1.12

require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/onsi/gomega v1.5.0
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 // indirect
)
33 changes: 33 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/onsi/ginkgo v1.6.0 h1:Ix8l273rp3QzYgXSR+c8d1fTG7UPgYkOSELPhiY/YGw=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
30 changes: 30 additions & 0 deletions internal/samsahai/command/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package command

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var SamsahaiCmd = &cobra.Command{
Use: "samsahai",
Short: "Samsahai is a command line for create Blue/Green enviroment",
Long: "Samsahai is a command line for create Blue/Green enviroment",
}

// Execute executes the current command.
func Execute() {
if err := SamsahaiCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
addCommands()
}

func addCommands() {
SamsahaiCmd.AddCommand(versionCmd())
}
32 changes: 32 additions & 0 deletions internal/samsahai/command/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package command

import (
"fmt"

"github.com/agoda-com/samsahai/internal/samsahai"
"github.com/spf13/cobra"
)

func getVersion() string {
var branch = ""
if samsahai.GitBranch != "master" {
branch = fmt.Sprintf(" git:%s-%s%s branch:%s", samsahai.GitCommit, samsahai.GitState, branch, samsahai.GitBranch)
}
return fmt.Sprintf("v%s%s", samsahai.Version, branch)
}

func getAppName() string {
return samsahai.AppName
}

func versionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Aliases: []string{"v"},
Short: "show version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(samsahai.AppName, getVersion())
},
}
return cmd
}
17 changes: 17 additions & 0 deletions internal/samsahai/command/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package command

import (
"testing"

. "github.com/onsi/gomega"
)

func TestGetAppName(t *testing.T) {
g := NewGomegaWithT(t)
g.Expect(getAppName()).ShouldNot(Equal(""), "app name shouldn't empty")
}

func TestGetAppVersion(t *testing.T) {
g := NewGomegaWithT(t)
g.Expect(getVersion()).ShouldNot(Equal("app version shouldn't empty"))
}
1 change: 1 addition & 0 deletions internal/samsahai/reporter/reporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package reporter
5 changes: 5 additions & 0 deletions internal/samsahai/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package samsahai

const AppName = "samsahai"

var GitCommit, GitState, GitBranch, Version string
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package samsahai
Loading

0 comments on commit fd45a92

Please sign in to comment.