summaryrefslogtreecommitdiffstats
path: root/tests/utils/osclient.sh
blob: 5dd2b17cda5aa424774db5b47f01a9843fa64859 (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
#!/bin/bash
# Copyright 2017 AT&T Intellectual Property, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# What this is: Setup script for OpenStack Clients (OSC) running in  
# an Unbuntu Xenial docker container. You can use this script to isolate the 
# OSC from your host, so that the OSC and related install pre-reqs do not 
# pollute your host environment. You can then then modify your tests scripts on 
# your host and run them using the OSC container rather than moving the test 
# scripts to DevStack or an OpenStack installation (see below). You can also
# attach to the OSC container. Enter "sudo docker attach osclient" then hit enter
# twice and you will be in the container as root (there are no other users).
# Once in the container, you can "source /tmp/osclient/admin-openrc.sh" and use
# any OSC commands you want.
#
# Status: this is a work in progress, under test.
#
# How to use:
#   $ bash osclient.sh setup|run|clean (see detailed parameters below)
#   setup: install the OpenStack CLI clients in a container on the host.
#     $ bash osclient.sh setup <path to credential script> [branch]
#     <path to credential script>: OpenStack CLI env setup script (e.g.
#       admin-openrc.sh), obtained from the OpenStack Dashboard via
#       Project->Access->Security->API. It's also recommended that you set the
#       OpenStack password explicitly in that script rather than take the
#       default which will prompt you on every command you pass to the container.
#       For example, if the admin-openrc.sh file is in the same directory as
#       osclient.sh and you want to use stable/newton:
#         $ bash osclient.sh setup admin-openrc.sh stable/newton
#     branch: git repo branch to install (e.g. stable/newton)
#   run: run a command in the container
#     $ bash osclient.sh run <command>
#     <command>: command to run, in quotes e.g. 
#       bash osclient.sh run 'openstack service list'
#       bash osclient.sh run 'bash mytest.sh'
#   clean: remove the osclient container and shared folder
#     $ bash osclient.sh clean

trap 'fail' ERR

pass() {
  echo "$0: $(date) Install success!"
  end=`date +%s`
  runtime=$((end-start))
  echo "$0: $(date) Duration = $runtime seconds"
  exit 0
}

fail() {
  echo "$0: $(date) Install Failed!"
  end=`date +%s`
  runtime=$((end-start))
  runtime=$((runtime/60))
  echo "$0: $(date) Duration = $runtime seconds"
  exit 1
}

function create_container() {
  echo "$0: $(date) Creating docker container for osclient"
  if [ "$dist" == "Ubuntu" ]; then
    echo "$0: $(date) Ubuntu-based install"
    sudo apt-get update
    sudo apt-get install apt-transport-https ca-certificates
    sudo apt-key adv \
               --keyserver hkp://ha.pool.sks-keyservers.net:80 \
               --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
    repo="deb https://apt.dockerproject.org/repo ubuntu-xenial main"
    echo "$repo" | sudo tee /etc/apt/sources.list.d/docker.list
    sudo apt-get update
    sudo apt-get install -y linux-image-extra-$(uname -r) linux-image-extra-virtual
    sudo apt-get install -y docker-engine
    sudo service docker start
    # xenial is needed for python 3.5
    sudo docker pull ubuntu:xenial
    sudo service docker start
    sudo docker run -i -t -d -v /tmp/osclient/:/tmp/osclient --name osclient \
      ubuntu:xenial /bin/bash
    sudo docker exec osclient /bin/bash /tmp/osclient/osclient-setup.sh \
      setup /tmp/osclient/admin-openrc.sh $branch
  else 
    # Centos
    echo "Centos-based install"
    sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository--parents 
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg 
EOF
    sudo yum install -y docker-engine
    # xenial is needed for python 3.5
    sudo service docker start
    sudo docker pull ubuntu:xenial
    sudo docker run -i -t -d -v /tmp/osclient/:/tmp/osclient --name osclient \
      ubuntu:xenial /bin/bash
    sudo docker exec osclient /bin/bash /tmp/osclient/osclient-setup.sh setup \
      /tmp/osclient/admin-openrc.sh $branch
  fi
}

install_client () {
  echo "$0: $(date) Install $1"
  git clone https://github.com/openstack/$1.git
  cd $1
  if [ $# -eq 2 ]; then git checkout $2; fi
  pip install -r requirements.txt
  pip install .
  cd ..
}

function setup () {
  apt-get update
  apt-get install -y python
  apt-get install -y python-dev
  apt-get install -y python-pip
  apt-get install -y wget
  apt-get install -y git
  apt-get install -y apg
  apt-get install -y libffi-dev
  apt-get install -y libssl-dev

  cd /tmp/osclient

  echo "$0: $(date) Upgrage pip"
  pip install --upgrade pip

  echo "$0: $(date) Install OpenStack clients"
  install_client python-openstackclient $branch
  install_client python-neutronclient $branch
  install_client python-heatclient $branch
  install_client python-congressclient $branch

  echo "$0: $(date) Setup shell environment variables"
  echo "source $openrc" >>~/.bashrc
  source ~/.bashrc
}

start=`date +%s`
dist=`grep DISTRIB_ID /etc/*-release | awk -F '=' '{print $2}'`

case "$1" in
  setup)
    openrc=$2
    branch=$3
    if [[ -f /.dockerenv ]]; then
      echo "$0: $(date) Running inside docker - finish setup"
      setup $openrc $branch
    else
      echo "$0: $(date) Setup shared virtual folder and save $1 script there"
      if [[ ! -d /tmp/osclient ]]; then mkdir /tmp/osclient; fi
      cp $0 /tmp/osclient/osclient-setup.sh
      cp $openrc /tmp/osclient/admin-openrc.sh
      chmod 755 /tmp/osclient/*.sh
      create_container
    fi
    pass
    ;;
  run)
    cat >/tmp/osclient/command.sh <<EOF
source /tmp/osclient/admin-openrc.sh
$2
exit
EOF
    sudo docker exec osclient /bin/bash /tmp/osclient/command.sh "$0"
    ;;
  clean)
    sudo docker stop osclient
    sudo docker rm -v osclient
    rm -rf /tmp/osclient
    pass
    ;;
  *)
echo "   $ bash osclient.sh setup|run|clean (see detailed parameters below)"
echo "   setup: install the OpenStack CLI clients in a container on the host."
echo "     $ bash osclient.sh setup <path to credential script> [branch]"
echo "     <path to credential script>: OpenStack CLI env setup script (e.g." 
echo "       admin-openrc.sh), obtained from the OpenStack Dashboard via"
echo "       Project->Access->Security->API. It's also recommended that you set the" 
echo "       OpenStack password explicitly in that script rather than take the"
echo "       default which will prompt you on every command you pass to the container."
echo "       For example, if the admin-openrc.sh file is in the same directory as "
echo "       osclient.sh and you want to use stable/newton:"
echo "         $ bash osclient.sh setup admin-openrc.sh stable/newton"
echo "     branch: git repo branch to install (e.g. stable/newton)"
echo "   run: run a command in the container"
echo "     $ bash osclient.sh run <command>"
echo "     <command>: command to run, in quotes e.g." 
echo "       bash osclient.sh run 'openstack service list'"
echo "       bash osclient.sh run 'bash mytest.sh'"
echo "   clean: remove the osclient container and shared folder"
echo "     $ bash osclient.sh clean"
fail
esac