You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
5.2 KiB

  1. #!/usr/bin/env sh
  2. #
  3. # DNS Integration for IBM Bluemix (formerly SoftLayer)
  4. #
  5. # Author: luizgn
  6. # Based on sample from Neilpang
  7. # Report Bugs here: https://github.com/luizgn/acme.sh
  8. #
  9. ######## Public functions #####################
  10. BLUEMIX_API_URL="https://${BLUEMIX_USER}:${BLUEMIX_KEY}@api.softlayer.com/rest/v3"
  11. domainId=
  12. domain=
  13. host=
  14. recordId=
  15. dns_bluemix_add() {
  16. fulldomain=$1
  17. txtvalue=$2
  18. _info "Attempting to add ${fulldomain} with ${txtvalue} into Bluemix's DNS."
  19. # Curl is required
  20. if ! type curl >/dev/null; then
  21. _err "curl missing. Please isntall curl"
  22. return 1
  23. fi
  24. # BLUEMIX_USER is required
  25. if [[ -z "${BLUEMIX_USER}" ]]; then
  26. _err "Environment variable BLUEMIX_USER not defined"
  27. return 1
  28. fi
  29. # BLUEMIX_KEY is required
  30. if [[ -z "${BLUEMIX_KEY}" ]]; then
  31. _err "Environment variable BLUEMIX_KEY not defined"
  32. return 1
  33. fi
  34. # Get right domain and domain id
  35. getDomain ${fulldomain}
  36. # Did we find domain?
  37. if [[ -z "${domain}" ]]; then
  38. return 1
  39. fi
  40. # Check if this DNS entry already exists
  41. getRecordId "${domainId}" "${host}"
  42. if [[ -z "${recordId}" ]]; then
  43. # Create record if it doesn't exist
  44. createTxtRecord "${domainId}" "${host}" "${txtvalue}"
  45. else
  46. # Update Record if it already exists
  47. updateTxtRecord "${recordId}" "${txtvalue}"
  48. fi
  49. return 0
  50. }
  51. #Usage: fulldomain txtvalue
  52. #Remove the txt record after validation.
  53. dns_bluemix_rm() {
  54. fulldomain=$1
  55. _info "Attempting to delete ${fulldomain} from Bluemix"
  56. # Curl is required
  57. if ! type curl >/dev/null; then
  58. _err "curl missing. Please isntall curl"
  59. return 1
  60. fi
  61. # BLUEMIX_USER is required
  62. if [[ -z "${BLUEMIX_USER}" ]]; then
  63. _err "Environment variable BLUEMIX_USER not defined"
  64. return 1
  65. fi
  66. # BLUEMIX_KEY is required
  67. if [[ -z "${BLUEMIX_KEY}" ]]; then
  68. _err "Environment variable BLUEMIX_KEY not defined"
  69. return 1
  70. fi
  71. # Get Domain ID
  72. getDomain ${fulldomain}
  73. if [[ -z "${domain}" ]]; then
  74. return 1
  75. fi
  76. # Get DNS entry in this Domain
  77. getRecordId "${domainId}" "${host}"
  78. if [[ -z "${recordId}" ]]; then
  79. _info "recordId for ${fulldomain} not found."
  80. return 1
  81. fi
  82. # Remove record
  83. deleteRecordId "${recordId}"
  84. return 0
  85. }
  86. #################### Private functions below ##################################
  87. function getDomain {
  88. fulldomain=$1
  89. output=$(curl -s -X GET "${BLUEMIX_API_URL}/SoftLayer_Account/getDomains")
  90. if [[ "${output}" =~ '"error":"Access Denied. "' ]]; then
  91. _err "Access Denied, check BLUEMIX_USER and BLUEMIX_KEY environment variables. Details: ${output}"
  92. return 1
  93. fi
  94. for domain_item in $(echo "${output}" | awk 'BEGIN{RS=","}/"name"/' | cut -f4 -d'"'); do
  95. if [[ "${fulldomain}" =~ ${domain_item}$ ]]; then
  96. domain="${domain_item}"
  97. break
  98. fi
  99. done
  100. if [[ -z "${domain}" ]]; then
  101. _err "Domain for ${fulldomain} was not found in this Bluemix account"
  102. return 1
  103. fi
  104. domainId=$(echo "${output}" | \
  105. awk -v DOMAIN=${domain} '
  106. BEGIN {
  107. RS=",";
  108. FS=":";
  109. }
  110. {
  111. if($1~"\"id\"") {
  112. id=$2;
  113. } else if($1~"\"name\"") {
  114. split($2,d,"\"");
  115. domain=d[2];
  116. }
  117. if($0~/\}$/ && domain==DOMAIN) {
  118. print id;
  119. }
  120. }
  121. ')
  122. host=$(echo "${fulldomain}" | sed "s/\.${domain}\$//g")
  123. _debug "Host is ${host}, domain is ${domain} and domain id is ${domainId}"
  124. }
  125. function getRecordId {
  126. domainId=$1
  127. host=$2
  128. output=$(curl -s -X GET "${BLUEMIX_API_URL}/SoftLayer_Dns_Domain/${domainId}/getResourceRecords")
  129. recordId=$(echo "${output}" | \
  130. awk -v HOST=${host} '
  131. BEGIN {
  132. RS=",";
  133. FS=":";
  134. }
  135. {
  136. if($1=="\"host\"") {
  137. host=$2;
  138. } else if($1=="\"id\"") {
  139. id=$2;
  140. }
  141. if($0~/[\}|\]]$/ && host==("\"" HOST "\"")) {
  142. print id;
  143. }
  144. }
  145. ')
  146. _debug "RecordId is ${recordId}"
  147. }
  148. function createTxtRecord {
  149. domainId=$1
  150. host=$2
  151. txtvalue=$3
  152. payload="{\"parameters\":[{\"host\":\"${host}\",\"data\":\"${txtvalue}\",\"ttl\":\"900\",\"type\":\"txt\",\"domainId\":\"${domainId}\"}]}"
  153. output=$(curl -s -X POST -d "${payload}" "${BLUEMIX_API_URL}/SoftLayer_Dns_Domain_ResourceRecord")
  154. rc=$?
  155. if [[ "${rc}" == "0" && "${output}" =~ \"host\":\"${host}\" ]]; then
  156. _info "${fulldomain} added into Bluemix's DNS."
  157. _debug ${output}
  158. else
  159. _err "Error adding ${fulldomain} in Bluemix's DNS. Details: ${output}"
  160. fi
  161. }
  162. function updateTxtRecord {
  163. recordId=$1
  164. txtvalue=$2
  165. payload="{\"parameters\":[{\"data\":\"${txtvalue}\"}]}"
  166. output=$(curl -s -X PUT -d "${payload}" "${BLUEMIX_API_URL}/SoftLayer_Dns_Domain_ResourceRecord/${recordId}")
  167. rc=$?
  168. if [[ "${rc}" == "0" && "${output}" == "true" ]]; then
  169. _info "${fulldomain} updated in Bluemix's DNS."
  170. else
  171. _err "Error adding ${fulldomain} in Bluemix's DNS. Details: ${output}"
  172. fi
  173. }
  174. function deleteRecordId {
  175. recordId=$1
  176. output=$(curl -s -X DELETE "${BLUEMIX_API_URL}/SoftLayer_Dns_Domain_ResourceRecord/${recordId}")
  177. rc=$?
  178. if [[ "${rc}" == "0" && "${output}" == "true" ]]; then
  179. _info "${fulldomain} deleted from Bluemix's DNS."
  180. else
  181. _err "Error deleting ${fulldomain}. Details: ${output}."
  182. fi
  183. }