<commit_before>package main

import (
	"flag"
	"log"
	"net"
	"os"
	"strconv"
	"strings"

	//"github.com/hashicorp/serf/client"
	"github.com/hashicorp/serf/command"
	"github.com/hashicorp/serf/command/agent"
	"github.com/hashicorp/serf/serf"
)

var (
	c                          = serf.DefaultConfig()
	mode           instaceMode = SlaveMode
	masterAddress  string
	listenAddress  string // this is the interface that serf runs on
	rpcAddress     string // this is the interface that serf's RPC runs on (localhost:7373)
	name           string
	defaultName, _ = os.Hostname()
	rpcAuthKey     = ""
)

type instaceMode int

const (
	MasterMode instaceMode = iota
	SlaveMode
	DeployMode
)

func init() {
	flag.StringVar(&masterAddress, "master", "mymaster.company.net:7946", "Join the cluster by coordinating with this master")
	flag.StringVar(&listenAddress, "listen", "localhost:7946", "Listen on the address for serf communication")
	flag.StringVar(&rpcAddress, "rpc", "localhost:7373", "RPC address of the serfbort master")
	flag.StringVar(&name, "name", defaultName, "Name to use in serf protocol")
	flag.Parse()
}

func main() {
	c.Tags = map[string]string{
		"role": "web",
		"env":  "dev",
	}
	c.NodeName = name

	fields := strings.Split(listenAddress, ":")
	if len(fields) != 2 {
		log.Fatalf("-listen requires host:port! %s is not valid", listenAddress)
	}
	bindAddr := fields[0]
	bindPort, err := strconv.Atoi(fields[1])
	if err != nil {
		log.Fatalf("Unable to parse %s into port", fields[1])
	}
	c.MemberlistConfig.BindAddr = bindAddr
	c.MemberlistConfig.BindPort = bindPort
	//TODO advertiseaddr and advertiseport
	//c.MemberlistConfig.AdvertiseAddr = x
	//c.MemberlistConfig.AdvertisePort = y

	evtCh := make(chan serf.Event, 64)
	c.EventCh = evtCh

	args := flag.Args()
	if len(args) > 0 {
		switch args[0] {
		case "master":
			mode = MasterMode
		case "deploy":
			mode = DeployMode
		default:
			mode = SlaveMode
		}
	}

	log.Print("Running...")
	switch mode {
	case SlaveMode:
		log.Print("Starting node")
		s := joinCluster()
		defer leaveCluster()
		slave := Slave{s, evtCh}
		slave.Run()
	case MasterMode:
		log.Printf("Starting master on %s", listenAddress)
		s := joinCluster()
		defer leaveCluster()
		master := Master{s, evtCh}

		log.Printf("Starting master RPC listener on %s", rpcAddress)
		//TODO we should create an agent with agent.Create instead of this!
		logOutput := os.Stdout
		logWriter := agent.NewLogWriter(123)
		a := agent.Agent{}
		rpcListener, err := net.Listen("tcp", rpcAddress)
		if err != nil {
			log.Fatal("Error starting RPC listener: %s", err)
		}
		agent.NewAgentIPC(&a, rpcAuthKey, rpcListener, logOutput, logWriter)

		master.Run()
	case DeployMode:
		/*
			clientCfg := client.Config{
				Addr:    listenAddress,
				AuthKey: "",
			} */
		//rpcclient, err := client.NewRPCClient(listenAddress)
		rpcclient, err := command.RPCClient(rpcAddress, rpcAuthKey)
		if err != nil {
			log.Fatalf("Unable to connect to master at %s: %s", rpcAddress, err)
		}

		log.Printf("Sending event to cluster...")
		err = rpcclient.UserEvent("deploy", []byte{}, false)
		if err != nil {
			log.Fatal(err)<commit_msg>fuck, deploy is causing a panic<commit_after>package main

import (
	"flag"
	"log"
	"net"
	"os"
	"strconv"
	"strings"

	//"github.com/hashicorp/serf/client"
	"github.com/hashicorp/serf/command"
	"github.com/hashicorp/serf/command/agent"
	"github.com/hashicorp/serf/serf"
)

var (
	c                          = serf.DefaultConfig()
	mode           instaceMode = SlaveMode
	masterAddress  string
	listenAddress  string // this is the interface that serf runs on
	rpcAddress     string // this is the interface that serf's RPC runs on (localhost:7373)
	name           string
	defaultName, _ = os.Hostname()
	rpcAuthKey     = ""
)

type instaceMode int

const (
	MasterMode instaceMode = iota
	SlaveMode
	DeployMode
)

func init() {
	flag.StringVar(&masterAddress, "master", "localhost:7946", "Join the cluster by coordinating with this master")
	flag.StringVar(&listenAddress, "listen", "localhost:7946", "Listen on the address for serf communication")
	flag.StringVar(&rpcAddress, "rpc", "localhost:7373", "RPC address of the serfbort master")
	flag.StringVar(&name, "name", defaultName, "Name to use in serf protocol")
	flag.Parse()
}

func main() {
	c.Tags = map[string]string{
		"role": "web",
		"env":  "dev",
	}
	c.NodeName = name

	fields := strings.Split(listenAddress, ":")
	if len(fields) != 2 {
		log.Fatalf("-listen requires host:port! %s is not valid", listenAddress)
	}
	bindAddr := fields[0]
	bindPort, err := strconv.Atoi(fields[1])
	if err != nil {
		log.Fatalf("Unable to parse %s into port", fields[1])
	}
	c.MemberlistConfig.BindAddr = bindAddr
	c.MemberlistConfig.BindPort = bindPort
	//TODO advertiseaddr and advertiseport
	//c.MemberlistConfig.AdvertiseAddr = x
	//c.MemberlistConfig.AdvertisePort = y

	evtCh := make(chan serf.Event, 64)
	c.EventCh = evtCh

	args := flag.Args()
	if len(args) > 0 {
		switch args[0] {
		case "master":
			mode = MasterMode
		case "deploy":
			mode = DeployMode
		default:
			mode = SlaveMode
		}
	}

	log.Print("Running...")
	switch mode {
	case SlaveMode:
		log.Print("Starting node")
		s := joinCluster()
		defer leaveCluster()
		slave := Slave{s, evtCh}
		slave.Run()
	case MasterMode:
		log.Printf("Starting master on %s", listenAddress)
		s := joinCluster()
		defer leaveCluster()
		master := Master{s, evtCh}

		log.Printf("Starting master RPC listener on %s", rpcAddress)
		//Create(agentConf *Config, conf *serf.Config, logOutput io.Writer) (*Agent, error)
		logOutput := os.Stdout
		logWriter := agent.NewLogWriter(123)
		a, err := agent.Create(agent.DefaultConfig(), c, logWriter)
		if err != nil {
			log.Fatalf("Unable to create agent: %s", err)
		}
		//TODO we should create an agent with agent.Create instead of this!
		//a := agent.Agent{}
		rpcListener, err := net.Listen("tcp", rpcAddress)
		if err != nil {
			log.Fatal("Error starting RPC listener: %s", err)
		}
		ipc := agent.NewAgentIPC(a, rpcAuthKey, rpcListener, logOutput, logWriter)
		log.Printf("Running IPC: %s", ipc)

		master.Run()
	case DeployMode:
		/*
			clientCfg := client.Config{
				Addr:    listenAddress,
				AuthKey: "",
			} */
		//rpcclient, err := client.NewRPCClient(listenAddress)
		rpcclient, err := command.RPCClient(rpcAddress, rpcAuthKey)
		if err != nil {
			log.Fatalf("Unable to connect to master at %s: %s", rpcAddress, err)
		}

		log.Printf("Sending event to cluster...")
		err = rpcclient.UserEvent("deploy", []byte("fuck"), false)
		if err != nil {
			log.Fatal(err)
{'fraction_non_alphanumeric': 0.13820890983731185, 'fraction_numerical': 0.00821043028736506, 'mean_word_length': 3.7019299499642604, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1702299', 'n_tokens_mistral': 2466, 'n_tokens_neox': 2301, 'n_words': 599}