summaryrefslogtreecommitdiff
path: root/diskimage-create/diskimage-create.sh
blob: 01be5cb5de815615814424d549bbedf8530d4697 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
#!/bin/bash

set -e

export IMAGE_SIZE=$DIB_IMAGE_SIZE
# This will unset parameter DIB_IMAGE_SIZE for Ubuntu and Fedora vanilla images
unset DIB_IMAGE_SIZE

# DEBUG_MODE is set by the -d flag, debug is enabled if the value is "true"
DEBUG_MODE="false"

# The default version for a MapR plugin
DIB_DEFAULT_MAPR_VERSION="5.2.0"

# The default version for Spark plugin
DIB_DEFAULT_SPARK_VERSION="2.1.0"

# The default version for Storm plugin
DIB_DEFAULT_STORM_VERSION="1.1.0"

# Bare metal image generation is enabled with the -b flag, it is off by default
SIE_BAREMETAL="false"

# Default list of datasource modules for ubuntu. Workaround for bug #1375645
export CLOUD_INIT_DATASOURCES=${DIB_CLOUD_INIT_DATASOURCES:-"NoCloud, ConfigDrive, OVF, MAAS, Ec2"}

# Tracing control
TRACING=

usage() {
    echo
    echo "Usage: $(basename $0)"
    echo "         [-p vanilla|spark|cloudera|storm|mapr|ambari|plain]"
    echo "         [-i ubuntu|fedora|centos|centos7]"
    echo "         [-v 2.7.1|5.5|5.7|5.9|5.11|2.2.0.0|2.2.1.0|2.4.2.0]"
    echo "         [-r 5.1.0|5.2.0]"
    echo "         [-s 1.3.1|1.6.0|2.1.0]"
    echo "         [-t 0.9.2|1.0.1|1.1.0]"
    echo "         [-f qcow2|raw]"
    echo "         [-d]"
    echo "         [-u]"
    echo "         [-j openjdk|oracle-java]"
    echo "         [-x]"
    echo "         [-h]"
    echo "   '-p' is plugin version (default: all plugins)"
    echo "   '-i' is operating system of the base image (default: all non-deprecated"
    echo "        by plugin). 'centos' images (CentOS 6) are deprecated."
    echo "   '-v' is hadoop version (default: all supported by plugin)"
    echo "   '-r' is MapR Version (default: ${DIB_DEFAULT_MAPR_VERSION})"
    echo "   '-s' is Spark version (default: ${DIB_DEFAULT_SPARK_VERSION})"
    echo "   '-f' is the image format (default: qcow2)"
    echo "   '-d' enable debug mode, root account will have password 'hadoop'"
    echo "   '-u' install missing packages necessary for building"
    echo "   '-j' is java distribution (default: openjdk)"
    echo "   '-x' turns on tracing"
    echo "   '-b' generate a bare metal image"
    echo "   '-h' display this message"
    echo
    echo "You shouldn't specify image type for spark plugin"
    echo "You shouldn't specify hadoop version for plain images"
    echo "Debug mode should only be enabled for local debugging purposes, not for production systems"
    echo "By default all images for all plugins will be created"
    echo
}

while getopts "p:i:v:f:dur:s:t:j:xhb" opt; do
    case $opt in
        p)
            PLUGIN=$OPTARG
        ;;
        i)
            BASE_IMAGE_OS=$OPTARG
        ;;
        v)
            HADOOP_VERSION=$OPTARG
        ;;
        d)
            DEBUG_MODE="true"
        ;;
        r)
            DIB_MAPR_VERSION=$OPTARG
        ;;
        s)
            DIB_SPARK_VERSION=$OPTARG
        ;;
        t)
            DIB_STORM_VERSION=$OPTARG
        ;;
        f)
            IMAGE_FORMAT="-t $OPTARG"
        ;;
        u)
            DIB_UPDATE_REQUESTED=true
        ;;
        j)
            JAVA_ELEMENT=$OPTARG
        ;;
        x)
            TRACING="$TRACING -x"
            set -x
        ;;
        b)
            SIE_BAREMETAL="true"
        ;;
        h)
            usage
            exit 0
        ;;
        *)
            usage
            exit 1
        ;;
    esac
done

shift $((OPTIND-1))
if [ "$1" ]; then
    usage
    exit 1
fi

JAVA_ELEMENT=${JAVA_ELEMENT:-"openjdk"}

if [ -e /etc/os-release ]; then
    platform=$(cat /etc/os-release | awk -F= '/^ID=/ {print tolower($2);}')
    # remove eventual quotes around ID=...
    platform=$(echo $platform | sed -e 's,^",,;s,"$,,')
