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.

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