Moving config from containers module
All checks were successful
continuous-integration/drone/push Build is passing

For better encapsulation
This commit is contained in:
Adam Štrauch 2022-02-06 00:49:32 +01:00
parent e58d6462a9
commit d43529fbb8
Signed by: cx
GPG Key ID: 018304FFA8988F8D
5 changed files with 221 additions and 50 deletions

View File

@ -8,7 +8,7 @@ import (
func TestGetProcesses(t *testing.T) {
driver := Driver{
DockerSock: "/run/user/1000/podman/podman.sock",
DockerSock: "unix:///run/user/1000/podman/podman.sock",
BindIPHTTP: "127.0.0.1",
BindIPSSH: "127.0.0.1",
}

View File

@ -7,7 +7,6 @@ import (
"strings"
"github.com/rosti-cz/node-api/apps"
"github.com/rosti-cz/node-api/common"
"github.com/rosti-cz/node-api/detector"
)
@ -24,22 +23,24 @@ type Process struct {
// Container extends App struct from App
type Container struct {
App *apps.App `json:"app"`
DockerSock string `json:"-"`
BindIPHTTP string `json:"-"`
BindIPSSH string `json:"-"`
AppsPath string `json:"-"`
}
func (c *Container) getDriver() *Driver {
config := common.GetConfig()
driver := &Driver{
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
DockerSock: c.DockerSock,
BindIPHTTP: c.BindIPHTTP,
BindIPSSH: c.BindIPSSH,
}
return driver
}
// volumeHostPath each container has one volume mounted into it,
func (c *Container) volumeHostPath() string {
config := common.GetConfig()
return path.Join(config.AppsPath, c.App.Name)
return path.Join(c.AppsPath, c.App.Name)
}
// GetRawResourceStats returns RAW CPU and memory usage directly from Docker API
@ -96,11 +97,6 @@ func (c *Container) GetState() (*apps.AppState, error) {
func (c *Container) Status() (string, error) {
status := "unknown"
// config := common.GetConfig()
// if _, err := os.Stat(path.Join(config.AppsPath, c.App.Name)); !os.IsNotExist(err) {
// status = "data-only"
// }
driver := c.getDriver()
containerStatus, err := driver.Status(c.App.Name)
if err != nil && err.Error() == "no container found" {
@ -200,8 +196,7 @@ func (c *Container) Delete() error {
}
}
config := common.GetConfig()
volumePath := path.Join(config.AppsPath, c.App.Name)
volumePath := path.Join(c.AppsPath, c.App.Name)
err = removeDirectory(volumePath)
if err != nil {
log.Println(err)

View File

@ -21,6 +21,10 @@ type Processor struct {
DB *gorm.DB
SnapshotProcessor *apps.SnapshotProcessor
WaitForAppLoops uint // each loop is five seconds
DockerSock string
BindIPHTTP string
BindIPSSH string
AppsPath string
}
// Return prepared Container instance
@ -37,6 +41,10 @@ func (p *Processor) getContainer() (containers.Container, error) {
container = docker.Container{
App: &app,
DockerSock: p.DockerSock,
BindIPHTTP: p.BindIPHTTP,
BindIPSSH: p.BindIPSSH,
AppsPath: p.AppsPath,
}
return container, nil

View File

@ -23,6 +23,10 @@ func homeHandler(c echo.Context) error {
func listAppsHandler(c echo.Context) error {
processor := glue.Processor{
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
applications, err := processor.List()
@ -40,6 +44,10 @@ func getAppHandler(c echo.Context) error {
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
app, err := processor.Get()
if err != nil {
@ -63,6 +71,10 @@ func createAppHandler(c echo.Context) error {
processor := glue.Processor{
AppName: appTemplate.Name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
if registerOnly {
@ -97,6 +109,10 @@ func updateAppHandler(c echo.Context) error {
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err = processor.Update(appTemplate)
if err != nil && strings.Contains(err.Error(), "validation error") {
@ -115,6 +131,10 @@ func stopAppHandler(c echo.Context) error {
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.Stop()
if err != nil {
@ -131,6 +151,10 @@ func startAppHandler(c echo.Context) error {
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.Start()
if err != nil {
@ -147,6 +171,10 @@ func restartAppHandler(c echo.Context) error {
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.Restart()
if err != nil {
@ -169,6 +197,10 @@ func setPasswordHandler(c echo.Context) error {
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err = processor.SetPassword(password.Password)
if err != nil {
@ -190,6 +222,10 @@ func setKeysHandler(c echo.Context) error {
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err = processor.UpdateKeys(string(body) + "\n")
if err != nil {
@ -211,6 +247,10 @@ func setServicesHandler(c echo.Context) error {
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err = processor.EnableTech(tech.Name, tech.Version)
@ -228,6 +268,10 @@ func rebuildAppHandler(c echo.Context) error {
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.Rebuild()
if err != nil {
@ -250,6 +294,10 @@ func addLabelHandler(c echo.Context) error {
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err = processor.AddLabel(label.Value)
if err != nil {
@ -272,6 +320,10 @@ func deleteLabelHandler(c echo.Context) error {
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err = processor.RemoveLabel(label.Value)
if err != nil {
@ -290,6 +342,10 @@ func deleteAppHandler(c echo.Context) error {
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.Delete()
if err != nil {
@ -309,6 +365,10 @@ func getOrphansHander(c echo.Context) error {
func getNodeInfoHandler(c echo.Context) error {
processor := glue.Processor{
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
node, err := processor.GetNode()
if err != nil {
@ -325,6 +385,10 @@ func getAppProcessesHandler(c echo.Context) error {
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
processes, err := processor.Processes()
if err != nil {
@ -341,6 +405,10 @@ func metricsHandler(c echo.Context) error {
// Node indexes
processor := glue.Processor{
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
node, err := processor.GetNode()
if err != nil {

View File

@ -95,6 +95,10 @@ func listEventHandler(m *nats.Msg, message *RequestMessage) error {
processor := glue.Processor{
AppName: message.AppName,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
applications, err := processor.List()
@ -165,6 +169,10 @@ func createEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err = processor.Create(appTemplate)
if err != nil && strings.Contains(err.Error(), "validation error") {
@ -197,6 +205,10 @@ func registerEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err = processor.Create(appTemplate)
if err != nil && strings.Contains(err.Error(), "validation error") {
@ -228,6 +240,10 @@ func updateEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err = processor.Update(appTemplate)
if err != nil && strings.Contains(err.Error(), "validation error") {
@ -249,6 +265,10 @@ func deleteEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.Delete()
if err != nil {
@ -268,6 +288,10 @@ func stopEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.Stop()
if err != nil {
@ -287,6 +311,10 @@ func startEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.Start()
if err != nil {
@ -306,6 +334,10 @@ func restartEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.Restart()
if err != nil {
@ -327,6 +359,10 @@ func updateKeysEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.UpdateKeys(body)
if err != nil {
@ -347,6 +383,10 @@ func setPasswordEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.SetPassword(password)
@ -367,6 +407,10 @@ func processesEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
processes, err := processor.Processes()
@ -426,6 +470,10 @@ func enableTechEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
processor.EnableTech(service, version)
if err != nil {
@ -445,6 +493,10 @@ func rebuildEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.Rebuild()
@ -467,6 +519,10 @@ func addLabelEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.AddLabel(label)
if err != nil {
@ -488,6 +544,10 @@ func removeLabelEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.RemoveLabel(label)
if err != nil {
@ -533,6 +593,10 @@ func getNodeEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
node, err := processor.GetNode()
@ -575,6 +639,10 @@ func createSnapshotEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.CreateSnapshot(strings.Split(message.Payload, ","))
@ -605,6 +673,10 @@ func restoreFromSnapshotEventHandler(m *nats.Msg, message *RequestMessage) error
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.RestoreFromSnapshot(message.Payload)
@ -633,6 +705,10 @@ func listSnapshotsEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
snapshots, err := processor.ListSnapshots()
if err != nil {
@ -666,6 +742,10 @@ func listAppsSnapshotsEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
snapshots, err := processor.ListAppsSnapshots(strings.Split(message.Payload, ","))
if err != nil {
@ -699,6 +779,10 @@ func listSnapshotsByLabelEventHandler(m *nats.Msg, message *RequestMessage) erro
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
snapshots, err := processor.ListSnapshotsByLabel(message.Payload)
if err != nil {
@ -732,6 +816,10 @@ func getSnapshotEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
snapshot, err := processor.GetSnapshot(message.Payload)
if err != nil {
@ -762,6 +850,10 @@ func getSnapshotDownloadLinkEventHandler(m *nats.Msg, message *RequestMessage) e
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
link, err := processor.GetSnapshotDownloadLink(message.Payload)
if err != nil {
@ -796,6 +888,10 @@ func deleteSnapshotEventHandler(m *nats.Msg, message *RequestMessage) error {
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.DeleteSnapshot(message.Payload)
@ -821,6 +917,10 @@ func deleteAppSnapshotsEventHandler(m *nats.Msg, message *RequestMessage) error
AppName: message.AppName,
DB: common.GetDBConnection(),
SnapshotProcessor: &snapshotProcessor,
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.DeleteAppSnapshots()