func()

in pkg/servicehandler/servicehandler.go [109:140]


func (handler *Handler) DeRegister(ctx *log.Context) error {
	// We need to make sure the version that the VM Agent is trying to uninstall is the correct one.
	// Failing to check this can cause to uninstall the service during the update workflow.
	targetVersion := os.Getenv(constants.ExtensionVersionEnvName)
	ctx.Log("message", "trying to uninstall extension with version: "+targetVersion)

	installedVersion, err := handler.GetInstalledVersion(ctx)
	if err != nil {
		return errors.Wrap(err, "error while checking the installed version of the service")
	}

	if targetVersion == installedVersion {
		err = handler.Stop()
		if err != nil {
			return fmt.Errorf("error while stopping unit: %v", err)
		}

		err = handler.Disable()
		if err != nil {
			return fmt.Errorf("error while disabling unit: %v", err)
		}

		err = handler.manager.RemoveUnitConfigurationFile(handler.config.Name, ctx)
		if err != nil {
			return fmt.Errorf("error while removing unit configuration: %v", err)
		}
	} else {
		ctx.Log("message", "Skipping action. Target version is not current installed")
	}

	return nil
}