Projet

Général

Profil

Révision 15ed27d8

Ajouté par Julien Enselme il y a plus de 9 ans

Use getopts to print help and to parse script arguments.

Voir les différences:

bin/d7-all-drush.sh
1 1
#!/bin/sh
2 2

  
3

  
4
help='d7-all-drush.sh drush_arguments'
5

  
6
usage() {
7
    echo "${help}"
8
}
9
. /home/assos/bin/print-help-if-required.sh
10

  
11

  
3 12
. /home/assos/bin/scripts-config.sh
4 13
. scripts-utils.sh
5 14

  
6
help="# ARGS: drush_command"
7 15

  
8 16
check_arguments "$#" 1 "${help}"
9 17

  
bin/d7-all-dump-individual.sh
1 1
#!/bin/sh
2 2

  
3
usage() {
4
    printf "d7-all-dump-individual.sh -m MODE\nRecognized mode are:\n\t%s\n\t%s\n" 'manunal' 'auto'
5
    printf "d7-all-dump-individual.sh -h prints this help message.\n"
6
}
7

  
8
mode=''
9
while getopts "hm:" opt; do
10
    case "${opt}" in
11
	h)
12
	    usage; exit 0;;
13
	m)
14
	    mode="$1";;
15
	:)
16
	    echo "Option -${OPTARG} requires an argument." >&2
17
	    usage >&2; exit 1;;
18
	\?)
19
	    usage >&2; exit 1;;
20
    esac
21
done
22
shift $((OPTIND-1))
23

  
24

  
3 25
. /home/assos/bin/scripts-config.sh
4 26
. scripts-utils.sh
5 27

  
6
help="# ARGS: auto or manual"
7

  
8
check_arguments "$#" 1 "$help"
9 28

  
10 29
current_date=$(date "+%Y-%m-%d-%Hh%Mm%Ss")
11 30

  
......
14 33
for dir in $(find . -maxdepth 1 -mindepth 1 -type d ! -name all | cut -c3-); do
15 34
    cd "${dir}"
16 35
    drush cc all
17
    if [ "$1" = 'auto' ] ; then
36
    if [ "${mode}" = 'auto' ] ; then
18 37
        drush sql-dump --result-file="${d7_dir_individual_auto_backup}/${dir}/${current_date}.${dir}.sql" --gzip
19 38
    else
20 39
        drush sql-dump --result-file="${d7_dir_individual_manual_backup}/${dir}/${current_date}.${dir}.sql" --gzip
bin/d7-all-update-contrib.sh
9 9
# Launch cron to check for updates.
10 10
d7-all-drush.sh -q cron
11 11

  
12
d7-all-dump-individual.sh auto
12
d7-all-dump-individual.sh -m auto
13 13

  
14 14
d7-all-drush.sh -y upc --no-core
15 15
d7-all-drush.sh -y updb
bin/d7-all-update-core.sh
1 1
#!/bin/sh
2 2

  
3
. /home/assos/bin/scripts-config.sh
3
usage() {
4
    printf "This script is intended to help you update drupal core. It follows steps described here: https://forge.centrale-marseille.fr/projects/clubdrupal/wiki/Utilisation_de_Drupal_multi-site#Mise-à-jour-du-noyau-de-Drupal
5
Please do not launch in cron.\n"
6
}
7
. /home/assos/bin/print-help-if-required.sh
8

  
4 9

  
5
help="This script is intended to help you update drupal core. It follows steps described here: https://forge.centrale-marseille.fr/projects/clubdrupal/wiki/Utilisation_de_Drupal_multi-site#Mise-%C3%A0-jour-du-noyau-de-Drupal
6
Please do not launch in cron."
10
. /home/assos/bin/scripts-config.sh
7 11

  
8 12
if ! tty -s ; then
9 13
    echo "${help}"
