add unix socket for postgres
This commit is contained in:
parent
2054a68af0
commit
2289379ad5
9 changed files with 32 additions and 29 deletions
83
lib/setup/postgres.rb
Normal file
83
lib/setup/postgres.rb
Normal file
|
@ -0,0 +1,83 @@
|
|||
|
||||
module Setup
|
||||
require_relative '../templates'
|
||||
require_relative "../user"
|
||||
require_relative "../execute"
|
||||
# dat execute postgres
|
||||
# example.rcp
|
||||
|
||||
module PostgreSQL
|
||||
extend Templates
|
||||
|
||||
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
|
||||
|
||||
|
||||
# attr_accessor :te
|
||||
|
||||
def self.init_db(context)
|
||||
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 postgres #{File.join(context.bin_dir, "/bin/initdb")} -D #{context.data_dir} --username=postgres")
|
||||
end
|
||||
|
||||
def self.setup_systemd(context)
|
||||
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)
|
||||
|
||||
## 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"
|
||||
|
||||
system("sudo mkdir -p #{socket_path}")
|
||||
system("sudo chown #{context.user_name}:services #{socket_path}")
|
||||
system("sudo chmod 711 #{socket_path}")
|
||||
|
||||
postgresql_conf = render("postgresql.conf", unix_socket: socket_path)
|
||||
|
||||
postgresql_conf_path = "#{File.join(context.data_dir, "postgresql.conf")}"
|
||||
write_as(context.user_name, postgresql_conf_path, postgresql_conf)
|
||||
|
||||
postgres_service = render(
|
||||
"postgres.service",
|
||||
postgres_bin: File.join(context.bin_dir, "/bin/postgres"),
|
||||
version: context.version,
|
||||
database_dir: context.data_dir
|
||||
)
|
||||
postgres_service_path = "/etc/systemd/system/postgres.service"
|
||||
write_as("root", postgres_service_path, postgres_service)
|
||||
system("sudo systemctl daemon-reexec")
|
||||
system("sudo systemctl daemon-reload")
|
||||
system("sudo systemctl enable postgres")
|
||||
system("sudo systemctl start postgres")
|
||||
|
||||
# debug service
|
||||
# sudo systemctl daemon-reexec && sudo systemctl daemon-reload && sudo systemctl restart postgresql.service
|
||||
end
|
||||
|
||||
def self.install(context)
|
||||
user_context = Execute::UserInstallContext.new(context.user_name, :service)
|
||||
User.install(user_context)
|
||||
init_db(context)
|
||||
setup_systemd(context)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue