Skip to content
This repository has been archived by the owner on Mar 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #235 from prashanthpai/main-cleanup
Browse files Browse the repository at this point in the history
Main() cleanup
  • Loading branch information
kshlm authored Feb 1, 2017
2 parents 5ed497a + 2fbbc93 commit 25fa3e4
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 50 deletions.
4 changes: 2 additions & 2 deletions commands/peers/peer-rpc-svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ func (p *PeerService) ExportAndStoreETCDConfig(nc netctx.Context, c *EtcdConfigR
newEtcdConfig.Dir = newEtcdConfig.Name + ".etcd"
}

// Gracefully stop embedded etcd server
err = etcdmgmt.DestroyEmbeddedEtcd()
// Gracefully stop embedded etcd server and remove local etcd data
err = etcdmgmt.DestroyEmbeddedEtcd(true)
if err != nil {
opRet = -1
opError = fmt.Sprintf("Error stopping embedded etcd server.")
Expand Down
26 changes: 15 additions & 11 deletions etcdmgmt/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func StartEmbeddedEtcd(cfg *embed.Config) error {
}

// DestroyEmbeddedEtcd will gracefully shut down the embedded etcd server and
// deletes the etcd data directory.
func DestroyEmbeddedEtcd() error {
// optionally deletes the etcd data directory.
func DestroyEmbeddedEtcd(deleteData bool) error {

etcdInstance.Lock()
defer etcdInstance.Unlock()
Expand All @@ -66,17 +66,21 @@ func DestroyEmbeddedEtcd() error {
etcdInstance.etcd = nil
log.Info("Etcd embedded server is stopped.")

err := os.RemoveAll(etcdConfig.Dir)
if err != nil {
return errors.New("Could not delete etcd data dir")
}
if deleteData {
err := os.RemoveAll(etcdConfig.Dir)
if err != nil {
return errors.New("Could not delete etcd data dir")
}

err = os.RemoveAll(etcdConfig.WalDir)
if err != nil {
return errors.New("Could not delete etcd WAL dir")
}
err = os.RemoveAll(etcdConfig.WalDir)
if err != nil {
return errors.New("Could not delete etcd WAL dir")
}

os.Remove(EtcdConfigFile)

os.Remove(EtcdConfigFile)
log.Info("Etcd data dir, WAL dir and config file removed")
}

return nil
}
7 changes: 0 additions & 7 deletions gdctx/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

log "github.com/Sirupsen/logrus"
"github.com/pborman/uuid"
config "github.com/spf13/viper"
)

// Various version constants that will be used by GD2
Expand Down Expand Up @@ -47,19 +46,13 @@ func initOpVersion() {
}

func doInit() {
log.Debug("Initializing GlusterD context")

utils.InitDir(config.GetString("localstatedir"))

initOpVersion()

// When glusterd is started for the first time, we will have Restart set to
// false. That is when we'll have to initialize prefixes by passing true to
// InitStore(). On subsequent restarts of glusterd, we would want to skip
// initializing prefixes by passing false to InitStore()
InitStore(!Restart)

log.Debug("Initialized GlusterD context")
}

// Init initializes the GlusterD context. This should be called once before doing anything else.
Expand Down
4 changes: 2 additions & 2 deletions gdctx/myuuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func InitMyUUID() uuid.UUID {
}

u := uuid.Parse(string(ubytes))
log.WithField("myuuid", u.String()).Info("restored uuid")
log.WithField("uuid", u.String()).Info("Found existing UUID")
Restart = true

return u
Expand All @@ -45,7 +45,7 @@ func genMyUUID() uuid.UUID {
u := uuid.NewRandom()

writeMyUUIDFile(u)
log.WithField("myuuid", u.String()).Info("generated new MyUUID")
log.WithField("uuid", u.String()).Info("Generated new UUID")
return u
}

Expand Down
55 changes: 28 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,74 +19,69 @@ import (

func main() {

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

// Parse flags and handle version and logging before continuing
// Parse command-line arguments
parseFlags()

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

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

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

// Read in config
// Read config file
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")
workdir := config.GetString("workdir")
if err := os.Chdir(workdir); err != nil {
log.WithError(err).Fatalf("Failed to change working directory to %s", workdir)
}

// 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"))
// Create directories inside workdir - run dir, logdir etc
createDirectories()

// Generate UUID if it doesn't exist
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.")
log.WithError(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.")
log.WithError(err).Fatal("Could not start embedded etcd server")
}

// Initialize op version and etcd store
gdctx.Init()

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

// Start all servers (rest, peerrpc, sunrpc) managed by suture supervisor
super := initGD2Supervisor()
super.ServeBackground()

super.Add(servers.New())

// Use the main goroutine as signal handling loop
sigCh := make(chan os.Signal)
signal.Notify(sigCh)
for s := range sigCh {
log.WithField("signal", s).Debug("Signal recieved")
log.WithField("signal", s).Debug("Signal received")
switch s {
case os.Interrupt:
log.WithField("signal", s).Info("Recieved SIGTERM. Stopping GlusterD.")
etcdmgmt.DestroyEmbeddedEtcd()
log.Info("Received SIGTERM. Stopping GlusterD")
// Stop embedded etcd server, but don't wipe local etcd data
etcdmgmt.DestroyEmbeddedEtcd(false)
super.Stop()
log.Info("Terminating GlusterD.")
log.Info("Stopped GlusterD")
return
default:
continue
Expand All @@ -98,7 +93,13 @@ func initGD2Supervisor() *suture.Supervisor {
superlogger := func(msg string) {
log.WithField("supervisor", "gd2-main").Println(msg)
}
super := suture.New("gd2-main", suture.Spec{Log: superlogger})
return suture.New("gd2-main", suture.Spec{Log: superlogger})
}

return super
func createDirectories() {
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"))
}
2 changes: 1 addition & 1 deletion servers/sunrpc/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func registerProgram(server *rpc.Server, program Program, port int) error {
"progver": program.Version(),
})

logger.Info("registering sunrpc program")
logger.Debug("registering sunrpc program")

// NOTE: This will throw some benign log messages complaining about
// signatures of methods in Program interface. rpc.Server.Register()
Expand Down

0 comments on commit 25fa3e4

Please sign in to comment.