Skip to content

Commit

Permalink
Upgrade to python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Imran.Hassan authored and syedimranhassan committed Sep 16, 2019
1 parent 22a645d commit 2d4c3d0
Show file tree
Hide file tree
Showing 74 changed files with 741 additions and 346 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ language: python
dist: xenial
python:
- "2.7"
- "3.6"

branches:
only:
- master

services:
- docker

addons:
apt:
packages:
Expand All @@ -23,6 +24,7 @@ before_install:
install:
- "pip install demjson"
- "pip install -r requirements.txt"
- if [ "$TRAVIS_PYTHON_VERSION" = "3.6" ]; then pip install -r requirements3.txt; fi

env:
- MAKE_TARGET=test.syntax SHARD=0 SHARDS=1
Expand Down
1 change: 1 addition & 0 deletions openedx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ supporting_teams:

oeps:
oep-2: true
oep-7: true
1 change: 1 addition & 0 deletions playbooks/active_instances_in_asg.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""

from __future__ import print_function
from __future__ import absolute_import
import argparse
import botocore.session
import botocore.exceptions
Expand Down
7 changes: 4 additions & 3 deletions playbooks/callback_plugins/task_timing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# From https://github.com/ansible/ansible/issues/31527#issuecomment-335495855
from __future__ import (absolute_import, division, print_function)
import six
__metaclass__ = type

import collections
Expand Down Expand Up @@ -193,8 +194,8 @@ def log_play(self, playbook_name, playbook_timestamp, results):

# Sort the tasks by their running time
sorted_results = sorted(
results.items(),
key=lambda (task, timestamp): timestamp.duration,
list(results.items()),
key=lambda task_timestamp: task_timestamp[1].duration,
reverse=True
)

Expand Down Expand Up @@ -275,7 +276,7 @@ def playbook_on_stats(self, stats):
# Flatten the stats so that multiple runs of the same task get listed
# individually.
flat_stats = {}
for task_name, task_runs in self.stats.iteritems():
for task_name, task_runs in six.iteritems(self.stats):
if len(task_runs) == 1:
flat_stats[task_name] = task_runs[0]
else:
Expand Down
38 changes: 21 additions & 17 deletions playbooks/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@

######################################################################

from __future__ import absolute_import
from __future__ import print_function
import sys
import os
import argparse
Expand All @@ -117,8 +119,10 @@
from boto import ec2
from boto import rds
from boto import route53
import ConfigParser
import six.moves.configparser
import traceback
import six
from six.moves import range

try:
import json
Expand Down Expand Up @@ -157,7 +161,7 @@ def __init__(self):
data_to_print = self.get_inventory_from_cache()
else:
data_to_print = self.json_format_dict(self.inventory, True)
print data_to_print
print(data_to_print)


def is_cache_valid(self):
Expand All @@ -181,7 +185,7 @@ def is_cache_valid(self):
def read_settings(self):
''' Reads the settings from the ec2.ini file '''

config = ConfigParser.SafeConfigParser()
config = six.moves.configparser.SafeConfigParser()
config.read(self.args.inifile)

# is eucalyptus?
Expand Down Expand Up @@ -290,7 +294,7 @@ def get_instances_by_region(self, region):

# connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
if conn is None:
print("region name: %s likely not supported, or AWS is down. connection to region failed." % region)
print(("region name: %s likely not supported, or AWS is down. connection to region failed." % region))
sys.exit(1)

reservations = conn.get_all_instances()
Expand All @@ -301,8 +305,8 @@ def get_instances_by_region(self, region):

except boto.exception.BotoServerError as e:
if not self.eucalyptus:
print "Looks like AWS is down again:"
print e
print("Looks like AWS is down again:")
print(e)
sys.exit(1)

def get_rds_instances_by_region(self, region):
Expand All @@ -316,8 +320,8 @@ def get_rds_instances_by_region(self, region):
for instance in instances:
self.add_rds_instance(instance, region)
except boto.exception.BotoServerError as e:
print "Looks like AWS RDS is down: "
print e
print("Looks like AWS RDS is down: ")
print(e)
sys.exit(1)

