<commit_before>require 'puppet/util/network_device'
require 'puppet/provider/dell_powerconnect'

Puppet::Type.type(:powerconnect_config).provide :dell_powerconnect, :parent => Puppet::Provider do
  mk_resource_methods

  def run(url, config_type, force)   
    dev = Puppet::Util::NetworkDevice.current
    digestlocalfile=''
    digestserverconfig=''
    extbackupconfigfile = ''
    flashtmpfile = 'flash://backup-configtemp.scr'
    backedupprevconfig = false
    
    ##first check whether there is any backup-config, if so store it to flash and restore it at the end
    dev.transport.command('show backup-config') do |extbackup|
      extbackupconfigfile<< extbackup
    end
    if extbackupconfigfile.include? "Configuration script 'backup-config' not found"
      ##There is no existing backup config so do nothing
      Puppet.debug "no previous backup config found"
    else
      ##There is an existing backup config
      Puppet.debug "There is a previous backup config found"
      executecommand(dev, 'copy backup-config ' + flashtmpfile,"Are you sure you want to start")
    end 
    
    ##copying the file from tftp to backup-config
    executecommand(dev, 'copy ' + url + ' backup-config',"Are you sure you want to start")
    
    digestlocalfile = getfilemd5hash(dev, 'show backup-config', 0..18)
    Puppet.debug "digest1 = #{digestlocalfile}"
          
    if config_type == 'startup'      
      digestserverconfig = getfilemd5hash(dev, 'show startup-config', 0..19)
      Puppet.debug "digest2 = #{digestserverconfig}"      
    else
      digestserverconfig = getfilemd5hash(dev, 'show running-config', 0..19)
      Puppet.debug "digest2 = #{digestserverconfig}"
    end
    
    if digestlocalfile == digestserverconfig && force == :false
      Puppet.info "No Configuration change"
    else 
      if config_type == 'startup'
        Puppet.debug "i am applying the startup config"
        executecommand(dev, 'copy ' + url + ' startup-config',"Are you sure you want to start")
      else
        Puppet.debug "i am applying the running config"
        executecommand(dev, 'copy ' + url + ' running-config',"Are you sure you want to start")
      end
    end
       
    if backedupprevconfig == true
      Puppet.debug "i am restoring the previous backup config"
      #Restoring the backed up backed up backup config
      executecommand(dev, 'copy ' + flashtmpfile+ ' backup-config',"Are you sure you want to start")
      # deleting the backup file from flash
      Puppet.debug "i am deleting the backup file"
      executecommand(dev, 'delete backup-configtemp.scr',"Delete ")
    end
    
    if config_type == 'startup' && force == :true
      Puppet.debug "i am doing a reload"
      #executecommand(dev, 'reload', "Are you sure you want to")
      dev.transport.command('reload') do |out|
        out.each_line do |line|
          #Puppet.debug "line = #{line}"
          if line.include?("Are you sure you want to")
            Puppet.debug "match found"
            dev.transport.send("y\r")
            return
          end
        end
      end  
    end
  end  
  
  def executecommand(dev, cmd, str)
    dev.transport.command(cmd) do |out|
      out.each_line do |line|
        if line.include?(str)
          dev.transport.send("y\r") 
          if dev.transport.class.name.include? 'Ssh'
            return
          end          
        end
      end   
    end  
  end
  
  def getfilemd5hash(dev, cmd, slice)
    filecontent = ''
    dev.transport.command(cmd) do |out|   
      filecontent<< out
    end
    if dev.transport.class.name.include? 'Telnet'
      filecontent.slice!(slice)
    else
      if dev.transport.class.name.include? 'Ssh'
        index = filecontent.rindex("!Current Configuration")
        filecontent = filecontent[index..-1]
      else
      end
    end
    digestlocalfile = Digest::MD5.hexdigest(filecontent)
    return digestlocalfile
  end
end<commit_msg>changes done: adding the code to initialize the switch back after the reload of the switch<commit_after>require 'puppet/util/network_device'
require 'puppet/provider/dell_powerconnect'