elif [ -e /etc/system-release ]; then
    case "$(head -1 /etc/system-release)" in
        "Red Hat Enterprise Linux Server"*)
            platform=rhel
            ;;
        "CentOS"*)
            platform=centos
            ;;
        *)
            echo -e "Unknown value in /etc/system-release. Impossible to build images.\nAborting"
            exit 2
            ;;
    esac
else
    echo -e "Unknown host OS. Impossible to build images.\nAborting"
    exit 2
fi

# Checks of input
if [ "$DEBUG_MODE" = "true" -a "$platform" != 'ubuntu' ]; then
    if [ "$(getenforce)" != "Disabled" ]; then
        echo "Debug mode cannot be used from this platform while SELinux is enabled, see https://bugs.launchpad.net/sahara/+bug/1292614"
        exit 1
    fi
fi

# Deprecation
if [ "$BASE_IMAGE_OS" = "centos" ]; then
    echo "*************************************************"
    echo "* __        ___    ____  _   _ ___ _   _  ____  *"
    echo "* \ \      / / \  |  _ \| \ | |_ _| \ | |/ ___| *"
    echo "*  \ \ /\ / / _ \ | |_) |  \| || ||  \| | |  _  *"
    echo "*   \ V  V / ___ \|  _ <| |\  || || |\  | |_| | *"
    echo "*    \_/\_/_/   \_\_| \_\_| \_|___|_| \_|\____| *"
    echo "*                                               *"
    echo "*        CentOS 6 images are deprecated !       *"
    echo "*          Please switch to 'centos7'           *"
    echo "*************************************************"
fi

check_spark_version () {
    case "$DIB_SPARK_VERSION" in
        "1.3.1" | "1.6.0" | "2.1.0");;
        "")
            echo "Spark version not specified"
            echo "Spark ${DIB_DEFAULT_SPARK_VERSION} will be used"
            DIB_SPARK_VERSION=${DIB_DEFAULT_SPARK_VERSION}
        ;;
        *)
            echo -e "Unknown Spark version selected.\nAborting"
            exit 1
        ;;
    esac
}

case "$PLUGIN" in
    "");;
    "vanilla")
        case "$HADOOP_VERSION" in
            "" | "2.7.1");;
            *)
                echo -e "Unknown hadoop version selected.\nAborting"
                exit 1
            ;;
        esac
        case "$BASE_IMAGE_OS" in
            "" | "ubuntu" | "fedora" | "centos" | "centos7");;
            *)
                echo -e "'$BASE_IMAGE_OS' image type is not supported by '$PLUGIN'.\nAborting"
                exit 1
            ;;
        esac
        check_spark_version
        ;;
    "cloudera")
        case "$BASE_IMAGE_OS" in
            "" | "ubuntu" | "centos" | "centos7");;
            *)
                echo -e "'$BASE_IMAGE_OS' image type is not supported by '$PLUGIN'.\nAborting"
                exit 1
            ;;
        esac

        case "$HADOOP_VERSION" in
            "" | "5.5" | "5.7" | "5.9" | "5.11");;
            *)
                echo -e "Unknown hadoop version selected.\nAborting"
                exit 1
            ;;
        esac

        if [ "$BASE_IMAGE_OS" = "centos7"  ]; then
            if [ ! -z "$HADOOP_VERSION" -a ! "$HADOOP_VERSION" = "5.5"  -a ! "$HADOOP_VERSION" = "5.7" -a ! "$HADOOP_VERSION" = "5.9" -a ! "$HADOOP_VERSION" = "5.11" ]; then
                echo -e "Unsupported version combination, Centos 7 can only be used with CDH 5.5 or higher version"
                exit 1
            fi
        fi

        if [ -n "$DIB_CDH_MINOR_VERSION" ]; then
            echo -e "Continuing image building with custom CDH version: \"$DIB_CDH_MINOR_VERSION\".\n"
        fi
        ;;
    "spark")
        case "$BASE_IMAGE_OS" in
            "" | "ubuntu");;
            *)
                echo -e "'$BASE_IMAGE_OS' image type is not supported by '$PLUGIN'.\nAborting"
                exit 1
            ;;
        esac
        check_spark_version
        ;;
    "storm")
        case "$BASE_IMAGE_OS" in
            "" | "ubuntu");;
            *)
                echo -e "'$BASE_IMAGE_OS' image type is not supported by '$PLUGIN'.\nAborting"
                exit 1
            ;;
        esac

        case "$DIB_STORM_VERSION" in
            "0.9.2" | "1.0.1" | "1.1.0");;
            "")
                echo "Storm version not specified"
                echo "Storm ${DIB_DEFAULT_STORM_VERSION} will be used"
                DIB_STORM_VERSION=${DIB_DEFAULT_STORM_VERSION}
            ;;
            *)
                echo -e "Unknown Storm version selected.\nAborting"
                exit 1
            ;;
        esac

        if [ -n "$HADOOP_VERSION" ]; then
            echo -e "You shouldn't specify hadoop version for '$PLUGIN'.\nAborting"
            exit 1
        fi
        ;;
    "ambari")
        case "$BASE_IMAGE_OS" in
            "" | "centos" | "centos7" | "ubuntu" )
            ;;
            * )
                echo "\"$BASE_IMAGE_OS\" image type is not supported by \"$PLUGIN\".\nAborting"
                exit 1
            ;;
        esac

        case "$HADOOP_VERSION" in
            "" | "2.2.0.0" | "2.2.1.0" | "2.4.2.0");;
            *)
                echo -e "Continuing image building with custom ambari version \"$HADOOP_VERSION\"\n"
            ;;
        esac
        ;;
    "mapr")
        case "$BASE_IMAGE_OS" in
            "" | "ubuntu" | "centos" | "centos7");;
            *)
                echo -e "'$BASE_IMAGE_OS' image type is not supported by '$PLUGIN'.\nAborting"
                exit 1
            ;;
        esac

        if [ -n "$HADOOP_VERSION" ]; then
            echo -e "You shouldn't specify hadoop version for 'mapr'.\nAborting"
            exit 1
        fi

        case "$DIB_MAPR_VERSION" in
            "")
                echo "MapR version is not specified"
                echo "${DIB_DEFAULT_MAPR_VERSION} version would be used"
                DIB_MAPR_VERSION=${DIB_DEFAULT_MAPR_VERSION}
            ;;
            "5.1.0" | "5.2.0");;
            *)
                echo -e "Unknown MapR version.\nExit"
                exit 1
            ;;
        esac
        ;;
    "plain")
        case "$BASE_IMAGE_OS" in
            "" | "ubuntu" | "fedora" | "centos" | "centos7");;
            *)
                echo -e "'$BASE_IMAGE_OS' image type is not supported by '$PLUGIN'.\nAborting"
                exit 1
            ;;
        esac

        if [ -n "$HADOOP_VERSION" ]; then
            echo -e "You shouldn't specify hadoop version for '$PLUGIN'.\nAborting"
            exit 1
        fi
        ;;
    *)
        echo -e "Unknown plugin selected.\nAborting"
        exit 1
