New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
auto update containers in systemd units #5480
Merged
openshift-merge-robot
merged 2 commits into
containers:master
from
vrothberg:auto-updates
Mar 18, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/containers/libpod/cmd/podman/cliconfig" | ||
"github.com/containers/libpod/pkg/adapter" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
autoUpdateCommand cliconfig.AutoUpdateValues | ||
autoUpdateDescription = `Auto update containers according to their auto-update policy. | ||
|
||
Auto-update policies are specified with the "io.containers.autoupdate" label.` | ||
_autoUpdateCommand = &cobra.Command{ | ||
Use: "auto-update [flags]", | ||
Short: "Auto update containers according to their auto-update policy", | ||
Args: noSubArgs, | ||
Long: autoUpdateDescription, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
restartCommand.InputArgs = args | ||
restartCommand.GlobalFlags = MainGlobalOpts | ||
return autoUpdateCmd(&restartCommand) | ||
}, | ||
Example: `podman auto-update`, | ||
} | ||
) | ||
|
||
func init() { | ||
autoUpdateCommand.Command = _autoUpdateCommand | ||
autoUpdateCommand.SetHelpTemplate(HelpTemplate()) | ||
autoUpdateCommand.SetUsageTemplate(UsageTemplate()) | ||
} | ||
|
||
func autoUpdateCmd(c *cliconfig.RestartValues) error { | ||
runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand) | ||
if err != nil { | ||
return errors.Wrapf(err, "error creating libpod runtime") | ||
} | ||
defer runtime.DeferredShutdown(false) | ||
|
||
units, failures := runtime.AutoUpdate() | ||
for _, unit := range units { | ||
fmt.Println(unit) | ||
} | ||
var finalErr error | ||
if len(failures) > 0 { | ||
finalErr = failures[0] | ||
for _, e := range failures[1:] { | ||
finalErr = errors.Errorf("%v\n%v", finalErr, e) | ||
} | ||
} | ||
return finalErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3334,6 +3334,7 @@ _podman_podman() { | |
" | ||
commands=" | ||
attach | ||
auto-update | ||
build | ||
commit | ||
container | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[Unit] | ||
Description=Podman auto-update service | ||
Documentation=man:podman-auto-update(1) | ||
Wants=network.target | ||
After=network-online.target | ||
|
||
[Service] | ||
ExecStart=/usr/bin/podman auto-update | ||
|
||
[Install] | ||
WantedBy=multi-user.target default.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[Unit] | ||
Description=Podman auto-update timer | ||
|
||
[Timer] | ||
OnCalendar=daily | ||
Persistent=true | ||
|
||
[Install] | ||
WantedBy=timers.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
% podman-auto-update(1) | ||
|
||
## NAME | ||
podman-auto-update - Auto update containers according to their auto-update policy | ||
|
||
## SYNOPSIS | ||
**podman auto-update** | ||
|
||
## DESCRIPTION | ||
`podman auto-update` looks up containers with a specified "io.containers.autoupdate" label (i.e., the auto-update policy). | ||
|
||
If the label is present and set to "image", Podman reaches out to the corresponding registry to check if the image has been updated. | ||
An image is considered updated if the digest in the local storage is different than the one of the remote image. | ||
If an image must be updated, Podman pulls it down and restarts the systemd unit executing the container. | ||
|
||
At container-creation time, Podman looks up the "PODMAN_SYSTEMD_UNIT" environment variables and stores it verbatim in the container's label. | ||
This variable is now set by all systemd units generated by `podman-generate-systemd` and is set to `%n` (i.e., the name of systemd unit starting the container). | ||
This data is then being used in the auto-update sequence to instruct systemd (via DBUS) to restart the unit and hence to restart the container. | ||
|
||
Note that `podman auto-update` relies on systemd and requires a fully-qualified image reference (e.g., quay.io/podman/stable:latest) to be used to create the container. | ||
This enforcement is necessary to know which image to actually check and pull. | ||
If an image ID was used, Podman would not know which image to check/pull anymore. | ||
|
||
## EXAMPLES | ||
|
||
``` | ||
# Start a container | ||
$ podman run -d busybox:latest top | ||
bc219740a210455fa27deacc96d50a9e20516492f1417507c13ce1533dbdcd9d | ||
|
||
# Generate a systemd unit for this container | ||
$ podman generate systemd --new --files bc219740a210455fa27deacc96d50a9e20516492f1417507c13ce1533dbdcd9d | ||
/home/user/containers/libpod/container-bc219740a210455fa27deacc96d50a9e20516492f1417507c13ce1533dbdcd9d.service | ||
|
||
# Load the new systemd unit and start it | ||
$ mv ./container-bc219740a210455fa27deacc96d50a9e20516492f1417507c13ce1533dbdcd9d.service ~/.config/systemd/user | ||
$ systemctl --user daemon-reload | ||
$ systemctl --user start container-bc219740a210455fa27deacc96d50a9e20516492f1417507c13ce1533dbdcd9d.service | ||
|
||
# Auto-update the container | ||
$ podman auto-update | ||
container-bc219740a210455fa27deacc96d50a9e20516492f1417507c13ce1533dbdcd9d.service | ||
``` | ||
|
||
## SEE ALSO | ||
podman(1), podman-generate-systemd(1), podman-run(1), systemd.unit(5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't actually work as described, but it's unrelated to your PR.
First: on my test setup (f31, root), I have to
restorecon /etc/systemd/system/xxx.service
, otherwise systemctl says "no such unit".Second, if the initial source container is started with
--name foo
,systemctl start
will constantly fail until youpodman rm -f
the original source container:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it go to
/usr/lib/systemd/system/
for system services? That's where I place them and selinux behaved.That's a super helpful observation! I think, we should not restart but stop-start the services, so that everything gets properly cleaned up. I'll investigate.
Thanks a lot for testing and the feedback, @edsantiago !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not: that's for installed packages; the recommendation is to use
/etc/systemd/system
for local configuration. Regardless, if I (root) am sitting in my home directory when I runpodman generate systemd --files
, thenmv
that file elsewhere, SELinux will have problems because the file will be labeledadmin_home_t
instead ofsystemd_unit_file_t
.This brings up a gripe I've long had, though: why does podman write the unit file to pwd? That causes pain for users who then have to figure out the correct destination, mv it, restorecon, etc. Is it too late to fix podman so it actually figures out the correct root/rootless path and writes the file to the proper place?
As I recall,
systemctl stop podman-xx.service
did not help (perhaps because of the--cidfile
option). Onlypodman rm -f
(orpodman stop, podman rm
) helped.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Misread the sentence. Yes, the initial container will remain. We want to add
--replace
which will remove existing containers in case of a name collision.