mirror of
https://github.com/jangraefen/hcloud-pricing-exporter
synced 2024-11-10 05:54:15 +00:00
Fix build target and linting issues
This commit is contained in:
parent
816a6d02fc
commit
f92d993002
8 changed files with 35 additions and 49 deletions
27
.github/workflows/build.yml
vendored
27
.github/workflows/build.yml
vendored
|
@ -2,7 +2,7 @@ name: Build
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ master ]
|
branches: [ main ]
|
||||||
paths-ignore:
|
paths-ignore:
|
||||||
- README.md
|
- README.md
|
||||||
- LICENSE
|
- LICENSE
|
||||||
|
@ -18,7 +18,7 @@ jobs:
|
||||||
- name: Install Go
|
- name: Install Go
|
||||||
uses: actions/setup-go@v1
|
uses: actions/setup-go@v1
|
||||||
with:
|
with:
|
||||||
go-version: ^1.15
|
go-version: ^1.16
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v1
|
uses: actions/checkout@v1
|
||||||
- name: Install golangci-lint
|
- name: Install golangci-lint
|
||||||
|
@ -27,34 +27,16 @@ jobs:
|
||||||
- name: Run linters
|
- name: Run linters
|
||||||
run: |
|
run: |
|
||||||
export PATH=$PATH:$(go env GOPATH)/bin
|
export PATH=$PATH:$(go env GOPATH)/bin
|
||||||
|
go mod download
|
||||||
golangci-lint run
|
golangci-lint run
|
||||||
test:
|
|
||||||
name: Test
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
go-version: [ 1.15.x ]
|
|
||||||
platform: [ ubuntu-latest ]
|
|
||||||
runs-on: ${{ matrix.platform }}
|
|
||||||
needs: [ lint ]
|
|
||||||
steps:
|
|
||||||
- name: Install Go
|
|
||||||
if: success()
|
|
||||||
uses: actions/setup-go@v1
|
|
||||||
with:
|
|
||||||
go-version: ${{ matrix.go-version }}
|
|
||||||
- name: Checkout code
|
|
||||||
uses: actions/checkout@v1
|
|
||||||
- name: Run tests
|
|
||||||
run: go test -v -race -covermode=atomic ./...
|
|
||||||
|
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: [ test ]
|
|
||||||
steps:
|
steps:
|
||||||
- name: Install Go
|
- name: Install Go
|
||||||
uses: actions/setup-go@v1
|
uses: actions/setup-go@v1
|
||||||
with:
|
with:
|
||||||
go-version: ^1.15
|
go-version: ^1.16
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v1
|
uses: actions/checkout@v1
|
||||||
- name: Get dependencies
|
- name: Get dependencies
|
||||||
|
@ -64,6 +46,7 @@ jobs:
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
export GO111MODULE=on
|
export GO111MODULE=on
|
||||||
|
go mod download
|
||||||
GOOS=linux GOARCH=amd64 go build -o bin/hcloud-pricing-exporter-linux-amd64 main.go
|
GOOS=linux GOARCH=amd64 go build -o bin/hcloud-pricing-exporter-linux-amd64 main.go
|
||||||
GOOS=linux GOARCH=arm64 go build -o bin/hcloud-pricing-exporter-linux-arm64 main.go
|
GOOS=linux GOARCH=arm64 go build -o bin/hcloud-pricing-exporter-linux-arm64 main.go
|
||||||
GOOS=windows GOARCH=amd64 go build -o bin/hcloud-pricing-exporter-windows-amd64.exe main.go
|
GOOS=windows GOARCH=amd64 go build -o bin/hcloud-pricing-exporter-windows-amd64.exe main.go
|
||||||
|
|
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
|
@ -17,7 +17,7 @@ jobs:
|
||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.15
|
go-version: 1.16
|
||||||
- name: Run GoReleaser
|
- name: Run GoReleaser
|
||||||
uses: goreleaser/goreleaser-action@v2
|
uses: goreleaser/goreleaser-action@v2
|
||||||
with:
|
with:
|
||||||
|
|
|
@ -7,6 +7,7 @@ ADD . /app/
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Build the application
|
# Build the application
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go mod download
|
||||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -o run .
|
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -o run .
|
||||||
|
|
||||||
# Add the execution user
|
# Add the execution user
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"github.com/hetznercloud/hcloud-go/hcloud"
|
"github.com/hetznercloud/hcloud-go/hcloud"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PriceProvider provides easy access to current HCloud prices
|
// PriceProvider provides easy access to current HCloud prices.
|
||||||
type PriceProvider struct {
|
type PriceProvider struct {
|
||||||
Client *hcloud.Client
|
Client *hcloud.Client
|
||||||
pricing *hcloud.Pricing
|
pricing *hcloud.Pricing
|
||||||
|
|
|
@ -26,7 +26,7 @@ func (serverTraffic serverTraffic) Run(client *hcloud.Client) error {
|
||||||
for _, s := range servers {
|
for _, s := range servers {
|
||||||
location := s.Datacenter.Location
|
location := s.Datacenter.Location
|
||||||
|
|
||||||
additionalTraffic := s.OutgoingTraffic - s.IncludedTraffic
|
additionalTraffic := int(s.OutgoingTraffic) - int(s.IncludedTraffic)
|
||||||
if additionalTraffic < 0 {
|
if additionalTraffic < 0 {
|
||||||
serverTraffic.hourly.WithLabelValues(s.Name, location.Name, s.ServerType.Name).Set(0)
|
serverTraffic.hourly.WithLabelValues(s.Name, location.Name, s.ServerType.Name).Set(0)
|
||||||
serverTraffic.monthly.WithLabelValues(s.Name, location.Name, s.ServerType.Name).Set(0)
|
serverTraffic.monthly.WithLabelValues(s.Name, location.Name, s.ServerType.Name).Set(0)
|
||||||
|
|
|
@ -25,7 +25,7 @@ func (volume volume) Run(client *hcloud.Client) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range volumes {
|
for _, v := range volumes {
|
||||||
monthlyPrice := math.Ceil(float64(v.Size/sizeGB)) * volume.pricing.Volume()
|
monthlyPrice := math.Ceil(float64(v.Size)/sizeGB) * volume.pricing.Volume()
|
||||||
hourlyPrice := pricingPerHour(monthlyPrice)
|
hourlyPrice := pricingPerHour(monthlyPrice)
|
||||||
|
|
||||||
volume.hourly.WithLabelValues(v.Name, v.Location.Name, strconv.Itoa(v.Size)).Set(hourlyPrice)
|
volume.hourly.WithLabelValues(v.Name, v.Location.Name, strconv.Itoa(v.Size)).Set(hourlyPrice)
|
||||||
|
|
48
main.go
48
main.go
|
@ -19,21 +19,13 @@ const (
|
||||||
defaultFetchInterval = 1 * time.Minute
|
defaultFetchInterval = 1 * time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
func toScheduler(client *hcloud.Client, f func(*hcloud.Client) error) func() {
|
var (
|
||||||
return func() {
|
hcloudAPIToken string
|
||||||
if err := f(client); err != nil {
|
port uint
|
||||||
panic(err)
|
fetchInterval time.Duration
|
||||||
}
|
)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
var (
|
|
||||||
hcloudAPIToken string
|
|
||||||
port uint
|
|
||||||
fetchInterval time.Duration
|
|
||||||
)
|
|
||||||
|
|
||||||
|
func handleFlags() {
|
||||||
flag.StringVar(&hcloudAPIToken, "hcloud-token", "", "the token to authenticate against the HCloud API")
|
flag.StringVar(&hcloudAPIToken, "hcloud-token", "", "the token to authenticate against the HCloud API")
|
||||||
flag.UintVar(&port, "port", defaultPort, "the port that the exporter exposes its data on")
|
flag.UintVar(&port, "port", defaultPort, "the port that the exporter exposes its data on")
|
||||||
flag.DurationVar(&fetchInterval, "fetch-interval", defaultFetchInterval, "the interval between data fetching cycles")
|
flag.DurationVar(&fetchInterval, "fetch-interval", defaultFetchInterval, "the interval between data fetching cycles")
|
||||||
|
@ -47,19 +39,29 @@ func main() {
|
||||||
if hcloudAPIToken == "" {
|
if hcloudAPIToken == "" {
|
||||||
panic(fmt.Errorf("no API token for HCloud specified, but required"))
|
panic(fmt.Errorf("no API token for HCloud specified, but required"))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func toScheduler(client *hcloud.Client, f func(*hcloud.Client) error) func() {
|
||||||
|
return func() {
|
||||||
|
if err := f(client); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
handleFlags()
|
||||||
|
|
||||||
client := hcloud.NewClient(hcloud.WithToken(hcloudAPIToken))
|
client := hcloud.NewClient(hcloud.WithToken(hcloudAPIToken))
|
||||||
priceRepository := &fetcher.PriceProvider{Client: client}
|
priceRepository := &fetcher.PriceProvider{Client: client}
|
||||||
|
|
||||||
var (
|
floatingIP := fetcher.NewFloatingIP(priceRepository)
|
||||||
floatingIP = fetcher.NewFloatingIP(priceRepository)
|
loadBalancer := fetcher.NewLoadbalancer(priceRepository)
|
||||||
loadBalancer = fetcher.NewLoadbalancer(priceRepository)
|
server := fetcher.NewServer(priceRepository)
|
||||||
server = fetcher.NewServer(priceRepository)
|
serverBackup := fetcher.NewServerBackup(priceRepository)
|
||||||
serverBackup = fetcher.NewServerBackup(priceRepository)
|
serverTraffic := fetcher.NewServerTraffic(priceRepository)
|
||||||
serverTraffic = fetcher.NewServerTraffic(priceRepository)
|
snapshot := fetcher.NewSnapshot(priceRepository)
|
||||||
snapshot = fetcher.NewSnapshot(priceRepository)
|
volume := fetcher.NewVolume(priceRepository)
|
||||||
volume = fetcher.NewVolume(priceRepository)
|
|
||||||
)
|
|
||||||
|
|
||||||
scheduler.RunTaskAtInterval(toScheduler(client, floatingIP.Run), fetchInterval, 0)
|
scheduler.RunTaskAtInterval(toScheduler(client, floatingIP.Run), fetchInterval, 0)
|
||||||
scheduler.RunTaskAtInterval(toScheduler(client, loadBalancer.Run), fetchInterval, 0)
|
scheduler.RunTaskAtInterval(toScheduler(client, loadBalancer.Run), fetchInterval, 0)
|
||||||
|
|
Loading…
Reference in a new issue