Skip to content

Commit d12f51e

Browse files
authored
[Compute] az vmss create/update: Add new parameters --automatic-repairs-action to support repair action (Azure#20485)
* repair action * test * update CLIError to ArgumentUsageError * conflicts * conflicts * update yaml * update * fix bug
1 parent 2e87ac8 commit d12f51e

8 files changed

+539
-652
lines changed

linter_exclusions.yml

+6
Original file line numberDiff line numberDiff line change
@@ -3632,6 +3632,9 @@ vmss create:
36323632
automatic_repairs_grace_period:
36333633
rule_exclusions:
36343634
- option_length_too_long
3635+
automatic_repairs_action:
3636+
rule_exclusions:
3637+
- option_length_too_long
36353638
data_disk_encryption_sets:
36363639
rule_exclusions:
36373640
- option_length_too_long
@@ -3689,6 +3692,9 @@ vmss update:
36893692
enable_automatic_repairs:
36903693
rule_exclusions:
36913694
- option_length_too_long
3695+
automatic_repairs_action:
3696+
rule_exclusions:
3697+
- option_length_too_long
36923698
enable_terminate_notification:
36933699
rule_exclusions:
36943700
- option_length_too_long

src/azure-cli/azure/cli/command_modules/vm/_params.py

+3
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ def load_arguments(self, _):
656656
c.argument('scale_in_policy', scale_in_policy_type)
657657
c.argument('automatic_repairs_grace_period', min_api='2018-10-01',
658658
help='The amount of time (in minutes, between 30 and 90) for which automatic repairs are suspended due to a state change on VM.')
659+
c.argument('automatic_repairs_action', arg_type=get_enum_type(['Replace', 'Restart', 'Reimage']), min_api='2021-11-01', help='Type of repair action that will be used for repairing unhealthy virtual machines in the scale set.')
659660
c.argument('user_data', help='UserData for the virtual machines in the scale set. It can be passed in as file or string.', completer=FilesCompleter(), type=file_type, min_api='2021-03-01')
660661
c.argument('network_api_version', min_api='2021-03-01',
661662
help="Specify the Microsoft.Network API version used when creating networking resources in the Network "
@@ -714,11 +715,13 @@ def load_arguments(self, _):
714715
c.argument('enable_vtpm', enable_vtpm_type)
715716

716717
with self.argument_context('vmss update', min_api='2018-10-01', arg_group='Automatic Repairs') as c:
718+
717719
c.argument('enable_automatic_repairs', arg_type=get_three_state_flag(), help='Enable automatic repairs')
718720
c.argument(
719721
'automatic_repairs_grace_period',
720722
help='The amount of time (in minutes, between 30 and 90) for which automatic repairs are suspended due to a state change on VM.'
721723
)
724+
c.argument('automatic_repairs_action', arg_type=get_enum_type(['Replace', 'Restart', 'Reimage']), min_api='2021-11-01', help='Type of repair action that will be used for repairing unhealthy virtual machines in the scale set.')
722725

723726
for scope in ['vmss create', 'vmss update']:
724727
with self.argument_context(scope) as c:

src/azure-cli/azure/cli/command_modules/vm/_template_builder.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ def build_vmss_resource(cmd, name, computer_name_prefix, location, tags, overpro
848848
orchestration_mode=None, user_data=None, network_api_version=None,
849849
enable_spot_restore=None, spot_restore_timeout=None, capacity_reservation_group=None,
850850
enable_auto_update=None, patch_mode=None, enable_agent=None, security_type=None,
851-
enable_secure_boot=None, enable_vtpm=None,):
851+
enable_secure_boot=None, enable_vtpm=None, automatic_repairs_action=None):
852852

853853
# Build IP configuration
854854
ip_configuration = {}
@@ -1167,10 +1167,11 @@ def build_vmss_resource(cmd, name, computer_name_prefix, location, tags, overpro
11671167
}
11681168
virtual_machine_profile['scheduledEventsProfile'] = scheduled_events_profile
11691169

1170-
if automatic_repairs_grace_period is not None:
1170+
if automatic_repairs_grace_period is not None or automatic_repairs_action is not None:
11711171
automatic_repairs_policy = {
11721172
'enabled': 'true',
1173-
'gracePeriod': automatic_repairs_grace_period
1173+
'gracePeriod': automatic_repairs_grace_period or 'PT10M',
1174+
'repairAction': automatic_repairs_action or 'Replace'
11741175
}
11751176
vmss_properties['automaticRepairsPolicy'] = automatic_repairs_policy
11761177

src/azure-cli/azure/cli/command_modules/vm/_validators.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1910,18 +1910,21 @@ def _validate_vmss_terminate_notification(cmd, namespace): # pylint: disable=un
19101910

19111911

19121912
def _validate_vmss_create_automatic_repairs(cmd, namespace): # pylint: disable=unused-argument
1913-
if namespace.automatic_repairs_grace_period is not None:
1913+
if namespace.automatic_repairs_grace_period is not None or namespace.automatic_repairs_action is not None:
19141914
if namespace.load_balancer is None or namespace.health_probe is None:
1915-
raise CLIError("usage error: --load-balancer and --health-probe are required "
1916-
"when creating vmss with automatic repairs")
1915+
raise ArgumentUsageError("usage error: --load-balancer and --health-probe are required "
1916+
"when creating vmss with automatic repairs")
19171917
_validate_vmss_automatic_repairs(cmd, namespace)
19181918

19191919

19201920
def _validate_vmss_update_automatic_repairs(cmd, namespace): # pylint: disable=unused-argument
1921-
if namespace.enable_automatic_repairs is False and namespace.automatic_repairs_grace_period is not None:
1922-
raise CLIError("usage error: please enable --enable-automatic-repairs")
1923-
if namespace.enable_automatic_repairs is True and namespace.automatic_repairs_grace_period is None:
1924-
raise CLIError("usage error: please set --automatic-repairs-grace-period")
1921+
if namespace.enable_automatic_repairs is False and \
1922+
(namespace.automatic_repairs_grace_period is not None or namespace.automatic_repairs_action is not None):
1923+
raise ArgumentUsageError("usage error: please enable --enable-automatic-repairs")
1924+
if namespace.enable_automatic_repairs is True and namespace.automatic_repairs_grace_period is None\
1925+
and namespace.automatic_repairs_action is None:
1926+
raise ArgumentUsageError("usage error: please set --automatic-repairs-grace-period or"
1927+
" --automatic-repairs-action")
19251928
_validate_vmss_automatic_repairs(cmd, namespace)
19261929

19271930

src/azure-cli/azure/cli/command_modules/vm/custom.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -2816,7 +2816,8 @@ def create_vmss(cmd, vmss_name, resource_group_name, image=None,
28162816
enable_cross_zone_upgrade=None, prioritize_unhealthy_instances=None, edge_zone=None,
28172817
user_data=None, network_api_version=None, enable_spot_restore=None, spot_restore_timeout=None,
28182818
capacity_reservation_group=None, enable_auto_update=None, patch_mode=None, enable_agent=None,
2819-
security_type=None, enable_secure_boot=None, enable_vtpm=None):
2819+
security_type=None, enable_secure_boot=None, enable_vtpm=None, automatic_repairs_action=None):
2820+
28202821
from azure.cli.core.commands.client_factory import get_subscription_id
28212822
from azure.cli.core.util import random_string, hash_string
28222823
from azure.cli.core.commands.arm import ArmTemplateBuilder
@@ -3079,7 +3080,8 @@ def _get_public_ip_address_allocation(value, sku):
30793080
enable_spot_restore=enable_spot_restore, spot_restore_timeout=spot_restore_timeout,
30803081
capacity_reservation_group=capacity_reservation_group, enable_auto_update=enable_auto_update,
30813082
patch_mode=patch_mode, enable_agent=enable_agent, security_type=security_type,
3082-
enable_secure_boot=enable_secure_boot, enable_vtpm=enable_vtpm)
3083+
enable_secure_boot=enable_secure_boot, enable_vtpm=enable_vtpm,
3084+
automatic_repairs_action=automatic_repairs_action)
30833085

