Python 2.4.4 and boto-2.0b3 import hashlib error

Python 2.4.4 and boto-2.0b3 import hashlib error has a fix found here

Running an older Debian AMI with RAID0 XFS EBS volumes, I patch boto-2.0b3. Hopefully, have this instance migrated using a newer Ubuntu EBS AMI and use ec2-consistent-snapshot instead.

1. Copy a patch file to a temporary file:

# vi /tmp/c_14.patch
--- boto-2.0b3/boto/ec2/   Fri Nov 12 14:16:07 2010
+++ boto/ec2/      Fri Nov 12 15:19:21 2010
@@ -28,7 +28,6 @@
 import base64
 import hmac
 import boto
-from hashlib import sha1 as sha
 from boto.connection import AWSQueryConnection
 from boto.resultset import ResultSet
 from boto.ec2.image import Image, ImageAttribute
@@ -51,6 +50,11 @@
 from boto.ec2.tag import Tag
 from boto.exception import EC2ResponseError
+    from hashlib import sha1 as sha
+except ImportError:
+    import sha
 class EC2Connection(AWSQueryConnection):

2. Now we can get the source of boto:

# cd /usr/lib/python2.4/
# mv boto-2.0b3-py2.4.eggboto-2.0b3-py2.4.egg boto-2.0b3-py2.4.egg.old
# easy_install -Z boto 
# cd boto-2.0b3-py2.4.egg/boto/ec2
# patch < /tmp/c_14.patch

3. A simple Python script to snapshot NFS volumes, for the meantime. Note if you want to use it, you need to supply the access and secret keys.

#!/usr/bin/env python 
# Filename: 
# Desc:  Snapshot XFS volumes.
# Date: Dec 14, 2010
# Author:
import getopt, sys, os

aws_access_key = ''
aws_secret_key = ''

def usage():
    print 'Description: Snapshot a mounted volume formatted as XFS.'  
    print 'Usage:'
    print '  -m /mount_dir -v vol-xxxx,vol-xxxx -d volume-description'
    print ''

def snapshot_volumes(mount_dir, volumes,vol_desc):
    import boto 
    from boto.ec2.connection import EC2Connection 
    conn = None 
       #conn = EC2Connection()
       conn = EC2Connection(aws_access_key, aws_secret_key)
    except TypeError, boto.exception.EC2ResponseError:
       print 'Error: aws keys not set.'

    isMounted = False
    print 'Checking mount directory: '  + mount_dir , 
    if os.path.ismount(mount_dir): 
       isMounted = True
       print ' mounted.'
       print  ' not mounted.'

    if isMounted: 
       print 'Executing xfs_freeze -f on ' + mount_dir
       os.system("xfs_freeze -f " + mount_dir)

    for volume in volumes: 
          snapshot = None
          if vol_desc is None: 
             snapshot = conn.create_snapshot(volume)
             print 'Setting volume description to: ' + vol_desc
             snapshot = conn.create_snapshot(volume,vol_desc)
          print "Volume: " + volume + " Snapshot:" +
       except boto.exception.EC2ResponseError:
          print 'Error snapshotting volume: ' + volume 

    if isMounted: 
       print 'Executing xfs_freeze -u on ' + mount_dir
       os.system("xfs_freeze -u " + mount_dir)

def main():
        opts, args = getopt.getopt(sys.argv[1:], "v:m:d:h", ["help"])
    except getopt.GetoptError, err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"

    mount_dir = None
    vol_desc = None 
    volumes =  []

    for o, arg in opts:
        if o == "-v":
            volumes = arg.split(",")
            #print volumes
        elif o == "-d":
            vol_desc = arg
        elif o == "-m":
            mount_dir = arg
            #print 'Mount Directory: ' + mount_dir
        elif o in ("-h", "--help"):
            assert False, "Opps unhandled option"

    if mount_dir != None and len(volumes)>0:
        print "Backing Up mount directory and XFS volume"
        # do backup here.
        snapshot_volumes(mount_dir, volumes,vol_desc)

if __name__ == "__main__":

Leave a Reply

Your email address will not be published. Required fields are marked *