Add LXD support
This commit is contained in:
48
scan/base.go
48
scan/base.go
@@ -90,6 +90,12 @@ func (l base) allContainers() (containers []config.Container, err error) {
|
||||
return containers, err
|
||||
}
|
||||
return l.parseDockerPs(stdout)
|
||||
case "lxd":
|
||||
stdout, err := l.lxdPs("-c n")
|
||||
if err != nil {
|
||||
return containers, err
|
||||
}
|
||||
return l.parseLxdPs(stdout)
|
||||
default:
|
||||
return containers, fmt.Errorf(
|
||||
"Not supported yet: %s", l.ServerInfo.Container.Type)
|
||||
@@ -104,6 +110,12 @@ func (l *base) runningContainers() (containers []config.Container, err error) {
|
||||
return containers, err
|
||||
}
|
||||
return l.parseDockerPs(stdout)
|
||||
case "lxd":
|
||||
stdout, err := l.lxdPs("volatile.last_state.power=RUNNING -c n")
|
||||
if err != nil {
|
||||
return containers, err
|
||||
}
|
||||
return l.parseLxdPs(stdout)
|
||||
default:
|
||||
return containers, fmt.Errorf(
|
||||
"Not supported yet: %s", l.ServerInfo.Container.Type)
|
||||
@@ -118,6 +130,12 @@ func (l *base) exitedContainers() (containers []config.Container, err error) {
|
||||
return containers, err
|
||||
}
|
||||
return l.parseDockerPs(stdout)
|
||||
case "lxd":
|
||||
stdout, err := l.lxdPs("volatile.last_state.power=STOPPED -c n")
|
||||
if err != nil {
|
||||
return containers, err
|
||||
}
|
||||
return l.parseLxdPs(stdout)
|
||||
default:
|
||||
return containers, fmt.Errorf(
|
||||
"Not supported yet: %s", l.ServerInfo.Container.Type)
|
||||
@@ -133,6 +151,15 @@ func (l *base) dockerPs(option string) (string, error) {
|
||||
return r.Stdout, nil
|
||||
}
|
||||
|
||||
func (l *base) lxdPs(option string) (string, error) {
|
||||
cmd := fmt.Sprintf("lxc list %s", option)
|
||||
r := l.ssh(cmd, noSudo)
|
||||
if !r.isSuccess() {
|
||||
return "", fmt.Errorf("failed to SSH: %s", r)
|
||||
}
|
||||
return r.Stdout, nil
|
||||
}
|
||||
|
||||
func (l *base) parseDockerPs(stdout string) (containers []config.Container, err error) {
|
||||
lines := strings.Split(stdout, "\n")
|
||||
for _, line := range lines {
|
||||
@@ -151,6 +178,27 @@ func (l *base) parseDockerPs(stdout string) (containers []config.Container, err
|
||||
return
|
||||
}
|
||||
|
||||
func (l *base) parseLxdPs(stdout string) (containers []config.Container, err error) {
|
||||
lines := strings.Split(stdout, "\n")
|
||||
for i, line := range lines[3:] {
|
||||
if i % 2 == 1 {
|
||||
continue
|
||||
}
|
||||
fields := strings.Fields(strings.Replace(line, "|", " ", -1))
|
||||
if len(fields) == 0 {
|
||||
break
|
||||
}
|
||||
if len(fields) != 1 {
|
||||
return containers, fmt.Errorf("Unknown format: %s", line)
|
||||
}
|
||||
containers = append(containers, config.Container{
|
||||
ContainerID: fields[0],
|
||||
Name: fields[0],
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (l *base) detectPlatform() error {
|
||||
ok, instanceID, err := l.detectRunningOnAws()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user