summaryrefslogtreecommitdiffstats
path: root/deploy/prepare.sh
blob: d326bd5b1ad454cb57dd50854d146d7cbf48de28 (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
#!/bin/bash
##############################################################################
# Copyright (c) 2016 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
##############################################################################
function print_logo()
{

    figlet -ctf slant Compass Installer
    set +x; sleep 2; set -x
}

function download_iso()
{
    iso_name=`basename $ISO_URL`
    rm -f $WORK_DIR/cache/"$iso_name.md5"
    curl --connect-timeout 10 -o $WORK_DIR/cache/"$iso_name.md5" $ISO_URL.md5
    if [[ -f $WORK_DIR/cache/$iso_name ]]; then
        local_md5=`md5sum $WORK_DIR/cache/$iso_name | cut -d ' ' -f 1`
        repo_md5=`cat $WORK_DIR/cache/$iso_name.md5 | cut -d ' ' -f 1`
        if [[ "$local_md5" == "$repo_md5" ]]; then
            return
        fi
    fi

    curl --connect-timeout 10 -o $WORK_DIR/cache/$iso_name $ISO_URL
}

function prepare_env() {

    sudo service libvirt-bin restart
    if sudo service openvswitch-switch status|grep stop; then
        sudo service openvswitch-switch start
    fi

    # prepare work dir
    rm -rf $WORK_DIR/{installer,vm,network,iso}
    mkdir -p $WORK_DIR/installer
    mkdir -p $WORK_DIR/vm
    mkdir -p $WORK_DIR/network
    mkdir -p $WORK_DIR/iso
    mkdir -p $WORK_DIR/cache

    download_iso

    cp $WORK_DIR/cache/`basename $ISO_URL` $WORK_DIR/iso/centos.iso -f

    # copy compass
    mkdir -p $WORK_DIR/mnt
    sudo mount -o loop $WORK_DIR/iso/centos.iso $WORK_DIR/mnt
    cp -rf $WORK_DIR/mnt/compass/compass-core $WORK_DIR/installer/
    cp -rf $WORK_DIR/mnt/compass/compass-install $WORK_DIR/installer/
    sudo umount $WORK_DIR/mnt
    rm -rf $WORK_DIR/mnt

    chmod 755 $WORK_DIR -R

    sudo cp ${COMPASS_DIR}/deploy/qemu_hook.sh /etc/libvirt/hooks/qemu
}

function  _prepare_python_env() {
   rm -rf $WORK_DIR/venv
   mkdir -p $WORK_DIR/venv

   if [[ "$DEPLOY_FIRST_TIME" == "true" ]]; then

        if [[ ! -z "$JHPKG_URL" ]]; then
             _pre_env_setup
        else
             sudo apt-get update -y
             sudo apt-get install -y --force-yes mkisofs bc curl ipmitool openvswitch-switch
             sudo apt-get install -y --force-yes git python-dev python-pip figlet
             sudo apt-get install -y --force-yes libxslt-dev libxml2-dev libvirt-dev build-essential qemu-utils qemu-kvm libvirt-bin virtinst libmysqld-dev
             sudo apt-get install -y --force-yes libffi-dev libssl-dev

        fi
   fi

   if [[ ! -z "$JHPKG_URL" ]]; then
        _pre_pip_setup
   else
        sudo pip install --upgrade virtualenv
        virtualenv $WORK_DIR/venv
        source $WORK_DIR/venv/bin/activate

        pip install --upgrade cffi
        pip install --upgrade MarkupSafe
        pip install --upgrade pip
        pip install --upgrade cheetah
        pip install --upgrade pyyaml
        pip install --upgrade requests
        pip install --upgrade netaddr
        pip install --upgrade oslo.config
        pip install --upgrade ansible==2.1.1.0
   fi
}

function _pre_env_setup()
{
     rm -rf $WORK_DIR/prepare
     mkdir -p $WORK_DIR/prepare
     jhpkg_url=${JHPKG_URL:7}
     echo $jhpkg_url
     if [[ ! -f "$jhpkg_url" ]]; then
          echo "There is no jh_env_package."
          exit 1
     fi

     tar -zxvf $jhpkg_url -C $WORK_DIR/prepare/
     cd $WORK_DIR/prepare/jh_env_package
     tar -zxvf trusty-jh-ppa.tar.gz

     if [[ ! -z /etc/apt/sources.list.d ]]; then
          mv /etc/apt/sources.list.d /etc/apt/sources.list.d.bak
     fi

     if [[ -f /etc/apt/apt.conf ]]; then
          mv /etc/apt/apt.conf /etc/apt/apt.conf.bak
     fi

     cat << EOF > /etc/apt/apt.conf
APT::Get::Assume-Yes "true";
APT::Get::force-yes "true";
Acquire::http::Proxy::127.0.0.1:9998 DIRECT;
EOF

     if [[ -f /etc/apt/sources.list ]]; then
          mv /etc/apt/sources.list /etc/apt/sources.list.bak
     fi

     cat << EOF > /etc/apt/sources.list
deb [arch=amd64] http://127.0.0.1:9998/trusty-jh-ppa trusty main
EOF

     nohup python -m SimpleHTTPServer 9998 &

     cd -
     sleep 5
     apt-get update
     apt-get install -y mkisofs bc curl ipmitool openvswitch-switch \
         git python-pip python-dev figlet \
         libxslt-dev libxml2-dev libvirt-dev \
         build-essential qemu-utils qemu-kvm libvirt-bin \
         virtinst libmysqld-dev \
         libssl-dev libffi-dev python-cffi
     pid=$(ps -ef | grep SimpleHTTPServer | grep 9998 | awk '{print $2}')
     echo $pid
     kill -9 $pid

     sudo cp ${COMPASS_DIR}/deploy/qemu_hook.sh /etc/libvirt/hooks/qemu

     rm -rf /etc/apt/sources.list
     if [[ -f /etc/apt/sources.list.bak ]]; then
          mv /etc/apt/sources.list.bak /etc/apt/sources.list
     fi

     rm -rf /etc/apt/apt.conf
     if [[ -f /etc/apt/apt.conf.bak ]]; then
          mv /etc/apt/apt.conf.bak /etc/apt/apt.conf
     fi

     if [[ ! -z /etc/apt/sources.list.d.bak ]]; then
          mv /etc/apt/sources.list.d.bak /etc/apt/sources.list.d
     fi
}

function _pre_pip_setup()
{
     if [[ -d ~/.pip ]]; then
          if [[ -f ~/.pip/pip.conf ]]; then
               mv ~/.pip/pip.conf ~/.pip/pip.conf.bak
          fi
     else
          mkdir -p ~/.pip
     fi

#     rm -rf ~/.pip
#     mkdir -p ~/.pip
     rm -rf $WORK_DIR/prepare
     mkdir -p $WORK_DIR/prepare
     jhpkg_url=${JHPKG_URL:7}
     echo $jhpkg_url
     if [[ ! -f "$jhpkg_url" ]]; then
          echo "There is no jh_env_package."
          exit 1
     fi

     tar -zxvf $jhpkg_url -C $WORK_DIR/prepare/
     cd $WORK_DIR/prepare/jh_env_package
     tar -zxvf env_trusty_pip.tar.gz

     cat << EOF > ~/.pip/pip.conf
[global]
find-links = http://127.0.0.1:9999/jh_pip
no-index = true
[install]
trusted-host=127.0.0.1
EOF

     nohup python -m SimpleHTTPServer 9999 &

     sleep 5

     cd -

     pip install --upgrade virtualenv

     virtualenv $WORK_DIR/venv
     source $WORK_DIR/venv/bin/activate

     #pip install --upgrade cffi

     PIP="cffi MarkupSafe pip cheetah pyyaml requests netaddr oslo.config ansible"

     #PIP="paramiko jinja2 PyYAML setuptools pycrypto pyasn1 cryptography MarkupSafe idna six enum34 ipaddress pycparser virtualenv cheetah requests netaddr pbr oslo.config ansible"
     for i in ${PIP}; do
        pip install --upgrade $i
     done

     pid=$(ps -ef | grep SimpleHTTPServer | grep 9999 | awk '{print $2}')
     echo $pid
     kill -9 $pid

     if [[ -f ~/.pip/pip.conf.bak ]]; then
          mv ~/.pip/pip.conf.bak ~/.pip/pip.conf
     else
          rm -rf ~/.pip/pip.conf
     fi
#     rm -rf ~/.pip/pip.conf
}

function prepare_python_env()
{
    if [[ "$DEPLOY_FIRST_TIME" == "true" ]]; then
        _prepare_python_env
    else
        source $WORK_DIR/venv/bin/activate
        if [[ $? -ne 0 ]]; then
            _prepare_python_env
        fi
    fi
    which python
}