summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorJhon Honce <jhonce@redhat.com>2022-04-04 13:04:40 -0700
committeropenshift-cherrypick-robot <>2022-05-04 13:42:38 +0000
commita21e112364f93a1a7d342584a239b289a8ee9f0a (patch)
treeb6c3632932fc703a82a55fc50727f24f0fc323d2 /cmd
parent3210a3f4257d19744abd4ae2302018b16edb2507 (diff)
downloadpodman-a21e112364f93a1a7d342584a239b289a8ee9f0a.tar.gz
podman-a21e112364f93a1a7d342584a239b289a8ee9f0a.tar.bz2
podman-a21e112364f93a1a7d342584a239b289a8ee9f0a.zip
Add podman machine events
Signed-off-by: Jhon Honce <jhonce@redhat.com>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/podman/machine/init.go4
-rw-r--r--cmd/podman/machine/inspect.go1
-rw-r--r--cmd/podman/machine/list.go1
-rw-r--r--cmd/podman/machine/machine.go132
-rw-r--r--cmd/podman/machine/rm.go10
-rw-r--r--cmd/podman/machine/start.go4
-rw-r--r--cmd/podman/machine/stop.go2
-rw-r--r--cmd/podman/main.go9
-rw-r--r--cmd/podman/system/connection.go15
-rw-r--r--cmd/podman/validate/noop.go9
10 files changed, 162 insertions, 25 deletions
diff --git a/cmd/podman/machine/init.go b/cmd/podman/machine/init.go
index 4991c6aa3..733266b85 100644
--- a/cmd/podman/machine/init.go
+++ b/cmd/podman/machine/init.go
@@ -9,6 +9,7 @@ import (
"github.com/containers/common/pkg/completion"
"github.com/containers/podman/v4/cmd/podman/registry"
+ "github.com/containers/podman/v4/libpod/events"
"github.com/containers/podman/v4/pkg/machine"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@@ -145,11 +146,14 @@ func initMachine(cmd *cobra.Command, args []string) error {
// Finished = *, err != nil - Exit with an error message
return err
}
+ newMachineEvent(events.Init, events.Event{Name: initOpts.Name})
fmt.Println("Machine init complete")
+
if now {
err = vm.Start(initOpts.Name, machine.StartOptions{})
if err == nil {
fmt.Printf("Machine %q started successfully\n", initOpts.Name)
+ newMachineEvent(events.Start, events.Event{Name: initOpts.Name})
}
} else {
extra := ""
diff --git a/cmd/podman/machine/inspect.go b/cmd/podman/machine/inspect.go
index 21e5074b7..0ddcec8b5 100644
--- a/cmd/podman/machine/inspect.go
+++ b/cmd/podman/machine/inspect.go
@@ -4,7 +4,6 @@
package machine
import (
- "encoding/json"
"os"
"github.com/containers/podman/v4/cmd/podman/common"
diff --git a/cmd/podman/machine/list.go b/cmd/podman/machine/list.go
index c987bf71a..861feee07 100644
--- a/cmd/podman/machine/list.go
+++ b/cmd/podman/machine/list.go
@@ -4,7 +4,6 @@
package machine
import (
- "encoding/json"
"os"
"sort"
"strconv"
diff --git a/cmd/podman/machine/machine.go b/cmd/podman/machine/machine.go
index d3775f022..4c566b11f 100644
--- a/cmd/podman/machine/machine.go
+++ b/cmd/podman/machine/machine.go
@@ -4,25 +4,39 @@
package machine
import (
+ "errors"
+ "net"
+ "os"
+ "path/filepath"
+ "regexp"
"strings"
+ "sync"
+ "time"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/cmd/podman/validate"
+ "github.com/containers/podman/v4/libpod/events"
"github.com/containers/podman/v4/pkg/machine"
+ "github.com/containers/podman/v4/pkg/util"
+ "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
- noOp = func(cmd *cobra.Command, args []string) error {
- return nil
- }
+ // Pull in configured json library
+ json = registry.JSONLibrary()
+
+ sockPaths []string // Paths to unix domain sockets for publishing
+ openEventSock sync.Once // Singleton support for opening sockets as needed
+ sockets []net.Conn // Opened sockets, if any
+
// Command: podman _machine_
machineCmd = &cobra.Command{
Use: "machine",
Short: "Manage a virtual machine",
Long: "Manage a virtual machine. Virtual machines are used to run Podman.",
- PersistentPreRunE: noOp,
- PersistentPostRunE: noOp,
+ PersistentPreRunE: initMachineEvents,
+ PersistentPostRunE: closeMachineEvents,
RunE: validate.SubCommandExists,
}
)
@@ -64,3 +78,111 @@ func getMachines(toComplete string) ([]string, cobra.ShellCompDirective) {
}
return suggestions, cobra.ShellCompDirectiveNoFileComp
}
+
+func initMachineEvents(cmd *cobra.Command, _ []string) error {
+ logrus.Debugf("Called machine %s.PersistentPreRunE(%s)", cmd.Name(), strings.Join(os.Args, " "))
+
+ sockPaths, err := resolveEventSock()
+ if err != nil {
+ return err
+ }
+
+ // No sockets found, so no need to publish events...
+ if len(sockPaths) == 0 {
+ return nil
+ }
+
+ for _, path := range sockPaths {
+ conn, err := (&net.Dialer{}).DialContext(registry.Context(), "unix", path)
+ if err != nil {
+ logrus.Warnf("Failed to open event socket %q: %v", path, err)
+ continue
+ }
+ logrus.Debugf("Machine event socket %q found", path)
+ sockets = append(sockets, conn)
+ }
+ return nil
+}
+
+func resolveEventSock() ([]string, error) {
+ // Used mostly for testing
+ if sock, found := os.LookupEnv("PODMAN_MACHINE_EVENTS_SOCK"); found {
+ return []string{sock}, nil
+ }
+
+ xdg, err := util.GetRuntimeDir()
+ if err != nil {
+ logrus.Warnf("Failed to get runtime dir, machine events will not be published: %s", err)
+ return nil, nil
+ }
+
+ re := regexp.MustCompile(`machine_events.*\.sock`)
+ sockPaths := make([]string, 0)
+ fn := func(path string, info os.DirEntry, err error) error {
+ switch {
+ case err != nil:
+ return err
+ case info.IsDir():
+ return nil
+ case info.Type() != os.ModeSocket:
+ return nil
+ case !re.MatchString(info.Name()):
+ return nil
+ }
+
+ logrus.Debugf("Machine events will be published on: %q", path)
+ sockPaths = append(sockPaths, path)
+ return nil
+ }
+
+ if err := filepath.WalkDir(filepath.Join(xdg, "podman"), fn); err != nil {
+ if errors.Is(err, os.ErrNotExist) {
+ return nil, nil
+ }
+ return nil, err
+ }
+ return sockPaths, nil
+}
+
+func newMachineEvent(status events.Status, event events.Event) {
+ openEventSock.Do(func() {
+ // No sockets where found, so no need to publish events...
+ if len(sockPaths) == 0 {
+ return
+ }
+
+ for _, path := range sockPaths {
+ conn, err := (&net.Dialer{}).DialContext(registry.Context(), "unix", path)
+ if err != nil {
+ logrus.Warnf("Failed to open event socket %q: %v", path, err)
+ continue
+ }
+ logrus.Debugf("Machine event socket %q found", path)
+ sockets = append(sockets, conn)
+ }
+ })
+
+ event.Status = status
+ event.Time = time.Now()
+ event.Type = events.Machine
+
+ payload, err := json.Marshal(event)
+ if err != nil {
+ logrus.Errorf("Unable to format machine event: %q", err)
+ return
+ }
+
+ for _, sock := range sockets {
+ if _, err := sock.Write(payload); err != nil {
+ logrus.Errorf("Unable to write machine event: %q", err)
+ }
+ }
+}
+
+func closeMachineEvents(cmd *cobra.Command, _ []string) error {
+ logrus.Debugf("Called machine %s.PersistentPostRunE(%s)", cmd.Name(), strings.Join(os.Args, " "))
+ for _, sock := range sockets {
+ _ = sock.Close()
+ }
+ return nil
+}
diff --git a/cmd/podman/machine/rm.go b/cmd/podman/machine/rm.go
index 617a70a76..e678eb629 100644
--- a/cmd/podman/machine/rm.go
+++ b/cmd/podman/machine/rm.go
@@ -10,6 +10,7 @@ import (
"strings"
"github.com/containers/podman/v4/cmd/podman/registry"
+ "github.com/containers/podman/v4/libpod/events"
"github.com/containers/podman/v4/pkg/machine"
"github.com/spf13/cobra"
)
@@ -50,7 +51,7 @@ func init() {
flags.BoolVar(&destroyOptions.SaveImage, imageFlagName, false, "Do not delete the image file")
}
-func rm(cmd *cobra.Command, args []string) error {
+func rm(_ *cobra.Command, args []string) error {
var (
err error
vm machine.VM
@@ -83,5 +84,10 @@ func rm(cmd *cobra.Command, args []string) error {
return nil
}
}
- return remove()
+ err = remove()
+ if err != nil {
+ return err
+ }
+ newMachineEvent(events.Remove, events.Event{Name: vmName})
+ return nil
}
diff --git a/cmd/podman/machine/start.go b/cmd/podman/machine/start.go
index 56acb09cb..2b0c8213d 100644
--- a/cmd/podman/machine/start.go
+++ b/cmd/podman/machine/start.go
@@ -7,6 +7,7 @@ import (
"fmt"
"github.com/containers/podman/v4/cmd/podman/registry"
+ "github.com/containers/podman/v4/libpod/events"
"github.com/containers/podman/v4/pkg/machine"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@@ -31,7 +32,7 @@ func init() {
})
}
-func start(cmd *cobra.Command, args []string) error {
+func start(_ *cobra.Command, args []string) error {
var (
err error
vm machine.VM
@@ -62,5 +63,6 @@ func start(cmd *cobra.Command, args []string) error {
return err
}
fmt.Printf("Machine %q started successfully\n", vmName)
+ newMachineEvent(events.Start, events.Event{Name: vmName})
return nil
}
diff --git a/cmd/podman/machine/stop.go b/cmd/podman/machine/stop.go
index e6bf3cf2b..dcb124dd4 100644
--- a/cmd/podman/machine/stop.go
+++ b/cmd/podman/machine/stop.go
@@ -7,6 +7,7 @@ import (
"fmt"
"github.com/containers/podman/v4/cmd/podman/registry"
+ "github.com/containers/podman/v4/libpod/events"
"github.com/containers/podman/v4/pkg/machine"
"github.com/spf13/cobra"
)
@@ -49,5 +50,6 @@ func stop(cmd *cobra.Command, args []string) error {
return err
}
fmt.Printf("Machine %q stopped successfully\n", vmName)
+ newMachineEvent(events.Stop, events.Event{Name: vmName})
return nil
}
diff --git a/cmd/podman/main.go b/cmd/podman/main.go
index 8f580601e..929c8a757 100644
--- a/cmd/podman/main.go
+++ b/cmd/podman/main.go
@@ -18,6 +18,7 @@ import (
_ "github.com/containers/podman/v4/cmd/podman/secrets"
_ "github.com/containers/podman/v4/cmd/podman/system"
_ "github.com/containers/podman/v4/cmd/podman/system/connection"
+ "github.com/containers/podman/v4/cmd/podman/validate"
_ "github.com/containers/podman/v4/cmd/podman/volumes"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/rootless"
@@ -64,8 +65,8 @@ func parseCommands() *cobra.Command {
c.Command.Hidden = true
// overwrite persistent pre/post function to skip setup
- c.Command.PersistentPostRunE = noop
- c.Command.PersistentPreRunE = noop
+ c.Command.PersistentPostRunE = validate.NoOp
+ c.Command.PersistentPreRunE = validate.NoOp
addCommand(c)
continue
}
@@ -120,7 +121,3 @@ func addCommand(c registry.CliCommand) {
c.Command.SetUsageTemplate(usageTemplate)
c.Command.DisableFlagsInUseLine = true
}
-
-func noop(cmd *cobra.Command, args []string) error {
- return nil
-}
diff --git a/cmd/podman/system/connection.go b/cmd/podman/system/connection.go
index 5164de78c..5dbe50fc9 100644
--- a/cmd/podman/system/connection.go
+++ b/cmd/podman/system/connection.go
@@ -7,18 +7,15 @@ import (
)
var (
- // Skip creating engines since this command will obtain connection information to said engines
- noOp = func(cmd *cobra.Command, args []string) error {
- return nil
- }
-
+ // ConnectionCmd skips creating engines (PersistentPreRunE/PersistentPostRunE are No-Op's) since
+ // sub-commands will obtain connection information to said engines
ConnectionCmd = &cobra.Command{
Use: "connection",
- Short: "Manage remote ssh destinations",
- Long: `Manage ssh destination information in podman configuration`,
- PersistentPreRunE: noOp,
+ Short: "Manage remote API service destinations",
+ Long: `Manage remote API service destination information in podman configuration`,
+ PersistentPreRunE: validate.NoOp,
RunE: validate.SubCommandExists,
- PersistentPostRunE: noOp,
+ PersistentPostRunE: validate.NoOp,
TraverseChildren: false,
}
)
diff --git a/cmd/podman/validate/noop.go b/cmd/podman/validate/noop.go
new file mode 100644
index 000000000..2243ef5c4
--- /dev/null
+++ b/cmd/podman/validate/noop.go
@@ -0,0 +1,9 @@
+package validate
+
+import (
+ "github.com/spf13/cobra"
+)
+
+func NoOp(_ *cobra.Command, _ []string) error {
+ return nil
+}