lib/devinstall.rb in devinstall-0.2.6 vs lib/devinstall.rb in devinstall-1.0.0

- old
+ new

@@ -1,99 +1,82 @@ require 'devinstall/version' require 'devinstall/deep_symbolize' require 'devinstall/utils' -require 'devinstall/settings' ## in near future we will have to abandon Settings - # for something more complex because we will need to - # define things (repos/install-hosts) for different - # environments (dev/qa/prelive/live/prod/backup and so) +require 'devinstall/settings' require 'pp' module Devinstall class Pkg include Utils - # @param [Symbol] type - def get_version(type) - if type == :deb - begin - deb_changelog = File.expand_path "#{Settings.local[:folder]}/#{@package}/debian/changelog" # This is the folder that should be checked - deb_package_version = File.open(deb_changelog, 'r') { |f| f.gets.chomp.sub(/^.*\((.*)\).*$/, '\1') } - @_package_version[:deb] = deb_package_version - - rescue IOError => e - puts "IO Error while opening #{deb_changelog}" - puts "Aborting \n #{e}" - exit! 1 - end + def get_version + case @config.type + when :deb + begin + deb_changelog = File.expand_path "#{@config.local(:folder)}/#{@package}/debian/changelog" # This is the folder that should be checked + unless File.exists? deb_changelog + exit! <<-eos + No 'debian/changelog' found in specified :local:folder (#{@config.local(:folder)}) + Please check your config file + eos + end + @_package_version[:deb] = File.open(deb_changelog, 'r') { |f| f.gets.chomp.sub(/^.*\((.*)\).*$/, '\1') } + rescue IOError => e + exit! <<-eos + IO Error while opening #{deb_changelog} + Aborting \n #{e} + eos + end end end # @param [String] package - def initialize (package) - # curently implemented only for .deb packages (for .rpm later :D) - unless Settings.packages.has_key? package.to_sym - puts "You required an undefined package #{package}" - puts "Aborting!" - exit! 1 - end - @package = package.to_sym - @_package_version = Hash.new # versions for types: - @package_files = Hash.new - arch = Settings.build[:arch] - pname = "#{package}_#{get_version :deb}" - @package_files[:deb] = {deb: "#{pname}_#{arch}.deb", - tgz: "#{pname}.tar.gz", - dsc: "#{pname}.dsc", - chg: "#{pname}_amd64.changes"} + # @param [String] config + def initialize (config) + @config=config #class variable,first thing! + # currently implemented only for .deb packages (for .rpm later :D) + @package = @config.pkg.to_sym + @_package_version = {} # versions for types: + @package_files = {} + arch = @config.build(:arch) + p_name = "#{@package}_#{get_version}" + @package_files[:deb] = {deb: "#{p_name}_#{arch}.deb", + tgz: "#{p_name}.tar.gz", + dsc: "#{p_name}.dsc", + chg: "#{p_name}_amd64.changes"} end - def upload (env) - scp = Settings.base[:scp] + def upload + scp = @config.base(:scp) repo = {} - type = Settings.repos[:environments][env][:type].to_sym - [:user, :host, :folder].each do |k| - unless Settings.repos[:environments][env].has_key?(k) - puts "Unexistent key #{k} in repos:environments:#{env}" - puts "Aborting" - exit! 1 - end - repo[k] = Settings.repos[:environments][env][k] + [:user, :host, :folder, :type].each do |k| + repo[k] = @config.repos(k) # looks stupid end - @package_files[type].each do |p,f| + @package_files[type].each do |p, f| puts "Uploading #{f}\t\t[#{p}] to $#{repo[:host]}" - command("#{scp} #{Settings.local[:temp]}/#{f} #{repo[:user]}@#{repo[:host]}:#{repo[:folder]}") + command("#{scp} #{@config.local(:temp)}/#{f} #{repo[:user]}@#{repo[:host]}:#{repo[:folder]}") end end - # @param [Symbol] type - def build (type) - puts "Building package #{@package} type #{type.to_s}" - unless Settings.packages[@package].has_key? type - puts "Package '#{@package}' cannot be built for the required environment" - puts "undefined build configuration for '#{type.to_s}'" - exit! 1 - end + def build + type = @config.type + puts "Building package #{@package} type #{type}" build = {} [:user, :host, :folder, :target].each do |k| - unless Settings.build.has_key? k - puts "Undefined key 'build:#{k.to_s}:'" - puts "Aborting!" - exit! 1 - end - build[k] = Settings.build[k] + build[k] = @config.build(k) end - ssh = Settings.base[:ssh] - build_command = Settings.packages[@package][type][:build_command] - rsync = Settings.base[:rsync] - local_folder = File.expand_path Settings.local[:folder] - local_temp = File.expand_path Settings.local[:temp] + ssh = @config.base(:ssh) + build_command = @config.build(:command) + rsync = @config.base(:rsync) + local_folder = File.expand_path @config.local(:folder) + local_temp = File.expand_path @config.local(:temp) build_command = build_command.gsub('%f', build[:folder]). - gsub('%t', Settings.build[:target]). + gsub('%t', @config.build(:target)). gsub('%p', @package.to_s). gsub('%T', type.to_s) upload_sources("#{local_folder}/", "#{build[:user]}@#{build[:host]}:#{build[:folder]}") command("#{ssh} #{build[:user]}@#{build[:host]} \"#{build_command}\"") @@ -101,71 +84,63 @@ puts "Receiving target #{p.to_s} for #{t.to_s}" command("#{rsync} -az #{build[:user]}@#{build[:host]}:#{build[:target]}/#{t} #{local_temp}") end end - def run_tests(env) - # for tests we will use aprox the same setup as for build - test = {} - [:user, :machine, :command, :folder].each do |k| - unless Settings.tests[env].has_key? k - puts("Undefined key 'tests:#{env}:#{k.to_s}:'") - exit! 1 - end - test[k] = Settings.tests[env][k] - end - ssh = Settings.base[:ssh] - - test[:command] = test[:command].gsub('%f', test[:folder]). - gsub('%t', Settings.build[:target]). - gsub('%p', @package.to_s) - - local_folder = File.expand_path Settings.local[:folder] #take the sources from the local folder - - upload_sources("#{local_folder}/", "#{test[:user]}@#{test[:machine]}:#{test[:folder]}") # upload them to the test machine - - puts "Running all tests for the #{env} environment" - puts "This will take some time and you have no output" - command("#{ssh} #{test[:user]}@#{test[:machine]} \"#{test[:command]}\"") - rescue => ee - puts "Unknown exception during parsing config file" - puts "Aborting (#{ee})" - exit! 1 - end - - def install (environment) - puts "Installing #{@package} in #{environment} environment." - local_temp = Settings.local[:temp] - sudo = Settings.base[:sudo] - scp = Settings.base[:scp] - type = Settings.install[:environments][environment][:type].to_sym + def install + env = @config.env + puts "Installing #{@package} in #{env} environment." + local_temp = @config.local(:temp) + sudo = @config.base(:sudo) + scp = @config.base(:scp) + type = @config.type install = {} [:user, :host, :folder].each do |k| - unless Settings.install[:environments][environment].has_key? k - puts "Undefined key 'install:#{environment.to_s}:#{k.to_s}'" - exit! 1 - end - install[k] = Settings.install[:environments][environment][k] + install[k] = @config.install(k) end - install[:host] = [install[:host]] unless Array === install[:host] - case type - when :deb - install[:host].each do |host| - command("#{scp} #{local_temp}/#{@package_files[type][:deb]} #{install[:user]}@#{host}:#{install[:folder]}") - command("#{sudo} #{install[:user]}@#{host} /usr/bin/dpkg -i #{install[:folder]}/#{@package_files[type][:deb]}") - end - else - puts "unknown package type '#{type.to_s}'" - exit! 1 + when :deb + install[:host].each do |host| + command("#{scp} #{local_temp}/#{@package_files[type][:deb]} #{install[:user]}@#{host}:#{install[:folder]}") + command("#{sudo} #{install[:user]}@#{host} /usr/bin/dpkg -i #{install[:folder]}/#{@package_files[type][:deb]}") + end + else + exit! "unknown package type '#{type.to_s}'" end end + def run_tests + # check if we have the test section in the configuration file + unless @config.tests + puts "No test section in the config file." + puts "Skipping tests" + return; + end + # for tests we will use almost the same setup as for build + test = {} + [:user, :machine, :command, :folder].each do |k| + test[k] = @config.(k) + end + ssh = @config.base(:ssh) + # replace "variables" in commands + test[:command] = test[:command]. + gsub('%f', test[:folder]). # %f is the folder where the sources are rsync-ed + gsub('%t', @config.build(:target)). # %t is the folder where the build places the result + gsub('%p', @package.to_s) # %p is the package name + # take the sources from the local folder + local_folder = File.expand_path @config.local(:folder) + # upload them to the test machine + upload_sources("#{local_folder}/", "#{test[:user]}@#{test[:machine]}:#{test[:folder]}") + puts "Running all tests" + puts 'This will take some time and you have no output' + command("#{ssh} #{test[:user]}@#{test[:machine]} \"#{test[:command]}\"") + end + def upload_sources (source, dest) - rsync = Settings.base[:rsync] + rsync = @config.base(:rsync) command("#{rsync} -az #{source} #{dest}") end end -end +end