github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/repository/charms/wordpress/inc/common (about)

     1  #!/bin/bash
     2  #
     3  #    common - Common functions
     4  #
     5  #    Copyright (C) 2012  Marco Ceppi
     6  #    Author: Marco Ceppi <marco@ceppi.net>
     7  #
     8  #    This program is free software: you can redistribute it and/or modify
     9  #    it under the terms of the GNU General Public License as published by
    10  #    the Free Software Foundation, either version 3 of the License, or
    11  #    (at your option) any later version.
    12  #
    13  #    This program is distributed in the hope that it will be useful,
    14  #    but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  #    GNU General Public License for more details.
    17  #
    18  #    You should have received a copy of the GNU General Public License
    19  #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    20  #
    21  
    22  hostname=`unit-get public-address`
    23  private_name=`hostname --all-fqdns | cut -f 1 -d ' '`
    24  
    25  wp_install_path="/var/www"
    26  config_file_path="$wp_install_path/wp-config.php"
    27  config_info_path="$wp_install_path/wp-info.php"
    28  config_override_path="$wp_install_path/wp-overrides.php"
    29  
    30  ####
    31  ## The only hook who should ever remove the NFS mount is the nfs-relation-(departed|broken) hook
    32  ## Everyone else should just assume it's there or try to mount it. We don't want to loose people's
    33  ## data, they get mad when that happens :(
    34  ####
    35  
    36  engine()
    37  {
    38  	echo `cat .web-engine`
    39  }
    40  
    41  total_memory()
    42  {
    43    echo $((`awk '/MemTotal/{print $2}' /proc/meminfo` / 1024))
    44  }
    45  
    46  apc_shm_size()
    47  {
    48    all_memory=`total_memory`
    49    if [ -z $all_memory ]; then
    50      echo 256 # Play it safe, no memory was reported.
    51      return 0
    52    fi
    53  
    54    if [ $all_memory -gt 1280 ]; then
    55      echo 1024
    56    else
    57      echo $((all_memory * 80 / 100))
    58    fi
    59  }
    60  
    61  make_bare()
    62  {
    63  	# Un-do APC
    64  	rm -f /etc/php5/conf.d/apc.ini
    65  	# IF there is an NFS mount, get everything out of it
    66  	if [ -f .nfs-mount ]; then # Looks like someone got fancy with NFS
    67  		if [ -L $wp_install_path/wp-content ]; then # Check if we actually have a symlink
    68  			rm -f $wp_install_path/wp-content
    69  			rsync -az /mnt/wordpress/wp-content $wp_install_path
    70  		fi
    71  	fi
    72  
    73  	echo "<?php" > $config_override_path
    74  	juju-log "We are now bare"
    75  }
    76  
    77  make_single()
    78  {
    79  	make_bare
    80  	juju-log "Installing PHP apc.ini ..."
    81  	rm -f /etc/php5/conf.d/apc.ini
    82  	install -D -o root -g root -m 0644 files/charm/php/php5_conf.d_apc.ini /etc/php5/conf.d/apc.ini
    83  
    84  	shm_size=`apc_shm_size`
    85  	sed -i -e "s/^apc\.shm_size=.*$/apc.shm_size=${shm_size}M/" /etc/php5/conf.d/apc.ini
    86  
    87  	## We do NFS here, because Configuration changes cascade. So to get to Optimzied you *must* first 
    88  	## make "single" (and by association, "bare"). Since nfs is both a single and optimized thing we do it now.
    89  
    90  	do_nfs
    91  
    92  	juju-log "We are now single, ALL THE SINGLE UNITS ALL THE SINGLE UNITS"
    93  }
    94  
    95  make_optimized()
    96  {
    97  	make_single
    98  	cat > $config_override_path <<EOF
    99  <?php
   100  define('DISALLOW_FILE_MODS', TRUE);
   101  EOF
   102  	juju-log "We are now optimized prime"
   103  }
   104  
   105  do_nfs()
   106  {
   107  	if [ -f .nfs-mount ]; then
   108  		# This has all the NFS mount stuff in it
   109  		source .nfs-mount
   110  		mkdir -p /mnt/wordpress
   111  		if grep -qs '/mnt/wordpress' /proc/mounts; then
   112  			juju-log "We're already mounted."
   113  		else
   114  			mount -t $MOUNT_TYPE $MOUNT_OPS $MOUNT_SERVER:$MOUNT_PATH /mnt/wordpress
   115  			if [ $? -ne 0 ]; then
   116  				juju-log "Could not connect to file-server"
   117  				exit 1 # OH THE HUMANITY OF IT ALL
   118  			fi
   119  			if [ ! -d /mnt/wordpress/wp-content ]; then
   120  				rsync -az $wp_install_path/wp-content /mnt/wordpress/
   121  			fi
   122  
   123  			mv $wp_install_path/wp-content $wp_install_path/wp-content.bak.$(date +%Y%m%d-%H%M%S) && rm -rf $wp_install_path/wp-content
   124  			ln -s /mnt/wordpress/wp-content $wp_install_path
   125  			juju-log "Mounted NFS"
   126  		fi
   127  	else
   128  		juju-log "There is no nfs mount, not sure what to do, so we'll just bail"
   129  	fi
   130  }
   131  
   132  do_vcs()
   133  {
   134  	source "/usr/share/charm-helper/bash/vcs.bash"
   135  
   136  	local NEW_REPO_URL=${1:-""}
   137  
   138  	if [ -z "$NEW_REPO_URL" ]; then
   139  		rm -f .wp-repo
   140  		return 0
   141  	fi
   142  
   143  	if [ -f .wp-repo ]; then
   144  		source .wp-repo
   145  	else
   146  		REPO_URL=""
   147  	fi
   148  
   149  	if [ "$NEW_REPO_URL" != "$REPO_URL" ]; then
   150  		# We need to determine what kind of URL this is
   151  
   152  		repo_path=`ch_fetch_repo $NEW_REPO_URL`
   153  		if [ $? -ne 0 ]; then
   154  			juju-log "Failed to fetch repo, charm-helper/bash/vcs.bash exited with $?"
   155  			return 1
   156  		fi
   157  
   158  		# Write all our new data to disk
   159  		cat > .wp-repo <<EOF
   160  #!/bin/bash
   161  REPO_URL=$NEW_REPO_URL
   162  REPO_PATH=$repo_path
   163  EOF
   164  		source .wp-repo # Just so the rest of this can run all willy nilly
   165  	else
   166  		# Lets just try and do a fetch
   167  		ch_update_repo "$REPO_PATH"
   168  		if [ $? -ne 0 ]; then
   169  			juju-log "Failed to update repository, charm-helper/bash/vcs.bash exited with $?"
   170  			return 2
   171  		fi
   172  	fi
   173  
   174  	repo_type=`ch_detect_vcs $REPO_PATH`
   175  
   176  	if [ ! -d "$REPO_PATH/wp-content" ]; then
   177  		# This is just someone who has their source control at the root level of the repo.
   178  		wp_content_path="$REPO_PATH/wp-content"
   179  		rsync -az --delete --exclude ".$repo_type/*" $REPO_PATH/ $wp_install_path/wp-content/
   180  	else
   181  		# This is a repo with a wp-content folder. So we'll move that then everything else they have in that repo
   182  		# to the web root. OMG requirement.
   183  		rsync -az --delete --exclude ".$repo_type/*" $REPO_PATH/wp-content/ $wp_install_path/wp-content/
   184  		# This is just incase people have modifications outside of the wp-content directory
   185  		rsync -az --exclude ".$repo_type/*" $REPO_PATH/ $wp_install_path/
   186  	fi
   187  }
   188  
   189  do_cache()
   190  {
   191  	if [ ! -f .memcache ]; then
   192  		if [ -d $wp_install_path/wp-content/plugins/wp-ffpc ]; then
   193  			wp plugin uninstall --path=$wp_install_path wp-ffpc
   194  			wp plugin delete --path=$wp_install_path wp-ffpc
   195  		fi
   196  
   197  		return 0
   198  	fi
   199  
   200  	source .memcache
   201  
   202  	wp plugin install --path=$wp_install_path wp-ffpc
   203  	sed -i -e "s/'host'=>.*/'host'=>'$CACHE_HOST',/" $wp_install_path/wp-content/plugins/wp-ffpc/wp-ffpc.php
   204  	wp plugin activate --path=$wp_install_path wp-ffpc
   205  
   206  	install -o www-data -g www-data -m 0644 files/charm/wordpress/advanced-cache.php $wp_install_path/wp-content/advanced-cache.php
   207  	sed -i -e "s/\$wp_ffpc_config\['host'\]=.*/\$wp_ffpc_config\['host'\]='$CACHE_HOST';/" \
   208  		-e "s/\$wp_ffpc_config\['port'\]=.*/\$wp_ffpc_config\['host'\]='$CACHE_PORT';/" \
   209  		$wp_install_path/wp-content/advanced-cache.php
   210  
   211  	engine=`cat .web-engine`
   212  	if [ "$engine" == "nginx" ]; then
   213  		sed -i -e "s/memcached_pass .*/memcached_pass $CACHE_HOST\:$CACHE_PORT;/" /etc/nginx/sites-enabled/wordpress
   214  	fi
   215  }