Class: Recipes::DatabaseContainer

Inherits:
Rails::AppBuilder
  • Object
show all
Defined in:
lib/potassium/recipes/database_container.rb

Constant Summary collapse

CONTAINER_VARS =
{
  postgresql: { port: 5432, user: 'postgres' },
  mysql: { port: 3306, user: 'root' }
}
POSTGRESQL_SERVICE =
<<~YAML
  image: postgres:#{Potassium::POSTGRES_VERSION}
  ports:
    - #{CONTAINER_VARS[:postgresql][:port]}
  environment:
    POSTGRES_USER: #{CONTAINER_VARS[:postgresql][:user]}
    POSTGRES_PASSWORD: ''
  volumes:
    - postgresql_data:/var/lib/postgresql/data
YAML
MYSQL_SERVICE =
<<~YAML
  image: "mysql:#{Potassium::MYSQL_VERSION}"
  ports:
    - #{CONTAINER_VARS[:mysql][:port]}
  environment:
    MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
  volumes:
    - mysql_data:/var/lib/mysql
YAML

Instance Method Summary collapse

Instance Method Details

#createObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/potassium/recipes/database_container.rb', line 30

def create
  db_type = get(:database)
  return if [:None, :none].include? db_type.to_sym

  copy_file '../assets/docker-compose.yml', 'docker-compose.yml'
  compose = DockerHelpers.new('docker-compose.yml')

  compose.add_service(db_type.to_s, self.class.const_get("#{db_type}_service".upcase))
  compose.add_volume("#{db_type}_data")
  template '../assets/Makefile.erb', 'Makefile'

  run "docker-compose up -d"

  set_env(db_type, CONTAINER_VARS[db_type][:port], CONTAINER_VARS[db_type][:user])
  set_dot_env(db_type, CONTAINER_VARS[db_type][:port], CONTAINER_VARS[db_type][:user])
end

#installObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/potassium/recipes/database_container.rb', line 47

def install
  database_config = YAML.safe_load(IO.read('config/database.yml'), [], [], true)
  database = database_config['development']['adapter'].gsub(/\d+/, '').to_sym
  set :database, database

  template "../assets/config/database_#{database}.yml.erb", 'config/database.yml'

  setup_text = # setup file is templated on project creation, manual install is needed
    <<~TEXT
      # Set up required services
      docker-compose up -d

    TEXT

  insert_into_file 'bin/setup', setup_text, before: "# Set up database"
  create
  run 'bin/setup'
  info "A new container with a #{get(:database)} database has been created."
end

#installed?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/potassium/recipes/database_container.rb', line 67

def installed?
  file_exist?("docker-compose.yml")
end