<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import os <EOL> import fnmatch <EOL> try : <EOL> import libvirt <EOL> HAS_LIBVIRT = True <EOL> except ImportError : <EOL> HAS_LIBVIRT = False <EOL> import salt . utils <EOL> from salt . exceptions import CommandExecutionError <EOL> __virtualname__ = '<STR_LIT>' <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in __salt__ : <EOL> return __virtualname__ <EOL> return False <EOL> def keys ( name , basepath = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:name>' : name , '<STR_LIT>' : { } , '<STR_LIT:result>' : True , '<STR_LIT>' : '<STR_LIT>' } <EOL> pillar = __salt__ [ '<STR_LIT>' ] ( { '<STR_LIT>' : '<STR_LIT:_>' } ) <EOL> paths = { <EOL> '<STR_LIT>' : os . path . join ( basepath , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : os . path . join ( basepath , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : os . path . join ( basepath , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : os . path . join ( basepath , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : os . path . join ( basepath , '<STR_LIT>' , '<STR_LIT>' ) <EOL> } <EOL> for key in paths : <EOL> p_key = '<STR_LIT>' . format ( key ) <EOL> if p_key not in pillar : <EOL> continue <EOL> if not os . path . exists ( os . path . dirname ( paths [ key ] ) ) : <EOL> os . makedirs ( os . path . dirname ( paths [ key ] ) ) <EOL> if os . path . isfile ( paths [ key ] ) : <EOL> with salt . utils . fopen ( paths [ key ] , '<STR_LIT:r>' ) as fp_ : <EOL> if fp_ . read ( ) != pillar [ p_key ] : <EOL> ret [ '<STR_LIT>' ] [ key ] = '<STR_LIT>' <EOL> else : <EOL> ret [ '<STR_LIT>' ] [ key ] = '<STR_LIT>' <EOL> if not ret [ '<STR_LIT>' ] : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif __opts__ [ '<STR_LIT:test>' ] : <EOL> ret [ '<STR_LIT:result>' ] = None <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> ret [ '<STR_LIT>' ] = { } <EOL> else : <EOL> for key in ret [ '<STR_LIT>' ] : <EOL> with salt . utils . fopen ( paths [ key ] , '<STR_LIT>' ) as fp_ : <EOL> fp_ . write ( pillar [ '<STR_LIT>' . format ( key ) ] ) <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret <EOL> def _virt_call ( domain , function , section , comment , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:name>' : domain , '<STR_LIT>' : { } , '<STR_LIT:result>' : True , '<STR_LIT>' : '<STR_LIT>' } <EOL> targeted_domains = fnmatch . filter ( __salt__ [ '<STR_LIT>' ] ( ) , domain ) <EOL> changed_domains = list ( ) <EOL> ignored_domains = list ( ) <EOL> for domain in targeted_domains : <EOL> try : <EOL> response = __salt__ [ '<STR_LIT>' . format ( function ) ] ( domain , ** kwargs ) <EOL> if isinstance ( response , dict ) : <EOL> response = response [ '<STR_LIT:name>' ] <EOL> changed_domains . append ( { '<STR_LIT>' : domain , function : response } ) <EOL> except libvirt . libvirtError as err : <EOL> ignored_domains . append ( { '<STR_LIT>' : domain , '<STR_LIT>' : str ( err ) } ) <EOL> if not changed_domains : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if ignored_domains : <EOL> ret [ '<STR_LIT>' ] = { '<STR_LIT>' : ignored_domains } <EOL> else : <EOL> ret [ '<STR_LIT>' ] = { section : changed_domains } <EOL> ret [ '<STR_LIT>' ] = comment <EOL> return ret <EOL> def stopped ( name ) : <EOL> '''<STR_LIT>''' <EOL> return _virt_call ( name , '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" ) <EOL> def powered_off ( name ) : <EOL> '''<STR_LIT>''' <EOL> return _virt_call ( name , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def running ( name , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT:result>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( name ) <EOL> } <EOL> kwargs = salt . utils . clean_kwargs ( ** kwargs ) <EOL> cpu = kwargs . pop ( '<STR_LIT>' , False ) <EOL> mem = kwargs . pop ( '<STR_LIT>' , False ) <EOL> image = kwargs . pop ( '<STR_LIT:image>' , False ) <EOL> try : <EOL> try : <EOL> __salt__ [ '<STR_LIT>' ] ( name ) <EOL> if __salt__ [ '<STR_LIT>' ] ( name ) != '<STR_LIT>' : <EOL> __salt__ [ '<STR_LIT>' ] ( name ) <EOL> ret [ '<STR_LIT>' ] [ name ] = '<STR_LIT>' <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> except CommandExecutionError : <EOL> kwargs = salt . utils . clean_kwargs ( ** kwargs ) <EOL> __salt__ [ '<STR_LIT>' ] ( name , cpu = cpu , mem = mem , image = image , ** kwargs ) <EOL> ret [ '<STR_LIT>' ] [ name ] = '<STR_LIT>' <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> except libvirt . libvirtError : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> return ret <EOL> def snapshot ( name , suffix = None ) : <EOL> '''<STR_LIT>''' <EOL> return _virt_call ( name , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , suffix = suffix ) <EOL> def rebooted ( name ) : <EOL> '''<STR_LIT>''' <EOL> return _virt_call ( name , '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" ) <EOL> def unpowered ( name ) : <EOL> '''<STR_LIT>''' <EOL> return _virt_call ( name , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def saved ( name , suffix = None ) : <EOL> '''<STR_LIT>''' <EOL> return _virt_call ( name , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , suffix = suffix ) <EOL> def reverted ( name , snapshot = None , cleanup = False ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:name>' : name , '<STR_LIT>' : { } , '<STR_LIT:result>' : False , '<STR_LIT>' : '<STR_LIT>' } <EOL> try : <EOL> domains = fnmatch . filter ( __salt__ [ '<STR_LIT>' ] ( ) , name ) <EOL> if not domains : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> else : <EOL> ignored_domains = list ( ) <EOL> if len ( domains ) > <NUM_LIT:1> : <EOL> ret [ '<STR_LIT>' ] = { '<STR_LIT>' : list ( ) } <EOL> for domain in domains : <EOL> result = { } <EOL> try : <EOL> result = __salt__ [ '<STR_LIT>' ] ( domain , snapshot = snapshot , cleanup = cleanup ) <EOL> result = { '<STR_LIT>' : domain , '<STR_LIT>' : result [ '<STR_LIT>' ] , '<STR_LIT>' : result [ '<STR_LIT>' ] } <EOL> except CommandExecutionError as err : <EOL> if len ( domains ) > <NUM_LIT:1> : <EOL> ignored_domains . append ( { '<STR_LIT>' : domain , '<STR_LIT>' : str ( err ) } ) <EOL> if len ( domains ) > <NUM_LIT:1> : <EOL> if result : <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] . append ( result ) <EOL> else : <EOL> ret [ '<STR_LIT>' ] = result <EOL> break <EOL> ret [ '<STR_LIT:result>' ] = len ( domains ) != len ( ignored_domains ) <EOL> if ret [ '<STR_LIT:result>' ] : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( len ( domains ) > <NUM_LIT:1> and "<STR_LIT:s>" or "<STR_LIT>" ) <EOL> if ignored_domains : <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = ignored_domains <EOL> if not ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> ret [ '<STR_LIT>' ] . pop ( '<STR_LIT>' ) <EOL> except libvirt . libvirtError as err : <EOL> ret [ '<STR_LIT>' ] = str ( err ) <EOL> except CommandExecutionError as err : <EOL> ret [ '<STR_LIT>' ] = str ( err ) <EOL> return ret </s>
