<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import , print_function <EOL> import os . path <EOL> import logging <EOL> import salt . client <EOL> import salt . utils . virt <EOL> import salt . utils . cloud <EOL> import salt . key <EOL> from salt . exceptions import SaltClientError <EOL> import salt . ext . six as six <EOL> log = logging . getLogger ( __name__ ) <EOL> def _determine_host ( data , omit = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> host = '<STR_LIT>' <EOL> bestmem = <NUM_LIT:0> <EOL> for hv_ , comps in six . iteritems ( data ) : <EOL> if hv_ == omit : <EOL> continue <EOL> if not isinstance ( comps , dict ) : <EOL> continue <EOL> if comps . get ( '<STR_LIT>' , <NUM_LIT:0> ) > bestmem : <EOL> bestmem = comps [ '<STR_LIT>' ] <EOL> host = hv_ <EOL> return host <EOL> def _find_vm ( name , data , quiet = False ) : <EOL> '''<STR_LIT>''' <EOL> for hv_ in data : <EOL> if not isinstance ( data [ hv_ ] , dict ) : <EOL> continue <EOL> if name in data [ hv_ ] . get ( '<STR_LIT>' , { } ) : <EOL> ret = { hv_ : { name : data [ hv_ ] [ '<STR_LIT>' ] [ name ] } } <EOL> if not quiet : <EOL> __jid_event__ . fire_event ( { '<STR_LIT:data>' : ret , '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> return ret <EOL> return { } <EOL> def query ( host = None , quiet = False , hyper = None ) : <EOL> '''<STR_LIT>''' <EOL> if hyper is not None : <EOL> salt . utils . warn_until ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> host = hyper <EOL> if quiet : <EOL> log . warning ( "<STR_LIT>" ) <EOL> ret = { } <EOL> client = salt . client . get_local_client ( __opts__ [ '<STR_LIT>' ] ) <EOL> try : <EOL> for info in client . cmd_iter ( '<STR_LIT>' , <EOL> '<STR_LIT>' , expr_form = '<STR_LIT>' ) : <EOL> if not info : <EOL> continue <EOL> if not isinstance ( info , dict ) : <EOL> continue <EOL> chunk = { } <EOL> id_ = next ( info . iterkeys ( ) ) <EOL> if host : <EOL> if host != id_ : <EOL> continue <EOL> if not isinstance ( info [ id_ ] , dict ) : <EOL> continue <EOL> if '<STR_LIT>' not in info [ id_ ] : <EOL> continue <EOL> if not isinstance ( info [ id_ ] [ '<STR_LIT>' ] , dict ) : <EOL> continue <EOL> chunk [ id_ ] = info [ id_ ] [ '<STR_LIT>' ] <EOL> ret . update ( chunk ) <EOL> if not quiet : <EOL> __jid_event__ . fire_event ( { '<STR_LIT:data>' : chunk , '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> except SaltClientError as client_error : <EOL> print ( client_error ) <EOL> return ret <EOL> def list ( host = None , quiet = False , hyper = None ) : <EOL> '''<STR_LIT>''' <EOL> if hyper is not None : <EOL> salt . utils . warn_until ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> host = hyper <EOL> if quiet : <EOL> log . warning ( "<STR_LIT>" ) <EOL> ret = { } <EOL> client = salt . client . get_local_client ( __opts__ [ '<STR_LIT>' ] ) <EOL> for info in client . cmd_iter ( '<STR_LIT>' , <EOL> '<STR_LIT>' , expr_form = '<STR_LIT>' ) : <EOL> if not info : <EOL> continue <EOL> if not isinstance ( info , dict ) : <EOL> continue <EOL> chunk = { } <EOL> id_ = next ( six . iterkeys ( info ) ) <EOL> if host : <EOL> if host != id_ : <EOL> continue <EOL> if not isinstance ( info [ id_ ] , dict ) : <EOL> continue <EOL> if '<STR_LIT>' not in info [ id_ ] : <EOL> continue <EOL> if not isinstance ( info [ id_ ] [ '<STR_LIT>' ] , dict ) : <EOL> continue <EOL> data = { } <EOL> for key , val in six . iteritems ( info [ id_ ] [ '<STR_LIT>' ] ) : <EOL> if val [ '<STR_LIT:state>' ] in data : <EOL> data [ val [ '<STR_LIT:state>' ] ] . append ( key ) <EOL> else : <EOL> data [ val [ '<STR_LIT:state>' ] ] = [ key ] <EOL> chunk [ id_ ] = data <EOL> ret . update ( chunk ) <EOL> if not quiet : <EOL> __jid_event__ . fire_event ( { '<STR_LIT:data>' : chunk , '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> return ret <EOL> def next_host ( ) : <EOL> '''<STR_LIT>''' <EOL> host = _determine_host ( query ( quiet = True ) ) <EOL> print ( host ) <EOL> return host <EOL> def next_hyper ( ) : <EOL> '''<STR_LIT>''' <EOL> salt . utils . warn_until ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> return next_host ( ) <EOL> def host_info ( host = None ) : <EOL> '''<STR_LIT>''' <EOL> data = query ( host , quiet = True ) <EOL> for id_ in data : <EOL> if '<STR_LIT>' in data [ id_ ] : <EOL> data [ id_ ] . pop ( '<STR_LIT>' ) <EOL> __jid_event__ . fire_event ( { '<STR_LIT:data>' : data , '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> return data <EOL> def hyper_info ( hyper = None ) : <EOL> '''<STR_LIT>''' <EOL> salt . utils . warn_until ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> return host_info ( hyper ) <EOL> def init ( <EOL> name , <EOL> cpu , <EOL> mem , <EOL> image , <EOL> hyper = None , <EOL> hypervisor = '<STR_LIT>' , <EOL> host = None , <EOL> seed = True , <EOL> nic = '<STR_LIT:default>' , <EOL> install = True , <EOL> start = True , <EOL> disk = '<STR_LIT:default>' , <EOL> saltenv = '<STR_LIT>' , <EOL> enable_vnc = False ) : <EOL> '''<STR_LIT>''' <EOL> if hyper is not None : <EOL> salt . utils . warn_until ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> host = hyper <EOL> __jid_event__ . fire_event ( { '<STR_LIT:message>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> data = query ( host , quiet = True ) <EOL> for node in data : <EOL> if '<STR_LIT>' in data [ node ] : <EOL> if name in data [ node ] [ '<STR_LIT>' ] : <EOL> __jid_event__ . fire_event ( <EOL> { '<STR_LIT:message>' : '<STR_LIT>' . format ( name ) } , <EOL> '<STR_LIT>' <EOL> ) <EOL> return '<STR_LIT>' <EOL> if host is None : <EOL> host = _determine_host ( data ) <EOL> if host not in data or not host : <EOL> __jid_event__ . fire_event ( <EOL> { '<STR_LIT:message>' : '<STR_LIT>' . format ( host ) } , <EOL> '<STR_LIT>' <EOL> ) <EOL> return '<STR_LIT>' <EOL> pub_key = None <EOL> priv_key = None <EOL> if seed : <EOL> __jid_event__ . fire_event ( { '<STR_LIT:message>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> priv_key , pub_key = salt . utils . cloud . gen_keys ( ) <EOL> accepted_key = os . path . join ( __opts__ [ '<STR_LIT>' ] , '<STR_LIT>' , name ) <EOL> with salt . utils . fopen ( accepted_key , '<STR_LIT:w>' ) as fp_ : <EOL> fp_ . write ( pub_key ) <EOL> client = salt . client . get_local_client ( __opts__ [ '<STR_LIT>' ] ) <EOL> __jid_event__ . fire_event ( <EOL> { '<STR_LIT:message>' : '<STR_LIT>' . format ( name , host ) } , <EOL> '<STR_LIT>' <EOL> ) <EOL> try : <EOL> cmd_ret = client . cmd_iter ( <EOL> host , <EOL> '<STR_LIT>' , <EOL> [ <EOL> name , <EOL> cpu , <EOL> mem , <EOL> image , <EOL> nic , <EOL> hypervisor , <EOL> start , <EOL> disk , <EOL> saltenv , <EOL> seed , <EOL> install , <EOL> pub_key , <EOL> priv_key , <EOL> enable_vnc , <EOL> ] , <EOL> timeout = <NUM_LIT> ) <EOL> except SaltClientError as client_error : <EOL> print ( client_error ) <EOL> ret = next ( cmd_ret ) <EOL> if not ret : <EOL> __jid_event__ . fire_event ( { '<STR_LIT:message>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> for minion_id in ret : <EOL> if ret [ minion_id ] [ '<STR_LIT>' ] is False : <EOL> print ( '<STR_LIT>' . format ( name , ret [ minion_id ] [ '<STR_LIT>' ] ) ) <EOL> return '<STR_LIT>' <EOL> __jid_event__ . fire_event ( { '<STR_LIT:message>' : '<STR_LIT>' . format ( name , host ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> def vm_info ( name , quiet = False ) : <EOL> '''<STR_LIT>''' <EOL> data = query ( quiet = True ) <EOL> return _find_vm ( name , data , quiet ) <EOL> def reset ( name ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> client = salt . client . get_local_client ( __opts__ [ '<STR_LIT>' ] ) <EOL> data = vm_info ( name , quiet = True ) <EOL> if not data : <EOL> __jid_event__ . fire_event ( { '<STR_LIT:message>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> host = next ( six . iterkeys ( data ) ) <EOL> try : <EOL> cmd_ret = client . cmd_iter ( <EOL> host , <EOL> '<STR_LIT>' , <EOL> [ name ] , <EOL> timeout = <NUM_LIT> ) <EOL> for comp in cmd_ret : <EOL> ret . update ( comp ) <EOL> __jid_event__ . fire_event ( { '<STR_LIT:message>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> except SaltClientError as client_error : <EOL> print ( client_error ) <EOL> return ret <EOL> def start ( name ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> client = salt . client . get_local_client ( __opts__ [ '<STR_LIT>' ] ) <EOL> data = vm_info ( name , quiet = True ) <EOL> if not data : <EOL> __jid_event__ . fire_event ( { '<STR_LIT:message>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> host = next ( six . iterkeys ( data ) ) <EOL> if data [ host ] [ name ] [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> print ( '<STR_LIT>' . format ( name ) ) <EOL> return '<STR_LIT>' <EOL> try : <EOL> cmd_ret = client . cmd_iter ( <EOL> host , <EOL> '<STR_LIT>' , <EOL> [ name ] , <EOL> timeout = <NUM_LIT> ) <EOL> except SaltClientError as client_error : <EOL> return '<STR_LIT>' . format ( name , client_error ) <EOL> for comp in cmd_ret : <EOL> ret . update ( comp ) <EOL> __jid_event__ . fire_event ( { '<STR_LIT:message>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> def force_off ( name ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> client = salt . client . get_local_client ( __opts__ [ '<STR_LIT>' ] ) <EOL> data = vm_info ( name , quiet = True ) <EOL> if not data : <EOL> print ( '<STR_LIT>' . format ( name ) ) <EOL> return '<STR_LIT>' <EOL> host = next ( six . iterkeys ( data ) ) <EOL> if data [ host ] [ name ] [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> print ( '<STR_LIT>' . format ( name ) ) <EOL> return '<STR_LIT>' <EOL> try : <EOL> cmd_ret = client . cmd_iter ( <EOL> host , <EOL> '<STR_LIT>' , <EOL> [ name ] , <EOL> timeout = <NUM_LIT> ) <EOL> except SaltClientError as client_error : <EOL> return '<STR_LIT>' . format ( name , client_error ) <EOL> for comp in cmd_ret : <EOL> ret . update ( comp ) <EOL> __jid_event__ . fire_event ( { '<STR_LIT:message>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> def purge ( name , delete_key = True ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> client = salt . client . get_local_client ( __opts__ [ '<STR_LIT>' ] ) <EOL> data = vm_info ( name , quiet = True ) <EOL> if not data : <EOL> __jid_event__ . fire_event ( { '<STR_LIT:error>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> host = next ( six . iterkeys ( data ) ) <EOL> try : <EOL> cmd_ret = client . cmd_iter ( <EOL> host , <EOL> '<STR_LIT>' , <EOL> [ name , True ] , <EOL> timeout = <NUM_LIT> ) <EOL> except SaltClientError as client_error : <EOL> return '<STR_LIT>' . format ( name , client_error ) <EOL> for comp in cmd_ret : <EOL> ret . update ( comp ) <EOL> if delete_key : <EOL> log . debug ( '<STR_LIT>' . format ( name ) ) <EOL> skey = salt . key . Key ( __opts__ ) <EOL> skey . delete_key ( name ) <EOL> __jid_event__ . fire_event ( { '<STR_LIT:message>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> def pause ( name ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> client = salt . client . get_local_client ( __opts__ [ '<STR_LIT>' ] ) <EOL> data = vm_info ( name , quiet = True ) <EOL> if not data : <EOL> __jid_event__ . fire_event ( { '<STR_LIT:error>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> host = next ( six . iterkeys ( data ) ) <EOL> if data [ host ] [ name ] [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> __jid_event__ . fire_event ( { '<STR_LIT:error>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> try : <EOL> cmd_ret = client . cmd_iter ( <EOL> host , <EOL> '<STR_LIT>' , <EOL> [ name ] , <EOL> timeout = <NUM_LIT> ) <EOL> except SaltClientError as client_error : <EOL> return '<STR_LIT>' . format ( name , client_error ) <EOL> for comp in cmd_ret : <EOL> ret . update ( comp ) <EOL> __jid_event__ . fire_event ( { '<STR_LIT:message>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> def resume ( name ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> client = salt . client . get_local_client ( __opts__ [ '<STR_LIT>' ] ) <EOL> data = vm_info ( name , quiet = True ) <EOL> if not data : <EOL> __jid_event__ . fire_event ( { '<STR_LIT:error>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> host = next ( six . iterkeys ( data ) ) <EOL> if data [ host ] [ name ] [ '<STR_LIT:state>' ] != '<STR_LIT>' : <EOL> __jid_event__ . fire_event ( { '<STR_LIT:error>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> try : <EOL> cmd_ret = client . cmd_iter ( <EOL> host , <EOL> '<STR_LIT>' , <EOL> [ name ] , <EOL> timeout = <NUM_LIT> ) <EOL> except SaltClientError as client_error : <EOL> return '<STR_LIT>' . format ( name , client_error ) <EOL> for comp in cmd_ret : <EOL> ret . update ( comp ) <EOL> __jid_event__ . fire_event ( { '<STR_LIT:message>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> def migrate ( name , target = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> client = salt . client . get_local_client ( __opts__ [ '<STR_LIT>' ] ) <EOL> data = query ( quiet = True ) <EOL> origin_data = _find_vm ( name , data , quiet = True ) <EOL> try : <EOL> origin_host = list ( origin_data . keys ( ) ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> __jid_event__ . fire_event ( { '<STR_LIT:error>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> disks = origin_data [ origin_host ] [ name ] [ '<STR_LIT>' ] <EOL> if not origin_data : <EOL> __jid_event__ . fire_event ( { '<STR_LIT:error>' : '<STR_LIT>' . format ( name ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> if not target : <EOL> target = _determine_host ( data , origin_host ) <EOL> if target not in data : <EOL> __jid_event__ . fire_event ( { '<STR_LIT:error>' : '<STR_LIT>' . format ( origin_data ) } , '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> try : <EOL> client . cmd ( target , '<STR_LIT>' , [ disks , True ] ) <EOL> jid = client . cmd_async ( origin_host , <EOL> '<STR_LIT>' , <EOL> [ name , target ] ) <EOL> except SaltClientError as client_error : <EOL> return '<STR_LIT>' . format ( name , client_error ) <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( name , target , jid ) <EOL> __jid_event__ . fire_event ( { '<STR_LIT:message>' : msg } , '<STR_LIT>' ) </s>
