Skip to content

Commit a8d58b7

Browse files
author
Stefano Torresi
authored
Merge pull request #102 from stefanotorresi/feature/obs-automated-releases
OBS automated releases
2 parents ee986cc + 71cafd0 commit a8d58b7

File tree

9 files changed

+223
-117
lines changed

9 files changed

+223
-117
lines changed

.ci/gh_release_to_obs_changeset.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
import json
5+
import sys
6+
import textwrap
7+
from urllib import request
8+
import urllib.error
9+
from datetime import datetime
10+
from datetime import timezone
11+
import tempfile
12+
13+
parser = argparse.ArgumentParser(description="Add a GitHub release to an RPM changelog", usage=argparse.SUPPRESS)
14+
parser.add_argument("repo", help="GitHub repository (owner/name)")
15+
parser.add_argument("-t", "--tag", help="A specific Git tag to get; if none, latest will be used")
16+
parser.add_argument("-a", "--author", help="The author of the RPM changelog entry")
17+
parser.add_argument("-f", "--file", help="Prepend the new changelog entry to file instead of printing in stdout")
18+
19+
if len(sys.argv) == 1:
20+
parser.print_help(sys.stderr)
21+
sys.exit(1)
22+
23+
args = parser.parse_args()
24+
25+
releaseSegment = f"/tags/{args.tag}" if args.tag else "/latest"
26+
url = f'https://api.github.com/repos/{args.repo}/releases{releaseSegment}'
27+
28+
try:
29+
response = request.urlopen(url)
30+
except urllib.error.HTTPError as error:
31+
print(f"GitHub API responded with a {error.code} error!", file=sys.stderr)
32+
print("Url:", url, file=sys.stderr)
33+
print("Response:", json.dumps(json.load(error), indent=4), file=sys.stderr, sep="\n")
34+
sys.exit(1)
35+
36+
release = json.load(response)
37+
38+
releaseDate = datetime.strptime(release['published_at'], "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc)
39+
40+
with tempfile.TemporaryFile("r+") as temp:
41+
print("-------------------------------------------------------------------", file=temp)
42+
43+
print(f"{releaseDate.strftime('%c')} {releaseDate.strftime('%Z')}", end="", file=temp)
44+
if args.author:
45+
print(f" - {args.author}", end="", file=temp)
46+
print("\n", file=temp)
47+
48+
print(f"- Release {args.tag}", end="", file=temp)
49+
if release['name'] and release['name'] != args.tag:
50+
print(f" - {release['name']}", end="", file=temp)
51+
print("\n", file=temp)
52+
53+
if release['body']:
54+
print(textwrap.indent(release['body'], " "), file=temp, end="\n\n")
55+
temp.seek(0)
56+
57+
if args.file:
58+
with open(args.file, "r") as prev:
59+
old = prev.read()
60+
with open(args.file, "w") as new:
61+
for line in temp:
62+
new.write(line)
63+
new.write(old)
64+
sys.exit(0)
65+
66+
print(temp.read())

.ci/init_osc_creds.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# this file is intended to be run inside a shap/continuous_deliver container
5+
# see https://github.com/arbulu89/continuous-delivery
6+
7+
source /scripts/utils.sh
8+
9+
OSCRC_FILE=${OSCRC_FILE:=/root/.config/osc/oscrc}
10+
11+
check_user

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ ha_cluster_exporter
1616

1717
# we don't use vendoring
1818
/vendor
19+
/build

.travis.yml

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
sudo: false
21
language: go
32
go:
4-
- 1.11.x
5-
env:
6-
- GO111MODULE=on
3+
- "stable"
4+
5+
cache:
6+
directories:
7+
- $GOPATH/pkg/mod
78

89
jobs:
910
include:
@@ -13,6 +14,26 @@ jobs:
1314
- stage: Test
1415
name: Unit tests
1516
script: make test
16-
- stage: Release
17-
script: make release
17+
- stage: Build & release
1818
if: tag IS present
19+
services:
20+
- docker
21+
env:
22+
- VERSION=$TRAVIS_TAG
23+
script:
24+
- make -j4 build-all
25+
deploy:
26+
provider: releases
27+
api_key: $GITHUB_OAUTH_TOKEN
28+
file_glob: yes
29+
file: build/bin/*
30+
name: $TRAVIS_TAG
31+
skip_cleanup: true
32+
on:
33+
tags: true
34+
after_deploy:
35+
- |
36+
docker run --rm -t -v "$(pwd):/package" -w /package \
37+
-e OBS_USER -e OBS_PASS -e OBS_PROJECT -e OBS_PACKAGE -e VERSION -e REPOSITORY=$TRAVIS_REPO_SLUG \
38+
shap/continuous_deliver \
39+
bash -c ".ci/init_osc_creds.sh && make obs-commit"

Makefile

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1-
default: clean static-checks test build post-build
1+
# this is the what ends up in the RPM "Version" field and it is also used as suffix for the built binaries
2+
# it can be arbitrary for local builds, but it if you want to commit to OBS it must correspond to a Git tag with an associated GitHub release
3+
VERSION ?= dev
4+
5+
# we only use this to comply with RPM changelog conventions at SUSE
6+
7+
8+
# you can customize any of the following to build forks
9+
OBS_PROJECT ?= server:monitoring
10+
OBS_PACKAGE ?= prometheus-ha_cluster_exporter
11+
REPOSITORY ?= clusterlabs/ha_cluster_exporter
12+
13+
# the Go archs we crosscompile to
14+
ARCHS ?= amd64 arm64 ppc64le s390x
15+
16+
default: clean mod-tidy fmt vet-check test build
217

318
download:
419
go mod download
520
go mod verify
621

7-
build: ha_cluster_exporter
8-
ha_cluster_exporter: download
9-
go fmt
10-
go build .
22+
build: amd64
23+
24+
build-all: clean-bin $(ARCHS)
25+
26+
$(ARCHS):
27+
@mkdir -p build/bin
28+
CGO_ENABLED=0 GOOS=linux GOARCH=$@ go build -trimpath -ldflags "-s -w -X main.version=$(VERSION)" -o build/bin/ha_cluster_exporter-$(VERSION)-$@
1129

1230
install:
1331
go install
@@ -17,6 +35,12 @@ static-checks: vet-check fmt-check
1735
vet-check: download
1836
go vet .
1937

38+
fmt:
39+
go fmt
40+
41+
mod-tidy:
42+
go mod tidy
43+
2044
fmt-check:
2145
.ci/go_lint.sh
2246

@@ -28,13 +52,31 @@ coverage.out:
2852
go test -cover -coverprofile=coverage.out
2953
go tool cover -html=coverage.out
3054

31-
clean:
55+
clean: clean-bin clean-obs
3256
go clean
3357
rm -f coverage.out
3458

35-
post-build:
36-
go mod tidy
59+
clean-bin:
60+
rm -rf build/bin
61+
62+
clean-obs:
63+
rm -rf build/obs
64+
65+
obs-workdir: clean-obs
66+
@mkdir -p build/obs/$(OBS_PACKAGE)
67+
osc checkout $(OBS_PROJECT)/$(OBS_PACKAGE) -o build/obs
68+
cp ha_cluster_exporter.spec build/obs/$(OBS_PACKAGE).spec
69+
cp -r doc LICENSE *.md ha_cluster_exporter.service build/obs/$(OBS_PACKAGE)/
70+
cp build/bin/* build/obs/$(OBS_PACKAGE)/
71+
mv build/obs/$(OBS_PACKAGE)/ha_cluster_exporter-$(VERSION)-arm64 build/obs/$(OBS_PACKAGE)/ha_cluster_exporter-$(VERSION)-aarch64
72+
mv build/obs/$(OBS_PACKAGE)/ha_cluster_exporter-$(VERSION)-amd64 build/obs/$(OBS_PACKAGE)/ha_cluster_exporter-$(VERSION)-x86_64
73+
sed -i 's/%%VERSION%%/$(VERSION)/' build/obs/$(OBS_PACKAGE).spec
74+
rm build/obs/*.tar.gz
75+
tar -cvzf build/obs/$(OBS_PACKAGE)-$(VERSION).tar.gz -C build/obs/$(OBS_PACKAGE) .
76+
.ci/gh_release_to_obs_changeset.py $(REPOSITORY) -a $(AUTHOR) -t $(VERSION) -f build/obs/$(OBS_PACKAGE).changes || true
3777

38-
release:
78+
obs-commit: obs-workdir
79+
cd build/obs; osc addremove
80+
cd build/obs; osc commit -m "Automated $(VERSION) release"
3981

40-
.PHONY: default download install static-checks vet-check fmt-check test clean release post-build
82+
.PHONY: default download install static-checks vet-check fmt fmt-check mod-tidy test clean clean-bin clean-obs build build-all obs-commit obs-workdir $(ARCHS)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Please refer to the example [YAML configuration](ha_cluster_exporter.yaml) for m
9090

9191
### systemd integration
9292

93-
A [systemd unit file](prometheus-ha_cluster_exporter.service) is provided with the RPM packages. You can enable and start it as usual:
93+
A [systemd unit file](ha_cluster_exporter.service) is provided with the RPM packages. You can enable and start it as usual:
9494

9595
```
9696
systemctl --now enable prometheus-ha_cluster_exporter
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[Unit]
2-
Description=Prometheus exporter for ha_cluster metrics (pacemaker)
2+
Description=Prometheus exporter for Pacemaker HA clusters metrics
33
Wants=pacemaker.service
44
After=network.target pacemaker.service
55

ha_cluster_exporter.spec

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Name: prometheus-ha_cluster_exporter
2+
# Version will be processed by the set_version source service
3+
Version: %%VERSION%%
4+
Release: 0
5+
License: Apache-2.0
6+
Summary: Prometheus exporter for Pacemaker HA clusters metrics
7+
Group: System/Monitoring
8+
Url: https://github.com/ClusterLabs/ha_cluster_exporter
9+
Source: %{name}-%{version}.tar.gz
10+
ExclusiveArch: aarch64 x86_64 ppc64le s390x
11+
Provides: ha_cluster_exporter = %{version}-%{release}
12+
Provides: prometheus(ha_cluster_exporter) = %{version}-%{release}
13+
BuildRoot: %{_tmppath}/%{name}-%{version}-build
14+
15+
# Make sure that the binary is not getting stripped.
16+
%undefine _build_create_debug
17+
%define __arch_install_post export NO_BRP_STRIP_DEBUG=true
18+
19+
%description
20+
Prometheus exporter for Pacemaker HA clusters metrics
21+
22+
%prep
23+
%setup -q -c # unpack project sources
24+
25+
%define shortname ha_cluster_exporter
26+
27+
%install
28+
29+
# Install the binary.
30+
install -D -m 0755 %{shortname}-%{version}-%{_arch} "%{buildroot}%{_bindir}/%{shortname}"
31+
32+
# Install the systemd unit
33+
install -D -m 0644 %{shortname}.service %{buildroot}%{_unitdir}/%{name}.service
34+
35+
# Install compat wrapper for legacy init systems
36+
install -Dd -m 0755 %{buildroot}%{_sbindir}
37+
ln -s /usr/sbin/service %{buildroot}%{_sbindir}/rc%{name}
38+
39+
%pre
40+
%service_add_pre %{name}.service
41+
42+
%post
43+
%service_add_post %{name}.service
44+
45+
%preun
46+
%service_del_preun %{name}.service
47+
48+
%postun
49+
%service_del_postun %{name}.service
50+
51+
%files
52+
%defattr(-,root,root)
53+
%doc *.md
54+
%doc doc/*
55+
%if 0%{?suse_version} >= 1500
56+
%license LICENSE
57+
%else
58+
%doc LICENSE
59+
%endif
60+
%{_bindir}/%{shortname}
61+
%{_unitdir}/%{name}.service
62+
%{_sbindir}/rc%{name}
63+
64+
%changelog

prometheus-ha_cluster_exporter.spec

Lines changed: 0 additions & 99 deletions
This file was deleted.

0 commit comments

Comments
 (0)