-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.sh
107 lines (97 loc) · 2.9 KB
/
start.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
#!/bin/bash
error_check () {
if [ "$1" = "" ]; then
echo "$2が未入力です"
exit 1
fi
}
delete () {
if [ "${NAMESPACE}" != "" ]; then
kubectl delete cm -n ${NAMESPACE} ${DOMAIN}-$1
else
kubectl delete cm ${DOMAIN}-$1
fi
}
create () {
if [ -e "/etc/letsencrypt/live/${DOMAIN}/$1.pem" ]; then
if [ "${NAMESPACE}" != "" ]; then
kubectl create cm -n ${NAMESPACE} ${DOMAIN}-$1 --from-file="/etc/letsencrypt/live/${DOMAIN}/$1.pem"
else
kubectl create cm ${DOMAIN}-$1 --from-file="/etc/letsencrypt/live/${DOMAIN}/$1.pem"
fi
fi
}
restart () {
if [ "${NAMESPACE}" != "" ]; then
kubectl patch deployment -n ${NAMESPACE} ${DEPNAME} -p \
"{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"reloaded-at\":\"`date +'%Y%m%d%H%M%S'`\"}}}}}"
else
kubectl patch deployment ${DEPNAME} -p \
"{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"reloaded-at\":\"`date +'%Y%m%d%H%M%S'`\"}}}}}"
fi
}
init () {
# 必須チェック
error_check ${EMAIL} "EMAIL"
error_check ${SAKURACLOUD_ACCESS_TOKEN:-""} "SAKURACLOUD_ACCESS_TOKEN"
error_check ${SAKURACLOUD_ACCESS_TOKEN_SECRET:-""} "SAKURACLOUD_ACCESS_TOKEN_SECRET"
# さくらのクラウドDNS情報ファイル作成
cat << EOF > ./.sakura
dns_sakuracloud_api_token = ${SAKURACLOUD_ACCESS_TOKEN}
dns_sakuracloud_api_secret = ${SAKURACLOUD_ACCESS_TOKEN_SECRET}
EOF
# アクセス権限の変更
chmod 0600 ./.sakura
# TIMEOUT設定
SLEEP_TIME_=${SLEEP_TIME:-120}
}
main() {
# ワイルドカードドメイン設定
local WDOMAIN="*.${DOMAIN}"
# 証明書の取得
# ポータルサイト参考:https://free-ssl.jp/command/
if [ ! -e "/etc/letsencrypt/live/${DOMAIN}/" ]; then
CERTBOT_SUBCOMMAND="certonly"
else
CERTBOT_SUBCOMMAND="renew"
CERTBOT_ARGS="${CERTBOT_ARGS} --days 30"
fi
cmd="${CERTBOT_SUBCOMMAND} \
--dns-sakuracloud \
--dns-sakuracloud-credentials ./.sakura \
--dns-sakuracloud-propagation-seconds ${SLEEP_TIME_} \
-d ${WDOMAIN} \
-m ${EMAIL} \
--agree-tos \
--no-eff-email \
--keep-until-expiring ${CERTBOT_ARGS}"
if [ "${DEBUG:-"N"}" = "Y" ]; then
cmd="$cmd --dry-run"
fi
set $cmd
echo $@
certbot "$@"
rm -f ./.sakura
if [ ${K8S_CM:-"Y"} = "Y" ]; then
# kubectlで削除
delete fullchain
delete privkey
# kubectlで作成
create fullchain
create privkey
# コンテナが指定されていたら
if [ "$DEPNAME" != "" ]; then
restart
fi
fi
}
init
for DOMAIN_NS_DP in ${DOMAIN_NS_DPS[@]};
do
# ドメイン:ネームスペース:デプロイメント名
sp=($(echo $DOMAIN_NS_DP | tr ':' ' '))
DOMAIN=${sp[0]}
NAMESPACE=${sp[1]}
DEPNAME=${sp[2]}
main
done