From 6555f720ebfdb3c546422c6b085744f82caa4e7c Mon Sep 17 00:00:00 2001 From: George Shaw Date: Thu, 18 Jan 2018 15:33:21 -0600 Subject: [PATCH] restructured to serve out of /home/[bundle_name]/document_root/ and also store logs in /home/[bundle_name]/logs, removing the need to keep track of anything other than .db files within the program for each bundle. the log read/write/truncate api's need fixed though --- .gitignore | 4 +- bundles/README.md | 3 + bundles/default_bundle/logs/README.md | 9 -- bundles/default_bundle/public/test.html | 10 --- pkg/api/bundle/create.go | 9 +- pkg/api/settings/bundle_name.go | 4 +- pkg/file/dircopy.go | 112 ------------------------ pkg/gpaccount/gpaccount.go | 4 +- pkg/gpserver/bundles.go | 11 +-- pkg/gpserver/gpserver.go | 3 +- pkg/public/public.go | 53 ++++++++--- pkg/public/servehttp.go | 4 +- pkg/system/constants.go | 35 ++++++++ pkg/system/user.go | 18 ++++ 14 files changed, 113 insertions(+), 166 deletions(-) create mode 100644 bundles/README.md delete mode 100644 bundles/default_bundle/logs/README.md delete mode 100644 bundles/default_bundle/public/test.html delete mode 100644 pkg/file/dircopy.go create mode 100644 pkg/system/constants.go diff --git a/.gitignore b/.gitignore index 6c6cf0d..da0f825 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,9 @@ main gpanel -# IGNORE ALL BUNDLES EXCEPT FOR DEFAULT_BUNDLE +# IGNORE ALL BUNDLES bundles/* -!bundles/default_bundle +!bundles/README.md # IGNORE ALL FILES IN /LOG EXCEPT README **/logs/* diff --git a/bundles/README.md b/bundles/README.md new file mode 100644 index 0000000..6d6c58f --- /dev/null +++ b/bundles/README.md @@ -0,0 +1,3 @@ +# Bundles + +This folder is where all bundles created will exist. The bundle folders will hold .db files that contain data relevant to each bundle. \ No newline at end of file diff --git a/bundles/default_bundle/logs/README.md b/bundles/default_bundle/logs/README.md deleted file mode 100644 index 9ee4345..0000000 --- a/bundles/default_bundle/logs/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# gPanel Logs - -The actual log files are ignored by the .gitignore file. They are generated automatically upon being needed within the software itself. - -Current log files: -- client_errors.log -- account_errors.log -- server_errors.log -- loadtime.log diff --git a/bundles/default_bundle/public/test.html b/bundles/default_bundle/public/test.html deleted file mode 100644 index 94e2867..0000000 --- a/bundles/default_bundle/public/test.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - Public Test - - -

Public Test

-

My public test paragraph.

