Skip to content

Commit

Permalink
Merge pull request #9 from insidieux/issue-8
Browse files Browse the repository at this point in the history
Issue 8
  • Loading branch information
insidieux authored Dec 28, 2020
2 parents 8c1fd56 + 8f49f1c commit e6f28d3
Show file tree
Hide file tree
Showing 24 changed files with 234 additions and 154 deletions.
38 changes: 7 additions & 31 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
name: Build binary
runs-on: ubuntu-18.04
needs: test
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
if: github.event_name == 'push'
strategy:
matrix:
goos: [ darwin, linux, windows ]
Expand Down Expand Up @@ -82,7 +82,7 @@ jobs:
name: Push docker image
runs-on: ubuntu-18.04
needs: binary
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
if: github.event_name == 'push'
strategy:
matrix:
goos: [ linux ]
Expand All @@ -101,50 +101,26 @@ jobs:
with:
ref: ${{ github.ref }}
head_ref: ${{ github.head_ref }}
- name: Build docker image for github registry
run: |
make docker-image-build DOCKER_TAG=${{ steps.get-tag-reference.outputs.tag }}
- name: Push docker image to github registry
run: |
make docker-image-push \
DOCKER_USER=${{ github.actor }} \
DOCKER_PASSWORD=${{ secrets.GITHUB_TOKEN }} \
DOCKER_TAG=${{ steps.get-tag-reference.outputs.tag }}
- name: Build docker image for github registry as latest
run: |
make docker-image-build DOCKER_TAG="latest"
- name: Push docker image to github registry as latest
run: |
make docker-image-push \
DOCKER_USER=${{ github.actor }} \
DOCKER_PASSWORD=${{ secrets.GITHUB_TOKEN }} \
DOCKER_TAG="latest"
- name: Build docker image for docker hub registry
run: |
make docker-image-build \
DOCKER_IMAGE=${{ github.repository }} \
DOCKER_TAG=${{ steps.get-tag-reference.outputs.tag }}
- name: Push docker image to docker hub registry
run: |
make docker-image-push \
DOCKER_REGISTRY=docker.io \
DOCKER_IMAGE=${{ github.repository }} \
DOCKER_USER=${{ secrets.DOCKER_HUB_USERNAME }} \
DOCKER_PASSWORD=${{ secrets.DOCKER_HUB_PASSWORD }} \
DOCKER_TAG=${{ steps.get-tag-reference.outputs.tag }}
- name: Build docker image for docker hub registry as latest
run: |
make docker-image-build \
DOCKER_IMAGE=${{ github.repository }} \
DOCKER_TAG="latest"
run: make docker-image-build
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
- name: Push docker image to docker hub registry as latest
run: |
make docker-image-push \
DOCKER_REGISTRY=docker.io \
DOCKER_IMAGE=${{ github.repository }} \
DOCKER_USER=${{ secrets.DOCKER_HUB_USERNAME }} \
DOCKER_PASSWORD=${{ secrets.DOCKER_HUB_PASSWORD }} \
DOCKER_TAG="latest"
DOCKER_PASSWORD=${{ secrets.DOCKER_HUB_PASSWORD }}
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')

release:
name: Upload release asset
runs-on: ubuntu-18.04
Expand Down
27 changes: 24 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ CGO_ENABLED?=0

BUIlD_VERSION?=latest

DOCKER_REGISTRY?=docker.pkg.github.com
DOCKER_IMAGE?=${DOCKER_REGISTRY}/insidieux/pinchy/${APP_NAME}
DOCKER_REGISTRY?=docker.io
DOCKER_IMAGE?=insidieux/${APP_NAME}
DOCKER_TAG?=latest
DOCKER_USER=
DOCKER_PASSWORD=
DOCKER_TAG?=latest

ifeq (, $(shell which docker))
$(error "Binary docker not found in $(PATH)")
Expand Down Expand Up @@ -111,6 +111,12 @@ build:

