Tempest plugin for the Neutron project.
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.

159 lines
5.0KB

  1. # This script include functions that allow guest image files customization
  2. # before uploading them to OpenStack image service
  3. # ensure we don't re-source this in the same environment
  4. [[ -z "$_NEUTRON_TEMPEST_PLUGIN_CUSTOMIZE_IMAGE" ]] || return 0
  5. declare -r -g _NEUTRON_TEMPEST_PLUGIN_CUSTOMIZE_IMAGE=1
  6. source "${NEUTRON_TEMPEST_PLUGIN_DIR}/functions.sh"
  7. # By default enable guest image customization. It will be automatically skipped
  8. # for cirros images
  9. CUSTOMIZE_IMAGE=${CUSTOMIZE_IMAGE:-False}
  10. # Image customization is performed using virt-customize
  11. # using direct backend by default
  12. LIBGUESTFS_BACKEND=${LIBGUESTFS_BACKEND:-direct}
  13. # Disable KVM hardware accelleration by default
  14. LIBGUESTFS_BACKEND_SETTINGS=${LIBGUESTFS_BACKEND_SETTINGS:-force_tcg}
  15. # Install tools required for customizing guest image files
  16. function install_customize_image_tools {
  17. local do_customize=$(trueorfalse True CUSTOMIZE_IMAGE)
  18. if [ ${do_customize} == True ]; then
  19. # Make sure virt-customize is installed
  20. install_package libguestfs-tools
  21. fi
  22. }
  23. # Wraps upload_image function to eventually customize image file before
  24. # uploading it via "openstack image create" command
  25. save_function upload_image overridden_upload_image
  26. function upload_image {
  27. local image_url=$1
  28. # Fork a subshell to have environment restored at the end of this function
  29. (
  30. # Check user configuration
  31. local customize_image=$(trueorfalse True CUSTOMIZE_IMAGE)
  32. if [ ${customize_image} == True ]; then
  33. # Temporarly wraps openstack command with openstack_image_create
  34. # function
  35. function openstack {
  36. IMAGE_URL=${image_url} upload_custom_image "$@"
  37. }
  38. fi
  39. # Execute original upload_image function
  40. overridden_upload_image "$@"
  41. )
  42. }
  43. # Wraps "openstack image create" command to customize image file before
  44. # uploading it to OpenstackImage service.
  45. # Called only when ${CUSTOMIZE_IMAGE} is True
  46. function upload_custom_image {
  47. # Copy command arguments for later use
  48. local args=( "$@" )
  49. # Look for image create sub-command:
  50. # skip any argument before "image" and "create" words
  51. local i=0
  52. local subcommands=()
  53. for subcommand in image create; do
  54. for (( ; i < ${#args[@]}; )) {
  55. local arg=${args[i]}
  56. (( ++i ))
  57. if [ "${arg}" == "${subcommand}" ]; then
  58. subcommands+=( "${arg}" )
  59. break
  60. fi
  61. }
  62. done
  63. if [ "${subcommands[*]}" == "image create" ]; then
  64. # create image subcommand has been detected
  65. # Put here temporary files to be delete before exiting from this
  66. # function
  67. local temp_dir=$(mktemp -d)
  68. chmod 777 "${temp_dir}"
  69. # Parse openstack image create subcommand arguments
  70. local image_url="${IMAGE_URL}"
  71. local image_file=
  72. local disk_format=auto
  73. local container_format=bare
  74. for (( ; i < ${#args[@]}; )) {
  75. local arg=${args[$i]}
  76. (( ++i ))
  77. if [[ "${arg}" == --* ]]; then
  78. # Handle --<option_name>=<option_value> syntax
  79. local option_fields=(${arg//=/ })
  80. local option_name=${option_fields[0]}
  81. local option_value=${option_fields[1]:-}
  82. case "${option_name}" in
  83. --container-format) # Found container format
  84. container_format=${option_value:-${args[ (( i++ )) ]}}
  85. ;;
  86. --disk-format) # Found disk format
  87. disk_format=${option_value:-${args[ (( i++ )) ]}}
  88. ;;
  89. --file) # Found image file name
  90. image_file=${option_value:-${args[ (( i++ )) ]}}
  91. ;;
  92. esac
  93. fi
  94. }
  95. if [ "${image_file}" == "" ]; then
  96. # Copy image file from stdin to a temporary file
  97. image_file=${temp_dir}/$(basename "${image_url}")
  98. cat > "${image_file}"
  99. # Add option to load image from file
  100. args+=( --file "${image_file}" )
  101. fi
  102. # Make image file readable and writable by qemu user
  103. sudo chmod 666 "${image_file}"
  104. # Customize image file
  105. TEMP_DIR=${temp_dir} \
  106. DISK_FORMAT=${disk_format} \
  107. customize_image "${image_file}"
  108. fi
  109. # Upload custom image file
  110. overridden_openstack "${args[@]}" || local error=$?
  111. # Finally delete temporary files
  112. sudo rm -fR "${temp_dir}" || true
  113. return ${error:-0}
  114. }
  115. function overridden_openstack {
  116. "$(which openstack)" "$@"
  117. }
  118. # Execute customization commands on a VM with attached guest image file.
  119. # Called only when ${CUSTOMIZE_IMAGE} is True
  120. function customize_image {
  121. local image_file=$1
  122. local top_dir=$(dirname "${NEUTRON_TEMPEST_PLUGIN_DIR}")
  123. (
  124. export TEMP_DIR DISK_FORMAT RC_DIR
  125. if [[ "$(basename ${image_file})" == ubuntu-* ]]; then
  126. "${top_dir}/tools/customize_ubuntu_image" "${image_file}"
  127. fi
  128. )
  129. }