Install ViMbAdmin with Chef

Two years ago I wrote a tutorial to install a complete Postfix mail server with Dovecot, SpamAssassin and ViMbAdmin. At the moment I am automating this tutorial for CentOS 7 with Chef. For ViMbAdmin I created a recipe that clones the ViMbAdmin repository from Github, puts the configuration files from templates, creates the database, tables and inserts an admin user into the database. I thought it would be nice to share my vimbadmin recipe:

 

vimbadmin.rb

chef_gem 'bcrypt'
require 'bcrypt'

# Clone ViMbAdmin files
git node['my-mailserver']['vimbadmin']['dir'] do
  repository node['my-mailserver']['vimbadmin']['repo']['url']
  reference node['my-mailserver']['vimbadmin']['repo']['reference']
  action :sync
end

# Put configuration
template "#{node['my-mailserver']['vimbadmin']['dir']}/application/configs/application.ini" do
  source 'vimbadmin/application.ini.erb'
  owner node['my-webserver']['user']
  group node['my-webserver']['group']
  mode '0440'
end

# Put .htaccess
template "#{node['my-mailserver']['vimbadmin']['dir']}/public/.htaccess" do
  source 'vimbadmin/.htaccess.erb'
  owner 'root'
  group 'root'
  mode '0644'
end

# Install composer vendors
composer_project node['my-mailserver']['vimbadmin']['dir'] do
  dev true
  quiet false
  prefer_dist true
  action :install
end

# Make the var directory writable for the web server
bash 'vimbadmin file permissions' do
  cwd '/tmp'
  code <<-EOH
    find #{node['my-mailserver']['vimbadmin']['dir']}/var ! -user #{node['my-webserver']['user']} -exec chown #{node['my-webserver']['user']}: {} \\;
  EOH
end

include_recipe 'my-dbserver::common'

# Set mysql connection info
mysql_connection_info = {
  :host => node['my-mailserver']['vimbadmin']['db']['host'],
  :username => 'root',
  :password => node['mariadb']['server_root_password']
}

# Grant all privileges the vimbadmin database from localhost and %
%w(localhost %).each do |host|
  mysql_database_user node['my-mailserver']['vimbadmin']['db']['username'] do
    connection mysql_connection_info
    password node['my-mailserver']['vimbadmin']['db']['password']
    host host
    database_name node['my-mailserver']['vimbadmin']['db']['name']
    privileges [:all]
    action :grant
  end
end

# Create tables
execute 'vimbadmin-create-tables' do
  cwd node['my-mailserver']['vimbadmin']['dir']
  command './bin/doctrine2-cli.php orm:schema-tool:create'
  action :nothing
end

encrypted_password = BCrypt::Password.create(
  node['my-mailserver']['vimbadmin']['admin']['password'],
  :cost => node['my-mailserver']['vimbadmin']['admin']['password_cost']
)

date_time = Time.now.strftime("%Y-%m-%d %H:%M:%S")

# Insert admin
mysql_database "#{node['my-mailserver']['vimbadmin']['db']['name']}_insert-admin" do
  connection mysql_connection_info
  database_name node['my-mailserver']['vimbadmin']['db']['name']
  sql "INSERT INTO admin (id, username, password, super, active, created, modified) "\
    "VALUES (1, '#{node['my-mailserver']['vimbadmin']['admin']['email']}', '#{encrypted_password}', "\
    "1, 1, '#{date_time}', '#{date_time}');"
  action :nothing
end

# Gather dbversion variables
dbversion = {
  'name' => nil,
  'number' => nil
}

if File.exist?("#{node['vogentis-mailserver']['vimbadmin']['dir']}/library/ViMbAdmin/Version.php")
  File.readlines("#{node['my-mailserver']['vimbadmin']['dir']}/library/ViMbAdmin/Version.php").select do |line|
    if line =~ / DBVERSION_NAME /
      dbversion['name'] = /'([^']*)'/.match(line).to_s.gsub("'", '')
      Chef::Log.debug "
Found DBVERSION_NAME: #{dbversion['name']}"
    end

    if line =~ / DBVERSION /
      dbversion['number'] = line.gsub(/\D/, '')
      Chef::Log.debug "Found DBVERSION: #{dbversion['number']}"
    end
  end
end

# Insert dbversion
mysql_database "#{node['my-mailserver']['vimbadmin']['db']['name']}_insert-dbversion" do
  connection mysql_connection_info
  database_name node['my-mailserver']['vimbadmin']['db']['name']
  sql "INSERT INTO dbversion (id, version, name, applied_on) VALUES (1, #{dbversion['number']}, '#{dbversion['name']}', '#{date_time}');"
  action :nothing
end

# Create database
mysql_database node['my-mailserver']['vimbadmin']['db']['name'] do
  connection mysql_connection_info
  action :create
  notifies :run, 'execute[vimbadmin-create-tables]', :immediately
  notifies :query, "mysql_database[#{node['my-mailserver']['vimbadmin']['db']['name']}_insert-admin]", :immediately
  notifies :query, "mysql_database[#{node['my-mailserver']['vimbadmin']['db']['name']}_insert-dbversion]", :immediately
end
Tags: ,,