.PHONY: docker-image-build
docker-image-build:
ifndef DOCKER_IMAGE
$(error DOCKER_IMAGE is not set)
endif
ifndef DOCKER_TAG
$(error DOCKER_TAG is not set)
endif
@docker rmi ${DOCKER_IMAGE}:${DOCKER_TAG} || true
@docker build \
-f ${PWD}/build/docker/cmd/pinchy/Dockerfile \
Expand All @@ -119,6 +125,21 @@ docker-image-build:

.PHONY: docker-image-push
docker-image-push:
ifndef DOCKER_REGISTRY
$(error DOCKER_REGISTRY is not set)
endif
ifndef DOCKER_USER
$(error DOCKER_USER is not set)
endif
ifndef DOCKER_PASSWORD
$(error DOCKER_PASSWORD is not set)
endif
ifndef DOCKER_IMAGE
$(error DOCKER_IMAGE is not set)
endif
ifndef DOCKER_TAG
$(error DOCKER_TAG is not set)
endif
@docker login -u ${DOCKER_USER} -p ${DOCKER_PASSWORD} ${DOCKER_REGISTRY}
@docker push ${DOCKER_IMAGE}:${DOCKER_TAG}

Expand Down
3 changes: 3 additions & 0 deletions cmd/pinchy/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func NewCommand(version string) *cobra.Command {
}
watchCommand.Flags().Duration(`scheduler.interval`, time.Minute, `Interval between manager runs (1s, 1m, 5m, 1h and others)`)
registryCmd.PersistentFlags().Bool(`manager.continue-on-error`, false, `Omit errors during process manager`)
registryCmd.PersistentFlags().Bool(`manager.exit-on-error`, false, `Stop manager process on first error and by pass it to command line`)
_ = registryCmd.PersistentFlags().MarkDeprecated(`manager.continue-on-error`, `Flag "manager.continue-on-error" is deprecated, use "manager.exit-on-error" instead`)
_ = registryCmd.PersistentFlags().MarkHidden(`manager.continue-on-error`)
registryCmd.PersistentFlags().AddFlagSet(registryProvider.Flags())
registryCmd.AddCommand(onceCommand)
registryCmd.AddCommand(watchCommand)
Expand Down
2 changes: 1 addition & 1 deletion cmd/pinchy/internal/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func provideSource(commandViper *viper.Viper, factory source.Factory, logger cor

// Provider for core.Source
func provideManagerExitOnError(commandViper *viper.Viper) core.ManagerExitOnError {
return core.ManagerExitOnError(commandViper.GetBool(`manager.continue-on-error`))
return core.ManagerExitOnError(commandViper.GetBool(`manager.exit-on-error`))
}

// Provider for time.Ticker
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions deployments/docker-compose/pinchy/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3'

services:
pinchy:
image: insidieux/pinchy:v1.0.0
volumes:
- ./configs/source/file:/etc/pinchy
command:
- 'file'
- 'consul'
- '--source.path'
- '/etc/pinchy/example.yml'
- '--registry.address'
- 'http://consul:8500'
- 'once'
2 changes: 1 addition & 1 deletion docs/registry/consul.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
## Available flags

```
--registry.address %full-http-url-to-consul-api%
--registry.address string Consul http api address (default "127.0.0.1:8500")
```
15 changes: 3 additions & 12 deletions docs/source/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@
## Available flags

```
--source.path %full-path-to-yml-file%
--source.path string YML file config path (default "$HOME/services.yml")
```

## config.yml example
## services.yml example

```yaml
- name: service-name
id: service-id
port: 80
tags:
- tag-1
- tag-2
meta:
key: value
```
Example services.yml file be found in [configs](./../../configs/source/file/example.yml) file.
71 changes: 56 additions & 15 deletions docs/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

### Github Release

Visit the [releases page](https://github.com/insidieux/pinchy/releases/latest) to download one of the pre-built binaries for your platform.
Visit the [releases page](https://github.com/insidieux/pinchy/releases/latest) to download one of the pre-built binaries
for your platform.

### Docker

Expand All @@ -14,13 +15,6 @@ Use the [Docker image](https://hub.docker.com/repository/docker/insidieux/pinchy
docker pull insidieux/pinchy
```

or

```shell
echo PASSWORD_FILE | docker login docker.pkg.github.com --username USERNAME --password-stdin
docker pull docker.pkg.github.com/insidieux/pinchy
```

### go get

Alternatively, you can use the go get method:
Expand All @@ -36,29 +30,76 @@ Ensure that `$GOPATH/bin` is added to your `$PATH`.
### Binary

```shell
pinchy ...
pinchy %source% %registry% %mode% [flags]
```

### Docker

```shell
docker run insidieux/pinchy
docker run insidieux/pinchy:latest %source% %registry% %mode% [flags]
```

or
### Docker-compose run

```shell
docker run docker.pkg.github.com/insidieux/pinchy/pinchy
Example docker-compose file be found in [deployment](./../deployments/docker-compose/pinchy/docker-compose.yml)
directory

## Modes

`once` mode run sync process only single time

`watch` mode run sync process repeatedly with constant `schedule.interval`

## Command common flags

```
--logger.level string Log level (default "info")
--manager.exit-on-error Stop manager process on first error and by pass it to command line
```

### Watch mode

```
--scheduler.interval duration Interval between manager runs (1s, 1m, 5m, 1h and others) (default 1m0s)
```

### Available source types
### Source and Registry flags

Flags for chosen `source` and `registry` are described in a related documentation for sources and registry types.

## Available source types

- [file]

[file]: ./source/file.md

### Available registry types
## Available registry types

- [consul]

[consul]: ./registry/consul.md

## Examples

### Once

```
pinchy \
file \
consul \
once \
--source.path /etc/pinchy/services.yml \
--registry.address http://127.0.0.1:8500
```

### Watch

```
pinchy \
file \
consul \
watch \
--source.path /etc/pinchy/services.yml \
--registry.address http://127.0.0.1:8500 \
--scheduler.interval 5s
```
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.15

require (
github.com/agrea/ptr v0.0.0-20180711073057-77a518d99b7b
github.com/go-playground/validator/v10 v10.4.0
github.com/google/wire v0.4.0
github.com/hashicorp/consul/api v1.7.0
github.com/hashicorp/go-cleanhttp v0.5.1
Expand All @@ -18,6 +17,7 @@ require (
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.6.1
github.com/thoas/go-funk v0.7.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/net v0.0.0-20200519113804-d87ec0cfa476 // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
Expand Down
10 changes: 0 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@ github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.4.0 h1:72qIR/m8ybvL8L5TIyfgrigqkrw7kVYAvjEvpT85l70=
github.com/go-playground/validator/v10 v10.4.0/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
Expand Down Expand Up @@ -155,8 +147,6 @@ github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand Down
8 changes: 5 additions & 3 deletions internal/extension/registry/consul/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const (
registryName = `consul`

flagConsulAddress = `address`

defaultCommonTag = `pinchy`
)

type (
Expand All @@ -33,7 +35,7 @@ func init() {
}
}

func newClientConfig(v *viper.Viper, transport *http.Transport) (*api.Config, error) {
func provideClientConfig(v *viper.Viper, transport *http.Transport) (*api.Config, error) {
flag := registry.MakeFlagName(flagConsulAddress)
address := v.GetString(flag)
if address == `` {
Expand All @@ -50,14 +52,14 @@ func provideConsulClientFactory() factory {
return api.NewClient
}

func newClient(cfg *api.Config, factory factory) (client, error) {
func provideClient(cfg *api.Config, factory factory) (client, error) {
c, err := factory(cfg)
if err != nil {
return nil, errors.Wrap(err, `failed to create consul client`)
}
return c, nil
}

func newAgent(c client) pkgConsul.Agent {
func provideAgent(c client) pkgConsul.Agent {
return c.Agent()
}
7 changes: 4 additions & 3 deletions internal/extension/registry/consul/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
func NewRegistry(*viper.Viper) (core.Registry, func(), error) {
panic(wire.Build(
cleanhttp.DefaultPooledTransport,
newClientConfig,
provideClientConfig,
provideConsulClientFactory,
newClient,
newAgent,
provideClient,
provideAgent,
consul.NewRegistry,
wire.Value(consul.Tag(defaultCommonTag)),
wire.Bind(new(core.Registry), new(*consul.Registry)),
))
}
Loading

0 comments on commit e6f28d3

Please sign in to comment.