Guest User

Untitled

a guest
Apr 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Copyright 2015 Red Hat, Inc.
  4. #
  5. # NAME
  6. # demo-create-mysql - grading/setup script for DO276
  7. #
  8. # SYNOPSIS
  9. # demo-create-mysql {setup|grade|solve|reset}
  10. #
  11. # setup - no setup is needed for this lab
  12. # grade - checks if the docker image was pulled and it is running
  13. # solve - installs a mysql:5.5 image with the name demo-mysql-basic
  14. # reset - destroys the mysql container and remove the image from the local repository
  15. #
  16. # All functions only work on workstation
  17. #
  18. # DESCRIPTION
  19. # This script is used by the Demo: Creating a MySQL database instance
  20. #
  21. # CHANGELOG
  22. # * Thu Feb 04 2016 Ricardo Jun <jtaniguc@redhat.com>
  23. # - Updated to fix the command issued for the lab
  24. # * Thu Dec 17 2015 Ricardo Jun <jtaniguc@redhat.com>
  25. # - Updated the script name
  26. # - include sudo to the commands
  27. # * Wed Dec 16 2015 Ricardo Jun <jtaniguc@redhat.com>
  28. # - original code
  29.  
  30. PATH=/usr/bin:/bin:/usr/sbin:/sbin
  31.  
  32. # Initialize and set some variables
  33.  
  34. target='workstation'
  35.  
  36. # This defines which subcommands are supported (solve, reset, etc.).
  37. # Corresponding lab_COMMAND functions must be defined.
  38. declare -a valid_commands=(setup grade solve reset)
  39.  
  40. # Additional functions for this grading script
  41.  
  42. function print_usage {
  43. local problem_name=$(basename $0 | sed -e 's/^lab-//')
  44. cat << EOF
  45. This script controls the setup and grading of this lab.
  46. Usage: lab ${problem_name} COMMAND
  47. lab ${problem_name} -h|--help
  48.  
  49. COMMAND is one of: ${valid_commands[@]}
  50.  
  51. EOF
  52. }
  53.  
  54. function lab_setup {
  55. docker_service=$(sudo systemctl status docker.service | grep running | wc -l)
  56. echo -n " Check if docker daemon is running ... "
  57. if [ $docker_service -eq 1 ]
  58. then
  59. print_SUCCESS
  60. else
  61. print_FAIL
  62. fi
  63. }
  64.  
  65. function lab_grade {
  66. docker_images=$(docker images | grep "mysql" | grep "5.5" | wc -l 2>/dev/null)
  67. docker_running_mysql=$(docker ps -a | grep "demo-mysql-basic" | grep "mysql:5.5" | wc -l 2>/dev/null)
  68. docker_verify_credentials=$(docker exec -it demo-mysql-basic mysql -uuser1 -pmypa55 -N -s -e "show databases;" | grep items | wc -l 2>/dev/null)
  69. docker_verify_credentials_root=$(docker exec -it demo-mysql-basic mysql -uroot -pr00tpa55 -N -s -e "show databases;" | grep items | wc -l 2>/dev/null)
  70.  
  71. echo -n " Check Correct Docker Image ... "
  72. if [ $docker_images -eq 1 ]
  73. then
  74. print_PASS
  75. else
  76. print_FAIL
  77. fi
  78. echo -n " Check Correct Name And Image ... "
  79. if [[ $docker_running_mysql -eq 1 ]]
  80. then
  81. print_PASS
  82. else
  83. print_FAIL
  84. fi
  85. echo -n " Check Correct Login/Password to access MySQL ... "
  86. if [[ $docker_verify_credentials -eq 1 ]]
  87. then
  88. print_PASS
  89. else
  90. print_FAIL
  91. fi
  92.  
  93. echo -n " Check Root user/Password to access MySQL ... "
  94. if [[ $docker_verify_credentials_root -eq 1 ]]
  95. then
  96. print_PASS
  97. else
  98. print_FAIL
  99. fi
  100.  
  101. }
  102.  
  103. function lab_solve {
  104.  
  105. lab_setup
  106. lab_reset
  107. echo -n ' Solving the lab for you ... '
  108. docker run --name demo-mysql-basic -e MYSQL_USER=user1 -e MYSQL_PASSWORD=mypa55 -e MYSQL_DATABASE=items -e MYSQL_ROOT_PASSWORD=r00tpa55 -d mysql:5.5 2>/dev/null 1>/dev/null
  109. print_SUCCESS
  110. sleep 15
  111. echo
  112. }
  113.  
  114. function lab_reset {
  115.  
  116. echo -n ' Stopping containers ... '
  117. docker stop demo-mysql-basic 2>/dev/null 1>/dev/null
  118. print_SUCCESS
  119. echo -n ' Removing containers ... '
  120. docker rm demo-mysql-basic 2>/dev/null 1>/dev/null
  121. print_SUCCESS
  122. echo -n ' Removing images ... '
  123. docker rmi mysql:5.5 2>/dev/null 1>/dev/null
  124. print_SUCCESS
  125.  
  126. echo
  127. }
  128.  
  129. ############### Don't EVER change anything below this line ###############
  130.  
  131. # Source library of functions
  132. source /usr/local/lib/labtool.shlib
  133. source /usr/local/lib/labtool.do276.shlib
  134.  
  135. grading_main_program "$@"
Add Comment
Please, Sign In to add comment