Skip to content

Instantly share code, notes, and snippets.

@Preetam
Last active May 7, 2016 05:20
Show Gist options
  • Save Preetam/512992bc8551f232df43e28d4d5792c2 to your computer and use it in GitHub Desktop.
Save Preetam/512992bc8551f232df43e28d4d5792c2 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"log"
"strings"
"github.com/Preetam/libab/go/ab"
"github.com/digitalocean/godo"
"golang.org/x/oauth2"
)
type tokenSource string
func (s tokenSource) Token() (*oauth2.Token, error) {
return &oauth2.Token{AccessToken: string(s)}, nil
}
var node *ab.Node
var client *godo.Client
var dropletID int
var floatingIP string
type handler struct{}
func (h handler) OnAppend(round uint64, commit uint64, data string) {}
func (h handler) OnCommit(round uint64, commit uint64) {}
func (h handler) LostLeadership() {}
func (h handler) OnLeaderChange(leaderID uint64) {}
func (h handler) GainedLeadership() {
client.FloatingIPActions.Assign(floatingIP, dropletID)
}
func main() {
flag.IntVar(&dropletID, "droplet-id", 0, "droplet ID")
addr := flag.String("addr", "", "listen address")
clusterSize := flag.Int("cluster-size", 3, "cluster size")
peers := flag.String("peers", "", "comma-separated list of peers")
flag.StringVar(&floatingIP, "floating-ip", "", "floating IP address")
token := flag.String("token", "", "DigitalOcean token")
flag.Parse()
var err error
node, err = ab.NewNode(uint64(dropletID), *addr, handler{}, *clusterSize)
if err != nil {
log.Fatal(err)
}
for _, peer := range strings.Split(*peers, ",") {
node.AddPeer(peer)
}
oauthClient := oauth2.NewClient(oauth2.NoContext,
tokenSource(*token))
client = godo.NewClient(oauthClient)
node.Run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment