mimas-postgresql 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/mimas/plugins/postgresql/commands/configure.rb +25 -0
- data/lib/mimas/plugins/postgresql/commands/console.rb +22 -0
- data/lib/mimas/plugins/postgresql/commands/create_db.rb +38 -0
- data/lib/mimas/plugins/postgresql/commands/create_role.rb +31 -0
- data/lib/mimas/plugins/postgresql/commands/install.rb +25 -0
- data/lib/mimas/plugins/postgresql/commands/setup.rb +26 -0
- data/lib/mimas/plugins/postgresql/version.rb +7 -0
- data/lib/mimas/plugins/postgresql.rb +23 -0
- data/lib/mimas/plugins/server/console.rb +14 -0
- data/lib/mimas/plugins/server/postgresql.rb +40 -0
- data/lib/mimas/plugins/templates/postgresql/conf.d/01-mimas.conf +6 -0
- data/lib/mimas/plugins/templates/postgresql/pg_hba.conf +17 -0
- data/lib/mimas/plugins/templates/postgresql/pg_hba.d/99-mimas.conf +5 -0
- data/lib/mimas-postgresql.rb +15 -0
- metadata +67 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: '082013adafb7795ab1e364ae00e5c092f4b75c1485745fe1e2cd74e0c5ab2768'
|
|
4
|
+
data.tar.gz: '0922ae80ccfb5863bf8bbfcf8f2f61a5b67ae46591490c9feb17d89299f1b7e2'
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 81f3a5e2ef7ae98132a7d2cb9c915d5b1de0ee59951bf8b07c0dba2f1516084b642d524fcacdd61e9b7be8f43a8a11dae00400950d6c9eee70d01a788d9d3a3e
|
|
7
|
+
data.tar.gz: 7edc8d7066a4ac96ebe2b5dca9838aca15944b3e9d30c1a7941dcf0a60f6bc003277e16f9f1d369fd8b9dba12d98f3f7fcafb14b6874bffd1dc77783c00ccdde
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module Mimas
|
|
2
|
+
module Plugins
|
|
3
|
+
module PostgreSQL
|
|
4
|
+
module Commands
|
|
5
|
+
class Configure < Mimas::CLI::Commands::BaseCommand
|
|
6
|
+
desc "Install configuration files for PostgreSQL."
|
|
7
|
+
|
|
8
|
+
argument :server_name, required: true, desc: "The name of the server to install PostgreSQL on"
|
|
9
|
+
|
|
10
|
+
option :user, default: "admin", aliases: ["-u"], desc: "A user with sudo access"
|
|
11
|
+
|
|
12
|
+
def call(server_name:, **options)
|
|
13
|
+
server = Config.current.servers[server_name.to_sym]
|
|
14
|
+
sudo_user = options[:user]
|
|
15
|
+
|
|
16
|
+
server.configure_postgresql(sudo_user: sudo_user)
|
|
17
|
+
|
|
18
|
+
say ""
|
|
19
|
+
say "PostgreSQL has been configured! 🎉"
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module Mimas
|
|
2
|
+
module Plugins
|
|
3
|
+
module PostgreSQL
|
|
4
|
+
module Commands
|
|
5
|
+
class Console < Mimas::CLI::Commands::BaseCommand
|
|
6
|
+
desc "Start the PostgreSQL console."
|
|
7
|
+
|
|
8
|
+
argument :server_name, required: true, desc: "The name of the server to install PostgreSQL on"
|
|
9
|
+
|
|
10
|
+
option :user, default: "admin", aliases: ["-u"], desc: "A user with sudo access"
|
|
11
|
+
|
|
12
|
+
def call(server_name:, **options)
|
|
13
|
+
server = Config.current.servers[server_name.to_sym]
|
|
14
|
+
sudo_user = options[:user]
|
|
15
|
+
|
|
16
|
+
server.start_postgresql_console(sudo_user: sudo_user)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module Mimas
|
|
2
|
+
module Plugins
|
|
3
|
+
module PostgreSQL
|
|
4
|
+
module Commands
|
|
5
|
+
class CreateDB < Mimas::CLI::Commands::BaseCommand
|
|
6
|
+
desc "Create a new database on the server."
|
|
7
|
+
|
|
8
|
+
argument :db_name, required: true, desc: "The name of the database to create"
|
|
9
|
+
argument :server_name, required: true, desc: "The name of the server to install PostgreSQL on"
|
|
10
|
+
|
|
11
|
+
option :user, default: "admin", aliases: ["-u"], desc: "A user with sudo access"
|
|
12
|
+
option :owner, aliases: ["-o"], desc: "The owner of the database. Omit to create a new role named after the database"
|
|
13
|
+
|
|
14
|
+
def call(server_name:, db_name:, **options)
|
|
15
|
+
server = Config.current.servers[server_name.to_sym]
|
|
16
|
+
sudo_user = options[:user]
|
|
17
|
+
|
|
18
|
+
ssh(server, user: sudo_user) do |session|
|
|
19
|
+
unless options[:owner]
|
|
20
|
+
password = SecureRandom.alphanumeric(64)
|
|
21
|
+
session.run "sudo -u postgres psql -c \"CREATE ROLE #{db_name} WITH LOGIN PASSWORD \'#{password}\';\"", capture: true
|
|
22
|
+
|
|
23
|
+
say ""
|
|
24
|
+
say "Created a new role #{db_name.dup.bold.magenta} with password: #{password.bold.white}"
|
|
25
|
+
say "Ensure you save this password as it cannot be displayed again."
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
session.run "sudo -u postgres psql -c \"CREATE DATABASE #{db_name} OWNER #{db_name};\"", capture: true
|
|
29
|
+
|
|
30
|
+
say ""
|
|
31
|
+
say "Created database named #{db_name.dup.bold.magenta}"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module Mimas
|
|
2
|
+
module Plugins
|
|
3
|
+
module PostgreSQL
|
|
4
|
+
module Commands
|
|
5
|
+
class CreateRole < Mimas::CLI::Commands::BaseCommand
|
|
6
|
+
desc "Create a new PostgreSQL role."
|
|
7
|
+
|
|
8
|
+
argument :role_name, required: true, desc: "The name of the role to create"
|
|
9
|
+
argument :server_name, required: true, desc: "The name of the server to install PostgreSQL on"
|
|
10
|
+
|
|
11
|
+
option :user, default: "admin", aliases: ["-u"], desc: "A user with sudo access"
|
|
12
|
+
option :password, aliases: ["-p"], desc: "The password for the role. Leave blank to autogenerate a secure password"
|
|
13
|
+
|
|
14
|
+
def call(server_name:, role_name:, **options)
|
|
15
|
+
server = Config.current.servers[server_name.to_sym]
|
|
16
|
+
sudo_user = options[:user]
|
|
17
|
+
|
|
18
|
+
password = options[:password] || SecureRandom.alphanumeric(64)
|
|
19
|
+
ssh(server, user: sudo_user) do |session|
|
|
20
|
+
session.run "sudo -u postgres psql -c \"CREATE ROLE #{role_name} WITH LOGIN PASSWORD \'#{password}\';\"", capture: true
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
say ""
|
|
24
|
+
say "Created a new role #{role_name.dup.bold.magenta} with password: #{password.bold.white}"
|
|
25
|
+
say "Ensure you save this password as it cannot be displayed again."
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module Mimas
|
|
2
|
+
module Plugins
|
|
3
|
+
module PostgreSQL
|
|
4
|
+
module Commands
|
|
5
|
+
class Install < Mimas::CLI::Commands::BaseCommand
|
|
6
|
+
desc "Install PostgreSQL on the server."
|
|
7
|
+
|
|
8
|
+
argument :server_name, required: true, desc: "The name of the server to install PostgreSQL on"
|
|
9
|
+
|
|
10
|
+
option :user, default: "admin", aliases: ["-u"], desc: "A user with sudo access"
|
|
11
|
+
|
|
12
|
+
def call(server_name:, **options)
|
|
13
|
+
server = Config.current.servers[server_name.to_sym]
|
|
14
|
+
sudo_user = options[:user]
|
|
15
|
+
|
|
16
|
+
server.install_postgresql(sudo_user: sudo_user)
|
|
17
|
+
|
|
18
|
+
say ""
|
|
19
|
+
say "PostgreSQL has been installed! 🎉"
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module Mimas
|
|
2
|
+
module Plugins
|
|
3
|
+
module PostgreSQL
|
|
4
|
+
module Commands
|
|
5
|
+
class Setup < Mimas::CLI::Commands::BaseCommand
|
|
6
|
+
desc "Install and configure PostgreSQL on the server."
|
|
7
|
+
|
|
8
|
+
argument :server_name, required: true, desc: "The name of the server to install PostgreSQL on"
|
|
9
|
+
|
|
10
|
+
option :user, default: "admin", aliases: ["-u"], desc: "A user with sudo access"
|
|
11
|
+
|
|
12
|
+
def call(server_name:, **options)
|
|
13
|
+
server = Config.current.servers[server_name.to_sym]
|
|
14
|
+
sudo_user = options[:user]
|
|
15
|
+
|
|
16
|
+
server.install_postgresql(sudo_user: sudo_user)
|
|
17
|
+
server.configure_postgresql(sudo_user: sudo_user)
|
|
18
|
+
|
|
19
|
+
say ""
|
|
20
|
+
say "PostgreSQL has been installed and configured! 🎉"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module Mimas
|
|
2
|
+
module Plugins
|
|
3
|
+
module PostgreSQL
|
|
4
|
+
extend self
|
|
5
|
+
|
|
6
|
+
def register_commands(cli)
|
|
7
|
+
cli.register "postgresql" do |pg|
|
|
8
|
+
pg.register "install", Commands::Install
|
|
9
|
+
pg.register "console", Commands::Console
|
|
10
|
+
pg.register "configure", Commands::Configure
|
|
11
|
+
pg.register "setup", Commands::Setup
|
|
12
|
+
pg.register "create_db", Commands::CreateDB
|
|
13
|
+
pg.register "create_role", Commands::CreateRole
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
TEMPLATES_DIR = Pathname.new(__dir__).join("templates")
|
|
18
|
+
def templates_dir
|
|
19
|
+
TEMPLATES_DIR
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module Mimas
|
|
2
|
+
module Plugins
|
|
3
|
+
module Server
|
|
4
|
+
module PostgreSQLConsole
|
|
5
|
+
def start_postgresql_console(sudo_user:)
|
|
6
|
+
command = "sudo -u postgres psql"
|
|
7
|
+
interactive_ssh(host, user: sudo_user, command: command)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
::Mimas::Server.include Mimas::Plugins::Server::PostgreSQLConsole
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
module Mimas
|
|
2
|
+
module Plugins
|
|
3
|
+
module Server
|
|
4
|
+
module PostgreSQL
|
|
5
|
+
def install_postgresql(sudo_user:)
|
|
6
|
+
ssh(self, user: sudo_user) do |session|
|
|
7
|
+
session.run "sudo apt-get install postgresql postgresql-contrib -y"
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def configure_postgresql(sudo_user:)
|
|
12
|
+
ssh(self, user: sudo_user) do |session|
|
|
13
|
+
config_file_path = session.run "sudo -u postgres psql -AtXc 'SHOW config_file'", capture: true
|
|
14
|
+
config_dir = Pathname.new(config_file_path.strip).parent
|
|
15
|
+
|
|
16
|
+
config_files = find_files("postgresql/**/*.conf")
|
|
17
|
+
config_files = config_files.map do |path|
|
|
18
|
+
[ path, path.split("/postgresql/")[1] ]
|
|
19
|
+
end.to_h
|
|
20
|
+
|
|
21
|
+
tmp_dir = Pathname.new("/").join("tmp", "postgresql")
|
|
22
|
+
|
|
23
|
+
config_files.each do |file, destination|
|
|
24
|
+
destination_parent_folder = Pathname.new(destination).parent
|
|
25
|
+
|
|
26
|
+
session.run "mkdir -p #{tmp_dir.join(destination_parent_folder)}"
|
|
27
|
+
session.upload file, tmp_dir.join(destination)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
session.run "sudo rsync --archive --recursive --chown=postgres:postgres #{tmp_dir.join("*")} #{config_dir}"
|
|
31
|
+
session.run "rm -rf #{tmp_dir}"
|
|
32
|
+
session.run "sudo systemctl restart postgresql"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
::Mimas::Server.include Mimas::Plugins::Server::PostgreSQL
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# PostgreSQL Client Authentication Configuration File
|
|
2
|
+
# ===================================================
|
|
3
|
+
#
|
|
4
|
+
# Refer to the "Client Authentication" section in the PostgreSQL
|
|
5
|
+
# documentation for a complete description of this file.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
# DO NOT DISABLE!
|
|
9
|
+
# If you change this first entry you will need to make sure that the
|
|
10
|
+
# database superuser can access the database using some other method.
|
|
11
|
+
# Noninteractive access to all databases is required during automatic
|
|
12
|
+
# maintenance (custom daily cronjobs, replication, and similar tasks).
|
|
13
|
+
#
|
|
14
|
+
# Database administrative login by Unix domain socket
|
|
15
|
+
local all postgres peer
|
|
16
|
+
|
|
17
|
+
include_dir pg_hba.d
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require "mimas"
|
|
2
|
+
require "securerandom"
|
|
3
|
+
|
|
4
|
+
require_relative "mimas/plugins/postgresql"
|
|
5
|
+
require_relative "mimas/plugins/server/postgresql"
|
|
6
|
+
require_relative "mimas/plugins/server/console"
|
|
7
|
+
|
|
8
|
+
require_relative "mimas/plugins/postgresql/commands/install"
|
|
9
|
+
require_relative "mimas/plugins/postgresql/commands/console"
|
|
10
|
+
require_relative "mimas/plugins/postgresql/commands/configure"
|
|
11
|
+
require_relative "mimas/plugins/postgresql/commands/setup"
|
|
12
|
+
require_relative "mimas/plugins/postgresql/commands/create_db"
|
|
13
|
+
require_relative "mimas/plugins/postgresql/commands/create_role"
|
|
14
|
+
|
|
15
|
+
Mimas::Plugins.register(Mimas::Plugins::PostgreSQL)
|
metadata
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mimas-postgresql
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ayush Newatia
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: mimas
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0.5'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0.5'
|
|
26
|
+
email: ayush@hey.com
|
|
27
|
+
executables: []
|
|
28
|
+
extensions: []
|
|
29
|
+
extra_rdoc_files: []
|
|
30
|
+
files:
|
|
31
|
+
- lib/mimas-postgresql.rb
|
|
32
|
+
- lib/mimas/plugins/postgresql.rb
|
|
33
|
+
- lib/mimas/plugins/postgresql/commands/configure.rb
|
|
34
|
+
- lib/mimas/plugins/postgresql/commands/console.rb
|
|
35
|
+
- lib/mimas/plugins/postgresql/commands/create_db.rb
|
|
36
|
+
- lib/mimas/plugins/postgresql/commands/create_role.rb
|
|
37
|
+
- lib/mimas/plugins/postgresql/commands/install.rb
|
|
38
|
+
- lib/mimas/plugins/postgresql/commands/setup.rb
|
|
39
|
+
- lib/mimas/plugins/postgresql/version.rb
|
|
40
|
+
- lib/mimas/plugins/server/console.rb
|
|
41
|
+
- lib/mimas/plugins/server/postgresql.rb
|
|
42
|
+
- lib/mimas/plugins/templates/postgresql/conf.d/01-mimas.conf
|
|
43
|
+
- lib/mimas/plugins/templates/postgresql/pg_hba.conf
|
|
44
|
+
- lib/mimas/plugins/templates/postgresql/pg_hba.d/99-mimas.conf
|
|
45
|
+
homepage: https://codeberg.org/ayushn21/mimas-postgresql
|
|
46
|
+
licenses:
|
|
47
|
+
- MIT
|
|
48
|
+
metadata:
|
|
49
|
+
mimas_plugin: 'true'
|
|
50
|
+
rdoc_options: []
|
|
51
|
+
require_paths:
|
|
52
|
+
- lib
|
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: 3.4.0
|
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - ">="
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: '0'
|
|
63
|
+
requirements: []
|
|
64
|
+
rubygems_version: 4.0.18
|
|
65
|
+
specification_version: 4
|
|
66
|
+
summary: Install and manage PostgreSQL using Mimas
|
|
67
|
+
test_files: []
|