bashrightscale

Same Rightscript used multiple times on a ServerTemplate with different inputs


I have just created my first Rightscale ServerTemplate and Deployment using a bunch of Rightscripts. One of the scripts I created was to add a virtual host to apache.

#!/bin/bash -e

if [ $RS_DISTRO = ubuntu ]; then 
  export apache=apache2
  export apache_extra_conf_dir=/etc/apache2/conf.d
elif [ $RS_DISTRO = centos ]; then
  export apache=httpd
  export apache_extra_conf_dir=/etc/httpd/conf.d
fi
server_name=$SERVER_NAME

echo "Adding virtual hosts to ${apache_extra_conf_dir}/vh-${server_name}.conf"
cat > $apache_extra_conf_dir/vh-${server_name}.conf <<EOF 

NameVirtualHost $SITE_IP:$SITE_PORT

<VirtualHost $SITE_IP:$SITE_PORT>
    ServerName $SERVER_NAME
    ServerAlias $SITE_DOMAIN *.$SITE_DOMAIN
    UseCanonicalName Off

    ServerAdmin $ADMIN_EMAIL
    DocumentRoot $APACHE_WWW_DIR
    <Directory "$APACHE_WWW_DIR">
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
EOF

service $apache restart
exit 0

My question is can I use the same Rightscript twice on the ServerTemplate but set different inputs for each (IP, Port, www dir and Servername)? eg.

ServerTemplate:

Execute Rightscript vhost: *:80 /www-x/ x.com 
Execute Rightscript vhost: *:80 /www-y/ y.com

OR do I have to create a special Rightscript just for this server deployment that has both virtual hosts defined in the same script?

Execute Rightscript vhost: *:80 /www-x/ x.com | *:80 /www-y/ y.com

Solution

  • You can use the same RightScript with different inputs only if you put that script in the "Operational Scripts" section of your ServerTemplate. Then if you leave the inputs blank, you can execute that operational script with different inputs many times.

    If you want that behavior in the "Boot Scripts" section, you'll have to either create a single script which handles two sets of inputs (for two vhosts) or create a clone of the RightScript with a different name and different input names.

    You might consider taking a look at the Chef based tools. You can create a Chef recipe which takes an array of vhost names as input, then executes the "apache_site" resource that sets up a vhost. You can see an example in the RightScale Chef code linked below.

    https://github.com/rightscale/cookbooks_public/blob/master/cookbooks/web_apache/recipes/setup_frontend_http_vhost.rb