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"
)
@ -23,23 +22,25 @@ type Process struct {
// Container extends App struct from App
type Container struct {
App *apps.App `json:"app"`
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
@ -36,7 +40,11 @@ func (p *Processor) getContainer() (containers.Container, error) {
}
container = docker.Container{
App: &app,
App: &app,
DockerSock: p.DockerSock,
BindIPHTTP: p.BindIPHTTP,
BindIPSSH: p.BindIPSSH,
AppsPath: p.AppsPath,
}
return container, nil

View File

@ -22,7 +22,11 @@ func homeHandler(c echo.Context) error {
func listAppsHandler(c echo.Context) error {
processor := glue.Processor{
DB: common.GetDBConnection(),
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
applications, err := processor.List()
@ -38,8 +42,12 @@ func getAppHandler(c echo.Context) error {
name := c.Param("name")
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
app, err := processor.Get()
if err != nil {
@ -61,8 +69,12 @@ func createAppHandler(c echo.Context) error {
}
processor := glue.Processor{
AppName: appTemplate.Name,
DB: common.GetDBConnection(),
AppName: appTemplate.Name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
if registerOnly {
@ -95,8 +107,12 @@ func updateAppHandler(c echo.Context) error {
}
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
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") {
@ -113,8 +129,12 @@ func stopAppHandler(c echo.Context) error {
name := c.Param("name")
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.Stop()
if err != nil {
@ -129,8 +149,12 @@ func startAppHandler(c echo.Context) error {
name := c.Param("name")
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.Start()
if err != nil {
@ -145,8 +169,12 @@ func restartAppHandler(c echo.Context) error {
name := c.Param("name")
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.Restart()
if err != nil {
@ -167,8 +195,12 @@ func setPasswordHandler(c echo.Context) error {
}
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
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 {
@ -188,8 +220,12 @@ func setKeysHandler(c echo.Context) error {
}
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
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 {
@ -209,8 +245,12 @@ func setServicesHandler(c echo.Context) error {
}
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err = processor.EnableTech(tech.Name, tech.Version)
@ -226,8 +266,12 @@ func rebuildAppHandler(c echo.Context) error {
name := c.Param("name")
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.Rebuild()
if err != nil {
@ -248,8 +292,12 @@ func addLabelHandler(c echo.Context) error {
}
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
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 {
@ -270,8 +318,12 @@ func deleteLabelHandler(c echo.Context) error {
}
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
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 {
@ -288,8 +340,12 @@ func deleteAppHandler(c echo.Context) error {
go func(name string) {
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
err := processor.Delete()
if err != nil {
@ -308,7 +364,11 @@ func getOrphansHander(c echo.Context) error {
// Return info about the node including performance index
func getNodeInfoHandler(c echo.Context) error {
processor := glue.Processor{
DB: common.GetDBConnection(),
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
node, err := processor.GetNode()
if err != nil {
@ -323,8 +383,12 @@ func getAppProcessesHandler(c echo.Context) error {
name := c.Param("name")
processor := glue.Processor{
AppName: name,
DB: common.GetDBConnection(),
AppName: name,
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
processes, err := processor.Processes()
if err != nil {
@ -340,7 +404,11 @@ func metricsHandler(c echo.Context) error {
// Node indexes
processor := glue.Processor{
DB: common.GetDBConnection(),
DB: common.GetDBConnection(),
DockerSock: config.DockerSocket,
BindIPHTTP: config.AppsBindIPHTTP,
BindIPSSH: config.AppsBindIPSSH,
AppsPath: config.AppsPath,
}
node, err := processor.GetNode()
if err != nil {

View File

@ -93,8 +93,12 @@ func listEventHandler(m *nats.Msg, message *RequestMessage) error {
log.Println("> List")
processor := glue.Processor{
AppName: message.AppName,
DB: common.GetDBConnection(),
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()