github.com/GuanceCloud/cliutils@v1.1.21/pipeline/manager/manager.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  // Package manager for managing pipeline scripts
     7  package manager
     8  
     9  import (
    10  	"os"
    11  	"path/filepath"
    12  
    13  	"github.com/GuanceCloud/cliutils/pipeline/ptinput/plmap"
    14  	"github.com/GuanceCloud/cliutils/point"
    15  )
    16  
    17  type Manager struct {
    18  	storeMap map[point.Category]*ScriptStore
    19  	relation *ScriptRelation
    20  }
    21  
    22  type ManagerCfg struct {
    23  	gTags [][2]string
    24  	upFn  plmap.UploadFunc
    25  }
    26  
    27  func NewManagerCfg(upFn plmap.UploadFunc, gTags [][2]string) ManagerCfg {
    28  	return ManagerCfg{
    29  		upFn:  upFn,
    30  		gTags: gTags,
    31  	}
    32  }
    33  
    34  func NewManager(cfg ManagerCfg) *Manager {
    35  	center := &Manager{
    36  		storeMap: map[point.Category]*ScriptStore{},
    37  		relation: NewPipelineRelation(),
    38  	}
    39  	for _, cat := range point.AllCategories() {
    40  		center.storeMap[cat] = NewScriptStore(cat, cfg)
    41  	}
    42  	return center
    43  }
    44  
    45  func (m *Manager) whichStore(category point.Category) (*ScriptStore, bool) {
    46  	if category == point.MetricDeprecated {
    47  		category = point.Metric
    48  	}
    49  	if v, ok := m.storeMap[category]; ok && v != nil {
    50  		return v, ok
    51  	}
    52  	return nil, false
    53  }
    54  
    55  func (m *Manager) UpdateDefaultScript(mp map[point.Category]string) {
    56  	for _, cat := range point.AllCategories() {
    57  		if store, ok := m.whichStore(cat); ok {
    58  			if v, ok := mp[cat]; ok && v != "" {
    59  				store.SetDefaultScript(v)
    60  			} else {
    61  				store.SetDefaultScript("")
    62  			}
    63  		}
    64  	}
    65  }
    66  
    67  func (m *Manager) GetScriptRelation() *ScriptRelation {
    68  	return m.relation
    69  }
    70  
    71  func (m *Manager) QueryScript(category point.Category, name string,
    72  	DisableDefaultP ...struct{}) (*PlScript, bool) {
    73  
    74  	if v, ok := m.whichStore(category); ok {
    75  		if ss, ok := v.IndexGet(name); ok {
    76  			return ss, ok
    77  		}
    78  
    79  		if len(DisableDefaultP) == 0 {
    80  			return v.IndexDefault()
    81  		}
    82  	}
    83  	return nil, false
    84  }
    85  
    86  func (m *Manager) ScriptCount(category point.Category) int {
    87  	if v, ok := m.whichStore(category); ok {
    88  		return v.Count()
    89  	}
    90  	return 0
    91  }
    92  
    93  func (m *Manager) LoadScriptsFromWorkspace(ns, plPath string, tags map[string]string) {
    94  	if plPath == "" {
    95  		return
    96  	}
    97  
    98  	scripts, _ := ReadWorkspaceScripts(plPath)
    99  
   100  	m.LoadScripts(ns, scripts, tags)
   101  }
   102  
   103  // LoadScripts is used to load and clean the script, parameter scripts example:
   104  // {point.Logging: {ScriptName: ScriptContent},... }.
   105  func (m *Manager) LoadScripts(ns string, scripts map[point.Category](map[string]string),
   106  	tags map[string]string,
   107  ) {
   108  	for _, cat := range point.AllCategories() {
   109  		if ss, ok := scripts[cat]; ok {
   110  			m.LoadScriptWithCat(cat, ns, ss, tags)
   111  		} else {
   112  			// cleanup the store for this category
   113  			m.LoadScriptWithCat(cat, ns, map[string]string{}, tags)
   114  		}
   115  	}
   116  }
   117  
   118  func (m *Manager) LoadScriptWithCat(category point.Category, ns string,
   119  	scripts, tags map[string]string,
   120  ) {
   121  	if v, ok := m.whichStore(category); ok {
   122  		v.UpdateScriptsWithNS(ns, scripts, tags)
   123  	}
   124  }
   125  
   126  func CategoryDirName() map[point.Category]string {
   127  	return map[point.Category]string{
   128  		point.Metric:       "metric",
   129  		point.Network:      "network",
   130  		point.KeyEvent:     "keyevent",
   131  		point.Object:       "object",
   132  		point.CustomObject: "custom_object",
   133  		point.Logging:      "logging",
   134  		point.Tracing:      "tracing",
   135  		point.RUM:          "rum",
   136  		point.Security:     "security",
   137  		point.Profiling:    "profiling",
   138  		point.DialTesting:  "dialtesting",
   139  	}
   140  }
   141  
   142  func SearchWorkspaceScripts(basePath string) map[point.Category](map[string]string) {
   143  	files := map[point.Category](map[string]string){}
   144  
   145  	var err error
   146  	files[point.Logging], err = SearchScripts(basePath)
   147  	if err != nil {
   148  		log.Warn(err)
   149  	}
   150  
   151  	for category, dirName := range CategoryDirName() {
   152  		s, err := SearchScripts(filepath.Join(basePath, dirName))
   153  		if err != nil {
   154  			log.Warn(err)
   155  		}
   156  		if _, ok := files[category]; !ok {
   157  			files[category] = map[string]string{}
   158  		}
   159  		for k, v := range s {
   160  			files[category][k] = v
   161  		}
   162  	}
   163  	return files
   164  }
   165  
   166  func ReadWorkspaceScripts(basePath string) (
   167  	map[point.Category](map[string]string), map[point.Category](map[string]string),
   168  ) {
   169  	scriptsPath := SearchWorkspaceScripts(basePath)
   170  
   171  	scripts := map[point.Category](map[string]string){}
   172  	for cat, ssPath := range scriptsPath {
   173  		if _, ok := scripts[cat]; !ok {
   174  			scripts[cat] = map[string]string{}
   175  		}
   176  		for _, path := range ssPath {
   177  			if name, script, err := ReadScript(path); err == nil {
   178  				scripts[cat][name] = script
   179  			} else {
   180  				log.Error(err)
   181  			}
   182  		}
   183  	}
   184  
   185  	return scripts, scriptsPath
   186  }
   187  
   188  func SearchScripts(dirPath string) (map[string]string, error) {
   189  	ret := map[string]string{}
   190  	dirPath = filepath.Clean(dirPath)
   191  
   192  	dirEntry, err := os.ReadDir(dirPath)
   193  	if err != nil {
   194  		return nil, err
   195  	}
   196  
   197  	for _, v := range dirEntry {
   198  		if v.IsDir() {
   199  			// todo: support sub dir
   200  			continue
   201  		}
   202  		if sName := v.Name(); filepath.Ext(sName) == ".p" {
   203  			ret[sName] = filepath.Join(dirPath, sName)
   204  		}
   205  	}
   206  	return ret, nil
   207  }
   208  
   209  func ReadScripts(dirPath string) (map[string]string, map[string]string) {
   210  	ret := map[string]string{}
   211  
   212  	scriptsPath, err := SearchScripts(dirPath)
   213  	if err != nil {
   214  		log.Warn(err)
   215  		return nil, nil
   216  	}
   217  
   218  	for _, path := range scriptsPath {
   219  		if name, script, err := ReadScript(path); err == nil {
   220  			ret[name] = script
   221  		} else {
   222  			log.Error(err)
   223  		}
   224  	}
   225  
   226  	return ret, scriptsPath
   227  }
   228  
   229  func ReadScript(fp string) (string, string, error) {
   230  	fp = filepath.Clean(fp)
   231  	if v, err := os.ReadFile(filepath.Clean(fp)); err == nil {
   232  		_, sName := filepath.Split(fp)
   233  		return sName, string(v), nil
   234  	} else {
   235  		return "", "", err
   236  	}
   237  }