forked from morpht/letsencrypt_drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.sh
118 lines (100 loc) · 3.64 KB
/
functions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env bash
# Environment variables that need to be available:
# * PROJECT
# * ENVIRONMENT
# Build up all required variables.
DRUSH_ALIAS="@${PROJECT}.${ENVIRONMENT}"
# Project root is a known path on Acquia Cloud.
PROJECT_ROOT="$HOME/letsencrypt_drupal_config/${PROJECT}.${ENVIRONMENT}"
FILE_CONFIG=${PROJECT_ROOT}/letsencrypt_drupal/config_${PROJECT}.${ENVIRONMENT}.sh
DIRECTORY_DEHYDRATED_CONFIG=${PROJECT_ROOT}/letsencrypt_drupal/dehydrated
FILE_DOMAINSTXT=${PROJECT_ROOT}/letsencrypt_drupal/domains_${PROJECT}.${ENVIRONMENT}.txt
DEHYDRATED="https://github.com/dehydrated-io/dehydrated.git"
CERT_DIR=~/.letsencrypt_drupal
TMP_DIR=/tmp/letsencrypt_drupal
FILE_BASECONFIG=${TMP_DIR}/baseconfig
# Detect core version
DRUPAL_VERSION="9"
if grep -q -r -i --include Drupal.php "const version" ${PROJECT_ROOT}; then DRUPAL_VERSION="8"; fi
if grep -q -r -i --include bootstrap.inc "define('VERSION', '" ${PROJECT_ROOT}; then DRUPAL_VERSION="7"; fi
# Load all variables provided by the project.
. ${FILE_CONFIG}
#---------------------------------------------------------------------
acquire_lock_or_exit()
{
# Check we are not running already: http://mywiki.wooledge.org/BashFAQ/045
# @ToDo: Platform specific lock.
exec 8>/tmp/cert_renew_lock
if ! flock -n 8 ; then
logline "Another instance of this script running.";
exit 1
fi
# This now runs under the lock until 8 is closed (it will be closed automatically when the script ends)
}
#---------------------------------------------------------------------
slackpost()
{
# Can either be one of 'good', 'warning', 'danger', or any hex color code
COLOR="${2}"
USERNAME="${3}"
TEXT="${4}"
if [[ "$SLACK_WEBHOOK_URL" =~ ^https:\/\/hooks.slack.com* ]]; then
# based on https://gist.github.com/dopiaza/6449505
escapedText=$(echo $TEXT | sed 's/"/\"/g' | sed "s/'/\'/g")
json="{\"channel\": \"$SLACK_CHANNEL\", \"username\":\"$USERNAME\", \"icon_emoji\":\"ghost\", \"attachments\":[{\"color\":\"$COLOR\" , \"text\": \"$escapedText\"}]}"
curl -s -d "payload=$json" "$SLACK_WEBHOOK_URL" || logline "Failed to send message to slack: ${USERNAME}: ${TEXT}"
else
logline "No Slack: ${USERNAME}: ${TEXT}"
fi
}
#---------------------------------------------------------------------
logline()
{
printf '\n[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$1"
}
#---------------------------------------------------------------------
cd_or_exit()
{
rv=0
cd "$1" || rv=$?
if [ $rv -ne 0 ]; then
logline "Failed to cd into $1 directory. exiting."
exit 31
fi
}
#---------------------------------------------------------------------
drush_set_challenge()
{
DRUSH_ALIAS="${1}"
DRUPAL_VERSION="${2}"
DOMAIN="${3}"
TOKEN_VALUE="${4}"
pushd /var/www/html/${PROJECT}.${ENVIRONMENT}
echo "Checking if basic auth is enabled"
shield_en=$(drush --uri=${DOMAIN} pml | grep shield | grep Enabled)
if [[ -z "${shield_en}" ]]; then
echo "Shield disabled"
else
ENABLE_SHIELD="true"
echo "Shield enabled, disabling"
drush -y --uri=${DOMAIN} pmu shield
fi
echo "EXECUTING: drush en -y --uri=${DOMAIN} letsencrypt_challenge"
drush en -y --uri=${DOMAIN} letsencrypt_challenge
echo "EXECUTING: drush sset -y --uri=${DOMAIN} letsencrypt_challenge.challenge \"${TOKEN_VALUE}\""
drush -y --uri=${DOMAIN} sset letsencrypt_challenge.challenge "${TOKEN_VALUE}"
drush --uri=${DOMAIN} cr
popd
}
drush_clean_challenge()
{
DRUSH_ALIAS="${1}"
DRUPAL_VERSION="${2}"
DOMAIN="${3}"
pushd /var/www/html/${PROJECT}.${ENVIRONMENT}
drush pmu -y --uri=${DOMAIN} letsencrypt_challenge
if [[ ! -z "${ENABLE_SHIELD}" ]]; then
drush -y --uri=${DOMAIN} en shield
fi
popd
}