Example #1
0
// ExportAndStoreETCDConfig will store & export etcd environment variable along
// with storing etcd configuration
func (p *PeerService) ExportAndStoreETCDConfig(nc netctx.Context, c *EtcdConfigReq) (*PeerGenericResp, error) {
	var opRet int32
	var opError string

	newEtcdConfig, err := etcdmgmt.GetEtcdConfig(false)
	if err != nil {
		opRet = -1
		opError = fmt.Sprintf("Could not fetch etcd configuration.")
		goto Out
	}

	if !c.DeletePeer {
		// This is an add peer request containing information about
		// which cluster to join.
		newEtcdConfig.InitialCluster = c.InitialCluster
		newEtcdConfig.ClusterState = c.ClusterState
		newEtcdConfig.Name = c.EtcdName
		newEtcdConfig.Dir = newEtcdConfig.Name + ".etcd"
	}

	// Gracefully stop embedded etcd server
	err = etcdmgmt.DestroyEmbeddedEtcd()
	if err != nil {
		opRet = -1
		opError = fmt.Sprintf("Error stopping embedded etcd server.")
		log.WithField("Error", err).Error("Error stopping embedded etcd server.")
		goto Out
	}

	// Start embedded etcd server
	err = etcdmgmt.StartEmbeddedEtcd(newEtcdConfig)
	if err != nil {
		opRet = -1
		opError = fmt.Sprintf("Could not start embedded etcd server.")
		log.WithField("Error", err).Error("Could not start embedded etcd server.")
		goto Out
	}

	if c.DeletePeer {
		// After being detached from the cluster, this glusterd instance
		// now should get back to clean slate i.e state of a single node
		// standalone cluster.
		gdctx.InitStore(true)
		peer.AddSelfDetails()
	} else {
		// Store the etcd config in a file for use during restarts.

		err = etcdmgmt.StoreEtcdConfig(newEtcdConfig)
		if err != nil {
			opRet = -1
			opError = fmt.Sprintf("Error storing etcd configuration.")
			goto Out
		}
	}

Out:
	reply := &PeerGenericResp{
		OpRet:   opRet,
		OpError: opError,
	}

	return reply, nil
}
Example #2
0
func main() {

	// Set IP and hostname once.
	gdctx.SetHostnameAndIP()

	// Parse flags and handle version and logging before continuing
	parseFlags()

	showvers, _ := flag.CommandLine.GetBool("version")
	if showvers {
		dumpVersionInfo()
		return
	}

	logLevel, _ := flag.CommandLine.GetString("loglevel")
	initLog(logLevel, os.Stderr)

	log.WithField("pid", os.Getpid()).Info("GlusterD starting")

	// Read in config
	confFile, _ := flag.CommandLine.GetString("config")
	initConfig(confFile)

	// Change to working directory before continuing
	if e := os.Chdir(config.GetString("workdir")); e != nil {
		log.WithError(e).Fatalf("failed to change working directory")
	}

	// TODO: This really should go into its own function.
	utils.InitDir(config.GetString("localstatedir"))
	utils.InitDir(config.GetString("rundir"))
	utils.InitDir(config.GetString("logdir"))
	utils.InitDir(path.Join(config.GetString("rundir"), "gluster"))
	utils.InitDir(path.Join(config.GetString("logdir"), "glusterfs/bricks"))

	gdctx.MyUUID = gdctx.InitMyUUID()

	// Start embedded etcd server
	etcdConfig, err := etcdmgmt.GetEtcdConfig(true)
	if err != nil {
		log.WithField("Error", err).Fatal("Could not fetch config options for etcd.")
	}
	err = etcdmgmt.StartEmbeddedEtcd(etcdConfig)
	if err != nil {
		log.WithField("Error", err).Fatal("Could not start embedded etcd server.")
	}

	gdctx.Init()

	for _, c := range commands.Commands {
		gdctx.Rest.SetRoutes(c.Routes())
		c.RegisterStepFuncs()
	}

	// Store self information in the store if GlusterD is coming up for
	// first time
	if !gdctx.Restart {
		peer.AddSelfDetails()
	}

	// Start listening for incoming RPC requests
	err = server.StartListener()
	if err != nil {
		log.Fatal("Could not register RPC listener. Aborting")
	}

	sigCh := make(chan os.Signal)
	signal.Notify(sigCh)
	go func() {
		for s := range sigCh {
			log.WithField("signal", s).Debug("Signal recieved")
			switch s {
			case os.Interrupt:
				log.WithField("signal", s).Info("Recieved SIGTERM. Stopping GlusterD.")
				gdctx.Rest.Stop()
				etcdmgmt.DestroyEmbeddedEtcd()
				server.StopServer()
				log.Info("Termintaing GlusterD.")
				os.Exit(0)

			default:
				continue
			}
		}
	}()

	// Start GlusterD REST server
	err = gdctx.Rest.Listen()
	if err != nil {
		log.Fatal("Could not start GlusterD Rest Server. Aborting.")
	}

}