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

This commit is contained in:
George Shaw 2018-01-18 15:33:21 -06:00
parent 0db133f475
commit 6555f720eb
14 changed files with 113 additions and 166 deletions

4
.gitignore vendored
View file

@ -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/*

3
bundles/README.md Normal file
View file

@ -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.

View file

@ -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

View file

@ -1,10 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Public Test</title>
</head>
<body>
<h1>Public Test</h1>
<p>My public test paragraph.</p>
</body>
</html>

View file

@ -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()

View file

@ -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)

View file

@ -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
}

View file

@ -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,

View file

@ -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
}
}

View file

@ -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{

View file

@ -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
}

View file

@ -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)

35
pkg/system/constants.go Normal file
View file

@ -0,0 +1,35 @@
package system
const DEFAULT_INDEX = `<!DOCTYPE html>
<html lang="en">
<head>
<title>New gPanel Bundle Instance</title>
<style>
body {
background-color:#E0EBF5;
color:#747d84;
}
.wrapper {
width:960px;
height:auto;
margin:0px auto;
}
</style>
</head>
<body>
<div class="wrapper">
<h1>Welcome to Your New gPanel Bundle Instance!</h1>
<p>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.</p>
<p>To utilize your new bundle instance:</p>
<ul>
<li>Log into your bundle account</li>
<li>Use the interface to register your SSH keys</li>
<li>Create/Upload/Transfer files onto your server account</li>
</ul>
</div>
</body>
</html>`

View file

@ -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())
}