From 326fdcae6eef6dc016e99a5c2b573ca79e859579 Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Mon, 19 Jun 2017 00:01:44 +0200 Subject: [PATCH] very basic thoughts on module and file structure basic configuration basic logger --- api/api.go | 1 + config/config.go | 32 +++++++++++++++++++++++++++ environments/docker.go | 18 ++++++++++++++++ environments/environment.go | 43 +++++++++++++++++++++++++++++++++++++ logs/.gitkeep | 0 main.go | 24 +++++++++++++++++++++ services/service.go | 7 ++++++ tools/logging.go | 31 ++++++++++++++++++++++++++ 8 files changed, 156 insertions(+) create mode 100644 api/api.go create mode 100644 config/config.go create mode 100644 environments/docker.go create mode 100644 environments/environment.go create mode 100644 logs/.gitkeep create mode 100644 main.go create mode 100644 services/service.go create mode 100644 tools/logging.go diff --git a/api/api.go b/api/api.go new file mode 100644 index 0000000..778f64e --- /dev/null +++ b/api/api.go @@ -0,0 +1 @@ +package api diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..b2d5f2f --- /dev/null +++ b/config/config.go @@ -0,0 +1,32 @@ +package config + +import ( + "github.com/spf13/viper" +) + +// Config contains the configuration of the Pterodactyl Daemon +type Config struct { + + // Log contains configuration related to logging + Log struct { + + // DeleteAfterDays is the time in days after which logfiles are deleted + // If set to <= 0 logs are kept forever + DeleteAfterDays int + } `json:"log"` +} + +func LoadConfiguration() error { + viper.SetConfigName("config") + viper.AddConfigPath(".") + // Find and read the config file + if err := viper.ReadInConfig(); err != nil { + return err + } + + return nil +} + +func setDefaults() { + +} diff --git a/environments/docker.go b/environments/docker.go new file mode 100644 index 0000000..135058d --- /dev/null +++ b/environments/docker.go @@ -0,0 +1,18 @@ +package environments + +type DockerEnvironment struct { + BaseEnvironment +} + +func NewDockerEnvironment() *DockerEnvironment { + return &DockerEnvironment{} +} + +func (env *DockerEnvironment) Exec() error { + +} + +func (env *DockerEnvironment) Create() error { + + return nil +} diff --git a/environments/environment.go b/environments/environment.go new file mode 100644 index 0000000..9c06212 --- /dev/null +++ b/environments/environment.go @@ -0,0 +1,43 @@ +package environments + +// Environment provides abstraction of different environments +type Environment interface { + // Execute a command in the environment + Exec() error + + // Create creates the environment + Create() error + + // Destroy destroys the environment + Destroy() error + + // Exists checks wether the Environment exists or not + Exists() bool + + // ReCreate recreates the environment by first Destroying and then Creating + ReCreate() error +} + +type BaseEnvironment struct { +} + +func (env *BaseEnvironment) Create() error { + return nil +} + +func (env *BaseEnvironment) Destroy() error { + return nil +} + +func (env *BaseEnvironment) Exists() bool { + return false +} + +func (env *BaseEnvironment) ReCreate() error { + if env.Exists() { + if err := env.Destroy(); err != nil { + return err + } + } + return env.Create() +} diff --git a/logs/.gitkeep b/logs/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/main.go b/main.go new file mode 100644 index 0000000..f4c9af7 --- /dev/null +++ b/main.go @@ -0,0 +1,24 @@ +package main + +import ( + log "github.com/Sirupsen/logrus" + "github.com/schrej/pterodactyld/config" + "github.com/schrej/pterodactyld/tools" +) + +const ( + // Version of pterodactyld + Version = "0.0.1-alpha" +) + +func main() { + tools.ConfigureLogging() + + log.Info("Starting pterodactyld version ", Version) + + // Load configuration + log.Info("Loading configuration...") + if err := config.LoadConfiguration(); err != nil { + log.WithError(err).Fatal("Failed to find configuration file") + } +} diff --git a/services/service.go b/services/service.go new file mode 100644 index 0000000..dc1d1c3 --- /dev/null +++ b/services/service.go @@ -0,0 +1,7 @@ +package services + +import "github.com/schrej/pterodactyld/environments" + +type Service struct { + Environment environments.Environment +} diff --git a/tools/logging.go b/tools/logging.go new file mode 100644 index 0000000..e5a1d8d --- /dev/null +++ b/tools/logging.go @@ -0,0 +1,31 @@ +package tools + +import ( + "time" + + log "github.com/Sirupsen/logrus" + rotatelogs "github.com/lestrrat/go-file-rotatelogs" + "github.com/rifflock/lfshook" +) + +func ConfigureLogging() { + + log.SetFormatter(&log.TextFormatter{ + DisableTimestamp: true, + }) + + path := "logs/" + writer := rotatelogs.New( + path+"pterodactyld.%Y%m%d-%H%M.log", + rotatelogs.WithLinkName(path), + rotatelogs.WithMaxAge(time.Duration(86400)*time.Second), + rotatelogs.WithRotationTime(time.Duration(604800)*time.Second), + ) + + log.AddHook(lfshook.NewHook(lfshook.WriterMap{ + log.InfoLevel: writer, + log.ErrorLevel: writer, + log.FatalLevel: writer, + log.PanicLevel: writer, + })) +}