Skip to content

Commit

Permalink
add amazon and hetzner support
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Mar 2, 2018
1 parent f8ff2ab commit 8918c86
Show file tree
Hide file tree
Showing 99 changed files with 5,410 additions and 2,212 deletions.
11 changes: 11 additions & 0 deletions .drone.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

set -e
set -x

go build \
-ldflags '-extldflags "-static"' \
-ldflags "-X main.version=${DRONE_TAG=latest}" \
-ldflags "-X main.commit=${DRONE_COMMIT_SHA}" \
-o release/linux/arm64/drone-autoscaler \
github.com/drone/autoscaler/cmd/drone-autoscaler
8 changes: 2 additions & 6 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@ pipeline:

build:
image: golang
commands:
- |
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags "-X main.version=${DRONE_TAG} -X main.commit=${DRONE_COMMIT_SHA}" \
-o release/linux/arm64/drone-autoscaler \
github.com/drone/autoscaler/cmd/drone-autoscaler
commands: sh .drone.sh

publish:
image: plugins/docker
repo: drone/autoscaler
auto_tag: true
secrets: [ docker_username, docker_password ]
when:
branch: master
event: [ push, tag ]
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
NOTES.md
release
vendor
.*.toml
*.sqlite
*.sqlite3
*.bak
*.out
*.db
Expand Down
12 changes: 6 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
FROM drone/ca-certs
EXPOSE 8080 80 443
VOLUME /data

ENV GODEBUG=netdns=go
ENV XDG_CACHE_HOME /var/lib/autoscaler
ENV DRONE_DATABASE_PATH /var/lib/autoscaler/snapshot.db
ENV GODEBUG netdns=go
ENV XDG_CACHE_HOME /data
ENV DATABASE_DRIVER sqlite3
ENV DATABASE_DATASOURCE /data/database.sqlite?cache=shared&mode=rwc&_busy_timeout=9999999

ADD release/linux/arm64/drone-autoscaler /bin/

EXPOSE 8080 80 443

ENTRYPOINT ["/bin/drone-autoscaler"]
47 changes: 32 additions & 15 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
branch = "master"
name = "github.com/bluele/slack"

[[constraint]]
name = "github.com/boltdb/bolt"
version = "1.3.1"

[[constraint]]
branch = "master"
name = "github.com/dchest/uniuri"
Expand All @@ -43,7 +39,7 @@

[[constraint]]
name = "github.com/drone/drone-go"
version = "0.8.4"
revision = "5b15e044e3275274bc54fc711d88ebf73e2061eb"

[[constraint]]
branch = "master"
Expand Down Expand Up @@ -100,3 +96,7 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/hetznercloud/hcloud-go"
version = "1.3.1"
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Additional Use Grant: Usage of the software is free for entities with both:
of domicile); and (b) less than (USD) $5 million in
aggregate debt and equity funding.

Change Date: 2021-01-01
Change Date: 2022-03-01 (yyyy-MM-dd)

Change License: BSD-3-Clause

Expand Down
47 changes: 27 additions & 20 deletions cmd/drone-autoscaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
"github.com/drone/autoscaler"
"github.com/drone/autoscaler/config"
"github.com/drone/autoscaler/drivers/digitalocean"
"github.com/drone/autoscaler/drivers/hetznercloud"
"github.com/drone/autoscaler/engine"
"github.com/drone/autoscaler/metrics"
"github.com/drone/autoscaler/scaler"
"github.com/drone/autoscaler/server"
"github.com/drone/autoscaler/slack"
"github.com/drone/autoscaler/store"
Expand All @@ -30,7 +31,9 @@ import (
"golang.org/x/oauth2"
"golang.org/x/sync/errgroup"

_ "github.com/go-sql-driver/mysql"
_ "github.com/joho/godotenv/autoload"
_ "github.com/mattn/go-sqlite3"
)

