Skip to content
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

unattended-upgrades disable issue #308

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 23 additions & 51 deletions pkg/pmk/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,17 @@ func PrepNode(ctx objects.Config, allClients client.Client, auth keystone.Keysto

platform := debian.NewDebian(allClients.Executor)
result, _ := platform.CheckIfdpkgISLock()

if StatusUnattendedUpdates(allClients) {
// stop unattended-upgrades
// this do not stop them if they are already running
StopUnattendedUpdates(allClients)
defer StartUnattendedUpdates(allClients)

if IsEnabledUnattendedUpdates(allClients) {
DisableUnattendedUpdates(allClients)
defer EnableUnattendedUpdates(allClients)
}
}

if !result {
zap.S().Error("Dpkg lock is acquired by another process while prep-node was running")
return fmt.Errorf("Dpkg is under lock")
}

if ifUnattendedUpgradesAreEnabled(allClients) {
DisableUnattendedUpgrades(allClients)
defer EnableUnattendedUpgrades(allClients)
} else {
zap.S().Debug("unattended-upgrades are diabled by user")
}
}

present := pf9PackagesPresent(hostOS, allClients.Executor)
Expand Down Expand Up @@ -173,64 +167,42 @@ func PrepNode(ctx objects.Config, allClients client.Client, auth keystone.Keysto
return nil
}

func StatusUnattendedUpdates(allClients client.Client) bool {
zap.S().Debug("Checking Status of unattended-upgrades")
output, err := allClients.Executor.RunWithStdout("bash", "-c", "systemctl is-active unattended-upgrades")
if err != nil {
zap.S().Debugf("Failed to check unattended-upgrades : %s", err)
}
output = strings.TrimSpace(output)
return !strings.Contains(output, "inactive")
}

func StopUnattendedUpdates(allClients client.Client) {
zap.S().Debug("Stopping unattended-upgrades")
_, err := allClients.Executor.RunWithStdout("bash", "-c", "systemctl stop unattended-upgrades")
if err != nil {
zap.S().Debugf("Failed to stop unattended-upgrades : %s", err)
} else {
zap.S().Debug("Stopped unattended-upgrades")
}
}

func StartUnattendedUpdates(allClients client.Client) {
zap.S().Debug("Start unattended-upgrades")
_, err := allClients.Executor.RunWithStdout("bash", "-c", "systemctl start unattended-upgrades")
if err != nil {
zap.S().Debugf("Failed to start unattended-upgrades : %s", err)
} else {
zap.S().Debug("Started unattended-upgrades")
}
}

func IsEnabledUnattendedUpdates(allClients client.Client) bool {
func ifUnattendedUpgradesAreEnabled(allClients client.Client) bool {
zap.S().Debug("Checking if unattended-upgrades is enabled")
output, err := allClients.Executor.RunWithStdout("bash", "-c", "systemctl is-enabled unattended-upgrades")
output, err := allClients.Executor.RunWithStdout("bash", "-c", "cat /etc/apt/apt.conf.d/20auto-upgrades")
if err != nil {
zap.S().Debugf("Failed to check unattended-upgrades is enabled : %s", err)
zap.S().Debugf("Failed to check if unattended-upgrades are enabled : %s", err)
}
output = strings.TrimSpace(output)
return strings.Contains(output, "enabled")
return !strings.Contains(output, `APT::Periodic::Unattended-Upgrade "0";`)
}

func DisableUnattendedUpdates(allClients client.Client) {
func DisableUnattendedUpgrades(allClients client.Client) {
zap.S().Debug("Disabling unattended-upgrades")
_, err := allClients.Executor.RunWithStdout("bash", "-c", "systemctl disable unattended-upgrades")
_, err := allClients.Executor.RunWithStdout("bash", "-c", "sed -i 's/[1-9]/0/g' /etc/apt/apt.conf.d/20auto-upgrades")
if err != nil {
zap.S().Debugf("Failed to disable unattended-upgrades : %s", err)
} else {
zap.S().Debug("Disabled unattended-upgrades")
}
_, err = allClients.Executor.RunWithStdout("bash", "-c", "systemctl restart unattended-upgrades")
if err != nil {
zap.S().Debug("Failed to restart unattended-upgrades service")
}
}

func EnableUnattendedUpdates(allClients client.Client) {
func EnableUnattendedUpgrades(allClients client.Client) {
zap.S().Debug("Enabling unattended-upgrades")
_, err := allClients.Executor.RunWithStdout("bash", "-c", "systemctl enable unattended-upgrades")
_, err := allClients.Executor.RunWithStdout("bash", "-c", "sed -i 's/0/1/g' /etc/apt/apt.conf.d/20auto-upgrades")
if err != nil {
zap.S().Debugf("Failed to enable unattended-upgrades : %s", err)
} else {
zap.S().Debug("Enabled unattended-upgrades")
}
_, err = allClients.Executor.RunWithStdout("bash", "-c", "systemctl restart unattended-upgrades")
if err != nil {
zap.S().Debug("Failed to restart unattended-upgrades service")
}
}

func installHostAgent(ctx objects.Config, auth keystone.KeystoneAuth, hostOS string, exec cmdexec.Executor) error {
Expand Down