esac

if [ "$PLUGIN" != "mapr" -a -n "$DIB_MAPR_VERSION" ]; then
    echo -e "'-r' parameter should be used only with 'mapr' plugin.\nAborting"
    exit 1
fi


if [ "$JAVA_ELEMENT" != "openjdk" -a "$JAVA_ELEMENT" != "oracle-java" ]; then
    echo "Unknown java distro"
    exit 1
fi

#################

is_installed() {
    if [ "$platform" = 'ubuntu' ]; then
        dpkg -s "$1" &> /dev/null
    else
        # centos, fedora, opensuse, or rhel
        if ! rpm -q "$1" &> /dev/null; then
            rpm -q "$(rpm -q --whatprovides "$1")"
        fi
    fi
}

need_required_packages() {
    case "$platform" in
        "ubuntu")
            package_list="qemu kpartx git"
            ;;
        "fedora")
            package_list="qemu-img kpartx git"
            ;;
        "opensuse")
            package_list="qemu kpartx git-core"
            ;;
        "rhel" | "centos")
            package_list="qemu-kvm qemu-img kpartx git"
            if [ ${platform} = "centos" ]; then
                # CentOS requires the python-argparse package be installed separately
                package_list="$package_list python-argparse"
            fi
            ;;
        *)
            echo -e "Unknown platform '$platform' for the package list.\nAborting"
            exit 2
            ;;
    esac

    for p in `echo $package_list`; do
        if ! is_installed $p; then
            return 0
        fi
    done
    return 1
}

if need_required_packages; then
    # install required packages if requested
    if [ -n "$DIB_UPDATE_REQUESTED" ]; then
        case "$platform" in
            "ubuntu")
                sudo apt-get update
                sudo apt-get install $package_list -y
                ;;
            "opensuse")
                sudo zypper --non-interactive --gpg-auto-import-keys in $package_list
                ;;
            "fedora" | "rhel" | "centos")
                if [ ${platform} = "centos" ]; then
                    # install EPEL repo, in order to install argparse
                    sudo rpm -Uvh --force http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
                fi
                sudo yum install $package_list -y
                ;;
            *)
                echo -e "Unknown platform '$platform' for installing packages.\nAborting"
                exit 2
                ;;
        esac
    else
        echo "Missing one of the following packages: $package_list"
        echo "Please install manually or rerun with the update option (-u)."
        exit 1
    fi
