Class: Deployku::PostgresPlugin

Inherits:
Plugin
  • Object
show all
Includes:
Configurable, Containerable
Defined in:
lib/deployku/plugins/postgres.rb

Instance Attribute Summary

Attributes included from Configurable

#config

Instance Method Summary collapse

Methods included from Containerable

#container_id_path, #get_container_id, #get_status, #logs, #set_container_id, #status, #stop

Methods included from Configurable

#config_get, #config_load, #config_path, #config_save, #config_set, #config_set_engine, #config_set_from, #config_set_port, #config_show, #config_unset, #config_unset_engine, #config_unset_from, #config_unset_port

Methods inherited from Plugin

<<, command_description, filter_plugins, find_plugin, help, inherited, instance, #packages, run

Constructor Details

#initializePostgresPlugin

Returns a new instance of PostgresPlugin.



9
10
11
12
13
14
# File 'lib/deployku/plugins/postgres.rb', line 9

def initialize
  @config = {
    'from' => 'postgres',
    'env' => {}
  }
end

Instance Method Details

#container_name(app_name) ⇒ Object



189
190
191
# File 'lib/deployku/plugins/postgres.rb', line 189

def container_name(app_name)
  "deployku-postgres-#{Deployku.sanitize_app_name(app_name)}"
end

#create(name) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/deployku/plugins/postgres.rb', line 17

def create(name)
  app_dir = dir(name)
  unless Dir.exists?(app_dir)
    FileUtils.mkdir_p(app_dir)
  end
  app_dir
end

#db_connect(name, db_name) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/deployku/plugins/postgres.rb', line 115

def db_connect(name, db_name)
  config_load(name)
  db_id = get_container_id(name)
  dbname = Deployku.sanitize_app_name(db_name)
  ip = Deployku::Engine.ip(db_id)
  system "PGPASSWORD=\"#{@config['env']['POSTGRES_PASSWORD']}\" psql -h #{ip} -U postgres #{dbname}"
end

#db_connect_app(name, app_name) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/deployku/plugins/postgres.rb', line 124

def db_connect_app(name, app_name)
  config_load(name)
  db_id = get_container_id(name)
  ip = Deployku::Engine.ip(db_id)
  database_url = Deployku::AppPlugin.instance.config_get(app_name, 'DATABASE_URL')
  if database_url =~ /postgres:\/\/([^:]+):([^@]+)@[^\/]+\/([^\/]+)/
    user_name, password, dbname = $1, $2, $3
    system "PGPASSWORD=\"#{password}\" psql -h #{ip} -U #{user_name} #{dbname}"
  else
    puts "Wrong DATABASE_URL: #{database_url}"
  end
end

#db_create(name, db_name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/deployku/plugins/postgres.rb', line 67

def db_create(name, db_name)
  config_load(name)
  cid = get_container_id(name)
  unless Deployku::Engine.running?(cid)
    puts "Database instance '#{name}' is not running."
    exit 1
  end
  ip = Deployku::Engine.ip(cid)
  db = Deployku.sanitize_app_name(db_name)
  system "echo 'CREATE DATABASE #{db};' | PGPASSWORD=\"#{@config['env']['POSTGRES_PASSWORD']}\" psql -h #{ip} -U postgres"
  #system "echo \"#{@config['env']['POSTGRES_PASSWORD']}\n\" 'CREATE DATABASE #{db};' | psql -h #{ip} -U postgres"
end

#db_drop(name, db_name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/deployku/plugins/postgres.rb', line 81

def db_drop(name, db_name)
  config_load(name)
  cid = get_container_id(name)
  unless Deployku::Engine.running?(cid)
    puts "Database instance '#{name}' is not running."
    exit 1
  end
  ip = Deployku::Engine.ip(cid)
  db = Deployku.sanitize_app_name(db_name)
  #system "echo \"#{@config['env']['POSTGRES_PASSWORD']}\n\" 'DROP DATABASE #{db};' | psql -h #{ip} -U postgres"
  system "echo 'DROP DATABASE #{db};' | PGPASSWORD=\"#{@config['env']['POSTGRES_PASSWORD']}\" psql -h #{ip} -U postgres"
end

#db_dump(name, db_name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/deployku/plugins/postgres.rb', line 54

