Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
reddec committed Jan 23, 2023
0 parents commit be6ff2e
Show file tree
Hide file tree
Showing 15 changed files with 781 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
.DS_Store
/dist
46 changes: 46 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Build and release
on:
push:
tags:
- 'v*'

env:
REGISTRY: ghcr.io

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '~1.19'
id: go
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
- name: Check out code into the Go module directory
uses: actions/checkout@v2
with:
lfs: true
fetch-depth: 0
- name: Checkout LFS objects
run: git lfs checkout
- name: Pull tag
run: git fetch --tags
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
.DS_Store
/dist
41 changes: 41 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- darwin
goarch:
- arm64
- amd64
main: ./cmd/scheduler/main.go
binary: scheduler
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
dockers:
- image_templates:
- "ghcr.io/reddec/{{ .ProjectName }}:{{ .Version }}-amd64"
use: buildx
dockerfile: Dockerfile
build_flag_templates:
- "--platform=linux/amd64"
- image_templates:
- "ghcr.io/reddec/{{ .ProjectName }}:{{ .Version }}-arm64v8"
use: buildx
goarch: arm64
dockerfile: Dockerfile
build_flag_templates:
- "--platform=linux/arm64/v8"
docker_manifests:
- name_template: "ghcr.io/reddec/{{ .ProjectName }}:{{ .Version }}"
image_templates:
- "ghcr.io/reddec/{{ .ProjectName }}:{{ .Version }}-amd64"
- "ghcr.io/reddec/{{ .ProjectName }}:{{ .Version }}-arm64v8"

changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM alpine:3.17 AS certs
RUN apk add --no-cache ca-certificates && update-ca-certificates

FROM scratch
ENTRYPOINT ["/scheduler"]
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
ADD scheduler /
15 changes: 15 additions & 0 deletions Dockerfile.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.19 AS build
WORKDIR /usr/src/app
COPY go.mod go.sum ./
RUN go mod download && go mod verify

COPY . .
RUN CGO_ENABLED=0 go build -v -o /usr/local/bin/app ./cmd/...

FROM alpine:3.17 AS certs
RUN apk add --no-cache ca-certificates && update-ca-certificates

FROM scratch
ENTRYPOINT ["/app"]
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build /usr/local/bin/app /app
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 reddec

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
92 changes: 92 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Docker-Compose scheduler

Simple and lightweight service which can execute `docker compose run ...` services from the same file based on cron
expression.

Features:

- Zero-configuration by-default
- Designed for docker compose (auto-detect, respects namespace)
- HTTP notifications with retries

Inspired by [ofelia](https://github.com/mcuadros/ofelia).

```yaml
services:
web:
image: "nginx"
labels:
- "net.reddec.scheduler.cron=@daily"
- "net.reddec.scheduler.exec=nginx -s reload"

date:
image: busybox
restart: "no"
labels:
- "net.reddec.scheduler.cron=* * * * *"

scheduler:
image: ghcr.io/reddec/compose-scheduler:1
restart: unless-stopped
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
```
Supports two modes:
- plain `docker compose run`
- exec command inside service (extra label `net.reddec.scheduler.exec`)

## Usage

```
Application Options:
--project= Docker compose project, will be automatically detected if not set [$PROJECT]

HTTP notification:
--notify.url= URL to invoke [$NOTIFY_URL]
--notify.retries= Number of additional retries (default: 5) [$NOTIFY_RETRIES]
--notify.interval= Interval between attempts (default: 12s) [$NOTIFY_INTERVAL]
--notify.method= HTTP method (default: POST) [$NOTIFY_METHOD]
--notify.timeout= Request timeout (default: 30s) [$NOTIFY_TIMEOUT]
--notify.authorization= Authorization header value [$NOTIFY_AUTHORIZATION]

Help Options:
-h, --help Show this help message
```
## Notifications
Scheduler will send notifications after each job if `NOTIFY_URL` env variable or `--notify.url` flag set. Each
notification is a simple HTTP request.
HTTP method, attempts number, and interval between attempts can be configured.
Authorization via `Authorization` header also supported.
Scheduler will stop retries if at least one of the following criteria met:
- reached maximum number of attempts
- server returned any `2xx` code (ex: `200`, `201`, ...)
Outgoing custom headers:
- `Content-Type: application/json`
- `User-Agent: scheduler/<version>`, where `<version>` is build version
- `Authorization: <value>` (if set)
Payload:
```json
{
"project": "compose-project",
"service": "web",
"container": "deadbeaf1234",
"schedule": "@daily",
"started": "2023-01-20T11:10:39.44006+08:00",
"finished": "2023-01-20T11:10:39.751879+08:00",
"failed": true,
"error": "exit code 1"
}
```

> field `error` exists only if `failed == true`
59 changes: 59 additions & 0 deletions cmd/scheduler/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"context"
"fmt"
"log"
"os"
"os/signal"

"github.com/jessevdk/go-flags"
scheduler "github.com/reddec/compose-scheduler"
)

//nolint:gochecknoglobals
var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
)

type Config struct {
Project string `long:"project" env:"PROJECT" description:"Docker compose project, will be automatically detected if not set"`
Notify scheduler.HTTPNotification `group:"HTTP notification" namespace:"notify" env-namespace:"NOTIFY"`
}

func main() {
var config Config
config.Notify.UserAgent = "scheduler/" + version
parser := flags.NewParser(&config, flags.Default)
parser.ShortDescription = "Compose scheduler"
parser.LongDescription = fmt.Sprintf("Docker compose scheduler\nscheduler %s, commit %s, built at %s by %s\nAuthor: Aleksandr Baryshnikov <[email protected]>", version, commit, date, builtBy)

if _, err := parser.Parse(); err != nil {
os.Exit(1)
}

ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()

var opts []scheduler.Option
if config.Project != "" {
opts = append(opts, scheduler.WithProject(config.Project))
}
if config.Notify.URL != "" {
opts = append(opts, scheduler.WithNotification(&config.Notify))
}
sc, err := scheduler.Create(ctx, opts...)
if err != nil {
log.Panic(err)
}
defer sc.Close()
log.Println("started")
err = sc.Run(ctx)
if err != nil {
log.Panic(err)
}
log.Println("finished")
}
18 changes: 18 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# example docker compose
services:
app: # any stateful application
image: "nginx"
labels:
- "net.reddec.scheduler.cron=* * * * *"
- "net.reddec.scheduler.exec=nginx -s reload"
date:
image: busybox
restart: "no"
labels:
- "net.reddec.scheduler.cron=* * * * *"
scheduler:
build:
dockerfile: Dockerfile.build
restart: unless-stopped
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
23 changes: 23 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module github.com/reddec/compose-scheduler

go 1.19

require (
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v20.10.23+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/jessevdk/go-flags v1.5.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/tools v0.1.12 // indirect
)
Loading

0 comments on commit be6ff2e

Please sign in to comment.