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, #run, #set_container_id, #status, #stop

Methods included from Configurable

#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



139
140
141
# File 'lib/deployku/plugins/postgres.rb', line 139

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



87
88
89
90
91
92
93
# File 'lib/deployku/plugins/postgres.rb', line 87

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_create(name, db_name) ⇒ Object



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

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 \"#{@config['env']['POSTGRES_PASSWORD']}\n\" 'CREATE DATABASE #{db};' | psql -h #{ip} -U postgres"
end

#db_drop(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_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"
end


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

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 \"#{@config['env']['POSTGRES_PASSWORD']}\n\" \"CREATE USER #{user_name} WITH PASSWORD '#{user_passwd}';\" | psql -h #{ip} -U postgres"
  system "echo \"#{@config['env']['POSTGRES_PASSWORD']}\n\" 'GRANT ALL ON DATABASE #{db} TO #{user_name};' | 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
# File 'lib/deployku/plugins/postgres.rb', line 26

def delete(name)
  app_dir = dir(name)
  if Dir.exists?(app_dir)
    FileUtils.rm_rf(app_dir)
  end
end

#dir(name) ⇒ Object



135
136
137
# File 'lib/deployku/plugins/postgres.rb', line 135

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

#listObject



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

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



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

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

#start(app_name) ⇒ Object



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

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_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