File: customize_image.sh

package info (click to toggle)
neutron-tempest-plugin 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,964 kB
  • sloc: python: 22,740; sh: 289; makefile: 21
file content (158 lines) | stat: -rw-r--r-- 5,164 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# This script include functions that allow guest image files customization
# before uploading them to OpenStack image service

# ensure we don't re-source this in the same environment
[[ -z "$_NEUTRON_TEMPEST_PLUGIN_CUSTOMIZE_IMAGE" ]] || return 0
declare -r -g _NEUTRON_TEMPEST_PLUGIN_CUSTOMIZE_IMAGE=1

source "${NEUTRON_TEMPEST_PLUGIN_DIR}/functions.sh"

# By default enable guest image customization. It will be automatically skipped
# for cirros images
CUSTOMIZE_IMAGE=${CUSTOMIZE_IMAGE:-False}

# Image customization is performed using virt-customize
# using direct backend by default
LIBGUESTFS_BACKEND=${LIBGUESTFS_BACKEND:-direct}

# Disable KVM hardware accelleration by default
LIBGUESTFS_BACKEND_SETTINGS=${LIBGUESTFS_BACKEND_SETTINGS:-force_tcg}

# Install tools required for customizing guest image files
function install_customize_image_tools {
    local do_customize=$(trueorfalse True CUSTOMIZE_IMAGE)
    if [ ${do_customize} == True ]; then
        # Make sure virt-customize is installed
        install_package libguestfs-tools
    fi
}

# Wraps upload_image function to eventually customize image file before
# uploading it via "openstack image create" command
save_function upload_image overridden_upload_image
function upload_image {
    local image_url=$1

    # Fork a subshell to have environment restored at the end of this function
    (
        # Check user configuration
        local customize_image=$(trueorfalse True CUSTOMIZE_IMAGE)
        if [ ${customize_image} == True ]; then
            # Temporarly wraps openstack command with openstack_image_create
            # function
            function openstack {
                IMAGE_URL=${image_url} upload_custom_image "$@"
            }
        fi

        # Execute original upload_image function
        overridden_upload_image "$@"
    )
}

# Wraps "openstack image create" command to customize image file before
# uploading it to OpenstackImage service.
# Called only when ${CUSTOMIZE_IMAGE} is True
function upload_custom_image {
    # Copy command arguments for later use
    local args=( "$@" )

    # Look for image create sub-command:
    # skip any argument before "image" and "create" words
    local i=0
    local subcommands=()
    for subcommand in image create; do
        for (( ; i < ${#args[@]}; )) {
            local arg=${args[i]}
            (( ++i ))
            if [ "${arg}" == "${subcommand}" ]; then
                subcommands+=( "${arg}" )
                break
            fi
        }
    done

    if [ "${subcommands[*]}" == "image create" ]; then
        # create image subcommand has been detected

        # Put here temporary files to be delete before exiting from this
        # function
        local temp_dir=$(mktemp -d)
        chmod 777 "${temp_dir}"

        # Parse openstack image create subcommand arguments
        local image_url="${IMAGE_URL}"
        local image_file=
        local disk_format=auto
        local container_format=bare

        for (( ; i < ${#args[@]}; )) {
            local arg=${args[$i]}
            (( ++i ))

            if [[ "${arg}" == --* ]]; then
                # Handle --<option_name>=<option_value> syntax
                local option_fields=(${arg//=/ })
                local option_name=${option_fields[0]}
                local option_value=${option_fields[1]:-}

                case "${option_name}" in

                    --container-format)  # Found container format
                        container_format=${option_value:-${args[ (( i++ )) ]}}
                        ;;

                    --disk-format)  # Found disk format
                        disk_format=${option_value:-${args[ (( i++ )) ]}}
                        ;;

                   --file)  # Found image file name
                        image_file=${option_value:-${args[ (( i++ )) ]}}
                        ;;
                esac
            fi
        }

        if [ "${image_file}" == "" ]; then
            # Copy image file from stdin to a temporary file
            image_file=${temp_dir}/$(basename "${image_url}")
            cat > "${image_file}"

            # Add option to load image from file
            args+=( --file "${image_file}" )
        fi

        # Make image file readable and writable by qemu user
        sudo chmod 666 "${image_file}"

        # Customize image file
        TEMP_DIR=${temp_dir} \
            DISK_FORMAT=${disk_format} \
            customize_image "${image_file}"
    fi

    # Upload custom image file
    overridden_openstack "${args[@]}" || local error=$?

    # Finally delete temporary files
    sudo rm -fR "${temp_dir}" || true

    return ${error:-0}
}

function overridden_openstack {
    "$(which openstack)" "$@"
}

# Execute customization commands on a VM with attached guest image file.
# Called only when ${CUSTOMIZE_IMAGE} is True
function customize_image {
    local image_file=$1
    local top_dir=$(dirname "${NEUTRON_TEMPEST_PLUGIN_DIR}")
    (
        export TEMP_DIR DISK_FORMAT RC_DIR
        if [[ "$(basename ${image_file})" == ubuntu-* ]]; then
            "${top_dir}/tools/customize_ubuntu_image" "${image_file}"
        fi
    )
}