Puppet::Type.type(:powerconnect_config).provide :dell_powerconnect, :parent => Puppet::Provider do
  mk_resource_methods

  def run(url, config_type, force)   
    dev = Puppet::Util::NetworkDevice.current
    digestlocalfile=''
    digestserverconfig=''
    extBackupConfigfile = ''
    flashtmpfile = 'flash://backup-configtemp.scr'
    backedupPrevConfig = false
    
    Puppet.debug "transport name = #{dev.transport.class.name}"    
    
    ##first check whether there is any backup-config, if so store it to flash and restore it at the end
    dev.transport.command('show backup-config') do |extBackup|
      extBackupConfigfile<< extBackup
    end
    if extBackupConfigfile.include? "Configuration script 'backup-config' not found"
      ##There is no existing backup config so do nothing
      Puppet.debug "no previous backup config found"
    else
      ##There is an existing backup config
      Puppet.debug "There is a previous backup config found"
      executeCommand(dev, 'copy backup-config ' + flashtmpfile,"Are you sure you want to start")
    end 
    
    ##copying the file from tftp to backup-config
    executeCommand(dev, 'copy ' + url + ' backup-config',"Are you sure you want to start")
    
    digestlocalfile = getfileMD5(dev, 'show backup-config', 0..18)
    Puppet.debug "digest1 = #{digestlocalfile}"
          
    if config_type == 'startup'      
      digestserverconfig = getfileMD5(dev, 'show startup-config', 0..19)
      Puppet.debug "digest2 = #{digestserverconfig}"      
    else
      digestserverconfig = getfileMD5(dev, 'show running-config', 0..19)
      Puppet.debug "digest2 = #{digestserverconfig}"
    end
    
    if digestlocalfile == digestserverconfig && force == :false
      Puppet.info "No Configuration change"
    else 
      if config_type == 'startup'
        Puppet.debug "i am applying the startup config"
        executeCommand(dev, 'copy ' + url + ' startup-config',"Are you sure you want to start")
      else
        Puppet.debug "i am applying the running config"
        executeCommand(dev, 'copy ' + url + ' running-config',"Are you sure you want to start")
      end
    end
       
    if backedupPrevConfig == true
      Puppet.debug "i am restoring the previous backup config"
      #Restoring the backed up backed up backup config
      executeCommand(dev, 'copy ' + flashtmpfile+ ' backup-config',"Are you sure you want to start")
      # deleting the backup file from flash
      Puppet.debug "i am deleting the backup file"
      executeCommand(dev, 'delete backup-configtemp.scr',"Delete ")
    end
    
    if config_type == 'startup' && force == :true
      Puppet.debug "i am doing a reload"
      #executeCommand(dev, 'reload', "Are you sure you want to") 
      rebootswitch  
    end
  end  
  
  def executeCommand(dev, cmd, str)
    dev.transport.command(cmd) do |out|
      out.each_line do |line|
        if line.include?(str)
          dev.transport.send("y\r") 
          if dev.transport.class.name.include? 'Ssh'
            return
          end          
        end
      end   
    end  
  end
  
  def getfileMD5(dev, cmd, slice)
    filecontent = ''
    dev.transport.command(cmd) do |out|   
      filecontent<< out
    end
    if dev.transport.class.name.include? 'Telnet'
      filecontent.slice!(slice)
    else
      if dev.transport.class.name.include? 'Ssh'
        index = filecontent.rindex("!Current Configuration")
        filecontent = filecontent[index..-1]
      else
      end
    end
    digestlocalfile = Digest::MD5.hexdigest(filecontent)
    return digestlocalfile
  end
  
  def rebootswitch()
    dev = Puppet::Util::NetworkDevice.current
    flagfirstresponse=false
    flagsecondresponse=false
    flagthirdresponse=false

    dev.transport.command("reload")  do |out|
      firstresponse =out.scan("Are you sure you want to continue")
      secondresponse = out.scan("Are you sure you want to reload the stack")
      unless firstresponse.empty?
        flagfirstresponse=true
        break
      end
      unless secondresponse.empty?
        flagsecondresponse=true
        break
      end
    end

    #Some times sending reload command returning with console prompt without doing anything, in that case retry max for 3 times
#    if (!flagfirstresponse && !flagsecondresponse) && rebootrycount<3
#      Puppet.debug "i am here 1"
#      rebootrycount=rebootrycount+1
#      rebootswitch()
#    end

    if flagfirstresponse
      dev.transport.command("y") do |out|
        thirdresponse = out.scan("Are you sure you want to reload the stack")
        unless thirdresponse.empty?
          flagthirdresponse=true
          break
        end
      end
      if flagthirdresponse
        dev.transport.send("y\r") do |out|
        end
      else
        Puppet.debug "ELSE BLOCK1.2"
      end
    else
      Puppet.debug "ELSE BLOCK1.1"
    end
    if flagsecondresponse
      dev.transport.send("y\r") do |out|
        #without this block expecting for prompt and so hanging
        break
      end
    else
      Puppet.debug "ELSE BLOCK2"
    end

    #Sleep for 3 mins t wait for switch to come up
    Puppet.info("Going to sleep for 3 minutes, for switch reboot...")
    sleep 180

    #Reesatblish transport session
    Puppet.info("Trying to reconnect to switch...")
    dev.connect_transport
    dev.switch.transport=dev.transport
    Puppet.info("Session established...")
  end
end
{'fraction_non_alphanumeric': 0.09255765199161425, 'fraction_numerical': 0.005241090146750524, 'mean_word_length': 2.6236232434485376, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7319164', 'n_tokens_mistral': 2845, 'n_tokens_neox': 2789, 'n_words': 837}