diff --git a/pkg/pmk/node.go b/pkg/pmk/node.go index 2032145..86ff64d 100644 --- a/pkg/pmk/node.go +++ b/pkg/pmk/node.go @@ -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) @@ -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 {