helpers.bash 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. if [ "$store" = "zookeeper" ]; then
  116. read discovery provider address < <(parse_discovery_str zk://${bridge_ip}:2182)
  117. elif [ "$store" = "etcd" ]; then
  118. read discovery provider address < <(parse_discovery_str etcd://${bridge_ip}:42000)
  119. else
  120. read discovery provider address < <(parse_discovery_str consul://${bridge_ip}:8500)
  121. fi
  122. cat > ${tomlfile} <<EOF
  123. title = "LibNetwork Configuration file for ${name}"
  124. [daemon]
  125. debug = false
  126. [cluster]
  127. discovery = "${discovery}"
  128. Heartbeat = 10
  129. [scopes]
  130. [scopes.global]
  131. [scopes.global.client]
  132. provider = "${provider}"
  133. address = "${address}"
  134. EOF
  135. cat ${tomlfile}
  136. docker run \
  137. -d \
  138. --name=${name} \
  139. --privileged \
  140. -p ${hport}:${cport} \
  141. -v $(pwd)/:/go/src/github.com/docker/libnetwork \
  142. -v /tmp:/tmp \
  143. -v $(pwd)/${TMPC_ROOT}:/scratch \
  144. -v /usr/local/bin/runc:/usr/local/bin/runc \
  145. -w /go/src/github.com/docker/libnetwork \
  146. golang:1.4 ./cmd/dnet/dnet -d -D ${hopt} -c ${tomlfile}
  147. wait_for_dnet $(inst_id2port ${inst}) ${name}
  148. }
  149. function skip_for_circleci() {
  150. if [ -n "$CIRCLECI" ]; then
  151. skip
  152. fi
  153. }
  154. function stop_dnet() {
  155. local name
  156. name=$(dnet_container_name $1 $2)
  157. rm -rf /tmp/dnet/${name} || true
  158. docker stop ${name} || true
  159. # You cannot destroy a container in Circle CI. So do not attempt destroy in circleci
  160. if [ -z "$CIRCLECI" ]; then
  161. docker rm -f ${name} || true
  162. fi
  163. }
  164. function dnet_cmd() {
  165. local hport
  166. hport=$1
  167. shift
  168. ./cmd/dnet/dnet -H tcp://127.0.0.1:${hport} $*
  169. }
  170. function dnet_exec() {
  171. docker exec -it ${1} bash -c "$2"
  172. }
  173. function runc() {
  174. local dnet
  175. dnet=${1}
  176. shift
  177. dnet_exec ${dnet} "cp /var/lib/docker/network/files/${1}*/* /scratch/rootfs/etc"
  178. dnet_exec ${dnet} "mkdir -p /var/run/netns"
  179. dnet_exec ${dnet} "touch /var/run/netns/c && mount -o bind /var/run/docker/netns/${1} /var/run/netns/c"
  180. dnet_exec ${dnet} "ip netns exec c unshare -fmuip --mount-proc chroot \"/scratch/rootfs\" /bin/sh -c \"/bin/mount -t proc proc /proc && ${2}\""
  181. dnet_exec ${dnet} "umount /var/run/netns/c && rm /var/run/netns/c"
  182. }
  183. function start_etcd() {
  184. local bridge_ip
  185. stop_etcd
  186. bridge_ip=$(get_docker_bridge_ip)
  187. docker run -d \
  188. --net=host \
  189. --name=dn_etcd \
  190. mrjana/etcd --listen-client-urls http://0.0.0.0:42000 \
  191. --advertise-client-urls http://${bridge_ip}:42000
  192. sleep 2
  193. }
  194. function stop_etcd() {
  195. docker stop dn_etcd || true
  196. # You cannot destroy a container in Circle CI. So do not attempt destroy in circleci
  197. if [ -z "$CIRCLECI" ]; then
  198. docker rm -f dn_etcd || true
  199. fi
  200. }
  201. function start_zookeeper() {
  202. stop_zookeeper
  203. docker run -d \
  204. --name=zookeeper_server \
  205. -p 2182:2181 \
  206. -h zookeeper \
  207. dnephin/docker-zookeeper:3.4.6
  208. sleep 2
  209. }
  210. function stop_zookeeper() {
  211. echo "zookeeper started"
  212. docker stop zookeeper_server || true
  213. # You cannot destroy a container in Circle CI. So do not attempt destroy in circleci
  214. if [ -z "$CIRCLECI" ]; then
  215. docker rm -f zookeeper_server || true
  216. fi
  217. }
  218. function test_overlay() {
  219. dnet_suffix=$1
  220. shift
  221. echo $(docker ps)
  222. start=1
  223. end=3
  224. # Setup overlay network and connect containers ot it
  225. dnet_cmd $(inst_id2port 1) network create -d overlay multihost
  226. for i in `seq ${start} ${end}`;
  227. do
  228. dnet_cmd $(inst_id2port $i) container create container_${i}
  229. net_connect ${i} container_${i} multihost
  230. done
  231. # Now test connectivity between all the containers using service names
  232. for i in `seq ${start} ${end}`;
  233. do
  234. for j in `seq ${start} ${end}`;
  235. do
  236. if [ "$i" -eq "$j" ]; then
  237. continue
  238. fi
  239. runc $(dnet_container_name $i $dnet_suffix) $(get_sbox_id ${i} container_${i}) \
  240. "ping -c 1 container_$j"
  241. done
  242. done
  243. # Teardown the container connections and the network
  244. for i in `seq ${start} ${end}`;
  245. do
  246. net_disconnect ${i} container_${i} multihost
  247. dnet_cmd $(inst_id2port $i) container rm container_${i}
  248. done
  249. dnet_cmd $(inst_id2port 2) network rm multihost
  250. }
  251. function check_etchosts() {
  252. local dnet sbid retval
  253. dnet=${1}
  254. shift
  255. sbid=${1}
  256. shift
  257. retval="true"
  258. for i in $*;
  259. do
  260. run runc ${dnet} ${sbid} "cat /etc/hosts"
  261. if [ "$status" -ne 0 ]; then
  262. retval="${output}"
  263. break
  264. fi
  265. line=$(echo ${output} | grep ${i})
  266. if [ "${line}" == "" ]; then
  267. retval="false"
  268. fi
  269. done
  270. echo ${retval}
  271. }
  272. function test_overlay_etchosts() {
  273. local clist dnet_suffix
  274. dnet_suffix=$1
  275. shift
  276. echo $(docker ps)
  277. start=1
  278. end=3
  279. # Setup overlay network and connect containers ot it
  280. dnet_cmd $(inst_id2port 1) network create -d overlay multihost
  281. for iter in `seq 1 2`;
  282. do
  283. for i in `seq ${start} ${end}`;
  284. do
  285. dnet_cmd $(inst_id2port $i) container create container_${iter}_${i}
  286. net_connect ${i} container_${iter}_${i} multihost
  287. done
  288. # Now test the /etc/hosts content of all the containers
  289. for i in `seq ${start} ${end}`;
  290. do
  291. clist=""
  292. oldclist=""
  293. for j in `seq ${start} ${end}`;
  294. do
  295. if [ "$i" -eq "$j" ]; then
  296. continue
  297. fi
  298. clist="$clist container_${iter}_$j"
  299. oldclist="$oldclist container_1_$j"
  300. done
  301. rv=$(check_etchosts $(dnet_container_name $i $dnet_suffix) \
  302. $(get_sbox_id ${i} container_${iter}_${i}) \
  303. ${clist})
  304. [ "$rv" = "true" ]
  305. # check to see the containers don't have stale entries from previous iteration
  306. if [ "$iter" -eq 2 ]; then
  307. rv=$(check_etchosts $(dnet_container_name $i $dnet_suffix) \
  308. $(get_sbox_id ${i} container_${iter}_${i}) \
  309. ${oldclist})
  310. [ "$rv" = "false" ]
  311. fi
  312. done
  313. # Teardown the container connections and the network
  314. clist=""
  315. for i in `seq ${start} ${end}`;
  316. do
  317. net_disconnect ${i} container_${iter}_${i} multihost
  318. dnet_cmd $(inst_id2port $i) container rm container_${iter}_${i}
  319. #check if the /etc/hosts of other containers does not contain this container
  320. for j in `seq ${start} ${end}`;
  321. do
  322. if [ "$i" -eq "$j" ]; then
  323. continue
  324. fi
  325. if [[ "${clist}" =~ .*container_${iter}_${j}.* ]]; then
  326. continue
  327. fi
  328. rv=$(check_etchosts $(dnet_container_name $j $dnet_suffix) \
  329. $(get_sbox_id ${j} container_${iter}_${j}) \
  330. container_${iter}_${i})
  331. [ "$rv" = "false" ]
  332. done
  333. clist="${clist} container_${iter}_${i}"
  334. done
  335. done
  336. dnet_cmd $(inst_id2port 2) network rm multihost
  337. }
  338. function test_overlay_singlehost() {
  339. dnet_suffix=$1
  340. shift
  341. echo $(docker ps)
  342. start=1
  343. end=3
  344. # Setup overlay network and connect containers ot it
  345. dnet_cmd $(inst_id2port 1) network create -d overlay multihost
  346. for i in `seq ${start} ${end}`;
  347. do
  348. dnet_cmd $(inst_id2port 1) container create container_${i}
  349. net_connect 1 container_${i} multihost
  350. done
  351. # Now test connectivity between all the containers using service names
  352. for i in `seq ${start} ${end}`;
  353. do
  354. for j in `seq ${start} ${end}`;
  355. do
  356. if [ "$i" -eq "$j" ]; then
  357. continue
  358. fi
  359. runc $(dnet_container_name 1 $dnet_suffix) $(get_sbox_id 1 container_${i}) \
  360. "ping -c 1 container_$j"
  361. done
  362. done
  363. # Teardown the container connections and the network
  364. for i in `seq ${start} ${end}`;
  365. do
  366. net_disconnect 1 container_${i} multihost
  367. dnet_cmd $(inst_id2port 1) container rm container_${i}
  368. done
  369. dnet_cmd $(inst_id2port 1) network rm multihost
  370. }