Commit 87068c90 authored by David Johnson's avatar David Johnson

Add python3 support to our various scripts.

(Note that if the system default python is python2, the call to getmanifests.py
will use python2, so test-common.py must continue to support python2.  But
when setup-user-info.py goes to add ssh keys via the novaclient library, for
release >= Stein, that is python3.  Thus test-common.py must also be python3-
compat.)
parent ec70ca6b
......@@ -30,6 +30,7 @@
#
#
import six
import sys
import pwd
import getopt
......@@ -38,7 +39,9 @@ import re
import os.path
dirname = os.path.abspath(os.path.dirname(sys.argv[0]))
execfile("%s/test-common.py" % (dirname,))
exec(open("%s/test-common.py" % (dirname,)).read())
myprint = six.print_
#
# Convert the certificate into a credential.
......@@ -56,7 +59,7 @@ if rval:
Fatal("Could not get manifests")
pass
if len(sys.argv) < 2:
print response["value"]
myprint(response["value"])
else:
f = open("%s.xml" % (sys.argv[1],),'w')
value = response["value"]["manifests"]
......
......@@ -414,6 +414,28 @@ if [ $OSVERSION -ge $OSSTEIN ]; then
PYPKGPREFIX="python3"
ISPYTHON3=1
PYTHONBINNAME="python3"
#
# If our openstack python libs will be python3, we also need
# python3-cryptography for setup-user-info.py, because that file
# must be run by whatever python version contains the novaclient
# library. And we don't know that we need that until here, unlike
# above where we install the v2 python-cryptography.
#
if [ $GENIUSER -eq 1 ]; then
dpkg -s python-cryptography >/dev/null 2>&1
if [ ! $? -eq 0 ]; then
apt-get $DPKGOPTS install $APTGETINSTALLOPTS ${PYTHONBINNAME}-cryptography
# Keep trying again with updated cache forever;
# we must have this package.
success=$?
while [ ! $success -eq 0 ]; do
apt-get update
apt-get $DPKGOPTS install $APTGETINSTALLOPTS ${PYTHONBINNAME}-cryptography
success=$?
done
fi
fi
fi
#
......
......@@ -20,6 +20,9 @@ import os.path
#import novaclient
import traceback
import logging
import six
myprint = six.print_
LOG = logging.getLogger(__name__)
# Define a default handler at INFO logging level
......@@ -33,11 +36,11 @@ KEYSTONE_OPTS = [ 'OS_PROJECT_DOMAIN_ID','OS_USER_DOMAIN_ID',
'OS_USERNAME','OS_PASSWORD','OS_AUTH_URL' ]
#'OS_IDENTITY_API_VERSION'
execfile(CLOUDLAB_SETTINGS_FILE)
execfile(CLOUDLAB_AUTH_FILE)
exec(open(CLOUDLAB_SETTINGS_FILE).read())
exec(open(CLOUDLAB_AUTH_FILE).read())
dirname = os.path.abspath(os.path.dirname(sys.argv[0]))
execfile("%s/test-common.py" % (dirname,))
exec(open("%s/test-common.py" % (dirname,)).read())
#
# Convert the certificate into a credential.
......@@ -137,7 +140,7 @@ for userdict in response['value']:
urn = userdict['urn']
login = userdict['login']
for keydict in userdict['keys']:
if not keydict.has_key('type') or keydict['type'] != 'ssh':
if not 'type' in keydict or keydict['type'] != 'ssh':
continue
key = keydict['key']
......@@ -152,7 +155,7 @@ for userdict in response['value']:
rname += 'X'
pass
if not keysdone.has_key(rname):
if not rname in keysdone:
try:
nova.keypairs.create(rname,key)
keysdone[rname] = key
......@@ -189,14 +192,14 @@ else:
# where user_id=\'${AUID}\'
cmd = 'export AAUID="`%s | awk \'/ adminapi / {print $2}\'`" ; export AUID="`%s | awk \'/ admin / {print $2}\'`" ; mysqldump -u nova --password=%s nova -t key_pairs --skip-comments --quote-names --no-create-info --no-create-db --complete-insert --compact | sed -e \'s/,[0-9]*,/,NULL,/gi\' | sed -e "s/,\'${AAUID}\',/,\'${AUID}\',/gi" | mysql -u nova --password=%s nova ; echo "update key_pairs set deleted=0" | mysql -u nova --password=%s nova' % (os_cred_stuff,os_cred_stuff,NOVA_DBPASS,NOVA_DBPASS,NOVA_DBPASS,)
#cmd = 'export OS_PASSWORD="%s" ; export OS_AUTH_URL="%s" ; export OS_USERNAME="%s" ; export OS_TENANT_NAME="%s" ; export AAUID="`keystone user-list | awk \'/ adminapi / {print $2}\'`" ; export AUID="`keystone user-list | awk \'/ admin / {print $2}\'`" ; mysqldump -u nova --password=%s nova -t key_pairs --skip-comments --quote-names --no-create-info --no-create-db --complete-insert --compact | sed -e \'s/,[0-9]*,/,NULL,/gi\' | sed -e "s/,\'${AAUID}\',/,\'${AUID}\',/gi" | mysql -u nova --password=%s nova ; echo "update key_pairs set deleted=0 where user_id=\'${AUID}\'" | mysql -u nova --password=%s nova' % (OS_PASSWORD,OS_AUTH_URL,OS_USERNAME,OS_PASSWORD,NOVA_DBPASS,NOVA_DBPASS,NOVA_DBPASS,)
print "Running adminapi -> admin key import: %s..." % (cmd,)
myprint("Running adminapi -> admin key import: %s..." % (cmd,))
os.system(cmd)
#
# Ugh, the tables are now split between nova and nova_api ... so just do this too.
#
cmd = 'export AAUID="`%s | awk \'/ adminapi / {print $2}\'`" ; export AUID="`%s | awk \'/ admin / {print $2}\'`" ; mysqldump -u nova --password=%s nova_api -t key_pairs --skip-comments --quote-names --no-create-info --no-create-db --complete-insert --compact | sed -e \'s/,[0-9]*,/,NULL,/gi\' | sed -e "s/,\'${AAUID}\',/,\'${AUID}\',/gi" | mysql -u nova --password=%s nova_api ; echo "update key_pairs set deleted=0" | mysql -u nova_api --password=%s nova' % (os_cred_stuff,os_cred_stuff,NOVA_DBPASS,NOVA_DBPASS,NOVA_DBPASS,)
print "Running adminapi -> admin key import: %s..." % (cmd,)
myprint("Running adminapi -> admin key import: %s..." % (cmd,))
os.system(cmd)
sys.exit(0)
......@@ -27,11 +27,21 @@
# }}}
#
from urlparse import urlsplit, urlunsplit
from urllib import splitport
import xmlrpclib
import six
try:
from urlparse import urlsplit, urlunsplit
from urllib import splitport
import xmlrpclib
import httplib
except:
from urllib.parse import urlsplit, urlunsplit
from urllib.request import splitport
import xmlrpc.client as xmlrpclib
import http.client as httplib
import os
import getopt
import sys
import time
import httplib
import traceback
import ssl
from cryptography import x509
......@@ -73,16 +83,18 @@ authenticate=DEFAULTAUTHENTICATE
verify = False
cacertificate = None
myprint = six.print_
if "Usage" not in dir():
def Usage():
print "usage: " + sys.argv[ 0 ] + " [option...]"
print "Options:"
myprint("usage: " + sys.argv[ 0 ] + " [option...]")
myprint("Options:")
BaseOptions()
pass
pass
def BaseOptions():
print """
myprint("""
-a file, --admincredentials=file read admin credentials from file
-A, --authenticated authenticate client
-c file, --credentials=file read self-credentials from file
......@@ -94,12 +106,12 @@ def BaseOptions():
-l uri, --sa=uri specify uri of slice authority
[default: local]
-m uri, --cm=uri specify uri of component manager
[default: local]"""
[default: local]""")
if "ACCEPTSLICENAME" in globals():
print """ -n name, --slicename=name specify human-readable name of slice
[default: mytestslice]"""
myprint(""" -n name, --slicename=name specify human-readable name of slice
[default: mytestslice]""")
pass
print """ -p file, --passphrase=file read passphrase from file
myprint(""" -p file, --passphrase=file read passphrase from file
[default: ~/.ssl/password]
-r file, --read-commands=file specify additional configuration file
-s file, --slicecredentials=file read slice credentials from file
......@@ -107,7 +119,7 @@ def BaseOptions():
-S file, --speaksfor=file read speaksfor credential from file
-U, --unauthenticated do not authenticate client
--verify enable server verification
--cacertificate=file read CA certificate from file"""
--cacertificate=file read CA certificate from file""")
pass
try:
......@@ -120,8 +132,8 @@ try:
"speaksfor=", "unauthenticated",
"delete", "verify", "cacertificate=" ] )
except getopt.GetoptError, err:
print >> sys.stderr, str( err )
except getopt.GetoptError:
myprint(str(sys.exc_info()[1]),file=sys.stderr)
Usage()
sys.exit( 1 )
......@@ -186,16 +198,17 @@ try:
fd = open(CERTIFICATE)
certdata = fd.read()
fd.close()
except IOError, e:
print 'Error reading certificate file %s: %s' % (CERTIFICATE,e.strerror)
except IOError:
myprint('Error reading certificate file %s: %s' % (CERTIFICATE,sys.exc_info()[1].strerror))
cert = None
try:
cert = x509.load_pem_x509_certificate(certdata,default_backend())
except Exception, e:
print 'Error loading certificate: %s' % (str(e))
cert = x509.load_pem_x509_certificate(six.b(certdata),default_backend())
except Exception:
myprint('Error loading certificate: %s' % (str(sys.exc_info()[1])))
if verify and cacertificate is not None:
if not os.access(cacertificate, os.R_OK):
print "CA Certificate cannot be accessed: " + cacertificate
myprint("CA Certificate cannot be accessed: " + cacertificate)
sys.exit(-1);
# XMLRPC server: use www.emulab.net for the clearinghouse.
......@@ -222,9 +235,9 @@ try:
if port:
path = ":" + port + path
SERVER_PATH["default"] = path
except Exception, err:
except Exception:
if debug:
print "Warning: error getting authInfoAccess extension value:"
myprint("Warning: error getting authInfoAccess extension value:")
traceback.print_exc()
pass
......@@ -255,17 +268,17 @@ if os.path.exists(PASSPHRASEFILE):
passphrase = open(PASSPHRASEFILE).readline()
passphrase = passphrase.strip()
if passphrase == '':
print 'Passphrase file empty; you may be prompted'
myprint('Passphrase file empty; you may be prompted')
passphrase = None
except IOError, e:
print 'Error reading passphrase file %s: %s' % (
PASSPHRASEFILE,e.strerror)
except IOError:
myprint('Error reading passphrase file %s: %s' % (
PASSPHRASEFILE,sys.exc_info()[1].strerror))
else:
if debug:
print 'Passphrase file %s does not exist' % (PASSPHRASEFILE)
myprint('Passphrase file %s does not exist' % (PASSPHRASEFILE))
def Fatal(message):
print >> sys.stderr, message
myprint(message,file=sys.stderr)
sys.exit(1)
def geni_am_response_handler(method, method_args):
......@@ -327,12 +340,12 @@ def do_method(module, method, params, URI=None, quiet=False, version=None,
url = urlsplit(URI, "https")
if debug:
print str( url ) + " " + method
myprint(str( url ) + " " + method)
if url.scheme == "https":
if authenticate and not cert:
if not quiet:
print >> sys.stderr, "error: missing emulab certificate: " + CERTIFICATE
myprint("error: missing emulab certificate: " + CERTIFICATE,file=sys.stderr)
return (-1, None)
port = url.port if url.port else 443
......@@ -371,32 +384,34 @@ def do_method(module, method, params, URI=None, quiet=False, version=None,
response = server.getresponse()
if response.status == 503:
if not quiet:
print >> sys.stderr, "Will try again in a moment. Be patient!"
myprint("Will try again in a moment. Be patient!",file=sys.stderr)
time.sleep(5.0)
continue
elif response.status != 200:
if not quiet:
print >> sys.stderr, str(response.status) + " " + response.reason
myprint(str(response.status) + " " + response.reason,file=sys.stderr)
return (-1,None)
response = xmlrpclib.loads( response.read() )[ 0 ][ 0 ]
break
except httplib.HTTPException, e:
if not quiet: print >> sys.stderr, e
except httplib.HTTPException:
if not quiet: myprint(sys.exc_info()[1],file=sys.stderr)
return (-1, None)
except xmlrpclib.Fault, e:
except xmlrpclib.Fault:
e = sys.exc_info()[1]
if e.faultCode == 503:
print >> sys.stderr, e.faultString + " Retrying\n";
myprint(e.faultString + " Retrying\n",file=sys.stderr)
time.sleep(5.0)
continue;
if not quiet: print >> sys.stderr, e.faultString
if not quiet: myprint(e.faultString,file=sys.stderr)
return (-1, None)
except ssl.CertificateError, e:
except ssl.CertificateError:
e = sys.exc_info()[1]
if not quiet:
print >> sys.stderr, "Warning: possible certificate host name mismatch."
print >> sys.stderr, "Please consult:"
print >> sys.stderr, " http://www.protogeni.net/trac/protogeni/wiki/HostNameMismatch"
print >> sys.stderr, "for recommended solutions."
print >> sys.stderr, e
myprint("Warning: possible certificate host name mismatch.",file=sys.stderr)
myprint("Please consult:",file=sys.stderr)
myprint(" http://www.protogeni.net/trac/protogeni/wiki/HostNameMismatch",file=sys.stderr)
myprint("for recommended solutions.",file=sys.stderr)
myprint(e,file=sys.stderr)
pass
return (-1, None)
......@@ -406,7 +421,7 @@ def do_method(module, method, params, URI=None, quiet=False, version=None,
# Dictionary, hence the code below.
#
if response[ "code" ] and len(response["output"]):
if not quiet: print >> sys.stderr, response["output"] + ":",
if not quiet: myprint(response["output"] + ":",file=sys.stderr)
pass
rval = response["code"]
......@@ -440,7 +455,7 @@ def do_method_retry(suffix, method, params):
rval, response = do_method(suffix, method, params)
while count > 0 and response and response["code"] == 14:
count = count - 1
print " Will try again in a few seconds\n"
myprint(" Will try again in a few seconds\n")
time.sleep(5.0)
rval, response = do_method(suffix, method, params)
return (rval, response)
......@@ -448,7 +463,7 @@ def do_method_retry(suffix, method, params):
def resolve_slice( name, selfcredential ):
if slicecredentialfile:
myslice = {}
myslice["urn"] = SLICEURN
myslice["urn"] = SLICEURN
return myslice
params = {}
params["credential"] = mycredential
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment