github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/repository/charms/wordpress/hooks/config-changed (about) 1 #!/bin/bash 2 3 set -ue 4 5 source inc/common 6 7 ## 8 # This is where things can get a bit hectic. So this long blog is just to 9 # help preface what's going on here. When this hook fires it will always 10 # try to re-configure the entire unit. It's like a GOD hook, triggering 11 # or using data from every other hook with the exception of the install 12 # and upgrade-charm hooks. 13 # 14 # First, get a bunch of configuration values. The idea being this is should 15 # be the only place that config-get is run as it's "unreliable" anywhere else 16 # Data should then either be passed to functions or written down - you know 17 # "YOU SHOULD ALWAYS LEAVE A NOTE!" 18 # 19 # From here, the web engine will either be updated or left alone. We always 20 # assume it's going to be nginx unless you specify apache. So if you put 21 # "lighttpd" or "i love apache" you're going to get nginx, so don't do that. 22 # Configuration files are re-written every time. This is to make sure that 23 # any changes to long running units overtime are included. It may seem 24 # expensive to keep re-writing the same files but in the end having all 25 # the units in a sane and identical state is worth it. 26 # 27 # Next, we do some small file moving around. Just for debug stuff. 28 # 29 # After that tuning levels are decided and executed. This is a bit more 30 # involved than just running a function, check inc/common for that craziness 31 # 32 # After that, it's time to get any user-defined content! do_vcs does that 33 # and a little more 34 # 35 # Caching stuff, basically "DO WE HAVE MEMCACHED, GOOD LETS DO CONFIG" 36 # 37 # Then we stop and start everything again and wait for the next round. 38 ## 39 40 tuning_level=`config-get tuning` 41 wp_content_repo=`config-get wp-content` 42 expose_info=`config-get debug` 43 engine=`config-get engine` 44 unit_address=`unit-get private-address` 45 46 # Make it lower case 47 tuning_level=${tuning_level,,} 48 expose_info=${expose_info,,} 49 engine=${engine,,} 50 51 52 if [ "$engine" == "apache" ] || [ "$engine" == "apache2" ]; then 53 status-set maintenance "configuring apache" 54 if [ -f .web-engine ]; then 55 web_engine=`cat .web-engine` 56 service $web_engine stop 57 fi 58 sed -i -e "s/# deb \(.*\) multiverse/deb \1 multiverse/g" /etc/apt/sources.list #for libapache2-mod-fastcgi 59 apt-get update 60 apt-get -y purge nginx 61 apt-get install -y apache2-mpm-worker libapache2-mod-fastcgi 62 service apache2 stop 63 64 rm -f /var/www/index.html 65 66 rm -f /etc/apache2/sites-enabled/* 67 a2enmod actions fastcgi alias proxy_balancer proxy_http headers 68 69 install -o root -g root -m 0644 files/charm/apache/etc_apache2_conf-d_php5-fpm.conf /etc/apache2/conf.d/php5-fpm.conf 70 71 juju-log "Installing Apache loadbal config..." 72 install -o root -g root -m 0644 files/charm/apache/etc_apache2_sites-enabled_loadbalancer /etc/apache2/sites-available/loadbalancer 73 sed -i -e "s/^ ServerName .*/ ServerName ${unit_address}/" /etc/apache2/sites-available/loadbalancer 74 a2ensite loadbalancer 75 76 juju-log "Installing Apache wordpress config..." 77 install -o root -g root -m 0644 files/charm/apache/etc_apache2_sites-enabled_wordpress /etc/apache2/sites-available/wordpress 78 a2ensite wordpress 79 80 echo "apache2" > .web-engine 81 else 82 status-set maintenance "configuring nginx" 83 if [ -f .web-engine ]; then 84 web_engine=`cat .web-engine` 85 service $web_engine stop 86 fi 87 apt-get -y purge apache2* libapache2* 88 apt-get install -y nginx 89 service nginx stop 90 91 juju-log "Cleaning any old or default nginx site configs ..." 92 rm -f /etc/nginx/sites-enabled/* 93 rm -f /etc/nginx/conf.d/* 94 95 juju-log "Installing nginx common config ..." 96 rm -f /etc/nginx/nginx.conf 97 install -o root -g root -m 0644 files/charm/nginx/etc_nginx_nginx.conf /etc/nginx/nginx.conf 98 99 juju-log "Installing nginx actual site config ..." 100 #rm -f /etc/nginx/sites-available/ 101 install -o root -g root -m 0644 files/charm/nginx/etc_nginx_sites-enabled_wordpress /etc/nginx/sites-available/wordpress 102 ln -sf ../sites-available/wordpress /etc/nginx/sites-enabled/wordpress 103 104 juju-log "Installing nginx loadbal config ..." 105 rm -f /etc/nginx/sites-available/loadbalancer 106 install -o root -g root -m 0644 files/charm/nginx/etc_nginx_sites-enabled_loadbalancer /etc/nginx/sites-available/loadbalancer 107 ln -sf ../sites-available/loadbalancer /etc/nginx/sites-enabled/loadbalancer 108 109 juju-log "Moving nginx var dirs to /mnt storage ..." 110 rsync -az /var/lib/nginx /mnt/ && rm -rf /var/lib/nginx && ln -s /mnt/nginx /var/lib/ 111 112 echo "nginx" > .web-engine 113 fi 114 115 # http://i.imgur.com/TUF91.gif 116 hooks/loadbalancer-rebuild 117 118 status-set maintenance "restarting services" 119 juju-log "Restarting Services ..." 120 source hooks/restart 121 122 if [ ! -f $config_file_path ]; then 123 juju-log "Nothing to configure, since nothing is installed" 124 status-set blocked "Waiting for database" 125 exit 0 126 fi 127 128 juju-log "Show details? $expose_info" 129 130 if [ "$expose_info" == "yes" ]; then 131 rsync -az files/_debug $wp_install_path/ 132 else 133 rm -rf $wp_install_path/_debug 134 fi 135 136 juju-log "I will be using this tuning level: $tuning_level" 137 138 if [ "$tuning_level" == "optimized" ]; then 139 # First and foremost, we need to disable the ability to edit 140 # themes and upload/update plugins. This breaks a scale-out 141 # environment. It's sad but true. If you want to update a plugin 142 # install a theme, etc; take a look at the README. 143 make_optimized 144 elif [ "$tuning_level" == "single" ]; then 145 # We need to prepare an NFS mount, because someone is probably 146 # going to try to scale out. We also need to vamp up caching. 147 make_single 148 elif [ "$tuning_level" == "bare" ]; then 149 # Okay, you know what you're doing. You're probably going to 150 # use Gluster to stream-line your files, so you don't need to 151 # disable anything. We trust you to do what you need to. 152 make_bare 153 else 154 juju-log "Not sure about that tuning level." 155 exit 1 156 fi 157 158 do_vcs $wp_content_repo 159 160 if [ -z "$wp_content_repo" ]; then 161 wp plugin update --path=$wp_install_path --all 162 fi 163 164 do_cache 165 166 chown -R www-data.www-data $wp_install_path 167 168 . hooks/restart 169 170 status-set active