sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

host create folders

This commit is contained in:
Martin Geno 2016-12-07 23:41:14 +01:00
parent 67ee4a8559
commit 8818a82b9f
3 changed files with 69 additions and 2 deletions

View File

@ -12,12 +12,23 @@ type Config struct {
Log struct {
Path string `yaml:"path"`
} `yaml:"log"`
DatabaseDebug bool `yaml:"databasedebug"`
DatabaseDebug bool `yaml:"databasedebug"`
PathUser string `yaml:"path_user"`
PathGroup string `yaml:"path_group"`
Web struct {
Enable bool `yaml:"enable"`
ConfigurationTemplate string `yaml:"template"`
ConfigurationFile string `yaml:"config"`
PathDomain string `yaml:"path_domain"`
} `yaml:"web"`
WebModul struct {
Enable bool `yaml:"enable"`
PathID string `yaml:"path_id"`
} `yaml:"web_modul"`
FTP struct {
Enable bool `yaml:"enable"`
PathID string `yaml:"path_id"`
} `yaml:"ftp"`
Database struct {
Enable bool `yaml:"enable"`
Type string `yaml:"type"`

View File

@ -3,10 +3,16 @@ warehostdatabase: "host=localhost user=warehost dbname=warehost password=hallo s
log:
path: test.log
databasedebug: false
path_user: http
path_group: http
ftp:
enable: true
path_id: /tmp/ftp/%d/
web:
enable: true
template: caddy
config: /tmp/a
path_domain: /tmp/b
database:
enable: true
type: mysql

View File

@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"os"
"os/user"
"strconv"
"strings"
"text/template"
@ -15,6 +16,7 @@ import (
liblog "dev.sum7.eu/sum7/warehost/lib/log"
host "dev.sum7.eu/sum7/warehost/modul/host"
web "dev.sum7.eu/sum7/warehost/modul/web"
system "dev.sum7.eu/sum7/warehost/system"
)
@ -45,8 +47,44 @@ func main() {
//load system Models to database
system.SyncModels(dbconnection)
web.SyncModels(dbconnection)
host.SyncModels(dbconnection)
u, _ := user.Lookup(config.PathUser)
userid, _ := strconv.Atoi(u.Uid)
g, _ := user.LookupGroup(config.PathGroup)
groupid, _ := strconv.Atoi(g.Gid)
if config.FTP.Enable {
var logins []*system.Login
dbconnection.Joins("LEFT JOIN host_profil profil ON profil.login=login.id").Where("profil.id IS NOT NULL").Find(&logins)
for _, login := range logins {
path := fmt.Sprintf(config.FTP.PathID, login.ID)
err = os.Mkdir(path, 0770)
if err != nil && !strings.Contains(err.Error(), "file exists") {
liblog.Log.Warn("ftp folder: ", err)
} else {
os.Chown(path, userid, groupid)
liblog.Log.Info("ftp folder: ", path)
}
}
}
if config.WebModul.Enable {
var sites []*web.Website
dbconnection.Find(&sites)
for _, site := range sites {
path := fmt.Sprintf(config.WebModul.PathID, site.ID)
err = os.Mkdir(path, 0770)
if err != nil && !strings.Contains(err.Error(), "file exists") {
liblog.Log.Warn("web modul folder: ", err)
} else {
os.Chown(path, userid, groupid)
liblog.Log.Info("web modul folder: ", path)
}
}
}
// Configurate Webserver
if config.Web.Enable {
tmpl, err := template.ParseFiles(config.Web.ConfigurationTemplate)
@ -63,9 +101,21 @@ func main() {
dbconnection.Preload("Domain.Profil.Login").Order("length(subdomain) asc").Find(&web)
for _, item := range web {
if item.Domain.Active && item.Domain.Web && item.Domain.Profil.Login.Active {
path := config.Web.PathDomain
if len(item.Subdomain) > 0 {
path = fmt.Sprintf("%s/%s.%s", path, item.Subdomain, item.Domain.FQDN)
} else {
path = fmt.Sprintf("%s/%s", path, item.Domain.FQDN)
}
err = os.Mkdir(path, 0770)
if err != nil && !strings.Contains(err.Error(), "file exists") {
liblog.Log.Warn("domain folder: ", err)
} else {
os.Chown(path, userid, groupid)
}
err = tmpl.Execute(file, item)
if err != nil {
liblog.Log.Warning("write config: ", err)
liblog.Log.Warn("write config: ", err)
}
}
}