github.com/dustinrc/deis@v1.10.1-0.20150917223407-0894a5fb979e/mesos/pkg/boot/zookeeper/main/boot.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http"
     6  	"os"
     7  	"os/signal"
     8  	"strconv"
     9  	"strings"
    10  	"syscall"
    11  	"time"
    12  
    13  	"github.com/deis/deis/mesos/bindata/zookeeper"
    14  	"github.com/deis/deis/mesos/pkg/boot/zookeeper"
    15  	"github.com/deis/deis/mesos/pkg/confd"
    16  	"github.com/deis/deis/mesos/pkg/etcd"
    17  	logger "github.com/deis/deis/mesos/pkg/log"
    18  	oswrapper "github.com/deis/deis/mesos/pkg/os"
    19  	"github.com/deis/deis/version"
    20  )
    21  
    22  var (
    23  	etcdPath   = oswrapper.Getopt("ETCD_PATH", "/zookeeper/nodes")
    24  	log        = logger.New()
    25  	signalChan = make(chan os.Signal, 1)
    26  )
    27  
    28  func main() {
    29  	host := oswrapper.Getopt("HOST", "127.0.0.1")
    30  	etcdPort := oswrapper.Getopt("ETCD_PORT", "4001")
    31  	etcdCtlPeers := oswrapper.Getopt("ETCD_PEERS", "127.0.0.1:"+etcdPort)
    32  	etcdURL := etcd.GetHTTPEtcdUrls(host+":"+etcdPort, etcdCtlPeers)
    33  	etcdClient := etcd.NewClient(etcdURL)
    34  
    35  	etcd.Mkdir(etcdClient, etcdPath)
    36  
    37  	log.Infof("boot version [%v]", version.Version)
    38  	log.Info("zookeeper: starting...")
    39  
    40  	zookeeper.CheckZkMappingInFleet(etcdPath, etcdClient, etcdURL)
    41  
    42  	// we need to write the file /opt/zookeeper-data/data/myid with the id of this node
    43  	os.MkdirAll("/opt/zookeeper-data/data", 0640)
    44  	zkID := etcd.Get(etcdClient, etcdPath+"/"+host+"/id")
    45  	ioutil.WriteFile("/opt/zookeeper-data/data/myid", []byte(zkID), 0640)
    46  
    47  	zkServer := &zookeeper.ZkServer{
    48  		Stdout: os.Stdout,
    49  		Stderr: os.Stderr,
    50  	}
    51  
    52  	signal.Notify(signalChan,
    53  		syscall.SIGHUP,
    54  		syscall.SIGINT,
    55  		syscall.SIGKILL,
    56  		syscall.SIGTERM,
    57  		syscall.SIGQUIT,
    58  		os.Interrupt,
    59  	)
    60  
    61  	// Wait for a signal and exit
    62  	exitChan := make(chan int)
    63  	go func() {
    64  		for {
    65  			s := <-signalChan
    66  			log.Debugf("Signal received: %v", s)
    67  			switch s {
    68  			case syscall.SIGTERM:
    69  				exitChan <- 0
    70  			case syscall.SIGQUIT:
    71  				exitChan <- 0
    72  			case syscall.SIGKILL:
    73  				exitChan <- 1
    74  			default:
    75  				exitChan <- 1
    76  			}
    77  		}
    78  	}()
    79  
    80  	// wait for confd to run once and install initial templates
    81  	confd.WaitForInitialConf(getConfdNodes(host, etcdCtlPeers, 4001), 10*time.Second)
    82  
    83  	params := make(map[string]string)
    84  	params["HOST"] = host
    85  	if log.Level.String() == "debug" {
    86  		params["DEBUG"] = "true"
    87  	}
    88  
    89  	err := oswrapper.RunScript("pkg/boot/zookeeper/bash/add-node.bash", params, bindata.Asset)
    90  	if err != nil {
    91  		log.Printf("command finished with error: %v", err)
    92  	}
    93  
    94  	if err := zkServer.Start(); err != nil {
    95  		panic(err)
    96  	}
    97  
    98  	log.Info("zookeeper: running...")
    99  
   100  	go func() {
   101  		log.Debugf("starting pprof http server in port 6060")
   102  		http.ListenAndServe("localhost:6060", nil)
   103  	}()
   104  
   105  	code := <-exitChan
   106  	log.Debugf("execution terminated with exit code %v", code)
   107  
   108  	log.Debugf("executing pre shutdown script")
   109  	err = oswrapper.RunScript("pkg/boot/zookeeper/bash/remove-node.bash", params, bindata.Asset)
   110  	if err != nil {
   111  		log.Printf("command finished with error: %v", err)
   112  	}
   113  
   114  	log.Info("stopping zookeeper node")
   115  	zkServer.Stop()
   116  }
   117  
   118  func getConfdNodes(host, etcdCtlPeers string, port int) []string {
   119  	result := []string{host + ":" + strconv.Itoa(port)}
   120  
   121  	if etcdCtlPeers != "127.0.0.1" {
   122  		hosts := strings.Split(etcdCtlPeers, ",")
   123  		result = []string{}
   124  		for _, _host := range hosts {
   125  			result = append(result, _host)
   126  		}
   127  	}
   128  
   129  	return result
   130  }