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.

212 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. # BLUEMIX_USER is required
  20. if [ -z "${BLUEMIX_USER}" ]; then
  21. _err "Environment variable BLUEMIX_USER not defined"
  22. return 1
  23. fi
  24. # BLUEMIX_KEY is required
  25. if [ -z "${BLUEMIX_KEY}" ]; then
  26. _err "Environment variable BLUEMIX_KEY not defined"
  27. return 1
  28. fi
  29. # Check BLUEMIX_USER and BLUEMIX_KEY access
  30. if ! hasAccess; then
  31. _err "Error accessing BlueMix API. Check \$BLUEMIX_USER and \$BLUEMIX_KEY and ensure there is access to https://api.softlayer.com/"
  32. return 1
  33. fi
  34. # Get right domain and domain id
  35. if ! getDomain ${fulldomain}; then
  36. _err "Domain for ${fulldomain} was not found in this Bluemix account"
  37. return 1
  38. fi
  39. # Check if this DNS entry already exists
  40. if getRecordId "${domainId}" "${host}"; then
  41. # Update Record if it already exists
  42. updateTxtRecord "${recordId}" "${txtvalue}"
  43. else
  44. # Create record if it doesn't exist
  45. createTxtRecord "${domainId}" "${host}" "${txtvalue}"
  46. fi
  47. return 0
  48. }
  49. #Usage: fulldomain txtvalue
  50. #Remove the txt record after validation.
  51. dns_bluemix_rm() {
  52. fulldomain=$1
  53. _info "Attempting to delete ${fulldomain} from Bluemix"
  54. # BLUEMIX_USER is required
  55. if [ -z "${BLUEMIX_USER}" ]; then
  56. _err "Environment variable BLUEMIX_USER not defined"
  57. return 1
  58. fi
  59. # BLUEMIX_KEY is required
  60. if [ -z "${BLUEMIX_KEY}" ]; then
  61. _err "Environment variable BLUEMIX_KEY not defined"
  62. return 1
  63. fi
  64. # Check BLUEMIX_USER and BLUEMIX_KEY access
  65. if ! hasAccess; then
  66. _err "Error accessing BlueMix API. Check \$BLUEMIX_USER and \$BLUEMIX_KEY and ensure there is access to https://api.softlayer.com/"
  67. return 1
  68. fi
  69. # Get Domain ID
  70. if ! getDomain ${fulldomain}; then
  71. _err "Domain for ${fulldomain} was not found in this Bluemix account"
  72. return 1
  73. fi
  74. # Get DNS entry in this Domain
  75. if getRecordId "${domainId}" "${host}"; then
  76. # Remove record
  77. deleteRecordId "${recordId}"
  78. fi
  79. return 0
  80. }
  81. #################### Private functions below ##################################
  82. function hasAccess {
  83. response=$(_get "${BLUEMIX_API_URL}/SoftLayer_Account/getDomains")
  84. if [[ -z "${response}" || "${response}" =~ 'Access Denied' ]]; then
  85. _debug "Code=${code}, Response=${response}"
  86. return 1
  87. else
  88. return 0
  89. fi
  90. }
  91. function getDomain {
  92. fulldomain=$1
  93. response=$(_get "${BLUEMIX_API_URL}/SoftLayer_Account/getDomains")
  94. _debug "Code=${code}, Response=${response}"
  95. for domain_item in $(echo "${response}" | tr , \\n | grep "^\"name\":" | cut -f4 -d'"'); do
  96. if [[ "${fulldomain}" =~ ${domain_item}$ ]]; then
  97. domain="${domain_item}"
  98. break
  99. fi
  100. done
  101. if [ -z "${domain}" ]; then
  102. return 1
  103. fi
  104. domainId=$(echo "${response}" | tr \} \\n | grep "\"name\":\"${domain}\"" | sed -n 's/.*\"id\":\([0-9]*\).*/\1/p')
  105. host=$(echo "${fulldomain}" | sed "s/\.${domain}\$//g")
  106. _debug "Host is ${host}, domain is ${domain} and domain id is ${domainId}"
  107. return 0
  108. }
  109. function getRecordId {
  110. domainId=$1
  111. host=$2
  112. response=$(_get "${BLUEMIX_API_URL}/SoftLayer_Dns_Domain/${domainId}/getResourceRecords")
  113. _debug "Code=${code}, Response=${response}"
  114. recordId=$(echo "${response}" | tr \} \\n | grep "\"host\":\"${host}\"" | sed -n 's/.*\"id\":\([0-9]*\).*/\1/p')
  115. if [ -z "${recordId}" ]; then
  116. return 1
  117. else
  118. _debug "RecordId is ${recordId}"
  119. return 0
  120. fi
  121. }
  122. function createTxtRecord {
  123. domainId=$1
  124. host=$2
  125. txtvalue=$3
  126. payload="{\"parameters\":[{\"host\":\"${host}\",\"data\":\"${txtvalue}\",\"ttl\":\"60\",\"type\":\"txt\",\"domainId\":\"${domainId}\"}]}"
  127. response=$(_post "${payload}" "${BLUEMIX_API_URL}/SoftLayer_Dns_Domain_ResourceRecord" "")
  128. _debug "Code=${code}, Response=${response}"
  129. if [[ "${response}" =~ \"host\":\"${host}\" ]]; then
  130. _info "${fulldomain} added into Bluemix's DNS."
  131. return 0
  132. else
  133. _err "Error adding ${fulldomain} in Bluemix's DNS. Details: ${response}"
  134. return 1
  135. fi
  136. }
  137. function updateTxtRecord {
  138. recordId=$1
  139. txtvalue=$2
  140. payload="{\"parameters\":[{\"data\":\"${txtvalue}\"}]}"
  141. response=$(_post "${payload}" "${BLUEMIX_API_URL}/SoftLayer_Dns_Domain_ResourceRecord/${recordId}" "" "PUT")
  142. _debug "Code=${code}, Response=${response}"
  143. if [ "${response}" == "true" ]; then
  144. _info "${fulldomain} updated in Bluemix's DNS."
  145. return 0
  146. else
  147. _err "Error adding ${fulldomain} in Bluemix's DNS. Details: ${response}"
  148. return 1
  149. fi
  150. }
  151. function deleteRecordId {
  152. recordId=$1
  153. response=$(_post "" "${BLUEMIX_API_URL}/SoftLayer_Dns_Domain_ResourceRecord/${recordId}" "" "DELETE")
  154. _debug "Code=${code}, Response=${response}"
  155. if [ "${response}" == "true" ]; then
  156. _info "${fulldomain} deleted from Bluemix's DNS."
  157. return 0
  158. else
  159. _err "Error deleting ${fulldomain}. Details: ${response}."
  160. return 1
  161. fi
  162. }