fi

if [ "$DEBUG_MODE" = "true" ]; then
    echo "Using Image Debug Mode, using root-pwd in images, NOT FOR PRODUCTION USAGE."
    # Each image has a root login, password is "hadoop"
    export DIB_PASSWORD="hadoop"
fi

#################

# Common helper for invoking disk-image-create, adding all the common
# elements and arguments, and setting common environment variables.
#
# Usage:
#   image_create DISTRO OUTPUT [args...]
# - DISTRO is the main element of the distribution
# - OUTPUT is the output name for the image
# - any other argument is passed directly to disk-image-create
image_create() {
    local distro=$1
    shift
    local output=$1
    shift

    # the base elements and args, used in *all* the images
    local elements="sahara-version ntp xfs-tools"
    if [ $SIE_BAREMETAL = "true" ]; then
        elements="grub2 baremetal dhcp-all-interfaces $elements"
    else
        elements="vm $elements"
    fi
    local args=""

    # debug mode handling
    if [ "$DEBUG_MODE" = "true" ]; then
        elements="$elements root-passwd"
    fi
    # mirror handling
    if [ -n "$USE_MIRRORS" ]; then
        case "$distro" in
            ubuntu) elements="$elements apt-mirror" ;;
            fedora) elements="$elements fedora-mirror" ;;
            centos | centos7) elements="$elements centos-mirror" ;;
        esac
    fi
    # use a custom cloud image for CentOS 6
    case "$distro" in
        centos)
            export BASE_IMAGE_FILE=${BASE_IMAGE_FILE:-"CentOS-6.6-cloud-init-20150821.qcow2"}
            export DIB_CLOUD_IMAGES=${DIB_CLOUD_IMAGES:-"http://sahara-files.mirantis.com"}
        ;;
    esac

    disk-image-create $IMAGE_FORMAT $TRACING -o "$output" $args "$distro" $elements "$@"

    # cleanup
    case "$distro" in
        centos)
            unset BASE_IMAGE_FILE DIB_CLOUD_IMAGES
        ;;
    esac
}

#############################
# Images for Vanilla plugin #
#############################

