<s> """<STR_LIT>""" <EOL> try : <EOL> import anyjson <EOL> except ImportError : <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> class anyjson ( object ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def serialize ( buf ) : <EOL> return json . dumps ( buf ) <EOL> @ staticmethod <EOL> def deserialize ( buf ) : <EOL> return json . loads ( buf ) <EOL> from cStringIO import StringIO <EOL> import fcntl <EOL> import logging <EOL> import os <EOL> import shutil <EOL> import subprocess <EOL> import platform <EOL> import pyxenstore <EOL> import re <EOL> import socket <EOL> import time <EOL> from ctypes import * <EOL> import agentlib <EOL> import commands <EOL> import debian . network <EOL> import redhat . network <EOL> import arch . network <EOL> import suse . network <EOL> import gentoo . network <EOL> import freebsd . network <EOL> import utils <EOL> XENSTORE_INTERFACE_PATH = "<STR_LIT>" <EOL> XENSTORE_HOSTNAME_PATH = "<STR_LIT>" <EOL> DEFAULT_HOSTNAME = '<STR_LIT>' <EOL> HOSTS_FILE = '<STR_LIT>' <EOL> RESOLV_CONF_FILE = '<STR_LIT>' <EOL> RESOLVCONF_RESOLV_CONF_FILE = "<STR_LIT>" <EOL> RESOLVCONF_CONF_FILE = "<STR_LIT>" <EOL> RESOLVCONF_BASE = '<STR_LIT>' <EOL> if os . uname ( ) [ <NUM_LIT:0> ] . lower ( ) == '<STR_LIT>' : <EOL> INTERFACE_LABELS = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> else : <EOL> INTERFACE_LABELS = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> NETMASK_TO_PREFIXLEN = { <EOL> '<STR_LIT>' : <NUM_LIT:32> , '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:30> , '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:20> , '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:16> , '<STR_LIT>' : <NUM_LIT:15> , <EOL> '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:12> , '<STR_LIT>' : <NUM_LIT:11> , <EOL> '<STR_LIT>' : <NUM_LIT:10> , '<STR_LIT>' : <NUM_LIT:9> , <EOL> '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT:7> , <EOL> '<STR_LIT>' : <NUM_LIT:6> , '<STR_LIT>' : <NUM_LIT:5> , <EOL> '<STR_LIT>' : <NUM_LIT:4> , '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> } <EOL> class CustomErrorResolvConfBase ( Exception ) : <EOL> def __init__ ( self , message ) : <EOL> self . message = repr ( message ) <EOL> logging . error ( "<STR_LIT>" % self . message ) <EOL> def __str__ ( self ) : <EOL> return self . message <EOL> class NetworkCommands ( commands . CommandBase ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> pass <EOL> @ staticmethod <EOL> def detect_os ( ) : <EOL> """<STR_LIT>""" <EOL> translations = { "<STR_LIT>" : debian , <EOL> "<STR_LIT>" : debian , <EOL> "<STR_LIT>" : redhat , <EOL> "<STR_LIT>" : redhat , <EOL> "<STR_LIT>" : redhat , <EOL> "<STR_LIT>" : redhat , <EOL> "<STR_LIT>" : arch , <EOL> "<STR_LIT>" : suse , <EOL> "<STR_LIT>" : suse , <EOL> "<STR_LIT>" : gentoo , <EOL> "<STR_LIT>" : freebsd } <EOL> system = os . uname ( ) [ <NUM_LIT:0> ] <EOL> if system == "<STR_LIT>" : <EOL> system = platform . linux_distribution ( full_distribution_name = <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> if not system and os . path . exists ( '<STR_LIT>' ) : <EOL> system = '<STR_LIT>' <EOL> if not system : <EOL> return None <EOL> system = system . lower ( ) <EOL> global DEFAULT_HOSTNAME <EOL> DEFAULT_HOSTNAME = system <EOL> return translations . get ( system ) <EOL> @ commands . command_add ( '<STR_LIT>' ) <EOL> def resetnetwork_cmd ( self , data ) : <EOL> """<STR_LIT>""" <EOL> os_mod = self . detect_os ( ) <EOL> if not os_mod : <EOL> raise SystemError ( "<STR_LIT>" ) <EOL> xs_handle = pyxenstore . Handle ( ) <EOL> try : <EOL> hostname = xs_handle . read ( XENSTORE_HOSTNAME_PATH ) <EOL> logging . info ( '<STR_LIT>' % hostname ) <EOL> except pyxenstore . NotFoundError : <EOL> try : <EOL> hostname = os_mod . network . get_hostname ( ) <EOL> except : <EOL> hostname = _get_hostname ( ) <EOL> if not hostname : <EOL> hostname = DEFAULT_HOSTNAME <EOL> logging . info ( '<STR_LIT>' % hostname ) <EOL> interfaces = [ ] <EOL> try : <EOL> entries = xs_handle . entries ( XENSTORE_INTERFACE_PATH ) <EOL> except pyxenstore . NotFoundError : <EOL> entries = [ ] <EOL> for entry in entries : <EOL> data = xs_handle . read ( XENSTORE_INTERFACE_PATH + '<STR_LIT:/>' + entry ) <EOL> data = anyjson . deserialize ( data ) <EOL> interfaces . append ( data ) <EOL> logging . info ( '<STR_LIT>' % ( entry , data ) ) <EOL> del xs_handle <EOL> by_macaddr = dict ( [ ( mac , ( up , name ) ) <EOL> for name , up , mac in agentlib . get_interfaces ( ) ] ) <EOL> config = { } <EOL> for interface in interfaces : <EOL> ifconfig = { } <EOL> mac = interface . get ( '<STR_LIT>' ) <EOL> if not mac : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> mac = mac . lower ( ) <EOL> ifconfig [ '<STR_LIT>' ] = mac <EOL> if mac not in by_macaddr : <EOL> raise RuntimeError ( '<STR_LIT>' % mac ) <EOL> ifconfig [ '<STR_LIT:label>' ] = interface . get ( '<STR_LIT:label>' ) <EOL> up , ifname = by_macaddr [ mac ] <EOL> ifconfig [ '<STR_LIT>' ] = up <EOL> ip4s = interface . get ( '<STR_LIT>' , [ ] ) <EOL> ip6s = interface . get ( '<STR_LIT>' , [ ] ) <EOL> if not ip4s and not ip6s : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> gateway4 = interface . get ( '<STR_LIT>' ) <EOL> gateway6 = interface . get ( '<STR_LIT>' ) <EOL> for ip in ip4s + ip6s : <EOL> try : <EOL> ip [ '<STR_LIT>' ] = int ( ip . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> except ValueError : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ip . get ( '<STR_LIT>' ) ) <EOL> ip4s = filter ( lambda i : i [ '<STR_LIT>' ] , ip4s ) <EOL> ip6s = filter ( lambda i : i [ '<STR_LIT>' ] , ip6s ) <EOL> for ip in ip4s : <EOL> if '<STR_LIT>' not in ip : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' not in ip : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> ip [ '<STR_LIT:address>' ] = ip . pop ( '<STR_LIT>' ) <EOL> ip [ '<STR_LIT>' ] = NETMASK_TO_PREFIXLEN [ ip [ '<STR_LIT>' ] ] <EOL> for ip in ip6s : <EOL> if '<STR_LIT>' not in ip and '<STR_LIT:address>' not in ip : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' not in ip : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' in ip : <EOL> gateway6 = ip . pop ( '<STR_LIT>' ) <EOL> if '<STR_LIT:address>' not in ip : <EOL> ip [ '<STR_LIT:address>' ] = ip . pop ( '<STR_LIT>' ) <EOL> ip [ '<STR_LIT>' ] = ip . pop ( '<STR_LIT>' ) <EOL> ifconfig [ '<STR_LIT>' ] = ip4s <EOL> ifconfig [ '<STR_LIT>' ] = ip6s <EOL> ifconfig [ '<STR_LIT>' ] = gateway4 <EOL> ifconfig [ '<STR_LIT>' ] = gateway6 <EOL> routes = interface . get ( '<STR_LIT>' , [ ] ) <EOL> for route in routes : <EOL> if '<STR_LIT>' not in route : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' not in route : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' not in route : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> route [ '<STR_LIT>' ] = route . pop ( '<STR_LIT>' ) <EOL> route [ '<STR_LIT>' ] = NETMASK_TO_PREFIXLEN [ route [ '<STR_LIT>' ] ] <EOL> ifconfig [ '<STR_LIT>' ] = routes <EOL> ifconfig [ '<STR_LIT>' ] = interface . get ( '<STR_LIT>' , [ ] ) <EOL> config [ ifname ] = ifconfig <EOL> return os_mod . network . configure_network ( hostname , config ) <EOL> def _get_etc_hosts ( infile , interfaces , hostname ) : <EOL> ips = set ( ) <EOL> for interface in interfaces . itervalues ( ) : <EOL> ip4s = interface [ '<STR_LIT>' ] <EOL> if ip4s : <EOL> ips . add ( ip4s [ <NUM_LIT:0> ] [ '<STR_LIT:address>' ] ) <EOL> ip6s = interface [ '<STR_LIT>' ] <EOL> if ip6s : <EOL> ips . add ( ip6s [ <NUM_LIT:0> ] [ '<STR_LIT:address>' ] ) <EOL> outfile = StringIO ( ) <EOL> for line in infile : <EOL> line = line . strip ( ) <EOL> if '<STR_LIT:#>' in line : <EOL> config , comment = line . split ( '<STR_LIT:#>' , <NUM_LIT:1> ) <EOL> config = config . strip ( ) <EOL> comment = '<STR_LIT>' + comment <EOL> else : <EOL> config , comment = line , '<STR_LIT>' <EOL> parts = re . split ( '<STR_LIT>' , config ) <EOL> if parts : <EOL> if parts [ <NUM_LIT:0> ] in ips : <EOL> confip = parts . pop ( <NUM_LIT:0> ) <EOL> if len ( parts ) == <NUM_LIT:1> and parts [ <NUM_LIT:0> ] != hostname : <EOL> print >> outfile , '<STR_LIT>' % line <EOL> print >> outfile , '<STR_LIT>' % ( confip , hostname , comment ) <EOL> elif len ( parts ) == <NUM_LIT:2> and len ( <EOL> filter ( lambda h : '<STR_LIT:.>' in h , parts ) ) == <NUM_LIT:1> : <EOL> hostnames = map ( <EOL> lambda h : ( '<STR_LIT:.>' in h ) and h or hostname , parts ) <EOL> print >> outfile , '<STR_LIT>' % line <EOL> print >> outfile , '<STR_LIT>' % ( confip , <EOL> '<STR_LIT:U+0020>' . join ( hostnames ) , comment ) <EOL> else : <EOL> print >> outfile , line <EOL> ips . remove ( confip ) <EOL> else : <EOL> print >> outfile , line <EOL> else : <EOL> print >> outfile , line <EOL> for ip in ips : <EOL> print >> outfile , '<STR_LIT>' % ( ip , hostname ) <EOL> outfile . seek ( <NUM_LIT:0> ) <EOL> return outfile . read ( ) <EOL> def get_etc_hosts ( interfaces , hostname ) : <EOL> if os . path . exists ( HOSTS_FILE ) : <EOL> infile = open ( HOSTS_FILE ) <EOL> else : <EOL> infile = StringIO ( ) <EOL> return HOSTS_FILE , _get_etc_hosts ( infile , interfaces , hostname ) <EOL> def get_gateways ( interfaces ) : <EOL> gateway4s = [ ] <EOL> gateway6s = [ ] <EOL> for interface in interfaces . itervalues ( ) : <EOL> gateway = interface . get ( '<STR_LIT>' ) <EOL> if gateway : <EOL> gateway4s . append ( gateway ) <EOL> gateway = interface . get ( '<STR_LIT>' ) <EOL> if gateway : <EOL> gateway6s . append ( gateway ) <EOL> if len ( gateway4s ) > <NUM_LIT:1> : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if len ( gateway6s ) > <NUM_LIT:1> : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> gateway4 = gateway4s and gateway4s [ <NUM_LIT:0> ] or None <EOL> gateway6 = gateway6s and gateway6s [ <NUM_LIT:0> ] or None <EOL> return gateway4 , gateway6 <EOL> def get_nameservers ( interfaces ) : <EOL> for interface in interfaces . itervalues ( ) : <EOL> for nameserver in interface [ '<STR_LIT>' ] : <EOL> yield nameserver <EOL> def get_resolv_conf ( interfaces ) : <EOL> resolv_data = '<STR_LIT>' <EOL> for nameserver in get_nameservers ( interfaces ) : <EOL> resolv_data += '<STR_LIT>' % nameserver <EOL> if not resolv_data : <EOL> return None , None <EOL> return RESOLV_CONF_FILE , '<STR_LIT>' + resolv_data <EOL> def sethostname ( hostname ) : <EOL> agentlib . sethostname ( hostname ) <EOL> def stage_files ( update_files ) : <EOL> tmp_suffix = '<STR_LIT>' % os . getpid ( ) <EOL> for filepath , data in update_files . items ( ) : <EOL> if os . path . exists ( filepath ) : <EOL> if data == open ( filepath ) . read ( ) : <EOL> logging . info ( "<STR_LIT>" % filepath ) <EOL> del update_files [ filepath ] <EOL> continue <EOL> logging . info ( "<STR_LIT>" % ( filepath , tmp_suffix ) ) <EOL> tmp_file = '<STR_LIT>' % ( filepath , tmp_suffix ) <EOL> f = open ( tmp_file , '<STR_LIT:w>' ) <EOL> try : <EOL> f . write ( data ) <EOL> f . close ( ) <EOL> os . chown ( tmp_file , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> os . chmod ( tmp_file , <NUM_LIT:0> <NUM_LIT> ) <EOL> except : <EOL> os . unlink ( tmp_file ) <EOL> raise <EOL> def move_files ( update_files , remove_files = None ) : <EOL> if not remove_files : <EOL> remove_files = set ( ) <EOL> tmp_suffix = '<STR_LIT>' % os . getpid ( ) <EOL> bak_suffix = '<STR_LIT>' % time . time ( ) <EOL> for filepath in update_files . iterkeys ( ) : <EOL> if os . path . exists ( filepath ) : <EOL> logging . info ( "<STR_LIT>" % ( filepath , bak_suffix ) ) <EOL> os . rename ( filepath , '<STR_LIT>' % ( filepath , bak_suffix ) ) <EOL> logging . info ( "<STR_LIT>" % filepath ) <EOL> try : <EOL> os . rename ( '<STR_LIT>' % ( filepath , tmp_suffix ) , filepath ) <EOL> except : <EOL> os . rename ( '<STR_LIT>' % ( filepath , bak_suffix ) , filepath ) <EOL> raise <EOL> for filepath in remove_files : <EOL> logging . info ( "<STR_LIT>" % ( filepath , bak_suffix ) ) <EOL> os . rename ( filepath , '<STR_LIT>' % ( filepath , bak_suffix ) ) <EOL> def update_files ( update_files , remove_files = None ) : <EOL> stage_files ( update_files ) <EOL> move_files ( update_files , remove_files ) <EOL> def _mkdir_p ( pathname ) : <EOL> if not os . path . exists ( pathname ) : <EOL> basedir , _ = os . path . split ( pathname ) <EOL> if not os . path . exists ( basedir ) : <EOL> _mkdir_p ( basedir ) <EOL> logging . info ( "<STR_LIT>" % pathname ) <EOL> os . mkdir ( pathname ) <EOL> def _update_resolvconf_base ( nameservers ) : <EOL> if nameservers != None : <EOL> nameservers = [ "<STR_LIT>" % dns for dns in nameservers ] <EOL> elif os . path . isfile ( '<STR_LIT>' ) : <EOL> fyl = open ( '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> dat = fyl . readlines ( ) <EOL> fyl . close ( ) <EOL> nameservers = [ ] <EOL> for line in dat : <EOL> entry = re . findall ( r'<STR_LIT>' , line ) <EOL> if entry != [ ] : <EOL> nameservers . extend ( entry [ <NUM_LIT:0> ] . split ( ) [ <NUM_LIT:1> : ] ) <EOL> nameservers = list ( set ( nameservers ) ) <EOL> nameservers = [ "<STR_LIT>" % dns for dns in nameservers ] <EOL> else : <EOL> raise CustomErrorResolvConfBase ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> basedir , _ = os . path . split ( RESOLVCONF_BASE ) <EOL> _mkdir_p ( basedir ) <EOL> fyl = open ( RESOLVCONF_BASE , '<STR_LIT:w>' ) <EOL> fyl . write ( "<STR_LIT:\n>" . join ( nameservers ) ) <EOL> fyl . close ( ) <EOL> logging . info ( "<STR_LIT>" % nameservers ) <EOL> def _prepare_resolvconf_config ( nameservers ) : <EOL> if not os . path . isdir ( "<STR_LIT>" ) : <EOL> if not os . path . isdir ( "<STR_LIT>" ) : <EOL> os . mkdir ( "<STR_LIT>" ) <EOL> os . mkdir ( "<STR_LIT>" ) <EOL> os . rename ( RESOLV_CONF_FILE , RESOLVCONF_CONF_FILE ) <EOL> open ( RESOLVCONF_RESOLV_CONF_FILE , '<STR_LIT:a>' ) . close ( ) <EOL> os . symlink ( RESOLVCONF_RESOLV_CONF_FILE , RESOLV_CONF_FILE ) <EOL> _update_resolvconf_base ( nameservers ) <EOL> logging . info ( "<STR_LIT>" ) <EOL> def _update_if_resolvconf_in_path ( ) : <EOL> for path in os . getenv ( '<STR_LIT>' ) . split ( '<STR_LIT::>' ) : <EOL> resolvconf_path = os . path . join ( path , '<STR_LIT>' ) <EOL> if os . path . isfile ( resolvconf_path ) and os . path . isfile ( RESOLVCONF_BASE ) : <EOL> shutil . copy ( RESOLVCONF_BASE , RESOLVCONF_RESOLV_CONF_FILE ) <EOL> logging . info ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return True <EOL> return False <EOL> def update_resolvconf ( nameservers = None ) : <EOL> try : <EOL> if os . getenv ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> logging . info ( "<STR_LIT>" ) <EOL> if os . path . islink ( RESOLV_CONF_FILE ) : <EOL> logging . info ( "<STR_LIT>" % RESOLV_CONF_FILE ) <EOL> else : <EOL> _prepare_resolvconf_config ( nameservers ) <EOL> if subprocess . call ( [ "<STR_LIT>" , "<STR_LIT>" ] ) == <NUM_LIT:0> : <EOL> logging . info ( "<STR_LIT>" ) <EOL> return True <EOL> return _update_if_resolvconf_in_path ( ) <EOL> except Exception , e : <EOL> logging . info ( "<STR_LIT>" % repr ( e ) ) <EOL> os . rename ( RESOLV_CONF_FILE , RESOLVCONF_CONF_FILE ) <EOL> return False <EOL> def _get_hostname ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> with open ( "<STR_LIT>" ) as fyl : <EOL> _hostname = fyl . read ( ) . strip ( ) <EOL> if _hostname != "<STR_LIT>" : <EOL> return _hostname <EOL> return None <EOL> except Exception , e : <EOL> logging . info ( "<STR_LIT>" % str ( e ) ) <EOL> return None </s>
