Skip to content

Commit

Permalink
init: ping-pong lakaluka
Browse files Browse the repository at this point in the history
Signed-off-by: Eray Ates <eates23@gmail.com>
  • Loading branch information
rytsh committed Sep 17, 2022
1 parent 8ecce53 commit 57f9f0c
Show file tree
Hide file tree
Showing 25 changed files with 1,081 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- master
- main
- init
pull_request:
permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: ${{ secrets.GO_VERSION }}
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: latest

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true then the all caching functionality will be complete disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
39 changes: 39 additions & 0 deletions .github/workflows/tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: "Release"
on:
push:
tags:
- v*

permissions:
contents: write
packages: write

jobs:
release:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3
- name: Install Go
uses: actions/setup-go@v3
with:
# stable: 'false' # Keep this line to be able to use rc and beta version of Go (ex: 1.18.0-rc1).
go-version: 1.18
- name: Unshallow
run: git fetch --prune --unshallow

- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Create release
uses: goreleaser/goreleaser-action@v3
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Test

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ${{ secrets.GO_VERSION }}

- name: Run coverage
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...
- name: Upload coverage to Codecov
run: bash <(curl -s https://codecov.io/bash)
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/pong
/dist
/coverage.*

*.retry
65 changes: 65 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
project_name: pong

release:
footer: |
**Full Changelog**: https://github.com/worldline-go/pong/compare/{{ .PreviousTag }}...{{ .Tag }}
before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
# - windows
# - darwin
main: ./cmd/pong
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.ShortCommit}} -X main.date={{.Date}}
flags:
- -trimpath
archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
format_overrides:
- goos: windows
format: zip
checksum:
name_template: '{{ .ProjectName }}-{{ .Version }}-checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
use: github
filters:
exclude:
- '^test:'
- '^chore'
- 'merge conflict'
- Merge pull request
- Merge remote-tracking branch
- Merge branch
- go mod tidy
groups:
- title: Dependency updates
regexp: "^.*feat\\(deps\\)*:+.*$"
order: 300
- title: 'New Features'
regexp: "^.*feat[(\\w)]*:+.*$"
order: 100
- title: 'Bug fixes'
regexp: "^.*fix[(\\w)]*:+.*$"
order: 200
- title: 'Documentation updates'
regexp: "^.*docs[(\\w)]*:+.*$"
order: 400
- title: Other work
order: 9999
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
BINARY := pong
ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
VERSION := $(or $(IMAGE_TAG),$(shell git describe --tags --first-parent --match "v*" 2> /dev/null || echo v0.0.0))
LOCAL_BIN_DIR := $(ROOT_DIR)/bin

.DEFAULT_GOAL := help

.PHONY: $(BINARY) test coverage help html html-gen html-wsl

build: ## Build the binary
go build -trimpath -ldflags="-s -w -X main.version=$(VERSION)" -o $(BINARY) cmd/$(BINARY)/main.go

test: ## Run unit tests
@go test -race ./...

coverage: ## Run unit tests with coverage
@go test -v -race -cover -coverpkg=./... -coverprofile=coverage.out -covermode=atomic ./...
@go tool cover -func=coverage.out

html: ## Show html coverage result
@go tool cover -html=./coverage.out

html-gen: ## Export html coverage result
@go tool cover -html=./coverage.out -o ./coverage.html

html-wsl: html-gen ## Open html coverage result in wsl
@explorer.exe `wslpath -w ./coverage.html` || true

help: ## Display this help screen
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# PONG 🏓

[![Codecov](https://img.shields.io/codecov/c/github/worldline-go/pong?logo=codecov&style=flat-square)](https://app.codecov.io/gh/worldline-go/pong)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/worldline-go/pong/Test?logo=github&style=flat-square&label=ci)](https://github.com/worldline-go/pong/actions)
[![Go Reference](https://pkg.go.dev/badge/github.com/worldline-go/pong.svg)](https://pkg.go.dev/github.com/worldline-go/pong)

Pong status checker.

Currently support only REST.

## Usage

Give a json or yaml file with the following structure:

```yaml
# LogLevel is the log level, default info
log_level: "debug"

check:
rest:
- concurrent: 1
check:
# URL could be multiple URLs, separated by space
- url: "https://api.punkapi.com/v2/beers/1 https://api.punkapi.com/v2/beers/2"
# Method is the HTTP method to use, default is GET
method: GET
# Timeout is in millisecond, default is 0 (no timeout)
timeout: 1000
# Status to check, default 200
status: 200
```
```sh
pong test.yaml
```

## With Ansible

Get pong binary in release page add the plugin modules area.

```sh
make build
```

```sh
docker run -it --rm -v ${PWD}:/workspace williamyeh/ansible:debian9 /bin/bash
```

Inside the container

```sh
echo localhost ansible_connection=local > /etc/ansible/hosts

mkdir -p /usr/share/ansible/plugins/modules/
cp /workspace/pong /usr/share/ansible/plugins/modules

ansible-playbook /workspace/testdata/ansible/deploy_check.yml
```

Example of playbook

```yaml
- name: Check image exists
pong:
check:
rest:
- concurrent: 1
check:
- url: "{% for k,item in yaml_return.ansible_facts.compose.services.items() %} https://hub.docker.com/v2/repositories/{{ item.image.split(':')[0] }}/tags/{{ item.image.split(':')[1] }} {% endfor %}"
timeout: 1000
register: http_result
failed_when: http_result.failed
```
## Development
Run `make` command to see available commands.

Local generate binary and docker image

```sh
goreleaser release --snapshot --rm-dist
```
Loading

0 comments on commit 57f9f0c

Please sign in to comment.