2011-05-12 09:19:01 -04:00
|
|
|
|
2010-08-10 12:44:38 -04:00
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
|
|
|
# Copyright 2010 United States Government as represented by the
|
|
|
|
# Administrator of the National Aeronautics and Space Administration.
|
|
|
|
# All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Copyright 2010 OpenStack, LLC
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2012-03-28 19:21:11 +00:00
|
|
|
"""Installation script for Nova's development virtualenv."""
|
2010-07-26 23:16:49 +02:00
|
|
|
|
2011-10-30 09:00:59 -04:00
|
|
|
import optparse
|
2010-07-26 23:16:49 +02:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
2010-08-09 17:45:00 -04:00
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
2011-12-08 15:14:48 -08:00
|
|
|
VENV = os.path.join(ROOT, '.venv')
|
2010-07-26 23:16:49 +02:00
|
|
|
PIP_REQUIRES = os.path.join(ROOT, 'tools', 'pip-requires')
|
2012-01-07 19:34:41 -08:00
|
|
|
TEST_REQUIRES = os.path.join(ROOT, 'tools', 'test-requires')
|
2011-05-16 11:16:55 -05:00
|
|
|
PY_VERSION = "python%s.%s" % (sys.version_info[0], sys.version_info[1])
|
|
|
|
|
2011-05-02 16:55:54 -04:00
|
|
|
|
2010-07-26 23:16:49 +02:00
|
|
|
def die(message, *args):
|
2011-06-01 10:37:54 -04:00
|
|
|
print >> sys.stderr, message % args
|
2011-05-16 11:16:55 -05:00
|
|
|
sys.exit(1)
|
|
|
|
|
2010-07-26 23:16:49 +02:00
|
|
|
|
2011-05-02 16:55:54 -04:00
|
|
|
def check_python_version():
|
2011-05-16 11:16:55 -05:00
|
|
|
if sys.version_info < (2, 6):
|
|
|
|
die("Need Python Version >= 2.6")
|
|
|
|
|
2011-05-02 16:55:54 -04:00
|
|
|
|
2011-09-29 15:06:54 +01:00
|
|
|
def run_command_with_code(cmd, redirect_output=True, check_exit_code=True):
|
2012-03-28 19:21:11 +00:00
|
|
|
"""Runs a command in an out-of-process shell.
|
|
|
|
|
|
|
|
Returns the output of that command. Working directory is ROOT.
|
2011-05-16 11:16:55 -05:00
|
|
|
"""
|
|
|
|
if redirect_output:
|
|
|
|
stdout = subprocess.PIPE
|
|
|
|
else:
|
|
|
|
stdout = None
|
2010-07-26 23:16:49 +02:00
|
|
|
|
2011-05-16 11:16:55 -05:00
|
|
|
proc = subprocess.Popen(cmd, cwd=ROOT, stdout=stdout)
|
|
|
|
output = proc.communicate()[0]
|
|
|
|
if check_exit_code and proc.returncode != 0:
|
|
|
|
die('Command "%s" failed.\n%s', ' '.join(cmd), output)
|
2011-09-29 15:06:54 +01:00
|
|
|
return (output, proc.returncode)
|
2010-07-26 23:16:49 +02:00
|
|
|
|
|
|
|
|
2011-09-29 15:06:54 +01:00
|
|
|
def run_command(cmd, redirect_output=True, check_exit_code=True):
|
|
|
|
return run_command_with_code(cmd, redirect_output, check_exit_code)[0]
|
2010-07-26 23:16:49 +02:00
|
|
|
|
2010-08-10 12:51:03 -04:00
|
|
|
|
2011-09-29 15:06:54 +01:00
|
|
|
class Distro(object):
|
|
|
|
|
|
|
|
def check_cmd(self, cmd):
|
|
|
|
return bool(run_command(['which', cmd], check_exit_code=False).strip())
|
|
|
|
|
|
|
|
def install_virtualenv(self):
|
|
|
|
if self.check_cmd('virtualenv'):
|
|
|
|
return
|
2011-05-16 11:16:55 -05:00
|
|
|
|
2011-09-29 15:06:54 +01:00
|
|
|
if self.check_cmd('easy_install'):
|
2011-05-16 11:16:55 -05:00
|
|
|
print 'Installing virtualenv via easy_install...',
|
2011-09-29 15:06:54 +01:00
|
|
|
if run_command(['easy_install', 'virtualenv']):
|
|
|
|
print 'Succeeded'
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
print 'Failed'
|
|
|
|
|
|
|
|
die('ERROR: virtualenv not found.\n\nNova development'
|
|
|
|
' requires virtualenv, please install it using your'
|
|
|
|
' favorite package management tool')
|
|
|
|
|
2011-11-09 14:00:55 -05:00
|
|
|
def post_process(self):
|
|
|
|
"""Any distribution-specific post-processing gets done here.
|
|
|
|
|
|
|
|
In particular, this is useful for applying patches to code inside
|
2012-03-28 19:21:11 +00:00
|
|
|
the venv.
|
|
|
|
"""
|
2011-11-09 14:00:55 -05:00
|
|
|
pass
|
|
|
|
|
2011-09-29 15:06:54 +01:00
|
|
|
|
|
|
|
class Fedora(Distro):
|
2011-11-01 15:46:24 -04:00
|
|
|
"""This covers all Fedora-based distributions.
|
|
|
|
|
|
|
|
Includes: Fedora, RHEL, CentOS, Scientific Linux"""
|
2011-09-29 15:06:54 +01:00
|
|
|
|
|
|
|
def check_pkg(self, pkg):
|
|
|
|
return run_command_with_code(['rpm', '-q', pkg],
|
|
|
|
check_exit_code=False)[1] == 0
|
|
|
|
|
|
|
|
def yum_install(self, pkg, **kwargs):
|
2012-01-05 13:15:15 +00:00
|
|
|
print "Attempting to install '%s' via yum" % pkg
|
2011-09-29 15:06:54 +01:00
|
|
|
run_command(['sudo', 'yum', 'install', '-y', pkg], **kwargs)
|
|
|
|
|
2011-11-15 08:43:46 +00:00
|
|
|
def apply_patch(self, originalfile, patchfile):
|
|
|
|
run_command(['patch', originalfile, patchfile])
|
|
|
|
|
2011-09-29 15:06:54 +01:00
|
|
|
def install_virtualenv(self):
|
|
|
|
if self.check_cmd('virtualenv'):
|
|
|
|
return
|
|
|
|
|
|
|
|
if not self.check_pkg('python-virtualenv'):
|
|
|
|
self.yum_install('python-virtualenv', check_exit_code=False)
|
|
|
|
|
|
|
|
super(Fedora, self).install_virtualenv()
|
|
|
|
|
2011-11-09 14:00:55 -05:00
|
|
|
def post_process(self):
|
|
|
|
"""Workaround for a bug in eventlet.
|
|
|
|
|
|
|
|
This currently affects RHEL6.1, but the fix can safely be
|
|
|
|
applied to all RHEL and Fedora distributions.
|
|
|
|
|
2012-03-28 19:21:11 +00:00
|
|
|
This can be removed when the fix is applied upstream.
|
2011-11-09 14:00:55 -05:00
|
|
|
|
|
|
|
Nova: https://bugs.launchpad.net/nova/+bug/884915
|
2012-03-28 19:21:11 +00:00
|
|
|
Upstream: https://bitbucket.org/which_linden/eventlet/issue/89
|
|
|
|
"""
|
2011-11-09 14:00:55 -05:00
|
|
|
|
|
|
|
# Install "patch" program if it's not there
|
|
|
|
if not self.check_pkg('patch'):
|
|
|
|
self.yum_install('patch')
|
|
|
|
|
|
|
|
# Apply the eventlet patch
|
2011-11-15 08:43:46 +00:00
|
|
|
self.apply_patch(os.path.join(VENV, 'lib', PY_VERSION, 'site-packages',
|
|
|
|
'eventlet/green/subprocess.py'),
|
|
|
|
'contrib/redhat-eventlet.patch')
|
2011-11-09 14:00:55 -05:00
|
|
|
|
2011-09-29 15:06:54 +01:00
|
|
|
|
|
|
|
def get_distro():
|
2012-05-16 17:08:27 +08:00
|
|
|
if (os.path.exists('/etc/fedora-release') or
|
|
|
|
os.path.exists('/etc/redhat-release')):
|
2011-09-29 15:06:54 +01:00
|
|
|
return Fedora()
|
|
|
|
else:
|
|
|
|
return Distro()
|
|
|
|
|
|
|
|
|
|
|
|
def check_dependencies():
|
|
|
|
get_distro().install_virtualenv()
|
2010-07-26 23:16:49 +02:00
|
|
|
|
|
|
|
|
2011-10-30 09:00:59 -04:00
|
|
|
def create_virtualenv(venv=VENV, no_site_packages=True):
|
2012-03-28 19:21:11 +00:00
|
|
|
"""Creates the virtual environment and installs PIP.
|
|
|
|
|
|
|
|
Creates the virtual environment and installs PIP only into the
|
|
|
|
virtual environment.
|
2011-05-16 11:16:55 -05:00
|
|
|
"""
|
|
|
|
print 'Creating venv...',
|
2011-10-30 09:00:59 -04:00
|
|
|
if no_site_packages:
|
|
|
|
run_command(['virtualenv', '-q', '--no-site-packages', VENV])
|
|
|
|
else:
|
|
|
|
run_command(['virtualenv', '-q', VENV])
|
2011-05-16 11:16:55 -05:00
|
|
|
print 'done.'
|
|
|
|
print 'Installing pip in virtualenv...',
|
2012-01-02 13:35:57 -08:00
|
|
|
if not run_command(['tools/with_venv.sh', 'easy_install',
|
|
|
|
'pip>1.0']).strip():
|
2011-05-16 11:16:55 -05:00
|
|
|
die("Failed to install pip.")
|
|
|
|
print 'done.'
|
2010-07-26 23:16:49 +02:00
|
|
|
|
|
|
|
|
2011-09-29 15:06:54 +01:00
|
|
|
def pip_install(*args):
|
2011-09-29 15:06:54 +01:00
|
|
|
run_command(['tools/with_venv.sh',
|
|
|
|
'pip', 'install', '--upgrade'] + list(args),
|
2011-09-29 15:06:54 +01:00
|
|
|
redirect_output=False)
|
|
|
|
|
|
|
|
|
2010-07-26 23:16:49 +02:00
|
|
|
def install_dependencies(venv=VENV):
|
2011-05-16 11:16:55 -05:00
|
|
|
print 'Installing dependencies with pip (this can take a while)...'
|
2011-09-29 15:06:54 +01:00
|
|
|
|
2011-10-25 14:26:13 +01:00
|
|
|
# First things first, make sure our venv has the latest pip and distribute.
|
2012-09-06 21:40:44 -04:00
|
|
|
# NOTE: we keep pip at version 1.1 since the most recent version causes
|
|
|
|
# the .venv creation to fail. See:
|
|
|
|
# https://bugs.launchpad.net/nova/+bug/1047120
|
|
|
|
pip_install('pip==1.1')
|
2011-10-25 14:26:13 +01:00
|
|
|
pip_install('distribute')
|
2011-10-19 18:03:51 -04:00
|
|
|
|
2011-05-16 11:16:55 -05:00
|
|
|
# Install greenlet by hand - just listing it in the requires file does not
|
|
|
|
# get it in stalled in the right order
|
2011-09-29 15:06:54 +01:00
|
|
|
pip_install('greenlet')
|
|
|
|
|
|
|
|
pip_install('-r', PIP_REQUIRES)
|
2012-01-07 19:34:41 -08:00
|
|
|
pip_install('-r', TEST_REQUIRES)
|
2011-05-16 11:16:55 -05:00
|
|
|
|
2010-08-17 11:03:15 -04:00
|
|
|
|
2011-11-09 14:00:55 -05:00
|
|
|
def post_process():
|
|
|
|
get_distro().post_process()
|
|
|
|
|
|
|
|
|
2010-07-26 23:16:49 +02:00
|
|
|
def print_help():
|
2011-05-16 11:16:55 -05:00
|
|
|
help = """
|
|
|
|
Nova development environment setup is complete.
|
2010-07-26 23:16:49 +02:00
|
|
|
|
2011-05-16 11:16:55 -05:00
|
|
|
Nova development uses virtualenv to track and manage Python dependencies
|
|
|
|
while in development and testing.
|
2010-07-26 23:16:49 +02:00
|
|
|
|
2011-05-16 11:16:55 -05:00
|
|
|
To activate the Nova virtualenv for the extent of your current shell
|
|
|
|
session you can run:
|
2010-07-26 23:16:49 +02:00
|
|
|
|
2011-12-08 15:14:48 -08:00
|
|
|
$ source .venv/bin/activate
|
2010-07-26 23:16:49 +02:00
|
|
|
|
2011-05-16 11:16:55 -05:00
|
|
|
Or, if you prefer, you can run commands in the virtualenv on a case by case
|
|
|
|
basis by running:
|
2010-07-26 23:16:49 +02:00
|
|
|
|
2011-05-16 11:16:55 -05:00
|
|
|
$ tools/with_venv.sh <your command>
|
2010-07-26 23:16:49 +02:00
|
|
|
|
2011-05-16 11:16:55 -05:00
|
|
|
Also, make test will automatically use the virtualenv.
|
|
|
|
"""
|
|
|
|
print help
|
2010-07-26 23:16:49 +02:00
|
|
|
|
|
|
|
|
2011-10-30 09:00:59 -04:00
|
|
|
def parse_args():
|
2012-03-28 19:21:11 +00:00
|
|
|
"""Parses command-line arguments."""
|
2011-10-30 09:00:59 -04:00
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option("-n", "--no-site-packages", dest="no_site_packages",
|
|
|
|
default=False, action="store_true",
|
|
|
|
help="Do not inherit packages from global Python install")
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2010-07-26 23:16:49 +02:00
|
|
|
def main(argv):
|
2011-10-30 09:00:59 -04:00
|
|
|
(options, args) = parse_args()
|
2011-05-16 11:16:55 -05:00
|
|
|
check_python_version()
|
|
|
|
check_dependencies()
|
2011-10-30 09:00:59 -04:00
|
|
|
create_virtualenv(no_site_packages=options.no_site_packages)
|
2011-05-16 11:16:55 -05:00
|
|
|
install_dependencies()
|
2011-11-09 14:00:55 -05:00
|
|
|
post_process()
|
2011-05-16 11:16:55 -05:00
|
|
|
print_help()
|
2010-07-26 23:16:49 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2011-05-16 11:16:55 -05:00
|
|
|
main(sys.argv)
|