Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 4.54 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. ####################################################################
  2. #Alessandro Perucchini
  3. #alexp@roguewave.com
  4. #03/25/2009
  5. #Rogue Wave Software, Inc All Rights Reserved
  6. #
  7. #Initial phase one of the Polycom User File Management Application
  8. #
  9. #phoneList.rb - Creates Polycom XML files from a list of users in the
  10. #database and then ftp the files to the Asterick Server
  11. #
  12. #The script works on Windows XP and 2003 Servers
  13. ####################################################################
  14. require 'rubygems'
  15. require 'activerecord'
  16. require 'erb'
  17. require 'pp'
  18. require 'fileutils'
  19. require 'net/ftp'
  20. ######################################
  21. def connect_db
  22.   begin
  23.     ActiveRecord::Base.establish_connection(
  24.       :adapter => "sqlite3",
  25.       :database => "AstericksPhone.sqlite",
  26.       :pool => 5,
  27.       :timeout => 5000
  28.      )
  29.   rescue
  30.     puts "The database connection failed."
  31.   end
  32. end
  33. ######################################
  34. class Phone < ActiveRecord::Base
  35. end
  36. ######################################
  37. def create_cfg_files
  38.  connect_db()
  39.  phoneList = Phone.find(:all, :conditions=>{:assignStatus=>0})
  40.  
  41.  if !phoneList.empty?
  42.     #open the per_phone.cfg & master_sip file in read-only mode
  43.     begin
  44.       per_phone = File.open("configTemplates/per_phone.cfg", "r")
  45.       master_sip = File.open("configTemplates/master_sip.cfg", "r")
  46.     rescue
  47.       puts "Error: The cfg template files cannot be opened for writing"
  48.       exit
  49.     end
  50.     #change to the createdConfig directory
  51.     Dir.chdir("createdConfig")
  52.     master_sip = ERB.new(master_sip, nil, '<>')
  53.     per_phone = ERB.new(per_phone, nil, '<>')
  54.    
  55.     puts "Creating new cfg files..."
  56.     phoneList.each do |item|
  57.       #write per_phone file to folder
  58.       phone_file = File.new("phone-#{item.serialNumber}.cfg", "w")
  59.       phone_file.write(per_phone.result(binding))
  60.       phone_file.close
  61.       #write master sip file to folder
  62.       sip_file = File.new("#{item.serialNumber}.cfg", "w")
  63.       sip_file.write(master_sip.result(binding))
  64.       sip_file.close
  65.       #update the Phones table record set assign_status=1
  66.       #Phone.update(item.id,{:assignStatus => 1})
  67.     end
  68.     pp Dir.glob('*.cfg')
  69.     puts "User(s) cfg files created! files available in \"createdConfig\" directory"
  70.     #reset the directory
  71.     Dir.chdir("../")
  72.   else
  73.     puts "No cfg files need to be created!"
  74.   end
  75. end
  76. ##############################################
  77. def disconnect
  78.   puts "Exiting program!"
  79.   exit
  80. end
  81. ##############################################
  82. def ftp_cfg_files
  83.   #switch directory
  84.   Dir.chdir("createdConfig")
  85.   if !Dir.glob('*.cfg').empty?
  86.      puts "FTP(ing) cfg files to the Asterick Server "
  87.      ftp = Net::FTP.new('AsteriskPOC.bco.roguewave.com') rescue nil
  88.      if ftp.nil?
  89.       puts "The Asterick Server is not responding"
  90.      end
  91.    
  92.      ftp.login('PlcmSpIp', 'PlcmSpIp')
  93.      puts ">> Logged in the Asterick Server"
  94.      files = Dir.glob('*.cfg')
  95.      files.each do |file|
  96.         ftp.putbinaryfile(file)
  97.       end
  98.      ftp.close
  99.      puts ">> Successfuly ftp(ed)the cfg files."
  100.   else
  101.      puts "No files to ftp"
  102.  end
  103.  #reset Dir
  104.  Dir.chdir("../")
  105. end
  106. ##############################################
  107. def archive_cfg_files
  108.   dest = File.expand_path("archive")
  109.   #change directory
  110.   Dir.chdir("createdConfig")
  111.   #move existing files to the archive folder
  112.   if !Dir.glob('*.cfg').empty?
  113.     puts "Archiving Files..."
  114.     FileUtils.mv(Dir.glob('*.cfg'), dest, :noop => false, :verbose => false)
  115.     #clean-up the directory
  116.     FileUtils.rm(Dir.glob('*.cfg'))
  117.     puts "Archive Completed"
  118.   else
  119.     puts "No files to archive"
  120.   end
  121.   #reset the directory
  122.   Dir.chdir("../")
  123. end
  124.  
  125. ##############################################
  126. def program_menu
  127.   loop do
  128.     puts "#### Asterick Polycom User Configuration App ##############"
  129.     puts %q{Please select an option:
  130.       1 - Create the (cfg)cofiguration files
  131.       2 - FTP the cfg files to Asterick Server
  132.       3 - Archive the cfg files (FTP or move newly created
  133.           files to the Asterisk Server before archiving)
  134.       4 - Quit}
  135.     puts "###########################################################"
  136.     case gets.chomp
  137.       when '1'
  138.         create_cfg_files
  139.       when '2'
  140.         ftp_cfg_files
  141.       when '3'
  142.         archive_cfg_files
  143.       when '4'
  144.         disconnect
  145.     end
  146.   end
  147. end
  148.  
  149. ##########################################################################
  150. #Run
  151. ##########################################################################
  152. program_menu