if [ -z "$PLUGIN" -o "$PLUGIN" = "vanilla" ]; then
    export HIVE_VERSION=${HIVE_VERSION:-"0.11.0"}
    export HADOOP_V2_7_1_NATIVE_LIBS_DOWNLOAD_URL=${HADOOP_V2_7_1_NATIVE_LIBS_DOWNLOAD_URL:-"https://tarballs.openstack.org/sahara/dist/common-artifacts/hadoop-native-libs-2.7.1.tar.gz"}
    export OOZIE_HADOOP_V2_7_1_DOWNLOAD_URL=${OOZIE_HADOOP_V2_7_1_FILE:-"http://sahara-files.mirantis.com/oozie-4.2.0-hadoop-2.7.1.tar.gz"}
    export DIB_HDFS_LIB_DIR="/opt/hadoop/share/hadoop/common/lib"
    export plugin_type="vanilla"

    if [ "$DIB_SPARK_VERSION" = "1.3.1" ]; then
        echo "Cannot use Spark 1.3.1 with Vanilla plugin"
        exit 1
    fi

    export DIB_SPARK_VERSION

    if [ "$DIB_SPARK_VERSION" = "1.6.0" ]; then
        export SPARK_HADOOP_DL=hadoop2.6
    else
        export SPARK_HADOOP_DL=hadoop2.7
    fi

    ubuntu_elements_sequence="hadoop oozie mysql hive $JAVA_ELEMENT swift_hadoop spark s3_hadoop"
    fedora_elements_sequence="hadoop oozie mysql disable-firewall hive $JAVA_ELEMENT swift_hadoop spark s3_hadoop"
    centos_elements_sequence="hadoop oozie mysql disable-firewall hive $JAVA_ELEMENT swift_hadoop spark nc s3_hadoop"
    centos7_elements_sequence="hadoop oozie mysql disable-firewall hive $JAVA_ELEMENT swift_hadoop spark nc s3_hadoop"

    # Workaround for https://bugs.launchpad.net/diskimage-builder/+bug/1204824
    # https://bugs.launchpad.net/sahara/+bug/1252684
    if [ "$platform" = 'ubuntu' ]; then
        echo "**************************************************************"
        echo "WARNING: As a workaround for DIB bug 1204824, you are about to"
        echo "         create a Fedora and CentOS images that has SELinux    "
        echo "         disabled. Do not use these images in production.       "
        echo "**************************************************************"
        fedora_elements_sequence="$fedora_elements_sequence selinux-permissive"
        centos_elements_sequence="$centos_elements_sequence selinux-permissive"
        centos7_elements_sequence="$centos7_elements_sequence selinux-permissive"
        suffix=".selinux-permissive"
    fi
    # Ubuntu cloud image
    if [ -z "$BASE_IMAGE_OS" -o "$BASE_IMAGE_OS" = "ubuntu" ]; then
        export DIB_CLOUD_INIT_DATASOURCES=$CLOUD_INIT_DATASOURCES

        if [ -z "$HADOOP_VERSION" -o "$HADOOP_VERSION" = "2.7.1" ]; then
            export DIB_HADOOP_VERSION=${DIB_HADOOP_VERSION_2_7_1:-"2.7.1"}
            export ubuntu_image_name=${ubuntu_vanilla_hadoop_2_7_1_image_name:-"ubuntu_sahara_vanilla_hadoop_2_7_1_latest"}
            export DIB_RELEASE=${DIB_RELEASE:-xenial}
            image_create ubuntu $ubuntu_image_name $ubuntu_elements_sequence
            unset DIB_RELEASE
        fi
        unset DIB_CLOUD_INIT_DATASOURCES
    fi

    # Fedora cloud image
    if [ -z "$BASE_IMAGE_OS" -o "$BASE_IMAGE_OS" = "fedora" ]; then
        if [ -z "$HADOOP_VERSION" -o "$HADOOP_VERSION" = "2.7.1" ]; then
            export DIB_HADOOP_VERSION=${DIB_HADOOP_VERSION_2_7_1:-"2.7.1"}
            export fedora_image_name=${fedora_vanilla_hadoop_2_7_1_image_name:-"fedora_sahara_vanilla_hadoop_2_7_1_latest$suffix"}
            image_create fedora $fedora_image_name $fedora_elements_sequence
        fi
    fi

    # CentOS 6 cloud image
    if [ "$BASE_IMAGE_OS" = "centos" ]; then
        if [ -z "$HADOOP_VERSION" -o "$HADOOP_VERSION" = "2.7.1" ]; then
            export DIB_HADOOP_VERSION=${DIB_HADOOP_VERSION_2_7_1:-"2.7.1"}
            export centos_image_name=${centos_vanilla_hadoop_2_7_1_image_name:-"centos_sahara_vanilla_hadoop_2_7_1_latest$suffix"}
            image_create centos $centos_image_name $centos_elements_sequence
        fi
    fi

    # CentOS 7 cloud image
    if [ -z "$BASE_IMAGE_OS" -o "$BASE_IMAGE_OS" = "centos7" ]; then
        if [ -z "$HADOOP_VERSION" -o "$HADOOP_VERSION" = "2.7.1" ]; then
            export DIB_HADOOP_VERSION=${DIB_HADOOP_VERSION_2_7_1:-"2.7.1"}
            export centos7_image_name=${centos7_vanilla_hadoop_2_7_1_image_name:-"centos7_sahara_vanilla_hadoop_2_7_1_latest$suffix"}
            image_create centos7 $centos7_image_name $centos7_elements_sequence
        fi
    fi

    unset plugin_type
    unset DIB_HDFS_LIB_DIR
    unset DIB_SPARK_VERSION
    unset SPARK_HADOOP_DL
fi

##########################
# Image for Spark plugin #
##########################

if [ -z "$PLUGIN" -o "$PLUGIN" = "spark" ]; then
    export DIB_HDFS_LIB_DIR="/usr/lib/hadoop-mapreduce"
    export DIB_CLOUD_INIT_DATASOURCES=$CLOUD_INIT_DATASOURCES
    export DIB_SPARK_VERSION
    export plugin_type="spark"
    export DIB_CDH_VERSION="5.5"
    # Tell the cloudera element to install only hdfs
    export DIB_CDH_HDFS_ONLY=1

    ubuntu_elements_sequence="$JAVA_ELEMENT swift_hadoop spark hadoop-cloudera s3_hadoop"
    export ubuntu_image_name=${ubuntu_spark_image_name:-"ubuntu_sahara_spark_latest"}

    # Creating Ubuntu cloud image
    export DIB_RELEASE=${DIB_RELEASE:-trusty}
    image_create ubuntu $ubuntu_image_name $ubuntu_elements_sequence
    unset DIB_CLOUD_INIT_DATASOURCES
    unset DIB_HDFS_LIB_DIR
    unset DIB_CDH_HDFS_ONLY
    unset DIB_CDH_VERSION
    unset DIB_SPARK_VERSION
    unset DIB_HADOOP_VERSION
    unset DIB_RELEASE
    unset plugin_type
