Переглянути джерело

fix(config): schema data config import export support (#1611)

* fix(config): schema data config import export support

Signed-off-by: Jianxiang Ran <rxan_embedded@163.com>

* fix(docs): fix schema repo not init issue

Signed-off-by: Jianxiang Ran <rxan_embedded@163.com>

* fix(docs): fix schema repo not init issue

Signed-off-by: Jianxiang Ran <rxan_embedded@163.com>

Signed-off-by: Jianxiang Ran <rxan_embedded@163.com>
superxan 2 роки тому
батько
коміт
ef18aa721a

+ 21 - 25
internal/plugin/native/manager.go

@@ -92,35 +92,31 @@ func InitManager() (*Manager, error) {
 	}
 	}
 	registry := &Manager{symbols: make(map[string]string), funcSymbolsDb: func_db, plgInstallDb: plg_db, plgStatusDb: plg_status_db, pluginDir: pluginDir, pluginConfDir: dataDir, runtime: make(map[string]*plugin.Plugin)}
 	registry := &Manager{symbols: make(map[string]string), funcSymbolsDb: func_db, plgInstallDb: plg_db, plgStatusDb: plg_status_db, pluginDir: pluginDir, pluginConfDir: dataDir, runtime: make(map[string]*plugin.Plugin)}
 	manager = registry
 	manager = registry
-	if manager.hasInstallFlag() {
-		manager.plugins = make([]map[string]string, 3)
-		for i := range manager.plugins {
-			manager.plugins[i] = make(map[string]string)
-		}
-		manager.pluginInstallWhenReboot()
-		manager.clearInstallFlag()
-	} else {
-		plugins := make([]map[string]string, 3)
-		for i := range plugins {
-			names, err := findAll(plugin2.PluginType(i), pluginDir)
-			if err != nil {
-				return nil, fmt.Errorf("fail to find existing plugins: %s", err)
-			}
-			plugins[i] = names
+
+	plugins := make([]map[string]string, 3)
+	for i := range plugins {
+		names, err := findAll(plugin2.PluginType(i), pluginDir)
+		if err != nil {
+			return nil, fmt.Errorf("fail to find existing plugins: %s", err)
 		}
 		}
-		registry.plugins = plugins
+		plugins[i] = names
+	}
+	registry.plugins = plugins
 
 
-		for pf := range plugins[plugin2.FUNCTION] {
-			l := make([]string, 0)
-			if ok, err := func_db.Get(pf, &l); ok {
-				registry.storeSymbols(pf, l)
-			} else if err != nil {
-				return nil, fmt.Errorf("error when querying kv: %s", err)
-			} else {
-				registry.storeSymbols(pf, []string{pf})
-			}
+	for pf := range plugins[plugin2.FUNCTION] {
+		l := make([]string, 0)
+		if ok, err := func_db.Get(pf, &l); ok {
+			registry.storeSymbols(pf, l)
+		} else if err != nil {
+			return nil, fmt.Errorf("error when querying kv: %s", err)
+		} else {
+			registry.storeSymbols(pf, []string{pf})
 		}
 		}
 	}
 	}
+	if manager.hasInstallFlag() {
+		manager.pluginInstallWhenReboot()
+		manager.clearInstallFlag()
+	}
 	return registry, nil
 	return registry, nil
 }
 }
 
 

+ 1 - 1
internal/plugin/portable/manager.go

