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, #restart, #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.



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

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

Instance Method Details

#container_name(app_name) ⇒ Object



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

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

#create(name) ⇒ Object



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

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



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

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



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

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



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

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


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

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



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

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

#dir(name) ⇒ Object



131
132
133
# File 'lib/deployku/plugins/postgres.rb', line 131

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

#listObject



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

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

#start(app_name) ⇒ Object



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

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