fi


##########################
# Image for Storm plugin #
##########################

if [ -z "$PLUGIN" -o "$PLUGIN" = "storm" ]; then
    export DIB_CLOUD_INIT_DATASOURCES=$CLOUD_INIT_DATASOURCES

    export DIB_STORM_VERSION
    export ubuntu_image_name=${ubuntu_storm_image_name:-"ubuntu_sahara_storm_latest_$DIB_STORM_VERSION"}

    ubuntu_elements_sequence="$JAVA_ELEMENT zookeeper storm"

    # Creating Ubuntu cloud image
    export DIB_RELEASE=${DIB_RELEASE:-xenial}
    image_create ubuntu $ubuntu_image_name $ubuntu_elements_sequence
    unset DIB_RELEASE
    unset DIB_CLOUD_INIT_DATASOURCES
fi

############################
# Images for Ambari plugin #
############################

if [ -z "$PLUGIN" -o "$PLUGIN" = "ambari" ]; then
    export DIB_AMBARI_VERSION="$HADOOP_VERSION"
    export plugin_type="ambari"
    # set the temporary folder for hadoop-openstack.jar file
    export DIB_HDFS_LIB_DIR="/opt"

    if [ -z "$BASE_IMAGE_OS" -o "$BASE_IMAGE_OS" = "ubuntu" ]; then
        ambari_ubuntu_image_name=${ambari_ubuntu_image_name:-ubuntu_sahara_ambari}
        ambari_element_sequence="ambari $JAVA_ELEMENT swift_hadoop kdc s3_hadoop"
        export DIB_RELEASE="trusty"
        image_create ubuntu $ambari_ubuntu_image_name $ambari_element_sequence
        unset DIB_RELEASE
    fi
    if [ "$BASE_IMAGE_OS" = "centos" ]; then
        ambari_centos_image_name=${ambari_centos_image_name:-centos_sahara_ambari}
        ambari_element_sequence="ambari $JAVA_ELEMENT disable-firewall swift_hadoop kdc nc s3_hadoop"
        image_create centos $ambari_centos_image_name $ambari_element_sequence
    fi
    if [ -z "$BASE_IMAGE_OS" -o "$BASE_IMAGE_OS" = "centos7" ]; then
        ambari_centos7_image_name=${ambari_centos7_image_name:-"centos7-sahara-ambari"}
        ambari_element_sequence="disable-selinux ambari $JAVA_ELEMENT disable-firewall swift_hadoop kdc nc s3_hadoop"
        image_create centos7 $ambari_centos7_image_name $ambari_element_sequence
    fi

    unset DIB_HDFS_LIB_DIR
    unset plugin_type
    unset DIB_AMBARI_VERSION
fi

#########################
# Images for CDH plugin #
#########################

