Untitled diff

생성일 비교 결과 만료 없음
59 삭제
348 행
74 추가
310 행
#!/bin/bash
#!/bin/bash
# Copyright 2016 Google, Inc.
# Copyright 2016 Google, Inc.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing, software
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.


# This init script installs a cloud-sql-proxy on each node in the cluster, and
# This init script installs a cloud-sql-proxy on each node in the cluster, and
# uses that proxy to expose TCP proxies of one or more CloudSQL instances.
# uses that proxy to expose TCP proxies of one or more CloudSQL instances.
# One of these instances is used for the clusters Hive Metastore.
# One of these instances is used for the clusters Hive Metastore.


# Do not use "set -x" to avoid printing passwords in clear in the logs
# Do not use "set -x" to avoid printing passwords in clear in the logs
set -euo pipefail
set -euo pipefail


# Whether to configure the Hive metastore to point to a Cloud SQL database.
# Whether to configure the Hive metastore to point to a Cloud SQL database.
# This is not required for Hive & Spark I/O.
# This is not required for Hive & Spark I/O.

HIVE_USER_DEFAULT='${sql_user}'
HIVE_PASSWORD_DEFAULT='${sql_password}'
SQL_USER_DEFAULT='root'
SQL_PASSWORD_DEFAULT='${sql_password}'

readonly enable_cloud_sql_metastore="$(/usr/share/google/get_metadata_value attributes/enable-cloud-sql-hive-metastore || echo 'true')"
readonly enable_cloud_sql_metastore="$(/usr/share/google/get_metadata_value attributes/enable-cloud-sql-hive-metastore || echo 'true')"


# Whether to enable the proxy on workers. This is not necessary for the
# Whether to enable the proxy on workers. This is not necessary for the
# Metastore, but is required for Hive & Spark I/O.
# Metastore, but is required for Hive & Spark I/O.
readonly enable_proxy_on_workers="$(/usr/share/google/get_metadata_value attributes/enable-cloud-sql-proxy-on-workers || echo 'true')"
readonly enable_proxy_on_workers="$(/usr/share/google/get_metadata_value attributes/enable-cloud-sql-proxy-on-workers || echo 'true')"


# Database user to use to access metastore.
# Database user to use to access metastore.
readonly db_hive_user="$(/usr/share/google/get_metadata_value attributes/db-hive-user || echo 'hive')"
readonly db_hive_user="$(/usr/share/google/get_metadata_value attributes/db-hive-user || echo $HIVE_USER_DEFAULT)"


readonly db_admin_user="$(/usr/share/google/get_metadata_value attributes/db-admin-user || echo 'root')"
readonly db_admin_user="$(/usr/share/google/get_metadata_value attributes/db-admin-user || echo $SQL_USER_DEFAULT)"


readonly kms_key_uri="$(/usr/share/google/get_metadata_value attributes/kms-key-uri)"
readonly kms_key_uri="$(/usr/share/google/get_metadata_value attributes/kms-key-uri)"


# Database admin user password used to create the metastore database and user.
# Database admin user password used to create the metastore database and user.
readonly db_admin_password_uri="$(/usr/share/google/get_metadata_value attributes/db-admin-password-uri)"
readonly db_admin_password_uri="$(/usr/share/google/get_metadata_value attributes/db-admin-password-uri)"
if [[ -n "${db_admin_password_uri}" ]]; then
if [[ -n "$${db_admin_password_uri}" ]]; then
# Decrypt password
# Decrypt password
readonly db_admin_password="$(gsutil cat $db_admin_password_uri | \
readonly db_admin_password="$(gsutil cat $db_admin_password_uri | \
gcloud kms decrypt \
gcloud kms decrypt \
--ciphertext-file - \
--ciphertext-file - \
--plaintext-file - \
--plaintext-file - \
--key $kms_key_uri)"
--key $kms_key_uri)"
else
else
readonly db_admin_password=''
readonly db_admin_password=$SQL_PASSWORD_DEFAULT
fi
fi
if [ "${db_admin_password}" == "" ]; then
if [ "$${db_admin_password}" == "" ]; then
readonly db_admin_password_parameter=""
readonly db_admin_password_parameter=""
else
else
readonly db_admin_password_parameter="-p${db_admin_password}"
readonly db_admin_password_parameter="-p$${db_admin_password}"
fi
fi


