summaryrefslogtreecommitdiffstats
path: root/controller/bin/dtf-get-machine.in
blob: 2a668b3f86113ec8f099fbb6d6f3c44ebeaf0e3c (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
#!/bin/bash

. "@sysconfdir@/dtf.sh" || exit 1

opt_quiet=0

die() { echo "$@" ; exit 1 ; }
info()
{
    test "$opt_quiet" -eq 0 && echo " * $@"
}

opt_openstack_instance="$DTF_OPENSTACK_DEFAULT_ID"
opt_distro=fedora
opt_distro_version=20

function show_help()
{
cat <<EOHELP >&2
Usage: $0 OPTION

Script is aimed to help sysadmin.

Options:
  --distro=NAME              Distro name, like fedora
  --distro-version=VERSION   E.g. 20 for Fedora 20
  --openstack-instance=ID
  --name=NAME                Name of the machine
  --quite                    Print only the VM IP address to stdout
EOHELP
test -n "$1" && exit "$1"
}

boot()
(
    set -o pipefail
    nova boot "$1" --poll \
            --image "$2" \
            --flavor "$3" \
            --security-groups "$os_security_group" \
            --key-name "$os_keypair" \
        | grep "| id " | cut -d\| -f 3 | xargs -n 1
)

get_ip()
(
    id="$1"
    set -o pipefail
    nova show "$id" | grep ' network ' \
        | cut -d\| -f 3 | cut -d, -f2 | xargs -n 1
)


longopts="distro:,distro-version:,openstack-instance:,name:,quiet"
ARGS=$(getopt -o "" -l "help,$longopts" -n "$0" -- "$@") \
    || exit 1
eval set -- "$ARGS"

while true; do
    case "$1" in
    --distro|--distro-version|--openstack-instance|--name)
        opt=$(sed -e 's/^--//' -e 's/[^[a-zA-Z0-9]/_/g'<<<"$1")
        eval "opt_$opt=\"${2,,}\""
        shift 2
        ;;

    --quiet)
        opt=$(sed -e 's/^--//' -e 's/[^[a-zA-Z0-9]/_/g'<<<"$1")
        eval "opt_$opt=1"
        shift
        ;;

    --help)
        show_help 0
        ;;

    --)
        shift
        break
        ;;
    *)
        echo >&2 "programmer mistake"
        exit 1
        ;;
    esac
done

if test -n "$DTF_GET_MACHINE_FAKE_IP"; then
    echo "$DTF_GET_MACHINE_FAKE_IP"
    exit 0
fi

test -z "$opt_openstack_instance" && die "no openstack intance ID used"

. "@pkgdatadir@/parse_credsfile" "$opt_openstack_instance" || exit 1
. "$HOME/.dtf/os/$opt_openstack_instance.sh" || exit 1

image_version=$opt_distro$opt_distro_version
image=${os_image_ids[$image_version]}
flavor=${os_flavor_ids[$image_version]}

test -z "$image" && die "no image for '$image_version'"
test -z "$flavor" && die "no flavor for '$image_version'"
test -z "$opt_name" && opt_name="testing-$image"

info "booting machine $image_version from $image"

machine=$(boot "$opt_name" "$image" "$flavor") || exit 1

info "machine id: $machine"
get_ip "$machine"