Skip to content

Commit

Permalink
#276: Update logging mechanics (#277)
Browse files Browse the repository at this point in the history
* Update logging mechanics
* satisfy golangci-lint
* Cleanup ssh debugging
* gofmt
* Adjust ssh-add output to expected value.
  • Loading branch information
fubarhouse committed Nov 27, 2020
1 parent da80a22 commit 54474e8
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 52 deletions.
2 changes: 1 addition & 1 deletion cmd/addkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var addkeyCmd = &cobra.Command{

Key, _ := cmd.Flags().GetString("key")

_, e := library.SshKeyAdd(c, Key, 0)
e := library.SshKeyAdd(c, Key, 0)
if e != nil {
fmt.Println(e)
}
Expand Down
11 changes: 6 additions & 5 deletions service/interface/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,25 +448,26 @@ func DockerContainerStart(ID string, options types.ContainerStartOptions) error
return err
}

func DockerContainerLogs(ID string, Follow bool) ([]byte, error) {
func DockerContainerLogs(ID string) ([]byte, error) {
ctx := context.Background()
cli, err := client.NewClientWithOpts()
cli.NegotiateAPIVersion(ctx)
if err != nil {
return []byte{}, err
}
b, _ := cli.ContainerLogs(ctx, ID, types.ContainerLogsOptions{
b, e := cli.ContainerLogs(ctx, ID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: Follow,
})

if e != nil {
return []byte{}, e
}

buf := new(bytes.Buffer)
if _, f := buf.ReadFrom(b); f != nil {
fmt.Println(f)
}

b.Close()

return buf.Bytes(), nil
}
67 changes: 49 additions & 18 deletions service/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ func (Service *Service) Setup() error {
// Start will perform a series of checks to see if the container starting
// is supposed be removed before-hand and will check to see if the
// container is running before it is actually started.
func (Service *Service) Start() ([]byte, error) {
func (Service *Service) Start() error {

name, err := Service.GetFieldString("name")
discrete, _ := Service.GetFieldBool("discrete")
output, _ := Service.GetFieldBool("output")
purpose, _ := Service.GetFieldString("purpose")

if err != nil {
return []byte{}, nil
return nil
}

s := false
Expand All @@ -56,13 +57,13 @@ func (Service *Service) Start() ([]byte, error) {
var e error
s, e = Service.Status()
if e != nil {
return []byte{}, e
return e
}
}

if s && !Service.HostConfig.AutoRemove && !discrete {
fmt.Printf("Already running %v\n", name)
return []byte{}, nil
return nil
}

if purpose == "addkeys" || purpose == "showkeys" {
Expand All @@ -75,21 +76,28 @@ func (Service *Service) Start() ([]byte, error) {

}

output, err := Service.DockerRun()
err = Service.DockerRun()
if err != nil {
return []byte{}, err
return err
}

l, _ := Service.DockerLogs()
if output && string(l) != "" {
fmt.Println(string(l))
}

if c, err := Service.GetRunning(); c.ID != "" {
if !Service.HostConfig.AutoRemove && !discrete {
if !discrete {
fmt.Printf("Successfully started %v\n", name)
} else if Service.HostConfig.AutoRemove && err != nil {
return nil
}
if err != nil {
// We cannot guarantee this container is running at this point if it is to be removed.
return output, fmt.Errorf("Failed to run %v: %v\n", name, err)
return fmt.Errorf("Failed to run %v: %v\n", name, err)
}
}

return output, nil
return nil
}

// Status will check if the container is running.
Expand Down Expand Up @@ -200,14 +208,37 @@ func (Service *Service) Stop() error {
// _ will ensure DockerService is implemented by Service.
var _ DockerService = (*Service)(nil)

// DockerLogs will return the logs from the container.
func (Service *Service) DockerLogs() ([]byte, error) {
ctx := context.Background()
cli, err := client.NewClientWithOpts()
cli.NegotiateAPIVersion(ctx)
if err != nil {
return []byte{}, err
}

name, _ := Service.GetFieldString("name")
log, e := docker.DockerContainerLogs(name)

if e != nil {
return []byte{}, err
}

if string(log) != "" {
return log, nil
}

return []byte{}, nil
}

// DockerRun will setup and run a given container.
func (Service *Service) DockerRun() ([]byte, error) {
func (Service *Service) DockerRun() error {

ctx := context.Background()
cli, err := client.NewClientWithOpts()
cli.NegotiateAPIVersion(ctx)
if err != nil {
return []byte{}, err
return err
}

// Ensure we have the image available:
Expand Down Expand Up @@ -240,25 +271,25 @@ func (Service *Service) DockerRun() ([]byte, error) {
c, _ := docker.DockerContainerList()
for _, cn := range c {
if strings.HasSuffix(cn.Names[0], Service.Config.Labels["pygmy.name"]) {
return []byte{}, nil
return nil
}
}

// We need the container name.
name, e := Service.GetFieldString("name")
if e != nil {
return []byte{}, fmt.Errorf("container config is missing label for name")
return fmt.Errorf("container config is missing label for name")
}

resp, err := docker.DockerContainerCreate(name, Service.Config, Service.HostConfig, Service.NetworkConfig)
_, err = docker.DockerContainerCreate(name, Service.Config, Service.HostConfig, Service.NetworkConfig)
if err != nil {
return []byte{}, err
return err
}

if err := docker.DockerContainerStart(name, types.ContainerStartOptions{}); err != nil {
return []byte{}, err
return err
}

return docker.DockerContainerLogs(resp.ID, Service.HostConfig.AutoRemove)
return nil

}
2 changes: 1 addition & 1 deletion service/interface/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type DockerService interface {
Setup() error
Status() (bool, error)
Start() ([]byte, error)
Start() error
Stop() error
}

Expand Down
39 changes: 33 additions & 6 deletions service/library/sshkeyadd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@ import (
)

// SshKeyAdd will add a given key to the ssh agent.
func SshKeyAdd(c Config, key string, index int) ([]byte, error) {
func SshKeyAdd(c Config, key string, index int) error {

Setup(&c)

if key != "" {
if _, err := os.Stat(key); err != nil {
fmt.Printf("%v\n", err)
return []byte{}, err
return err
}
}

var b []byte
var e error

for _, Container := range c.Services {
Expand Down Expand Up @@ -58,17 +57,45 @@ func SshKeyAdd(c Config, key string, index int) ([]byte, error) {
fmt.Println(e)
}

return newService.Start()
e = newService.Start()
if e != nil {
return e
}
l, e := newService.DockerLogs()
if e != nil {
return e
}

// We need tighter control on the output of this container...
for _, line := range strings.Split(string(l), "\n") {
if strings.Contains(line, "Identity added:") {
fmt.Println(line)
}
}

} else {

return Container.Start()
e := Container.Start()
if e != nil {
return e
}
l, e := Container.DockerLogs()
if e != nil {
return e
}

// We need tighter control on the output of this container...
for _, line := range strings.Split(string(l), "\n") {
if strings.Contains(line, "Identity added:") {
fmt.Println(line)
}
}

}

}
}

}
return b, e
return e
}
10 changes: 7 additions & 3 deletions service/library/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,13 @@ func Status(c Config) {
for _, v := range c.Services {
purpose, _ := v.GetFieldString("purpose")
if purpose == "showkeys" {
out, _ := v.Start()
if len(string(out)) > 0 {
output := strings.Trim(string(out), "\n")
e := v.Start()
if e != nil {
fmt.Println(e)
}
l, _ := v.DockerLogs()
if len(string(l)) > 0 {
output := strings.Trim(string(l), "\n")
fmt.Println(output)
}
}
Expand Down
11 changes: 4 additions & 7 deletions service/library/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func Up(c Config) {
service := c.Services[s]
enabled, _ := service.GetFieldBool("enable")
purpose, _ := service.GetFieldString("purpose")
output, _ := service.GetFieldBool("output")

// Do not show or add keys:
if enabled && purpose != "addkeys" && purpose != "showkeys" {
Expand All @@ -76,9 +75,9 @@ func Up(c Config) {
}
}

o, _ := service.Start()
if output && string(o) != "" {
fmt.Println(string(o))
e := service.Start()
if e != nil {
fmt.Println(e)
}
}

Expand Down Expand Up @@ -133,11 +132,9 @@ func Up(c Config) {
if agentPresent {
i := 1
for _, v := range c.Keys {
out, err := SshKeyAdd(c, v, i)
err := SshKeyAdd(c, v, i)
if err != nil {
fmt.Println(err)
} else if string(out) != "" {
fmt.Println(strings.Trim(string(out), "\n"))
}
i++
}
Expand Down
2 changes: 1 addition & 1 deletion service/library/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Update(c Config) {
fmt.Println(e)
}
if s, _ := service.Status(); !s {
_, e = service.Start()
e = service.Start()
if e != nil {
fmt.Println(e)
}
Expand Down
29 changes: 20 additions & 9 deletions service/ssh/agent/ssh_agent.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package agent

import (
"fmt"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -39,33 +41,42 @@ func New() model.Service {
// SshKeyLister will grab the output of all running containers with the proper
// config after starting them, and return it.
// which is indicated by the purpose tag.
func List(service model.Service) []string {
var r []byte
func List(service model.Service) ([]byte, error) {
purpose, _ := service.GetFieldString("purpose")
if purpose == "showkeys" {
r, _ = service.Start()
e := service.Start()
if e != nil {
return []byte{}, e
}
}
return strings.Split(string(r), "\n")
return service.DockerLogs()
}

// Search will determine if an SSH key has been added to the agent.
func Search(service model.Service, key string) bool {
result := false
if _, err := os.Stat(key); !os.IsNotExist(err) {
stripped := strings.Trim(key, ".pub")
data, err := ioutil.ReadFile(stripped + ".pub")
if err != nil {
fmt.Println(err)
return false
}

items := List(service)
items, _ := List(service)

if len(items) == 0 {
return false
}

for _, item := range items {
for _, item := range strings.Split(string(items), "\n") {
if strings.Contains(item, "The agent has no identities") {
return false
}
if strings.Contains(item, key) {
return true
if strings.Contains(item, string(data)) {
result = true
}
}
}
return false
return result
}

0 comments on commit 54474e8

Please sign in to comment.