So here are the files as I have them:
fabfiley.py
from fabric.api import *import boto, os, time, crt
key_path= ''my keypath"
instance = crt.crtinst('ami-3c47a355')
env.hosts=[instance.dns_name]
print instance.dns_name
env.user='root'
env.key_filename=key_path
def testvol():
conn = boto.connect_ec2()
try:
fo = open('volume10.txt', 'a')
print ('Instance %s has been created'%instance.id)
#Create Volume
print 'Creating Volume'
start_real_effective= start_real= time.time()
vol = conn.create_volume(10, instance.placement)
end_real=time.time()
while not vol.volume_state()=='available':
time.sleep(1)
vol.update()
end_real_effective=time.time()
fo.write("\nCall Create Volume: %f Real Seconds" % (end_real -
start_real))
fo.write("\nEffective Create Volume: %f Real Seconds" %
(end_real_effective - start_real_effective))
#Describe Volumes
print '--------Current Volumes------'
start_real=time.time()
print conn.get_all_volumes()
end_real=time.time()
fo.write("\nCall Get Volumes: %f Real Seconds" % (end_real -
start_real))
print 'Current status of the volume:'
print vol.id,':',vol.volume_state()
#Attach Volume
print '----------Now the volume is ready to go---------'
start_real_effective=start_real=time.time()
if vol.attach(instance.id, '/dev/sdg'):
print '\nVolume %s has been successfully attached to %s'%(vol.id,
instance.id)
end_real=time.time()
while vol.volume_state()!='in-use':
time.sleep(1)
vol.update()
end_real_effective=time.time()
fo.write("\nCall Volume Attachment: %f Real Seconds" % (end_real -
start_real))
fo.write("\nEffective Volume Attachment: %f Real Seconds" %
(end_real_effective - start_real_effective))
#Get attachment state
start_real_effective=start_real=time.time()
print 'Current Attachment state:', vol.attachment_state()
end_real=time.time()
while vol.attachment_state()!='attached':
time.sleep(1)
vol.update()
end_real_effective = time.time()
fo.write("\nCall Volume Attachment State: %f Real Seconds" % (end_real
- start_real))
fo.write("\nEffective Volume Attachment State: %f Real Seconds" %
(end_real_effective - start_real_effective))
info()
#Make Volume Usable
start_real= time.time()
mount_ebs_volue()
end_real = time.time()
fo.write('\nMake Volume Usable: %f Real Seconds'%(end_real-start_real))
run('df -h')
#Reboot instance
'''start_real_effective= start_real=time.time()
instance.reboot()
end_real=time.time()
while crt.ping(instance)!=True:
time.sleep(1)
print "Instance Rebooted, check to see if volume still mounted"
run('df -h')'''
#unmount volume
run('umount /mnt/volume')
run('df -h')
#Create a snapshot of the volume
start_real_effective=start_real=time.time()
if vol.create_snapshot('mysnapshot'):
print 'Just created a snapshot of the volume %s' % vol.id
end_real=time.time()
while vol.snapshots()[0].status!='completed':
time.sleep(1)
vol.snapshots()[0].update()
end_real_effective= time.time()
fo.write("\nCall Create Snapshot: %f Real Seconds" % (end_real -
start_real))
fo.write("\nEffective Create Snapshot: %f Real
Seconds"%(end_real_effective-start_real_effective))
#detach volume
print "-----Now let's detach the volume from the instance-----"
start_real_effective=start_real= time.time()
vol.detach()
end_real=time.time()
while vol.volume_state()!='available':
time.sleep(1)
vol.update()
end_real_effective=time.time()
fo.write("\nCall Detach Volume: %f Real Seconds" % (end_real -
start_real))
fo.write("\nEffective Detach Volume: %f Real Seconds" %
(end_real_effective - start_real_effective))
#Get attachment state
print 'Current Attachment state:', vol.attachment_state()
while vol.attachment_state()=='attached':
time.sleep(1)
vol.update()
snaps = vol.snapshots()[0]
#Delete Volume
print "--------Now let's delete the volume-----"
start_real_effective=start_real= time.time()
vol.delete()
end_real=time.time()
while vol.id in conn.get_all_volumes():
time.sleep(1)
end_real_effective=time.time()
fo.write("\nCall Delete Volume: %f Real Seconds" % (end_real -
start_real))
fo.write("\nEffective Delete Volume: %f Real Seconds" %
(end_real_effective - start_real_effective))
#Delete Snapshot
print "------------Let's delete the snapshot-----------"
start_real_effective = start_real = time.time()
snaps.delete()
end_real = time.time()
'''while snaps.status=='completed':
time.sleep(1)
snaps.update()
end_real_effective=time.time()'''
fo.write("\nCall Delete Snapshot: %f Real Seconds" % (end_real -
start_real))
#fo.write("\nEffective Delete Snapshot: %f Real Seconds" %
(end_real_effective - start_real_effective))
except IOError:
print "Error: can't find file or read data"
finally:
instance.terminate()
while not instance.state == 'terminated':
time.sleep(1)
instance.update()
fo.close()def mount_ebs_volue(): sudo('yes | mkfs -t ext3 /dev/sdg')
sudo('mkdir /mnt/volume')
sudo('echo "/dev/sdg /mnt/volume ext3 user 0 0" >> /etc/fstab')
sudo('mount /dev/sdg /mnt/volume')def info(): run('ls -al')
run('df -h')
def main():
testvol()
if __name__ == '__main__':
main()
So the stuff highlighted in yellow is where I setup my env variables. It first
creates an instance, and then in env.hosts, it adds the instance address to the
list. It is first called when I call the info() function. If I run it
directly from the command line it all works great. But when I do it from the
cron job, this is the output I get:
ec2-184-73-45-84.compute-1.amazonaws.com
Instance i-5b4a0f3a has been created
Creating Volume
--------Current Volumes------
[Volume:vol-7a17d610]
Current status of the volume:
vol-7a17d610 : available
----------Now the volume is ready to go---------
Volume vol-7a17d610 has been successfully attached to i-5b4a0f3a
Current Attachment state: attaching
No hosts found. Please specify (single) host string for connection:
As you can see, it prompts me for a host address connection. After getting no
response the program just shuts down right at this point.
cron job file
alex@alex-VirtualBox:~$ crontab -l
*/10 12-14 11 8 * /usr/bin/python /home/alex/fabfile.py >> output.log
Not sure where to go from here._______________________________________________
Fab-user mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/fab-user