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}")
|
2025-08-08 10:17:01 +02:00
|
|
|
system("sudo -u postgres #{File.join(context.bin_dir, "/bin/initdb")} -D #{context.data_dir} --username=postgres")
|
2025-08-07 17:36:47 +02:00
|
|
|
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-08 10:17:01 +02:00
|
|
|
## TODO: move this to user module
|
|
|
|
#uid = Etc.getpwnam(context.user_name.to_s).uid
|
|
|
|
#socket_path = "/run/user/#{uid}"
|
|
|
|
socket_path = "/run/user/#{context.user_name}"
|
|
|
|
#socket_path = "/tmp"
|
|
|
|
|
2025-08-07 21:05:37 +02:00
|
|
|
system("sudo mkdir -p #{socket_path}")
|
|
|
|
system("sudo chown #{context.user_name}:services #{socket_path}")
|
2025-08-08 10:17:01 +02:00
|
|
|
system("sudo chmod 711 #{socket_path}")
|
2025-08-07 21:05:37 +02:00
|
|
|
|
|
|
|
postgresql_conf = render("postgresql.conf", unix_socket: socket_path)
|
|
|
|
|
2025-08-07 19:41:04 +02:00
|
|
|
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-08 10:17:01 +02:00
|
|
|
"postgres.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
|
|
|
)
|
2025-08-08 10:17:01 +02:00
|
|
|
postgres_service_path = "/etc/systemd/system/postgres.service"
|
2025-08-07 19:41:04 +02:00
|
|
|
write_as("root", postgres_service_path, postgres_service)
|
2025-08-07 21:05:37 +02:00
|
|
|
system("sudo systemctl daemon-reexec")
|
2025-08-07 19:41:04 +02:00
|
|
|
system("sudo systemctl daemon-reload")
|
2025-08-08 10:17:01 +02:00
|
|
|
system("sudo systemctl enable postgres")
|
|
|
|
system("sudo systemctl start postgres")
|
2025-08-07 21:05:37 +02:00
|
|
|
|
|
|
|
# debug service
|
|
|
|
# sudo systemctl daemon-reexec && sudo systemctl daemon-reload && sudo systemctl restart postgresql.service
|
2025-08-07 19:41:04 +02:00
|
|
|
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
|