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.

215 lines
4.2 KiB

8 years ago
  1. #!/bin/bash
  2. set -e -u
  3. set -o pipefail
  4. export TMPDIR_ROOT=$(mktemp -d /tmp/artifactory-tests.XXXXXX)
  5. trap "rm -rf $TMPDIR_ROOT" EXIT
  6. if [ -d /opt/resource ]; then
  7. resource_dir=/opt/resource
  8. else
  9. resource_dir=$(cd $(dirname $0)/../assets && pwd)
  10. fi
  11. run() {
  12. export TMPDIR=$(mktemp -d ${TMPDIR_ROOT}/artifactory-tests.XXXXXX)
  13. echo -e 'running \e[33m'"$@"$'\e[0m...'
  14. eval "$@" 2>&1 | sed -e 's/^/ /g'
  15. echo ""
  16. }
  17. find_primary_ip() {
  18. ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'
  19. }
  20. find_docker_host_ip() {
  21. /sbin/ip route | awk '/default/ { print $3 }'
  22. }
  23. create_version_file() {
  24. local version=$1
  25. local src=$2
  26. mkdir $src/version
  27. echo "$version" > $src/version/number
  28. echo version/number
  29. }
  30. create_file() {
  31. local src=$1
  32. local file=$2
  33. # Mock the artifact
  34. mkdir $src/build-output
  35. echo "Dummy File" > $src/build-output/$file
  36. touch $src/build-output/$file
  37. echo "$src/build-output/$file"
  38. }
  39. # CHECK
  40. check_without_credentials_and_version() {
  41. local endpoint=$1
  42. local regex=$2
  43. local folder=$3
  44. local src=$4
  45. jq -n "{
  46. source: {
  47. endpoint: $(echo $endpoint | jq -R .),
  48. repository: $(echo $folder | jq -R .),
  49. regex: $(echo $regex | jq -R .)
  50. }
  51. }" | $resource_dir/check "$src" | tee /dev/stderr
  52. }
  53. # CHECK
  54. check_without_credentials_with_version() {
  55. local endpoint=$1
  56. local regex=$2
  57. local folder=$3
  58. local version=$4
  59. local src=$5
  60. jq -n "{
  61. source: {
  62. endpoint: $(echo $endpoint | jq -R .),
  63. repository: $(echo $folder | jq -R .),
  64. regex: $(echo $regex | jq -R .)
  65. },
  66. version: { version: $(echo $version| jq -R .)
  67. }
  68. }" | $resource_dir/check "$src" | tee /dev/stderr
  69. }
  70. # CHECK
  71. check_with_credentials_with_version() {
  72. local endpoint=$1
  73. local regex=$2
  74. local username=$3
  75. local password=$4
  76. local folder=$5
  77. local version=$6
  78. local src=$7
  79. jq -n "{
  80. source: {
  81. endpoint: $(echo $endpoint | jq -R .),
  82. repository: $(echo $folder | jq -R .),
  83. regex: $(echo $regex | jq -R .),
  84. username: $(echo $username | jq -R .),
  85. password: $(echo $password | jq -R .)
  86. },
  87. version: { version: $(echo $version| jq -R .)
  88. }
  89. }" | $resource_dir/check "$src" | tee /dev/stderr
  90. }
  91. # IN
  92. in_without_credentials_with_version() {
  93. local endpoint=$1
  94. local regex=$2
  95. local folder=$3
  96. local version=$4
  97. local src=$5
  98. jq -n "{
  99. source: {
  100. endpoint: $(echo $endpoint | jq -R .),
  101. repository: $(echo $folder | jq -R .),
  102. regex: $(echo $regex | jq -R .)
  103. },
  104. version: { version: $(echo $version| jq -R .)
  105. }
  106. }" | $resource_dir/in "$src" | tee /dev/stderr
  107. }
  108. # IN
  109. in_with_credentials_with_version() {
  110. local endpoint=$1
  111. local regex=$2
  112. local folder=$3
  113. local version=$4
  114. local src=$5
  115. local username=$6
  116. local password=$7
  117. jq -n "{
  118. source: {
  119. endpoint: $(echo $endpoint | jq -R .),
  120. username: $(echo $username | jq -R .),
  121. password: $(echo $password | jq -R .),
  122. repository: $(echo $folder | jq -R .),
  123. regex: $(echo $regex | jq -R .)
  124. },
  125. version: { version: $(echo $version| jq -R .)
  126. }
  127. }" | $resource_dir/in "$src" | tee /dev/stderr
  128. }
  129. # OUT
  130. deploy_without_credentials() {
  131. local endpoint=$1
  132. local regex=$2
  133. local repository=$3
  134. local file=$(create_file "$6" "$4")
  135. local version=$5
  136. local src=$6
  137. local version_file=$(create_version_file "$version" "$src")
  138. jq -n "{
  139. params: {
  140. file: $(echo $file | jq -R .),
  141. version_file: $(echo $version_file | jq -R .)
  142. },
  143. source: {
  144. endpoint: $(echo $endpoint | jq -R .),
  145. repository: $(echo $repository | jq -R .),
  146. regex: $(echo $regex | jq -R .)
  147. }
  148. }" | $resource_dir/out "$src" | tee /dev/stderr
  149. }
  150. # OUT
  151. deploy_with_credentials() {
  152. local endpoint=$1
  153. local regex=$2
  154. local repository=$3
  155. local file=$(create_file "$6" "$4")
  156. local version=$5
  157. local src=$6
  158. local username=$7
  159. local password=$8
  160. local version_file=$(create_version_file "$version" "$src")
  161. cat <<EOF | $resource_dir/out "$src" | tee /dev/stderr
  162. {
  163. "params": {
  164. "file": "$file",
  165. "version_file": "$version_file"
  166. },
  167. "source": {
  168. "endpoint": "$endpoint",
  169. "repository": "$repository",
  170. "username": "$username",
  171. "password": "$password"
  172. }
  173. }
  174. EOF
  175. }