Esempio n. 1
0
File: site.go Progetto: safeie/nfss
// 更新站点
func (s *site) Update(data map[string]string) (msg string, err error) {
	var ok bool
	var k, v, config, configFile string

	// 开始处理站点配置文件
	config = s.siteTpl
	for _, k = range fieldSiteUpdate {
		if v, ok = data[k]; !ok || v == "" {
			if k == "alias" {
				data[k] = ""
			} else {
				return "", errors.New(k + " is empty")
			}
		}
		if k == "root" {
			data[k] = s.siteDir + data[k]
		}
		config = strings.Replace(config, "["+strings.ToUpper(k)+"]", data[k], 1)
	}
	// 日志单独处理
	log := s.logDir + data["domain"] + "_access.log"
	config = strings.Replace(config, "[LOG]", log, 1)

	// 写入配置文件
	configFile = s.nginxConfDir + data["domain"] + ".conf"
	err = ioutil.WriteFile(configFile, []byte(config), 0664)
	if err != nil {
		return "", errors.New("Nginx Config Write Error!" + err.Error())
	}

	// 创建站点目录
	ok, _ = util.IsExist(data["root"])
	if ok == false {
		err = os.Mkdir(data["root"], 0755)
		if err != nil {
			return "", errors.New("Site dir create failed!" + err.Error())
		}
	}

	// 设置站点目录权限

	// 重截Nginx使配置变更生效
	var argv []string
	if strings.Index(s.nginxBin, " ") >= 0 {
		argv = strings.Split(s.nginxBin, " ")
		s.nginxBin = argv[0]
		argv = argv[1:]
	} else {
		argv = make([]string, 0)
	}
	cmd := exec.Command(s.nginxBin, argv...)
	_, err = cmd.Output()
	if err != nil {
		return "", errors.New("Nginx reload Error!" + err.Error())
	}

	return "site update ok", nil
}
Esempio n. 2
0
File: db.go Progetto: safeie/nfss
// 检测配置文件
func (s *db) checkConfig() error {
	host, err := s.main.Conf.GetString("db", "mysqlHost")
	if err != nil {
		return err
	}
	port, err := s.main.Conf.GetString("db", "mysqlPort")
	if err != nil {
		return err
	}
	user, err := s.main.Conf.GetString("db", "mysqlUser")
	if err != nil {
		return err
	}
	pass, err := s.main.Conf.GetString("db", "mysqlPass")
	if err != nil {
		return err
	}
	backupDir, err := s.main.Conf.GetString("db", "backupDir")
	if err != nil {
		return err
	}
	var ok bool
	// 检测备份路径是否存在
	ok, _ = util.IsExist(backupDir)
	if ok == false {
		return errors.New("db backupDir is not exist!")
	}
	// 检测备份路径是否可写
	ok, _ = util.IsWritable(backupDir)
	if ok == false {
		return errors.New("db backupDir is not writable!")
	}
	s.mysqlHost = host
	s.mysqlPort = port
	s.mysqlUser = user
	s.mysqlPass = pass
	s.backupDir = backupDir
	return nil
}
Esempio n. 3
0
File: site.go Progetto: safeie/nfss
// 开启站点
func (s *site) Start(data map[string]string) (msg string, err error) {
	var ok bool
	var k, v, config, configFile string

	// 开始处理站点配置文件
	// 判断参数
	for _, k = range fieldSiteStart {
		if v, ok = data[k]; !ok || v == "" {
			return "", errors.New(k + " is empty")
		}
	}
	configFile = s.nginxConfDir + data["domain"] + ".conf"
	// 读取配置文件
	ok, _ = util.IsExist(configFile)
	if ok == false {
		return "", errors.New("Site " + data["domain"] + " not exist!")
	}
	fileHandle, err := os.OpenFile(configFile, os.O_RDWR, 0664)
	defer fileHandle.Close()
	if err != nil {
		return "", errors.New("Nginx site config file open failed!" + err.Error())
	}
	reader := bufio.NewReader(fileHandle)
	config = ""
	// 清理注释配置文件
	var i int = 0
	for {
		line, err := reader.ReadString('\n')
		if err != nil {
			if err == io.EOF {
				break
			} else {
				return "", errors.New("Nginx site config read error!" + err.Error())
			}
		}
		if i == 0 && len(line) > 0 && line[0] != '#' {
			return "", errors.New("site already started!")
		}
		config += string(line[1:])
		i++
	}
	// 写入配置文件
	fileHandle.Truncate(0)
	_, err = fileHandle.WriteAt([]byte(config), 0)
	if err != nil {
		return "", errors.New("Nginx Config Write Error!" + err.Error())
	}

	// 重截Nginx使配置变更生效
	var argv []string
	if strings.Index(s.nginxBin, " ") >= 0 {
		argv = strings.Split(s.nginxBin, " ")
		s.nginxBin = argv[0]
		argv = argv[1:]
	} else {
		argv = make([]string, 0)
	}
	cmd := exec.Command(s.nginxBin, argv...)
	_, err = cmd.Output()
	if err != nil {
		return "", errors.New("Nginx reload Error!" + err.Error())
	}

	return "site start ok", nil
}