func RunUninstallCommands()

in agent/plugins/configurecontainers/windowscontainerutil/windowscontainerutil.go [309:433]


func RunUninstallCommands(context context.T, orchestrationDirectory string, out iohandler.IOHandler) {
	var err error
	var command string
	var parameters []string
	var requireReboot bool
	var platformVersion string

	var isNanoServer bool
	var output string

	var log = context.Log()
	platformVersion, err = dep.PlatformVersion(log)
	if err != nil {
		log.Error("Error detecting platform version", err)
		out.MarkAsFailed(fmt.Errorf("Error detecting platform version: %v", err))
		return
	}
	log.Debug("Platform Version:", platformVersion)
	if !strings.HasPrefix(platformVersion, "10") {
		out.MarkAsFailed(errors.New("ConfigureDocker is only supported on Microsoft Windows Server 2016 and above."))
		return
	}

	//Check if docker daemon registered and running
	var dockerServiceStatusOutput string
	command = "(Get-Service docker).Status"
	parameters = make([]string, 0)
	dockerServiceStatusOutput, err = dep.UpdateUtilExeCommandOutput(context, 120, log, command, parameters, "", "", "", "", true)
	if err != nil {
		log.Error("Error getting Docker service status", err)
		out.MarkAsFailed(fmt.Errorf("Error getting Docker service status: %v", err))
		return
	}
	log.Debug("Get-Service output:", dockerServiceStatusOutput)

	ServiceRunning := strings.Contains(dockerServiceStatusOutput, "Running")

	//Stop service
	if ServiceRunning {
		out.AppendInfo("Stopping Docker Service.")
		command = "Stop-Service docker"
		parameters = make([]string, 0)
		dockerServiceStatusOutput, err = dep.UpdateUtilExeCommandOutput(context, 180, log, command, parameters, "", "", "", "", true)
		if err != nil {
			log.Error("Error stopping Docker service", err)
			out.MarkAsFailed(fmt.Errorf("Error stopping Docker service: %v", err))
			return
		}
		log.Debug("stop-service output:", dockerServiceStatusOutput)
	}

	//Unregister Service
	if len(strings.TrimSpace(dockerServiceStatusOutput)) > 0 {
		out.AppendInfo("Unregistering dockerd service.")

		command = `dockerd`
		log.Debug("dockerd cmd:", command)
		parameters = []string{"--unregister-service"}
		dockerServiceStatusOutput, err = dep.UpdateUtilExeCommandOutput(context, 120, log, command, parameters, "", "", "", "", true)
		if err != nil {
			log.Error("Error unregistering Docker service", err)
			out.MarkAsFailed(fmt.Errorf("Error unregistering Docker service: %v", err))
			return
		}
		log.Debug("dockerd output:", dockerServiceStatusOutput)

	}

	//Remove docker directory
	if _, err := os.Stat(DOCKER_INSTALLED_DIRECTORY); err == nil {
		out.AppendInfo("Removing Docker directory.")
		os.RemoveAll(DOCKER_INSTALLED_DIRECTORY)
	}

	//check if Nano
	isNanoServer, err = dep.IsPlatformNanoServer(log)
	if err != nil {
		log.Error("Error detecting if Nano Server", err)
		out.MarkAsFailed(fmt.Errorf("Error detecting if Nano Server: %v", err))
		return
	}

	if isNanoServer {
		out.AppendInfo("Removing packages from Nano server not currently supported.")
	} else {
		//uninstall windows containers feature
		command = "(Get-WindowsFeature -Name containers).Installed"
		parameters = make([]string, 0)
		output, err = dep.UpdateUtilExeCommandOutput(context, 50, log, command, parameters, "", "", "", "", true)
		if err != nil {
			log.Error("Error getting containers feature", err)
			out.MarkAsFailed(fmt.Errorf("Error getting containers feature: %v", err))
			return
		}
		log.Debug("Get-WindowsFeature output:", output)
		packageInstalled := strings.Contains(output, "True")

		if packageInstalled {
			out.AppendInfo("Uninstalling containers Windows feature.")
			command = "(Uninstall-WindowsFeature -Name containers).RestartNeeded"
			parameters = make([]string, 0)
			output, err = dep.UpdateUtilExeCommandOutput(context, 300, log, command, parameters, "", "", "", "", true)
			if err != nil {
				log.Error("Error uninstalling containers Windows feature", err)
				out.MarkAsFailed(fmt.Errorf("Error uninstalling containers Windows feature: %v", err))
				return
			}
			log.Debug("Uninstall-WindowsFeature output:", output)
			requireReboot = strings.Contains(output, "Yes")
			log.Debug("Requireboot:", requireReboot)
		}
		//reboot if needed
		if requireReboot {
			out.AppendInfo("Rebooting machine to complete install")
			log.Debug("require reboot is true", requireReboot)
			out.SetStatus(contracts.ResultStatusSuccessAndReboot)
			return
		}
	}
	out.AppendInfo("Uninstallation complete.")
	log.Debug("Uninstallation complete")
	out.SetStatus(contracts.ResultStatusSuccess)

	return
}