aboutsummaryrefslogtreecommitdiffstats
path: root/juju/juju_setup.sh
blob: eb49360400a1801ec7d9a4af1df300be4dc5ab8a (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
#!/bin/bash
##############################################################################
# Copyright (c) 2016-2017 HUAWEI TECHNOLOGIES CO.,LTD and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

IMG_DIR=${WORK_DIR}/img
CSAR_DIR=${WORK_DIR}/csar

function juju_env_prepare()
{
    sudo rm -f /root/.ssh/known_hosts
    sudo rm -f /root/.ssh/known_hosts.old

    mkdir -p ${WORK_DIR}/venv
    sudo pip install --upgrade virtualenv
    virtualenv ${WORK_DIR}/venv

    source ${WORK_DIR}/venv/bin/activate
    pip install --upgrade python-openstackclient python-neutronclient
}

function juju_download_img()
{
    if [ ! -e ${IMG_DIR}/${1##*/} ];then
        wget -O ${IMG_DIR}/${1##*/} $1
    fi
}

function juju_download_csar()
{
    if [ ! -e ${CSAR_DIR}/${1##*/} ];then
        wget -O ${CSAR_DIR}/${1##*/} $1
    fi
}

function juju_prepare()
{
    log_info "juju_prepare enter"

    mkdir -p $IMG_DIR
    for((i=0;i<${#JUJU_IMG_NAME[@]};i++))
    do
        juju_download_img ${JUJU_IMG_URL[i]}
        if [[ ! $(glance image-list | grep ${JUJU_IMG_NAME[i]}) ]]; then
            glance image-create --name=${JUJU_IMG_NAME[i]} \
                --disk-format qcow2 --container-format=bare \
                --visibility=public --file ${IMG_DIR}/${JUJU_IMG_URL[i]##*/}
        fi
    done

    wget -nc -O $IMG_DIR/$JUJU_VM_IMG $JUJU_VM_IMG_URL
    if [[ $(glance image-list | grep $JUJU_VM_IMG) ]]; then
        openstack image delete $JUJU_VM_IMG
    fi
    glance image-create --name=$JUJU_VM_IMG \
        --disk-format qcow2 --container-format=bare \
        --visibility=public --file $IMG_DIR/$JUJU_VM_IMG

    mkdir -p $CSAR_DIR
    for((i=0;i<${#CSAR_NAME[@]};i++))
    do
        juju_download_csar ${CSAR_URL[i]}
    done

    if [[ ! $(neutron net-list | grep juju-net) ]]; then
        neutron net-create juju-net
    fi

    if [[ ! $(neutron subnet-list | grep juju-subnet) ]]; then
        neutron subnet-create juju-net $JUJU_NET_CIDR --name juju-subnet --gateway $JUJU_NET_GW \
                                                      --dns_nameservers list=true 8.8.8.8
    fi

    if [[ ! $(neutron router-list | grep juju-router) ]]; then
        neutron router-create juju-router
        neutron router-interface-add juju-router juju-subnet
        neutron router-gateway-set juju-router ext-net
    fi

    local default_secgroup_id=$(nova secgroup-list | grep "Default security group" | awk '{print $2}')

    if [[ ! $(neutron security-group-rule-list | grep default | grep "icmp") ]]; then
        neutron security-group-rule-create --direction ingress --protocol icmp \
                                           --remote-ip-prefix 0.0.0.0/0 $default_secgroup_id
    fi

    if [[ ! $(neutron security-group-rule-list | grep default | grep "icmp") ]]; then
        neutron security-group-rule-create --direction egress --protocol icmp \
                                           --remote-ip-prefix 0.0.0.0/0 $default_secgroup_id
    fi

    if [[ ! $(neutron security-group-rule-list | grep default | grep "tcp") ]]; then
        neutron security-group-rule-create --direction ingress --protocol tcp \
                                           --remote-ip-prefix 0.0.0.0/0 $default_secgroup_id
    fi

    if [[ ! $(neutron security-group-rule-list | grep default | grep "tcp") ]]; then
        neutron security-group-rule-create --direction egress --protocol tcp \
                                           --remote-ip-prefix 0.0.0.0/0 $default_secgroup_id
    fi

    if [[ ! $(neutron security-group-rule-list | grep default | grep "udp") ]]; then
        neutron security-group-rule-create --direction ingress --protocol udp \
                                           --remote-ip-prefix 0.0.0.0/0 $default_secgroup_id
    fi

    if [[ ! $(neutron security-group-rule-list | grep default | grep "udp") ]]; then
        neutron security-group-rule-create --direction egress --protocol udp \
                                           --remote-ip-prefix 0.0.0.0/0 $default_secgroup_id
    fi

    echo -e 'n\n'|ssh-keygen -q -t rsa -N "" -f /root/.ssh/id_rsa 1>/dev/null

    openstack keypair delete jump-key | true
    openstack keypair create --public-key /root/.ssh/id_rsa.pub jump-key

    openstack flavor show m1.tiny   || openstack flavor create --ram 512 --disk 5 --vcpus 1 --public m1.tiny
    openstack flavor show m1.small  || openstack flavor create --ram 1024 --disk 10 --vcpus 1 --public m1.small
    openstack flavor show m1.medium || openstack flavor create --ram 2048 --disk 10 --vcpus 2 --public m1.medium
    openstack flavor show m1.large  || openstack flavor create --ram 3072 --disk 10 --vcpus 2 --public m1.large
    openstack flavor show m1.xlarge || openstack flavor create --ram 8096 --disk 30 --vcpus 4 --public m1.xlarge

    openstack quota set --instances 20 admin
    openstack quota set --core 30 admin
}