#!/bin/bash # Queries and reports on drupal installations. # # This script helps maintain a drupal installation group. # include library file . "`dirname ${0}`/drupalLibrary.sh" # Dumps out script usage printUsage() { echo -e " " echo -e "Reports on a drupal installation (or all installations in a group)." echo -e " " echo -e "Usage: " echo -e "\t`basename $0` listsites [install group root dir]" echo -e "\t`basename $0` ( | all ) [install group root dir]" echo -e " " echo -e "listsites - generates a list of site installations on the designated group root dir. Useful for scripting." echo -e " " echo -e " is one of:" echo -e "admin - shows the admin login username/password -- the username for user with uid=1 and the db password (this is the convention)" echo -e "aliases - shows list of aliases for a site." echo -e "dbconn - generates a string that can be used as a parameter to 'mysql' command to connect to the site database." echo -e "\ttip! add the following lines to your .bashrc file:" echo -e "\tdmysql() { mysql \`"`basename $0`" dbconn \"\$1\"\`; }" echo -e "dbparms - Displays the DB connection parameters for a site." echo -e "dbvalid - reports whether the configured db information allows successful connection to a database." echo -e "modules - generates a list (sutible for use in scripts) of installed contrib modules for the given site. (see also 'themes')" echo -e "\tnote: here 'module' means a downloaded module project which could contain several drupal modules." echo -e "roles - generates a role/user report. (see also 'users')" echo -e "sitedir - returns the 'sites' directory for this installation." echo -e "siteroot - returns the root directory of the installation." echo -e "\ttip! add the following lines to your .bashrc file:" echo -e "\tdcd() { cd \"\`"`basename $0`" sitedir \"\$1\"\`\"; }" echo -e "\tdpushd() { pushd \"\`"`basename $0`" sitedir \"\$1\"\`\"; }" echo -e "status - generates a formatted report on the status for a site" echo -e "\t(summarizes above info as well as active modules from drupalDBQuery.sh listmodules)." echo -e "statusnc - generates the same report as 'status' but without the update/updated-tag notices." echo -e "themes - generates a list (sutible for use in scripts) of installed contrib themes for the given site. (see also 'modules')" echo -e "updates - generates a detailed listing of any updates available for the core and any installed contrib modules and themes" echo -e "users - generates a user/role report. (see also 'roles')" echo -e " " outputInstallGroupUsage foo echo -e " " } ######### # Param handling if [ $# -eq 0 ]; then printUsage exit 0 fi # handle parameters pOp="" pInstall="" case "${1}" in listsites ) pOp="$1" shift ;; admin | aliases | dbconn | dbparms | dbvalid | modules | roles | sitedir | siteroot | status | statusnc | themes | updates | users ) if [ $# -lt 2 ]; then echo -e "** No install parameter specified. Aborting\n" printUsage exit 1 fi pOp="${1}" pInstall="${2}" shift; shift ;; * ) echo -e "Invalid parameter(s) specified. Aborting.\n" printUsage exit 1 esac # process Group Root Dir pRootDir="`getGroupRootDir "${1}"`" errorStr="`validateGroupRootDir "${pRootDir}"`" if [ -n "${errorStr}" ]; then echo -e "${errorStr}\n" printUsage exit 1 fi ########################## # Startup if [ -z "${pInstall}" ]; then case "${pOp}" in listsites ) getSitesInGroupDir "${pRootDir}" ;; esac else if [ "`toLower "${pInstall}"`" == "all" ]; then pInstall="`getSitesInGroupDir "${pRootDir}"`" else # Validate the installation errorStr="`validateSite "${pInstall}" "${pRootDir}"`" if [ -n "${errorStr}" ]; then echo -e "** Error with specified install: ${errorStr}. Aborting.\n" exit 1 fi fi for site in ${pInstall}; do if [ "${site}" != "${pInstall}" ]; then echo -e "\nsite: ${site}" fi case "${pOp}" in admin ) # Username is username for user w/ UID = 1 # password is db connection passwork echo -n "username: " runSQL "${site}" "${pRootDir}" "SELECT name FROM users WHERE uid = 1;" echo -n "password: " getDBPassword "${site}" "${pRootDir}" ;; aliases ) getInstallAliases "${site}" "${pRootDir}" ;; dbconn ) getDBConnectionString "${site}" "${pRootDir}" ;; dbparms ) dbHost="`getDBHost "${site}" "${pRootDir}"`" dbName="`getDBName "${site}" "${pRootDir}"`" dbUser="`getDBUsername "${site}" "${pRootDir}"`" dbPass="`getDBPassword "${site}" "${pRootDir}"`" echo "Site '${site}' has the following DB connection parameters:" echo -e "\tDB host: ${dbHost}" echo -e "\tDB name: ${dbName}" echo -e "\tusername: ${dbUser}" echo -e "\tpassword: ${dbPass}" ;; dbvalid ) dbConnectionTest="`testDBConnectionForSite "${site}" "${pRootDir}"`" if [ -z "${dbConnectionTest}" ]; then echo "Connection Valid." else echo "Connection NOT valid: ${dbConnectionTest}" fi echo " " ;; modules ) getInstalledContribModuleList "${site}" "${pRootDir}" ;; sitedir ) getSiteDir "${site}" "${pRootDir}" ;; siteroot ) getTargetDir "${site}" "${pRootDir}" ;; themes ) getInstalledContribThemeList "${site}" "${pRootDir}" ;; updates ) first="" findDrupalUpdates "${site}" "${pRootDir}" | while read update; do if [ -z "${first}" ]; then echo -e "Drupal core (version `getSiteDrupalVersion "${site}" "${pRootDir}"`):" first="nope" fi echo -e "\t${update}" done getInstalledContribModuleList "${site}" "${pRootDir}" | while read item; do first="" findContribModuleUpdates "${site}" "${pRootDir}" "${item}" | while read update; do if [ -z "${first}" ]; then echo "Module ${item}:" first="nope" fi echo -e "\t${update}" done done getInstalledContribThemeList "${site}" "${pRootDir}" | while read item; do first="" findContribThemeUpdates "${site}" "${pRootDir}" "${item}" | while read update; do if [ -z "${first}" ]; then echo "Theme ${item}:" first="nope" fi echo -e "\t${update}" done done ;; status | statusnc ) targetDir="`getTargetDir "${site}" "${pRootDir}"`" majorVersion="`getSiteDrupalMajorVersion "${site}" "${pRootDir}"`" aliases="`getInstallAliases "${site}" "${pRootDir}"`" echo -e "Site: ${site}" echo -e "\tInstall dir: ${targetDir}" echo -en "\tDrupal version: `getSiteDrupalVersion "${site}" "${pRootDir}"`" if [ "${pOp}" == "status" ]; then if [ -n "`findDrupalUpdates "${site}" "${pRootDir}"`" ]; then echo -n "*" fi fi echo -n " (`gitTag "${targetDir}" "label"`" # Get latest tag if [ "${pOp}" == "status" ]; then latestTag="`getLatestDrupalTag "${majorVersion}"`" if [ -z "${latestTag}" ]; then latestTag="unknown" fi echo -n "/latest tag:${latestTag}" fi echo ")" # output error message if no db connection dbConnectionTest="`testDBConnectionForSite "${site}" "${pRootDir}"`" if [ -n "${dbConnectionTest}" ]; then echo -e "\tDB Connection parameters ('`getDBConnectionString "${site}" "${pRootDir}"`') invalid: ${dbConnectionTest}" fi # output site aliases if [ -n "${aliases}" ]; then echo -en "Site Aliases: " for item in ${aliases}; do echo -n "${item} " done echo "" fi # non-contrib themes if [ -n "`getNonContribThemeList "${site}" "${pRootDir}"`" ]; then echo -en "Installed non-Contrib Themes: " getNonContribThemeList "${site}" "${pRootDir}" | while read item; do echo -n "${item} " done echo "" fi # contrib themes if [ -n "`getInstalledContribThemeList "${site}" "${pRootDir}"`" ]; then echo -e "Installed Contrib Themes:" if [ "${pOp}" == "status" ]; then echo -e "\tformat: [*] (/[])" else echo -e "\tformat: ()" fi getInstalledContribThemeList "${site}" "${pRootDir}" | while read item; do contribThemeDir="`getDirForContribTheme "${site}" "${pRootDir}" "${item}"`" echo -en "\t${item}" if [ "${pOp}" == "status" ]; then if [ -n "`findContribThemeUpdates "${site}" "${pRootDir}" "${item}"`" ]; then echo -n "*" fi fi echo -n " (`gitTag "${contribThemeDir}" "label"`" # Get latest tag if [ "${pOp}" == "status" ]; then tag="`gitTag "${contribThemeDir}"`" latestTag="`getLatestContribTag "${item}" "${majorVersion}"`" if [ -n "${latestTag}" ]; then if [[ "${tag}" != "${latestTag}" ]]; then echo -n "/${latestTag}" fi else echo -n "/latest unknown" fi fi echo ")" done fi # non-contrib modules if [ -n "`getNonContribModuleList "${site}" "${pRootDir}"`" ]; then echo -en "Installed non-Contrib Modules: " echo -e "\tformat: : {modules active from that installation}" getNonContribModuleList "${site}" "${pRootDir}" | while read item; do echo -en "\t${item}: " # Find active modules for this module installation if [ -z "${dbConnectionTest}" ]; then moduleDirRel="`getDirForNonContribModule "${site}" "${pRootDir}" "${item}" "true"`" orClauses="`for alias in ${aliases}; do newModuleDirRel="\`echo "${moduleDirRel}" | sed "s|${site}|${alias}|"\`" echo -n " OR filename LIKE '${newModuleDirRel}%'" first="1" done`" query="SELECT name FROM system WHERE status = 1 AND type = 'module' AND ( filename LIKE '${moduleDirRel}%' ${orClauses} ) ORDER BY name;" oneActive="" runSQL "${site}" "${pRootDir}" "${query}" | ( while read activeModule; do echo -n "${activeModule} " oneActive="yup" done if [ -z "${oneActive}" ]; then echo -n "*NONE*" fi ) echo "" else echo "(no db connection)" fi done fi # contrib modules if [ -n "`getInstalledContribModuleList "${site}" "${pRootDir}"`" ]; then echo -e "Installed Contrib Modules:" if [ "${pOp}" == "status" ]; then echo -e "\tformat: [*] (/[]): {modules active from that installation}" else echo -e "\tformat: (): {modules active from that installation}" fi getInstalledContribModuleList "${site}" "${pRootDir}" | while read item; do contribModuleDir="`getDirForContribModule "${site}" "${pRootDir}" "${item}"`" echo -en "\t${item}" if [ "${pOp}" == "status" ]; then if [ -n "`findContribModuleUpdates "${site}" "${pRootDir}" "${item}"`" ]; then echo -n "*" fi fi echo -n " (`gitTag "${contribModuleDir}" "label"`" # Get latest tag if [ "${pOp}" == "status" ]; then tag="`gitTag "${contribModuleDir}"`" latestTag="`getLatestContribTag "${item}" "${majorVersion}"`" if [ -n "${latestTag}" ]; then if [[ "${tag}" != "${latestTag}" ]]; then echo -n "/${latestTag}" fi else echo -n "/latest unknown" fi fi echo -n "): " # Find active modules for this module installation if [ -z "${dbConnectionTest}" ]; then moduleDirRel="`getDirForContribModule "${site}" "${pRootDir}" "${item}" "true"`" orClauses="`for alias in ${aliases}; do newModuleDirRel="\`echo "${moduleDirRel}" | sed "s|${site}|${alias}|"\`" echo -n " OR filename LIKE '${newModuleDirRel}%'" first="1" done`" query="SELECT name FROM system WHERE status = 1 AND type = 'module' AND ( filename LIKE '${moduleDirRel}%' ${orClauses} ) ORDER BY name;" oneActive="" runSQL "${site}" "${pRootDir}" "${query}" | ( while read activeModule; do echo -n "${activeModule} " oneActive="yup" done if [ -z "${oneActive}" ]; then echo -n "*NONE*" fi ) echo "" else echo "(no db connection)" fi # Special handling for the fckeditor and ckeditor modules if [ "${item}" == "fckeditor" -o "${item}" == "ckeditor" ]; then echo -en "\t\t" "`dirname $0`/drupalManageLibraries.sh" -quiet query "${item}" "${site}" "${pRootDir}" fi done fi if [ "${pOp}" == "status" ]; then echo -e "('' indicates updates available for item)" fi echo " " ;; roles | users ) if [ "${pOp}" == "users" ]; then query="SELECT uid, if(status = 0, concat(name, ' (inactive)'), name), mail FROM users WHERE uid >= 1" innerquery="SELECT r.rid, r.name FROM role r, users_roles ur WHERE r.rid = ur.rid AND ur.uid =" tag="User" connector="has roles" else # pOp = "roles" query="SELECT rid, name FROM role;" innerquery="SELECT u.uid, if(u.status = 0, concat(u.name, ' (inactive)'), u.name), u.mail FROM users u, users_roles ur WHERE u.uid = ur.uid AND ur.rid =" tag="Role" connector="is set for users" fi # get outer list runSQL "${site}" "${pRootDir}" "${query}" | sed 's/\t/|/g' | while read result; do id="`echo "${result}" | cut -d '|' -f 1`" name="`echo "${result}" | cut -d '|' -f 2`" email="`echo "${result}" | cut -d '|' -f 3`" echo -e "${tag} ${id} - '${name}'/${email} ${connector}:" # get inner result runSQL "${site}" "${pRootDir}" "${innerquery} ${id}" | sed 's/\t/|/g' | ( while read innerresult; do iid="`echo "${innerresult}" | cut -d '|' -f 1`" iname="`echo "${innerresult}" | cut -d '|' -f 2`" iemail="`echo "${innerresult}" | cut -d '|' -f 3`" if [ -n "${iemail}" ]; then echo -e "\t${iid} - '${iname}'/${iemail}" else echo -e "\t${iid} - '${iname}'" fi oneActive="yup" done if [ -z "${oneActive}" ]; then echo -e "\t*NONE*" fi ) done ;; esac done fi