github.com/whamcloud/lemur@v0.0.0-20190827193804-4655df8a52af/cmd/lhsmd/main.go (about)

     1  // Copyright (c) 2018 DDN. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"flag"
     9  	"log"
    10  	"os"
    11  	"os/signal"
    12  	"syscall"
    13  	"time"
    14  
    15  	"github.com/intel-hpdd/go-metrics-influxdb"
    16  	"github.com/pkg/errors"
    17  	"github.com/rcrowley/go-metrics"
    18  
    19  	"golang.org/x/net/context"
    20  
    21  	"github.com/intel-hpdd/lemur/cmd/lhsmd/agent"
    22  	"github.com/intel-hpdd/lemur/pkg/fsroot"
    23  	"github.com/intel-hpdd/logging/alert"
    24  	"github.com/intel-hpdd/logging/audit"
    25  	"github.com/intel-hpdd/logging/debug"
    26  	"github.com/intel-hpdd/go-lustre/hsm"
    27  
    28  	// Register the supported transports
    29  	_ "github.com/intel-hpdd/lemur/cmd/lhsmd/transport/grpc"
    30  )
    31  
    32  func init() {
    33  	flag.Var(debug.FlagVar())
    34  }
    35  
    36  func interruptHandler(once func()) {
    37  	c := make(chan os.Signal, 1)
    38  	signal.Notify(c, os.Interrupt, syscall.SIGTERM)
    39  
    40  	go func() {
    41  		stopping := false
    42  		for sig := range c {
    43  			debug.Printf("signal received: %s", sig)
    44  			if !stopping {
    45  				stopping = true
    46  				once()
    47  			}
    48  		}
    49  	}()
    50  
    51  }
    52  
    53  func run(conf *agent.Config) error {
    54  	debug.Printf("current configuration:\n%v", conf.String())
    55  	if err := agent.ConfigureMounts(conf); err != nil {
    56  		return errors.Wrap(err, "Error while creating Lustre mountpoints")
    57  	}
    58  
    59  	if conf.InfluxDB != nil && conf.InfluxDB.URL != "" {
    60  		debug.Print("Configuring InfluxDB stats target")
    61  		go influxdb.InfluxDB(
    62  			metrics.DefaultRegistry, // metrics registry
    63  			time.Second*10,          // interval
    64  			conf.InfluxDB.URL,
    65  			conf.InfluxDB.DB,       // your InfluxDB database
    66  			conf.InfluxDB.User,     // your InfluxDB user
    67  			conf.InfluxDB.Password, // your InfluxDB password
    68  		)
    69  	}
    70  
    71  	client, err := fsroot.New(conf.AgentMountpoint())
    72  	if err != nil {
    73  		return errors.Wrap(err, "Could not get fs client")
    74  	}
    75  	as := hsm.NewActionSource(client.Root())
    76  
    77  	ct, err := agent.New(conf, client, as)
    78  	if err != nil {
    79  		return errors.Wrap(err, "Error creating agent")
    80  	}
    81  
    82  	interruptHandler(func() {
    83  		ct.Stop()
    84  	})
    85  
    86  	return errors.Wrap(ct.Start(context.Background()),
    87  		"Error in HsmAgent.Start()")
    88  }
    89  
    90  func main() {
    91  	flag.Parse()
    92  
    93  	if debug.Enabled() {
    94  		// Set this so that plugins can use it without needing
    95  		// to mess around with plugin args.
    96  		os.Setenv(debug.EnableEnvVar, "true")
    97  	}
    98  
    99  	// Setting the prefix helps us to track down deprecated calls to log.*
   100  	log.SetFlags(log.LstdFlags | log.Lshortfile)
   101  	log.SetOutput(audit.Writer().Prefix("DEPRECATED "))
   102  
   103  	conf := agent.ConfigInitMust()
   104  	err := run(conf)
   105  
   106  	// Ensure that we always clean up.
   107  	if err := agent.CleanupMounts(conf); err != nil {
   108  		alert.Warn(errors.Wrap(err, "Error while cleaning up Lustre mountpoints"))
   109  	}
   110  
   111  	if err != nil {
   112  		alert.Abort(err)
   113  	}
   114  }