Пример #1
0
func (app *App) GetRemoteDescriptor() (remoteDescriptor descriptors.AppDescriptor) {
	if app.remoteDescriptorCached {
		return app.remoteDescriptor
	}

	app.remoteDescriptorCached = true

	bootDescriptor := app.bootDescriptor
	localDescriptor := app.GetLocalDescriptor()

	var remoteDescriptorURL *url.URL
	var err error

	if localDescriptor != nil {
		if localDescriptor.IsSkipUpdateCheck() {
			log.Notice("Skipping update check, as requested by the local descriptor")
			return nil
		}

		remoteDescriptorURL, err = localDescriptor.GetRemoteFileURL(localDescriptor.GetDescriptorFileName())
	} else {
		remoteDescriptorURL, err = bootDescriptor.GetRemoteFileURL(bootDescriptor.GetDescriptorFileName())
	}

	if err != nil {
		log.Warning(err.Error())
		return nil
	}

	log.Notice("The remote descriptor's URL is: %v", remoteDescriptorURL)

	log.Info("Retrieving the remote descriptor...")
	remoteDescriptorBytes, err := caravel.RetrieveFromURL(remoteDescriptorURL)
	if err != nil {
		log.Warning(err.Error())
		return nil
	}
	log.Notice("Remote descriptor retrieved")

	log.Info("Opening the remote descriptor...")
	remoteDescriptor, err = descriptors.NewAppDescriptorFromBytes(remoteDescriptorBytes)
	if err != nil {
		log.Warning(err.Error())
		return nil
	}
	log.Notice("Remote descriptor ready")

	log.Debug("The remote descriptor is: %#v", remoteDescriptor)

	app.remoteDescriptor = remoteDescriptor

	return remoteDescriptor
}
Пример #2
0
func (app *App) GetReferenceDescriptor() (referenceDescriptor descriptors.AppDescriptor, err error) {
	if app.referenceDescriptorCached {
		return app.referenceDescriptor, nil
	}

	app.referenceDescriptorCached = true

	localDescriptor := app.GetLocalDescriptor()
	remoteDescriptor := app.GetRemoteDescriptor()

	if remoteDescriptor == nil && localDescriptor == nil {
		return nil, fmt.Errorf("Cannot run the application: it is not installed and cannot be downloaded")
	}

	if remoteDescriptor == nil {
		if localDescriptor.IsSkipUpdateCheck() {
			log.Info("The remote descriptor is missing as requested, so the local descriptor will be used")
		} else {
			log.Warning("The remote descriptor is missing, so the local descriptor will be used")
		}
		app.referenceDescriptor = localDescriptor
	} else if localDescriptor == nil {
		log.Notice("The local descriptor is missing, so the remote descriptor will be used")
		app.referenceDescriptor = remoteDescriptor
	} else if remoteDescriptor.GetAppVersion().NewerThan(localDescriptor.GetAppVersion()) {
		log.Notice("Switching to the remote descriptor, as it is more recent")
		app.referenceDescriptor = remoteDescriptor
	} else {
		log.Notice("Keeping the local descriptor, as the remote descriptor is NOT more recent")
		app.referenceDescriptor = localDescriptor
	}

	return app.referenceDescriptor, nil
}
Пример #3
0
func (app *App) GetLocalDescriptor() (localDescriptor descriptors.AppDescriptor) {
	if app.localDescriptorCached {
		return app.localDescriptor
	}

	app.localDescriptorCached = true

	if !caravel.FileExists(app.localDescriptorPath) {
		log.Notice("The local descriptor is missing")
		return nil
	}

	log.Notice("The local descriptor has been found! Opening it...")
	localDescriptor, err := descriptors.NewAppDescriptorFromPath(app.localDescriptorPath)
	if err != nil {
		log.Warning(err.Error())
		return nil
	}
	log.Notice("Local descriptor ready")

	log.Debug("The local descriptor is: %#v", localDescriptor)

	app.localDescriptor = localDescriptor

	return localDescriptor
}
Пример #4
0
func backgroundOrchestrator(launcher launchers.Launcher, bootDescriptorPath string, guiOutcomeChannel chan guiOutcomeStruct) {
	outcome := runEngineWithGtk(launcher, bootDescriptorPath)
	userInterface := outcome.userInterface
	err := outcome.err

	log.SetCallback(func(level logging.Level, message string) {})
	log.Debug("Result returned by the background routine. Is UI available? %v", userInterface != nil)

	if err != nil {
		log.Warning("Err is: %v", err)

		if userInterface != nil {
			switch err.(type) {

			case *engine.ExecutionCanceled:
				break

			default:
				userInterface.ShowError(err.Error())
			}
		}
	}

	log.Debug("Now programmatically quitting GTK")
	gtk.MainQuit()

	guiOutcomeChannel <- outcome
}
Пример #5
0
func (app *App) installPackage(
	packageName string,
	settings config.Settings,
	progressCallback caravel.RetrievalProgressCallback) (err error) {

	remoteDescriptor := app.GetRemoteDescriptor()

	packageURL, err := remoteDescriptor.GetRemoteFileURL(packageName)
	if err != nil {
		return err
	}

	log.Debug("Creating package temp file...")
	packageTempFile, err := ioutil.TempFile(os.TempDir(), packageName)
	if err != nil {
		return err
	}
	packageTempFilePath := packageTempFile.Name()
	log.Debug("Package temp file created '%v'", packageTempFilePath)

	defer func() {
		packageTempFile.Close()

		log.Debug("Deleting package temp file: '%v'", packageTempFilePath)
		tempFileRemovalErr := os.Remove(packageTempFilePath)
		if tempFileRemovalErr != nil {
			log.Warning("Could not remove the package temp file! '%v'", tempFileRemovalErr)
		} else {
			log.Notice("Package temp file removed")
		}
	}()

	log.Info("Retrieving package: %v", packageURL)
	err = caravel.RetrieveChunksFromURL(packageURL, packageTempFile, settings.GetBufferSize(), progressCallback)
	if err != nil {
		return err
	}
	log.Notice("Package retrieved")

	log.Debug("Closing the package temp file...")
	packageTempFile.Close()
	if err != nil {
		return err
	}
	log.Notice("Package temp file closed")

	err = os.MkdirAll(app.filesDirectory, 0700)
	if err != nil {
		return err
	}

	log.Info("Extracting the package. Skipping levels: %v...", remoteDescriptor.GetSkipPackageLevels())
	err = caravel.ExtractZipSkipLevels(packageTempFilePath, app.filesDirectory, remoteDescriptor.GetSkipPackageLevels())
	if err != nil {
		return err
	}
	log.Notice("Package extracted")

	return nil
}
Пример #6
0
func (app *App) GetActualIconPath(launcher launchers.Launcher) string {
	referenceDescriptor, err := app.GetReferenceDescriptor()
	if err != nil {
		log.Warning("Error while retrieving the reference descriptor: %v", err)
		return launcher.GetIconPath()
	}

	referenceIconPath := referenceDescriptor.GetIconPath()

	if referenceIconPath != "" {
		return filepath.Join(app.filesDirectory, referenceIconPath)
	}

	return launcher.GetIconPath()
}
Пример #7
0
func (app *App) SaveReferenceDescriptor() (referenceDescriptorSaved bool) {
	referenceDescriptor, err := app.GetReferenceDescriptor()
	if err != nil {
		log.Warning("Cannot save the reference descriptor: %v", err)
		return false
	}

	log.Info("Saving the reference descriptor as the local descriptor...")
	referenceDescriptorBytes, err := referenceDescriptor.GetBytes()
	if err != nil {
		log.Error("Could not serialize the reference descriptor: %v", err)
		return false
	}

	err = ioutil.WriteFile(app.localDescriptorPath, referenceDescriptorBytes, 0600)
	if err != nil {
		log.Error("Could not save the reference descriptor: %v", err)
		return false
	}

	log.Notice("Reference descriptor saved")
	return true
}
Пример #8
0
func StartGUI(launcher launchers.Launcher, bootDescriptorPath string) (err error) {
	log.Debug("Initializing GTK...")
	gtkui.InitGTK()
	log.Debug("GTK initialized")

	guiOutcomeChannel := make(chan guiOutcomeStruct)
	defer close(guiOutcomeChannel)

	go backgroundOrchestrator(launcher, bootDescriptorPath, guiOutcomeChannel)

	log.Debug("Starting GTK main loop...")
	gtk.Main()

	log.SetCallback(func(level logging.Level, message string) {})
	log.Debug("GTK main loop terminated")

	select {
	case guiOutcome := <-guiOutcomeChannel:
		log.Debug("Outcome retrieved from the GUI channel")

		if guiOutcome.userInterface != nil && guiOutcome.userInterface.IsClosedByUser() {
			return &engine.ExecutionCanceled{}
		}

		err = guiOutcome.err
		if err != nil {
			log.Warning("Err is: %v", err)
			return err
		}

		log.Notice("OK")
		return nil
	default:
		log.Debug("The user has manually closed the program")
		return &engine.ExecutionCanceled{}
	}
}
Пример #9
0
func exitWithCancel() {
	log.Warning("*** EXECUTION CANCELED ***")
	os.Exit(v3.ExitCodeCanceled)
}
Пример #10
0
func GetGitHubDescriptorInfo(baseURL *url.URL, descriptorFileName string) *GitHubDescriptorInfo {
	projectParams := latestVersionURLRegex.FindStringSubmatch(baseURL.String())
	if projectParams == nil {
		log.Debug("The URL does not reference a 'latest' release on GitHub")
		return nil
	}
	log.Debug("The URL references a 'latest' release on GitHub")

	gitHubUser := projectParams[1]
	gitHubRepo := projectParams[2]

	apiLatestVersionURL, err := url.Parse(fmt.Sprintf(
		apiLatestVersionURLTemplate,
		gitHubUser,
		gitHubRepo))
	if err != nil {
		log.Warning(err.Error())
		return nil
	}

	log.Debug("Calling GitHub's API, at '%v'...", apiLatestVersionURL)

	apiResponseBytes, err := caravel.RetrieveFromURL(apiLatestVersionURL)
	if err != nil {
		log.Warning(err.Error())
		return nil
	}
	log.Debug("API returned OK")

	log.Debug("Deserializing the API response...")
	var latestVersionResponse latestVersionResponse
	err = json.Unmarshal(apiResponseBytes, &latestVersionResponse)
	if err != nil {
		log.Warning(err.Error())
		return nil
	}
	log.Debug("Response correctly deserialized: %#v", latestVersionResponse)

	log.Debug("Now processing the response fields...")

	result := &GitHubDescriptorInfo{}

	for _, asset := range latestVersionResponse.Assets {
		if asset.Name == descriptorFileName {
			result.DescriptorURL, err = url.Parse(asset.BrowserDownloadURL)
			if err != nil {
				log.Warning("Error while parsing the BrowserDownloadURL: %v", err.Error())
				return nil
			}
			break
		}
	}

	if result.DescriptorURL == nil {
		log.Warning("The app descriptor ('%v') could not be found as an asset of the latest release", descriptorFileName)
		return nil
	}

	tagComponents := tagRegex.FindStringSubmatch(latestVersionResponse.TagName)
	if tagComponents == nil {
		log.Warning("GitHub's release tag must be in the format: <any string, even empty><VERSION>, not '%v'", latestVersionResponse.TagName)
		return nil
	}

	result.Version, err = versioning.ParseVersion(tagComponents[1])
	if err != nil {
		log.Warning("Error while parsing the version: %v", err.Error())
		return nil
	}

	log.Notice("Response fields correctly processed")

	return result
}
Пример #11
0
func (app *App) CreateDesktopShortcut(launcher launchers.Launcher, referenceDescriptor descriptors.AppDescriptor) (err error) {
	desktopDir, err := caravel.GetUserDesktop()
	if err != nil {
		return err
	}

	shortcutName := caravel.FormatFileName(referenceDescriptor.GetName()) + ".lnk"
	log.Debug("Shortcut file name: '%v'", shortcutName)

	shortcutFilePath := filepath.Join(desktopDir, shortcutName)
	log.Debug("Shortcut path: '%v'", shortcutFilePath)

	log.Info("Creating desktop shortcut: '%v'...", shortcutFilePath)

	log.Debug("Creating temp file for script...")

	salt := rand.Int63()
	tempFileName := fmt.Sprintf("shortcutScript_%v_%v.vbs", time.Now().Unix(), salt)
	tempFilePath := filepath.Join(os.TempDir(), tempFileName)
	tempFile, err := os.Create(tempFilePath)

	if err != nil {
		return err
	}
	defer func() {
		tempFile.Close()

		tempRemovalErr := os.Remove(tempFilePath)
		if tempRemovalErr != nil {
			log.Warning("Cannot remove the temp script: %v", tempFilePath)
		} else {
			log.Debug("Temp script successfully removed")
		}

		if err != nil {
			os.Remove(shortcutFilePath)
		}
	}()

	log.Debug("Temp script file created: %v", tempFilePath)
	actualIconPath := app.GetActualIconPath(launcher)
	log.Debug("Actual icon path: '%v'", actualIconPath)

	workingDirectory := filepath.Dir(app.GetLocalDescriptorPath())
	log.Debug("Working directory: '%v'", workingDirectory)

	shortcutScript := fmt.Sprintf(windowsShortcutContent,
		shortcutFilePath,
		app.GetLocalDescriptorPath(),
		referenceDescriptor.GetDescription(),
		actualIconPath,
		workingDirectory)

	log.Debug("Writing script temp file...")
	tempFile.Write([]byte(shortcutScript))
	tempFile.Close()
	log.Debug("Temp script ready")

	log.Debug("Now executing the temp script...")

	shortcutCreationCommand := exec.Command("wscript", "/b", "/nologo", tempFilePath)

	err = shortcutCreationCommand.Run()
	if err != nil {
		return err
	}

	if !shortcutCreationCommand.ProcessState.Success() {
		return fmt.Errorf("The script did not run successfully")
	}

	log.Debug("The script was successful")

	return nil
}
Пример #12
0
/*
Run is the entry point you must employ to create a custom installer, for example to
employ custom settings or a brand-new user interface, based on any technology
*/
func Run(
	launcher launchers.Launcher,
	userInterface ui.UserInterface,
	bootDescriptor descriptors.AppDescriptor) (err error) {

	settings := launcher.GetSettings()

	//----------------------------------------------------------------------------

	setupUserInterface(launcher, userInterface)
	defer dismissUserInterface(userInterface)

	//----------------------------------------------------------------------------

	userInterface.SetHeader("Performing startup operations")

	log.Debug("The boot descriptor is: %#v", bootDescriptor)

	//----------------------------------------------------------------------------

	userInterface.SetApp(bootDescriptor.GetName())

	//----------------------------------------------------------------------------

	appGallery := apps.NewAppGallery(settings.GetGalleryDirectory())
	log.Debug("The app gallery is: %#v", appGallery)

	//----------------------------------------------------------------------------

	log.Info("Resolving the app...")
	app, err := appGallery.GetApp(bootDescriptor)
	if err != nil {
		return err
	}
	log.Notice("The app directory is: '%v'", app.Directory)

	log.Debug("App is: %#v", app)

	firstRun := !app.DirectoryExists()
	log.Debug("Is this a first run for the app? %v", firstRun)

	//----------------------------------------------------------------------------

	if firstRun {
		log.Info("Now asking the user if the app can run...")

		canRun := app.CanPerformFirstRun(userInterface)
		if !canRun {
			return &ExecutionCanceled{}
		}

		log.Debug("The user agreed to proceed")

		log.Info("Ensuring the app dir is available...")
		err = app.EnsureDirectory()
		if err != nil {
			return err
		}
		log.Notice("App dir available")
	}

	//----------------------------------------------------------------------------

	log.Info("Locking the app dir...")
	err = app.LockDirectory()
	if err != nil {
		return err
	}
	defer func() {
		unlockErr := app.UnlockDirectory()
		if unlockErr != nil {
			log.Warning(unlockErr.Error())
		}
	}()

	log.Notice("App dir locked")

	//----------------------------------------------------------------------------

	log.Info("Checking for conflicting local descriptors...")
	err = app.CheckForConflictingLocalDescriptors()
	if err != nil {
		return err
	}
	log.Notice("No conflicting local descriptors found")

	//----------------------------------------------------------------------------

	log.Info("Resolving the local descriptor...")
	localDescriptor := app.GetLocalDescriptor()

	startedWithLocalDescriptor := localDescriptor != nil
	log.Debug("Started with local descriptor? %v", startedWithLocalDescriptor)

	if startedWithLocalDescriptor {
		log.Info("Checking that local descriptor and boot descriptor actually match...")
		err = descriptors.CheckDescriptorMatch(localDescriptor, bootDescriptor)
		if err != nil {
			return err
		}
		log.Notice("The descriptors match correctly")
	}

	//----------------------------------------------------------------------------

	log.Info("Resolving the remote descriptor...")
	remoteDescriptor := app.GetRemoteDescriptor()

	if remoteDescriptor != nil {
		log.Info("Checking that remote descriptor and boot descriptor actually match...")
		err = descriptors.CheckDescriptorMatch(remoteDescriptor, bootDescriptor)
		if err != nil {
			return err
		}
		log.Notice("The descriptors match correctly")
	}

	//----------------------------------------------------------------------------

	log.Info("Now choosing the reference descriptor...")
	referenceDescriptor, err := app.GetReferenceDescriptor()
	if err != nil {
		return err
	}
	log.Notice("Reference descriptor chosen")

	log.Debug("The reference descriptor is: %#v", referenceDescriptor)

	//----------------------------------------------------------------------------

	err = referenceDescriptor.CheckRequirements()
	if err != nil {
		return err
	}

	//----------------------------------------------------------------------------

	userInterface.SetApp(referenceDescriptor.GetTitle())

	//----------------------------------------------------------------------------

	log.Info("Resolving the OS-specific app command line...")
	commandLine := referenceDescriptor.GetCommandLine()
	log.Notice("Command line resolved")

	log.Debug("Command line is: %v", commandLine)

	//----------------------------------------------------------------------------

	err = app.CheckFiles(settings, userInterface)
	if err != nil {
		return err
	}

	//----------------------------------------------------------------------------

	userInterface.SetHeader("Preparing the command...")

	log.Info("Creating the command...")
	command := app.PrepareCommand(commandLine)
	log.Notice("Command created")

	log.Debug("Command path: %v", command.Path)
	log.Debug("Command arguments: %v", command.Args)

	//----------------------------------------------------------------------------

	referenceDescriptorSaved := app.SaveReferenceDescriptor()

	if !startedWithLocalDescriptor && referenceDescriptorSaved {
		if userInterface.AskForDesktopShortcut(referenceDescriptor) {
			log.Info("Creating desktop shortcut...")

			err = app.CreateDesktopShortcut(launcher, referenceDescriptor)
			if err != nil {
				log.Warning("Could not create desktop shortcut: %v", err)
			} else {
				log.Notice("Desktop shortcut created")
			}
		} else {
			log.Info("The user refused to create a desktop shortcut")
		}
	}

	//----------------------------------------------------------------------------

	app.UnlockDirectory()

	//----------------------------------------------------------------------------

	userInterface.SetHeader("Launching the application")
	userInterface.SetStatus("")

	return app.Launch(command, settings, userInterface)
}