if [ -z "$PLUGIN" -o "$PLUGIN" = "cloudera" ]; then
    # Cloudera installation requires additional space
    export DIB_MIN_TMPFS=5
    export plugin_type="cloudera"
    export DIB_HDFS_LIB_DIR="/usr/lib/hadoop-mapreduce"

    if [ -n "$DIB_CDH_MINOR_VERSION" ]; then
        # cut minor version number, e.g. from 5.7.1 to  5.7
        # this is needed if user specified minor version but didn't specify
        # hadoop version by '-v' parameter
        HADOOP_VERSION=${DIB_CDH_MINOR_VERSION%.*}
    fi

    cloudera_elements_sequence="hadoop-cloudera swift_hadoop kdc s3_hadoop"
    if [ -z "$BASE_IMAGE_OS" -o "$BASE_IMAGE_OS" = "ubuntu" ]; then
        if [ -z "$HADOOP_VERSION" -o "$HADOOP_VERSION" = "5.5" ]; then
            export DIB_CDH_VERSION="5.5"
            cloudera_5_5_ubuntu_image_name=${cloudera_5_5_ubuntu_image_name:-ubuntu_sahara_cloudera_5_5_0}

            # Cloudera supports 14.04 Ubuntu in 5.5
            export DIB_RELEASE="trusty"
            image_create ubuntu $cloudera_5_5_ubuntu_image_name $cloudera_elements_sequence
            unset DIB_CDH_VERSION DIB_RELEASE
        fi
        if [ -z "$HADOOP_VERSION" -o "$HADOOP_VERSION" = "5.7" ]; then
            export DIB_CDH_VERSION="5.7"
            export DIB_CDH_MINOR_VERSION=${DIB_CDH_MINOR_VERSION:-$DIB_CDH_VERSION.0}
            cloudera_5_7_ubuntu_image_name=${cloudera_5_7_ubuntu_image_name:-ubuntu_sahara_cloudera_$DIB_CDH_MINOR_VERSION}

            export DIB_RELEASE="trusty"
            image_create ubuntu $cloudera_5_7_ubuntu_image_name $cloudera_elements_sequence
            unset DIB_CDH_VERSION DIB_RELEASE DIB_CDH_MINOR_VERSION
        fi
        if [ -z "$HADOOP_VERSION" -o "$HADOOP_VERSION" = "5.9" ]; then
            export DIB_CDH_VERSION="5.9"
            export DIB_CDH_MINOR_VERSION=${DIB_CDH_MINOR_VERSION:-$DIB_CDH_VERSION.0}
            cloudera_5_9_ubuntu_image_name=${cloudera_5_9_ubuntu_image_name:-ubuntu_sahara_cloudera_$DIB_CDH_MINOR_VERSION}

            export DIB_RELEASE="trusty"
            image_create ubuntu $cloudera_5_9_ubuntu_image_name $cloudera_elements_sequence
            unset DIB_CDH_VERSION DIB_RELEASE DIB_CDH_MINOR_VERSION
        fi
        if [ -z "$HADOOP_VERSION" -o "$HADOOP_VERSION" = "5.11" ]; then
            export DIB_CDH_VERSION="5.11"
            export DIB_CDH_MINOR_VERSION=${DIB_CDH_MINOR_VERSION:-$DIB_CDH_VERSION.0}
            cloudera_5_11_ubuntu_image_name=${cloudera_5_11_ubuntu_image_name:-ubuntu_sahara_cloudera_$DIB_CDH_MINOR_VERSION}

            export DIB_RELEASE="xenial"
            image_create ubuntu $cloudera_5_11_ubuntu_image_name $cloudera_elements_sequence
            unset DIB_CDH_VERSION DIB_RELEASE DIB_CDH_MINOR_VERSION
        fi
    fi

    if [ "$BASE_IMAGE_OS" = "centos" ]; then
        centos_cloudera_elements_sequence="selinux-permissive disable-firewall nc s3_hadoop"
        if [ -z "$HADOOP_VERSION" -o "$HADOOP_VERSION" = "5.5" ]; then
            export DIB_CDH_VERSION="5.5"

            cloudera_5_5_centos_image_name=${cloudera_5_5_centos_image_name:-centos_sahara_cloudera_5_5_0}
            image_create centos $cloudera_5_5_centos_image_name $cloudera_elements_sequence $centos_cloudera_elements_sequence

            unset DIB_CDH_VERSION
        fi
    fi

    if [ -z "$BASE_IMAGE_OS" -o "$BASE_IMAGE_OS" = "centos7" ]; then
        centos7_cloudera_elements_sequence="selinux-permissive disable-firewall nc s3_hadoop"
        if [ -z "$HADOOP_VERSION" -o "$HADOOP_VERSION" = "5.5" ]; then
            export DIB_CDH_VERSION="5.5"

            cloudera_5_5_centos7_image_name=${cloudera_5_5_centos7_image_name:-centos7_sahara_cloudera_5_5_0}
            image_create centos7 $cloudera_5_5_centos7_image_name $cloudera_elements_sequence $centos7_cloudera_elements_sequence

            unset DIB_CDH_VERSION
        fi
        if [ -z "$HADOOP_VERSION" -o "$HADOOP_VERSION" = "5.7" ]; then
            export DIB_CDH_VERSION="5.7"
            export DIB_CDH_MINOR_VERSION=${DIB_CDH_MINOR_VERSION:-$DIB_CDH_VERSION.0}

            cloudera_5_7_centos7_image_name=${cloudera_5_7_centos7_image_name:-centos7_sahara_cloudera_$DIB_CDH_MINOR_VERSION}
            image_create centos7 $cloudera_5_7_centos7_image_name $cloudera_elements_sequence $centos7_cloudera_elements_sequence

            unset DIB_CDH_VERSION DIB_CDH_MINOR_VERSION
        fi
        if [ -z "$HADOOP_VERSION" -o "$HADOOP_VERSION" = "5.9" ]; then
            export DIB_CDH_VERSION="5.9"
            export DIB_CDH_MINOR_VERSION=${DIB_CDH_MINOR_VERSION:-$DIB_CDH_VERSION.0}

            cloudera_5_9_centos7_image_name=${cloudera_5_9_centos7_image_name:-centos7_sahara_cloudera_$DIB_CDH_MINOR_VERSION}
            image_create centos7 $cloudera_5_9_centos7_image_name $cloudera_elements_sequence $centos7_cloudera_elements_sequence

            unset DIB_CDH_VERSION DIB_CDH_MINOR_VERSION
        fi
        if [ -z "$HADOOP_VERSION" -o "$HADOOP_VERSION" = "5.11" ]; then
            export DIB_CDH_VERSION="5.11"
            export DIB_CDH_MINOR_VERSION=${DIB_CDH_MINOR_VERSION:-$DIB_CDH_VERSION.0}

            cloudera_5_11_centos7_image_name=${cloudera_5_11_centos7_image_name:-centos7_sahara_cloudera_$DIB_CDH_MINOR_VERSION}
            image_create centos7 $cloudera_5_11_centos7_image_name $cloudera_elements_sequence $centos7_cloudera_elements_sequence

            unset DIB_CDH_VERSION DIB_CDH_MINOR_VERSION
        fi
    fi

    unset DIB_CDH_MINOR_VERSION
    unset DIB_HDFS_LIB_DIR
    unset DIB_MIN_TMPFS
    unset plugin_type
