Tuesday, October 16, 2012

Bash programming tips part 2

In part 1 we have introduced the basic script outline. In configuration section we specify variables and configuration options. Example bellow in an extract (short version) of some deployment script configuration, but it is a good example of how complex configuration can be.


# <config>

###
# CONSTANTS
###

#turn debuging on/off
_DEBUG=1
#two levels ERROR=2/INFO=1/DEBUG=0
_DEBUG_LEVEL=0

#custom return codes
_YES=100
_NO=200

_LOCK=/tmp/.lock.deploy

_PROGNAME=$(basename $0)

_MASTER=
_CLUSTER_MODE="cluster"
_SINGLE_MODE="single"

#local environment variables
_LOCALIP=
_LOCALUSER=
_HOSTNAME=

#script specific parameters
_TARGET=
_DUMP=
_MAINTENANCE=
_TAG=

#arrays
declare -a _FEATURES=()
declare -a _INTERFACES=()


#user for deploy
_REMOTE_USER=root

###
# Input dependent arrays
###

#all possible known projects
_PROJECTS=(project1 project2 project3 project4 )

# Project source
declare -A _PROJECT_PATH
#sn
_PROJECT_PATH[${_PROJECTS[0]}]="/var/www/vhosts/project1/www/"
#polet
_PROJECT_PATH[${_PROJECTS[1]}]="/var/www/vhosts/project2/www/"
#deloidom
_PROJECT_PATH[${_PROJECTS[2]}]="/var/www/vhosts/project3/www/"
#pogledi
_PROJECT_PATH[${_PROJECTS[3]}]="/var/www/vhosts/project4/www/"

declare -A _PROJECT_WEB_SERVERS
_PROJECT_WEB_SERVERS[${_PROJECTS[0]}]="server1 server2"
_PROJECT_WEB_SERVERS[${_PROJECTS[1]}]="server1 server2"
_PROJECT_WEB_SERVERS[${_PROJECTS[2]}]="server1 server2"
_PROJECT_WEB_SERVERS[${_PROJECTS[3]}]="server1 server2"
_PROJECT_WEB_SERVERS[${_PROJECTS[4]}]="server1 server2"
_PROJECT_WEB_SERVERS[${_PROJECTS[5]}]="server1 server2"
_PROJECT_WEB_SERVERS[${_PROJECTS[6]}]="server1 server2"

declare -A _PROJECT_DUMP_LOCATION
_PROJECT_DUMP_LOCATION[${_PROJECTS[0]}]="/tmp"
_PROJECT_DUMP_LOCATION[${_PROJECTS[1]}]="/tmp"
_PROJECT_DUMP_LOCATION[${_PROJECTS[2]}]="/tmp"
_PROJECT_DUMP_LOCATION[${_PROJECTS[3]}]="/tmp"

declare -A _PROJECT_OTHER_SERVERS
_PROJECT_OTHER_SERVERS[${_PROJECTS[0]}]="10.0.0.1 10.0.0.2"
_PROJECT_OTHER_SERVERS[${_PROJECTS[1]}]="10.0.0.1 10.0.0.2"
_PROJECT_OTHER_SERVERS[${_PROJECTS[2]}]="10.0.0.1 10.0.0.2"
_PROJECT_OTHER_SERVERS[${_PROJECTS[3]}]="10.0.0.1 10.0.0.2"

In script above I've used powerful bash feature - array. This allows me to easily group configurations, without having to use long names like PROJECT1_WEB_SERVER1 to keep clarity.




Post a Comment