module Execute end require 'rubygems' require 'ostruct' # dat execute progress # dat execute < default Configfile # dat execute --dry-run # Execute single command # dat command service zulip module Execute attr_accessor :options def context(new_value = nil) @options = new_value end class ServiceInstallContext attr_accessor :bin_dir, :data_dir, :version, :user_name, :forced def initialize(bin_dir, data_dir, user_name, version, forced = false) @bin_dir = bin_dir @data_dir = data_dir @user_name = user_name @version = version @forced = forced end end class UserInstallContext attr_accessor :user_name, :type, :can_login def initialize(user_name, type = :user, can_login = nil) @user_name = user_name @type = type if can_login == nil case type when :service @can_login = false when :user @can_login = true else raise "Can not create user for type: #{type}" end else @can_login = can_login end end end def dependency(name) puts "Checking for #{name}..." unless gem_installed?(name) puts "Installing #{name}..." system("gem install #{name}") else puts "#{name} is already installed." end require name rescue LoadError => e puts "Failed to load #{name}: #{e.message}" end def gem_installed?(name) Gem::Specification::find_all_by_name(name).any? end def get_install_executor(name) require 'make' pdata = Make.context(name) bin_dir = pdata.get_prefix data_dir = "/data/#{pdata.name}/#{pdata.version.split(".").first}" service_install_context = ServiceInstallContext.new( bin_dir, data_dir, name, pdata.version, @options.forced ) case name when :postgresql require 'setup/postgresql' -> { Setup::PostgreSQL.install(service_install_context) } # ->(context) { # Setup::PostgreSQL.install(context) # } else raise "Can't find the executor" end end def execute_with_context(context) end # def postgres # require 'setup/postgresql' # Setup::PostgreSQL.install # end def service(name) executor = get_install_executor(name) #executor.call(service_install_context) executor.call exit -1 if block_given? context = OpenStruct.new # service context output = yield context puts output puts "context: #{context}" else puts "No block provided" end end def user(*users) require 'user' users.each do |name| context = UserInstallContext.new(name, :user) User.install(context) end end def install(*packages) packages.each do |pkg| puts "Installing #{pkg}" end end def self.file(options) dsl = Object.new dsl.extend(Execute) dsl.options = options # Configfile path = File.join(Dir.pwd, options.name || "./Configfile") dsl.instance_eval(File.read(path), path) end end