def get_instance(self, region, instance_id):
Expand All @@ -330,7 +334,7 @@ def get_instance(self, region, instance_id):

# connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
if conn is None:
print("region name: %s likely not supported, or AWS is down. connection to region failed." % region)
print(("region name: %s likely not supported, or AWS is down. connection to region failed." % region))
sys.exit(1)

reservations = conn.get_all_instances([instance_id])
Expand Down Expand Up @@ -382,12 +386,12 @@ def add_instance(self, instance, region):
key = self.to_safe("security_group_" + group.name)
self.push(self.inventory, key, dest)
except AttributeError:
print 'Package boto seems a bit older.'
print 'Please upgrade boto >= 2.3.0.'
print('Package boto seems a bit older.')
print('Please upgrade boto >= 2.3.0.')
sys.exit(1)

# Inventory: Group by tag keys
for k, v in instance.tags.iteritems():
for k, v in six.iteritems(instance.tags):
key = self.to_safe("tag_" + k + "=" + v)
self.push(self.inventory, key, dest)
self.keep_first(self.inventory, 'first_in_' + key, dest)
Expand Down Expand Up @@ -439,8 +443,8 @@ def add_rds_instance(self, instance, region):
key = self.to_safe("security_group_" + instance.security_group.name)
self.push(self.inventory, key, dest)
except AttributeError:
print 'Package boto seems a bit older.'
print 'Please upgrade boto >= 2.3.0.'
print('Package boto seems a bit older.')
print('Please upgrade boto >= 2.3.0.')
sys.exit(1)

# Inventory: Group by engine
Expand Down Expand Up @@ -523,14 +527,14 @@ def get_host_info(self):
# Handle complex types
if type(value) in [int, bool]:
instance_vars[key] = value
elif type(value) in [str, unicode]:
elif type(value) in [str, six.text_type]:
instance_vars[key] = value.strip()
elif type(value) == type(None):
instance_vars[key] = ''
elif key == 'ec2_region':
instance_vars[key] = value.name
elif key == 'ec2_tags':
for k, v in value.iteritems():
for k, v in six.iteritems(value):
key = self.to_safe('ec2_tag_' + k)
instance_vars[key] = v
elif key == 'ec2_groups':
Expand Down Expand Up @@ -615,7 +619,7 @@ def json_format_dict(self, data, pretty=False):
# Run the script
RETRIES = 3

for _ in xrange(RETRIES):
for _ in range(RETRIES):
try:
Ec2Inventory()
break
Expand Down
6 changes: 4 additions & 2 deletions playbooks/library/ec2_acl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import
from __future__ import print_function
DOCUMENTATION = """
---
module: ec2_acl
Expand Down Expand Up @@ -54,7 +56,7 @@ import sys
try:
import boto.vpc
except ImportError:
print "failed=True msg={0}".format(sys.executable)
print("failed=True msg={0}".format(sys.executable))
#print "failed=True msg='boto required for this module'"
sys.exit(1)

Expand Down Expand Up @@ -258,7 +260,7 @@ def main():
if region:
try:
connection = boto.vpc.connect_to_region(region, profile_name=profile)
except boto.exception.NoAuthHandlerFound, e:
except boto.exception.NoAuthHandlerFound as e:
module.fail_json(msg=str(e))
else:
module.fail_json(msg="region must be specified")
Expand Down
12 changes: 8 additions & 4 deletions playbooks/library/ec2_group_local
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# -*- coding: utf-8 -*-


from __future__ import absolute_import
from __future__ import print_function
import six
from functools import reduce
DOCUMENTATION = '''
---
module: ec2_group
Expand Down Expand Up @@ -103,7 +107,7 @@ EXAMPLES = '''
try:
import boto.ec2
except ImportError:
print "failed=True msg='boto required for this module'"
print("failed=True msg='boto required for this module'")
sys.exit(1)


Expand Down Expand Up @@ -239,7 +243,7 @@ def main():
'''found a match, delete it'''
try:
group.delete()
except Exception, e:
except Exception as e:
module.fail_json(msg="Unable to delete security group '%s' - %s" % (group, e))
else:
group = None
Expand Down Expand Up @@ -318,7 +322,7 @@ def main():
changed = True

# Finally, remove anything left in the groupRules -- these will be defunct rules
for rule in groupRules.itervalues():
for rule in six.itervalues(groupRules):
for grant in rule.grants:
grantGroup = None
if grant.group_id:
Expand Down Expand Up @@ -382,7 +386,7 @@ def main():
del groupRules[default_egress_rule]

# Finally, remove anything left in the groupRules -- these will be defunct rules
for rule in groupRules.itervalues():
for rule in six.itervalues(groupRules):
for grant in rule.grants:
grantGroup = None
if grant.group_id:
Expand Down
6 changes: 4 additions & 2 deletions playbooks/library/ec2_iam_role
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import
from __future__ import print_function
DOCUMENTATION = """
---
module: ec2_iam_role
Expand Down Expand Up @@ -54,7 +56,7 @@ import sys
try:
import boto
except ImportError:
print "failed=True msg='boto required for this module'"
print("failed=True msg='boto required for this module'")
sys.exit(1)

