environment/lib/execute.rb

148 lines
3 KiB
Ruby
Raw Normal View History

2025-08-06 17:49:36 +02:00
module Execute
end
require 'rubygems'
require 'ostruct'
# dat execute progress
# dat execute < default Configfile
2025-08-07 13:02:01 +02:00
# dat execute --dry-run
# Execute single command
# dat command service zulip
2025-08-06 17:49:36 +02:00
module Execute
2025-08-07 17:36:47 +02:00
attr_accessor :options
def context(new_value = nil)
@options = new_value
end
2025-08-07 14:23:42 +02:00
class ServiceInstallContext
2025-08-07 17:36:47 +02:00
attr_accessor :bin_dir, :data_dir, :version, :user_name, :forced
2025-08-07 14:23:42 +02:00
2025-08-07 17:36:47 +02:00
def initialize(bin_dir, data_dir, user_name, version, forced = false)
2025-08-07 14:23:42 +02:00
@bin_dir = bin_dir
@data_dir = data_dir
@user_name = user_name
@version = version
2025-08-07 17:36:47 +02:00
@forced = forced
2025-08-07 14:23:42 +02:00
end
end
2025-08-07 16:29:11 +02:00
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
2025-08-07 14:23:42 +02:00
2025-08-06 17:49:36 +02:00
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
2025-08-07 14:23:42 +02:00
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(
2025-08-07 17:36:47 +02:00
bin_dir, data_dir, name, pdata.version, @options.forced
2025-08-07 14:23:42 +02:00
)
case name
when :postgresql
2025-08-06 17:49:36 +02:00
require 'setup/postgresql'
2025-08-07 14:23:42 +02:00
-> { Setup::PostgreSQL.install(service_install_context) }
# ->(context) {
# Setup::PostgreSQL.install(context)
# }
else
raise "Can't find the executor"
end
2025-08-06 17:49:36 +02:00
end
2025-08-07 14:23:42 +02:00
def execute_with_context(context)
end
# def postgres
# require 'setup/postgresql'
# Setup::PostgreSQL.install
# end
2025-08-06 17:49:36 +02:00
def service(name)
2025-08-07 14:23:42 +02:00
executor = get_install_executor(name)
#executor.call(service_install_context)
executor.call
exit -1
2025-08-06 17:49:36 +02:00
if block_given?
context = OpenStruct.new
2025-08-07 14:23:42 +02:00
# service context
2025-08-06 17:49:36 +02:00
output = yield context
puts output
puts "context: #{context}"
else
puts "No block provided"
end
end
2025-08-07 16:29:11 +02:00
def user(*users)
require 'user'
users.each do |name|
context = UserInstallContext.new(name, :user)
User.install(context)
end
2025-08-06 17:49:36 +02:00
end
def install(*packages)
packages.each do |pkg|
puts "Installing #{pkg}"
end
end
def self.file(options)
dsl = Object.new
dsl.extend(Execute)
2025-08-07 17:36:47 +02:00
dsl.options = options
2025-08-07 14:23:42 +02:00
2025-08-06 17:49:36 +02:00
# Configfile
path = File.join(Dir.pwd, options.name || "./Configfile")
dsl.instance_eval(File.read(path), path)
end
end