github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/repository/xenial/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 if [ -f .web-engine ]; then 54 web_engine=`cat .web-engine` 55 service $web_engine stop 56 fi 57 sed -i -e "s/# deb \(.*\) multiverse/deb \1 multiverse/g" /etc/apt/sources.list #for libapache2-mod-fastcgi 58 apt-get update 59 apt-get -y purge nginx 60 apt-get install -y apache2-mpm-worker libapache2-mod-fastcgi 61 service apache2 stop 62 63 rm -f /var/www/index.html 64 65 rm -f /etc/apache2/sites-enabled/* 66 a2enmod actions fastcgi alias proxy_balancer proxy_http headers 67 68 install -o root -g root -m 0644 files/charm/apache/etc_apache2_conf-d_php5-fpm.conf /etc/apache2/conf.d/php5-fpm.conf 69 70 juju-log "Installing Apache loadbal config..." 71 install -o root -g root -m 0644 files/charm/apache/etc_apache2_sites-enabled_loadbalancer /etc/apache2/sites-available/loadbalancer 72 sed -i -e "s/^ ServerName .*/ ServerName ${unit_address}/" /etc/apache2/sites-available/loadbalancer 73 a2ensite loadbalancer 74 75 juju-log "Installing Apache wordpress config..." 76 install -o root -g root -m 0644 files/charm/apache/etc_apache2_sites-enabled_wordpress /etc/apache2/sites-available/wordpress 77 a2ensite wordpress 78 79 echo "apache2" > .web-engine 80 else 81 if [ -f .web-engine ]; then 82 web_engine=`cat .web-engine` 83 service $web_engine stop 84 fi 85 apt-get -y purge apache2* libapache2* 86 apt-get install -y nginx 87 service nginx stop 88 89 juju-log "Cleaning any old or default nginx site configs ..." 90 rm -f /etc/nginx/sites-enabled/* 91 rm -f /etc/nginx/conf.d/* 92 93 juju-log "Installing nginx common config ..." 94 rm -f /etc/nginx/nginx.conf 95 install -o root -g root -m 0644 files/charm/nginx/etc_nginx_nginx.conf /etc/nginx/nginx.conf 96 97 juju-log "Installing nginx actual site config ..." 98 #rm -f /etc/nginx/sites-available/ 99 install -o root -g root -m 0644 files/charm/nginx/etc_nginx_sites-enabled_wordpress /etc/nginx/sites-available/wordpress 100 ln -sf ../sites-available/wordpress /etc/nginx/sites-enabled/wordpress 101 102 juju-log "Installing nginx loadbal config ..." 103 rm -f /etc/nginx/sites-available/loadbalancer 104 install -o root -g root -m 0644 files/charm/nginx/etc_nginx_sites-enabled_loadbalancer /etc/nginx/sites-available/loadbalancer 105 ln -sf ../sites-available/loadbalancer /etc/nginx/sites-enabled/loadbalancer 106 107 juju-log "Moving nginx var dirs to /mnt storage ..." 108 rsync -az /var/lib/nginx /mnt/ && rm -rf /var/lib/nginx && ln -s /mnt/nginx /var/lib/ 109 110 echo "nginx" > .web-engine 111 fi 112 113 # http://i.imgur.com/TUF91.gif 114 hooks/loadbalancer-rebuild 115 116 juju-log "Restarting Services ..." 117 source hooks/restart 118 119 if [ ! -f $config_file_path ]; then 120 juju-log "Nothing to configure, since nothing is installed" 121 exit 0 122 fi 123 124 juju-log "Show details? $expose_info" 125 126 if [ "$expose_info" == "yes" ]; then 127 rsync -az files/_debug $wp_install_path/ 128 else 129 rm -rf $wp_install_path/_debug 130 fi 131 132 juju-log "I will be using this tuning level: $tuning_level" 133 134 if [ "$tuning_level" == "optimized" ]; then 135 # First and foremost, we need to disable the ability to edit 136 # themes and upload/update plugins. This breaks a scale-out 137 # environment. It's sad but true. If you want to update a plugin 138 # install a theme, etc; take a look at the README. 139 make_optimized 140 elif [ "$tuning_level" == "single" ]; then 141 # We need to prepare an NFS mount, because someone is probably 142 # going to try to scale out. We also need to vamp up caching. 143 make_single 144 elif [ "$tuning_level" == "bare" ]; then 145 # Okay, you know what you're doing. You're probably going to 146 # use Gluster to stream-line your files, so you don't need to 147 # disable anything. We trust you to do what you need to. 148 make_bare 149 else 150 juju-log "Not sure about that tuning level." 151 exit 1 152 fi 153 154 do_vcs $wp_content_repo 155 156 if [ -z "$wp_content_repo" ]; then 157 wp plugin update --path=$wp_install_path --all 158 fi 159 160 do_cache 161 162 chown -R www-data.www-data $wp_install_path 163 164 . hooks/restart