......
16 20
translations_backupdir=/var/tmp
17 21
cp "${translations_fr}" "${translations_backupdir}"
18 22

  
19
d7-all-dump-individual.sh manual
23
d7-all-dump-individual.sh -m manual
20 24

  
21 25
d7-all-drush.sh -y en update
22 26

  
bin/d7-all-usage.sh
7 7
cd "${d7_dir_sites}"
8 8

  
9 9
for line in $(drush pml --no-core --pipe); do
10
    usage.sh d7 enabled "${line}"
10
    usage.sh -v d7 -s enabled -n "${line}"
11 11
    echo -e "\n"
12 12
done
bin/d7-create-site.sh
1 1
#!/bin/sh
2 2

  
3
. /home/assos/bin/scripts-config.sh
4
. /home/assos/bin/scripts-config-site.sh "$1"
5
. /home/assos/bin/scripts-utils.sh
3
usage() {
4
    printf "d7-create-site.sh -s site_name -m site_mail -p admin_password [-l admin_login] [-d]\n"
5
    printf "Options:\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n"\
6
	   "-s The name of the site"\
7
	   "-m The mail of the site"\
8
	   "-p The password for the administrator (must be changed)"\
9
	   "-l The login of the admin. Optional, set to admin by default"\
10
	   "-d If passed, the database is not setup."
11
}
12

  
13
site_name=''
14
site_mail=''
15
admin_password=''
16
admin_login=''
17
init_db=true
18

  
19
while getopts "hs:m:p:l:d" opt; do
20
    case "${opt}" in
21
	h)
22
	    usage; exit 0;;
23
	s)
24
	    site_name="${OPTARG}";;
25
	m)
26
	    site_mail="${OPTARG}";;
27
	p)
28
	    admin_password="${OPTARG}";;
29
	l)
30
	    admin_login="${OPTARG}";;
31
	d)
32
	    init_db=false;;
33
	:)
34
	    echo "Option -$OPTARG requires an argument." >&2
35
	    usage >&2; exit 1;;
36
	\?)
37
	    usage >&2; exit 1;;
38
    esac
39
done
40
shift $((OPTIND-1))
41
admin_login=${admin_login:-'admin'}
42

  
43
# Check that all required parameters are there
44
if [ -z "${site_name}" ] || [ -z "${site_mail}" ] || [ -z "${admin_password}" ]; then
45
    echo "At least a required parameter is missing." >&2
46
    usage >&2
47
    exit 1
48
fi
6 49

  
7
help="# ARGS: site_name site_mail admin_password [--no-init-database]"
8 50

  
9
check_arguments "$#" 3 "${help}"
51
. /home/assos/bin/scripts-config.sh
52
. /home/assos/bin/scripts-config-site.sh "${site_name}"
53
. /home/assos/bin/scripts-utils.sh
10 54

  
11 55
# Check if site already exists.
12 56
if site_exists "${d7_site_name}" ; then
13 57
    exit 1
14 58
fi
15 59

  
16
init_db=1
17
if [ "$4" = "--no-init-database" ] ; then
18
    init_db=0
19
fi
20

  
21 60
######## Exceptions
22 61
echo "Checking if work tree is clean (may take a while)"
23 62
if ! work_tree_clean ; then
......
26 65
fi
27 66

  
28 67
# "-" is forbidden because it provokes database error.
29
if [ $(echo "$1" | grep -) ] ; then
68
if [ $(echo "${site_name}" | grep -) ] ; then
30 69
    echo '"-" is forbidden in the site name'
31 70
    exit 1
32 71
fi
33 72

  
34 73
# Site name length must be lower or equal to 16 due to database limitations.
35
if [ $(echo "$1" | wc -c) -gt 16 ] ; then
74
if [ $(echo "${site_name}" | wc -c) -gt 16 ] ; then
36 75
    echo "site name can't have more than 16 characters"
37 76
    exit 1
