func()

in agent/pluginmanager/acspluginmanager/acspluginmanager.go [533:632]


func (pm *PluginManager) installFromFile(packagePath string, config *pluginConfig, plugin *PluginInfo, pluginIndex int, timeout time.Duration) (*Fetched, ExitingError) {
	ctx := context.Background()
	var cancel context.CancelFunc
	if timeout > 0 {
		ctx, cancel = context.WithTimeout(ctx, timeout)
		defer cancel()
	}

	var envPrePluginDir string

	if pluginIndex == -1 {
		plugin = &PluginInfo{
			Timeout: "60",
		}
	} else {
		envPrePluginDir = filepath.Join(pm.pluginRoot, plugin.Name, plugin.Version)
	}

	executionTimeoutInSeconds := 60
	if t, err := strconv.Atoi(config.Timeout); err != nil {
		config.Timeout = plugin.Timeout
	} else {
		executionTimeoutInSeconds = t
	}

	plugin.Name = config.Name
	plugin.Arch = config.Arch
	plugin.OSType = config.OsType
	plugin.RunPath = config.RunPath
	plugin.Timeout = config.Timeout
	plugin.Publisher = config.Publisher
	plugin.Version = config.Version
	plugin.AddSysTag = config.AddSysTag
	plugin.SetPluginType(config.PluginType())
	plugin.Url = "local"
	if config.HeartbeatInterval <= 0 {
		plugin.HeartbeatInterval = 60
	} else {
		plugin.HeartbeatInterval = config.HeartbeatInterval
	}

	// TODO-FIXME: Calculating the MD5 checksum for plugin package is also a
	// time-consuming procedure, which should be cancelable when timed out
	md5Checksum, err := util.ComputeMd5(packagePath)
	if err != nil {
		return nil, NewMD5CheckExitingError(err, "Compute md5 of plugin file err: "+err.Error())
	}
	plugin.Md5 = md5Checksum

	pluginPath := filepath.Join(pm.pluginRoot, plugin.Name, plugin.Version)
	pathutil.MakeSurePath(pluginPath)
	if err := zipfile.UnzipContext(ctx, packagePath, pluginPath, false); err != nil {
		return nil, NewUnzipExitingError(err, fmt.Sprintf("Unzip err, file is [%s], target dir is [%s], err is [%s]", packagePath, pluginPath, err.Error()))
	}

	if config.PluginType() == PLUGIN_COMMANDER {
		commanderInfoPath := filepath.Join(pluginPath, "axt-commander.json")
		if !fileutil.CheckFileIsExist(commanderInfoPath) {
			err := errors.New(fmt.Sprintf("File axt-commander.json not exist, %s.", commanderInfoPath))
			return nil, NewPluginFormatExitingError(err, fmt.Sprintf("File axt-commander.json not exist, %s.", commanderInfoPath))
		}
		commanderInfo := CommanderInfo{}
		if content, err := fuzzyjson.UnmarshalFile(commanderInfoPath, &commanderInfo); err != nil {
			return nil, NewUnmarshalExitingError(err, fmt.Sprintf("Unmarshal axt-commander.json err, axt-commander.json is [%s], err is [%s]", string(content), err.Error()))
		}
		plugin.CommanderInfo = commanderInfo
	}

	cmdPath := filepath.Join(pluginPath, config.RunPath)
	if !fileutil.CheckFileIsExist(cmdPath) {
		log.GetLogger().Infoln("Cmd file not exist: ", cmdPath)
		return nil, NewPluginFormatExitingError(errors.New("Cmd file not exist: "+cmdPath), fmt.Sprintf("Executable file not exist, %s.", cmdPath))
	}
	if osutil.GetOsType() != osutil.OSWin {
		if err := os.Chmod(cmdPath, os.FileMode(0o744)); err != nil {
			return nil, NewExecutablePermissionExitingError(err, "Make plugin file executable err: "+err.Error())
		}
	}
	if pluginIndex == -1 {
		plugin.PluginID = "local_" + plugin.Name + "_" + plugin.Version
		_, err = insertNewInstalledPlugin(plugin)
	} else {
		err = updateInstalledPlugin(pluginIndex, plugin)
	}
	if err != nil {
		return nil, NewDumpInstalledPluginsExitingError(err)
	}

	return &Fetched{
		PluginName:    config.Name,
		PluginVersion: config.Version,
		PluginType:    config.PluginType(),
		AddSysTag:     config.AddSysTag,

		Entrypoint:                cmdPath,
		ExecutionTimeoutInSeconds: executionTimeoutInSeconds,
		EnvPluginDir:              pluginPath,
		EnvPrePluginDir:           envPrePluginDir,
	}, nil
}