Top Level Namespace

Defined Under Namespace

Modules: Capistrano

Instance Method Summary collapse

Instance Method Details

#create_database(connection_string, db_name) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/demonz/common.rb', line 99

def create_database
  create_sql = <<-SQL
    CREATE DATABASE #{db_name};
  SQL

  run "mysql --user=#{db_admin_user} --password=#{db_admin_password} --execute=\"#{create_sql}\""
end

#database_exists?(connection_string, db_name) ⇒ Boolean

Check if a MySQL database exists Modified from www.grahambrooks.com/blog/create-mysql-database-with-capistrano/

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
# File 'lib/demonz/common.rb', line 87

def database_exists?(connection_string, db_name)
  exists = false

  run "#{connection_string} --execute=\"show databases;\"" do |channel, stream, data|
    exists = exists || data.include?(db_name)
  end

  exists
end

#db_string_regex(type) ⇒ Object

Get the regex pattern to extract details from the mysql connection string



81
82
83
# File 'lib/demonz/common.rb', line 81

def db_string_regex(type)
  "--#{type}='?([a-zA-Z0-9!@\#$%^&*-=+]+)'?\s"
end

#delete_database(connection_string, db_name) ⇒ Object

Delete a MySQL database



108
109
110
111
112
113
114
# File 'lib/demonz/common.rb', line 108

def delete_database(connection_string, db_name)
  drop_sql = <<-SQL
    DROP DATABASE #{db_name};
  SQL

  run "#{connection_string} --execute=\"#{drop_sql}\""
end

#get_db_name(application, release) ⇒ Object

Get the database name given an application and release name



71
72
73
74
75
76
77
78
# File 'lib/demonz/common.rb', line 71

def get_db_name(application, release)
  # Match possible db username (max 16 chars) and,
  # remove characters that may cause MySQL issues
  clean_application_name = application.downcase.gsub(/([\.\-\/])/, '').slice(0, 16)
  clean_release_name = release.downcase.gsub(/([\.\-\/])/, '_')

  db_name = "#{clean_application_name}__#{clean_release_name}"
end

#get_release_history(release_file) ⇒ Object

Get release history from server as string



46
47
48
49
# File 'lib/demonz/common.rb', line 46

def get_release_history(release_file)
  release_history = remote_file_exists?(release_file) ? capture("cat #{release_file}").strip : ''
  release_history
end

#local_dir_exists?(full_path) ⇒ Boolean

Check if a local directory exists

Returns:

  • (Boolean)


14
15
16
# File 'lib/demonz/common.rb', line 14

def local_dir_exists?(full_path)
  File.directory?(full_path)
end

#local_file_exists?(full_path) ⇒ Boolean

Check if a local file exists

Returns:

  • (Boolean)


9
10
11
# File 'lib/demonz/common.rb', line 9

def local_file_exists?(full_path)
  File.exists?(full_path)
end

#remote_dir_exists?(dir_path) ⇒ Boolean

Sames as above but with directories

Returns:

  • (Boolean)


25
26
27
# File 'lib/demonz/common.rb', line 25

def remote_dir_exists?(dir_path)
  'true' == capture("if [[ -d #{dir_path} ]]; then echo 'true'; fi").strip
end

#remote_file_exists?(full_path) ⇒ Boolean

From stackoverflow.com/a/1662001/356237 Needs full remote path

Returns:

  • (Boolean)


20
21
22
# File 'lib/demonz/common.rb', line 20

def remote_file_exists?(full_path)
  'true' ==  capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip
end

#remove_release_from_history(release, release_file) ⇒ Object

Remove a particular release from history



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/demonz/common.rb', line 52

def remove_release_from_history(release, release_file)
  release_history = capture("cat #{release_file}").split

  # Remove release if it exists
  release_history.delete_at release_history.index(release) unless release_history.index(release).nil?

  # Save
  first_rel = true
  release_history.each do |rel|
    if first_rel
      try_sudo "echo '#{rel}' > #{release_file}"
      first_rel = false
    else
      try_sudo "echo '#{rel}' >> #{release_file}"
    end
  end
end

#set_perms_dirs(dir_path, perm = 755) ⇒ Object

Recursively set directory permissions in a directory



41
42
43
# File 'lib/demonz/common.rb', line 41

def set_perms_dirs(dir_path, perm = 755)
  try_sudo "find #{dir_path} -type d -exec chmod #{perm} {} \\;"
end

#set_perms_files(dir_path, perm = 644) ⇒ Object

Recursively set file permissions in a directory



30
31
32
# File 'lib/demonz/common.rb', line 30

def set_perms_files(dir_path, perm = 644)
  try_sudo "find #{dir_path} -type f -exec chmod #{perm} {} \\;"
end

#setup_database_permissions(connection_string, db_name) ⇒ Object

Set permissions for a MySQL database From www.grahambrooks.com/blog/create-mysql-database-with-capistrano/



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/demonz/common.rb', line 118

def setup_database_permissions(connection_string, db_name)
  # We tack on a space at the end to help regex matches
  connection_string += " "

  db_admin_user = connection_string.match db_string_regex('user')
  db_admin_user = db_admin_user[1]

  db_admin_password = connection_string.match db_string_regex('password')
  db_admin_password = db_admin_password[1]

  grant_sql = <<-SQL
    GRANT ALL PRIVILEGES ON #{db_name}.* TO #{db_admin_user}@localhost IDENTIFIED BY '#{db_admin_password}';
  SQL

  run "#{connection_string} --execute=\"#{grant_sql}\""
end

#text_prompt(prompt = "Value: ") ⇒ Object

Prompts user entry Params: prompt



4
5
6
# File 'lib/demonz/common.rb', line 4

def text_prompt(prompt="Value: ")
  Capistrano::CLI.ui.ask(prompt) { |q| q.echo = true }
end

#update_db_in_settings_file(settings_file, db_name) ⇒ Object

Updates the Drupal settings file with the new database name



136
137
138
# File 'lib/demonz/common.rb', line 136

def update_db_in_settings_file(settings_file, db_name)
  run "#{try_sudo} sed -ri \"/^[ \\t]*(#|\\*|\\/)/! s/'database' => ''/'database' => '#{db_name}'/1\" #{settings_file}"
end