@ -2,7 +2,7 @@
# переменные, которые должны быть определены перед запуском
# переменные, которые должны быть определены перед запуском
# export SL_Ver="v1" - версия API: 'v2' (actual) или 'v1' (legacy).
# export SL_Ver="v1" - версия API: 'v2' (actual) или 'v1' (legacy).
# По-умолчанию: v2
# По-умолчанию: v1
# Если SL_Ver="v1"
# Если SL_Ver="v1"
# export SL_Key="API_KEY" - Токен Selectel (API key)
# export SL_Key="API_KEY" - Токен Selectel (API key)
# Посмотреть или создать можно в панели управления в правом верхнем углу откройте меню Профиль и настройки -> Ключи API.
# Посмотреть или создать можно в панели управления в правом верхнем углу откройте меню Профиль и настройки -> Ключи API.
@ -32,7 +32,6 @@ dns_selectel_add() {
fulldomain = $1
fulldomain = $1
txtvalue = $2
txtvalue = $2
#if ! _sl_init_vars; then
if ! _sl_init_vars; then
if ! _sl_init_vars; then
return 1
return 1
fi
fi
@ -66,7 +65,6 @@ dns_selectel_add() {
_ext_srv2 = "/records/"
_ext_srv2 = "/records/"
_data = " {\"type\":\"TXT\",\"ttl\":60,\"name\":\" $fulldomain \",\"content\":\" $txtvalue \"} "
_data = " {\"type\":\"TXT\",\"ttl\":60,\"name\":\" $fulldomain \",\"content\":\" $txtvalue \"} "
else
else
#not valid
_err " Error. Unsupported version API $SL_Ver "
_err " Error. Unsupported version API $SL_Ver "
return 1
return 1
fi
fi
@ -83,29 +81,27 @@ dns_selectel_add() {
# запись TXT с $fulldomain уже существует
# запись TXT с $fulldomain уже существует
if [ " $SL_Ver " = "v2" ] ; then
if [ " $SL_Ver " = "v2" ] ; then
# надо добавить к существующей записи еще один content
# надо добавить к существующей записи еще один content
#
# считать записи rrset
# считать записи rrset
_debug "Getting txt records"
_debug "Getting txt records"
_sl_rest GET " ${ _ext_uri } "
_sl_rest GET " ${ _ext_uri } "
# Если в данной записи, есть текстовое значение $txtvalue,
# то все хорошо, добавлять ничего не надо и результат успешный
# Уже есть значение $txtvalue, добавлять не надо
if _contains " $response " " $txtvalue " ; then
if _contains " $response " " $txtvalue " ; then
_info "Added, OK"
_info "Added, OK"
_info " Txt record ${ fulldomain } со значением ${ txtvalue } already exists "
_info " Txt record ${ fulldomain } со значением ${ txtvalue } already exists "
return 0
return 0
fi
fi
# группа \1 - полная запись rrset; группа \2 - значение records:[{"content":"\"v1\""},{"content":"\"v2\""}",...] , а именно {"content":"\"v1\""},{"content":"\"v2\""}",...
# группа \1 - полная запись rrset; группа \2 - значение атрибута records, а именно {"content":"\"value 1\""},{"content":"\"value 2\""}",...
_record_seg = " $( echo " $response " | sed -En " s/.*(\{\"id\"[^}]* ${ fulldomain } [^}]*records[^}]*\[(\{[^]]*\})\][^}]*}).*/\1/p " ) "
_record_seg = " $( echo " $response " | sed -En " s/.*(\{\"id\"[^}]* ${ fulldomain } [^}]*records[^}]*\[(\{[^]]*\})\][^}]*}).*/\1/p " ) "
_record_array = " $( echo " $response " | sed -En " s/.*(\{\"id\"[^}]* ${ fulldomain } [^}]*records[^}]*\[(\{[^]]*\})\][^}]*}).*/\2/p " ) "
_record_array = " $( echo " $response " | sed -En " s/.*(\{\"id\"[^}]* ${ fulldomain } [^}]*records[^}]*\[(\{[^]]*\})\][^}]*}).*/\2/p " ) "
# record id
# record id
_record_id = " $( echo " $_record_seg " | tr "," "\n" | tr "}" "\n" | tr -d " " | grep "\"id\"" | cut -d : -f 2 | tr -d "\"" ) "
_record_id = " $( echo " $_record_seg " | tr "," "\n" | tr "}" "\n" | tr -d " " | grep "\"id\"" | cut -d : -f 2 | tr -d "\"" ) "
# готовим _data
_tmp_str = " ${ _record_array } ,{\"content\":\" ${ _text_tmp } \"} "
_tmp_str = " ${ _record_array } ,{\"content\":\" ${ _text_tmp } \"} "
_data = " {\"ttl\": 60, \"records\": [ ${ _tmp_str } ]} "
_data = " {\"ttl\": 60, \"records\": [ ${ _tmp_str } ]} "
_debug3 _record_seg " $_record_seg "
_debug3 _record_seg " $_record_seg "
_debug3 _record_array " $_record_array "
_debug3 _record_array " $_record_array "
_debug3 _record_array " $_record_id "
_debug3 _record_array " $_record_id "
_debug3 _data " $_data "
# вызов REST API PATCH
_debug2 "New data for record" " $_data "
if _sl_rest PATCH " ${ _ext_uri } ${ _record_id } " " $_data " ; then
if _sl_rest PATCH " ${ _ext_uri } ${ _record_id } " " $_data " ; then
_info "Added, OK"
_info "Added, OK"
return 0
return 0
@ -124,7 +120,7 @@ dns_selectel_add() {
dns_selectel_rm( ) {
dns_selectel_rm( ) {
fulldomain = $1
fulldomain = $1
txtvalue = $2
txtvalue = $2
#SL_Key="${SL_Key:-$(_readaccountconf_mutable SL_Key)}"
if ! _sl_init_vars "nosave" ; then
if ! _sl_init_vars "nosave" ; then
return 1
return 1
fi
fi
@ -151,7 +147,6 @@ dns_selectel_rm() {
_ext_srv1 = "/"
_ext_srv1 = "/"
_ext_srv2 = "/records/"
_ext_srv2 = "/records/"
else
else
#not valid
_err " Error. Unsupported version API $SL_Ver "
_err " Error. Unsupported version API $SL_Ver "
return 1
return 1
fi
fi
@ -169,13 +164,9 @@ dns_selectel_rm() {
if [ " $SL_Ver " = "v2" ] ; then
if [ " $SL_Ver " = "v2" ] ; then
_record_seg = " $( echo " $response " | sed -En " s/.*(\{\"id\"[^}]*records[^[]*(\[(\{[^]]* ${ txtvalue } [^]]*)\])[^}]*}).*/\1/gp " ) "
_record_seg = " $( echo " $response " | sed -En " s/.*(\{\"id\"[^}]*records[^[]*(\[(\{[^]]* ${ txtvalue } [^]]*)\])[^}]*}).*/\1/gp " ) "
_record_arr = " $( echo " $response " | sed -En " s/.*(\{\"id\"[^}]*records[^[]*(\[(\{[^]]* ${ txtvalue } [^]]*)\])[^}]*}).*/\3/p " ) "
_record_arr = " $( echo " $response " | sed -En " s/.*(\{\"id\"[^}]*records[^[]*(\[(\{[^]]* ${ txtvalue } [^]]*)\])[^}]*}).*/\3/p " ) "
#_record_id="$(echo "$_record_seg" | tr "," "\n" | tr "}" "\n" | tr -d " " | grep "\"id\"" | cut -d : -f 2)"
elif [ " $SL_Ver " = "v1" ] ; then
elif [ " $SL_Ver " = "v1" ] ; then
_record_seg = " $( echo " $response " | _egrep_o " [^{]*\"content\" *: *\" $txtvalue \"[^}]*} " ) "
_record_seg = " $( echo " $response " | _egrep_o " [^{]*\"content\" *: *\" $txtvalue \"[^}]*} " ) "
# record id
#_record_id="$(echo "$_record_seg" | tr "," "\n" | tr "}" "\n" | tr -d " " | grep "\"id\"" | cut -d : -f 2)"
else
else
#not valid
_err " Error. Unsupported version API $SL_Ver "
_err " Error. Unsupported version API $SL_Ver "
return 1
return 1
fi
fi
@ -185,7 +176,7 @@ dns_selectel_rm() {
return 1
return 1
fi
fi
# record id
# record id
_record_id = " $( echo " $_record_seg " | tr "," "\n" | tr "}" "\n" | tr -d " " | grep "\"id\"" | cut -d : -f 2 | tr -d "\"" ) "
_record_id = " $( echo " $_record_seg " | tr "," "\n" | tr "}" "\n" | tr -d " " | grep "\"id\"" | cut -d : -f 2 | tr -d "\"" | sed '1!d' ) "
if [ -z " $_record_id " ] ; then
if [ -z " $_record_id " ] ; then
_err "can not find _record_id"
_err "can not find _record_id"
return 1
return 1
@ -194,7 +185,6 @@ dns_selectel_rm() {
# delete all record type TXT with text $txtvalue
# delete all record type TXT with text $txtvalue
if [ " $SL_Ver " = "v2" ] ; then
if [ " $SL_Ver " = "v2" ] ; then
# actual
# actual
#del_txt='it47Qq60vJuzQJXb9WEaapciTwtt1gb_14gm1ubwzrA';
_new_arr = " $( echo " $_record_seg " | sed -En " s/.*(\{\"id\"[^}]*records[^[]*(\[(\{[^]]* ${ txtvalue } [^]]*)\])[^}]*}).*/\3/gp " | sed -En "s/(\},\{)/}\n{/gp" | sed " / ${ txtvalue } /d " | sed ":a;N;s/\n/,/;ta" ) "
_new_arr = " $( echo " $_record_seg " | sed -En " s/.*(\{\"id\"[^}]*records[^[]*(\[(\{[^]]* ${ txtvalue } [^]]*)\])[^}]*}).*/\3/gp " | sed -En "s/(\},\{)/}\n{/gp" | sed " / ${ txtvalue } /d " | sed ":a;N;s/\n/,/;ta" ) "
# uri record for DEL or PATCH
# uri record for DEL or PATCH
_del_uri = " ${ _ext_uri } ${ _record_id } "
_del_uri = " ${ _ext_uri } ${ _record_id } "
@ -232,14 +222,10 @@ dns_selectel_rm() {
}
}
#################### Private functions below ##################################
#################### Private functions below ##################################
#_acme-challenge.www.domain.com
#returns
# _sub_domain=_acme-challenge.www
# _domain=domain.com
# _domain_id=sdjkglgdfewsdfg
_get_root( ) {
_get_root( ) {
domain = $1
domain = $1
#
if [ " $SL_Ver " = 'v1' ] ; then
if [ " $SL_Ver " = 'v1' ] ; then
# version API 1
# version API 1
if ! _sl_rest GET "/" ; then
if ! _sl_rest GET "/" ; then
@ -248,16 +234,12 @@ _get_root() {
i = 2
i = 2
p = 1
p = 1
while true; do
while true; do
#h=$(printf "%s" "$domain" | cut -d . -f $i-100)
h = $( printf "%s" " $domain " | cut -d . -f " $i " -100)
h = $( printf "%s" " $domain " | cut -d . -f " $i " -100)
_debug h " $h "
_debug h " $h "
if [ -z " $h " ] ; then
if [ -z " $h " ] ; then
#not valid
return 1
return 1
fi
fi
if _contains " $response " " \"name\" *: *\" $h \", " ; then
if _contains " $response " " \"name\" *: *\" $h \", " ; then
#_sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
_sub_domain = $( printf "%s" " $domain " | cut -d . -f 1-" $p " )
_sub_domain = $( printf "%s" " $domain " | cut -d . -f 1-" $p " )
_domain = $h
_domain = $h
_debug " Getting domain id for $h "
_debug " Getting domain id for $h "
@ -280,7 +262,6 @@ _get_root() {
_debug "domain:: " " $domain "
_debug "domain:: " " $domain "
# read records of all domains
# read records of all domains
if ! _sl_rest GET " $_ext_uri " ; then
if ! _sl_rest GET " $_ext_uri " ; then
#not valid
_err " Error read records of all domains $SL_Ver "
_err " Error read records of all domains $SL_Ver "
return 1
return 1
fi
fi
@ -290,29 +271,24 @@ _get_root() {
h = $( printf "%s" " $domain " | cut -d . -f " $i " -100)
h = $( printf "%s" " $domain " | cut -d . -f " $i " -100)
_debug h " $h "
_debug h " $h "
if [ -z " $h " ] ; then
if [ -z " $h " ] ; then
#not valid
_err "The domain was not found among the registered ones"
_err "The domain was not found among the registered ones"
return 1
return 1
fi
fi
_domain_record = $( echo " $response " | sed -En " s/.*(\{[^}]*id[^}]*\"name\" *: *\" $h \"[^}]*}).*/\1/p " )
_domain_record = $( echo " $response " | sed -En " s/.*(\{[^}]*id[^}]*\"name\" *: *\" $h \"[^}]*}).*/\1/p " )
_debug "_domain_record:: " " $_domain_record "
_debug "_domain_record:: " " $_domain_record "
if [ -n " $_domain_record " ] ; then
if [ -n " $_domain_record " ] ; then
_sub_domain = $( printf "%s" " $domain " | cut -d . -f 1-" $p " )
_sub_domain = $( printf "%s" " $domain " | cut -d . -f 1-" $p " )
_domain = $h
_domain = $h
_debug " Getting domain id for $h "
_debug " Getting domain id for $h "
#_domain_id="$(echo "$_domain_record" | tr "," "\n" | tr "}" "\n" | tr -d " " | grep "\"id\":" | cut -d : -f 2 | sed -En "s/\"([^\"]*)\"/\1\p")"
_domain_id = $( echo " $_domain_record " | sed -En "s/\{[^}]*\"id\" *: *\"([^\"]*)\"[^}]*\}/\1/p" )
_domain_id = $( echo " $_domain_record " | sed -En "s/\{[^}]*\"id\" *: *\"([^\"]*)\"[^}]*\}/\1/p" )
return 0
return 0
fi
fi
p = $i
p = $i
i = $( _math " $i " + 1)
i = $( _math " $i " + 1)
done
done
#not valid
_err " Error read records of all domains $SL_Ver "
_err " Error read records of all domains $SL_Ver "
return 1
return 1
else
else
#not valid
_err " Error. Unsupported version API $SL_Ver "
_err " Error. Unsupported version API $SL_Ver "
return 1
return 1
fi
fi
@ -341,14 +317,12 @@ _sl_rest() {
_debug3 "Full URI: " " $SL_Api / ${ SL_Ver } ${ ep } "
_debug3 "Full URI: " " $SL_Api / ${ SL_Ver } ${ ep } "
_debug3 "_H1:" " $_H1 "
_debug3 "_H1:" " $_H1 "
_debug3 "_H2:" " $_H2 "
_debug3 "_H2:" " $_H2 "
if [ " $m " != "GET" ] ; then
if [ " $m " != "GET" ] ; then
_debug data " $data "
_debug data " $data "
response = " $( _post " $data " " $SL_Api / ${ SL_Ver } ${ ep } " "" " $m " ) "
response = " $( _post " $data " " $SL_Api / ${ SL_Ver } ${ ep } " "" " $m " ) "
else
else
response = " $( _get " $SL_Api / ${ SL_Ver } ${ ep } " ) "
response = " $( _get " $SL_Api / ${ SL_Ver } ${ ep } " ) "
fi
fi
if [ " $? " != "0" ] ; then
if [ " $? " != "0" ] ; then
_err " error $ep "
_err " error $ep "
return 1
return 1
@ -357,8 +331,6 @@ _sl_rest() {
return 0
return 0
}
}
#################################################################3
# use:
_get_auth_token( ) {
_get_auth_token( ) {
if [ " $SL_Ver " = 'v1' ] ; then
if [ " $SL_Ver " = 'v1' ] ; then
# token for v1
# token for v1
@ -375,7 +347,7 @@ _get_auth_token() {
# field 3 - SL_Login_ID
# field 3 - SL_Login_ID
# field 4 - SL_Project_Name
# field 4 - SL_Project_Name
# field 5 - Receipt time
# field 5 - Receipt time
# separator - '; '
# separator - '$_sl_sep '
_login_name = $( _getfield " $token_v2 " 1 " $_sl_sep " )
_login_name = $( _getfield " $token_v2 " 1 " $_sl_sep " )
_token_keystone = $( _getfield " $token_v2 " 2 " $_sl_sep " )
_token_keystone = $( _getfield " $token_v2 " 2 " $_sl_sep " )
_project_name = $( _getfield " $token_v2 " 4 " $_sl_sep " )
_project_name = $( _getfield " $token_v2 " 4 " $_sl_sep " )
@ -386,7 +358,6 @@ _get_auth_token() {
_debug3 _project_name " $_project_name "
_debug3 _project_name " $_project_name "
_debug3 _receipt_time " $( date -d @" $_receipt_time " -u) "
_debug3 _receipt_time " $( date -d @" $_receipt_time " -u) "
# check the validity of the token for the user and the project and its lifetime
# check the validity of the token for the user and the project and its lifetime
#_dt_diff_minute=$(( ( $(EPOCHSECONDS)-$_receipt_time )/60 ))
_dt_diff_minute = $(( ( $( date +%s) - _receipt_time) / 60 ))
_dt_diff_minute = $(( ( $( date +%s) - _receipt_time) / 60 ))
_debug3 _dt_diff_minute " $_dt_diff_minute "
_debug3 _dt_diff_minute " $_dt_diff_minute "
[ " $_dt_diff_minute " -gt " $SL_Expire " ] && unset _token_keystone
[ " $_dt_diff_minute " -gt " $SL_Expire " ] && unset _token_keystone
@ -399,13 +370,9 @@ _get_auth_token() {
# the previous token is incorrect or was not received, get a new one
# the previous token is incorrect or was not received, get a new one
_debug "Update (get new) token"
_debug "Update (get new) token"
_data_auth = " {\"auth\":{\"identity\":{\"methods\":[\"password\"],\"password\":{\"user\":{\"name\":\" ${ SL_Login_Name } \",\"domain\":{\"name\":\" ${ SL_Login_ID } \"},\"password\":\" ${ SL_Pswd } \"}}},\"scope\":{\"project\":{\"name\":\" ${ SL_Project_Name } \",\"domain\":{\"name\":\" ${ SL_Login_ID } \"}}}}} "
_data_auth = " {\"auth\":{\"identity\":{\"methods\":[\"password\"],\"password\":{\"user\":{\"name\":\" ${ SL_Login_Name } \",\"domain\":{\"name\":\" ${ SL_Login_ID } \"},\"password\":\" ${ SL_Pswd } \"}}},\"scope\":{\"project\":{\"name\":\" ${ SL_Project_Name } \",\"domain\":{\"name\":\" ${ SL_Login_ID } \"}}}}} "
#_secure_debug2 "_data_auth" "$_data_auth"
export _H1 = "Content-Type: application/json"
export _H1 = "Content-Type: application/json"
# body url [needbase64] [POST|PUT|DELETE] [ContentType]
_result = $( _post " $_data_auth " " $auth_uri " )
_result = $( _post " $_data_auth " " $auth_uri " )
_token_keystone = $( grep 'x-subject-token' " $HTTP_HEADER " | sed -nE "s/[[:space:]]*x-subject-token:[[:space:]]*([[:print:]]*)(\r*)/\1/p" )
_token_keystone = $( grep 'x-subject-token' " $HTTP_HEADER " | sed -nE "s/[[:space:]]*x-subject-token:[[:space:]]*([[:print:]]*)(\r*)/\1/p" )
#echo $_token_keystone > /root/123456.qwe
#_dt_curr=$EPOCHSECONDS
_dt_curr = $( date +%s)
_dt_curr = $( date +%s)
SL_Token_V2 = " ${ SL_Login_Name } ${ _sl_sep } ${ _token_keystone } ${ _sl_sep } ${ SL_Login_ID } ${ _sl_sep } ${ SL_Project_Name } ${ _sl_sep } ${ _dt_curr } "
SL_Token_V2 = " ${ SL_Login_Name } ${ _sl_sep } ${ _token_keystone } ${ _sl_sep } ${ SL_Login_ID } ${ _sl_sep } ${ SL_Project_Name } ${ _sl_sep } ${ _dt_curr } "
_saveaccountconf_mutable SL_Token_V2 " $SL_Token_V2 "
_saveaccountconf_mutable SL_Token_V2 " $SL_Token_V2 "
@ -427,14 +394,13 @@ _sl_init_vars() {
# version API
# version API
SL_Ver = " ${ SL_Ver :- $( _readaccountconf_mutable SL_Ver) } "
SL_Ver = " ${ SL_Ver :- $( _readaccountconf_mutable SL_Ver) } "
if [ -z " $SL_Ver " ] ; then
if [ -z " $SL_Ver " ] ; then
SL_Ver = "v1 "
SL_Ver = "v2 "
fi
fi
if ! [ " $SL_Ver " = "v1" ] && ! [ " $SL_Ver " = "v2" ] ; then
if ! [ " $SL_Ver " = "v1" ] && ! [ " $SL_Ver " = "v2" ] ; then
_err "You don't specify selectel.ru API version."
_err "You don't specify selectel.ru API version."
_err "Please define specify API version."
_err "Please define specify API version."
fi
fi
_debug2 SL_Ver " $SL_Ver "
_debug2 SL_Ver " $SL_Ver "
if [ " $SL_Ver " = "v1" ] ; then
if [ " $SL_Ver " = "v1" ] ; then
# token
# token
SL_Key = " ${ SL_Key :- $( _readaccountconf_mutable SL_Key) } "
SL_Key = " ${ SL_Key :- $( _readaccountconf_mutable SL_Key) } "
@ -509,9 +475,9 @@ _sl_init_vars() {
_err "Please provide the correct API version and try again."
_err "Please provide the correct API version and try again."
return 1
return 1
fi
fi
if [ -z " $_non_save " ] ; then
if [ -z " $_non_save " ] ; then
_saveaccountconf_mutable SL_Ver " $SL_Ver "
_saveaccountconf_mutable SL_Ver " $SL_Ver "
fi
fi
return 0
return 0
}
}