From 997de2f8e9e5453a99108bde012aa6c41d7323ec Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Tue, 12 Jan 2021 14:29:27 -0500 Subject: Initial implementation of renaming containers Basic theory: We remove the container, but *only from the DB*. We leave it in c/storage, we leave the lock allocated, we leave it running (if it is). Then we create an identical container with an altered name, and add that back to the database. Theoretically we now have a renamed container. The advantage of this approach is that it doesn't just apply to rename - we can use this to make *any* configuration change to a container that does not alter its container ID. Potential problems are numerous. This process is *THOROUGHLY* non-atomic at present - if you `kill -9` Podman mid-rename things will be in a bad place, for example. Also, we can't rename containers that can't be removed normally - IE, containers with dependencies (pod infra containers, for example). The largest potential improvement will be to move the majority of the work into the DB, with a `RecreateContainer()` method - that will add atomicity, and let us remove the container without worrying about depencies and similar issues. Potential problems: long-running processes that edit the DB and may have an older version of the configuration around. Most notable example is `podman run --rm` - the removal command needed to be manually edited to avoid this one. This begins to get at the heart of me not wanting to do this in the first place... This provides CLI and API implementations for frontend, but no tunnel implementation. It will be added in a future release (just held back for time now - we need this in 3.0 and are running low on time). This is honestly kind of horrifying, but I think it will work. Signed-off-by: Matthew Heon --- cmd/podman/containers/rename.go | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 cmd/podman/containers/rename.go (limited to 'cmd') diff --git a/cmd/podman/containers/rename.go b/cmd/podman/containers/rename.go new file mode 100644 index 000000000..9c94e6272 --- /dev/null +++ b/cmd/podman/containers/rename.go @@ -0,0 +1,56 @@ +package containers + +import ( + "github.com/containers/podman/v2/cmd/podman/common" + "github.com/containers/podman/v2/cmd/podman/registry" + "github.com/containers/podman/v2/pkg/domain/entities" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var ( + renameDescription = "The podman rename command allows you to rename an existing container" + renameCommand = &cobra.Command{ + Use: "rename CONTAINER NAME", + Short: "Rename an existing container", + Long: renameDescription, + RunE: rename, + Args: cobra.ExactArgs(2), + ValidArgsFunction: common.AutocompletePortCommand, + Example: "podman rename containerA newName", + } + + containerRenameCommand = &cobra.Command{ + Use: renameCommand.Use, + Short: renameCommand.Short, + Long: renameCommand.Long, + RunE: renameCommand.RunE, + Args: renameCommand.Args, + ValidArgsFunction: renameCommand.ValidArgsFunction, + Example: "podman container rename containerA newName", + } +) + +func init() { + // TODO: Once bindings are done, add this to TunnelMode + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode}, + Command: renameCommand, + }) + + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode}, + Command: containerRenameCommand, + Parent: containerCmd, + }) +} + +func rename(cmd *cobra.Command, args []string) error { + if len(args) > 2 { + return errors.Errorf("must provide at least two arguments to rename") + } + renameOpts := entities.ContainerRenameOptions{ + NewName: args[1], + } + return registry.ContainerEngine().ContainerRename(registry.GetContext(), args[0], renameOpts) +} -- cgit v1.2.3-54-g00ecf