var (
Expand All @@ -41,8 +44,6 @@ var (

func main() {
conf := config.MustLoad()
metrics.MinPool(conf)
metrics.MaxPool(conf)
setupLogging(conf)

provider, err := setupProvider(conf)
Expand All @@ -55,27 +56,30 @@ func main() {
provider = metrics.ServerCreate(provider)
provider = metrics.ServerDelete(provider)

db, err := store.Connect(conf.Database.Driver, conf.Database.Datasource)
if err != nil {
log.Fatal().Err(err).
Msg("Cannot establish database connection")
}

servers := store.NewServerStore(db)
// instruments the provider with slack notifications
// instance creation and termination events.
if conf.Slack.Webhook != "" {
provider = slack.New(conf, provider)
servers = slack.New(conf, servers)
}

db := store.Must(conf.Database.Path)
servers := store.NewServerStore(db)

// instruments the store with prometheus metrics.
servers = metrics.ServerCount(servers)
defer db.Close()

client := setupClient(conf)

ascaler := &scaler.Scaler{
Client: client,
Config: conf,
Servers: servers,
Provider: provider,
}
enginex := engine.New(
client,
conf,
servers,
provider,
)

r := chi.NewRouter()
r.Use(hlog.NewHandler(log.Logger))
Expand All @@ -87,17 +91,17 @@ func main() {
r.Get("/metrics", server.HandleMetrics(conf.Prometheus.Token))
r.Get("/version", server.HandleVersion(source, version, commit))
r.Get("/healthz", server.HandleHealthz())
r.Get("/varz", server.HandleVarz(ascaler))
r.Get("/varz", server.HandleVarz(enginex))
r.Route("/api", func(r chi.Router) {
r.Use(server.CheckDrone(conf))

r.Post("/pause", server.HandleScalerPause(ascaler))
r.Post("/resume", server.HandleScalerResume(ascaler))
r.Post("/pause", server.HandleEnginePause(enginex))
r.Post("/resume", server.HandleEngineResume(enginex))
r.Get("/queue", server.HandleQueueList(client))
r.Get("/servers", server.HandleServerList(servers))
r.Post("/servers", server.HandleServerCreate(servers, provider, conf))
r.Post("/servers", server.HandleServerCreate(servers, conf))
r.Get("/servers/{name}", server.HandleServerFind(servers))
r.Delete("/servers/{name}", server.HandleServerDelete(servers, provider))
r.Delete("/servers/{name}", server.HandleServerDelete(servers))
})

//
Expand Down Expand Up @@ -134,7 +138,8 @@ func main() {
//

g.Go(func() error {
return scaler.Start(ctx, ascaler, conf.Interval)
enginex.Start(ctx)
return nil
})

if err := g.Wait(); err != nil {
Expand Down Expand Up @@ -185,6 +190,8 @@ func setupProvider(c config.Config) (autoscaler.Provider, error) {
switch {
case c.DigitalOcean.Token != "":
return digitalocean.FromConfig(c)
case c.HetznerCloud.Token != "":
return hetznercloud.FromConfig(c)
default:
return nil, errors.New("missing provider configuration")
}
Expand Down
13 changes: 12 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ type (
}

Database struct {
Path string `default:"snapshot.db"`
Driver string `default:"sqlite3"`
Datasource string `default:"database.sqlite?cache=shared&mode=rwc&_busy_timeout=9999999"`
}

Amazon struct {
Expand Down Expand Up @@ -93,5 +94,15 @@ type (
Project string
Tags []string
}

HetznerCloud struct {
Token string
Image string `default:"ubuntu-16.04"`
Datacenter string `default:"nbg1-dc3"`
SSHKey string `default:"/root/.ssh/id_rsa"`
SSHKeyID int `envconfig:"DRONE_HETZNERCLOUD_SSHKEY_ID"`
ServerType string `default:"cx11" envconfig:"DRONE_HETZNERCLOUD_TYPE"`
CloudConfig string
}
}
)
Loading

0 comments on commit 8918c86

Please sign in to comment.