GODT-2078: Launcher inception.

This commit is contained in:
Jakub
2022-11-11 07:58:54 +01:00
parent bbcb7ad980
commit b1eefd6c85
2 changed files with 46 additions and 34 deletions

View File

@ -55,6 +55,8 @@ const (
) )
func main() { //nolint:funlen func main() { //nolint:funlen
logrus.SetLevel(logrus.DebugLevel)
l := logrus.WithField("launcher_version", constants.Version)
reporter := sentry.NewReporter(appName, constants.Version, useragent.New()) reporter := sentry.NewReporter(appName, constants.Version, useragent.New())
crashHandler := crash.NewHandler(reporter.ReportException) crashHandler := crash.NewHandler(reporter.ReportException)
@ -62,58 +64,69 @@ func main() { //nolint:funlen
locationsProvider, err := locations.NewDefaultProvider(filepath.Join(constants.VendorName, configName)) locationsProvider, err := locations.NewDefaultProvider(filepath.Join(constants.VendorName, configName))
if err != nil { if err != nil {
logrus.WithError(err).Fatal("Failed to get locations provider") l.WithError(err).Fatal("Failed to get locations provider")
} }
locations := locations.New(locationsProvider, configName) locations := locations.New(locationsProvider, configName)
logsPath, err := locations.ProvideLogsPath() logsPath, err := locations.ProvideLogsPath()
if err != nil { if err != nil {
logrus.WithError(err).Fatal("Failed to get logs path") l.WithError(err).Fatal("Failed to get logs path")
} }
crashHandler.AddRecoveryAction(logging.DumpStackTrace(logsPath)) crashHandler.AddRecoveryAction(logging.DumpStackTrace(logsPath))
if err := logging.Init(logsPath); err != nil { if err := logging.Init(logsPath); err != nil {
logrus.WithError(err).Fatal("Failed to setup logging") l.WithError(err).Fatal("Failed to setup logging")
} }
logging.SetLevel(os.Getenv("VERBOSITY")) logging.SetLevel(os.Getenv("VERBOSITY"))
updatesPath, err := locations.ProvideUpdatesPath() updatesPath, err := locations.ProvideUpdatesPath()
if err != nil { if err != nil {
logrus.WithError(err).Fatal("Failed to get updates path") l.WithError(err).Fatal("Failed to get updates path")
} }
key, err := crypto.NewKeyFromArmored(updater.DefaultPublicKey) key, err := crypto.NewKeyFromArmored(updater.DefaultPublicKey)
if err != nil { if err != nil {
logrus.WithError(err).Fatal("Failed to create new verification key") l.WithError(err).Fatal("Failed to create new verification key")
} }
kr, err := crypto.NewKeyRing(key) kr, err := crypto.NewKeyRing(key)
if err != nil { if err != nil {
logrus.WithError(err).Fatal("Failed to create new verification keyring") l.WithError(err).Fatal("Failed to create new verification keyring")
} }
versioner := versioner.New(updatesPath) versioner := versioner.New(updatesPath)
exeToLaunch := guiName
args := os.Args[1:]
if inCLIMode(args) {
exeToLaunch = exeName
}
exe, err := getPathToUpdatedExecutable(exeToLaunch, versioner, kr, reporter)
if err != nil {
if exe, err = getFallbackExecutable(exeToLaunch, versioner); err != nil {
logrus.WithError(err).Fatal("Failed to find any launchable executable")
}
}
launcher, err := os.Executable() launcher, err := os.Executable()
if err != nil { if err != nil {
logrus.WithError(err).Fatal("Failed to determine path to launcher") logrus.WithError(err).Fatal("Failed to determine path to launcher")
} }
l = l.WithField("launcher_path", launcher)
args := os.Args[1:]
exe, err := getPathToUpdatedExecutable(filepath.Base(launcher), versioner, kr, reporter)
if err != nil {
exeToLaunch := guiName
if inCLIMode(args) {
exeToLaunch = exeName
}
l = l.WithField("exe_to_launch", exeToLaunch)
l.WithError(err).Info("No more updates found, looking up bridge executable")
path, err := versioner.GetExecutableInDirectory(exeToLaunch, filepath.Dir(launcher))
if err != nil {
l.WithError(err).Fatal("No executable in launcher directory")
}
exe = path
}
l = l.WithField("exe_path", exe)
args, wait, mainExe := findAndStripWait(args) args, wait, mainExe := findAndStripWait(args)
if wait { if wait {
waitForProcessToFinish(mainExe) waitForProcessToFinish(mainExe)
@ -134,7 +147,7 @@ func main() { //nolint:funlen
} }
if err != nil { if err != nil {
logrus.WithError(err).Fatal("Failed to launch") l.WithError(err).Fatal("Failed to launch")
} }
} }
@ -193,11 +206,11 @@ func findAndStripWait(args []string) ([]string, bool, string) {
func getPathToUpdatedExecutable( func getPathToUpdatedExecutable(
name string, name string,
versioner *versioner.Versioner, ver *versioner.Versioner,
kr *crypto.KeyRing, kr *crypto.KeyRing,
reporter *sentry.Reporter, reporter *sentry.Reporter,
) (string, error) { ) (string, error) {
versions, err := versioner.ListVersions() versions, err := ver.ListVersions()
if err != nil { if err != nil {
return "", errors.Wrap(err, "failed to list available versions") return "", errors.Wrap(err, "failed to list available versions")
} }
@ -208,7 +221,11 @@ func getPathToUpdatedExecutable(
} }
for _, version := range versions { for _, version := range versions {
vlog := logrus.WithField("version", version) vlog := logrus.WithFields(logrus.Fields{
"version": constants.Version,
"check_version": version,
"name": name,
})
if err := version.VerifyFiles(kr); err != nil { if err := version.VerifyFiles(kr); err != nil {
vlog.WithError(err).Error("Files failed verification and will be removed") vlog.WithError(err).Error("Files failed verification and will be removed")
@ -241,17 +258,6 @@ func getPathToUpdatedExecutable(
return "", errors.New("no available newer versions") return "", errors.New("no available newer versions")
} }
func getFallbackExecutable(name string, versioner *versioner.Versioner) (string, error) {
logrus.Info("Searching for fallback executable")
launcher, err := os.Executable()
if err != nil {
return "", errors.Wrap(err, "failed to determine path to launcher")
}
return versioner.GetExecutableInDirectory(name, filepath.Dir(launcher))
}
// waitForProcessToFinish waits until the process with the given path is finished. // waitForProcessToFinish waits until the process with the given path is finished.
func waitForProcessToFinish(exePath string) { func waitForProcessToFinish(exePath string) {
for { for {

View File

@ -17,6 +17,12 @@
package versioner package versioner
import "strings"
func getExeName(name string) string { func getExeName(name string) string {
if strings.HasSuffix(name, ".exe") {
return name
}
return name + ".exe" return name + ".exe"
} }