github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/system/service.go (about) 1 package system 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "time" 8 9 "github.com/containers/libpod/cmd/podmanV2/registry" 10 "github.com/containers/libpod/pkg/domain/entities" 11 "github.com/containers/libpod/pkg/rootless" 12 "github.com/containers/libpod/pkg/systemd" 13 "github.com/containers/libpod/pkg/util" 14 "github.com/sirupsen/logrus" 15 "github.com/spf13/cobra" 16 ) 17 18 var ( 19 srvDescription = `Run an API service 20 21 Enable a listening service for API access to Podman commands. 22 ` 23 24 srvCmd = &cobra.Command{ 25 Use: "service [flags] [URI]", 26 Args: cobra.MaximumNArgs(1), 27 Short: "Run API service", 28 Long: srvDescription, 29 RunE: service, 30 Example: `podman system service --time=0 unix:///tmp/podman.sock 31 podman system service --varlink --time=0 unix:///tmp/podman.sock`, 32 } 33 34 srvArgs = struct { 35 Timeout int64 36 Varlink bool 37 }{} 38 ) 39 40 func init() { 41 registry.Commands = append(registry.Commands, registry.CliCommand{ 42 Mode: []entities.EngineMode{entities.ABIMode}, 43 Command: srvCmd, 44 Parent: systemCmd, 45 }) 46 47 flags := srvCmd.Flags() 48 flags.Int64VarP(&srvArgs.Timeout, "time", "t", 5, "Time until the service session expires in seconds. Use 0 to disable the timeout") 49 flags.Int64Var(&srvArgs.Timeout, "timeout", 5, "Time until the service session expires in seconds. Use 0 to disable the timeout") 50 flags.BoolVar(&srvArgs.Varlink, "varlink", false, "Use legacy varlink service instead of REST") 51 52 _ = flags.MarkDeprecated("varlink", "valink API is deprecated.") 53 } 54 55 func service(cmd *cobra.Command, args []string) error { 56 apiURI, err := resolveApiURI(args) 57 if err != nil { 58 return err 59 } 60 logrus.Infof("using API endpoint: \"%s\"", apiURI) 61 62 opts := entities.ServiceOptions{ 63 URI: apiURI, 64 Timeout: time.Duration(srvArgs.Timeout) * time.Second, 65 Command: cmd, 66 } 67 68 if srvArgs.Varlink { 69 return registry.ContainerEngine().VarlinkService(registry.GetContext(), opts) 70 } 71 72 logrus.Warn("This function is EXPERIMENTAL") 73 fmt.Fprintf(os.Stderr, "This function is EXPERIMENTAL.\n") 74 return registry.ContainerEngine().RestService(registry.GetContext(), opts) 75 } 76 77 func resolveApiURI(_url []string) (string, error) { 78 79 // When determining _*THE*_ listening endpoint -- 80 // 1) User input wins always 81 // 2) systemd socket activation 82 // 3) rootless honors XDG_RUNTIME_DIR 83 // 4) if varlink -- adapter.DefaultVarlinkAddress 84 // 5) lastly adapter.DefaultAPIAddress 85 86 if _url == nil { 87 if v, found := os.LookupEnv("PODMAN_SOCKET"); found { 88 _url = []string{v} 89 } 90 } 91 92 switch { 93 case len(_url) > 0: 94 return _url[0], nil 95 case systemd.SocketActivated(): 96 logrus.Info("using systemd socket activation to determine API endpoint") 97 return "", nil 98 case rootless.IsRootless(): 99 xdg, err := util.GetRuntimeDir() 100 if err != nil { 101 return "", err 102 } 103 104 socketName := "podman.sock" 105 if srvArgs.Varlink { 106 socketName = "io.podman" 107 } 108 socketDir := filepath.Join(xdg, "podman", socketName) 109 if _, err := os.Stat(filepath.Dir(socketDir)); err != nil { 110 if os.IsNotExist(err) { 111 if err := os.Mkdir(filepath.Dir(socketDir), 0755); err != nil { 112 return "", err 113 } 114 } else { 115 return "", err 116 } 117 } 118 return "unix:" + socketDir, nil 119 case srvArgs.Varlink: 120 return registry.DefaultVarlinkAddress, nil 121 default: 122 return registry.DefaultAPIAddress, nil 123 } 124 }