38 77
fi
......
49 88
site_line_sites_php="\$sites['assos.centrale-marseille.fr.$d7_site_name'] = 'assos.centrale-marseille.fr.$d7_site_name';"
50 89
site_line_aliases_drushrc_php="\$aliases['$d7_site_name'] = array('uri' => 'assos.centrale-marseille.fr/$d7_site_name', 'root' => '/home/assos/drupal7/', );"
51 90
# NB: site_name is initialised in script-config-site.sh
52
site_mail="$2"
53
admin_password="$3"
91
admin_password="${admin_password}"
54 92

  
55 93

  
56 94
###### Main
......
77 115
generate_settings_local "${d7_site_name}" "${site_password}" "${d7_settings_local_template}" "${d7_site_settings_local}"
78 116

  
79 117
# Install the site
80
drush site-install -y standard --account-mail="${site_mail}" --account-name="admin" --account-pass="${admin_password}" --locale=fr --site-mail="${site_mail}" --site-name="${d7_site_name}" --sites-subdir="${dir_site_name}"
118
drush site-install -y standard --account-mail="${site_mail}" --account-name="${admin_login}" --account-pass="${admin_password}" --locale=fr --site-mail="${site_mail}" --site-name="${d7_site_name}" --sites-subdir="${dir_site_name}"
81 119

  
82 120
# Create symbolic link
83 121
cd "${d7_dir}"
bin/d7-delete-site.sh
1 1
#!/bin/sh
2 2

  
3
. /home/assos/bin/scripts-config.sh
4
. scripts-config-site.sh "$1"
5
. scripts-utils.sh
3
usage() {
4
    echo "d7-delete-site.sh -s site_name."
5
}
6 6

  
7
help="# ARGS: site name."
7
site_name=''
8
while getopts "hs:" opt; do
9
    case "${opt}" in
10
	h)
11
	    usage; exit 0;;
12
	s)
13
	    site_name="${OPTARG}";;
14
	:)
15
	    echo "Option -$OPTARG requires an argument." >&2
16
	    usage >&2; exit 1;;
17
	\?)
18
	    usage >&2; exit 1;;
19
    esac
20
done
21
shift $((OPTIND-1))
8 22

  
9
check_arguments "$#" 1 "${help}"
23
. /home/assos/bin/scripts-config.sh
24
. scripts-config-site.sh "${site_name}"
25
. scripts-utils.sh
10 26

  
11 27
echo 'Awaiting for git status. (may take a while)'
12 28
if ! work_tree_clean ; then
bin/d7-generate-nginx-map.sh
1 1
#!/bin/sh
2 2

  
3
. /home/assos/bin/scripts-config.sh
3
usage() {
4
    help=<<EOF
5
Generate nginx map from Drupal's sites.php.
4 6

  
5
help=<<EOF
6
Generate nginx map from Drupal's sites.php
7
No option required.
7 8

  
8 9
The left part of the map is $http_host$uri.
9 10
The right part is $subdir.
10 11
EOF
12
    echo -e "${help}"
13
}
14
. /home/assos/bin/print-help-if-required.sh
15

  
16

  
17
. /home/assos/bin/scripts-config.sh
11 18

  
12 19
# Only for sites with url like assos.centrale-marseille.fr/subsite (other sites don't need to be in the map)
13 20
cut -d"'" -f2,4 "${sites_php}" |\
bin/d7-sync-site.sh
1 1
#!/usr/bin/env bash
2 2

  
3
help=<<EOF
3
usage() {
4
    help=<<EOF
4 5
This script is intended to ease the synchronisation between any site hosted by assos.
5 6
Typically, this script is usefull when you have (or want to have) a test site based on
6 7
a already working site. It relies on bash, drush and drush aliases.
7 8

  
8 9
Before synching the site, the destination site's database is backuped. If the
9
destination site does not exist, it is created.
10
destination site does not exist, it is created. If prod is set, the site is not in
11
maintenance mode.
10 12

  
11
usage: d7-sync.sh SOURCE_SITENAME DEST_SITENAME [--prod]
13
usage: d7-sync.sh -s SOURCE_SITENAME -d DEST_SITENAME [-p]
12 14
EOF
15
    echo -e "${help}"
16
}
17

  
18
source_site_name=''
19
dest_site_name=''
20
prod=false
21
while getopts "hs:d:p" opt; do
22
    case "${opt}" in