fi

##########################
# Images for MapR plugin #
##########################
if [ -z "$PLUGIN" -o "$PLUGIN" = "mapr" ]; then
    export DIB_MAPR_VERSION=${DIB_MAPR_VERSION:-${DIB_DEFAULT_MAPR_VERSION}}
    export plugin_type="mapr"

    export DIB_CLOUD_INIT_DATASOURCES=$CLOUD_INIT_DATASOURCES

    export DIB_IMAGE_SIZE=${IMAGE_SIZE:-"10"}
    #MapR repository requires additional space
    export DIB_MIN_TMPFS=10

    mapr_ubuntu_elements_sequence="ssh hadoop-mapr $JAVA_ELEMENT s3_hadoop"
    mapr_centos_elements_sequence="ssh hadoop-mapr selinux-permissive $JAVA_ELEMENT disable-firewall nc s3_hadoop"

    if [ -z "$BASE_IMAGE_OS" -o "$BASE_IMAGE_OS" = "ubuntu" ]; then
        export DIB_RELEASE=${DIB_RELEASE:-trusty}

        mapr_ubuntu_image_name=${mapr_ubuntu_image_name:-ubuntu_${DIB_RELEASE}_mapr_${DIB_MAPR_VERSION}_latest}

        image_create ubuntu $mapr_ubuntu_image_name $mapr_ubuntu_elements_sequence

        unset DIB_RELEASE
    fi

    if [ "$BASE_IMAGE_OS" = "centos" ]; then
        mapr_centos_image_name=${mapr_centos_image_name:-centos_6.6_mapr_${DIB_MAPR_VERSION}_latest}

        image_create centos $mapr_centos_image_name $mapr_centos_elements_sequence

        unset DIB_CLOUD_INIT_DATASOURCES
    fi

    if [ -z "$BASE_IMAGE_OS" -o "$BASE_IMAGE_OS" = "centos7" ]; then
        mapr_centos7_image_name=${mapr_centos7_image_name:-centos_7_mapr_${DIB_MAPR_VERSION}_latest}

        image_create centos7 $mapr_centos7_image_name $mapr_centos_elements_sequence

        unset DIB_CLOUD_INIT_DATASOURCES
    fi
    unset plugin_type

fi

################
# Plain images #
################
if [ -z "$PLUGIN" -o "$PLUGIN" = "plain" ]; then
    # generate plain (no Hadoop components) images for testing

    common_elements="ssh"

    ubuntu_elements_sequence="$common_elements"
    fedora_elements_sequence="$common_elements"
    centos_elements_sequence="$common_elements disable-firewall disable-selinux nc"
    centos7_elements_sequence="$common_elements disable-firewall disable-selinux nc"

    if [ -z "$BASE_IMAGE_OS" -o "$BASE_IMAGE_OS" = "ubuntu" ]; then
        plain_image_name=${plain_ubuntu_image_name:-ubuntu_plain}

        image_create ubuntu $plain_image_name $ubuntu_elements_sequence
    fi

    if [ -z "$BASE_IMAGE_OS" -o "$BASE_IMAGE_OS" = "fedora" ]; then
        plain_image_name=${plain_fedora_image_name:-fedora_plain}

        image_create fedora $plain_image_name $fedora_elements_sequence
    fi

    if [ "$BASE_IMAGE_OS" = "centos" ]; then
        plain_image_name=${plain_centos_image_name:-centos_plain}

        image_create centos $plain_image_name $centos_elements_sequence
    fi

    if [ -z "$BASE_IMAGE_OS" -o "$BASE_IMAGE_OS" = "centos7" ]; then
        plain_image_name=${plain_centos7_image_name:-centos7_plain}

        image_create centos7 $plain_image_name $centos7_elements_sequence
    fi
fi