func NewConfig(configYamlFile string) (*DockerTestConfig, error) {
	file, err := os.OpenFile(configYamlFile, os.O_RDONLY, 0666)
	if err != nil {
		return &DockerTestConfig{}, err
	}
	config, err := config.ParseYamlFile(file.Name())
	return &DockerTestConfig{config}, err
}
Example #2
0
func Read(path string) error {
	log.Info("Reading configuration from ", path)
	var err error
	c, err = cfg.ParseYamlFile(path)
	if err != nil {
		log.Error(err)
	}
	return err
}
Example #3
0
// ParseConfig parses a configuration file located at path. Configuration files
// need to end in .yaml or .json, depending whether they are in yaml or json format
func ParseConfig(path string) error {
	var err error = nil
	if strings.HasSuffix(path, "yaml") {
		Global, err = config.ParseYamlFile(path)
	} else if strings.HasSuffix(path, "json") {
		Global, err = config.ParseJsonFile(path)
	}
	return err
}
Example #4
0
func loadConfig() *config.Config {
	//Load msct.conf, prefer local over /etc/, and parse yaml
	var yaml *config.Config
	if _, err := os.Stat("./msct.conf"); err == nil {
		yaml, _ = config.ParseYamlFile("./msct.conf")
	} else if _, err := os.Stat("/etc/msct.conf"); err == nil {
		yaml, _ = config.ParseYamlFile("/etc/msct.conf")
	} else {
		err := generateConfig()
		if err != nil {
			glog.Fatalf("Could not generate config file; please inform the developer.\n%s", err)
		}
		glog.Warningf("Could not locate msct.conf, so I generated the default file for you at /etc/msct.conf")
		yaml, _ = config.ParseYamlFile("/etc/msct.conf")
	}

	return yaml
}
Example #5
0
func Load(confPath string) (*Config, error) {
	log.Info("loading configuration file: %s", confPath)
	cfg, err := config.ParseYamlFile(confPath)
	if err != nil {
		return nil, err
	}

	// NOTE use defaults ?
	cfgName, err := cfg.String("sentinel.name")
	if err != nil {
		cfgName = DEFAULT_NAME
	}
	log.Info("parsing plugins configuration")
	adapterCfg, err := cfg.String("sentinel.adapter")
	if err != nil {
		return nil, err
	}
	actuatorCfg, err := cfg.String("sentinel.actuator")
	if err != nil {
		return nil, err
	}
	heartbeatCfg, err := cfg.String("sentinel.heartbeat")
	if err != nil {
		return nil, err
	}

	parser := NewREParser()

	partials := parser.Parse(heartbeatCfg)
	heartbeatConf_ := &HeartbeatConfig{
		Plugin: partials.Plugin,
		// NOTE pop out "on" ?
		Opts: partials.Opts,
		// FIXME what if "on" is not specified ?
		Filters: agent.ParseEventFilter(partials.Opts["on"]),
	}

	log.Info("loading sentinel configuration")
	return &Config{
		Name:      cfgName,
		Adapter:   parser.Parse(adapterCfg),
		Actuator:  parser.Parse(actuatorCfg),
		Heartbeat: heartbeatConf_,
	}, nil
}
Example #6
0
func main() {
	flag.Parse()

	cfg, err := config.ParseYamlFile(configFile)
	if err != nil {
		log.Fatalf("Error parsing config file: %s", err.Error())
	}

	if pidFile, err := cfg.String("pidfile"); err != nil {
		log.Fatalf("Error reading pidfile from config: %s", err.Error())
	} else {
		if _, err := pid.Do(pidFile); err != nil {
			log.Fatalf("error creating pidfile (%s): %s", pidFile, err.Error())
		}
	}

	if raidsDbFile, err := cfg.String("database.raids"); err != nil {
		log.Fatalf("Error reading database.raids from config file: %s", err.Error())
	} else {
		if err := raidDb.load(raidsDbFile); err != nil {
			log.Fatalf("Error reading %s: %s", raidsDbFile, err.Error())
		}
		if dur, err := cfg.String("maxAge"); err != nil {
			log.Fatalf("Error reading maxAge from config file: %s", err.Error())
		} else {
			if maxAge, err := time.ParseDuration(dur); err != nil {
				log.Fatalf("Error parsing maxAge as a time.Duration: %s", err.Error())
			} else {
				go raidDb.mindExpiration(maxAge)
			}
		}
	}

	if userDbFile, err := cfg.String("database.users"); err != nil {
		log.Fatalf("Error reading database.users from config file: %s", err.Error())
	} else {
		if err := udb.load(userDbFile); err != nil {
			log.Fatalf("Error reading %s: %s", userDbFile, err.Error())
		}
	}

	if lfgDbFile, err := cfg.String("database.lfg"); err != nil {
		log.Fatalf("Error reading database.lfg from config file: %s", err.Error())
	} else {
		if err := lfg.load(lfgDbFile); err != nil {
			log.Fatalf("Error reading %s: %s", lfgDbFile, err.Error())
		}
		go lfg.mindExpiration()
	}

	if slack.raidKey, err = cfg.String("slack.slashKey.raids"); err != nil {
		log.Fatalf("Error reading slack.slashKey.raids: %s", err.Error())
	}

	if adminsvar, err := cfg.List("slack.admins"); err != nil {
		log.Fatalf("Error reading slack.admins: %s", err.Error())
	} else {
		for _, v := range adminsvar {
			if admin, ok := v.(string); ok {
				admins = append(admins, admin)
			} else {
				log.Fatalf("%#v from %#v is not a string", v, adminsvar)
			}
		}
		xhrOutput.set("admins", admins)
	}

	if apiKey, err := cfg.String("slack.apiKey"); err != nil {
		log.Fatalf("Error reading slack.apiKey from config: %s", err.Error())
	} else {
		slack.apiKey = apiKey
	}

	if hmac, err := cfg.String("security.hmac_key"); err != nil {
		rand.Read(hmacKey)
	} else {
		hmacKey = []byte(hmac)
	}

	if auth, err := cfg.String("security.cookie.auth"); err == nil {
		if key, err := cfg.String("security.cookie.key"); err == nil {
			store = sessions.NewCookieStore([]byte(auth), []byte(key))
		} else {
			log.Fatalf("error reading security.cookie.key from config: %s", err.Error())
		}
	} else {
		log.Fatalf("error reading security.cookie.auth from config: %s", err.Error())
	}

	if cmd, err := cfg.String("slack.slashCommand.raids"); err == nil {
		raidSlashCommand = cmd
		xhrOutput.set("command", cmd)
	}

	go mindSlackMsgQueue()
	if listen, err := cfg.String("listen"); err != nil {
		log.Fatal(err)
	} else {
		var devmode = false
		if _, err := os.Stat("www/index.html"); err == nil {
			devmode = true
		}
		if devmode == false {
			http.Handle("/",
				http.FileServer(
					&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "www"}))
		} else {
			http.Handle("/", http.FileServer(http.Dir("www/")))
		}
		http.HandleFunc("/api", doHTTPPost)
		http.HandleFunc("/rest/", doRESTRouter)
		http.HandleFunc("/ics", func(w http.ResponseWriter, r *http.Request) {
			session, _ := store.Get(r, "raidbot")
			session.Options.MaxAge = 604800
			uri, _ := url.ParseRequestURI(r.RequestURI)
			if v, err := url.ParseQuery(uri.RawQuery); err != nil {
				log.Println(err.Error())
				doHTTPStatus(w, http.StatusInternalServerError)
				return
			} else {
				if err := requireAPIKey(session, w); err != nil {
					log.Println(err.Error())
					doHTTPStatus(w, http.StatusInternalServerError)
					return
				}
				if name, ok := session.Values["username"]; ok {
					w.Header().Set("Content-Type", "text/Calendar")
					w.Header().Set("Content-Disposition", "inline; filename="+cleanFromFilename.ReplaceAllString(v.Get("title"), "-")+".ics")
					fmt.Fprintf(w, v.Get("data"))
					log.Printf("@%s -- ics: %s", name.(string), v.Get("title"))
				} else {
					doHTTPStatus(w, http.StatusForbidden)
				}
			}
		})

		go mindChannelList()

		log.Println("Starting Up!")
		log.Fatal(http.ListenAndServe(listen, nil))
	}
}
Example #7
0
var flagVersion bool
var flagVerbose bool