23
	s)
24
	    source_site_name="${OPTARG}";;
25
	d)
26
	    dest_site_name="${OPTARG}";;
27
	p)
28
	    prod=true;;
29
	h)
30
	    usage; exit 0;;
31
	:)
32
	    echo "Option -$OPTARG requires an argument." >&2
33
	    usage >&2; exit 1;;
34
	\?)
35
	    usage >&2; exit 1;;
36
    esac
37
done
38
shift $((OPTIND-1))
39

  
40
if [ -z "${source_site_name}" ] || [ -z "${dest_site_name}" ]; then
41
    echo "At least a required parameter is missing." >&2
42
    usage >&2
43
    exit 1
44
fi
45

  
13 46

  
14 47
. /home/assos/bin/scripts-config.sh
15 48
. /home/assos/bin/scripts-utils.sh
......
18 51

  
19 52

  
20 53
# Create site if necessary
21
if ! site_exists "$2" > /dev/null ; then
22
    echo "$2 does not exit. We will create it"
23
    d7-create-site.sh "$2" --no-init-database
54
if ! site_exists "${dest_site_name}" > /dev/null ; then
55
    echo "${dest_site_name} does not exit. We will create it"
56
    d7-create-site.sh "${dest_site_name}" --no-init-database
24 57
    # if the site is new, there is no database
25 58
    new_site=1
26 59
fi
......
28 61

  
29 62
# Backup the database of SOURCE_SITE
30 63
current_date=$(date "+%Y-%m-%d-%Hh%Mm%Ss")
31
if [ "$1" = "default" ] ; then
32
    dir="$2"
64
if [ "${source_site_name}" = "default" ] ; then
65
    dir="${dest_site_name}"
33 66
else
34
    dir="assos.centrale-marseille.fr.$2"
67
    dir="assos.centrale-marseille.fr.${dest_site_name}"
35 68
fi
36 69
if [ -z "${new_site}" ] ; then
37
    drush -y "@$1" sql-dump --result-file="${d7_dir_individual_manual_backup}/${dir}/${current_date}.${dir}.sql" --gzip
70
    drush -y "@${source_site_name}" sql-dump --result-file="${d7_dir_individual_manual_backup}/${dir}/${current_date}.${dir}.sql" --gzip
38 71
fi
39 72

  
40 73

  
......
45 78
# Sync databases
46 79
## Save file system
47 80
if [ -z "${new_site}" ] ; then
48
    private_path=$(drush "@$2" vget --format=string file_private_path 2> /dev/null)
49
    public_path=$(drush "@$2" vget --format=string file_public_path 2> /dev/null)
50
    temp_path=$(drush "@$2" vget --format=string file_temporary_path 2> /dev/null)
81
    private_path=$(drush "@${dest_site_name}" vget --format=string file_private_path 2> /dev/null)
82
    public_path=$(drush "@${dest_site_name}" vget --format=string file_public_path 2> /dev/null)
83
    temp_path=$(drush "@${dest_site_name}" vget --format=string file_temporary_path 2> /dev/null)
51 84
fi
52 85

  
53 86
## Sync
54 87
current_date=$(date "+%Y-%m-%d-%Hh%Mm%Ss")
55
sql_file="${dir_tmp}/${current_date}.$1.sql"
56
drush -y "@$1" sql-dump --result-file="${sql_file}"
57
sed -i -e "s#https?://assos.centrale-marseille.fr/$1#https://assos.centrale-marseille.fr/$2#g" "${sql_file}"
58
sed -i -e "s#/$1/sites/assos.centrale-marseille.fr.$1#/$2/sites/assos.centrale-marseille.fr.$2#g" "${sql_file}"
59
drush "@$2" sql-drop
60
drush "@$2" sql-cli < "${sql_file}"
88
sql_file="${dir_tmp}/${current_date}.${source_site_name}.sql"
89
drush -y "@${source_site_name}" sql-dump --result-file="${sql_file}"
90
sed -i -e "s#https?://assos.centrale-marseille.fr/${source_site_name}#https://assos.centrale-marseille.fr/${dest_site_name}#g" "${sql_file}"
91
sed -i -e "s#/${source_site_name}/sites/assos.centrale-marseille.fr.${source_site_name}#/${dest_site_name}/sites/assos.centrale-marseille.fr.${dest_site_name}#g" "${sql_file}"
92
drush "@${dest_site_name}" sql-drop
93
drush "@${dest_site_name}" sql-cli < "${sql_file}"
61 94
rm "${sql_file}"
62 95

  
63 96
## Restore file system
64 97
if [ -n "${private_path}" ] ; then
65
    drush -y "@$2" vset file_private_path "${private_path}"
