func newConfig(pathMaybeEmpty string) (config.Config, error) { if pathMaybeEmpty == "" { return config.New(), nil } else { return config.NewFromFile(pathMaybeEmpty) } }
func runEtherInspector(args []string) int { if err := etherFlagset.Parse(args); err != nil { log.Critical(err) return 1 } useHookSwitch := _etherFlags.NFQNumber < 0 if useHookSwitch && _etherFlags.HookSwitchZMQAddr == "" { log.Critical("hookswitch is invalid") return 1 } if !useHookSwitch && _etherFlags.NFQNumber > 0xFFFF { log.Critical("nfq-number is invalid") return 1 } if _etherFlags.AutopilotConfig != "" && _etherFlags.OrchestratorURL != ocutil.LocalOrchestratorURL { log.Critical("non-default orchestrator url set for autopilot orchestration mode") return 1 } if _etherFlags.AutopilotConfig != "" { cfg, err := config.NewFromFile(_etherFlags.AutopilotConfig) if err != nil { panic(log.Critical(err)) } autopilotOrchestrator, err := ocutil.NewAutopilotOrchestrator(cfg) if err != nil { panic(log.Critical(err)) } log.Info("Starting autopilot-mode orchestrator") go autopilotOrchestrator.Start() } var etherInspector inspector.EthernetInspector if useHookSwitch { etherInspector = &inspector.HookSwitchInspector{ OrchestratorURL: _etherFlags.OrchestratorURL, EntityID: _etherFlags.EntityID, HookSwitchZMQAddr: _etherFlags.HookSwitchZMQAddr, EnableTCPWatcher: true, } } else { etherInspector = &inspector.NFQInspector{ OrchestratorURL: _etherFlags.OrchestratorURL, EntityID: _etherFlags.EntityID, NFQNumber: uint16(_etherFlags.NFQNumber), EnableTCPWatcher: true, } } if err := etherInspector.Start(); err != nil { panic(log.Critical(err)) } // NOTREACHED return 0 }
func runFsInspector(args []string) int { if err := fsFlagset.Parse(args); err != nil { log.Critical(err) return 1 } if _fsFlags.OriginalDir == "" { log.Critical("original-dir is not set") return 1 } if _fsFlags.Mountpoint == "" { log.Critical("mount-point is not set") return 1 } if _fsFlags.AutopilotConfig != "" && _fsFlags.OrchestratorURL != ocutil.LocalOrchestratorURL { log.Critical("non-default orchestrator url set for autopilot orchestration mode") return 1 } if logutil.Debug { // log level: 0..2 hookfs.SetLogLevel(1) } else { hookfs.SetLogLevel(0) } if _fsFlags.AutopilotConfig != "" { cfg, err := config.NewFromFile(_fsFlags.AutopilotConfig) if err != nil { panic(log.Critical(err)) } autopilotOrchestrator, err := ocutil.NewAutopilotOrchestrator(cfg) if err != nil { panic(log.Critical(err)) } log.Info("Starting autopilot-mode orchestrator") go autopilotOrchestrator.Start() } hook := &inspector.FilesystemInspector{ OrchestratorURL: _fsFlags.OrchestratorURL, EntityID: _fsFlags.EntityID, } fs, err := hookfs.NewHookFs(_fsFlags.OriginalDir, _fsFlags.Mountpoint, hook) if err != nil { panic(log.Critical(err)) } log.Infof("Serving %s", fs) log.Infof("Please run `fusermount -u %s` after using this, manually", _fsFlags.Mountpoint) if err = fs.Serve(); err != nil { panic(log.Critical(err)) } // NOTREACHED return 0 }
// depends on this.storageDirPath func (this *runner) initConfig() error { var err error confPath := path.Join(this.storageDirPath, historystorage.StorageTOMLConfigPath) this.config, err = config.NewFromFile(confPath) if err != nil { return fmt.Errorf("failed to parse config file %s: %s", confPath, err) } return nil }
func runProcInspector(args []string) int { if err := procFlagset.Parse(args); err != nil { log.Critical(err) return 1 } if _procFlags.RootPID <= 0 { log.Critical("root-pid is not set (or set to non-positive value)") return 1 } if _procFlags.AutopilotConfig != "" && _procFlags.OrchestratorURL != ocutil.LocalOrchestratorURL { log.Critical("non-default orchestrator url set for autopilot orchestration mode") return 1 } if _procFlags.AutopilotConfig != "" { cfg, err := config.NewFromFile(_procFlags.AutopilotConfig) if err != nil { panic(log.Critical(err)) } autopilotOrchestrator, err := ocutil.NewAutopilotOrchestrator(cfg) if err != nil { panic(log.Critical(err)) } log.Info("Starting autopilot-mode orchestrator") go autopilotOrchestrator.Start() } procInspector := &inspector.ProcInspector{ OrchestratorURL: _procFlags.OrchestratorURL, EntityID: _procFlags.EntityID, RootPID: _procFlags.RootPID, WatchInterval: _procFlags.WatchInterval, } if err := procInspector.Start(); err != nil { panic(log.Critical(err)) } // NOTREACHED return 0 }
func LoadStorage(dirPath string) HistoryStorage { confPath := dirPath + "/" + StorageTOMLConfigPath // TODO: we should not parse config twice. (run should have already parsed the config) cfg, err := config.NewFromFile(confPath) if err != nil { fmt.Printf("error: %s\n", err) return nil } storageType := cfg.Get("storageType") switch storageType { case "naive": return naive.New(dirPath) default: fmt.Printf("unknown history storage: %s\n", storageType) return nil } // NOTREACHED }
// FIXME: refactor func _init(args []string) int { if err := initFlagset.Parse(args); err != nil { fmt.Printf("%s", err.Error()) return 1 } if initFlagset.NArg() != 3 { fmt.Printf("specify <config file path> <materials dir path> <storage dir path>\n") return 1 } confPath := initFlagset.Arg(0) materials := initFlagset.Arg(1) storagePath := initFlagset.Arg(2) cfi, err := os.Stat(confPath) if err != nil { fmt.Printf("failed to stat path: %s (%s)\n", confPath, err) return 1 } if !cfi.Mode().IsRegular() { fmt.Printf("config file (%s) must be a regular file\n", confPath) return 1 } if _initFlags.Force { // avoid catastrophe caused by a typo wellKnownDirs := map[string]bool{ ".": true, "..": true, "/": true, "/home": true, "/tmp": true, "/dummyWellKnownDir": true} if wellKnownDirs[storagePath] { fmt.Printf("storage dir(%s) typo?\n", storagePath) return 1 } if err = os.RemoveAll(storagePath); err != nil { // NOTE: if storagePath does not exist, RemoveAll returns nil fmt.Printf("could not remove %s (%s)\n", storagePath, err) return 1 } if err = os.Mkdir(storagePath, 0777); err != nil { fmt.Printf("could not create %s (%s)\n", storagePath, err) return 1 } } sfi, err := os.Stat(storagePath) if err != nil { fmt.Printf("failed to stat path: %s (%s)\n", storagePath, err) return 1 } if !sfi.Mode().IsDir() { fmt.Printf("storagePath directory (%s) must be a directory\n", storagePath) return 1 } dir, err := os.Open(storagePath) if err != nil { fmt.Printf("failed to open storagePath directory: %s (%s)\n", storagePath, err) return 1 } fi, err := dir.Readdir(0) if err != nil { fmt.Printf("failed to read storagePath directory: %s (%s)\n", storagePath, err) return 1 } if len(fi) != 0 { fmt.Printf("directory for earthquake storagePath (%s) must be empty\n", storagePath) return 1 } cfg, err := config.NewFromFile(confPath) if err != nil { fmt.Printf("parsing config file (%s) failed: %s\n", confPath, err) return 1 } if !strings.EqualFold(filepath.Ext(confPath), ".toml") { fmt.Printf("this version does not support non-TOML configs") return 1 } err = copyFile(path.Join(storagePath, historystorage.StorageTOMLConfigPath), confPath, 0644) if err != nil { fmt.Printf("placing config file (%s) failed (%s)\n", confPath, err) return 1 } materialsDir := path.Join(storagePath, storageMaterialsPath) err = os.Mkdir(materialsDir, 0777) if err != nil { fmt.Printf("creating a directory for materials (%s) failed (%s)\n", materialsDir, err) return 1 // TODO: cleaning conf file } cmd.DefaultFactory.SetMaterialsDir(materialsDir) err = recursiveHardLink(materials, materialsDir) if err != nil { fmt.Printf("%s\n", err) return 1 } storage, err := historystorage.New(cfg.GetString("storageType"), storagePath) if err != nil { fmt.Printf("%s\n", err) return 1 } storage.CreateStorage() if cfg.GetString("init") != "" { initScriptPath := path.Join(materialsDir, cfg.GetString("init")) runCmd := cmd.DefaultFactory.CreateCmd(initScriptPath) if err = runCmd.Run(); err != nil { fmt.Printf("could not run %s (%s)\n", initScriptPath, err) return 1 } } fmt.Printf("ok\n") return 0 }