# Database password to use to access metastore.
# Database password to use to access metastore.
readonly db_hive_password_uri="$(/usr/share/google/get_metadata_value attributes/db-hive-password-uri)"
readonly db_hive_password_uri="$(/usr/share/google/get_metadata_value attributes/db-hive-password-uri)"
if [[ -n "${db_hive_password_uri}" ]]; then
if [[ -n "$${db_hive_password_uri}" ]]; then
# Decrypt password
# Decrypt password
readonly db_hive_password="$(gsutil cat $db_hive_password_uri | \
readonly db_hive_password="$(gsutil cat $db_hive_password_uri | \
gcloud kms decrypt \
gcloud kms decrypt \
--ciphertext-file - \
--ciphertext-file - \
--plaintext-file - \
--plaintext-file - \
--key $kms_key_uri)"
--key $kms_key_uri)"
else
else
readonly db_hive_password='hive-password'
readonly db_hive_password=$HIVE_PASSWORD_DEFAULT
fi
fi
if [ "${db_hive_password}" == "" ]; then
if [ "$${db_hive_password}" == "" ]; then
readonly db_hive_password_parameter=""
readonly db_hive_password_parameter=""
else
else
readonly db_hive_password_parameter="-p${db_hive_password}"
readonly db_hive_password_parameter="-p$${db_hive_password}"
fi
fi


readonly PROXY_DIR='/var/run/cloud_sql_proxy'
readonly PROXY_DIR='/var/run/cloud_sql_proxy'
readonly PROXY_BIN='/usr/local/bin/cloud_sql_proxy'
readonly PROXY_BIN='/usr/local/bin/cloud_sql_proxy'
readonly INIT_SCRIPT='/usr/lib/systemd/system/cloud-sql-proxy.service'
readonly INIT_SCRIPT='/usr/lib/systemd/system/cloud-sql-proxy.service'
readonly ADDITIONAL_INSTANCES_KEY='attributes/additional-cloud-sql-instances'
readonly ADDITIONAL_INSTANCES_KEY='attributes/additional-cloud-sql-instances'


# Dataproc master nodes information
readonly DATAPROC_MASTER=$(/usr/share/google/get_metadata_value attributes/dataproc-master)

function err() {
function err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
return 1
return 1
}
}


# Helper to run any command with Fibonacci backoff.
# Helper to run any command with Fibonacci backoff.
# If all retries fail, returns last attempt's exit code.
# If all retries fail, returns last attempt's exit code.
# Args: "$@" is the command to run.
# Args: "$@" is the command to run.
function run_with_retries() {
function run_with_retries() {
local retry_backoff=(1 1 2 3 5 8 13 21 34 55 89 144)
local retry_backoff=(1 1 2 3 5 8 13 21 34 55 89 144)
local -a cmd=("$@")
local -a cmd=("$@")
echo "About to run '${cmd[*]}' with retries..."
echo "About to run '$${cmd[*]}' with retries..."


local update_succeeded=0
local update_succeeded=0
for ((i = 0; i < ${#retry_backoff[@]}; i++)); do
for ((i = 0; i < $${#retry_backoff[@]}; i++)); do
if "${cmd[@]}"; then
if "$${cmd[@]}"; then
update_succeeded=1
update_succeeded=1
break
break
else
else
local sleep_time=${retry_backoff[$i]}
local sleep_time=$${retry_backoff[$i]}
echo "'${cmd[*]}' attempt $(( $i + 1 )) failed! Sleeping ${sleep_time}." >&2
echo "'$${cmd[*]}' attempt $(( $i + 1 )) failed! Sleeping $${sleep_time}." >&2
sleep ${sleep_time}
sleep $${sleep_time}
fi
fi