<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import logging <EOL> import time <EOL> from datetime import datetime <EOL> try : <EOL> import pythoncom <EOL> import wmi <EOL> import win32net <EOL> import win32api <EOL> import win32con <EOL> import pywintypes <EOL> from ctypes import windll <EOL> HAS_WIN32NET_MODS = True <EOL> except ImportError : <EOL> HAS_WIN32NET_MODS = False <EOL> import salt . utils <EOL> import salt . utils . locales <EOL> from salt . modules . reg import read_value <EOL> log = logging . getLogger ( __name__ ) <EOL> __virtualname__ = '<STR_LIT>' <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if HAS_WIN32NET_MODS and salt . utils . is_windows ( ) : <EOL> return __virtualname__ <EOL> return ( False , "<STR_LIT>" ) <EOL> def _convert_minutes_seconds ( timeout , in_seconds = False ) : <EOL> '''<STR_LIT>''' <EOL> return timeout if in_seconds else timeout * <NUM_LIT> <EOL> def _convert_date_time_string ( dt_string ) : <EOL> '''<STR_LIT>''' <EOL> dt_string = dt_string . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> dt_obj = datetime . strptime ( dt_string , '<STR_LIT>' ) <EOL> return dt_obj . strftime ( '<STR_LIT>' ) <EOL> def halt ( timeout = <NUM_LIT:5> , in_seconds = False ) : <EOL> '''<STR_LIT>''' <EOL> return shutdown ( timeout = timeout , in_seconds = in_seconds ) <EOL> def init ( runlevel ) : <EOL> '''<STR_LIT>''' <EOL> return '<STR_LIT>' <EOL> def poweroff ( timeout = <NUM_LIT:5> , in_seconds = False ) : <EOL> '''<STR_LIT>''' <EOL> return shutdown ( timeout = timeout , in_seconds = in_seconds ) <EOL> def reboot ( timeout = <NUM_LIT:5> , in_seconds = False , wait_for_reboot = False ) : <EOL> '''<STR_LIT>''' <EOL> ret = shutdown ( timeout = timeout , reboot = True , in_seconds = in_seconds ) <EOL> if wait_for_reboot : <EOL> seconds = _convert_minutes_seconds ( timeout , in_seconds ) <EOL> time . sleep ( seconds + <NUM_LIT:30> ) <EOL> return ret <EOL> def shutdown ( message = None , timeout = <NUM_LIT:5> , force_close = True , reboot = False , in_seconds = False ) : <EOL> '''<STR_LIT>''' <EOL> seconds = _convert_minutes_seconds ( timeout , in_seconds ) <EOL> if message : <EOL> message = message . decode ( '<STR_LIT:utf-8>' ) <EOL> try : <EOL> win32api . InitiateSystemShutdown ( '<STR_LIT:127.0.0.1>' , message , timeout , <EOL> force_close , reboot ) <EOL> return True <EOL> except pywintypes . error as exc : <EOL> ( number , context , message ) = exc <EOL> log . error ( '<STR_LIT>' ) <EOL> log . error ( '<STR_LIT>' . format ( number ) ) <EOL> log . error ( '<STR_LIT>' . format ( context ) ) <EOL> log . error ( '<STR_LIT>' . format ( message ) ) <EOL> return False <EOL> def shutdown_hard ( ) : <EOL> '''<STR_LIT>''' <EOL> return shutdown ( timeout = <NUM_LIT:0> ) <EOL> def shutdown_abort ( ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> win32api . AbortSystemShutdown ( '<STR_LIT:127.0.0.1>' ) <EOL> return True <EOL> except pywintypes . error as exc : <EOL> ( number , context , message ) = exc <EOL> log . error ( '<STR_LIT>' ) <EOL> log . error ( '<STR_LIT>' . format ( number ) ) <EOL> log . error ( '<STR_LIT>' . format ( context ) ) <EOL> log . error ( '<STR_LIT>' . format ( message ) ) <EOL> return False <EOL> def lock ( ) : <EOL> '''<STR_LIT>''' <EOL> return windll . user32 . LockWorkStation ( ) <EOL> def set_computer_name ( name ) : <EOL> '''<STR_LIT>''' <EOL> if name : <EOL> name = name . decode ( '<STR_LIT:utf-8>' ) <EOL> if windll . kernel32 . SetComputerNameExW ( win32con . ComputerNamePhysicalDnsHostname , <EOL> name ) : <EOL> ret = { '<STR_LIT>' : { '<STR_LIT>' : get_computer_name ( ) } } <EOL> pending = get_pending_computer_name ( ) <EOL> if pending not in ( None , False ) : <EOL> if pending == name . upper ( ) : <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = name <EOL> else : <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = pending <EOL> return ret <EOL> return False <EOL> def get_pending_computer_name ( ) : <EOL> '''<STR_LIT>''' <EOL> current = get_computer_name ( ) . upper ( ) <EOL> pending = read_value ( '<STR_LIT>' , <EOL> r'<STR_LIT>' , <EOL> '<STR_LIT>' ) [ '<STR_LIT>' ] <EOL> if pending : <EOL> return pending if pending != current else None <EOL> return False <EOL> def get_computer_name ( ) : <EOL> '''<STR_LIT>''' <EOL> name = win32api . GetComputerNameEx ( win32con . ComputerNamePhysicalDnsHostname ) <EOL> return name if name else False <EOL> def set_computer_desc ( desc = None ) : <EOL> '''<STR_LIT>''' <EOL> system_info = win32net . NetServerGetInfo ( None , <NUM_LIT> ) <EOL> if desc : <EOL> system_info [ '<STR_LIT>' ] = desc . decode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> return False <EOL> try : <EOL> win32net . NetServerSetInfo ( None , <NUM_LIT> , system_info ) <EOL> except win32net . error as exc : <EOL> ( number , context , message ) = exc <EOL> log . error ( '<STR_LIT>' ) <EOL> log . error ( '<STR_LIT>' . format ( number ) ) <EOL> log . error ( '<STR_LIT>' . format ( context ) ) <EOL> log . error ( '<STR_LIT>' . format ( message ) ) <EOL> return False <EOL> return { '<STR_LIT>' : get_computer_desc ( ) } <EOL> set_computer_description = salt . utils . alias_function ( set_computer_desc , '<STR_LIT>' ) <EOL> def get_system_info ( ) : <EOL> '''<STR_LIT>''' <EOL> os_type = { <NUM_LIT:1> : '<STR_LIT>' , <EOL> <NUM_LIT:2> : '<STR_LIT>' , <EOL> <NUM_LIT:3> : '<STR_LIT>' } <EOL> pythoncom . CoInitialize ( ) <EOL> c = wmi . WMI ( ) <EOL> system = c . Win32_OperatingSystem ( ) [ <NUM_LIT:0> ] <EOL> ret = { '<STR_LIT:name>' : get_computer_name ( ) , <EOL> '<STR_LIT:description>' : system . Description , <EOL> '<STR_LIT>' : system . InstallDate , <EOL> '<STR_LIT>' : system . LastBootUpTime , <EOL> '<STR_LIT>' : system . Manufacturer , <EOL> '<STR_LIT>' : system . Caption , <EOL> '<STR_LIT>' : system . NumberOfUsers , <EOL> '<STR_LIT>' : system . Organization , <EOL> '<STR_LIT>' : system . OSArchitecture , <EOL> '<STR_LIT>' : system . Primary , <EOL> '<STR_LIT>' : os_type [ system . ProductType ] , <EOL> '<STR_LIT>' : system . RegisteredUser , <EOL> '<STR_LIT>' : system . SystemDirectory , <EOL> '<STR_LIT>' : system . SystemDrive , <EOL> '<STR_LIT>' : system . Version , <EOL> '<STR_LIT>' : system . WindowsDirectory } <EOL> system = c . Win32_ComputerSystem ( ) [ <NUM_LIT:0> ] <EOL> ret . update ( { '<STR_LIT>' : system . Manufacturer , <EOL> '<STR_LIT>' : system . Model , <EOL> '<STR_LIT>' : system . NumberOfProcessors , <EOL> '<STR_LIT>' : system . NumberOfLogicalProcessors , <EOL> '<STR_LIT>' : system . SystemType } ) <EOL> system = c . Win32_BIOS ( ) [ <NUM_LIT:0> ] <EOL> ret . update ( { '<STR_LIT>' : system . SerialNumber , <EOL> '<STR_LIT>' : system . Manufacturer , <EOL> '<STR_LIT>' : system . Version , <EOL> '<STR_LIT>' : system . BIOSVersion , <EOL> '<STR_LIT>' : system . Caption , <EOL> '<STR_LIT>' : system . Description } ) <EOL> ret [ '<STR_LIT>' ] = _convert_date_time_string ( ret [ '<STR_LIT>' ] ) <EOL> ret [ '<STR_LIT>' ] = _convert_date_time_string ( ret [ '<STR_LIT>' ] ) <EOL> return ret <EOL> def get_computer_desc ( ) : <EOL> '''<STR_LIT>''' <EOL> desc = get_system_info ( ) [ '<STR_LIT:description>' ] <EOL> return desc if desc else False <EOL> get_computer_description = salt . utils . alias_function ( get_computer_desc , '<STR_LIT>' ) <EOL> def get_hostname ( ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' <EOL> ret = __salt__ [ '<STR_LIT>' ] ( cmd = cmd ) <EOL> _ , hostname = ret . split ( "<STR_LIT:\n>" ) <EOL> return hostname <EOL> def set_hostname ( hostname ) : <EOL> '''<STR_LIT>''' <EOL> curr_hostname = get_hostname ( ) <EOL> cmd = "<STR_LIT>" . format ( curr_hostname , hostname ) <EOL> ret = __salt__ [ '<STR_LIT>' ] ( cmd = cmd ) <EOL> return "<STR_LIT>" in ret <EOL> def _lookup_error ( number ) : <EOL> '''<STR_LIT>''' <EOL> return_values = { <EOL> <NUM_LIT:2> : '<STR_LIT>' , <EOL> <NUM_LIT:5> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> } <EOL> return return_values [ number ] <EOL> def join_domain ( domain , <EOL> username = None , <EOL> password = None , <EOL> account_ou = None , <EOL> account_exists = False , <EOL> restart = False ) : <EOL> '''<STR_LIT>''' <EOL> status = get_domain_workgroup ( ) <EOL> if '<STR_LIT>' in status : <EOL> if status [ '<STR_LIT>' ] == domain : <EOL> return '<STR_LIT>' . format ( domain ) <EOL> if username and '<STR_LIT:\\>' not in username and '<STR_LIT:@>' not in username : <EOL> username = '<STR_LIT>' . format ( username , domain ) <EOL> if username and password is None : <EOL> return '<STR_LIT>' <EOL> if isinstance ( account_ou , str ) : <EOL> account_ou = account_ou . split ( '<STR_LIT:\\>' ) <EOL> account_ou = '<STR_LIT>' . join ( account_ou ) <EOL> NETSETUP_JOIN_DOMAIN = <NUM_LIT> <EOL> NETSETUP_ACCOUNT_CREATE = <NUM_LIT> <EOL> NETSETUP_DOMAIN_JOIN_IF_JOINED = <NUM_LIT> <EOL> join_options = <NUM_LIT> <EOL> join_options |= NETSETUP_JOIN_DOMAIN <EOL> join_options |= NETSETUP_DOMAIN_JOIN_IF_JOINED <EOL> if not account_exists : <EOL> join_options |= NETSETUP_ACCOUNT_CREATE <EOL> pythoncom . CoInitialize ( ) <EOL> c = wmi . WMI ( ) <EOL> comp = c . Win32_ComputerSystem ( ) [ <NUM_LIT:0> ] <EOL> err = comp . JoinDomainOrWorkgroup ( Name = domain , <EOL> Password = password , <EOL> UserName = username , <EOL> AccountOU = account_ou , <EOL> FJoinOptions = join_options ) <EOL> if not err [ <NUM_LIT:0> ] : <EOL> ret = { '<STR_LIT>' : domain , <EOL> '<STR_LIT>' : False } <EOL> if restart : <EOL> ret [ '<STR_LIT>' ] = reboot ( ) <EOL> return ret <EOL> log . error ( _lookup_error ( err [ <NUM_LIT:0> ] ) ) <EOL> return False <EOL> def unjoin_domain ( username = None , <EOL> password = None , <EOL> domain = None , <EOL> workgroup = '<STR_LIT>' , <EOL> disable = False , <EOL> restart = False ) : <EOL> r'''<STR_LIT>''' <EOL> status = get_domain_workgroup ( ) <EOL> if '<STR_LIT>' in status : <EOL> if status [ '<STR_LIT>' ] == workgroup : <EOL> return '<STR_LIT>' . format ( workgroup ) <EOL> if username and '<STR_LIT:\\>' not in username and '<STR_LIT:@>' not in username : <EOL> if domain : <EOL> username = '<STR_LIT>' . format ( username , domain ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> if username and password is None : <EOL> return '<STR_LIT>' <EOL> NETSETUP_ACCT_DELETE = <NUM_LIT> <EOL> unjoin_options = <NUM_LIT> <EOL> if disable : <EOL> unjoin_options |= NETSETUP_ACCT_DELETE <EOL> pythoncom . CoInitialize ( ) <EOL> c = wmi . WMI ( ) <EOL> comp = c . Win32_ComputerSystem ( ) [ <NUM_LIT:0> ] <EOL> err = comp . UnjoinDomainOrWorkgroup ( Password = password , <EOL> UserName = username , <EOL> FUnjoinOptions = unjoin_options ) <EOL> if not err [ <NUM_LIT:0> ] : <EOL> err = comp . JoinDomainOrWorkgroup ( Name = workgroup ) <EOL> if not err [ <NUM_LIT:0> ] : <EOL> ret = { '<STR_LIT>' : workgroup , <EOL> '<STR_LIT>' : False } <EOL> if restart : <EOL> ret [ '<STR_LIT>' ] = reboot ( ) <EOL> return ret <EOL> else : <EOL> log . error ( _lookup_error ( err [ <NUM_LIT:0> ] ) ) <EOL> log . error ( '<STR_LIT>' . format ( workgroup ) ) <EOL> return False <EOL> else : <EOL> log . error ( _lookup_error ( err [ <NUM_LIT:0> ] ) ) <EOL> log . error ( '<STR_LIT>' . format ( status [ '<STR_LIT>' ] ) ) <EOL> return False <EOL> def get_domain_workgroup ( ) : <EOL> '''<STR_LIT>''' <EOL> pythoncom . CoInitialize ( ) <EOL> c = wmi . WMI ( ) <EOL> for computer in c . Win32_ComputerSystem ( ) : <EOL> if computer . PartOfDomain : <EOL> return { '<STR_LIT>' : computer . Domain } <EOL> else : <EOL> return { '<STR_LIT>' : computer . Domain } <EOL> def _get_date_time_format ( dt_string ) : <EOL> '''<STR_LIT>''' <EOL> valid_formats = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> for dt_format in valid_formats : <EOL> try : <EOL> datetime . strptime ( dt_string , dt_format ) <EOL> return dt_format <EOL> except ValueError : <EOL> continue <EOL> return False <EOL> def get_system_time ( ) : <EOL> '''<STR_LIT>''' <EOL> return datetime . strftime ( datetime . now ( ) , "<STR_LIT>" ) <EOL> def set_system_time ( newtime ) : <EOL> '''<STR_LIT>''' <EOL> time_format = _get_date_time_format ( newtime ) <EOL> dt_obj = datetime . strptime ( newtime , time_format ) <EOL> return set_system_date_time ( hours = int ( dt_obj . strftime ( '<STR_LIT>' ) ) , <EOL> minutes = int ( dt_obj . strftime ( '<STR_LIT>' ) ) , <EOL> seconds = int ( dt_obj . strftime ( '<STR_LIT>' ) ) ) <EOL> def set_system_date_time ( years = None , <EOL> months = None , <EOL> days = None , <EOL> hours = None , <EOL> minutes = None , <EOL> seconds = None ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> date_time = win32api . GetLocalTime ( ) <EOL> except win32api . error as exc : <EOL> ( number , context , message ) = exc <EOL> log . error ( '<STR_LIT>' ) <EOL> log . error ( '<STR_LIT>' . format ( number ) ) <EOL> log . error ( '<STR_LIT>' . format ( context ) ) <EOL> log . error ( '<STR_LIT>' . format ( message ) ) <EOL> return False <EOL> if not years : <EOL> years = date_time [ <NUM_LIT:0> ] <EOL> if not months : <EOL> months = date_time [ <NUM_LIT:1> ] <EOL> if not days : <EOL> days = date_time [ <NUM_LIT:3> ] <EOL> if not hours : <EOL> hours = date_time [ <NUM_LIT:4> ] <EOL> if not minutes : <EOL> minutes = date_time [ <NUM_LIT:5> ] <EOL> if not seconds : <EOL> seconds = date_time [ <NUM_LIT:6> ] <EOL> time_tuple = ( years , months , days , hours , minutes , seconds , <NUM_LIT:0> ) <EOL> try : <EOL> win32api . SetLocalTime ( time_tuple ) <EOL> except win32api . error as exc : <EOL> ( number , context , message ) = exc <EOL> log . error ( '<STR_LIT>' ) <EOL> log . error ( '<STR_LIT>' . format ( number ) ) <EOL> log . error ( '<STR_LIT>' . format ( context ) ) <EOL> log . error ( '<STR_LIT>' . format ( message ) ) <EOL> return False <EOL> return True <EOL> def get_system_date ( ) : <EOL> '''<STR_LIT>''' <EOL> return datetime . strftime ( datetime . now ( ) , "<STR_LIT>" ) <EOL> def set_system_date ( newdate ) : <EOL> '''<STR_LIT>''' <EOL> date_format = _get_date_time_format ( newdate ) <EOL> dt_obj = datetime . strptime ( newdate , date_format ) <EOL> return set_system_date_time ( years = int ( dt_obj . strftime ( '<STR_LIT>' ) ) , <EOL> months = int ( dt_obj . strftime ( '<STR_LIT>' ) ) , <EOL> days = int ( dt_obj . strftime ( '<STR_LIT>' ) ) ) <EOL> def start_time_service ( ) : <EOL> '''<STR_LIT>''' <EOL> return __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' ) <EOL> def stop_time_service ( ) : <EOL> '''<STR_LIT>''' <EOL> return __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' ) <EOL> def get_pending_component_servicing ( ) : <EOL> '''<STR_LIT>''' <EOL> vname = '<STR_LIT>' <EOL> key = r'<STR_LIT>' <EOL> reg_ret = read_value ( '<STR_LIT>' , key , vname ) <EOL> if reg_ret [ '<STR_LIT:success>' ] : <EOL> log . debug ( '<STR_LIT>' , key ) <EOL> return True <EOL> else : <EOL> log . debug ( '<STR_LIT>' , key ) <EOL> return False <EOL> def get_pending_domain_join ( ) : <EOL> '''<STR_LIT>''' <EOL> vname = '<STR_LIT>' <EOL> base_key = r'<STR_LIT>' <EOL> avoid_key = r'<STR_LIT>' . format ( base_key ) <EOL> join_key = r'<STR_LIT>' . format ( base_key ) <EOL> avoid_reg_ret = read_value ( '<STR_LIT>' , avoid_key , vname ) <EOL> if avoid_reg_ret [ '<STR_LIT:success>' ] : <EOL> log . debug ( '<STR_LIT>' , avoid_key ) <EOL> return True <EOL> else : <EOL> log . debug ( '<STR_LIT>' , avoid_key ) <EOL> join_reg_ret = read_value ( '<STR_LIT>' , join_key , vname ) <EOL> if join_reg_ret [ '<STR_LIT:success>' ] : <EOL> log . debug ( '<STR_LIT>' , join_key ) <EOL> return True <EOL> else : <EOL> log . debug ( '<STR_LIT>' , join_key ) <EOL> return False <EOL> def get_pending_file_rename ( ) : <EOL> '''<STR_LIT>''' <EOL> vnames = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> key = r'<STR_LIT>' <EOL> for vname in vnames : <EOL> reg_ret = read_value ( '<STR_LIT>' , key , vname ) <EOL> if reg_ret [ '<STR_LIT:success>' ] : <EOL> log . debug ( '<STR_LIT>' , key ) <EOL> if reg_ret [ '<STR_LIT>' ] and ( reg_ret [ '<STR_LIT>' ] != '<STR_LIT>' ) : <EOL> return True <EOL> else : <EOL> log . debug ( '<STR_LIT>' , key ) <EOL> return False <EOL> def get_pending_servermanager ( ) : <EOL> '''<STR_LIT>''' <EOL> vname = '<STR_LIT>' <EOL> key = r'<STR_LIT>' <EOL> reg_ret = read_value ( '<STR_LIT>' , key , vname ) <EOL> if reg_ret [ '<STR_LIT:success>' ] : <EOL> log . debug ( '<STR_LIT>' , key ) <EOL> try : <EOL> if int ( reg_ret [ '<STR_LIT>' ] ) > <NUM_LIT:0> : <EOL> return True <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> log . debug ( '<STR_LIT>' , key ) <EOL> return False <EOL> def get_pending_update ( ) : <EOL> '''<STR_LIT>''' <EOL> vname = '<STR_LIT>' <EOL> key = r'<STR_LIT>' <EOL> reg_ret = read_value ( '<STR_LIT>' , key , vname ) <EOL> if reg_ret [ '<STR_LIT:success>' ] : <EOL> log . debug ( '<STR_LIT>' , key ) <EOL> return True <EOL> else : <EOL> log . debug ( '<STR_LIT>' , key ) <EOL> return False <EOL> def get_pending_reboot ( ) : <EOL> '''<STR_LIT>''' <EOL> checks = ( get_pending_update , get_pending_file_rename , get_pending_servermanager , <EOL> get_pending_component_servicing , get_pending_computer_name , <EOL> get_pending_domain_join ) <EOL> for check in checks : <EOL> if check ( ) : <EOL> return True <EOL> return False </s>
