diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c01fc51 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +bin/ +dist/ \ No newline at end of file diff --git a/.gitignore b/.gitignore index 139ae32..99c0715 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ # Dependency directories (remove the comment below to include it) # vendor/ -bin/ \ No newline at end of file +bin/ +dist/ \ No newline at end of file diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..4422c89 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,54 @@ +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + - windows + - freebsd + goarch: + - amd64 + - arm + - arm64 + ignore: + - goos: freebsd + goarch: arm + - goos: freebsd + goarch: arm64 + dir: app + ldflags: "-s -w -X main.revision={{.Version}}-{{.CommitDate}}" + +archives: + - name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}" + replacements: + 386: i386 + amd64: x86_64 + darwin: macos + format_overrides: + - goos: windows + format: zip + files: + - LICENSE + - README.md + +nfpms: + - + package_name: cronn + file_name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}" + vendor: Umputun + homepage: https://github.com/umputun/cronn + maintainer: Umputun + description: cron replacement with extras + license: MIT + formats: + - deb + - rpm + bindir: /usr/bin + epoch: 1 + release: 1 + files: + "cronn.service": "/etc/systemd/system/cronn.service" + "crontab.example": "/etc/cronntab" + scripts: + postinstall: "scripts/postinstall.sh" + preremove: "scripts/preremove.sh" \ No newline at end of file diff --git a/Dockerfile.artifacts b/Dockerfile.artifacts index 0d98f59..33415eb 100644 --- a/Dockerfile.artifacts +++ b/Dockerfile.artifacts @@ -1,31 +1,17 @@ -FROM umputun/baseimage:buildgo-latest as build +FROM goreleaser/goreleaser as build WORKDIR /build ADD . /build -RUN \ - version=$(/script/git-rev.sh); \ - echo "version=$version" && \ - export GOFLAGS="-mod=vendor" && \ - echo "linux-amd64" && GOOS=linux GOARCH=amd64 go build -o cronn.linux-amd64 -ldflags "-X main.revision=${version} -s -w" ./app && \ - echo "linux-386" && GOOS=linux GOARCH=386 go build -o cronn.linux-386 -ldflags "-X main.revision=${version} -s -w" ./app && \ - echo "linux-arm" && GOOS=linux GOARCH=arm go build -o cronn.linux-arm -ldflags "-X main.revision=${version} -s -w" ./app && \ - echo "linux-arm64" && GOOS=linux GOARCH=arm64 go build -o cronn.linux-arm64 -ldflags "-X main.revision=${version} -s -w" ./app && \ - echo "windows-amd64" && GOOS=windows GOARCH=amd64 go build -o cronn.windows-amd64.exe -ldflags "-X main.revision=${version} -s -w" ./app && \ - echo "darwin-amd64" && GOOS=darwin GOARCH=amd64 go build -o cronn.darwin-amd64 -ldflags "-X main.revision=${version} -s -w" ./app && \ - echo "freebsd-amd64" && GOOS=freebsd GOARCH=amd64 go build -o cronn.freebsd-amd64 -ldflags "-X main.revision=${version} -s -w" ./app +RUN goreleaser --snapshot --skip-publish --rm-dist +FROM alpine +COPY --from=build /build/dist/ /dist/ RUN \ - apk add --no-cache --update zip && \ - tar cvzf cronn.linux-amd64.tar.gz cronn.linux-amd64 LICENSE README.md && \ - tar cvzf cronn.linux-386.tar.gz cronn.linux-386 LICENSE README.md && \ - tar cvzf cronn.linux-arm.tar.gz cronn.linux-arm LICENSE README.md && \ - tar cvzf cronn.linux-arm64.tar.gz cronn.linux-arm64 LICENSE README.md && \ - tar cvzf cronn.darwin-amd64.tar.gz cronn.darwin-amd64 LICENSE README.md && \ - tar cvzf cronn.freebsd-amd64.tar.gz cronn.freebsd-amd64 LICENSE README.md && \ - zip cronn.windows-amd64.zip cronn.windows-amd64.exe LICENSE README.md + mkdir -p /artifacts && \ + cp /dist/*.gz /artifacts/ && \ + cp /dist/*.zip /artifacts/ && \ + cp /dist/*.txt /artifacts/ && \ + ls -la /artifacts/* -FROM alpine -COPY --from=build /build/cronn.* /artifacts/ -RUN ls -la /artifacts/* CMD ["sleep", "100"] diff --git a/Makefile b/Makefile index 1085aee..ae81d36 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,15 @@ docker: docker build -t umputun/cronn . -bin: +dist: + - @mkdir -p dist docker build -f Dockerfile.artifacts -t cronn.bin . - @docker rm -f cronn.bin 2>/dev/null || exit 0 - - @mkdir -p bin docker run -d --name=cronn.bin cronn.bin - docker cp cronn.bin:/artifacts/cronn.linux-amd64.tar.gz bin/cronn.linux-amd64.tar.gz - docker cp cronn.bin:/artifacts/cronn.linux-386.tar.gz bin/cronn.linux-386.tar.gz - docker cp cronn.bin:/artifacts/cronn.linux-arm64.tar.gz bin/cronn.linux-arm64.tar.gz - docker cp cronn.bin:/artifacts/cronn.darwin-amd64.tar.gz bin/cronn.darwin-amd64.tar.gz - docker cp cronn.bin:/artifacts/cronn.freebsd-amd64.tar.gz bin/cronn.freebsd-amd64.tar.gz - docker cp cronn.bin:/artifacts/cronn.windows-amd64.zip bin/cronn.windows-amd64.zip + docker cp cronn.bin:/artifacts dist/ docker rm -f cronn.bin race_test: cd app && go test -race -mod=vendor -timeout=60s -count 1 ./... -.PHONY: bin docker \ No newline at end of file +.PHONY: dist docker race_test \ No newline at end of file diff --git a/app/crontab/crontab_test.go b/app/crontab/crontab_test.go index 5e06d96..b4df8d1 100644 --- a/app/crontab/crontab_test.go +++ b/app/crontab/crontab_test.go @@ -70,8 +70,8 @@ func TestParser_Changes(t *testing.T) { assert.Len(t, jobs, 2) time.AfterFunc(time.Millisecond*500, func() { - _, err = tmp.WriteString("3 * * * * ls\n") - require.NoError(t, err) + _, e := tmp.WriteString("3 * * * * ls\n") + require.NoError(t, e) }) ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) @@ -84,5 +84,4 @@ func TestParser_Changes(t *testing.T) { jobs, err = ctab.List() require.NoError(t, err) assert.Len(t, jobs, 3) - } diff --git a/cronn.service b/cronn.service index ff9c72b..edb1d20 100644 --- a/cronn.service +++ b/cronn.service @@ -7,8 +7,8 @@ StartLimitIntervalSec=0 Type=simple Restart=always RestartSec=1 -User= -ExecStart=/opt/cronn/cronn -f /opt/cronn/crontab -u +User=root +ExecStart=/usr/bin/cronn -f /etc/cronntab -u [Install] WantedBy=multi-user.target diff --git a/crontab.example b/crontab.example index d18eb1a..0d673f6 100644 --- a/crontab.example +++ b/crontab.example @@ -1,4 +1,4 @@ -*/5 * * * * ls -la . -*/2 1-18 * * * export -*/1 * * * * echo {{.YYYYMMDD}} -*/1 * * * * something blah bad +# */5 * * * * ls -la . +# */2 1-18 * * * export +# */1 * * * * echo {{.YYYYMMDD}} +# @every 1h15m something blah bad diff --git a/scripts/postinstall.sh b/scripts/postinstall.sh new file mode 100644 index 0000000..28bfdef --- /dev/null +++ b/scripts/postinstall.sh @@ -0,0 +1,3 @@ +#!/bin/sh +systemctl start cronn +systemctl enable cronn diff --git a/scripts/preremove.sh b/scripts/preremove.sh new file mode 100644 index 0000000..66f90ee --- /dev/null +++ b/scripts/preremove.sh @@ -0,0 +1,3 @@ +#!/bin/sh +systemctl stop cronn +systemctl disable cronn