- - diff --git a/pkg/api/bundle/create.go b/pkg/api/bundle/create.go index 23f3f81..194be40 100644 --- a/pkg/api/bundle/create.go +++ b/pkg/api/bundle/create.go @@ -8,10 +8,11 @@ import ( "net/http" "strconv" + "os" + "github.com/Ennovar/gPanel/pkg/database" "github.com/Ennovar/gPanel/pkg/emailer" "github.com/Ennovar/gPanel/pkg/encryption" - "github.com/Ennovar/gPanel/pkg/file" "github.com/Ennovar/gPanel/pkg/gpaccount" "github.com/Ennovar/gPanel/pkg/system" ) @@ -75,8 +76,8 @@ func Create(res http.ResponseWriter, req *http.Request, logger *log.Logger, bund return false } - newBundle := "bundles/bundle_" + createBundleRequestData.Name - err = file.CopyDir("bundles/default_bundle", newBundle) + newBundle := "bundles/" + createBundleRequestData.Name + err = os.Mkdir(newBundle, 0777) if err != nil { logger.Println(req.URL.Path + "::" + err.Error()) http.Error(res, err.Error(), http.StatusInternalServerError) @@ -131,7 +132,7 @@ func Create(res http.ResponseWriter, req *http.Request, logger *log.Logger, bund return false } - bundles[createBundleRequestData.Name] = gpaccount.New(newBundle+"/", databaseBundlePorts.Account, databaseBundlePorts.Public) + bundles[createBundleRequestData.Name] = gpaccount.New(newBundle+"/", createBundleRequestData.Name, databaseBundlePorts.Account, databaseBundlePorts.Public) _ = bundles[createBundleRequestData.Name].Start() _ = bundles[createBundleRequestData.Name].Public.Start() diff --git a/pkg/api/settings/bundle_name.go b/pkg/api/settings/bundle_name.go index f5c014b..bfde6ff 100644 --- a/pkg/api/settings/bundle_name.go +++ b/pkg/api/settings/bundle_name.go @@ -1,10 +1,10 @@ package settings import ( + "log" "net/http" "strconv" "strings" - "log" ) func BundleName(res http.ResponseWriter, req *http.Request, logger *log.Logger, dir string) bool { @@ -14,7 +14,7 @@ func BundleName(res http.ResponseWriter, req *http.Request, logger *log.Logger, return false } - dir = strings.Replace(dir, "bundles/bundle_", "", 1) + dir = strings.Replace(dir, "bundles/", "", 1) dir = strings.Replace(dir, "/", "", 1) res.WriteHeader(http.StatusOK) diff --git a/pkg/file/dircopy.go b/pkg/file/dircopy.go deleted file mode 100644 index 415fa96..0000000 --- a/pkg/file/dircopy.go +++ /dev/null @@ -1,112 +0,0 @@ -// Package file handles various file operations -package file - -import ( - "errors" - "io" - "io/ioutil" - "os" - "path/filepath" -) - -// CopyFile copies the contents of the file named src to the file named -// by dst. The file will be created if it does not already exist. If the -// destination file exists, all it's contents will be replaced by the contents -// of the source file. The file mode will be copied from the source and -// the copied data is synced/flushed to stable storage. -func CopyFile(src, dst string) error { - in, err := os.Open(src) - if err != nil { - return err - } - defer in.Close() - - out, err := os.Create(dst) - if err != nil { - return err - } - defer func() { - if e := out.Close(); e != nil { - err = e - } - }() - - _, err = io.Copy(out, in) - if err != nil { - return err - } - - err = out.Sync() - if err != nil { - return err - } - - si, err := os.Stat(src) - if err != nil { - return err - } - err = os.Chmod(dst, si.Mode()) - if err != nil { - return err - } - - return nil -} - -// CopyDir recursively copies a directory tree, attempting to preserve permissions. -// Source directory must exist, destination directory must *not* exist. -// Symlinks are ignored and skipped. -func CopyDir(src string, dst string) error { - src = filepath.Clean(src) - dst = filepath.Clean(dst) - - si, err := os.Stat(src) - if err != nil { - return err - } - if !si.IsDir() { - return errors.New("source is not a directory") - } - - _, err = os.Stat(dst) - if err != nil && !os.IsNotExist(err) { - return err - } - if err == nil { - return errors.New("destination already exists") - } - - err = os.MkdirAll(dst, si.Mode()) - if err != nil { - return err - } - - entries, err := ioutil.ReadDir(src) - if err != nil { - return err - } - - for _, entry := range entries { - srcPath := filepath.Join(src, entry.Name()) - dstPath := filepath.Join(dst, entry.Name()) - - if entry.IsDir() { - err = CopyDir(srcPath, dstPath) - if err != nil { - return err - } - } else { - // Skip symlinks. - if entry.Mode()&os.ModeSymlink != 0 { - continue - } - - err = CopyFile(srcPath, dstPath) - if err != nil { - return err - } - } - } - - return nil -} diff --git a/pkg/gpaccount/gpaccount.go b/pkg/gpaccount/gpaccount.go index bde0078..5fdb31b 100644 --- a/pkg/gpaccount/gpaccount.go +++ b/pkg/gpaccount/gpaccount.go @@ -27,7 +27,7 @@ var controller Controller var httpserver http.Server // New returns a new Controller reference. -func New(dir string, accPort int, pubPort int) *Controller { +func New(dir, name string, accPort, pubPort int) *Controller { f, err := os.OpenFile(dir+"logs/account_errors.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { fmt.Println("error whilst trying to start server logging instance:", err.Error()) @@ -40,7 +40,7 @@ func New(dir string, accPort int, pubPort int) *Controller { Directory: dir, DocumentRoot: "account/", Port: accPort, - Public: public.New(dir, pubPort), + Public: public.New("/home/"+name+"/", pubPort), GracefulShutdownTimeout: 5 * time.Second, Status: 0, AccountLogger: accountLogger, diff --git a/pkg/gpserver/bundles.go b/pkg/gpserver/bundles.go index 18ac894..c1d4a93 100644 --- a/pkg/gpserver/bundles.go +++ b/pkg/gpserver/bundles.go @@ -3,7 +3,6 @@ package gpserver import ( "fmt" "io/ioutil" - "strings" "github.com/Ennovar/gPanel/pkg/api/bundle" "github.com/Ennovar/gPanel/pkg/gpaccount" @@ -18,15 +17,11 @@ func (con *Controller) detectBundles() { } for _, dir := range dirs { - if dir.Name() == "default_bundle" || !dir.IsDir() { - continue - } - - if strings.HasPrefix(dir.Name(), "bundle_") { + if dir.IsDir() { dirPath := "bundles/" + dir.Name() + "/" err, accPort, pubPort := bundle.GetPorts(dirPath) - curBundle := gpaccount.New(dirPath, accPort, pubPort) + curBundle := gpaccount.New(dirPath, dir.Name(), accPort, pubPort) err = curBundle.Start() err2 := curBundle.Public.Start() @@ -34,7 +29,7 @@ func (con *Controller) detectBundles() { fmt.Println("error starting bundle:", dir.Name()) } - bundles[strings.Replace(dir.Name(), "bundle_", "", 1)] = curBundle + bundles[dir.Name()] = curBundle } } diff --git a/pkg/gpserver/gpserver.go b/pkg/gpserver/gpserver.go index 05ecd87..dc62403 100644 --- a/pkg/gpserver/gpserver.go +++ b/pkg/gpserver/gpserver.go @@ -2,7 +2,6 @@ package gpserver import ( - "fmt" "log" "os" @@ -20,7 +19,7 @@ type Controller struct { func New() *Controller { f, err := os.OpenFile("server/logs/server_errors.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { - fmt.Println("error whilst trying to start server logging instance:", err.Error()) + log.Fatalf("error whilst trying to start server logging instance:%v", err.Error()) } c := Controller{ diff --git a/pkg/public/public.go b/pkg/public/public.go index 2c3cd2e..3fffde7 100644 --- a/pkg/public/public.go +++ b/pkg/public/public.go @@ -2,10 +2,10 @@ package public import ( - "fmt" "log" "net/http" "os" + "path/filepath" "strconv" "time" ) @@ -24,26 +24,18 @@ var server http.Server // New function returns a new PublicWeb type. func New(dir string, port int) *Controller { - f, err := os.OpenFile(dir+"logs/public_errors.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) + ph, lh, err := getLogHandles(dir) if err != nil { - fmt.Errorf("Error whilst trying to start public logging instance: %v\n", err.Error()) + log.Fatalf("Error trying to start logging instances within %v: %v", dir, err.Error()) } - fh, err := os.OpenFile(dir+"logs/public_load_time.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) - if err != nil { - fmt.Errorf("Error whilst trying to start public load time logging instance: %v\n", err.Error()) - } - - publicLogger := log.New(f, "PUBLIC :: ", 3) - loadLogger := log.New(fh, "LOAD :: ", 3) - controller = Controller{ Directory: dir, Port: port, GracefulShutdownTimeout: 5 * time.Second, Status: 0, - PublicLogger: publicLogger, - LoadTimeLogger: loadLogger, + PublicLogger: ph, + LoadTimeLogger: lh, } server = http.Server{ @@ -56,3 +48,38 @@ func New(dir string, port int) *Controller { return &controller } + +// Function getLogHandles returns the handle for the public logger, load logger, +// and, if applicable, an error all in that order. +func getLogHandles(dir string) (*log.Logger, *log.Logger, error) { + var dirpath, pubpath, loadpath string + var err error + + if dirpath, err = filepath.Abs(dir + "logs/"); err != nil { + return nil, nil, err + } + if pubpath, err = filepath.Abs(dir + "logs/public_errors.log"); err != nil { + return nil, nil, err + } + if loadpath, err = filepath.Abs(dir + "logs/public_load_time.log"); err != nil { + return nil, nil, err + } + + if _, err = os.Stat(dirpath); os.IsNotExist(err) { + if err := os.Mkdir(dirpath, 0777); err != nil { + return nil, nil, err + } + } + + f, err := os.OpenFile(pubpath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777) + if err != nil { + return nil, nil, err + } + + fh, err := os.OpenFile(loadpath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777) + if err != nil { + return nil, nil, err + } + + return log.New(f, "PUBLIC :: ", 3), log.New(fh, "LOAD :: ", 3), nil +} diff --git a/pkg/public/servehttp.go b/pkg/public/servehttp.go index 2652691..368caef 100644 --- a/pkg/public/servehttp.go +++ b/pkg/public/servehttp.go @@ -93,9 +93,9 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) { path := req.URL.Path[1:] if len(path) == 0 { - path = con.Directory + "public/" + "index.html" + path = con.Directory + "document_root/" + "index.html" } else { - path = con.Directory + "public/" + path + path = con.Directory + "document_root/" + path } contentType, err := routing.GetContentType(path) diff --git a/pkg/system/constants.go b/pkg/system/constants.go new file mode 100644 index 0000000..a4a734a --- /dev/null +++ b/pkg/system/constants.go @@ -0,0 +1,35 @@ +package system + +const DEFAULT_INDEX = ` + + + New gPanel Bundle Instance + + + + +
+

Welcome to Your New gPanel Bundle Instance!

+

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Rhoncus ut odio sit amet, tincidunt ornare mi. Nunc malesuada mi ac pulvinar porta. Ut et leo ultricies, accumsan augue in, porta erat. Maecenas tempor elit justo, non consectetur tellus suscipit non. Proin nec turpis dolor. Etiam nec magna mi. Nulla vitae condimentum nisi. Donec id lectus sit amet magna mollis aliquet ac ac tellus. Phasellus bibendum mollis nulla, vel ornare lacus fermentum ac. Proin ut sollicitudin diam, id pellentesque enim. Quisque mollis mollis blandit. Maecenas laoreet diam ac suscipit facilisis. Donec nibh purus, pretium ut congue nec, aliquet at turpis. Nulla quis gravida elit. Pellentesque luctus, massa non rutrum congue, leo felis egestas augue, id lacinia justo elit iaculis nibh.

+ + +

To utilize your new bundle instance:

+ +
+ +` diff --git a/pkg/system/user.go b/pkg/system/user.go index 9d90518..c6f0d61 100644 --- a/pkg/system/user.go +++ b/pkg/system/user.go @@ -3,6 +3,8 @@ package system import ( "bytes" "errors" + "fmt" + "os" "os/exec" ) @@ -74,6 +76,15 @@ func CreateBundleUser(username string) (error, error) { return err, errors.New(cerr.String()) } + // Add default index page + f, err := os.OpenFile("/home/"+username+"/document_root/index.html", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777) + if err != nil { + return err, nil + } + defer f.Close() + + fmt.Fprintln(f, DEFAULT_INDEX) + /* OWNERSHIP AND FILE PERMISSIONS START */ cmd = exec.Command("chmod", "700", "/home/"+username+"/.ssh") cmd.Stderr = &cerr @@ -127,6 +138,13 @@ func CreateBundleUser(username string) (error, error) { cmd = exec.Command("chown", username+":", "/home/"+username+"/.ssh/authorized_keys") cmd.Stderr = &cerr + if err = cmd.Run(); err != nil { + return err, errors.New(cerr.String()) + } + + cmd = exec.Command("chown", username+":", "/home/"+username+"/document_root") + cmd.Stderr = &cerr + if err = cmd.Run(); err != nil { return err, errors.New(cerr.String()) }