mirror of
https://github.com/containers/podman.git
synced 2025-09-18 07:51:22 +08:00
Add podman farm create command
The podman farm create command allows users to create farms from the avaiable podman system connections. Signed-off-by: Urvashi Mohnani <umohnani@redhat.com>
This commit is contained in:
80
cmd/podman/farm/create.go
Normal file
80
cmd/podman/farm/create.go
Normal file
@ -0,0 +1,80 @@
|
||||
package farm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/containers/common/pkg/completion"
|
||||
"github.com/containers/common/pkg/config"
|
||||
"github.com/containers/podman/v4/cmd/podman/registry"
|
||||
"github.com/containers/podman/v4/pkg/util"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
farmCreateDescription = `Create a new farm with connections added via podman system connection add.
|
||||
|
||||
The "podman system connection add --farm" command can be used to add a new connection to a new or existing farm.`
|
||||
|
||||
createCommand = &cobra.Command{
|
||||
Use: "create [options] NAME [CONNECTIONS...]",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Short: "Create a new farm",
|
||||
Long: farmCreateDescription,
|
||||
RunE: create,
|
||||
ValidArgsFunction: completion.AutocompleteNone,
|
||||
Example: `podman farm create myfarm connection1
|
||||
podman farm create myfarm`,
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.Commands = append(registry.Commands, registry.CliCommand{
|
||||
Command: createCommand,
|
||||
Parent: farmCmd,
|
||||
})
|
||||
}
|
||||
|
||||
func create(cmd *cobra.Command, args []string) error {
|
||||
farmName := args[0]
|
||||
connections := args[1:]
|
||||
|
||||
cfg, err := config.ReadCustomConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, ok := cfg.Farms.List[farmName]; ok {
|
||||
// if farm exists return an error
|
||||
return fmt.Errorf("farm with name %q already exists", farmName)
|
||||
}
|
||||
|
||||
// Can create an empty farm without any connections
|
||||
if len(connections) == 0 {
|
||||
cfg.Farms.List[farmName] = []string{}
|
||||
}
|
||||
|
||||
for _, c := range connections {
|
||||
if _, ok := cfg.Engine.ServiceDestinations[c]; ok {
|
||||
if util.StringInSlice(c, cfg.Farms.List[farmName]) {
|
||||
// Don't add duplicate connections to a farm
|
||||
continue
|
||||
}
|
||||
cfg.Farms.List[farmName] = append(cfg.Farms.List[farmName], c)
|
||||
} else {
|
||||
return fmt.Errorf("cannot create farm, %q is not a system connection", c)
|
||||
}
|
||||
}
|
||||
|
||||
// If this is the first farm being created, set it as the default farm
|
||||
if len(cfg.Farms.List) == 1 {
|
||||
cfg.Farms.Default = farmName
|
||||
}
|
||||
|
||||
err = cfg.Write()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Farm %q created\n", farmName)
|
||||
return nil
|
||||
}
|
29
docs/source/markdown/podman-farm-create.1.md
Normal file
29
docs/source/markdown/podman-farm-create.1.md
Normal file
@ -0,0 +1,29 @@
|
||||
% podman-farm-create 1
|
||||
|
||||
## NAME
|
||||
podman\-farm\-create - Create a new farm
|
||||
|
||||
## SYNOPSIS
|
||||
**podman farm create** [*options*] *name* [*connections*]
|
||||
|
||||
## DESCRIPTION
|
||||
Create a new farm with connections that podman knows about which were added via the
|
||||
*podman system connection add* command.
|
||||
|
||||
An empty farm can be created without adding any connections to it. Add or remove
|
||||
connections from a farm via the *podman farm update* command.
|
||||
|
||||
## OPTIONS
|
||||
|
||||
## EXAMPLE
|
||||
|
||||
```
|
||||
$ podman farm create farm1 f37 f38
|
||||
|
||||
$ podman farm farm2
|
||||
```
|
||||
## SEE ALSO
|
||||
**[podman(1)](podman.1.md)**, **[podman-farm(1)](podman-farm.1.md)**
|
||||
|
||||
## HISTORY
|
||||
July 2023, Originally compiled by Urvashi Mohnani (umohnani at redhat dot com)
|
Reference in New Issue
Block a user