github.com/robszumski/deis@v1.4.1/logger/main.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"os/signal"
     7  	"syscall"
     8  	"time"
     9  
    10  	"github.com/coreos/go-etcd/etcd"
    11  
    12  	"github.com/deis/deis/logger/syslogd"
    13  )
    14  
    15  const (
    16  	timeout time.Duration = 10 * time.Second
    17  	ttl     time.Duration = timeout * 2
    18  )
    19  
    20  func main() {
    21  	host := getopt("HOST", "127.0.0.1")
    22  
    23  	etcdPort := getopt("ETCD_PORT", "4001")
    24  	etcdPath := getopt("ETCD_PATH", "/deis/logs")
    25  
    26  	externalPort := getopt("EXTERNAL_PORT", "514")
    27  
    28  	client := etcd.NewClient([]string{"http://" + host + ":" + etcdPort})
    29  
    30  	// Wait for terminating signal
    31  	exitChan := make(chan os.Signal, 2)
    32  	cleanupChan := make(chan bool)
    33  	signal.Notify(exitChan, syscall.SIGTERM, syscall.SIGINT)
    34  
    35  	go syslogd.Listen(exitChan, cleanupChan)
    36  
    37  	go publishService(client, host, etcdPath, externalPort, uint64(ttl.Seconds()))
    38  
    39  	// Wait for the proper shutdown of the syslog server before exit
    40  	<-cleanupChan
    41  }
    42  
    43  func publishService(client *etcd.Client, host string, etcdPath string, externalPort string, ttl uint64) {
    44  	for {
    45  		setEtcd(client, etcdPath+"/host", host, ttl)
    46  		setEtcd(client, etcdPath+"/port", externalPort, ttl)
    47  		time.Sleep(timeout)
    48  	}
    49  }
    50  
    51  func setEtcd(client *etcd.Client, key, value string, ttl uint64) {
    52  	_, err := client.Set(key, value, ttl)
    53  	if err != nil {
    54  		log.Println(err)
    55  	}
    56  }
    57  
    58  func getopt(name, dfault string) string {
    59  	value := os.Getenv(name)
    60  	if value == "" {
    61  		value = dfault
    62  	}
    63  	return value
    64  }