github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/cmd/supervisor/supervisor.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package main
    19  
    20  import (
    21  	"flag"
    22  	"fmt"
    23  	"os"
    24  	"path/filepath"
    25  
    26  	"github.com/mysteriumnetwork/node/metadata"
    27  	"github.com/mysteriumnetwork/node/supervisor/daemon/transport"
    28  	"github.com/mysteriumnetwork/node/supervisor/logconfig"
    29  	"github.com/mysteriumnetwork/node/supervisor/svflags"
    30  	"github.com/rs/zerolog/log"
    31  
    32  	"github.com/mysteriumnetwork/node/supervisor/config"
    33  	"github.com/mysteriumnetwork/node/supervisor/daemon"
    34  	"github.com/mysteriumnetwork/node/supervisor/install"
    35  )
    36  
    37  func ensureInstallFlags() {
    38  	if *svflags.FlagUid == "" {
    39  		fmt.Println("Error: required flags were not set")
    40  		flag.Usage()
    41  		os.Exit(1)
    42  	}
    43  }
    44  
    45  func main() {
    46  	svflags.Parse()
    47  
    48  	if *svflags.FlagVersion {
    49  		fmt.Println(metadata.VersionAsString())
    50  		os.Exit(0)
    51  	}
    52  
    53  	logOpts := logconfig.LogOptions{
    54  		LogLevel: *svflags.FlagLogLevel,
    55  		Filepath: *svflags.FlagLogFilePath,
    56  	}
    57  	if err := logconfig.Configure(logOpts); err != nil {
    58  		log.Fatal().Err(err).Msg("Failed to configure logging")
    59  	}
    60  
    61  	if *svflags.FlagInstall {
    62  		ensureInstallFlags()
    63  		path, err := thisPath()
    64  		if err != nil {
    65  			log.Fatal().Err(err).Msg("Failed to determine supervisor's path")
    66  		}
    67  		cfg := config.Config{
    68  			Uid: *svflags.FlagUid,
    69  		}
    70  		if err := cfg.Write(); err != nil {
    71  			log.Fatal().Err(err).Msg("Failed to write config")
    72  		}
    73  		options := install.Options{
    74  			SupervisorPath: path,
    75  		}
    76  		log.Info().Msgf("Installing supervisor with options: %#v", options)
    77  		if err = install.Install(options); err != nil {
    78  			log.Fatal().Err(err).Msg("Failed to install supervisor")
    79  		}
    80  		log.Info().Msg("Supervisor installed")
    81  	} else if *svflags.FlagUninstall {
    82  		log.Info().Msg("Uninstalling supervisor")
    83  		if err := install.Uninstall(); err != nil {
    84  			log.Fatal().Err(err).Msg("Failed to uninstall supervisor")
    85  		}
    86  		log.Info().Msg("Supervisor uninstalled")
    87  	} else {
    88  		log.Info().Msg("Running myst supervisor daemon")
    89  		cfg, err := config.Read()
    90  		if err != nil {
    91  			log.Fatal().Err(err).Msg("Failed to read configuration")
    92  		}
    93  		supervisor := daemon.New()
    94  		if err := supervisor.Start(transport.Options{WinService: *svflags.FlagWinService, Uid: cfg.Uid}); err != nil {
    95  			log.Fatal().Err(err).Msg("Error running supervisor")
    96  		}
    97  	}
    98  }
    99  
   100  func thisPath() (string, error) {
   101  	thisExec, err := os.Executable()
   102  	if err != nil {
   103  		return "", err
   104  	}
   105  	thisPath, err := filepath.Abs(thisExec)
   106  	if err != nil {
   107  		return "", err
   108  	}
   109  	return thisPath, nil
   110  }