forked from morpht/letsencrypt_drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.sh
144 lines (131 loc) · 7.23 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/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="/var/www/html/${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="12"
# 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}"
elif [[ ! -z "$TEAMS_WEBHOOK_URL" ]]; then
# https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook?tabs=newteams%2Cjavascript
# Can either be one of 'attention', 'good', 'warning' for AdaptiveCard: https://adaptivecards.io/explorer/TextBlock.html
if [[ "$COLOR" == "danger" ]]; then
COLOR="attention"
fi
escapedText=$(echo $TEXT | sed 's/"/\"/g' | sed "s/'/\'/g")
json="{\"type\": \"message\", \"attachments\": [{\"contentType\": \"application/vnd.microsoft.card.adaptive\",\"contentUrl\": null,\"content\": {\"$schema\": \"http://adaptivecards.io/schemas/adaptive-card.json\",\"type\": \"AdaptiveCard\",\"version\": \"1.2\",\"body\": [{\"type\": \"TextBlock\",\"text\": \"$escapedText\", \"color\":\"$COLOR\"}]}}]}"
curl -s -H "Content-Type: application/json" -d "$json" "$TEAMS_WEBHOOK_URL" || logline "Failed to send message to Teams: ${USERNAME}: ${TEXT}"
else
logline "No Slack/Teams: ${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}"
# if [[ "${DRUPAL_VERSION}" == "7" ]]; then
# echo "EXECUTING: drush8 ${DRUSH_ALIAS} en -y --uri=${DOMAIN} letsencrypt_challenge"
# drush8 ${DRUSH_ALIAS} en -y --uri=${DOMAIN} letsencrypt_challenge
# echo "EXECUTING: drush8 ${DRUSH_ALIAS} vset -y --uri=${DOMAIN} letsencrypt_challenge \"${TOKEN_VALUE}\""
# echo "$TOKEN_VALUE" | drush8 ${DRUSH_ALIAS} vset -y --uri=${DOMAIN} letsencrypt_challenge -
# echo "EXECUTING: drush8 ${DRUSH_ALIAS} vset -y --uri=${DOMAIN} letsencrypt_challenge.${DOMAIN} \"${TOKEN_VALUE}\""
# echo "$TOKEN_VALUE" | drush8 ${DRUSH_ALIAS} vset -y --uri=${DOMAIN} letsencrypt_challenge.${DOMAIN} -
# elif [[ "${DRUPAL_VERSION}" == "8" ]]; then
# echo "EXECUTING: drush9 ${DRUSH_ALIAS} en -y --uri=${DOMAIN} letsencrypt_challenge"
# drush9 ${DRUSH_ALIAS} en -y --uri=${DOMAIN} letsencrypt_challenge
# echo "EXECUTING: drush9 ${DRUSH_ALIAS} sset -y --uri=${DOMAIN} letsencrypt_challenge.challenge \"${TOKEN_VALUE}\""
# echo "$TOKEN_VALUE" | drush9 ${DRUSH_ALIAS} sset -y --uri=${DOMAIN} letsencrypt_challenge.challenge -
# echo "EXECUTING: drush9 ${DRUSH_ALIAS} sset -y --uri=${DOMAIN} letsencrypt_challenge.challenge.${DOMAIN} \"${TOKEN_VALUE}\""
# echo "$TOKEN_VALUE" | drush9 ${DRUSH_ALIAS} sset -y --uri=${DOMAIN} letsencrypt_challenge.challenge.${DOMAIN} -
# elif [[ "${DRUPAL_VERSION}" == "9" ]]; then
# echo "EXECUTING: drush9 ${DRUSH_ALIAS} en -y --uri=${DOMAIN} letsencrypt_challenge"
# drush9 ${DRUSH_ALIAS} en -y --uri=${DOMAIN} letsencrypt_challenge
# echo "EXECUTING: drush9 ${DRUSH_ALIAS} sset -y --uri=${DOMAIN} letsencrypt_challenge.challenge \"${TOKEN_VALUE}\""
# echo "$TOKEN_VALUE" | drush9 ${DRUSH_ALIAS} sset -y --uri=${DOMAIN} letsencrypt_challenge.challenge -
# echo "EXECUTING: drush9 ${DRUSH_ALIAS} sset -y --uri=${DOMAIN} letsencrypt_challenge.challenge.${DOMAIN} \"${TOKEN_VALUE}\""
# echo "$TOKEN_VALUE" | drush9 ${DRUSH_ALIAS} sset -y --uri=${DOMAIN} letsencrypt_challenge.challenge.${DOMAIN} -
# else
echo "EXECUTING: drush ${DRUSH_ALIAS} en -y --uri=${DOMAIN} letsencrypt_challenge"
drush ${DRUSH_ALIAS} en -y --uri=${DOMAIN} letsencrypt_challenge
echo "EXECUTING: drush ${DRUSH_ALIAS} sset -y --uri=${DOMAIN} letsencrypt_challenge.challenge \"${TOKEN_VALUE}\""
echo "$TOKEN_VALUE" | drush ${DRUSH_ALIAS} sset -y --uri=${DOMAIN} letsencrypt_challenge.challenge -
echo "EXECUTING: drush ${DRUSH_ALIAS} sset -y --uri=${DOMAIN} letsencrypt_challenge.challenge.${DOMAIN} \"${TOKEN_VALUE}\""
echo "$TOKEN_VALUE" | drush ${DRUSH_ALIAS} sset -y --uri=${DOMAIN} letsencrypt_challenge.challenge.${DOMAIN} -
# fi
}
drush_clean_challenge()
{
DRUSH_ALIAS="${1}"
DRUPAL_VERSION="${2}"
DOMAIN="${3}"
# if [[ "${DRUPAL_VERSION}" == "7" ]]; then
# echo "EXECUTING: drush8 ${DRUSH_ALIAS} dis -y --uri=${DOMAIN} letsencrypt_challenge"
# drush8 ${DRUSH_ALIAS} dis -y --uri=${DOMAIN} letsencrypt_challenge
# echo "EXECUTING: drush8 ${DRUSH_ALIAS} pmu -y --uri=${DOMAIN} letsencrypt_challenge"
# drush8 ${DRUSH_ALIAS} pmu -y --uri=${DOMAIN} letsencrypt_challenge
# elif [[ "${DRUPAL_VERSION}" == "8" ]]; then
# echo "EXECUTING: drush9 ${DRUSH_ALIAS} pmu -y --uri=${DOMAIN} letsencrypt_challenge"
# drush9 ${DRUSH_ALIAS} pmu -y --uri=${DOMAIN} letsencrypt_challenge
# elif [[ "${DRUPAL_VERSION}" == "9" ]]; then
# echo "EXECUTING: drush9 ${DRUSH_ALIAS} pmu -y --uri=${DOMAIN} letsencrypt_challenge"
# drush9 ${DRUSH_ALIAS} pmu -y --uri=${DOMAIN} letsencrypt_challenge
# else
echo "EXECUTING: drush ${DRUSH_ALIAS} pmu -y --uri=${DOMAIN} letsencrypt_challenge"
drush ${DRUSH_ALIAS} pmu -y --uri=${DOMAIN} letsencrypt_challenge
# fi
}