<s> import collections <EOL> from hm import managers , log , model , config <EOL> class Host ( model . BaseModel ) : <EOL> def __init__ ( self , id , dns_name , conf = None , alternative_id = <NUM_LIT:0> , ** kwargs ) : <EOL> self . id = id <EOL> self . dns_name = dns_name <EOL> self . manager = None <EOL> self . extra_args = set ( ) <EOL> self . config = conf <EOL> self . alternative_id = alternative_id <EOL> for k , v in kwargs . items ( ) : <EOL> self . extra_args . add ( k ) <EOL> setattr ( self , k , v ) <EOL> def to_json ( self ) : <EOL> obj = { <EOL> '<STR_LIT>' : self . id , <EOL> '<STR_LIT>' : self . dns_name , <EOL> '<STR_LIT>' : self . manager , <EOL> '<STR_LIT>' : self . alternative_id , <EOL> '<STR_LIT>' : getattr ( self , '<STR_LIT>' , None ) , <EOL> } <EOL> for key in self . extra_args : <EOL> obj [ key ] = getattr ( self , key ) <EOL> return obj <EOL> @ classmethod <EOL> def from_dict ( cls , dict , conf = None ) : <EOL> if dict is None : <EOL> return None <EOL> dict [ '<STR_LIT:id>' ] = dict [ '<STR_LIT>' ] <EOL> del dict [ '<STR_LIT>' ] <EOL> dict [ '<STR_LIT>' ] = conf <EOL> return Host ( ** dict ) <EOL> @ classmethod <EOL> def create ( cls , manager_name , group , conf = None ) : <EOL> manager = managers . by_name ( manager_name , conf ) <EOL> alternative_id = cls . _current_group_alternate ( group , conf ) <EOL> host = manager . create_host ( name = group , alternative_id = alternative_id ) <EOL> host . manager = manager_name <EOL> host . group = group <EOL> host . config = conf <EOL> model . storage ( conf ) . store_host ( host ) <EOL> return host <EOL> @ classmethod <EOL> def find ( cls , id , conf = None ) : <EOL> return model . storage ( conf ) . find_host ( id ) <EOL> @ classmethod <EOL> def list ( cls , filters = None , conf = None ) : <EOL> return model . storage ( conf ) . list_hosts ( filters ) <EOL> def destroy ( self ) : <EOL> manager = managers . by_name ( self . manager , self . config ) <EOL> try : <EOL> manager . destroy_host ( self . id ) <EOL> except Exception as e : <EOL> log . error ( "<STR_LIT>" . format ( self . id , self . manager , e ) ) <EOL> self . storage ( ) . remove_host ( self . id ) <EOL> def restore ( self ) : <EOL> manager = managers . by_name ( self . manager , self . config ) <EOL> try : <EOL> manager . restore_host ( self . id ) <EOL> except Exception as e : <EOL> log . error ( "<STR_LIT>" . format ( self . id , self . manager , e ) ) <EOL> raise e <EOL> @ classmethod <EOL> def _current_group_alternate ( cls , group , conf = None ) : <EOL> alternates_count = int ( config . get_config ( "<STR_LIT>" , <NUM_LIT:1> , conf ) ) <EOL> hosts = model . storage ( conf ) . list_hosts ( { '<STR_LIT>' : group } ) <EOL> alterantives_map = collections . defaultdict ( int ) <EOL> for host in hosts : <EOL> alterantives_map [ host . alternative_id ] += <NUM_LIT:1> <EOL> min_alt_id = <NUM_LIT:0> <EOL> min_alt_count = None <EOL> for i in xrange ( alternates_count ) : <EOL> if min_alt_count is None or alterantives_map [ i ] < min_alt_count : <EOL> min_alt_id = i <EOL> min_alt_count = alterantives_map [ i ] <EOL> return min_alt_id </s>