var rootCmd = &cobra.Command{
	Use:   "pops",
	Short: "Everything ops",
	Long:  `The CLI for Ops.`,
	RunE: func(cmd *cobra.Command, args []string) error {
		if !flagVersion {
			return cmd.Usage()
		}

		fmt.Println("Pops version " + lib.VersionNumber)

		if configPath, err := findConfigFile(); err == nil {
			cfg, err := config.ParseYamlFile(configPath)
			if err != nil {
				return err
			}
			if suggestedVersion, err := cfg.String("suggested_version"); err == nil {
				ok, err := checkVersion(suggestedVersion)
				if err != nil {
					return err
				}
				if !ok {
					fmt.Println("Your version is not compliant with " + suggestedVersion + " as specified in " + configPath)
					fmt.Println("Upgrade your version at https://github.com/MYOB-Technology/pops/releases")
				}
			}
		}
Example #8
0
func main() {
	flag.Parse()

	cfg, err := config.ParseYamlFile(configFile)
	if err != nil {
		log.Fatalf("Error parsing config file: %s", err.Error())
	}

	if pidFile, err := cfg.String("pidfile"); err != nil {
		log.Fatalf("Error reading pidfile from config: %s", err.Error())
	} else {
		if _, err := pid.Do(pidFile); err != nil {
			log.Fatalf("error creating pidfile (%s): %s", pidFile, err.Error())
		}
	}

	if raidsDbFile, err := cfg.String("database.raids"); err != nil {
		log.Fatalf("Error reading database.raids from config file: %s", err.Error())
	} else {
		if err := raidDb.load(raidsDbFile); err != nil {
			log.Fatalf("Error reading %s: %s", raidsDbFile, err.Error())
		}
		if dur, err := cfg.String("maxAge"); err != nil {
			log.Fatalf("Error reading maxAge from config file: %s", err.Error())
		} else {
			if maxAge, err := time.ParseDuration(dur); err != nil {
				log.Fatalf("Error parsing maxAge as a time.Duration: %s", err.Error())
			} else {
				go raidDb.mindExpiration(maxAge)
			}
		}
	}

	if userDbFile, err := cfg.String("database.users"); err != nil {
		log.Fatalf("Error reading database.users from config file: %s", err.Error())
	} else {
		if err := udb.load(userDbFile); err != nil {
			log.Fatalf("Error reading %s: %s", userDbFile, err.Error())
		}
	}

	if lfgDbFile, err := cfg.String("database.lfg"); err != nil {
		log.Fatalf("Error reading database.lfg from config file: %s", err.Error())
	} else {
		if err := lfg.load(lfgDbFile); err != nil {
			log.Fatalf("Error reading %s: %s", lfgDbFile, err.Error())
		}
		go lfg.mindExpiration()
	}

	if slack.raidKey, err = cfg.String("slack.slashKey.raids"); err != nil {
		log.Fatalf("Error reading slack.slashKey.raids: %s", err.Error())
	}

	if adminsvar, err := cfg.List("slack.admins"); err != nil {
		log.Fatalf("Error reading slack.admins: %s", err.Error())
	} else {
		for _, v := range adminsvar {
			if admin, ok := v.(string); ok {
				admins = append(admins, admin)
			} else {
				log.Fatalf("%#v from %#v is not a string", v, adminsvar)
			}
		}
		xhrOutput.set("admins", admins)
	}

	if apiKey, err := cfg.String("slack.apiKey"); err != nil {
		log.Fatalf("Error reading slack.apiKey from config: %s", err.Error())
	} else {
		slack.apiKey = apiKey
	}

	if hmac, err := cfg.String("security.hmac_key"); err != nil {
		rand.Read(hmacKey)
	} else {
		hmacKey = []byte(hmac)
	}

	if auth, err := cfg.String("security.cookie.auth"); err == nil {
		if key, err := cfg.String("security.cookie.key"); err == nil {
			store = sessions.NewCookieStore([]byte(auth), []byte(key))
		} else {
			log.Fatalf("error reading security.cookie.key from config: %s", err.Error())
		}
	} else {
		log.Fatalf("error reading security.cookie.auth from config: %s", err.Error())
	}

	if cmd, err := cfg.String("slack.slashCommand.raids"); err == nil {
		raidSlashCommand = cmd
		xhrOutput.set("command", cmd)
	}

	go mindSlackMsgQueue()
	if listen, err := cfg.String("listen"); err != nil {
		log.Fatal(err)
	} else {
		var devmode = false
		if _, err := os.Stat("www/index.html"); err == nil {
			devmode = true
		}
		if devmode == false {
			http.Handle("/",
				http.FileServer(
					&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "www"}))
		} else {
			http.Handle("/", http.FileServer(http.Dir("www/")))
		}
		http.HandleFunc("/api", doHTTPPost)
		http.HandleFunc("/rest/", doRESTRouter)

		go mindChannelList()

		log.Println("Starting Up!")
		log.Fatal(http.ListenAndServe(listen, nil))
	}
}