#!/usr/bin/env ruby require 'optparse' require 'tmpdir' require 'erb' require 'genZPK' require 'xmlsimple' include GenZPK 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("-p","--pull", "run \"git pull origin master\" prior to packaging") do options[:pull] = true 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 dir = String(ARGV[0]).end_with?('/') ? String(ARGV[0]).chomp : String(ARGV[0]) requireCondition File.exists?(File.expand_path '~/.genZPK/checks.xml'), "Unable to locate configuration files. Please run genZPK --configure" requireCondition (File.read(File.expand_path('~/.genZPK/checks.xml')).gsub(/\s+/, "") != ""), "Configuration empty. Please run genZPK --configure" requireCondition getZdpack.not_nil?, "unable to locate zdpack executable. Please check zend server installation at /usr/local/zend/" requireCondition dir.not_nil?,"Required parameters not set or invalid argument. Please run genZPK --help for more info." requireCondition options[:system].not_nil?,"Required parameters not set or invalid argument. Please run genZPK --help for more info." requireCondition options[:version].not_nil?,"Required parameters not set or invalid argument. Please run genZPK --help for more info." requireCondition options[:name].not_nil?,"Required parameters not set or invalid argument. Please run genZPK --help for more info." requireCondition File.exists?(dir), "Error: Directory \"#{dir}\" does not exist." configHash = XmlSimple.xml_in(File.expand_path( '~/.genZPK/checks.xml'), {'KeyAttr' => 'name', 'ForceArray'=>false}) if options[:pull] == true Dir.chdir dir do requireCondition system("git pull origin master"), "Unable to update code via git. Exiting..." end end if configHash["system"].has_key? "name" if not configHash["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 configHash["system"]["checks"]["check"].is_a? Array checks = configHash["system"]["checks"]["check"] else checks = Array.new checks.push configHash["system"]["checks"]["check"] end if configHash["system"].has_key? "subdomains" if configHash["system"]["subdomains"]["file"].is_a? Array subdomainChecks = configHash["system"]["subdomains"]["file"] else subdomainChecks = Array.new subdomainChecks.push configHash["system"]["subdomains"]["file"] end else subdomainChecks = nil end else if not configHash["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 configHash["system"][options[:system]]["checks"]["check"].is_a? Array checks = configHash["system"][options[:system]]["checks"]["check"] else checks = Array.new checks.push configHash["system"][options[:system]]["checks"]["check"] end if configHash["system"][options[:system]].has_key? "subdomains" if configHash["system"][options[:system]]["subdomains"]["file"].is_a? Array subdomainChecks = configHash["system"][options[:system]]["subdomains"]["file"] else subdomainChecks = Array.new subdomainChecks.push configHash["system"][options[:system]]["subdomains"]["file"] end else subdomainChecks = nil 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..." puts "-" * 80 puts "Verifying subdomains" puts "-" * 80 successes = 0 for i in 0..(subdomainChecks.size - 1) check = subdomainChecks[i] path = check["path"] display = path if File.exists?(dir + path) contents = File.read(dir + path) fieldPasses = 0 check.each do |field, value| if not field == "path" if not configHash["definitions"]["definition"][field].nil? begin regex = /#{configHash["definitions"]["definition"][field]["regex"]["content"]}/ rescue RegexpError => e puts "Invalid regular in configuration. Please run genZPK --configure\n#{e.message}" exit -1 end matchgrp = Integer(configHash["definitions"]["definition"][field]["regex"]["group"]) - 1 matches = contents.scan(regex) if matches[0][0] == value fieldPasses += 1 else if display.end_with? "\n" display << "\tField Failed: #{field}\n" display << "\t\tValue: #{matches[0][0]}\n" display << "\t\tDefined value: #{value}\n" else display << "\n\tField Failed: #{field}\n" display << "\t\tValue: #{matches[0][0]}\n" display << "\t\tDefined value: #{value}\n" end end else if display.end_with? "\n" display << "\tField not defined: #{field}\n" else display << "\n\tField not defined: #{field}\n" end end end end if fieldPasses == (check.size - 1) display << "." * (80 - "SUCCESS".size - display.size) display << "SUCCESS" puts display successes += 1 else tmpString = "#{fieldPasses} of #{check.size - 1} fields verified" display << tmpString display << "." * (80 - "#{fieldPasses} of #{check.size - 1} fields verified".size - "FAIL".size) display << "FAIL" puts display end else display << "." * (80 - "FAIL".size - display.size) display << "FAIL\n" display << "\tFile not found: #{dir + path}" puts display end end puts "-" * 80 if not successes == subdomainChecks.size puts "#{successes} of #{subdomainChecks.size} subdomains passed. Please check files listed above and try again." exit -1 end puts "All subdomains 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}"