setup.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/bin/bash
  2. DOCKER_NAME=$1
  3. UP_DOWN=$2
  4. CH_NAME=$3
  5. function printHelp () {
  6. echo "Usage: ./network_setup cli|orderer0 <up|down> <channel-name>"
  7. }
  8. function validateArgs () {
  9. if [ -z "${DOCKER_NAME}" ]; then
  10. echo "Option cli / orderer0 not mentioned"
  11. printHelp
  12. exit 1
  13. fi
  14. if [ -z "${UP_DOWN}" ]; then
  15. echo "Option up / down / restart not mentioned"
  16. printHelp
  17. exit 1
  18. fi
  19. if [ -z "${CH_NAME}" ]; then
  20. echo "setting to default channel 'mychannel'"
  21. CH_NAME=mychannel
  22. fi
  23. }
  24. function clearContainers () {
  25. CONTAINER_IDS=$(docker ps -aq)
  26. if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" = " " ]; then
  27. echo "---- No containers available for deletion ----"
  28. else
  29. docker rm -f $CONTAINER_IDS
  30. fi
  31. }
  32. function removeUnwantedImages() {
  33. DOCKER_IMAGE_IDS=$(docker images | grep "dev\|none\|test-vp\|peer[0-9]-" | awk '{print $3}')
  34. if [ -z "$DOCKER_IMAGE_IDS" -o "$DOCKER_IMAGE_IDS" = " " ]; then
  35. echo "---- No images available for deletion ----"
  36. else
  37. docker rmi -f $DOCKER_IMAGE_IDS
  38. fi
  39. }
  40. function networkUp () {
  41. if [ "${DOCKER_NAME}" == "cli" ]; then
  42. COMPOSE_FILE=docker-compose-cli.yaml
  43. elif [ "${DOCKER_NAME}" == "orderer0" ]; then
  44. COMPOSE_FILE=docker-compose-orderer0.yaml
  45. else
  46. echo "Option cli /orderer0 not mentioned"
  47. exit 1
  48. fi
  49. CHANNEL_NAME=$CH_NAME docker-compose -f $COMPOSE_FILE up -d 2>&1
  50. if [ $? -ne 0 ]; then
  51. echo "ERROR !!!! Unable to pull the images "
  52. exit 1
  53. fi
  54. if [ "${DOCKER_NAME}" == "cli" ]; then
  55. docker logs -f cli
  56. fi
  57. }
  58. function networkDown () {
  59. if [ "${DOCKER_NAME}" == "cli" ]; then
  60. COMPOSE_FILE=docker-compose-cli.yaml
  61. elif [ "${DOCKER_NAME}" == "orderer0" ]; then
  62. COMPOSE_FILE=docker-compose-orderer0.yaml
  63. else
  64. COMPOSE_FILE=
  65. fi
  66. docker-compose -f $COMPOSE_FILE down
  67. #Cleanup the chaincode containers
  68. clearContainers
  69. #Cleanup images
  70. removeUnwantedImages
  71. }
  72. validateArgs
  73. #Create the network using docker compose
  74. if [ "${UP_DOWN}" == "up" ]; then
  75. networkUp
  76. elif [ "${UP_DOWN}" == "down" ]; then ## Clear the network
  77. networkDown
  78. elif [ "${UP_DOWN}" == "restart" ]; then ## Restart the network
  79. networkDown
  80. networkUp
  81. else
  82. printHelp
  83. exit 1
  84. fi