add function to automatically set the amazon aws volume size depending on the filename size
authorPaul Tonelli <paul.tonelli@logilab.fr>
Mon, 22 Apr 2013 10:13:42 +0200
changeset 126 fc1f2d232ad5
parent 125 3c64efeb3ee4
child 127 c1280b12b455
add function to automatically set the amazon aws volume size depending on the filename size
_modules/aws_client.py
--- a/_modules/aws_client.py	Fri Apr 19 13:59:30 2013 +0200
+++ b/_modules/aws_client.py	Mon Apr 22 10:13:42 2013 +0200
@@ -7,6 +7,7 @@
 '''
 
 from time import sleep
+from math import ceil
 import os
 import logging
 
@@ -14,7 +15,8 @@
 
 # Import third party libs
 def create_ec2_volume(size, availability_zone, region, cert_path, pk_path):
-    ec2_create_output = __salt__['cmd.run']("ec2-create-volume --size 1 --availability-zone " + availability_zone + " --region " + region +  " -K " + pk_path + " -C " + cert_path)
+    ec2_create_output = __salt__['cmd.run']("ec2-create-volume --size " +
+            str(size) + " --availability-zone " + availability_zone + " --region " + region +  " -K " + pk_path + " -C " + cert_path)
     for bloc in ec2_create_output.split('\t'):
         if bloc.find('vol-') != -1:
             volume_id = bloc
@@ -45,12 +47,12 @@
         ami_name,
         region="eu-west-1",
         cert_path="/tmp/certificate.pem",
-        pk_path="/tmp/private-key.pem",
-        size=1):
+        pk_path="/tmp/private-key.pem"):
     '''
     upload .img file to amazon aws using the ec2 interface
     performs the following steps:
-        - create and attach volume
+        - create and attach volume (calculate size depending on filename size
+          in Gb)
         - dd file to volume
         - detach volume
         - create snapshot
@@ -60,6 +62,7 @@
     '''
     availability_zone = __salt__['cmd.run']("ec2metadata|grep availability-zone|awk '{print $2}'")
     instance_id = __salt__['cmd.run']("ec2metadata|grep instance-id|awk '{print $2}'")
+    size = int(ceil(os.path.getsize(filename)/(1024.**3)))
     volume_id = create_ec2_volume(size, availability_zone, region, cert_path, pk_path)
     log.info(__salt__['cmd.run']("ec2-attach-volume " + volume_id + " --instance " +
             instance_id + " --device /dev/sdf --region " + region + " -K " +