Project

General

Profile

Paste
Download (3.65 KB) Statistics
| Branch: | Revision:

root / bin / scripts-utils.sh @ ad4e6a76

1
#!/bin/sh
2

    
3
# This script contains useful functions for other scripts.
4

    
5
# Check if scripts-config.sh is imported.
6
if [ -z "${scripts_config}" ] ; then
7
    echo "Import of scripts-config.sh required."
8
    . /home/assos/bin/scripts-config.sh
9
fi
10

    
11
scripts_utils='imported'
12

    
13
ask_password() {
14
    # read -s doesn't work with sh.
15
    # usage: pass=$(ask_password "password please:")
16
    echo "$1" >&2
17
    echo -n ">" >&2
18
    stty_avant=$(stty -g)
19
    stty -echo
20
    read password
21
    stty "${stty_avant}"
22
    echo "${password}"
23
    unset password
24
}
25

    
26
generate_password() {
27
    # ARGS: [password_length]
28
    # The password contains special characters. '/' must be excluded to avoid sed malfunction.
29

    
30
    local site_password='/'
31

    
32
    if [ -z "$1" ] ; then
33
        local password_length=20
34
    else
35
        local password_length="$1"
36
    fi
37

    
38
    while echo "$site_password" | grep -Fq '/' ; do
39
        site_password=$(dd if=/dev/random count=1 | uuencode -m - | head -n 2 | tail -n 1 | cut -c-"${password_length}")
40
    done
41

    
42
    echo "$site_password"
43
}
44

    
45
check_arguments() {
46
    # ARGS: number of arguments passed to script, number of arguments required, [help text]
47
    if [ "$1" -lt "$2"  ] ; then
48
        echo "Not enough arguments." >&2
49
	echo -e "$3" >&2
50
        exit 1
51
    fi
52
}
53

    
54
generate_settings_local() {
55
    # ARGS: site_name, site_password, d7_settings_local_template, d7_site_settings_local
56
    sed "s/\%\%DBUSER\%\%/$1/ ; s/\%\%DBNAME\%\%/$1/ ; s/\%\%DBPASS\%\%/$2/ ; s/\%\%SITE_NAME\%\%/$1/" < "$3" > "$4"
57
}
58

    
59
give_dir() {
60
    # ARG: file
61
    # Return the abosulte directory path of a file or a dir.
62
    settings_location=$(realpath "$1")
63
    echo $(dirname "${settings_location}")
64
}
65

    
66
work_tree_clean() {
67
    git_status_output=$(git status --porcelain)
68
    if [ -z "${git_status_output}" ] ; then
69
	return 0
70
    else
71
	return 1
72
    fi
73
}
74

    
75
mail_unclean_work_tree() {
76
    cd "${dir_multi_assos}"
77
    git_status_output=$(git status)
78
    echo "${git_status_output}" | mail -s "$1" "${email_multi_assos}"
79
}
80

    
81
commit_if_unclean() {
82
    if ! work_tree_clean ; then
83
	commit_message="COMMIT OF UNCLEAN STUFF"
84
	commit -a -m "${commit_message}"
85
	mail_unclean_work_tree "[git] ${commit_message}"
86
    fi
87
}
88

    
89
commit() {
90
    # ARG: commit message
91
    if [ -z "$1" ] ; then
92
	echo "Empty commit message. Nothing was commited." >&2
93
	return 2
94
    fi
95
    cd "${dir_multi_assos}"
96
    git commit -a -m "$1"
97
}
98

    
99
site_exists() {
100
    # Check if site database exists.
101
    if mysql --defaults-extra-file="${myassos_cnf}" -e "USE $1" 2>/dev/null ; then
102
	echo "Database $1 already exits." >&2
103
	return 0
104
    fi
105

    
106
    # Check if site folder already exists.
107
    dir="${d7_dir_sites}/$1"
108
    if [ -d "${dir}" ] ; then
109
	echo "Folder ${dir} already exists." >&2
110
	return 0
111
    fi
112
    return 1
113
}
114

    
115
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
    # 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
    # grep -v "^self$" is used to remove self that appear if command is launched in one of drupal directories
136
    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
}