<s> """<STR_LIT>""" <EOL> import textwrap <EOL> import itertools <EOL> from ganeti . cli import * <EOL> from ganeti import constants <EOL> from ganeti import opcodes <EOL> from ganeti import utils <EOL> from ganeti import errors <EOL> from ganeti import objects <EOL> _LIST_DEF_FIELDS = [ "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def _HandleReservedIPs ( ips ) : <EOL> if ips is None : <EOL> return None <EOL> elif not ips : <EOL> return [ ] <EOL> else : <EOL> return utils . UnescapeAndSplit ( ips , sep = "<STR_LIT:U+002C>" ) <EOL> def AddNetwork ( opts , args ) : <EOL> """<STR_LIT>""" <EOL> ( network_name , ) = args <EOL> if opts . network is None : <EOL> raise errors . OpPrereqError ( "<STR_LIT>" , <EOL> errors . ECODE_INVAL ) <EOL> if opts . tags is not None : <EOL> tags = opts . tags . split ( "<STR_LIT:U+002C>" ) <EOL> else : <EOL> tags = [ ] <EOL> reserved_ips = _HandleReservedIPs ( opts . add_reserved_ips ) <EOL> op = opcodes . OpNetworkAdd ( network_name = network_name , <EOL> gateway = opts . gateway , <EOL> network = opts . network , <EOL> gateway6 = opts . gateway6 , <EOL> network6 = opts . network6 , <EOL> mac_prefix = opts . mac_prefix , <EOL> add_reserved_ips = reserved_ips , <EOL> conflicts_check = opts . conflicts_check , <EOL> tags = tags ) <EOL> SubmitOrSend ( op , opts ) <EOL> def _GetDefaultGroups ( cl , groups ) : <EOL> """<STR_LIT>""" <EOL> if groups : <EOL> return groups <EOL> return list ( itertools . chain ( * cl . QueryGroups ( [ ] , [ "<STR_LIT>" ] , False ) ) ) <EOL> def ConnectNetwork ( opts , args ) : <EOL> """<STR_LIT>""" <EOL> cl = GetClient ( ) <EOL> network = args [ <NUM_LIT:0> ] <EOL> nicparams = objects . FillDict ( constants . NICC_DEFAULTS , opts . nicparams ) <EOL> groups = _GetDefaultGroups ( cl , args [ <NUM_LIT:1> : ] ) <EOL> for group in groups : <EOL> op = opcodes . OpNetworkConnect ( group_name = group , <EOL> network_name = network , <EOL> network_mode = nicparams [ constants . NIC_MODE ] , <EOL> network_link = nicparams [ constants . NIC_LINK ] , <EOL> network_vlan = nicparams [ constants . NIC_VLAN ] , <EOL> conflicts_check = opts . conflicts_check ) <EOL> SubmitOpCode ( op , opts = opts , cl = cl ) <EOL> def DisconnectNetwork ( opts , args ) : <EOL> """<STR_LIT>""" <EOL> cl = GetClient ( ) <EOL> ( network , ) = args [ : <NUM_LIT:1> ] <EOL> groups = _GetDefaultGroups ( cl , args [ <NUM_LIT:1> : ] ) <EOL> for group in groups : <EOL> op = opcodes . OpNetworkDisconnect ( group_name = group , <EOL> network_name = network ) <EOL> SubmitOpCode ( op , opts = opts , cl = cl ) <EOL> def ListNetworks ( opts , args ) : <EOL> """<STR_LIT>""" <EOL> desired_fields = ParseFields ( opts . output , _LIST_DEF_FIELDS ) <EOL> fmtoverride = { <EOL> "<STR_LIT>" : <EOL> ( lambda data : <EOL> utils . CommaJoin ( "<STR_LIT>" % ( name , mode , link , vlan ) <EOL> for ( name , mode , link , vlan ) in data ) , <EOL> False ) , <EOL> "<STR_LIT>" : ( "<STR_LIT:U+002C>" . join , False ) , <EOL> "<STR_LIT>" : ( "<STR_LIT:U+002C>" . join , False ) , <EOL> } <EOL> cl = GetClient ( ) <EOL> return GenericList ( constants . QR_NETWORK , desired_fields , args , None , <EOL> opts . separator , not opts . no_headers , <EOL> verbose = opts . verbose , format_override = fmtoverride , <EOL> cl = cl ) <EOL> def ListNetworkFields ( opts , args ) : <EOL> """<STR_LIT>""" <EOL> cl = GetClient ( ) <EOL> return GenericListFields ( constants . QR_NETWORK , args , opts . separator , <EOL> not opts . no_headers , cl = cl ) <EOL> def ShowNetworkConfig ( _ , args ) : <EOL> """<STR_LIT>""" <EOL> cl = GetClient ( ) <EOL> result = cl . QueryNetworks ( fields = [ "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] , <EOL> names = args , use_locking = False ) <EOL> for ( name , network , gateway , network6 , gateway6 , <EOL> mac_prefix , free_count , reserved_count , <EOL> mapping , group_list , instances , ext_res , serial , uuid ) in result : <EOL> size = free_count + reserved_count <EOL> ToStdout ( "<STR_LIT>" , name ) <EOL> ToStdout ( "<STR_LIT>" , uuid ) <EOL> ToStdout ( "<STR_LIT>" , serial ) <EOL> ToStdout ( "<STR_LIT>" , network ) <EOL> ToStdout ( "<STR_LIT>" , gateway ) <EOL> ToStdout ( "<STR_LIT>" , network6 ) <EOL> ToStdout ( "<STR_LIT>" , gateway6 ) <EOL> ToStdout ( "<STR_LIT>" , mac_prefix ) <EOL> ToStdout ( "<STR_LIT>" , size ) <EOL> ToStdout ( "<STR_LIT>" , free_count , <EOL> <NUM_LIT:100> * float ( free_count ) / float ( size ) ) <EOL> ToStdout ( "<STR_LIT>" ) <EOL> lenmapping = len ( mapping ) <EOL> idx = <NUM_LIT:0> <EOL> while idx < lenmapping : <EOL> line = mapping [ idx : idx + <NUM_LIT:64> ] <EOL> ToStdout ( "<STR_LIT>" , str ( idx ) . rjust ( <NUM_LIT:4> ) , line . ljust ( <NUM_LIT:64> ) , idx + <NUM_LIT> ) <EOL> idx += <NUM_LIT:64> <EOL> ToStdout ( "<STR_LIT>" ) <EOL> if ext_res : <EOL> ToStdout ( "<STR_LIT>" ) <EOL> for line in textwrap . wrap ( ext_res , width = <NUM_LIT:64> ) : <EOL> ToStdout ( "<STR_LIT>" % line ) <EOL> if group_list : <EOL> ToStdout ( "<STR_LIT>" ) <EOL> for group , nic_mode , nic_link , nic_vlan in group_list : <EOL> ToStdout ( "<STR_LIT>" , <EOL> group , nic_mode , nic_link , nic_vlan ) <EOL> else : <EOL> ToStdout ( "<STR_LIT>" ) <EOL> if instances : <EOL> ToStdout ( "<STR_LIT>" , len ( instances ) ) <EOL> for name in instances : <EOL> ( ( ips , networks ) , ) = cl . QueryInstances ( [ name ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> use_locking = False ) <EOL> l = lambda value : "<STR_LIT:U+002CU+0020>" . join ( str ( idx ) + "<STR_LIT::>" + str ( ip ) <EOL> for idx , ( ip , net ) in enumerate ( value ) <EOL> if net == uuid ) <EOL> ToStdout ( "<STR_LIT>" , name , l ( zip ( ips , networks ) ) ) <EOL> else : <EOL> ToStdout ( "<STR_LIT>" ) <EOL> def SetNetworkParams ( opts , args ) : <EOL> """<STR_LIT>""" <EOL> all_changes = { <EOL> "<STR_LIT>" : opts . gateway , <EOL> "<STR_LIT>" : _HandleReservedIPs ( opts . add_reserved_ips ) , <EOL> "<STR_LIT>" : _HandleReservedIPs ( opts . remove_reserved_ips ) , <EOL> "<STR_LIT>" : opts . mac_prefix , <EOL> "<STR_LIT>" : opts . gateway6 , <EOL> "<STR_LIT>" : opts . network6 , <EOL> } <EOL> if all_changes . values ( ) . count ( None ) == len ( all_changes ) : <EOL> ToStderr ( "<STR_LIT>" ) <EOL> return <NUM_LIT:1> <EOL> op = opcodes . OpNetworkSetParams ( network_name = args [ <NUM_LIT:0> ] , ** all_changes ) <EOL> SubmitOrSend ( op , opts ) <EOL> def RemoveNetwork ( opts , args ) : <EOL> """<STR_LIT>""" <EOL> ( network_name , ) = args <EOL> op = opcodes . OpNetworkRemove ( network_name = network_name , force = opts . force ) <EOL> SubmitOrSend ( op , opts ) <EOL> commands = { <EOL> "<STR_LIT>" : ( <EOL> AddNetwork , ARGS_ONE_NETWORK , <EOL> [ DRY_RUN_OPT , NETWORK_OPT , GATEWAY_OPT , ADD_RESERVED_IPS_OPT , <EOL> MAC_PREFIX_OPT , NETWORK6_OPT , GATEWAY6_OPT , <EOL> NOCONFLICTSCHECK_OPT , TAG_ADD_OPT , PRIORITY_OPT ] + SUBMIT_OPTS , <EOL> "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT:list>" : ( <EOL> ListNetworks , ARGS_MANY_NETWORKS , <EOL> [ NOHDR_OPT , SEP_OPT , FIELDS_OPT , VERBOSE_OPT ] , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % utils . CommaJoin ( _LIST_DEF_FIELDS ) ) , <EOL> "<STR_LIT>" : ( <EOL> ListNetworkFields , [ ArgUnknown ( ) ] , [ NOHDR_OPT , SEP_OPT ] , "<STR_LIT>" , <EOL> "<STR_LIT>" ) , <EOL> "<STR_LIT:info>" : ( <EOL> ShowNetworkConfig , ARGS_MANY_NETWORKS , [ ] , <EOL> "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( <EOL> SetNetworkParams , ARGS_ONE_NETWORK , <EOL> [ DRY_RUN_OPT ] + SUBMIT_OPTS + <EOL> [ ADD_RESERVED_IPS_OPT , <EOL> REMOVE_RESERVED_IPS_OPT , GATEWAY_OPT , MAC_PREFIX_OPT , NETWORK6_OPT , <EOL> GATEWAY6_OPT , PRIORITY_OPT ] , <EOL> "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( <EOL> ConnectNetwork , <EOL> [ ArgNetwork ( min = <NUM_LIT:1> , max = <NUM_LIT:1> ) , <EOL> ArgGroup ( ) ] , <EOL> [ NOCONFLICTSCHECK_OPT , PRIORITY_OPT , NIC_PARAMS_OPT ] , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( <EOL> DisconnectNetwork , <EOL> [ ArgNetwork ( min = <NUM_LIT:1> , max = <NUM_LIT:1> ) , ArgGroup ( ) ] , <EOL> [ PRIORITY_OPT ] , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( <EOL> RemoveNetwork , ARGS_ONE_NETWORK , <EOL> [ FORCE_OPT , DRY_RUN_OPT ] + SUBMIT_OPTS + [ PRIORITY_OPT ] , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( <EOL> ListTags , ARGS_ONE_NETWORK , [ ] , <EOL> "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( <EOL> AddTags , [ ArgNetwork ( min = <NUM_LIT:1> , max = <NUM_LIT:1> ) , ArgUnknown ( ) ] , <EOL> [ TAG_SRC_OPT , PRIORITY_OPT ] + SUBMIT_OPTS , <EOL> "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( <EOL> RemoveTags , [ ArgNetwork ( min = <NUM_LIT:1> , max = <NUM_LIT:1> ) , ArgUnknown ( ) ] , <EOL> [ TAG_SRC_OPT , PRIORITY_OPT ] + SUBMIT_OPTS , <EOL> "<STR_LIT>" , "<STR_LIT>" ) , <EOL> } <EOL> def Main ( ) : <EOL> return GenericMain ( commands , override = { "<STR_LIT>" : constants . TAG_NETWORK } ) </s>
