#!/usr/bin/env ruby require 'optparse' require 'tmpdir' require 'erb' require 'genZPK' require 'xmlsimple' include GenZPK =begin cd /var/www/uat/ run checks nano deployment.xml rm -rf INSXCloud-UAT.zpk sudo /usr/local/zend/bin/zdpack pack uat/ =end options = {} opt_parser = OptionParser.new do |opt| opt.banner = "Usage: genZPK -s SYSTEM -v ZPK_VERSION -n PACKAGE_NAME DIR" opt.separator "Initial configuration: genZPK --configure " opt.separator "" opt.on("-s","--system SYSTEM","which system should checks be performed for") do |system| options[:system] = system end opt.on("-v","--app-version ZPK_VERSION","version of zpk to create") do |version| options[:version] = version end opt.on("-n","--name PACKAGE_NAME","name of application to deploy") do |name| options[:name] = name end opt.on("--configure","perform the initial configuration to setup checks prior to packaging") do GenZPK::doConfigure exit 0 end opt.on("--version", "prints the version of this application") do puts GenZPK::VERSION exit 0 end opt.on("-h","--help","help") do puts opt_parser end end begin opt_parser.parse! rescue OptionParser::InvalidOption warn "Required parameters not set or invalid argument. Please run genZPK --help for more info." exit -1 end if not File.exists?(File.expand_path '~/.genZPK/checks.xml') warn "Unable to locate configuration files. Please run genZPK --configure" exit -1 end if not File.read(File.expand_path('~/.genZPK/checks.xml')).gsub(/\s+/, "") == "" checkHash = XmlSimple.xml_in(File.expand_path( '~/.genZPK/checks.xml'), {'KeyAttr' => 'name', 'ForceArray'=>false}) else warn "Configuration empty. Please run genZPK --configure" exit -1 end if getZdpack.nil? warn "unable to locate zdpack executable. Please check zend server installation at /usr/local/zend/" exit -1 end dir = ARGV[0] if dir.nil? or options[:system].nil? or options[:version].nil? or options[:name].nil? warn "Required parameters not set or invalid argument. Please run genZPK --help for more info." exit -1 end if String(dir).end_with? '/' dir = dir.chomp end if not File.exists? dir warn %q{Error: Directory "#{dir}" does not exist.} exit -1 end Dir.chdir dir if not system("git pull origin master") warn "Unable to update code via git. Exiting..." Dir.chdir ".." exit -1 end Dir.chdir ".." if checkHash["system"].has_key? "name" if not checkHash["system"]["name"] == options[:system] warn "Could not find system \"#{options[:system]}\" in configuration. Please run genZPK --configure to setup checks for this system or verify the system name." exit -1 end if checkHash["system"]["check"].is_a? Array checks = checkHash["system"]["check"] else checks = Array.new checks.push checkHash["system"]["check"] end else if not checkHash["system"].has_key? options[:system] warn "Could not find system \"#{options[:system]}\" in configuration. Please run genZPK --configure to setup checks for this system or verify the system name." exit -1 end if checkHash["system"][options[:system]]["check"].is_a? Array checks = checkHash["system"][options[:system]]["check"] else checks = Array.new checks.push checkHash["system"][options[:system]]["check"] end end puts "Running checks" puts "-" * 80 successes = 0 for i in 0..(checks.size - 1) check = checks[i] desc = check["desc"] filepath = check["file"] matchval = check["value"] begin regex = /#{check["regex"]["content"]}/ rescue RegexpError => e puts "Invalid regular in configuration. Please run genZPK --configure\n#{e.message}" exit -1 end matchgrp = Integer(check["regex"]["group"]) - 1 display = desc if not File.exists?(dir + filepath) display << "." * (80 - "FAIL".size - desc.size) display << "FAIL\n" display << "\tFile not found: #{dir + filepath}" puts display next end contents = File.read(dir + filepath) matches = contents.scan(regex) for i in 0..(matches.size - 1) match = matches[i] if not match[matchgrp] == matchval display << "." * (80 - "FAIL".size - desc.size) display << "FAIL\n" display << "\t#{dir + filepath}\n" display << "\tValue found: #{match[matchgrp]}\n" display << "\tRequired value: #{matchval}" puts display next end end display << "." * (80 - "SUCCESS".size - desc.size) display << "SUCCESS" puts display successes += 1 end puts "-" * 80 if not successes == checks.size puts "#{successes} of #{checks.size} checks passed. Please check files listed above and try again." exit -1 end puts "All checks passed. Continuing..." if not File.exists? dir + "/scripts" FileUtils.mkpath dir + "/scripts" end scriptTemplate = getTemplate 'pre_activate.php' deploymentTemplate = getTemplate 'deployment.xml' releaseName = options[:name] zpkVersion = options[:version] script = ERB.new(scriptTemplate) deployment = ERB.new(deploymentTemplate) File.open(dir + '/scripts/pre_activate.php', 'w') {|f| f.puts(script.result(binding))} File.open(dir + '/deployment.xml', 'w') {|f| f.puts(deployment.result(binding))} zpkFile = "#{options[:name]}.zpk" while File.exists? zpkFile matches = /(.*\.zpk)\.?(.*)?/.match(zpkFile) if matches[2]=="" ver = 0 else ver = Integer(matches[2]) end zpkFile = "#{matches[1]}.#{ver + 1}" end Dir.mktmpdir do |tempDir| zdpack = getZdpack system("mv #{dir}/.git #{tempDir}/") system("#{zdpack} pack --output-dir=#{tempDir} #{dir}") system("mv #{tempDir}/.git #{dir}/") system("mv #{tempDir}/#{options[:name]}.zpk ./#{zpkFile}") system("rm -r #{dir + '/scripts'}") system("rm #{dir + '/deployment.xml'}") end puts "Successfully created ZPK at ./#{zpkFile}"