def db_dump(name, db_name)
  config_load(name)
  cid = get_container_id(name)
  unless Deployku::Engine.running?(cid)
    puts "Database instance '#{name}' is not running."
    exit 1
  end
  ip = Deployku::Engine.ip(cid)
  db = Deployku.sanitize_app_name(db_name)
  system "PGPASSWORD=\"#{@config['env']['POSTGRES_PASSWORD']}\" pg_dump -h #{ip} -d #{db} -U postgres"
end


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/deployku/plugins/postgres.rb', line 95

def db_link(name, db_name, app_name)
  config_load(name)
  db_id = get_container_id(name)
  ip = Deployku::Engine.ip(db_id)
  db = Deployku.sanitize_app_name(db_name)
  user_name = 'user_' + SecureRandom.uuid.gsub('-','')
  user_passwd = SecureRandom.uuid
  database_url = "postgres://#{user_name}:#{user_passwd}@#{container_name(name)}/#{db}"
  system "echo \"CREATE USER #{user_name} WITH PASSWORD '#{user_passwd}';\" | PGPASSWORD=\"#{@config['env']['POSTGRES_PASSWORD']}\" psql -h #{ip} -U postgres"
  system "echo 'GRANT ALL ON DATABASE #{db} TO #{user_name};' | PGPASSWORD=\"#{@config['env']['POSTGRES_PASSWORD']}\" psql -h #{ip} -U postgres"
  if $?.exitstatus == 0
    Deployku::AppPlugin.run('config:set', [app_name, 'DATABASE_URL', database_url])
    Deployku::AppPlugin.run(:link, [app_name, container_name(name)])
  else
    puts "Unable to create user in database."
    exit 1
  end
end

#delete(name) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/deployku/plugins/postgres.rb', line 26

def delete(name)
  app_dir = dir(name)
  if Dir.exists?(app_dir)
    puts "removing: #{app_dir}"
    FileUtils.rm_rf(app_dir)
  end
end

#dir(name) ⇒ Object



185
186
187
# File 'lib/deployku/plugins/postgres.rb', line 185

def dir(name)
  File.join(Deployku::Config.home, '.postgres', Deployku.sanitize_app_name(name))
end

#dumpall(name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/deployku/plugins/postgres.rb', line 42

def dumpall(name)
  config_load(name)
  cid = get_container_id(name)
  unless Deployku::Engine.running?(cid)
    puts "Database instance '#{name}' is not running."
    exit 1
  end
  ip = Deployku::Engine.ip(cid)
  system "PGPASSWORD=\"#{@config['env']['POSTGRES_PASSWORD']}\" pg_dumpall -h #{ip} -U postgres"
end

#listObject



35
36
37
38
39
# File 'lib/deployku/plugins/postgres.rb', line 35

def list
  Dir.glob(File.join(Deployku::Config.home, '.postgres', '*')) do |path|
    puts File.basename(path) if File.directory?(path)
  end
end

#restart(app_name) ⇒ Object



168
169
170
171
172
# File 'lib/deployku/plugins/postgres.rb', line 168

def restart(app_name)
  # restart can not be done seamlessly because we use named containers
  stop(app_name)
  start(app_name)
end

#run(app_name, *cmd) ⇒ Object



159
160
161
162
163
# File 'lib/deployku/plugins/postgres.rb', line 159

def run(app_name, *cmd)
  config_load(app_name)
  Deployku::Config.merge!(@config)
  Deployku::Engine.run(@config['from'], dir(app_name), *cmd)
end

#start(app_name) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/deployku/plugins/postgres.rb', line 139

def start(app_name)
  config_load(app_name)
  unless @config['volumes']
    @config['volumes'] = {
      File.join(dir(app_name), 'data') => '/postgresql/'
    }
  end
  @config['env']['PGDATA'] = '/postgresql/' unless @config['env']['PGDATA']
  @config['env']['POSTGRES_PASSWORD'] = SecureRandom.uuid unless @config['env']['POSTGRES_PASSWORD']
  @config['env']['PGPASSWORD'] = @config['env']['POSTGRES_PASSWORD']
  config_save(app_name)

  Deployku::Config.merge!(@config)
  app_hash = Deployku::Engine.start(@config['from'], dir(app_name), container_name(app_name))
  exit 1 if $?.nil? || $?.exitstatus != 0
  set_container_id(app_name, container_name(app_name))
  puts "Container #{app_hash} started."
end