@@ -2288,3 +2288,128 @@ def get(
22882288 return ret
22892289 except DEFAULT_EXCEPTIONS as exc :
22902290 raise salt .exceptions .SaltException (str (exc ))
2291+
2292+
2293+ def in_maintenance_mode (host , service_instance = None ):
2294+ """
2295+ Check if host is in maintenance mode.
2296+
2297+ host
2298+ Host IP or HostSystem/ManagedObjectReference (required).
2299+
2300+ service_instance
2301+ Use this vCenter service connection instance instead of creating a new one (optional).
2302+
2303+ .. code-block:: bash
2304+
2305+ salt '*' vmware_esxi.in_maintenance_mode '10.288.6.117'
2306+ """
2307+ if isinstance (host , vim .HostSystem ):
2308+ host_ref = host
2309+ else :
2310+ if service_instance is None :
2311+ service_instance = get_service_instance (opts = __opts__ , pillar = __pillar__ )
2312+ host_ref = utils_esxi .get_host (host , service_instance )
2313+ mode = "normal"
2314+ if host_ref .runtime .inMaintenanceMode :
2315+ mode = "inMaintenance"
2316+ return {"maintenanceMode" : mode }
2317+
2318+
2319+ def maintenance_mode (
2320+ host ,
2321+ timeout = 0 ,
2322+ evacuate_powered_off_vms = False ,
2323+ maintenance_spec = None ,
2324+ catch_task_error = True ,
2325+ service_instance = None ,
2326+ ):
2327+ """
2328+ Put host into maintenance mode.
2329+
2330+ host
2331+ Host IP or HostSystem/ManagedObjectReference (required).
2332+
2333+ timeout
2334+ If value is greater than 0 then task will timeout if not completed with in window (optional).
2335+
2336+ evacuate_powered_off_vms
2337+ Only supported by VirtualCenter (optional).
2338+ If True, for DRS will fail unless all powered-off VMs have been manually registered.
2339+ If False, task will successed with powered-off VMs.
2340+
2341+ maintenance_spec
2342+ HostMaintenanceSpec (optional).
2343+
2344+ catch_task_error
2345+ If False and task failed then a salt exception will be thrown (optional).
2346+
2347+ service_instance
2348+ Use this vCenter service connection instance instead of creating a new one (optional).
2349+
2350+ .. code-block:: bash
2351+
2352+ salt '*' vmware_esxi.maintenance_mode '10.288.6.117'
2353+ """
2354+ if isinstance (host , vim .HostSystem ):
2355+ host_ref = host
2356+ else :
2357+ if service_instance is None :
2358+ service_instance = get_service_instance (opts = __opts__ , pillar = __pillar__ )
2359+ host_ref = utils_esxi .get_host (host , service_instance )
2360+ mode = in_maintenance_mode (host_ref )
2361+ if mode ["maintenanceMode" ] == "inMaintenance" :
2362+ mode ["changes" ] = False
2363+ return mode
2364+ try :
2365+ task = host_ref .EnterMaintenanceMode_Task (
2366+ timeout , evacuate_powered_off_vms , maintenance_spec
2367+ )
2368+ utils_common .wait_for_task (task , host_ref .name , "maintenanceMode" )
2369+ except salt .exceptions .SaltException as exc :
2370+ if not catch_task_error :
2371+ raise exc
2372+ mode = in_maintenance_mode (host_ref , service_instance )
2373+ mode ["changes" ] = mode ["maintenanceMode" ] == "inMaintenance"
2374+ return mode
2375+
2376+
2377+ def exit_maintenance_mode (host , timeout = 0 , catch_task_error = True , service_instance = None ):
2378+ """
2379+ Put host out of maintenance mode.
2380+
2381+ host
2382+ Host IP or HostSystem/ManagedObjectReference (required).
2383+
2384+ timeout
2385+ If value is greater than 0 then task will timeout if not completed with in window (optional).
2386+
2387+ catch_task_error
2388+ If False and task failed then a salt exception will be thrown (optional).
2389+
2390+ service_instance
2391+ Use this vCenter service connection instance instead of creating a new one (optional).
2392+
2393+ .. code-block:: bash
2394+
2395+ salt '*' vmware_esxi.exit_maintenance_mode '10.288.6.117'
2396+ """
2397+ if isinstance (host , vim .HostSystem ):
2398+ host_ref = host
2399+ else :
2400+ if service_instance is None :
2401+ service_instance = get_service_instance (opts = __opts__ , pillar = __pillar__ )
2402+ host_ref = utils_esxi .get_host (host , service_instance )
2403+ mode = in_maintenance_mode (host_ref )
2404+ if mode ["maintenanceMode" ] == "normal" :
2405+ mode ["changes" ] = False
2406+ return mode
2407+ try :
2408+ task = host_ref .ExitMaintenanceMode_Task (timeout )
2409+ utils_common .wait_for_task (task , host_ref .name , "maintenanceMode" )
2410+ except salt .exceptions .SaltException as exc :
2411+ if not catch_task_error :
2412+ raise exc
2413+ mode = in_maintenance_mode (host_ref , service_instance )
2414+ mode ["changes" ] = mode ["maintenanceMode" ] == "normal"
2415+ return mode
0 commit comments