-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
284 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package datasources | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/cosandr/go-motd/utils" | ||
) | ||
|
||
type containerStatus struct { | ||
Name string | ||
Status string | ||
} | ||
|
||
type containerList struct { | ||
Runtime string | ||
Root bool | ||
Containers []containerStatus | ||
} | ||
|
||
func (cl *containerList) toHeaderContent(ignoreList []string, failedOnly bool) (header string, content string, err error) { | ||
// Make set of ignored containers | ||
var ignoreSet utils.StringSet | ||
ignoreSet = ignoreSet.FromList(ignoreList) | ||
// Process output | ||
var goodCont = make(map[string]string) | ||
var failedCont = make(map[string]string) | ||
var sortedNames []string | ||
for _, c := range cl.Containers { | ||
if ignoreSet.Contains(c.Name) { | ||
continue | ||
} | ||
status := strings.ToLower(c.Status) | ||
if status == "up" || status == "created" || status == "running" { | ||
goodCont[c.Name] = status | ||
} else { | ||
failedCont[c.Name] = status | ||
} | ||
sortedNames = append(sortedNames, c.Name) | ||
} | ||
sort.Strings(sortedNames) | ||
|
||
// Decide what header should be | ||
if len(goodCont) == 0 { | ||
header = fmt.Sprintf("%s: %s\n", utils.Wrap(cl.Runtime, padL, padR), utils.Err("critical")) | ||
} else if len(failedCont) == 0 { | ||
header = fmt.Sprintf("%s: %s\n", utils.Wrap(cl.Runtime, padL, padR), utils.Good("OK")) | ||
if failedOnly { | ||
return | ||
} | ||
} else if len(failedCont) < len(sortedNames) { | ||
header = fmt.Sprintf("%s: %s\n", utils.Wrap(cl.Runtime, padL, padR), utils.Warn("warning")) | ||
} | ||
// Only print all containers if requested | ||
for _, c := range sortedNames { | ||
if val, ok := goodCont[c]; ok && !failedOnly { | ||
content += fmt.Sprintf("%s: %s\n", utils.Wrap(c, padL, padR), utils.Good(val)) | ||
} else if val, ok := failedCont[c]; ok { | ||
content += fmt.Sprintf("%s: %s\n", utils.Wrap(c, padL, padR), utils.Err(val)) | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package datasources | ||
|
||
import ( | ||
"bytes" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
// getContainersExec returns container status using os/exec, ~5x slower than API | ||
func getContainersExec(podman bool, sudo bool) (cl containerList, err error) { | ||
var stdout bytes.Buffer | ||
cl.Root = sudo | ||
if podman { | ||
cl.Runtime = "Podman" | ||
} else { | ||
cl.Runtime = "Docker" | ||
} | ||
var cmd *exec.Cmd | ||
if sudo { | ||
cmd = exec.Command("sudo", strings.ToLower(cl.Runtime), "ps", "--format", `"{{.Names}} {{.Status}}"`, "-a") | ||
} else { | ||
cmd = exec.Command(strings.ToLower(cl.Runtime), "ps", "--format", `"{{.Names}} {{.Status}}"`, "-a") | ||
} | ||
cmd.Stdout = &stdout | ||
err = cmd.Run() | ||
if err != nil { | ||
return | ||
} | ||
for _, c := range strings.Split(stdout.String(), "\n") { | ||
var tmp = strings.Split(strings.Trim(c, `"`), " ") | ||
if len(tmp) < 2 { | ||
continue | ||
} | ||
// tmp[0] - container name | ||
// tmp[1] - container status (up/created/exited) | ||
cl.Containers = append(cl.Containers, containerStatus{ | ||
Name: tmp[0], | ||
Status: tmp[1], | ||
}) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.