github.com/jiasir/deis@v1.12.2/logger/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"log"
     6  	"os"
     7  	"os/signal"
     8  	"strconv"
     9  	"syscall"
    10  	"time"
    11  
    12  	"github.com/deis/deis/logger/configurer"
    13  	"github.com/deis/deis/logger/publisher"
    14  	"github.com/deis/deis/logger/storage"
    15  	"github.com/deis/deis/logger/syslogish"
    16  	"github.com/deis/deis/logger/weblog"
    17  )
    18  
    19  var (
    20  	// TODO: When semver permits us to do so, many of these flags should probably be phased out in
    21  	// favor of just using environment variables.  Fewer avenues of configuring this component means
    22  	// less confusion.
    23  	logAddr       = flag.String("log-addr", "0.0.0.0", "bind address for the logger")
    24  	logHost       = flag.String("log-host", getopt("HOST", "127.0.0.1"), "address of the host running logger")
    25  	logPort       = flag.Int("log-port", 514, "bind port for the logger")
    26  	enablePublish = flag.Bool("enable-publish", false, "enable publishing to service discovery")
    27  	webAddr       = flag.String("web-addr", "0.0.0.0", "bind address for the web service")
    28  	webPort       = flag.Int("web-port", 8088, "bind port for the web service")
    29  	// Support legacy flag names even though the variable names have been changed for improved
    30  	// clarity
    31  	etcdHost        = flag.String("publish-host", getopt("HOST", "127.0.0.1"), "service discovery hostname")
    32  	etcdPort        = flag.String("publish-port", getopt("ETCD_PORT", "4001"), "service discovery port")
    33  	etcdPath        = flag.String("publish-path", getopt("ETCD_PATH", "/deis/logs"), "path to publish host/port information")
    34  	configInterval  = flag.Int("config-interval", 10, "config interval in seconds")
    35  	publishInterval = flag.Int("publish-interval", 10, "publish interval in seconds")
    36  	publishTTL      int
    37  )
    38  
    39  func init() {
    40  	flag.StringVar(&storage.LogRoot, "log-root", "/data/logs", "log path to store logs")
    41  	// Support a legacy behavior that that allows default drain uri to be specified using a drain-uri
    42  	// flag.
    43  	flag.StringVar(&configurer.DefaultDrainURI, "drain-uri", "", "default drainURI, once set in etcd, this has no effect.")
    44  	flag.Parse()
    45  	// Set the default value for this AFTER the proper value of *publishInterval has been
    46  	// established, since publishTTL should be twice the publishInterval by default.
    47  	flag.IntVar(&publishTTL, "publish-ttl", *publishInterval*2, "publish TTL in seconds")
    48  	// Now reparse flags in case the default publishTTL is overriden by a flag.
    49  	flag.Parse()
    50  }
    51  
    52  func main() {
    53  	syslogishServer, err := syslogish.NewServer(*logAddr, *logPort)
    54  	if err != nil {
    55  		log.Fatal("Error creating syslogish server", err)
    56  	}
    57  	weblogServer, err := weblog.NewServer(*webAddr, *webPort, syslogishServer)
    58  	if err != nil {
    59  		log.Fatal("Error creating weblog server", err)
    60  	}
    61  	etcdPortNum, err := strconv.Atoi(*etcdPort)
    62  	if err != nil {
    63  		log.Fatalf("Invalid port specified for etcd server.  '%s' is not an integer.", *etcdPort)
    64  	}
    65  	configurer, err := configurer.NewConfigurer(*etcdHost, etcdPortNum, *etcdPath, *configInterval,
    66  		syslogishServer)
    67  	if err != nil {
    68  		log.Fatal("Error creating configurer", err)
    69  	}
    70  
    71  	configurer.Start()
    72  
    73  	// Give configurer time to run once so we know syslogishServer is ready to rock
    74  	time.Sleep(time.Duration(*configInterval+1) * time.Second)
    75  
    76  	syslogishServer.Listen()
    77  	weblogServer.Listen()
    78  
    79  	if *enablePublish {
    80  		publisher, err := publisher.NewPublisher(*etcdHost, etcdPortNum, *etcdPath, *publishInterval,
    81  			publishTTL, *logHost, *logPort)
    82  		if err != nil {
    83  			log.Fatal("Error creating publisher", err)
    84  		}
    85  		publisher.Start()
    86  	}
    87  
    88  	log.Println("deis-logger running")
    89  
    90  	// No cleanup is needed upon termination.  The signal to reopen log files (after hypothetical
    91  	// logroation, for instance), if applicable, is the only signal we'll care about.  Our main loop
    92  	// will just wait for that signal.
    93  	reopen := make(chan os.Signal, 1)
    94  	signal.Notify(reopen, syscall.SIGUSR1)
    95  
    96  	for {
    97  		<-reopen
    98  		if err := syslogishServer.ReopenLogs(); err != nil {
    99  			log.Fatal("Error reopening logs", err)
   100  		}
   101  	}
   102  }
   103  
   104  func getopt(name, dfault string) string {
   105  	value := os.Getenv(name)
   106  	if value == "" {
   107  		value = dfault
   108  	}
   109  	return value
   110  }