def present(connection, module):
Expand Down Expand Up @@ -151,7 +153,7 @@ def main():

try:
connection = boto.connect_iam(profile_name=profile)
except boto.exception.NoAuthHandlerFound, e:
except boto.exception.NoAuthHandlerFound as e:
module.fail_json(msg = str(e))

state = module.params.get('state')
Expand Down
15 changes: 9 additions & 6 deletions playbooks/library/ec2_lookup
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import
from __future__ import print_function
import six
DOCUMENTATION = '''
---
module: ec2_lookup
Expand Down Expand Up @@ -82,7 +85,7 @@ try:
import boto.ec2
from boto.ec2 import connect_to_region
except ImportError:
print "failed=True msg='boto required for this module'"
print("failed=True msg='boto required for this module'")
sys.exit(1)


Expand Down Expand Up @@ -110,26 +113,26 @@ def main():
try:
ec2 = connect_to_region(region, aws_access_key_id=aws_access_key,
aws_secret_access_key=aws_secret_key)
except boto.exception.NoAuthHandlerFound, e:
except boto.exception.NoAuthHandlerFound as e:
module.fail_json(msg=str(e))
# If we specified an ec2_url then try connecting to it
elif ec2_url:
try:
ec2 = boto.connect_ec2_endpoint(ec2_url, aws_access_key,
aws_secret_key)
except boto.exception.NoAuthHandlerFound, e:
except boto.exception.NoAuthHandlerFound as e:
module.fail_json(msg=str(e))
else:
module.fail_json(msg="Either region or ec2_url must be specified")

instances = []
instance_ids = []
for res in ec2.get_all_instances(filters={'tag:' + tag: value
for tag, value in module.params.get('tags').iteritems()}):
for tag, value in six.iteritems(module.params.get('tags'))}):
for inst in res.instances:
if inst.state == "running":
instances.append({k: v for k, v in inst.__dict__.iteritems()
if isinstance(v, (basestring))})
instances.append({k: v for k, v in six.iteritems(inst.__dict__)
if isinstance(v, (six.string_types))})
instance_ids.append(inst.id)
module.exit_json(changed=False, instances=instances,
instance_ids=instance_ids)
Expand Down
6 changes: 4 additions & 2 deletions playbooks/library/ec2_rt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import
from __future__ import print_function
DOCUMENTATION = """
---
module: ec2_rt
Expand Down Expand Up @@ -53,7 +55,7 @@ import sys
try:
import boto.vpc
except ImportError:
print "failed=True msg={0}".format(sys.executable)
print("failed=True msg={0}".format(sys.executable))
sys.exit(1)


Expand Down Expand Up @@ -211,7 +213,7 @@ def main():
if region:
try:
connection = boto.vpc.connect_to_region(region,profile_name=profile)
except boto.exception.NoAuthHandlerFound, e:
except boto.exception.NoAuthHandlerFound as e:
module.fail_json(msg = str(e))
else:
module.fail_json(msg="region must be specified")
Expand Down
Loading

0 comments on commit 2d4c3d0

Please sign in to comment.