- ####################################################################
- #Alessandro Perucchini
- #alexp@roguewave.com
- #03/25/2009
- #Rogue Wave Software, Inc All Rights Reserved
- #
- #Initial phase one of the Polycom User File Management Application
- #
- #phoneList.rb - Creates Polycom XML files from a list of users in the
- #database and then ftp the files to the Asterick Server
- #
- #The script works on Windows XP and 2003 Servers
- ####################################################################
- require 'rubygems'
- require 'activerecord'
- require 'erb'
- require 'pp'
- require 'fileutils'
- require 'net/ftp'
- ######################################
- def connect_db
- begin
- ActiveRecord::Base.establish_connection(
- :adapter => "sqlite3",
- :database => "AstericksPhone.sqlite",
- :pool => 5,
- :timeout => 5000
- )
- rescue
- puts "The database connection failed."
- end
- end
- ######################################
- class Phone < ActiveRecord::Base
- end
- ######################################
- def create_cfg_files
- connect_db()
- phoneList = Phone.find(:all, :conditions=>{:assignStatus=>0})
- if !phoneList.empty?
- #open the per_phone.cfg & master_sip file in read-only mode
- begin
- per_phone = File.open("configTemplates/per_phone.cfg", "r")
- master_sip = File.open("configTemplates/master_sip.cfg", "r")
- rescue
- puts "Error: The cfg template files cannot be opened for writing"
- exit
- end
- #change to the createdConfig directory
- Dir.chdir("createdConfig")
- master_sip = ERB.new(master_sip, nil, '<>')
- per_phone = ERB.new(per_phone, nil, '<>')
- puts "Creating new cfg files..."
- phoneList.each do |item|
- #write per_phone file to folder
- phone_file = File.new("phone-#{item.serialNumber}.cfg", "w")
- phone_file.write(per_phone.result(binding))
- phone_file.close
- #write master sip file to folder
- sip_file = File.new("#{item.serialNumber}.cfg", "w")
- sip_file.write(master_sip.result(binding))
- sip_file.close
- #update the Phones table record set assign_status=1
- #Phone.update(item.id,{:assignStatus => 1})
- end
- pp Dir.glob('*.cfg')
- puts "User(s) cfg files created! files available in \"createdConfig\" directory"
- #reset the directory
- Dir.chdir("../")
- else
- puts "No cfg files need to be created!"
- end
- end
- ##############################################
- def disconnect
- puts "Exiting program!"
- exit
- end
- ##############################################
- def ftp_cfg_files
- #switch directory
- Dir.chdir("createdConfig")
- if !Dir.glob('*.cfg').empty?
- puts "FTP(ing) cfg files to the Asterick Server "
- ftp = Net::FTP.new('AsteriskPOC.bco.roguewave.com') rescue nil
- if ftp.nil?
- puts "The Asterick Server is not responding"
- end
- ftp.login('PlcmSpIp', 'PlcmSpIp')
- puts ">> Logged in the Asterick Server"
- files = Dir.glob('*.cfg')
- files.each do |file|
- ftp.putbinaryfile(file)
- end
- ftp.close
- puts ">> Successfuly ftp(ed)the cfg files."
- else
- puts "No files to ftp"
- end
- #reset Dir
- Dir.chdir("../")
- end
- ##############################################
- def archive_cfg_files
- dest = File.expand_path("archive")
- #change directory
- Dir.chdir("createdConfig")
- #move existing files to the archive folder
- if !Dir.glob('*.cfg').empty?
- puts "Archiving Files..."
- FileUtils.mv(Dir.glob('*.cfg'), dest, :noop => false, :verbose => false)
- #clean-up the directory
- FileUtils.rm(Dir.glob('*.cfg'))
- puts "Archive Completed"
- else
- puts "No files to archive"
- end
- #reset the directory
- Dir.chdir("../")
- end
- ##############################################
- def program_menu
- loop do
- puts "#### Asterick Polycom User Configuration App ##############"
- puts %q{Please select an option:
- 1 - Create the (cfg)cofiguration files
- 2 - FTP the cfg files to Asterick Server
- 3 - Archive the cfg files (FTP or move newly created
- files to the Asterisk Server before archiving)
- 4 - Quit}
- puts "###########################################################"
- case gets.chomp
- when '1'
- create_cfg_files
- when '2'
- ftp_cfg_files
- when '3'
- archive_cfg_files
- when '4'
- disconnect
- end
- end
- end
- ##########################################################################
- #Run
- ##########################################################################
- program_menu