Alessandro Pilotti dc7699bc0e Implemented automatic updates plugin
Adds a plugin to configure Windows automatic updates.
The automatic updates configuration is retrieved
from the metadata service, or, if not set in the metadata,
from the config option "enable_automatic_updates".
The possible values for the config option are:
- Not set (None): no updates policy is configured
- True: updates are automatically installed
- False: updates are disabled on the system

Implements: blueprint windows-automated-updates-plugin
Co-Authored-By: Stefan Caraiman <scaraiman@cloudbasesolutions.com>
Co-Authored-By: Adrian Vladu <avladu@cloudbasesolutions.com>
Change-Id: Ida07a3a279321fe434a82a5123338679ec4506df
Signed-off-by: Stefan Caraiman <scaraiman@cloudbasesolutions.com>
2017-03-17 14:50:56 +02:00

44 lines
1.5 KiB
Python

# Copyright (c) 2017 Cloudbase Solutions Srl
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import random
from win32com import client
from cloudbaseinit.osutils import factory as osutils_factory
AU_DISABLED = 1
AU_SCHEDULED_INSTALLATION = 4
MIN_INSTALL_HOUR = 1
MAX_INSTALL_HOUR = 5
def set_automatic_updates(enabled):
# TODO(alexpilotti): the following settings are ignored on
# Windows 10 / Windows Server 2016 build 14393
auto_update = client.Dispatch("Microsoft.Update.AutoUpdate")
if enabled:
auto_update.Settings.NotificationLevel = AU_SCHEDULED_INSTALLATION
osutils = osutils_factory.get_os_utils()
if not osutils.check_os_version(6, 2):
# NOTE(alexpilotti): this setting is not supported starting
# with Windows 8 / Windows Server 2012
hour = random.randint(MIN_INSTALL_HOUR, MAX_INSTALL_HOUR)
auto_update.SettingsScheduledInstallationTime = hour
else:
auto_update.Settings.NotificationLevel = AU_DISABLED
auto_update.Settings.Save()