98
    drush -y "@${dest_site_name}" vset file_private_path "${private_path}"
66 99
fi
67 100
if [ -n "${public_path}" ] ; then
68
    drush -y "@$2" vset file_public_path "${public_path}"
101
    drush -y "@${dest_site_name}" vset file_public_path "${public_path}"
69 102
fi
70 103
if [ -n "${temp_path}" ] ; then
71
    drush -y "@$2" vset file_temporary_path "${temp_path}"
104
    drush -y "@${dest_site_name}" vset file_temporary_path "${temp_path}"
72 105
fi
73 106

  
74
if [ "$3" = "--prod" ] ; then
75
    drush -y "@$2" vset maintenance_mode 0
107
if "${prod}"; then
108
    drush -y "@${dest_site_name}" vset maintenance_mode 0
76 109
else
77
    drush -y "@$2" vset maintenance_mode 1
110
    drush -y "@${dest_site_name}" vset maintenance_mode 1
78 111
fi
bin/flush-files.sh
1 1
#!/bin/sh
2 2

  
3
help="# ARGS: number of files to keep, [email address]\n
4
# usage: flush files from a folder except a number equal to argument.\n
5
# It only prints a warning if scripts-utils.sh is not imported in order to be used
6
# from the terminal."
3
usage() {
4
    help="flush-files.sh -n number_of_files_to_keep [-m email_address]\n
5
usage: flush files from a folder except a number equal to argument.\n
6
It only prints a warning if scripts-utils.sh is not imported in order to be used
7
from the terminal."
8
    echo -e "${help}"
9
}
10

  
11

  
12
number_of_files_to_keep=''
13
email_address=''
14
while getopts "hn:m:" opt; do
15
    case "${opt}" in
16
	h)
17
	    usage; exit 0;;
18
	n)
19
	    number_of_files_to_keep="${OPTARG}";;
20
	m)
21
	    email_address="${OPTARG}";;
22
	:)
23
	    echo "Option -$OPTARG requires an argument." >&2
24
	    usage >&2; exit 1;;
25
	\?)
26
	    usage >&2; exit 1;;
27
    esac
28
done
29
shift $((OPTIND-1))
30

  
31

  
32
# Check that all required parameters are there
33
if [ -z "${number_of_files_to_keep}" ]; then
34
    echo "At least a required parameter is missing." >&2
35
    usage >&2
36
    exit 1
37
fi
38

  
7 39

  
8 40
# Check if scripts-utils.sh is imported.
9 41
if [ -z "${scripts_utils}" ] ; then
......
15 47

  
16 48
# Must not be quoted to avoid problem with ((…))
17 49
backups_number=$(ls | wc -l)
18
number_of_backups_to_delete=$((backups_number - $1))
50
number_of_backups_to_delete=$((backups_number - ${number_of_files_to_keep}))
19 51

  
20 52
if [ "${number_of_backups_to_delete}" -gt 0 ] ; then
21 53
    ls | head "-${number_of_backups_to_delete}" | xargs rm
22 54
else
23
    if [ -n "$2" ] ; then
55
    if [ -n "${email_address}" ] ; then
24 56
        dir=$(pwd)
25
        echo "There are not enough files in $dir to Flush it. Check if backup script works fine." | mail -s "[db] $dir has a backup problem" "$2"
