Merge pull request #14 from gianarb/feature/autoscaler-ls

Add basic CLA and debugging informations
This commit is contained in:
Gianluca Arbezzano 2017-03-18 22:14:14 +00:00 committed by GitHub
commit 6e021fc164
10 changed files with 139 additions and 1 deletions

29
api/autoscaler-ls.go Normal file
View File

@ -0,0 +1,29 @@
package api
import (
"encoding/json"
"net/http"
"github.com/gianarb/orbiter/autoscaler"
)
type AutoscalerResponse struct {
Name string `json:"name"`
}
func AutoscalerList(scalers autoscaler.Autoscalers) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
l := []AutoscalerResponse{}
for n, _ := range scalers {
c := AutoscalerResponse{
Name: n,
}
l = append(l, c)
}
cc := &CollectionResponse{
Data: l,
}
b, _ := json.Marshal(cc)
w.Write(b)
}
}

5
api/common.go Normal file
View File

@ -0,0 +1,5 @@
package api
type CollectionResponse struct {
Data interface{} `json:"data"`
}

View File

@ -9,6 +9,7 @@ import (
func GetRouter(core core.Core, eventChannel chan *logrus.Entry) *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/handle/{autoscaler_name}/{service_name}", Handle(core.Autoscalers)).Methods("POST")
r.HandleFunc("/autoscaler", AutoscalerList(core.Autoscalers)).Methods("GET")
r.HandleFunc("/health", Health()).Methods("GET")
r.HandleFunc("/events", Events(eventChannel)).Methods("GET")
return r

View File

@ -4,6 +4,7 @@ import "github.com/Sirupsen/logrus"
type Provider interface {
Scale(string, int, bool) error
Name() string
}
type Autoscalers map[string]Autoscaler

40
cmd/autoscaler-ls.go Normal file
View File

@ -0,0 +1,40 @@
package cmd
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/Sirupsen/logrus"
)
type AutoscalerListCmd struct {
}
func (c *AutoscalerListCmd) Run(args []string) int {
r, err := http.Get(fmt.Sprintf("%s/autoscaler", os.Getenv("ORBITER_HOST")))
if err != nil {
logrus.Fatal(err)
return 1
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
logrus.Fatal(err)
return 1
}
fmt.Printf("%s\n\r", body)
return 0
}
func (c *AutoscalerListCmd) Help() string {
helpText := `
Usage: List of autoscalers currently enabled. `
return strings.TrimSpace(helpText)
}
func (r *AutoscalerListCmd) Synopsis() string {
return "List all autoscalers currently managed by orbiter."
}

42
cmd/system-events.go Normal file
View File

@ -0,0 +1,42 @@
package cmd
import (
"bufio"
"fmt"
"net/http"
"os"
"strings"
"github.com/Sirupsen/logrus"
)
type SystemEventsCmd struct {
}
func (c *SystemEventsCmd) Run(args []string) int {
r, err := http.Get(fmt.Sprintf("%s/events", os.Getenv("ORBITER_HOST")))
if err != nil {
logrus.Fatal(err)
return 1
}
defer r.Body.Close()
reader := bufio.NewReader(r.Body)
for {
line, err := reader.ReadBytes('\n')
if err != nil {
logrus.Fatal(err)
return 1
}
fmt.Printf("%s", line)
}
}
func (c *SystemEventsCmd) Help() string {
helpText := `
Usage: Listen to all the events fired by the daemon`
return strings.TrimSpace(helpText)
}
func (r *SystemEventsCmd) Synopsis() string {
return "Listen to all the events fired by the daemon."
}

10
main.go
View File

@ -18,7 +18,15 @@ func main() {
c.Commands = map[string]cli.CommandFactory{
"daemon": func() (cli.Command, error) {
return &cmd.DaemonCmd{eventChannel}, nil
return &cmd.DaemonCmd{
EventChannel: eventChannel,
}, nil
},
"autoscaler ls": func() (cli.Command, error) {
return &cmd.AutoscalerListCmd{}, nil
},
"system events": func() (cli.Command, error) {
return &cmd.SystemEventsCmd{}, nil
},
}

View File

@ -34,6 +34,10 @@ func NewDigitalOceanProvider(c map[string]string) (autoscaler.Provider, error) {
return p, nil
}
func (p DigitalOceanProvider) Name() string {
return "digitalocean"
}
func (p DigitalOceanProvider) Scale(serviceId string, target int, direction bool) error {
var wg sync.WaitGroup
responseChannel := make(chan response, target)

View File

@ -3,6 +3,10 @@ package provider
type FakeProvider struct {
}
func (f FakeProvider) Name() string {
return "fake"
}
func (f FakeProvider) Scale(serviceId string, target int, direction bool) error {
return nil
}

View File

@ -33,6 +33,10 @@ func NewSwarmProvider(c map[string]string) (autoscaler.Provider, error) {
}
func (p SwarmProvider) Name() string {
return "swarm"
}
func (p SwarmProvider) Scale(serviceId string, target int, direction bool) error {
ctx := context.Background()
service, _, err := p.dockerClient.ServiceInspectWithRaw(ctx, serviceId)