30843086
vmss_resource['dependsOn'] = vmss_dependencies
30853087

@@ -3370,7 +3372,7 @@ def update_vmss(cmd, resource_group_name, name, license_type=None, no_wait=False
33703372
pause_time_between_batches=None, enable_cross_zone_upgrade=None, prioritize_unhealthy_instances=None,
33713373
user_data=None, enable_spot_restore=None, spot_restore_timeout=None, capacity_reservation_group=None,
33723374
vm_sku=None, ephemeral_os_disk_placement=None, force_deletion=None, enable_secure_boot=None,
3373-
enable_vtpm=None, **kwargs):
3375+
enable_vtpm=None, automatic_repairs_action=None, **kwargs):
33743376
vmss = kwargs['parameters']
33753377
aux_subscriptions = None
33763378
# pylint: disable=too-many-boolean-expressions
@@ -3430,10 +3432,13 @@ def update_vmss(cmd, resource_group_name, name, license_type=None, no_wait=False
34303432
TerminateNotificationProfile(not_before_timeout=terminate_notification_time,
34313433
enable=enable_terminate_notification)
34323434

3433-
if enable_automatic_repairs is not None or automatic_repairs_grace_period is not None:
3435+
if enable_automatic_repairs is not None or \
3436+
automatic_repairs_grace_period is not None or automatic_repairs_action is not None:
34343437
AutomaticRepairsPolicy = cmd.get_models('AutomaticRepairsPolicy')
34353438
vmss.automatic_repairs_policy = \
3436-
AutomaticRepairsPolicy(enabled="true", grace_period=automatic_repairs_grace_period)
3439+
AutomaticRepairsPolicy(enabled=enable_automatic_repairs,
3440+
grace_period=automatic_repairs_grace_period,
3441+
repair_action=automatic_repairs_action)
34373442

34383443
if ultra_ssd_enabled is not None:
34393444
if cmd.supported_api_version(min_api='2019-03-01', operation_group='virtual_machine_scale_sets'):

0 commit comments

Comments
 (0)