<s> '''<STR_LIT>''' <EOL> import os <EOL> import logging <EOL> from jinja2 import Environment , PackageLoader <EOL> from netaddr import IPNetwork <EOL> import util <EOL> from model import Pod <EOL> from dao import Dao <EOL> from writer import DhcpConfWriter <EOL> from propLoader import OpenClosProperty , loadLoggingConfig <EOL> from sqlalchemy . orm import exc <EOL> from exception import PodNotFound <EOL> moduleName = '<STR_LIT>' <EOL> loadLoggingConfig ( appName = moduleName ) <EOL> logger = logging . getLogger ( moduleName ) <EOL> ztpTemplateLocation = os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class ZtpServer ( ) : <EOL> def __init__ ( self , conf = { } , templateEnv = None , daoClass = Dao ) : <EOL> if any ( conf ) == False : <EOL> self . __conf = OpenClosProperty ( appName = moduleName ) . getProperties ( ) <EOL> else : <EOL> self . __conf = conf <EOL> self . _dao = daoClass . getInstance ( ) <EOL> if templateEnv is None : <EOL> self . templateEnv = Environment ( loader = PackageLoader ( '<STR_LIT>' , ztpTemplateLocation ) ) <EOL> self . templateEnv . lstrip_blocks = True <EOL> self . templateEnv . trim_blocks = True <EOL> def dcpServerReloadConfig ( self ) : <EOL> pass <EOL> '''<STR_LIT>''' <EOL> def generateSingleDhcpConf ( self , session ) : <EOL> if util . isPlatformUbuntu ( ) : <EOL> ztp = self . populateDhcpGlobalSettings ( ) <EOL> dhcpTemplate = self . templateEnv . get_template ( '<STR_LIT>' ) <EOL> return dhcpTemplate . render ( ztp = self . populateDhcpDeviceSpecificSettingForAllPods ( session , ztp ) ) <EOL> def createPodSpecificDhcpConfFile ( self , session , podId ) : <EOL> if podId is not None : <EOL> try : <EOL> pod = self . _dao . getObjectById ( session , Pod , podId ) <EOL> except ( exc . NoResultFound ) : <EOL> raise PodNotFound ( "<STR_LIT>" % ( podId ) ) <EOL> confWriter = DhcpConfWriter ( self . __conf , pod , self . _dao ) <EOL> confWriter . write ( self . generatePodSpecificDhcpConf ( session , pod . id ) ) <EOL> else : <EOL> raise PodNotFound ( "<STR_LIT>" ) <EOL> def getTemplate ( self ) : <EOL> '''<STR_LIT>''' <EOL> if util . isPlatformUbuntu ( ) : <EOL> if util . isZtpStaged ( self . __conf ) : <EOL> return self . templateEnv . get_template ( '<STR_LIT>' ) <EOL> else : <EOL> return self . templateEnv . get_template ( '<STR_LIT>' ) <EOL> elif util . isPlatformCentos ( ) : <EOL> if util . isZtpStaged ( self . __conf ) : <EOL> return self . templateEnv . get_template ( '<STR_LIT>' ) <EOL> else : <EOL> return self . templateEnv . get_template ( '<STR_LIT>' ) <EOL> elif util . isPlatformWindows ( ) : <EOL> '''<STR_LIT>''' <EOL> if util . isZtpStaged ( self . __conf ) : <EOL> return self . templateEnv . get_template ( '<STR_LIT>' ) <EOL> else : <EOL> return self . templateEnv . get_template ( '<STR_LIT>' ) <EOL> def generatePodSpecificDhcpConf ( self , session , podId ) : <EOL> ztp = self . populateDhcpGlobalSettings ( ) <EOL> dhcpTemplate = self . getTemplate ( ) <EOL> ztp = self . populateDhcpDeviceSpecificSetting ( session , podId , ztp ) <EOL> conf = dhcpTemplate . render ( ztp = ztp ) <EOL> return conf <EOL> def populateDhcpGlobalSettings ( self ) : <EOL> ztp = { } <EOL> ztpGlobalSettings = util . loadClosDefinition ( ) [ '<STR_LIT>' ] <EOL> subnet = ztpGlobalSettings [ '<STR_LIT>' ] <EOL> dhcpBlock = IPNetwork ( subnet ) <EOL> ipList = list ( dhcpBlock . iter_hosts ( ) ) <EOL> ztp [ '<STR_LIT>' ] = str ( dhcpBlock . network ) <EOL> ztp [ '<STR_LIT>' ] = str ( dhcpBlock . netmask ) <EOL> ztp [ '<STR_LIT>' ] = ztpGlobalSettings . get ( '<STR_LIT>' ) <EOL> if ztp [ '<STR_LIT>' ] is None or ztp [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> ztp [ '<STR_LIT>' ] = str ( ipList [ <NUM_LIT:0> ] ) <EOL> ztp [ '<STR_LIT>' ] = ztpGlobalSettings . get ( '<STR_LIT>' ) <EOL> if ztp [ '<STR_LIT>' ] is None or ztp [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> ztp [ '<STR_LIT>' ] = str ( ipList [ <NUM_LIT:1> ] ) <EOL> ztp [ '<STR_LIT>' ] = ztpGlobalSettings . get ( '<STR_LIT>' ) <EOL> if ztp [ '<STR_LIT>' ] is None or ztp [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> ztp [ '<STR_LIT>' ] = str ( ipList [ - <NUM_LIT:1> ] ) <EOL> ztp [ '<STR_LIT>' ] = str ( dhcpBlock . broadcast ) <EOL> ztp [ '<STR_LIT>' ] = self . __conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if ztpGlobalSettings . get ( '<STR_LIT>' ) is not None : <EOL> ztp [ '<STR_LIT>' ] = '<STR_LIT>' + ztpGlobalSettings . get ( '<STR_LIT>' ) <EOL> return ztp <EOL> def populateDhcpDeviceSpecificSettingForAllPods ( self , session , ztp = { } ) : <EOL> pods = self . _dao . getAll ( session , Pod ) <EOL> for pod in pods : <EOL> ztp = self . populateDhcpDeviceSpecificSetting ( session , pod . id , ztp ) <EOL> return ztp <EOL> def populateDhcpDeviceSpecificSetting ( self , session , podId , ztp = { } ) : <EOL> '''<STR_LIT>''' <EOL> imageUrlPrefix = '<STR_LIT>' <EOL> if ztp . get ( '<STR_LIT>' ) is None : <EOL> ztp [ '<STR_LIT>' ] = [ ] <EOL> pod = self . _dao . getObjectById ( session , Pod , podId ) <EOL> for device in pod . devices : <EOL> if device . family == '<STR_LIT>' : <EOL> continue <EOL> if device . role == '<STR_LIT>' : <EOL> imageName = util . getImageNameForDevice ( pod , device ) <EOL> if imageName is not None : <EOL> imageUrl = imageUrlPrefix + imageName <EOL> else : <EOL> imageUrl = None <EOL> elif device . role == '<STR_LIT>' : <EOL> if util . isZtpStaged ( self . __conf ) : <EOL> continue <EOL> imageName = util . getImageNameForDevice ( pod , device ) <EOL> if imageName is not None : <EOL> imageUrl = imageUrlPrefix + imageName <EOL> else : <EOL> imageUrl = None <EOL> else : <EOL> logger . error ( '<STR_LIT>' % ( pod . id , pod . name , device . name , device . role ) ) <EOL> continue <EOL> deviceMgmtIp = str ( IPNetwork ( device . managementIp ) . ip ) <EOL> if device . macAddress : <EOL> ztp [ '<STR_LIT>' ] . append ( { '<STR_LIT:name>' : device . name , '<STR_LIT>' : device . macAddress , <EOL> '<STR_LIT>' : '<STR_LIT>' + pod . id + '<STR_LIT>' + device . id + '<STR_LIT>' , <EOL> '<STR_LIT>' : imageUrl , '<STR_LIT>' : deviceMgmtIp } ) <EOL> logger . info ( '<STR_LIT>' % ( device . name , deviceMgmtIp ) ) <EOL> elif device . serialNumber : <EOL> ztp [ '<STR_LIT>' ] . append ( { '<STR_LIT:name>' : device . name , '<STR_LIT>' : device . serialNumber , <EOL> '<STR_LIT>' : '<STR_LIT>' + pod . id + '<STR_LIT>' + device . id + '<STR_LIT>' , <EOL> '<STR_LIT>' : imageUrl , '<STR_LIT>' : deviceMgmtIp } ) <EOL> logger . info ( '<STR_LIT>' % ( device . name , deviceMgmtIp ) ) <EOL> else : <EOL> logger . error ( '<STR_LIT>' % ( device . name , deviceMgmtIp ) ) <EOL> if util . isZtpStaged ( self . __conf ) : <EOL> ztp [ '<STR_LIT>' ] = [ ] <EOL> for leafSetting in pod . leafSettings : <EOL> setting = { } <EOL> setting [ '<STR_LIT>' ] = leafSetting . deviceFamily <EOL> if leafSetting . junosImage is not None : <EOL> setting [ '<STR_LIT>' ] = imageUrlPrefix + leafSetting . junosImage <EOL> else : <EOL> setting [ '<STR_LIT>' ] = None <EOL> setting [ '<STR_LIT>' ] = '<STR_LIT>' + pod . id + '<STR_LIT>' + leafSetting . deviceFamily <EOL> '''<STR_LIT>''' <EOL> setting [ '<STR_LIT>' ] = len ( '<STR_LIT>' + leafSetting . deviceFamily ) <EOL> ztp [ '<STR_LIT>' ] . append ( setting ) <EOL> return ztp <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> ztpServer = ZtpServer ( ) <EOL> with ztpServer . _dao . getReadSession ( ) as session : <EOL> pods = ztpServer . _dao . getAll ( session , Pod ) <EOL> ztpServer . createPodSpecificDhcpConfFile ( session , pods [ <NUM_LIT:0> ] . id ) <EOL> ztpServer . createPodSpecificDhcpConfFile ( session , pods [ <NUM_LIT:1> ] . id ) </s>
