config_manager_go/config_manager.go

208 lines
4.2 KiB
Go
Raw Normal View History

2017-04-27 10:02:08 +00:00
package config_manager_go
import (
"encoding/json"
"gopkg.in/yaml.v2"
"io/ioutil"
"loafle.com/overflow/agent_api/config_manager"
"loafle.com/overflow/agent_api/observer"
"loafle.com/overflow/agent_api/observer/messages"
"log"
"os"
"strings"
"sync"
)
type configManager struct {
config_manager.ConfigManager // interface implements
addCh chan interface{}
removeCh chan interface{}
2017-04-27 11:34:07 +00:00
configs map[string] *config_manager.Config
2017-04-27 10:02:08 +00:00
globalConfig config_manager.GlobalConfig
}
var _configManager *configManager
func init() {
var once sync.Once
once.Do(func() {
_configManager = NewConfigManager()
})
// regist handlers
agentStartHandler()
addSensorHandler()
removeSensorHandler()
2017-04-28 02:55:57 +00:00
agentEndHandler()
2017-04-27 10:02:08 +00:00
}
2017-04-28 02:55:57 +00:00
2017-04-27 10:02:08 +00:00
func NewConfigManager() *configManager {
c := &configManager{
configs: make(map[string]*config_manager.Config, 0),
addCh: make(chan interface{}, 0),
removeCh: make(chan interface{}, 0),
}
return c
}
func (c *configManager) getConfigPath() string {
return c.globalConfig.Paths.RootFolder + c.globalConfig.Paths.ConfigFolder
}
func (c *configManager) getContainerPath() string {
return c.getConfigPath() + "container/"
}
2017-05-10 06:14:31 +00:00
func (c *configManager) getConfigFilePath(config *config_manager.Config) string {
return c.getContainerPath() + appendSeperator(config.Crawler.Container) + appendSeperator(config.Crawler.Name) + config.Id
}
2017-04-27 10:02:08 +00:00
func appendSeperator(str string) string {
if strings.LastIndex(str, "/") != len(str)-1 {
return str + "/"
}
return str
}
func (c *configManager) loadGlobalConfig(path string) {
data, err := ioutil.ReadFile(path)
if err != nil {
}
err = yaml.Unmarshal([]byte(data), &c.globalConfig)
if err != nil {
}
}
func (c *configManager) loadCrawlerConfigAll() error {
root := c.getConfigPath()
files, err := ioutil.ReadDir(root)
if err != nil {
log.Panic(err)
}
for _, file := range files {
if file.IsDir() == true {
c.loadCrawlerConfig(root, file.Name())
}
}
return nil
}
func (c *configManager) loadCrawlerConfig(root string, dir string) {
separator := ""
if strings.LastIndex(root, "/") != len(root)-1 {
separator = "/"
}
currentDir := root + separator + dir
files, err := ioutil.ReadDir(currentDir)
if err != nil {
log.Panic(err)
}
for _, file := range files {
2017-05-10 06:14:31 +00:00
// 디렉터리라면 재귀 / 파일이라면 설정 로드
2017-04-27 10:02:08 +00:00
if file.IsDir() == true {
c.loadCrawlerConfig(currentDir, file.Name())
} else {
b, err := ioutil.ReadFile(currentDir + separator + file.Name())
if err != nil {
log.Panic(err)
}
var m = config_manager.Config{}
json.Unmarshal(b, &m)
c.configs[file.Name()] = &m
}
}
}
func (c *configManager) addConfig() {
for data := range c.addCh {
path := data.(string)
b, err := ioutil.ReadFile(path)
if err != nil {
// error process
continue
}
// 임시파일을 로드 , Config로 변환
var m = config_manager.Config{}
err = json.Unmarshal(b, &m)
if err != nil {
// error process
continue
}
// agent 폴더 / config / crawler / .. / .. / .. / 에 해당하는 파일이 있는지 확인, 있다면 삭제
// Config 파일로 저장
2017-05-10 06:14:31 +00:00
savePath := c.getConfigFilePath(&m)
2017-04-27 10:02:08 +00:00
ioutil.WriteFile(savePath, b, 0644)
// tempfile remove
err = os.Remove(path)
if err != nil {
// error process
continue
}
// Config 맵에 저장
c.configs[m.Id] = &m
// notify id
2017-05-11 08:02:44 +00:00
err = observer.Notify(messages.SCF_SENSOR_ADD_DONE, m.Id)
2017-04-27 10:02:08 +00:00
if err != nil {
continue
}
}
}
func (c *configManager) removeConfig() {
for data := range c.removeCh {
removeid := data.(string)
// check exists
config, ok := c.configs[removeid]
if !ok {
continue
}
// 해당 파일 삭제
2017-05-10 06:14:31 +00:00
path := c.getConfigFilePath(config)
2017-04-27 10:02:08 +00:00
err := os.Remove(path)
if err != nil {
// error check
continue
}
// 해당 id 삭제
delete(c.configs, removeid)
// notify id
2017-05-11 08:02:44 +00:00
err = observer.Notify("TASK_DONE", removeid)
2017-04-27 10:02:08 +00:00
if err != nil {
continue
}
}
}
2017-04-28 02:55:57 +00:00
2017-04-27 10:02:08 +00:00
// implements methods
func (c *configManager) GetGlobalConfig() *config_manager.GlobalConfig {
return &c.globalConfig
}
2017-04-28 02:39:17 +00:00
func (c *configManager) GetSensorById(id string) *config_manager.Config {
2017-04-27 10:02:08 +00:00
return c.configs[id]
}
2017-04-28 02:39:17 +00:00
func (c *configManager) GetSensors() map[string] *config_manager.Config {
2017-04-27 10:02:08 +00:00
return c.configs
}