57
        echo "There are not enough files in $dir to Flush it. Check if backup script works fine." | mail -s "[db] $dir has a backup problem" "${email_address}"
26 58
    fi
27 59
fi
bin/flush-full-databases-backups.sh
8 8

  
9 9
for dir in $(ls) ; do
10 10
    cd "${dir}"
11
    flush-files.sh "${db_full_backup_number}" "${email_multi_assos}"
11
    flush-files.sh -n "${db_full_backup_number}" -m "${email_multi_assos}"
12 12
    cd -
13 13
done
bin/flush-individual-databases-backups.sh
9 9
for dir in $(ls) ; do
10 10
    cd "${dir}"
11 11
    if [ "$1" = 'auto' ] ; then
12
        flush-files.sh "${db_individual_auto_backup_number}" "${email_multi_assos}"
12
        flush-files.sh -n "${db_individual_auto_backup_number}" -m "${email_multi_assos}"
13 13
    else
14
        flush-files.sh "${db_individual_manual_backup_number}"
14
        flush-files.sh -n "${db_individual_manual_backup_number}"
15 15
    fi
16 16
    cd -
17 17
done
bin/print-help-if-required.sh
1
#!/bin/sh
2

  
3
# You must define a 'usage' function before to use that script.
4
# Never import it if you are using another getopts in your script.
5

  
6
while getopts "h" opt; do
7
    case "${opt}" in
8
	h)
9
	    usage
10
	    exit 0
11
	    ;;
12
    esac
13
done
14
shift $((OPTIND-1))
bin/usage.sh
1 1
#!/bin/sh
2 2

  
3
. /home/assos/bin/scripts-config.sh
4
. scripts-utils.sh
5

  
6
help="# ARGS: Drupal_version, project_status, project_name\n
3
usage() {
4
    help="usage.sh -v Drupal_version -s project_status -n project_name\n
7 5
# List sites that have the project_name with the corresponding project_status.\n
8 6
# project status: enabled or disabled"
7
    echo -e "${help}"
8
}
9

  
10
drupal_version=''
11
project_status=''
12
project_name=''
13
while getopts "hv:s:n:" opt; do
14
    case "${opt}" in
15
	h)
16
	    usage; exit 0;;
17
	v)
18
	    drupal_version="${OPTARG}";;
19
	s)
20
	    project_status="${OPTARG}";;
21
	n)
22
	    project_name="${OPTARG}";;
23
	:)
24
	    echo "Option -$OPTARG requires an argument." >&2
25
	    usage >&2; exit 1;;
26
	\?)
27
	    usage >&2; exit 1;;
28
    esac
29
done
30
shift $((OPTIND-1))
31

  
32
# Check that all required parameters are there
33
if [ -z "${drupal_version}" ] || [ -z "${project_status}" ] || [ -z "${project_name}" ]; then
34
    echo "At least a required parameter is missing." >&2
35
    usage >&2
36
    exit 1
37
fi
38

  
39

  
40
. /home/assos/bin/scripts-config.sh
41
. scripts-utils.sh
9 42

  
10
check_arguments "$#" 3 "${help}"
11 43

  
12
if [ "$1" = d7 ] ; then
44
if [ "${drupal_version}" = d7 ] ; then
13 45
    cd "${d7_dir_sites}"
14 46
else
15 47
    echo Unrecognize version.
......
24 56
    # Print site_dir if listed.
25 57

  
26 58
    cd "${dir}";
27
	if [ 1 -le $(drush pml --status="$2" | grep "$3" | wc -l) ] ; then
59
	if [ 1 -le $(drush pml --status="${project_status}" | grep "${project_name}" | wc -l) ] ; then
28 60
        echo "${dir}";
29 61
        number_found=$(($number_found + 1))
30 62
    fi
31 63
    cd -
32 64
done
33 65

  
34
echo "Number of sites found for project $3 and status $2 : $number_found";
66
echo "Number of sites found for project ${project_name} and status ${project_status} : $number_found";

Formats disponibles : Unified diff