@@ -419,7 +419,7 @@ func (m *Manager) GetAllPlugins() map[string]string {
 }
 }
 
 
 func (m *Manager) GetAllPluginsStatus() map[string]string {
 func (m *Manager) GetAllPluginsStatus() map[string]string {
-	allPlgs, err := m.plgInstallDb.All()
+	allPlgs, err := m.plgStatusDb.All()
 	if err != nil {
 	if err != nil {
 		return nil
 		return nil
 	}
 	}

+ 47 - 31
internal/schema/registry.go

@@ -67,39 +67,38 @@ func InitRegistry() error {
 	if err != nil {
 	if err != nil {
 		return fmt.Errorf("cannot open schemaStatus db: %s", err)
 		return fmt.Errorf("cannot open schemaStatus db: %s", err)
 	}
 	}
-	if hasInstallFlag() {
-		schemaInstallWhenReboot()
-		clearInstallFlag()
-	} else {
-		for _, schemaType := range def.SchemaTypes {
-			schemaDir := filepath.Join(dataDir, "schemas", string(schemaType))
-			var newSchemas map[string]*Files
-			files, err := os.ReadDir(schemaDir)
-			if err != nil {
-				conf.Log.Warnf("cannot read schema directory: %s", err)
-				newSchemas = make(map[string]*Files)
-			} else {
-				newSchemas = make(map[string]*Files, len(files))
-				for _, file := range files {
-					fileName := filepath.Base(file.Name())
-					ext := filepath.Ext(fileName)
-					schemaId := strings.TrimSuffix(fileName, filepath.Ext(fileName))
-					ffs, ok := newSchemas[schemaId]
-					if !ok {
-						ffs = &Files{}
-						newSchemas[schemaId] = ffs
-					}
-					switch ext {
-					case ".so":
-						ffs.SoFile = filepath.Join(schemaDir, file.Name())
-					default:
-						ffs.SchemaFile = filepath.Join(schemaDir, file.Name())
-					}
-					conf.Log.Infof("schema file %s.%s loaded", schemaType, schemaId)
+	for _, schemaType := range def.SchemaTypes {
+		schemaDir := filepath.Join(dataDir, "schemas", string(schemaType))
+		var newSchemas map[string]*Files
+		files, err := os.ReadDir(schemaDir)
+		if err != nil {
+			conf.Log.Warnf("cannot read schema directory: %s", err)
+			newSchemas = make(map[string]*Files)
+		} else {
+			newSchemas = make(map[string]*Files, len(files))
+			for _, file := range files {
+				fileName := filepath.Base(file.Name())
+				ext := filepath.Ext(fileName)
+				schemaId := strings.TrimSuffix(fileName, filepath.Ext(fileName))
+				ffs, ok := newSchemas[schemaId]
+				if !ok {
+					ffs = &Files{}
+					newSchemas[schemaId] = ffs
+				}
+				switch ext {
+				case ".so":
+					ffs.SoFile = filepath.Join(schemaDir, file.Name())
+				default:
+					ffs.SchemaFile = filepath.Join(schemaDir, file.Name())
 				}
 				}
+				conf.Log.Infof("schema file %s.%s loaded", schemaType, schemaId)
 			}
 			}
-			registry.schemas[schemaType] = newSchemas
 		}
 		}
+		registry.schemas[schemaType] = newSchemas
+	}
+	if hasInstallFlag() {
+		schemaInstallWhenReboot()
+		clearInstallFlag()
 	}
 	}
 	return nil
 	return nil
 }
 }
@@ -124,7 +123,12 @@ func Register(info *Info) error {
 	if _, ok := registry.schemas[info.Type][info.Name]; ok {
 	if _, ok := registry.schemas[info.Type][info.Name]; ok {
 		return fmt.Errorf("schema %s.%s already registered", info.Type, info.Name)
 		return fmt.Errorf("schema %s.%s already registered", info.Type, info.Name)
 	}
 	}
-	return CreateOrUpdateSchema(info)
+	err := CreateOrUpdateSchema(info)
+	if err != nil {
+		return err
+	}
+	storeSchemaInstallScript(info)
+	return nil
 }
 }
 
 
 func CreateOrUpdateSchema(info *Info) error {
 func CreateOrUpdateSchema(info *Info) error {
@@ -236,6 +240,7 @@ func DeleteSchema(schemaType def.SchemaType, name string) error {
 		}
 		}
 	}
 	}
 	delete(registry.schemas[schemaType], name)
 	delete(registry.schemas[schemaType], name)
+	removeSchemaInstallScript(schemaType, name)
 	return nil
 	return nil
 }
 }
 
 
@@ -317,3 +322,14 @@ func schemaInstallWhenReboot() {
 		}
 		}
 	}
 	}
 }
 }
+
+func storeSchemaInstallScript(info *Info) {
+	key := string(info.Type) + "_" + info.Name
+	val := info.InstallScript()
+	_ = schemaDb.Set(key, val)
+}
+
+func removeSchemaInstallScript(schemaType def.SchemaType, name string) {
+	key := string(schemaType) + "_" + name
+	_ = schemaDb.Delete(key)
+}