Skip to content

Commit

Permalink
inspect through systemd engine
Browse files Browse the repository at this point in the history
  • Loading branch information
zc authored and CMGS committed Mar 6, 2020
1 parent f78ba77 commit 9dee8cb
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
59 changes: 59 additions & 0 deletions engine/systemd/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package systemd

import (
"bufio"
"bytes"
"encoding/csv"
"encoding/json"
"strings"

"github.com/pkg/errors"
)

type serviceStatus struct {
SubState string
ActiveState string
Environment string
Description string
User string
}

func newServiceStatus(buf *bytes.Buffer) *serviceStatus {
status := map[string]string{}
scanner := bufio.NewScanner(buf)
for scanner.Scan() {
text := scanner.Text()
parts := strings.SplitN(text, "=", 2)
status[parts[0]] = parts[1]
}
return &serviceStatus{
SubState: status["SubState"],
ActiveState: status["ActiveState"],
Environment: status["Environment"],
Description: status["Description"],
User: status["User"],
}
}

func (s *serviceStatus) running() bool {
return s.SubState == "running" && s.ActiveState == "active"
}

func (s *serviceStatus) env() ([]string, error) {
reader := csv.NewReader(strings.NewReader(s.Environment))
reader.Comma = ' '
records, err := reader.ReadAll()
if err != nil {
return nil, errors.Wrap(err, s.Environment)
}
return records[0], nil
}

func (s *serviceStatus) labels() (map[string]string, error) {
desc := &unitDesciption{}
description := strings.ReplaceAll(s.Description, "\\x5c", "\\")
if err := json.Unmarshal([]byte(description), desc); err != nil {
return nil, errors.Wrap(err, s.Description)
}
return desc.Labels, nil
}
16 changes: 14 additions & 2 deletions engine/systemd/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@ const (
)

type SystemdSSH struct {
hostIP string
client *ssh.Client
}

func NewSystemdSSH(endpoint string, config *ssh.ClientConfig) (*SystemdSSH, error) {
parts := strings.Split(endpoint, ":")
client, err := ssh.Dial("tcp", endpoint, config)
return &SystemdSSH{
hostIP: parts[0],
client: client,
}, err
}

func MakeClient(ctx context.Context, config coretypes.Config, nodename, endpoint, ca, cert, key string) (api engine.API, err error) {
signer, err := ssh.ParsePrivateKey([]byte(key))
if err != nil {
Expand All @@ -38,8 +48,10 @@ func MakeClient(ctx context.Context, config coretypes.Config, nodename, endpoint
},
HostKeyCallback: func(_ string, _ net.Addr, _ ssh.PublicKey) error { return nil },
}
sshClient, err := ssh.Dial("tcp", strings.TrimPrefix(endpoint, SSHPrefixKey), sshConfig)
return &SystemdSSH{sshClient}, err
return NewSystemdSSH(
strings.TrimPrefix(endpoint, SSHPrefixKey),
sshConfig,
)
}

func (s *SystemdSSH) WithSession(f func(*ssh.Session) error) (err error) {
Expand Down
6 changes: 6 additions & 0 deletions engine/systemd/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ func (b *unitBuilder) buildExec() *unitBuilder {
return b
}

user := b.opts.User
if user == "" {
user = "root"
}

env := []string{}
for _, e := range b.opts.Env {
env = append(env, fmt.Sprintf(`"%s"`, e))
Expand All @@ -153,6 +158,7 @@ func (b *unitBuilder) buildExec() *unitBuilder {

b.serviceBuffer = append(b.serviceBuffer, []string{
fmt.Sprintf("ExecStart=/usr/bin/cgexec -g memory,cpuset:%s %s", b.cgroupPath(), strings.Join(b.opts.Cmd, " ")),
fmt.Sprintf("User=%s", user),
fmt.Sprintf("Environment=%s", strings.Join(env, " ")),
fmt.Sprintf("StandardOutput=%s", stdioType),
fmt.Sprintf("StandardError=%s", stdioType),
Expand Down
25 changes: 20 additions & 5 deletions engine/systemd/virtualization.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
cmdSystemdReload = `/bin/systemctl daemon-reload`
cmdSystemdRestart = `/bin/systemctl restart %s`
cmdSystemdStop = `/bin/systemctl stop %s`
cmdSystemdStatus = `/bin/systemctl status %s --property SubState,ActiveState`
cmdSystemdStatus = `/bin/systemctl show %s --property SubState,ActiveState,Environment,Description --no-pager`
)

func (s *SystemdSSH) VirtualizationCreate(ctx context.Context, opts *enginetypes.VirtualizationCreateOptions) (created *enginetypes.VirtualizationCreated, err error) {
Expand Down Expand Up @@ -85,15 +85,30 @@ func (s *SystemdSSH) VirtualizationRemove(ctx context.Context, ID string, volume
}

func (s *SystemdSSH) VirtualizationInspect(ctx context.Context, ID string) (info *enginetypes.VirtualizationInfo, err error) {
_, stderr, err := s.runSingleCommand(ctx, fmt.Sprintf(cmdSystemdStatus, ID), nil)
stdout, stderr, err := s.runSingleCommand(ctx, fmt.Sprintf(cmdSystemdStatus, ID), nil)
if err != nil {
return nil, errors.Wrap(err, stderr.String())
}

serviceStatus := newServiceStatus(stdout)

env, err := serviceStatus.env()
if err != nil {
return
}

labels, err := serviceStatus.labels()
if err != nil {
return
}

return &enginetypes.VirtualizationInfo{
ID: ID,
User: "root",
Running: false,
ID: ID,
User: "root",
Running: serviceStatus.running(),
Env: env,
Labels: labels,
Networks: map[string]string{"host": s.hostIP},
}, nil
}

Expand Down

0 comments on commit 9dee8cb

Please sign in to comment.