Module: Gumboot::Strap

Defined in:
lib/gumboot/strap.rb

Instance Method Summary collapse

Instance Method Details

#clean_logsObject



73
74
75
76
# File 'lib/gumboot/strap.rb', line 73

def clean_logs
  message 'Removing old tempfiles'
  system 'rm -f log/*'
end

#clean_tempfilesObject



78
79
80
81
# File 'lib/gumboot/strap.rb', line 78

def clean_tempfiles
  message 'Removing old tempfiles'
  system 'rm -rf tmp/cache'
end

#clientObject



8
9
10
11
12
13
# File 'lib/gumboot/strap.rb', line 8

def client
  file = File.expand_path('~/.my.cnf')
  @client ||= Mysql2::Client.new(default_file: file,
                                 default_group: 'client',
                                 host: '127.0.0.1')
end

#create_and_grant_user(client, database, username, password) ⇒ Object



48
49
50
51
52
53
# File 'lib/gumboot/strap.rb', line 48

def create_and_grant_user(client, database, username, password)
  client.query("CREATE USER IF NOT EXISTS '#{client.escape(username)}'" \
               "@'localhost' IDENTIFIED BY '#{client.escape(password)}';")
  client.query("GRANT ALL PRIVILEGES ON `#{database}`.* " \
               "TO '#{client.escape(username)}'@'localhost'")
end

#ensure_activerecord_databases(environments) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/gumboot/strap.rb', line 15

def ensure_activerecord_databases(environments)
  environments.each do |env|
    message "Preparing #{env} database"

    db = ActiveRecord::Base.configurations[env]

    ensure_database(db)
    ensure_database_user(db)
  end
end

#ensure_database(dbase) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/gumboot/strap.rb', line 26

def ensure_database(dbase)
  adapter, database = dbase.values_at('adapter', 'database')
  raise('Only supports mysql2 adapter') unless adapter == 'mysql2'

  Rails.logger.info "Ensuring database `#{database}` exists"
  client.query("CREATE DATABASE IF NOT EXISTS `#{database}` " \
               'CHARACTER SET utf8 COLLATE utf8_bin')
end

#ensure_database_user(dbase) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gumboot/strap.rb', line 35

def ensure_database_user(dbase)
  adapter, database, username, password =
    dbase.values_at('adapter', 'database', 'username', 'password')

  raise('Only supports mysql2 adapter') unless adapter == 'mysql2'

  Rails.logger.info(
    "Ensuring access to `#{database}` for #{username} user is granted"
  )

  create_and_grant_user(client, database, username, password)
end

#install_dist_template(files) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gumboot/strap.rb', line 105

def install_dist_template(files)
  files.each do |file|
    src = "config/#{file}.dist"
    dest = "config/#{file}"

    raise("Missing dist config file: #{src}") unless File.exist?(src)

    next if File.exist?(dest)

    FileUtils.copy(src, dest)
  end
end


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gumboot/strap.rb', line 83

def link_global_configuration(files)
  files.each do |file|
    src = File.expand_path("~/.aaf/#{file}")
    raise("Missing global config file: #{src}") unless File.exist?(src)

    dest =  "config/#{file}"
    next if File.exist?(dest)

    FileUtils.ln_s(src, dest)
  end
end

#load_seedsObject



67
68
69
70
71
# File 'lib/gumboot/strap.rb', line 67

def load_seeds
  message 'Loading seeds'

  system 'rake db:seed'
end

#maintain_activerecord_schemaObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gumboot/strap.rb', line 55

def maintain_activerecord_schema
  message 'Loading database schema'

  if ActiveRecord::Base.connection.execute('SHOW TABLES').count.zero?
    Rails.logger.info 'No tables exist yet, loading schema'
    system 'rake db:schema:load'
  end

  Rails.logger.info 'Running migrations'
  system 'rake db:migrate'
end

#update_local_configuration(files) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/gumboot/strap.rb', line 95

def update_local_configuration(files)
  files.each do |file|
    src = "config/#{file}.dist"
    raise("Not a .yml file: #{file}") unless file.end_with?('.yml')
    raise("Missing dist config file: #{src}") unless File.exist?(src)

    merge_config(src, "config/#{file}")
  end
end