func()

in agent/pluginmanager/acspluginmanager/acspluginmanager.go [237:322]


func (pm *PluginManager) RemovePlugin(pluginName string) (exitCode int, err error) {
	defer func() {
		if exitCode != 0 || err != nil {
			fmt.Printf("RemovePlugin error, plugin[%s], err: %v\n", pluginName, err)
		} else {
			fmt.Printf("RemovePlugin success, plugin[%s]\n", pluginName)
		}
	}()
	const funcName = "RemovePlugin"

	idx, pluginInfo, err := getInstalledPluginNotRemovedByName(pluginName)
	if err != nil {
		exitCode, _ = errProcess(funcName, LOAD_INSTALLEDPLUGINS_ERR, err, "Load installed_plugins err: "+err.Error())
		return
	}
	if pluginInfo == nil {
		exitCode, _ = errProcess(funcName, PACKAGE_NOT_FOUND, err, "plugin not exist "+pluginName)
		err = errors.New("Plugin " + pluginName + " not found in installed_plugins")
		return
	}

	var pluginLockFile *os.File
	pluginLockFile, err = openPluginLockFile(pluginName)
	if err != nil {
		err = NewOpenPluginLockFileError(err)
		exitCode, _ = errProcess(funcName, LOCKING_ERR, err, fmt.Sprintf("Failed to remove plugin[%s]: %s", pluginName, err.Error()))
		return
	}
	defer pluginLockFile.Close()
	if err = filelock.TryLock(pluginLockFile); err != nil {
		err = NewAcquirePluginExclusiveLockError(err)
		exitCode, _ = errProcess(funcName, LOCKING_ERR, err, fmt.Sprintf("Failed to remove plugin[%s]: %s", pluginName, err.Error()))
		return
	}
	defer filelock.Unlock(pluginLockFile)

	if pluginInfo.PluginType() == PLUGIN_PERSIST {
		// 常驻型插件
		var (
			envPluginDir    string
			envPrePluginDir string
		)
		cmdPath := filepath.Join(pm.pluginRoot, pluginInfo.Name, pluginInfo.Version, pluginInfo.RunPath)
		envPluginDir = filepath.Join(pm.pluginRoot, pluginInfo.Name, pluginInfo.Version)

		var timeout int
		if timeout, err = strconv.Atoi(pluginInfo.Timeout); err != nil {
			timeout = 60
		}
		env := []string{
			"PLUGIN_DIR=" + envPluginDir,
			"PRE_PLUGIN_DIR=" + envPrePluginDir,
		}
		// --stop 停止插件进程
		paramList := []string{"--stop"}
		pm.executePlugin(cmdPath, paramList, timeout, env, false)
		// --uninstall 卸载插件服务
		paramList = []string{"--uninstall"}
		exitCode, _, err = pm.executePlugin(cmdPath, paramList, timeout, env, false)
		if exitCode != 0 || err != nil {
			return
		}
	}

	pluginInfo.IsRemoved = true // 标记为已删除
	// 更新installed_plugins文件
	if err = updateInstalledPlugin(idx, pluginInfo); err != nil {
		exitCode, _ = errProcess(funcName, DUMP_INSTALLEDPLUGINS_ERR, err, "Update installed_plugins file err: "+err.Error())
		return
	}
	sysTagType := ""
	if pluginInfo.AddSysTag {
		sysTagType = RemoveSysTag
	}
	if err = pm.ReportPluginStatus(pluginInfo.Name, pluginInfo.Version, REMOVED, sysTagType); err != nil {
		log.GetLogger().Errorf("Plugin[%s] is removed, but report the removed plugin to server error: %s", pluginInfo.Name, err.Error())
	}
	// 删除插件目录
	pluginDir := filepath.Join(pm.pluginRoot, pluginInfo.Name)
	if err = os.RemoveAll(pluginDir); err != nil {
		exitCode, _ = errProcess(funcName, REMOVE_FILE_ERR, err, fmt.Sprintf("Remove plugin directory err, pluginDir[%s], err: %s", pluginDir, err.Error()))
		return
	}

	return
}