environment/lib/setup/postgresql.rb

71 lines
2.2 KiB
Ruby
Raw Normal View History

2025-08-06 16:50:03 +02:00
module Setup
require_relative '../templates'
2025-08-07 16:29:11 +02:00
require_relative "../user"
require_relative "../execute"
2025-08-07 14:23:42 +02:00
# dat execute postgres
# example.rcp
2025-08-06 16:50:03 +02:00
module PostgreSQL
extend Templates
2025-08-07 19:41:04 +02:00
def self.write_as(user, path, content)
puts "wriiting by #{user} to #{path}"
# If executed as root we can just use File.write
# File.write(postgresql_conf_path, postgresql_conf_content)
IO.popen(["sudo", "-u", user.to_s, "tee", path], "w") do |io|
io.write(content)
end
end
2025-08-07 13:02:01 +02:00
2025-08-06 16:50:03 +02:00
# attr_accessor :te
2025-08-07 14:23:42 +02:00
def self.init_db(context)
2025-08-07 17:36:47 +02:00
puts "Mode, isForced #{context.forced}"
if Dir.exist?(context.data_dir)
if context.forced
puts("sudo rm -rf #{context.data_dir}")
else
raise "PostgreSQL data already exists"
end
end
system("sudo mkdir -p #{context.data_dir}")
system("sudo chown #{context.user_name}:services #{context.data_dir}")
system("sudo -u postgresql #{File.join(context.bin_dir, "/bin/initdb")} -D #{context.data_dir}")
end
def self.setup_systemd(context)
2025-08-07 19:41:04 +02:00
puts "DataDir: #{context.data_dir}"
pg_hba = render("pg_hba.conf")
pg_hba_path = "#{File.join(context.data_dir, "pg_hba.conf")}"
write_as(context.user_name, pg_hba_path, pg_hba)
2025-08-07 17:36:47 +02:00
2025-08-07 19:41:04 +02:00
postgresql_conf = render("postgresql.conf")
postgresql_conf_path = "#{File.join(context.data_dir, "postgresql.conf")}"
write_as(context.user_name, postgresql_conf_path, postgresql_conf)
2025-08-06 16:50:03 +02:00
2025-08-07 19:41:04 +02:00
postgres_service = render(
2025-08-07 13:02:01 +02:00
"postgresql.service",
2025-08-07 14:23:42 +02:00
postgres_bin: File.join(context.bin_dir, "/bin/postgres"),
version: context.version,
database_dir: context.data_dir
2025-08-07 19:41:04 +02:00
)
postgres_service_path = "/etc/systemd/system/postgresql.service"
write_as("root", postgres_service_path, postgres_service)
2025-08-07 14:23:42 +02:00
2025-08-07 19:41:04 +02:00
system("sudo systemctl daemon-reload")
system("sudo systemctl enable postgresql")
system("sudo systemctl start postgresql")
end
def self.install(context)
2025-08-07 16:29:11 +02:00
user_context = Execute::UserInstallContext.new(context.user_name, :service)
User.install(user_context)
2025-08-07 17:36:47 +02:00
init_db(context)
setup_systemd(context)
2025-08-06 16:50:03 +02:00
end
end
end