Projet

Général

Profil

Paste
Télécharger (3,65 ko) Statistiques
| Branche: | Révision:

root / bin / scripts-utils.sh @ 81b16cc2

1 d52a5b2f jenselme
#!/bin/sh
2
3
# This script contains useful functions for other scripts.
4
5
# Check if scripts-config.sh is imported.
6 3e9236bd Julien Enselme
if [ -z "${scripts_config}" ] ; then
7 d52a5b2f jenselme
    echo "Import of scripts-config.sh required."
8 653aeb8e Julien Enselme
    . /home/assos/bin/scripts-config.sh
9 d52a5b2f jenselme
fi
10
11
scripts_utils='imported'
12
13
ask_password() {
14
    # read -s doesn't work with sh.
15 683f57f9 Julien Enselme
    # usage: pass=$(ask_password "password please:")
16 3e9236bd Julien Enselme
    echo "$1" >&2
17 d52a5b2f jenselme
    echo -n ">" >&2
18 683f57f9 Julien Enselme
    stty_avant=$(stty -g)
19 d52a5b2f jenselme
    stty -echo
20
    read password
21 3e9236bd Julien Enselme
    stty "${stty_avant}"
22
    echo "${password}"
23 d52a5b2f jenselme
    unset password
24
}
25
26 4377ca71 Julien Enselme
generate_password() {
27 d52a5b2f jenselme
    # ARGS: [password_length]
28
    # The password contains special characters. '/' must be excluded to avoid sed malfunction.
29
30
    local site_password='/'
31
32 3e9236bd Julien Enselme
    if [ -z "$1" ] ; then
33 d52a5b2f jenselme
        local password_length=20
34
    else
35 3e9236bd Julien Enselme
        local password_length="$1"
36 d52a5b2f jenselme
    fi
37
38
    while echo "$site_password" | grep -Fq '/' ; do
39 8550e097 Julien Enselme
        site_password=$(dd if=/dev/random count=1 | uuencode -m - | head -n 2 | tail -n 1 | cut -c-"${password_length}")
40 d52a5b2f jenselme
    done
41
42 3e9236bd Julien Enselme
    echo "$site_password"
43 d52a5b2f jenselme
}
44
45
check_arguments() {
46
    # ARGS: number of arguments passed to script, number of arguments required, [help text]
47 3e9236bd Julien Enselme
    if [ "$1" -lt "$2"  ] ; then
48 3f774fab Thomas Berger
        echo "Not enough arguments." >&2
49
	echo -e "$3" >&2
50 1e4af651 Julien Enselme
        exit 1
51 d52a5b2f jenselme
    fi
52
}
53 469b9d6d Julien Enselme
54
generate_settings_local() {
55
    # ARGS: site_name, site_password, d7_settings_local_template, d7_site_settings_local
56 3e9236bd Julien Enselme
    sed "s/\%\%DBUSER\%\%/$1/ ; s/\%\%DBNAME\%\%/$1/ ; s/\%\%DBPASS\%\%/$2/ ; s/\%\%SITE_NAME\%\%/$1/" < "$3" > "$4"
57 469b9d6d Julien Enselme
}
58
59 4377ca71 Julien Enselme
give_dir() {
60 469b9d6d Julien Enselme
    # ARG: file
61 65ce03da Julien Enselme
    # Return the abosulte directory path of a file or a dir.
62 8550e097 Julien Enselme
    settings_location=$(realpath "$1")
63
    echo $(dirname "${settings_location}")
64 469b9d6d Julien Enselme
}
65 1c1c5a42 Julien Enselme
66
work_tree_clean() {
67 8550e097 Julien Enselme
    git_status_output=$(git status --porcelain)
68 3e9236bd Julien Enselme
    if [ -z "${git_status_output}" ] ; then
69 9987cca4 Julien Enselme
	return 0
70 1c1c5a42 Julien Enselme
    else
71 9987cca4 Julien Enselme
	return 1
72 1c1c5a42 Julien Enselme
    fi
73
}
74
75
mail_unclean_work_tree() {
76 8550e097 Julien Enselme
    cd "${dir_multi_assos}"
77
    git_status_output=$(git status)
78 3e9236bd Julien Enselme
    echo "${git_status_output}" | mail -s "$1" "${email_multi_assos}"
79 1c1c5a42 Julien Enselme
}
80
81
commit_if_unclean() {
82 683f57f9 Julien Enselme
    if ! work_tree_clean ; then
83 1c1c5a42 Julien Enselme
	commit_message="COMMIT OF UNCLEAN STUFF"
84 3e9236bd Julien Enselme
	commit -a -m "${commit_message}"
85
	mail_unclean_work_tree "[git] ${commit_message}"
86 1c1c5a42 Julien Enselme
    fi
87
}
88
89
commit() {
90
    # ARG: commit message
91 6d91f38e Julien Enselme
    if [ -z "$1" ] ; then
92 3f774fab Thomas Berger
	echo "Empty commit message. Nothing was commited." >&2
93 9987cca4 Julien Enselme
	return 2
94 1c1c5a42 Julien Enselme
    fi
95 3e9236bd Julien Enselme
    cd "${dir_multi_assos}"
96 6d91f38e Julien Enselme
    git commit -a -m "$1"
97 1c1c5a42 Julien Enselme
}
98 60103d5a Julien Enselme
99
site_exists() {
100
    # Check if site database exists.
101 3e9236bd Julien Enselme
    if mysql --defaults-extra-file="${myassos_cnf}" -e "USE $1" 2>/dev/null ; then
102 3f774fab Thomas Berger
	echo "Database $1 already exits." >&2
103 4ed597de Julien Enselme
	return 0
104 60103d5a Julien Enselme
    fi
105
106
    # Check if site folder already exists.
107 3e9236bd Julien Enselme
    dir="${d7_dir_sites}/$1"
108
    if [ -d "${dir}" ] ; then
109 76597ebf Thomas Berger
	echo "Folder ${dir} already exists." >&2
110 4ed597de Julien Enselme
	return 0
111 60103d5a Julien Enselme
    fi
112 4ed597de Julien Enselme
    return 1
113 60103d5a Julien Enselme
}
114 6c6343d9 Julien Enselme
115 d4c4525a Julien Enselme
get_site_dir_from_name() {
116
    if [ "$1" = 'default' ]; then
117
	dir='default'
118
    else
119
	dir="assos.centrale-marseille.fr.$1"
120
    fi
121
122
    echo "${dir}"
123
}
124
125
get_absolute_site_dir_from_name() {
126
    dir=$(get_site_dir_from_name "$1")
127
    echo "${d7_dir_sites}/${dir}"
128
}
129
130
sites_list() {
131 ad4e6a76 Julien Enselme
    # The commands output assos.centrale-marseille.fr/<site-name> or assos.centrale-marseille.fr (default).
132
    # Since we want only the site name (and default for default), we replace assos.centrale-marseille.fr by
133
    # assos.centrale-marseille.fr/default so it si like other site. We then use awk to split the strip on /
134
    # and get only the site name.
135 a0869461 Julien Enselme
    # grep -v "^self$" is used to remove self that appear if command is launched in one of drupal directories
136 ad4e6a76 Julien Enselme
    drush sa --format=csv --fields="name","uri" |
137
        sed 's#^assos.centrale-marseille.fr$#asoss.centrale-marseille.fr/default#' |
138
        awk '{if ($1 != "") { split($1, a, "/"); print a[2];}}' |
139
        sort |
140
        grep -v "^self$"
141 6c6343d9 Julien Enselme
}