/
sql_to_sync_site.sh
executable file
·156 lines (134 loc) · 4.98 KB
/
sql_to_sync_site.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
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env bash
set -euo pipefail
function finish() {
# Stop background jobs
kill "$(jobs -p)"
}
WP_DB_USERNAME_DC=$(echo "${WP_DB_USERNAME}" | base64 -d)
WP_DB_PASSWORD_DC=$(echo "${WP_DB_PASSWORD}" | base64 -d)
SITE_ENV=$1
CLOUDSQL_INSTANCE=${GOOGLE_PROJECT_ID}:us-central1:${CLOUDSQL_INSTANCE}
export GOOGLE_APPLICATION_CREDENTIALS="/tmp/workspace/src/key.json"
if [ -z ${CIRCLE_TAG+x} ]; then
SQL_TAG=$(date +%Y-%m-%d)
else
SQL_TAG=${CIRCLE_TAG}
fi
export SQL_TAG
export GCLOUD_ZONE=us-central1-a
echo ""
echo "We will try to get connected to: ${CLOUDSQL_INSTANCE}"
echo ""
echo ""
echo "Creating the credential file for mysql"
echo ""
cat <<EOF >mysql.cnf
[client]
user = ${WP_DB_USERNAME_DC}
password = ${WP_DB_PASSWORD_DC}
host = 127.0.0.1
EOF
trap finish EXIT
cloud_sql_proxy \
-instances="${CLOUDSQL_INSTANCE}=tcp:3306" &
echo ""
echo "Creating the credential file for mysql"
echo ""
cat <<EOF >mysql.cnf
[client]
user = ${WP_DB_USERNAME_DC}
password = ${WP_DB_PASSWORD_DC}
host = 127.0.0.1
EOF
mkdir -p content
sleep 2
BUCKET_DESTINATION="gs://${CONTAINER_PREFIX}-source"
MASTER_DB=$(yq -r .job_environments.production_environment.WP_DB_NAME /tmp/workspace/src/.circleci/config.yml)
FILE_TO_IMPORT=${MASTER_DB}-${SQL_TAG}.sql
WP_DB_TO_IMPORT_TO=$(yq -r .job_environments."${SITE_ENV}"_environment.WP_DB_NAME /tmp/workspace/src/.circleci/config.yml)
echo ""
echo "Exporting from $MASTER_DB the file $FILE_TO_IMPORT and importing it to $WP_DB_TO_IMPORT_TO"
echo ""
echo ""
echo "Copying the file from the bucket"
echo ""
gcloud storage cp "${BUCKET_DESTINATION}/${FILE_TO_IMPORT}.gz" "content/${FILE_TO_IMPORT}.gz"
echo ""
echo "Gunzip the file"
echo ""
gunzip "content/${FILE_TO_IMPORT}.gz"
echo ""
echo "Importing the database to the ${WP_DB_TO_IMPORT_TO} database"
echo ""
mysql --defaults-extra-file="mysql.cnf" "${WP_DB_TO_IMPORT_TO}" <"content/${FILE_TO_IMPORT}"
echo ""
echo "Get connected to gcloud"
echo ""
gcloud container clusters get-credentials "${GCLOUD_CLUSTER}" --zone "${GCLOUD_ZONE}" --project "${GOOGLE_PROJECT_ID}"
echo ""
echo "flushing the redis database"
echo ""
flush_redis.sh
SOURCE_BUCKET="${CONTAINER_PREFIX}-stateless"
echo ""
echo "Actually replacing the contents of the release stateless with the production stateless bucket"
echo "Source bucket: $SOURCE_BUCKET"
echo "Target bucket: $WP_STATELESS_BUCKET"
echo ""
gcloud storage rsync gs://"${SOURCE_BUCKET}" gs://"${WP_STATELESS_BUCKET}" --recursive --delete-unmatched-destination-objects
echo ""
echo "Set kubectl command to use the namespace"
echo ""
kc="kubectl -n ${HELM_NAMESPACE}"
echo ""
echo "Find the first php pod in the release ${HELM_RELEASE}"
echo ""
POD=$($kc get pods -l component=php | grep "${HELM_RELEASE}" | head -n1 | cut -d' ' -f1)
echo "Pod: $POD"
echo
# Full db domain and path replacement
# Check if we are in an pathless environment
if [[ $APP_HOSTPATH == "<nil>" ]]; then
OLD_PATH=$(yq -r .job_environments.production_environment.APP_HOSTNAME /tmp/workspace/src/.circleci/config.yml)
NEW_PATH=$(yq -r .job_environments."${SITE_ENV}"_environment.APP_HOSTNAME /tmp/workspace/src/.circleci/config.yml)
else
OLD_PATH=$(yq -r .job_environments.production_environment.APP_HOSTNAME /tmp/workspace/src/.circleci/config.yml)/$APP_HOSTPATH
NEW_PATH=$(yq -r .job_environments."${SITE_ENV}"_environment.APP_HOSTNAME /tmp/workspace/src/.circleci/config.yml)/$APP_HOSTPATH
fi
echo "Domain and path replacement."
echo "We will replace $OLD_PATH with $NEW_PATH"
echo
$kc exec "$POD" -- wp search-replace "$OLD_PATH" "$NEW_PATH" --precise --skip-columns=guid
echo
# Stateless domain and path replacement
# Check if this in .org or .ch domain
if [[ "$APP_HOSTNAME" == *"greenpeace.org"* ]]; then
OLD_PATH="https://www.greenpeace.org/static/${CONTAINER_PREFIX}-stateless/"
NEW_PATH="https://www.greenpeace.org/static/${CONTAINER_PREFIX}-stateless-${SITE_ENV}/"
elif [[ "$APP_HOSTNAME" == *"greenpeace.ch"* ]]; then
NEW_DOMAIN=$(yq -r .job_environments."${SITE_ENV}"_environment.APP_HOSTNAME /tmp/workspace/src/.circleci/config.yml)
OLD_PATH="https://${NEW_DOMAIN}/static/${CONTAINER_PREFIX}-stateless/"
NEW_PATH="https://www.greenpeace.ch/static/${CONTAINER_PREFIX}-stateless-${SITE_ENV}/"
else
OLD_PATH="https://storage.googleapis.com/${CONTAINER_PREFIX}-stateless/"
NEW_PATH="https://storage.googleapis.com/${CONTAINER_PREFIX}-stateless-${SITE_ENV}/"
fi
echo ""
echo "Stateless domain and path replacement."
echo "We will replace the path $OLD_PATH with $NEW_PATH for the images themselves"
echo ""
$kc exec "$POD" -- wp search-replace "$OLD_PATH" "$NEW_PATH" --precise --skip-columns=guid --verbose
echo
echo "Discourage search engines from indexing dev/stage sites"
echo
$kc exec "$POD" -- wp option update blog_public 0
echo
echo "Disable Cloudflare cache purging on deployments"
echo
$kc exec "$POD" -- wp option patch delete planet4_features cloudflare_deploy_purge || true
echo "Remove GF addons authentication"
$kc exec "$POD" -- wp p4-gf-addons disconnect
echo ""
echo "Flushing cache"
echo ""
$kc exec "$POD" -- wp cache flush