helpers.bash 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. function get_docker_bridge_ip() {
  2. echo $(docker run --rm -it busybox ip route show | grep default | cut -d" " -f3)
  3. }
  4. function inst_id2port() {
  5. echo $((41000+${1}-1))
  6. }
  7. function dnet_container_name() {
  8. echo dnet-$1-$2
  9. }
  10. function get_sbox_id() {
  11. local line
  12. line=$(dnet_cmd $(inst_id2port ${1}) service ls | grep ${2})
  13. echo ${line} | cut -d" " -f5
  14. }
  15. function net_connect() {
  16. dnet_cmd $(inst_id2port ${1}) service publish ${2}.${3}
  17. dnet_cmd $(inst_id2port ${1}) service attach ${2} ${2}.${3}
  18. }
  19. function net_disconnect() {
  20. dnet_cmd $(inst_id2port ${1}) service detach ${2} ${2}.${3}
  21. dnet_cmd $(inst_id2port ${1}) service unpublish ${2}.${3}
  22. }
  23. function start_consul() {
  24. stop_consul
  25. docker run -d \
  26. --name=pr_consul \
  27. -p 8500:8500 \
  28. -p 8300-8302:8300-8302/tcp \
  29. -p 8300-8302:8300-8302/udp \
  30. -h consul \
  31. progrium/consul -server -bootstrap
  32. sleep 2
  33. }
  34. function stop_consul() {
  35. echo "consul started"
  36. docker stop pr_consul || true
  37. # You cannot destroy a container in Circle CI. So do not attempt destroy in circleci
  38. if [ -z "$CIRCLECI" ]; then
  39. docker rm -f pr_consul || true
  40. fi
  41. }
  42. hrun() {
  43. local e E T oldIFS
  44. [[ ! "$-" =~ e ]] || e=1
  45. [[ ! "$-" =~ E ]] || E=1
  46. [[ ! "$-" =~ T ]] || T=1
  47. set +e
  48. set +E
  49. set +T
  50. output="$("$@" 2>&1)"
  51. status="$?"
  52. oldIFS=$IFS
  53. IFS=$'\n' lines=($output)
  54. [ -z "$e" ] || set -e
  55. [ -z "$E" ] || set -E
  56. [ -z "$T" ] || set -T
  57. IFS=$oldIFS
  58. }
  59. function wait_for_dnet() {
  60. local hport
  61. hport=$1
  62. echo "waiting on dnet to come up ..."
  63. for i in `seq 1 10`;
  64. do
  65. hrun ./cmd/dnet/dnet -H tcp://127.0.0.1:${hport} network ls
  66. echo ${output}
  67. if [ "$status" -eq 0 ]; then
  68. return
  69. fi
  70. if [[ "${lines[1]}" =~ .*EOF.* ]]
  71. then
  72. docker logs ${2}
  73. fi
  74. echo "still waiting after ${i} seconds"
  75. sleep 1
  76. done
  77. }
  78. function parse_discovery_str() {
  79. local d provider address
  80. discovery=$1
  81. provider=$(echo ${discovery} | cut -d":" -f1)
  82. address=$(echo ${discovery} | cut -d":" -f2):$(echo ${discovery} | cut -d":" -f3)
  83. address=${address:2}
  84. echo "${discovery} ${provider} ${address}"
  85. }
  86. function start_dnet() {
  87. local inst suffix name hport cport hopt store bridge_ip labels tomlfile
  88. local discovery provider address
  89. inst=$1
  90. shift
  91. suffix=$1
  92. shift
  93. stop_dnet ${inst} ${suffix}
  94. name=$(dnet_container_name ${inst} ${suffix})
  95. hport=$((41000+${inst}-1))
  96. cport=2385
  97. hopt=""
  98. store=${suffix}
  99. while [ -n "$1" ]
  100. do
  101. if [[ "$1" =~ ^[0-9]+$ ]]
  102. then
  103. hport=$1
  104. cport=$1
  105. hopt="-H tcp://0.0.0.0:${cport}"
  106. else
  107. store=$1
  108. fi
  109. shift
  110. done
  111. bridge_ip=$(get_docker_bridge_ip)
  112. echo "start_dnet parsed values: " ${inst} ${suffix} ${name} ${hport} ${cport} ${hopt} ${store} ${labels}
  113. mkdir -p /tmp/dnet/${name}
  114. tomlfile="/tmp/dnet/${name}/libnetwork.toml"
  115. # Try discovery URLs with or without path
  116. if [ "$store" = "zookeeper" ]; then
  117. read discovery provider address < <(parse_discovery_str zk://${bridge_ip}:2182)
  118. elif [ "$store" = "etcd" ]; then
  119. read discovery provider address < <(parse_discovery_str etcd://${bridge_ip}:42000/custom_prefix)
  120. else
  121. read discovery provider address < <(parse_discovery_str consul://${bridge_ip}:8500/custom_prefix)
  122. fi
  123. cat > ${tomlfile} <<EOF
  124. title = "LibNetwork Configuration file for ${name}"
  125. [daemon]
  126. debug = false
  127. [cluster]
  128. discovery = "${discovery}"
  129. Heartbeat = 10
  130. [scopes]
  131. [scopes.global]
  132. [scopes.global.client]
  133. provider = "${provider}"
  134. address = "${address}"
  135. EOF
  136. cat ${tomlfile}
  137. docker run \
  138. -d \
  139. --name=${name} \
  140. --privileged \
  141. -p ${hport}:${cport} \
  142. -v $(pwd)/:/go/src/github.com/docker/libnetwork \
  143. -v /tmp:/tmp \
  144. -v $(pwd)/${TMPC_ROOT}:/scratch \
  145. -v /usr/local/bin/runc:/usr/local/bin/runc \
  146. -w /go/src/github.com/docker/libnetwork \
  147. mrjana/golang ./cmd/dnet/dnet -d -D ${hopt} -c ${tomlfile}
  148. wait_for_dnet $(inst_id2port ${inst}) ${name}
  149. }
  150. function skip_for_circleci() {
  151. if [ -n "$CIRCLECI" ]; then
  152. skip
  153. fi
  154. }
  155. function stop_dnet() {
  156. local name
  157. name=$(dnet_container_name $1 $2)
  158. rm -rf /tmp/dnet/${name} || true
  159. docker stop ${name} || true
  160. # You cannot destroy a container in Circle CI. So do not attempt destroy in circleci
  161. if [ -z "$CIRCLECI" ]; then
  162. docker rm -f ${name} || true
  163. fi
  164. }
  165. function dnet_cmd() {
  166. local hport
  167. hport=$1
  168. shift
  169. ./cmd/dnet/dnet -H tcp://127.0.0.1:${hport} $*
  170. }
  171. function dnet_exec() {
  172. docker exec -it ${1} bash -c "$2"
  173. }
  174. function runc() {
  175. local dnet
  176. dnet=${1}
  177. shift
  178. dnet_exec ${dnet} "cp /var/lib/docker/network/files/${1}*/* /scratch/rootfs/etc"
  179. dnet_exec ${dnet} "mkdir -p /var/run/netns"
  180. dnet_exec ${dnet} "touch /var/run/netns/c && mount -o bind /var/run/docker/netns/${1} /var/run/netns/c"
  181. dnet_exec ${dnet} "ip netns exec c unshare -fmuip --mount-proc chroot \"/scratch/rootfs\" /bin/sh -c \"/bin/mount -t proc proc /proc && ${2}\""
  182. dnet_exec ${dnet} "umount /var/run/netns/c && rm /var/run/netns/c"
  183. }
  184. function start_etcd() {
  185. local bridge_ip
  186. stop_etcd
  187. bridge_ip=$(get_docker_bridge_ip)
  188. docker run -d \
  189. --net=host \
  190. --name=dn_etcd \
  191. mrjana/etcd --listen-client-urls http://0.0.0.0:42000 \
  192. --advertise-client-urls http://${bridge_ip}:42000
  193. sleep 2
  194. }
  195. function stop_etcd() {
  196. docker stop dn_etcd || true
  197. # You cannot destroy a container in Circle CI. So do not attempt destroy in circleci
  198. if [ -z "$CIRCLECI" ]; then
  199. docker rm -f dn_etcd || true
  200. fi
  201. }
  202. function start_zookeeper() {
  203. stop_zookeeper
  204. docker run -d \
  205. --name=zookeeper_server \
  206. -p 2182:2181 \
  207. -h zookeeper \
  208. dnephin/docker-zookeeper:3.4.6
  209. sleep 2
  210. }
  211. function stop_zookeeper() {
  212. echo "zookeeper started"
  213. docker stop zookeeper_server || true
  214. # You cannot destroy a container in Circle CI. So do not attempt destroy in circleci
  215. if [ -z "$CIRCLECI" ]; then
  216. docker rm -f zookeeper_server || true
  217. fi
  218. }
  219. function test_overlay() {
  220. dnet_suffix=$1
  221. echo $(docker ps)
  222. start=1
  223. end=3
  224. # Setup overlay network and connect containers ot it
  225. if [ -z "${2}" -o "${2}" != "skip_add" ]; then
  226. dnet_cmd $(inst_id2port 1) network create -d overlay multihost
  227. fi
  228. for i in `seq ${start} ${end}`;
  229. do
  230. dnet_cmd $(inst_id2port $i) container create container_${i}
  231. net_connect ${i} container_${i} multihost
  232. done
  233. # Now test connectivity between all the containers using service names
  234. for i in `seq ${start} ${end}`;
  235. do
  236. runc $(dnet_container_name $i $dnet_suffix) $(get_sbox_id ${i} container_${i}) \
  237. "ping -c 1 www.google.com"
  238. for j in `seq ${start} ${end}`;
  239. do
  240. if [ "$i" -eq "$j" ]; then
  241. continue
  242. fi
  243. runc $(dnet_container_name $i $dnet_suffix) $(get_sbox_id ${i} container_${i}) \
  244. "ping -c 1 container_$j"
  245. done
  246. done
  247. # Teardown the container connections and the network
  248. for i in `seq ${start} ${end}`;
  249. do
  250. net_disconnect ${i} container_${i} multihost
  251. dnet_cmd $(inst_id2port $i) container rm container_${i}
  252. done
  253. if [ -z "${2}" -o "${2}" != "skip_rm" ]; then
  254. dnet_cmd $(inst_id2port 2) network rm multihost
  255. fi
  256. }
  257. function check_etchosts() {
  258. local dnet sbid retval
  259. dnet=${1}
  260. shift
  261. sbid=${1}
  262. shift
  263. retval="true"
  264. for i in $*;
  265. do
  266. run runc ${dnet} ${sbid} "cat /etc/hosts"
  267. if [ "$status" -ne 0 ]; then
  268. retval="${output}"
  269. break
  270. fi
  271. line=$(echo ${output} | grep ${i})
  272. if [ "${line}" == "" ]; then
  273. retval="false"
  274. fi
  275. done
  276. echo ${retval}
  277. }
  278. function test_overlay_etchosts() {
  279. local clist dnet_suffix
  280. dnet_suffix=$1
  281. shift
  282. echo $(docker ps)
  283. start=1
  284. end=3
  285. # Setup overlay network and connect containers ot it
  286. dnet_cmd $(inst_id2port 1) network create -d overlay multihost
  287. for iter in `seq 1 2`;
  288. do
  289. for i in `seq ${start} ${end}`;
  290. do
  291. dnet_cmd $(inst_id2port $i) container create container_${iter}_${i}
  292. net_connect ${i} container_${iter}_${i} multihost
  293. done
  294. # Now test the /etc/hosts content of all the containers
  295. for i in `seq ${start} ${end}`;
  296. do
  297. clist=""
  298. oldclist=""
  299. for j in `seq ${start} ${end}`;
  300. do
  301. if [ "$i" -eq "$j" ]; then
  302. continue
  303. fi
  304. clist="$clist container_${iter}_$j"
  305. oldclist="$oldclist container_1_$j"
  306. done
  307. rv=$(check_etchosts $(dnet_container_name $i $dnet_suffix) \
  308. $(get_sbox_id ${i} container_${iter}_${i}) \
  309. ${clist})
  310. [ "$rv" = "true" ]
  311. # check to see the containers don't have stale entries from previous iteration
  312. if [ "$iter" -eq 2 ]; then
  313. rv=$(check_etchosts $(dnet_container_name $i $dnet_suffix) \
  314. $(get_sbox_id ${i} container_${iter}_${i}) \
  315. ${oldclist})
  316. [ "$rv" = "false" ]
  317. fi
  318. done
  319. # Teardown the container connections and the network
  320. clist=""
  321. for i in `seq ${start} ${end}`;
  322. do
  323. net_disconnect ${i} container_${iter}_${i} multihost
  324. dnet_cmd $(inst_id2port $i) container rm container_${iter}_${i}
  325. #check if the /etc/hosts of other containers does not contain this container
  326. for j in `seq ${start} ${end}`;
  327. do
  328. if [ "$i" -eq "$j" ]; then
  329. continue
  330. fi
  331. if [[ "${clist}" =~ .*container_${iter}_${j}.* ]]; then
  332. continue
  333. fi
  334. rv=$(check_etchosts $(dnet_container_name $j $dnet_suffix) \
  335. $(get_sbox_id ${j} container_${iter}_${j}) \
  336. container_${iter}_${i})
  337. [ "$rv" = "false" ]
  338. done
  339. clist="${clist} container_${iter}_${i}"
  340. done
  341. done
  342. dnet_cmd $(inst_id2port 2) network rm multihost
  343. }
  344. function test_overlay_singlehost() {
  345. dnet_suffix=$1
  346. shift
  347. echo $(docker ps)
  348. start=1
  349. end=3
  350. # Setup overlay network and connect containers ot it
  351. dnet_cmd $(inst_id2port 1) network create -d overlay multihost
  352. for i in `seq ${start} ${end}`;
  353. do
  354. dnet_cmd $(inst_id2port 1) container create container_${i}
  355. net_connect 1 container_${i} multihost
  356. done
  357. # Now test connectivity between all the containers using service names
  358. for i in `seq ${start} ${end}`;
  359. do
  360. for j in `seq ${start} ${end}`;
  361. do
  362. if [ "$i" -eq "$j" ]; then
  363. continue
  364. fi
  365. runc $(dnet_container_name 1 $dnet_suffix) $(get_sbox_id 1 container_${i}) \
  366. "ping -c 1 container_$j"
  367. done
  368. done
  369. # Teardown the container connections and the network
  370. for i in `seq ${start} ${end}`;
  371. do
  372. net_disconnect 1 container_${i} multihost
  373. dnet_cmd $(inst_id2port 1) container rm container_${i}
  374. done
  375. dnet_cmd $(inst_id2port 1) network rm multihost
  376. }