Guest User

Untitled

a guest
Jun 19th, 2016
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Скрипт для разворачивания приложения на Ubuntu 15.04 с LAMP
  4.  
  5. set -e
  6.  
  7. github_repo='https://github.com/applejacky/students'
  8. apache_root='/var/www'
  9. project_name='students.loc'
  10. host='127.0.0.1'
  11.  
  12. config_ini_path="${apache_root}/${project_name}/app/config.ini"
  13. required_packages=(
  14. 'git'
  15. 'composer'
  16. 'mysql'
  17. )
  18.  
  19. function die {
  20. message=$1
  21. echo "Error: ${message}" 1>&2
  22. kill $$
  23. exit 1
  24. }
  25.  
  26. function check_permission {
  27. if [[ $EUID -ne 0 ]]; then
  28. die "This script must be run as root."
  29. fi
  30. }
  31.  
  32. function check_packages_installed {
  33. packages=("$@")
  34. for package in "${packages[@]}"; do
  35. if ! which "${package}" > /dev/null; then
  36. echo "Package ${package} is not installed. Installing."
  37. apt-get install $package
  38. fi
  39. done
  40. }
  41.  
  42. function get_value_from_config_ini {
  43. config_key=$1
  44. path_to_config_ini=$2
  45. config_value=$(sed -n -e "s/${config_key}\s*=\s*//p" "$config_ini_path")
  46. [[ -z $config_value ]] && die "Variable ${config_key} not found in ${config_ini_path}"
  47. echo $config_value
  48. }
  49.  
  50. function write_value_to_config_ini {
  51. config_key=$1
  52. config_value=$2
  53. path_to_config_ini=$3
  54. if check_config_key_presented $config_key "$path_to_config_ini"; then
  55. sed -ri "/^${config_key}\s?=/s/=.*/= ${config_value}/" "$path_to_config_ini"
  56. else
  57. die "Key ${config_key} not found in ${path_to_config_ini}"
  58. fi
  59. }
  60.  
  61. function check_config_key_presented {
  62. config_key=$1
  63. path_to_config_ini=$2
  64. egrep -q "${config_key}(\s)?=" "$path_to_config_ini"
  65. }
  66.  
  67. function create_db_if_not_exists {
  68. dbname=$1
  69. mysql -u$username -p$password -e "CREATE DATABASE IF NOT EXISTS ${dbname}" > /dev/null 2>&1
  70. }
  71.  
  72. function create_students_table {
  73. mysql -u$username -p$password $dbname < "${apache_root}/${project_name}"/dump.sql > /dev/null 2>&1
  74. }
  75.  
  76. check_permission
  77.  
  78. check_packages_installed "${required_packages[@]}"
  79.  
  80. destination_dir="${apache_root}/${project_name}"
  81. echo "Downloading ${github_repo} to \"$destination_dir\"..."
  82. git clone $github_repo "$destination_dir"
  83.  
  84. echo "Type a database name: "
  85. read dbname;
  86. write_value_to_config_ini 'dbname' "$dbname $config_ini_path"
  87.  
  88. echo "Type a host: "
  89. read host
  90. write_value_to_config_ini 'host' "$host $config_ini_path"
  91.  
  92. echo "Type a username: "
  93. read username
  94. write_value_to_config_ini 'username' "$username $config_ini_path"
  95.  
  96. echo "Type a password: "
  97. read password
  98. write_value_to_config_ini 'password' "$password $config_ini_path"
  99.  
  100. cat "$config_ini_path"
  101.  
  102. dbname=$(get_value_from_config_ini 'dbname' $"config_ini_path")
  103. host=$(get_value_from_config_ini 'host' "$config_ini_path")
  104. username=$(get_value_from_config_ini 'username' "$config_ini_path")
  105. password=$(get_value_from_config_ini 'password' "$config_ini_path")
  106.  
  107. create_db_if_not_exists $dbname
  108. create_students_table
  109.  
  110. cd "${apache_root}/${project_name}"
  111. echo "Installing dependencies and configuring autoloader..."
  112. composer install
  113.  
  114. echo "Seeding students table..."
  115. "${apache_root}/${project_name}"/util/student_table_seeder.php
  116.  
  117. echo "Creating virtual host configuration..."
  118. virtual_host_path="/etc/apache2/sites-available/${project_name}.conf"
  119. tee $virtual_host_path > /dev/null <<EOF
  120. <VirtualHost *:80>
  121. ServerAdmin admin@${project_name}
  122. ServerName ${project_name}
  123. ServerAlias www.${project_name}
  124. DocumentRoot /var/www/${project_name}/public
  125. ErrorLog ${APACHE_LOG_DIR}/error.log
  126. CustomLog ${APACHE_LOG_DIR}/access.log combined
  127. <Directory /var/www/${project_name}/public>
  128. Options FollowSymLinks
  129. Allowoverride All
  130. Require all granted
  131. </Directory>
  132. </VirtualHost>
  133. EOF
  134.  
  135. systemctl start apache2
  136. a2ensite "${project_name}".conf
  137. a2enmod rewrite
  138. systemctl restart apache2
  139.  
  140. sed -i '/^'"${host}"'/ d' /etc/hosts
  141. cat <<EOF >> ./etc/hosts
  142. ${host} localhost
  143. ${host} ${project_name}
  144. EOF
Add Comment
Please, Sign In to add comment