<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import logging <EOL> from salt . exceptions import SaltSystemExit <EOL> __proxyenabled__ = [ '<STR_LIT>' ] <EOL> GRAINS_CACHE = { } <EOL> DETAILS = { } <EOL> log = logging . getLogger ( __file__ ) <EOL> __virtualname__ = '<STR_LIT>' <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in __salt__ : <EOL> return __virtualname__ <EOL> return False , '<STR_LIT>' <EOL> def init ( opts ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT:host>' not in opts [ '<STR_LIT>' ] : <EOL> log . critical ( '<STR_LIT>' ) <EOL> return False <EOL> if '<STR_LIT:username>' not in opts [ '<STR_LIT>' ] : <EOL> log . critical ( '<STR_LIT>' ) <EOL> return False <EOL> if '<STR_LIT>' not in opts [ '<STR_LIT>' ] : <EOL> log . critical ( '<STR_LIT>' ) <EOL> return False <EOL> host = opts [ '<STR_LIT>' ] [ '<STR_LIT:host>' ] <EOL> try : <EOL> username , password = find_credentials ( host ) <EOL> except SaltSystemExit as err : <EOL> log . critical ( '<STR_LIT>' . format ( err ) ) <EOL> return False <EOL> DETAILS [ '<STR_LIT:host>' ] = host <EOL> DETAILS [ '<STR_LIT:username>' ] = username <EOL> DETAILS [ '<STR_LIT:password>' ] = password <EOL> DETAILS [ '<STR_LIT>' ] = opts [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> DETAILS [ '<STR_LIT:port>' ] = opts [ '<STR_LIT>' ] . get ( '<STR_LIT:port>' ) <EOL> def grains ( ) : <EOL> '''<STR_LIT>''' <EOL> if not GRAINS_CACHE : <EOL> return _grains ( DETAILS [ '<STR_LIT:host>' ] , <EOL> DETAILS [ '<STR_LIT>' ] , <EOL> DETAILS [ '<STR_LIT:port>' ] ) <EOL> return GRAINS_CACHE <EOL> def grains_refresh ( ) : <EOL> '''<STR_LIT>''' <EOL> GRAINS_CACHE = { } <EOL> return grains ( ) <EOL> def ping ( ) : <EOL> '''<STR_LIT>''' <EOL> find_credentials ( DETAILS [ '<STR_LIT:host>' ] ) <EOL> try : <EOL> __salt__ [ '<STR_LIT>' ] ( host = DETAILS [ '<STR_LIT:host>' ] , <EOL> username = DETAILS [ '<STR_LIT:username>' ] , <EOL> password = DETAILS [ '<STR_LIT:password>' ] ) <EOL> except SaltSystemExit as err : <EOL> log . warning ( err ) <EOL> return False <EOL> return True <EOL> def shutdown ( ) : <EOL> '''<STR_LIT>''' <EOL> log . debug ( '<STR_LIT>' ) <EOL> def ch_config ( cmd , * args , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> for k in kwargs . keys ( ) : <EOL> if k . startswith ( '<STR_LIT>' ) : <EOL> kwargs . pop ( k ) <EOL> if '<STR_LIT>' + cmd not in __salt__ : <EOL> return { '<STR_LIT>' : - <NUM_LIT:1> , '<STR_LIT:message>' : '<STR_LIT>' + cmd + '<STR_LIT>' } <EOL> else : <EOL> return __salt__ [ '<STR_LIT>' + cmd ] ( * args , ** kwargs ) <EOL> def find_credentials ( host ) : <EOL> '''<STR_LIT>''' <EOL> user_names = [ __pillar__ [ '<STR_LIT>' ] . get ( '<STR_LIT:username>' , '<STR_LIT:root>' ) ] <EOL> passwords = __pillar__ [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> for user in user_names : <EOL> for password in passwords : <EOL> try : <EOL> ret = __salt__ [ '<STR_LIT>' ] ( host = host , <EOL> username = user , <EOL> password = password ) <EOL> except SaltSystemExit : <EOL> continue <EOL> if ret : <EOL> DETAILS [ '<STR_LIT:username>' ] = user <EOL> DETAILS [ '<STR_LIT:password>' ] = password <EOL> return user , password <EOL> raise SaltSystemExit ( '<STR_LIT>' ) <EOL> def _grains ( host , protocol = None , port = None ) : <EOL> '''<STR_LIT>''' <EOL> username , password = find_credentials ( DETAILS [ '<STR_LIT:host>' ] ) <EOL> ret = __salt__ [ '<STR_LIT>' ] ( host = host , <EOL> username = username , <EOL> password = password , <EOL> protocol = protocol , <EOL> port = port ) <EOL> GRAINS_CACHE . update ( ret ) <EOL> return GRAINS_CACHE </s>
