// RemoveScript removes the runner script from the server
RemoveScript removes the runner script from the server
// RemoveScript removes the runner script from the server
func (s RunnerService) RemoveScript(runner api.Runner) error {
	logger := s.logger.With(zap.String("runner", runner.Name))
	var server api.Server
	if err := s.DB.First(&server, "hostname = ?", runner.Hostname).Error; err != nil {
		logger.Error("Failed to retrive server from db", zap.String("server", runner.Hostname), zap.String("exception", err.Error()))
		return fmt.Errorf("Failed to retrive server from db: %s - %v", runner.Hostname, err.Error())
	}
	logger.Info("Creating powershell-session for runner")
	session, err := s.shell.NewSessionCredSSP(server.Hostname, server.Username, server.Password)
	if err != nil {
		logger.Error("Failed to create remote-client for powershell", zap.String("exception", err.Error()))
		return fmt.Errorf("failed to create remote-client for powershell: %v", err)
	}
	defer session.Close()
	var scriptName = fmt.Sprintf("%s\\%s.gen.rb", server.NuixPath, runner.Name)
	if err := session.RemoveItem(scriptName); err != nil {
		logger.Error("Failed to remove script-file in ps-session",
			zap.String("server", runner.Hostname),
			zap.String("nuix_path", server.NuixPath),
			zap.String("exception", err.Error()),
		)
		return fmt.Errorf("Failed to remove script in ps-session: %s - %v", runner.Hostname, err.Error())
	}
	if len(server.AvianScripts) == 0 {
		return nil
	}
	var dirName string
	for i := len(server.AvianScripts) - 1; i >= 0; i-- {
		if string(server.AvianScripts[i]) == "\\" {
			dirName = server.AvianScripts[i:]
			break
		}
		if string(server.AvianScripts[i]) == "/" {
			dirName = server.AvianScripts[i:]
			break
		}
	}
	var scriptsDir = fmt.Sprintf("%s\\%s", server.NuixPath, dirName)
	if err := session.CheckPath(scriptsDir); err == nil {
		if err := session.RemoveItem(scriptsDir); err != nil {
			logger.Error("Failed to remove scripts-dir in ps-session",
				zap.String("server", runner.Hostname),
				zap.String("scripts_dir", scriptsDir),
				zap.String("exception", err.Error()),
			)
			return fmt.Errorf("Failed to remove script in ps-session: %s - %v", runner.Hostname, err.Error())
		}
	}
	return nil
}
func ( s RunnerService ) RemoveScript ( runner api . Runner ) error { logger := s . logger . With ( zap . String ( " " , runner . Name ) ) var server api . Server if err := s . DB . First ( & server , " " , runner . Hostname ) . Error ; err != nil { logger . Error ( " " , zap . String ( " " , runner . Hostname ) , zap . String ( " " , err . Error ( ) ) ) return fmt . Errorf ( " " , runner . Hostname , err . Error ( ) ) } logger . Info ( " " ) session , err := s . shell . NewSessionCredSSP ( server . Hostname , server . Username , server . Password ) if err != nil { logger . Error ( " " , zap . String ( " " , err . Error ( ) ) ) return fmt . Errorf ( " " , err ) } defer session . Close ( ) var scriptName = fmt . Sprintf ( " \\ " , server . NuixPath , runner . Name ) if err := session . RemoveItem ( scriptName ) ; err != nil { logger . Error ( " " , zap . String ( " " , runner . Hostname ) , zap . String ( " " , server . NuixPath ) , zap . String ( " " , err . Error ( ) ) , ) return fmt . Errorf ( " " , runner . Hostname , err . Error ( ) ) } if len ( server . AvianScripts ) == 0 { return nil } var dirName string for i := len ( server . AvianScripts ) - 1 ; i >= 0 ; i -- { if string ( server . AvianScripts [ i ] ) == " \\ " { dirName = server . AvianScripts [ i : ] break } if string ( server . AvianScripts [ i ] ) == " " { dirName = server . AvianScripts [ i : ] break } } var scriptsDir = fmt . Sprintf ( " \\ " , server . NuixPath , dirName ) if err := session . CheckPath ( scriptsDir ) ; err == nil { if err := session . RemoveItem ( scriptsDir ) ; err != nil { logger . Error ( " " , zap . String ( " " , runner . Hostname ) , zap . String ( " " , scriptsDir ) , zap . String ( " " , err . Error ( ) ) , ) return fmt . Errorf ( " " , runner . Hostname , err . Error ( ) ) } } return nil }
RemoveScript removes the runner script from the server
RemoveScript removes the runner script from the server
// close the client on exit // Get the base-dirname of the avian-scripts path // Remove the scripts-dir if it exists