Quellcode durchsuchen

feat(plugin): support etc folder for plugins

ngjaying vor 4 Jahren
Ursprung
Commit
5a6939ffb3

etc/functions/labelImage/labels.txt → plugins/functions/labelImage/etc/labels.txt


etc/functions/labelImage/mobilenet_quant_v1_224.tflite → plugins/functions/labelImage/etc/mobilenet_quant_v1_224.tflite


+ 24 - 7
plugins/manager.go

@@ -440,7 +440,7 @@ func (m *Manager) Register(t PluginType, j Plugin) error {
 	}
 	}
 	if err != nil { //Revert for any errors
 	if err != nil { //Revert for any errors
 		if t == SOURCE && len(unzipFiles) == 1 { //source that only copy so file
 		if t == SOURCE && len(unzipFiles) == 1 { //source that only copy so file
-			os.Remove(unzipFiles[0])
+			os.RemoveAll(unzipFiles[0])
 		}
 		}
 		if len(j.GetSymbols()) > 0 {
 		if len(j.GetSymbols()) > 0 {
 			m.db.Close()
 			m.db.Close()
@@ -507,6 +507,13 @@ func (m *Manager) Delete(t PluginType, name string, stop bool) error {
 	paths := []string{
 	paths := []string{
 		soPath,
 		soPath,
 	}
 	}
+	// Find etc folder
+	etcPath := path.Join(m.etcDir, PluginTypes[t], name)
+	if fi, err := os.Stat(etcPath); err == nil {
+		if fi.Mode().IsDir() {
+			paths = append(paths, etcPath)
+		}
+	}
 	switch t {
 	switch t {
 	case SOURCE:
 	case SOURCE:
 		paths = append(paths, path.Join(m.etcDir, PluginTypes[t], name+".yaml"))
 		paths = append(paths, path.Join(m.etcDir, PluginTypes[t], name+".yaml"))
@@ -537,7 +544,7 @@ func (m *Manager) Delete(t PluginType, name string, stop bool) error {
 	for _, p := range paths {
 	for _, p := range paths {
 		_, err := os.Stat(p)
 		_, err := os.Stat(p)
 		if err == nil {
 		if err == nil {
-			err = os.Remove(p)
+			err = os.RemoveAll(p)
 			if err != nil {
 			if err != nil {
 				results = append(results, err.Error())
 				results = append(results, err.Error())
 			}
 			}
@@ -664,6 +671,11 @@ func (m *Manager) install(t PluginType, name, src string, shellParas []string) (
 			filenames = append(filenames, soPath)
 			filenames = append(filenames, soPath)
 			revokeFiles = append(revokeFiles, soPath)
 			revokeFiles = append(revokeFiles, soPath)
 			_, version = parseName(fileName)
 			_, version = parseName(fileName)
+		} else if strings.HasPrefix(fileName, "etc/") {
+			err = unzipTo(file, path.Join(m.etcDir, PluginTypes[t], strings.Replace(fileName, "etc", name, 1)))
+			if err != nil {
+				return filenames, "", err
+			}
 		} else { //unzip other files
 		} else { //unzip other files
 			err = unzipTo(file, path.Join(tempPath, fileName))
 			err = unzipTo(file, path.Join(tempPath, fileName))
 			if err != nil {
 			if err != nil {
@@ -692,11 +704,12 @@ func (m *Manager) install(t PluginType, name, src string, shellParas []string) (
 
 
 		if err != nil {
 		if err != nil {
 			for _, f := range revokeFiles {
 			for _, f := range revokeFiles {
-				os.Remove(f)
+				os.RemoveAll(f)
 			}
 			}
 			common.Log.Infof(`err:%v stdout:%s stderr:%s`, err, outb.String(), errb.String())
 			common.Log.Infof(`err:%v stdout:%s stderr:%s`, err, outb.String(), errb.String())
 			return filenames, "", err
 			return filenames, "", err
 		} else {
 		} else {
+			common.Log.Infof(`run install script:%s`, outb.String())
 			common.Log.Infof("install %s plugin %s", PluginTypes[t], name)
 			common.Log.Infof("install %s plugin %s", PluginTypes[t], name)
 		}
 		}
 	}
 	}
@@ -716,17 +729,21 @@ func parseName(n string) (string, string) {
 func unzipTo(f *zip.File, fpath string) error {
 func unzipTo(f *zip.File, fpath string) error {
 	_, err := os.Stat(fpath)
 	_, err := os.Stat(fpath)
 	if err == nil || !os.IsNotExist(err) {
 	if err == nil || !os.IsNotExist(err) {
-		if err = os.Remove(fpath); err != nil {
+		if err = os.RemoveAll(fpath); err != nil {
 			return fmt.Errorf("failed to delete file %s", fpath)
 			return fmt.Errorf("failed to delete file %s", fpath)
 		}
 		}
 	}
 	}
 
 
 	if f.FileInfo().IsDir() {
 	if f.FileInfo().IsDir() {
-		return fmt.Errorf("%s: not a file, but a directory", fpath)
+		// Make Folder
+		os.MkdirAll(fpath, os.ModePerm)
+		return nil
 	}
 	}
 
 
-	if err := os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
-		return err
+	if _, err := os.Stat(filepath.Dir(fpath)); os.IsNotExist(err) {
+		if err := os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
+			return err
+		}
 	}
 	}
 
 
 	outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
 	outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())

+ 9 - 2
plugins/manager_test.go

@@ -83,6 +83,10 @@ func TestManager_Register(t *testing.T) {
 			u:   endpoint + "/functions/echo2.zip",
 			u:   endpoint + "/functions/echo2.zip",
 			f:   []string{"misc", "echo3"},
 			f:   []string{"misc", "echo3"},
 			err: errors.New("function name echo3 already exists"),
 			err: errors.New("function name echo3 already exists"),
+		}, {
+			t: FUNCTION,
+			n: "comp",
+			u: endpoint + "/functions/comp.zip",
 		},
 		},
 	}
 	}
 	manager, err := NewPluginManager()
 	manager, err := NewPluginManager()
@@ -133,7 +137,7 @@ func TestManager_List(t *testing.T) {
 			r: []string{"file", "file2"},
 			r: []string{"file", "file2"},
 		}, {
 		}, {
 			t: FUNCTION,
 			t: FUNCTION,
-			r: []string{"accumulateWordCount", "countPlusOne", "echo", "echo2"},
+			r: []string{"accumulateWordCount", "comp", "countPlusOne", "echo", "echo2"},
 		},
 		},
 	}
 	}
 	manager, err := NewPluginManager()
 	manager, err := NewPluginManager()
@@ -160,7 +164,7 @@ func TestManager_Symbols(t *testing.T) {
 	if err != nil {
 	if err != nil {
 		t.Error(err)
 		t.Error(err)
 	}
 	}
-	r := []string{"accumulateWordCount", "countPlusOne", "echo", "echo2", "echo3", "misc"}
+	r := []string{"accumulateWordCount", "comp", "countPlusOne", "echo", "echo2", "echo3", "misc"}
 	result, err := manager.ListSymbols()
 	result, err := manager.ListSymbols()
 	if err != nil {
 	if err != nil {
 		t.Errorf("list symbols error : %s\n\n", err)
 		t.Errorf("list symbols error : %s\n\n", err)
@@ -245,6 +249,9 @@ func TestManager_Delete(t *testing.T) {
 		}, {
 		}, {
 			t: SOURCE,
 			t: SOURCE,
 			n: "random3",
 			n: "random3",
+		}, {
+			t: FUNCTION,
+			n: "comp",
 		},
 		},
 	}
 	}
 	manager, err := NewPluginManager()
 	manager, err := NewPluginManager()

BIN
plugins/testzips/functions/comp.zip