Commit d7713dcb by O'Reilly Media, Inc

Initial commit

parents
K 25
svn:wc:ra_dav:version-url
V 58
/!svn/ver/393746/internal/books/9781449305444/current/code
END
s3_versioning.py
K 25
svn:wc:ra_dav:version-url
V 75
/!svn/ver/393746/internal/books/9781449305444/current/code/s3_versioning.py
END
ec2_tag_instance.py
K 25
svn:wc:ra_dav:version-url
V 78
/!svn/ver/393746/internal/books/9781449305444/current/code/ec2_tag_instance.py
END
ec2_sync_keypair.py
K 25
svn:wc:ra_dav:version-url
V 78
/!svn/ver/393746/internal/books/9781449305444/current/code/ec2_sync_keypair.py
END
s3_mfa_delete.py
K 25
svn:wc:ra_dav:version-url
V 75
/!svn/ver/393746/internal/books/9781449305444/current/code/s3_mfa_delete.py
END
s3_logging.py
K 25
svn:wc:ra_dav:version-url
V 72
/!svn/ver/393746/internal/books/9781449305444/current/code/s3_logging.py
END
s3_reduced_redundancy.py
K 25
svn:wc:ra_dav:version-url
V 83
/!svn/ver/393746/internal/books/9781449305444/current/code/s3_reduced_redundancy.py
END
s3_modify_metadata.py
K 25
svn:wc:ra_dav:version-url
V 80
/!svn/ver/393746/internal/books/9781449305444/current/code/s3_modify_metadata.py
END
s3_store_metadata_with_key.py
K 25
svn:wc:ra_dav:version-url
V 88
/!svn/ver/393746/internal/books/9781449305444/current/code/s3_store_metadata_with_key.py
END
ec2_import_ssh_keypair.py
K 25
svn:wc:ra_dav:version-url
V 84
/!svn/ver/393746/internal/books/9781449305444/current/code/ec2_import_ssh_keypair.py
END
s3_create_bucket.py
K 25
svn:wc:ra_dav:version-url
V 78
/!svn/ver/393746/internal/books/9781449305444/current/code/s3_create_bucket.py
END
ec2_associate_elastic_ip_address.py
K 25
svn:wc:ra_dav:version-url
V 94
/!svn/ver/393746/internal/books/9781449305444/current/code/ec2_associate_elastic_ip_address.py
END
s3_copy_object.py
K 25
svn:wc:ra_dav:version-url
V 76
/!svn/ver/393746/internal/books/9781449305444/current/code/s3_copy_object.py
END
ec2_easy_alarm.py
K 25
svn:wc:ra_dav:version-url
V 76
/!svn/ver/393746/internal/books/9781449305444/current/code/ec2_easy_alarm.py
END
s3_website.py
K 25
svn:wc:ra_dav:version-url
V 72
/!svn/ver/393746/internal/books/9781449305444/current/code/s3_website.py
END
ec2_find_all_running_instances.py
K 25
svn:wc:ra_dav:version-url
V 92
/!svn/ver/393746/internal/books/9781449305444/current/code/ec2_find_all_running_instances.py
END
s3_bucket_du.py
K 25
svn:wc:ra_dav:version-url
V 74
/!svn/ver/393746/internal/books/9781449305444/current/code/s3_bucket_du.py
END
ec2_get_console_output.py
K 25
svn:wc:ra_dav:version-url
V 84
/!svn/ver/393746/internal/books/9781449305444/current/code/ec2_get_console_output.py
END
ec2_custom_script_server.py
K 25
svn:wc:ra_dav:version-url
V 86
/!svn/ver/393746/internal/books/9781449305444/current/code/ec2_custom_script_server.py
END
ec2_custom_script_iam.py
K 25
svn:wc:ra_dav:version-url
V 83
/!svn/ver/393746/internal/books/9781449305444/current/code/ec2_custom_script_iam.py
END
ec2_clone_instance.py
K 25
svn:wc:ra_dav:version-url
V 80
/!svn/ver/393746/internal/books/9781449305444/current/code/ec2_clone_instance.py
END
ec2_blank.py
K 25
svn:wc:ra_dav:version-url
V 71
/!svn/ver/393746/internal/books/9781449305444/current/code/ec2_blank.py
END
s3_create_bucket_in_location.py
K 25
svn:wc:ra_dav:version-url
V 90
/!svn/ver/393746/internal/books/9781449305444/current/code/s3_create_bucket_in_location.py
END
ec2_attach_volume.py
K 25
svn:wc:ra_dav:version-url
V 79
/!svn/ver/393746/internal/books/9781449305444/current/code/ec2_attach_volume.py
END
s3_store_private_data.py
K 25
svn:wc:ra_dav:version-url
V 83
/!svn/ver/393746/internal/books/9781449305444/current/code/s3_store_private_data.py
END
ec2_launch_instance.py
K 25
svn:wc:ra_dav:version-url
V 81
/!svn/ver/393746/internal/books/9781449305444/current/code/ec2_launch_instance.py
END
8
dir
403691
https://prod.oreilly.com/internal/books/9781449305444/current/code
https://prod.oreilly.com
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
svn:special svn:externals svn:needs-lock
116dc4a9-b042-40dd-b87b-4fe04d0a746b
s3_versioning.py
file
2011-10-05T15:30:38.000000Z
fb67e6d79d2b2be3e04d8cf1f9018fe6
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
ec2_tag_instance.py
file
2011-10-05T15:30:38.000000Z
d42789ca53e3e4a08921742b059d42d3
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
ec2_sync_keypair.py
file
2011-10-05T15:30:38.000000Z
62276678f9a45cfde4f07d46577d51fd
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
s3_mfa_delete.py
file
2011-10-05T15:30:38.000000Z
bdbcaaca068411448895bf9f2cff5b41
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
s3_logging.py
file
2011-10-05T15:30:38.000000Z
f6b19bebab8818ba612601edf5ad05b6
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
s3_reduced_redundancy.py
file
2011-10-05T15:30:38.000000Z
e116dae68bdbff9c3b557f5b10cd1402
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
s3_modify_metadata.py
file
2011-10-05T15:30:38.000000Z
677047144d603cd5551f0631ea0fbeb3
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
s3_store_metadata_with_key.py
file
2011-10-05T15:30:38.000000Z
98de1cf521979a6c6b947b73911a997f
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
ec2_import_ssh_keypair.py
file
2011-10-05T15:30:38.000000Z
e7c4d5746a7d81a50da691f519d491a5
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
s3_create_bucket.py
file
2011-10-05T15:30:38.000000Z
78312575531ffbeb80376e96355ea80d
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
ec2_associate_elastic_ip_address.py
file
2011-10-05T15:30:38.000000Z
6cac2763f7428a607fd7e204e8a296b1
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
s3_copy_object.py
file
2011-10-05T15:30:38.000000Z
5ceb9e06301a79341bc4fc7940760adb
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
ec2_easy_alarm.py
file
2011-10-05T15:30:38.000000Z
430aaa4e8ceba863cc2ae702eb5c930d
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
s3_website.py
file
2011-10-05T15:30:38.000000Z
0136d9d78a0931d61c5f6f1d8193c5c4
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
ec2_find_all_running_instances.py
file
2011-10-05T15:30:38.000000Z
2f323388808cf4b568270dd55e7630ac
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
s3_bucket_du.py
file
2011-10-05T15:30:38.000000Z
3dd8dadf52e557bbf3534f49923022e3
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
ec2_get_console_output.py
file
2011-10-05T15:30:38.000000Z
501bc61fa46cad14617ab5a319645b9b
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
ec2_custom_script_server.py
file
2011-10-05T15:30:38.000000Z
ab6f2a37d9c5f8b3245df5ce5a7dc8d2
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
ec2_custom_script_iam.py
file
2011-10-05T15:30:38.000000Z
cecda1fcca6c73d8abbc8b9fcf904f83
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
ec2_clone_instance.py
file
2011-10-05T15:30:38.000000Z
6ce91b02694f2b989fc182e5b7c66710
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
metrics
dir
ec2_blank.py
file
2011-10-05T15:30:38.000000Z
9d44ba1f2e538fb80c98364845c49bdb
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
s3_create_bucket_in_location.py
file
2011-10-05T15:30:38.000000Z
2b50cce273eadc14321f4f9672267369
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
ec2_attach_volume.py
file
2011-10-05T15:30:38.000000Z
39412dcceb413038d58f2bad67a498b1
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
s3_store_private_data.py
file
2011-10-05T15:30:38.000000Z
d07779dec96881de014910a0d40961cf
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
ec2_launch_instance.py
file
2011-10-05T15:30:38.000000Z
5cd000b368415f02e9a7d723096034a4
2011-10-05T15:30:38.563476Z
393746
jhosman@oreilly.com
import boto
ec2 = boto.connect_ec2()
# Let's assume the instance we are interested in has already been started
# in the previous examples and is tagged with "paws". This little
# incantation will retrieve it for us.
instance = ec2.get_all_instances(filters={'paws' : None})[0].instances[0]
# Allocate an Elastic IP Address. This will be associated with your
# account until you explicitly release it.
address = ec2.allocate_address()
# Associate our new Elastic IP Address with our instance.
ec2.associate_address(instance.id, address.public_ip)
# Alternatively, you could do this.
instance.use_ip(address)
import boto
import time
def create_and_attach_volume(instance, volume_size, device_name):
"""
Create a new EBS volume and attach it to the instance.
instance The instance object representing the instance to which
the volume will be attached.
volume_size The size (in GB) of the new volume.
device_name The device name to which the new volume will be
referred to on the instance.
"""
ec2 = instance.connection
# Determine the Availability Zone of the instance
azone = instance.placement
volume = ec2.create_volume(volume_size, azone)
# Wait for the volume to be created.
while volume.status != 'available':
time.sleep(5)
volume.update()
volume.attach(instance.id, device_name)
return volume
import boto
# Blank EC2 recipe example
import boto
from boto.ec2.blockdevicemapping import BlockDeviceMapping, BlockDeviceType
import os
import base64
def clone_instance(instance):
"""
Make an clone of an existing Instance object.
instance The Instance object to clone.
"""
new_bdm = None
ec2 = instance.connection
if instance.block_device_mapping:
root_device_name = instance.get_attribute('rootDeviceName')['rootDeviceName']
user_data = instance.get_attribute('userData')['userData']
# user_data comes back base64 encoded. Need to decode it so it
# can get re-encoded by run_instance !
user_data = base64.b64decode(user_data)
new_bdm = BlockDeviceMapping()
for dev in instance.block_device_mapping:
# if this entry is about the root device, skip it
if dev != root_device_name:
bdt = instance.block_device_mapping[dev]
if bdt.volume_id:
volume = ec2.get_all_volumes([bdt.volume_id])[0]
snaps = volume.snapshots()
if len(snaps) == 0:
print 'No snapshots available for %s' % volume.id
else:
# sort the list of snapshots, newest is at the end now
snaps.sort(key=lambda snap: snap.start_time)
latest_snap = snaps[-1]
new_bdt = BlockDeviceType()
new_bdt.snapshot_id = latest_snap.id
new_bdm[dev] = new_bdt
return ec2.run_instances(instance.image_id,
key_name=instance.key_name,
security_groups=[g.name for g in instance.groups],
user_data=user_data,
instance_type=instance.instance_type,
kernel_id=instance.kernel,
ramdisk_id=instance.ramdisk,
monitoring_enabled=instance.monitored,
placement=instance.placement,
block_device_map=new_bdm).instances[0]
import boto
# Create a restricted user using IAM
# The following JSON policy was generated using the
# AWS Policy Generator app.
# http://awspolicygen.s3.amazonaws.com/policygen.html
policy_json = """{
"Statement": [
{
"Sid": "Stmt1316576423630",
"Action": [
"cloudwatch:PutMetricData"
],
"Effect": "Allow",
"Resource": "*"
}
]
}"""
def create_restricted_user(user_name):
"""
Create a new user in this account. The user will be
restricted by the JSON policy document above.
This function returns a tuple containing the access key
and secret key for the new account.
user_name The name of the new user.
"""
iam = boto.connect_iam()
user = iam.create_user(user_name)
keys = iam.create_access_key(user_name)
response = iam.put_user_policy(user_name,
'CloudWatchPutMetricData',
policy_json)
fp = open('boto.cfg', 'w')
fp.write('[Credentials]\n')
fp.write('aws_access_key_id = %s\n' % keys.access_key_id)
fp.write('aws_secret_access_key = %s\n' % keys.secret_access_key)
fp.close()
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import gzip
import cStringIO
import os
script = """#!/bin/sh
echo "Installing boto..."
cd /root
git clone git://github.com/boto/boto.git
cd boto
python setup.py install
echo "...done"
"""
cloud_config = """
packages:
- python-setuptools
runcmd:
- [ easy_install, boto ]
"""
def create_txt_part(path, subtype, filename=None):
fp = open(path)
s = fp.read()
fp.close()
txt = MIMEText(s, _subtype=subtype)
if filename:
txt.add_header('Content-Disposition',
'attachment', filename=filename)
return txt
def build_userdata(metric_dir):
mp = MIMEMultipart()
# Add our part handler
path = os.path.join(metric_dir, 'metric_part_handler.py')
txt = create_txt_part(path, 'part-handler', 'metric_part_handler.py')
mp.attach(txt)
# Add the boto config file
path = os.path.join(metric_dir, 'boto.cfg')
txt = create_txt_part(path, 'x-config', 'boto.cfg')
mp.attach(txt)
# Add the cloud-config
txt = MIMEText(cloud_config, _subtype='cloud-config')