code refactoring, begin to move parts of tmp_state resize state to tmp_extfs (module), should work
authorPaul Tonelli <paul.tonelli@logilab.fr>
Wed, 13 Mar 2013 17:22:44 +0100
changeset 73 dae7b424b347
parent 72 a66ccae3aca2
child 74 442b3b154777
code refactoring, begin to move parts of tmp_state resize state to tmp_extfs (module), should work *** corrections
_modules/openstack_client.py
_modules/tmp_extfs.py
_modules/vm_create.py
_states/tmp_state.py
--- a/_modules/openstack_client.py	Wed Mar 13 16:07:36 2013 +0100
+++ b/_modules/openstack_client.py	Wed Mar 13 17:22:44 2013 +0100
@@ -40,7 +40,7 @@
                 environ[key] = id_elements[key]
     return glance_client.get_client(host='0.0.0.0')
 
-def upload_to_glance(filename, disk_format, other_params, image_name=None):
+def upload_to_glance(filename, disk_format, other_params='', image_name=None):
     '''
     upload image to glance, used in vm_create
     '''
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/_modules/tmp_extfs.py	Wed Mar 13 17:22:44 2013 +0100
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+# author: Paul Tonelli <ptonelli@logilab.fr>
+
+import logging
+
+log = logging.getLogger(__name__)
+
+def resize(name, size='', options=''):
+    return __salt__['cmd.run_all']('resize2fs -f '+ options + ' ' + name + ' '
+            + size)
+
+def fsck(name, options=''):
+    return __salt__['cmd.run_all']('e2fsck -n -f ' + name)
+
+def _has_enough_free_space(fs, space):
+    info = os.statvfs(fs)
+    free_space = (info.f_bavail * info.f_frsize)
+    if free_space > space:
+        return True
+    return False
+
--- a/_modules/vm_create.py	Wed Mar 13 16:07:36 2013 +0100
+++ b/_modules/vm_create.py	Wed Mar 13 17:22:44 2013 +0100
@@ -83,7 +83,7 @@
                 object_id[o_type] = __salt__['openstack_client.upload_to_glance'](
                         o_path[0], object_types[o_type], image_name=o_type + '_' + ami_name)
             else:
-                log.error('no ' + object_types[o_type] + ' found in ' + path)
+                log.error('no ' + o_type + ' found in ' + path)
                 ok_for_image = False
     if ok_for_image:
         o_path = _find_objects(path,'.img')
--- a/_states/tmp_state.py	Wed Mar 13 16:07:36 2013 +0100
+++ b/_states/tmp_state.py	Wed Mar 13 17:22:44 2013 +0100
@@ -113,16 +113,17 @@
         ret['result'] = False
         ret['comment'] = '{0} is not a file'.format(name, new_size)
         return ret
-    additionnal_flags = ''
+    str_size = ''
     minimize = ''
-    info = __salt__['cmd.run_all']('e2fsck -n -f ' + name)
+    info = __salt__['tmp_extfs.fsck'](name)
     if info['retcode']:
         ret['result'] = False
         ret['comment'] = 'Error while checking fs {0}: {1}'.format(name, info['stderr'])
         return ret
     change_value = new_size - current_size * block_size
-    minimum_size = int(__salt__['cmd.run_all']('resize2fs -P -f ' + name)['stdout'].split(':')[-1])
-    if abs(change_value) < pow(1024, 2) or (new_size < minimum_size and minimum_size == current_size):
+    minimum_size = int(__salt__['tmp_extfs.resize'](name, options='-P')['stdout'].split(':')[-1])
+    if abs(change_value) < pow(1024, 2) or (new_size < minimum_size and
+            minimum_size == current_size):
         ret['result'] = True
         ret['comment'] = 'Filesystem {0} is already at the correct/smaller size'.format(name)
         return ret
@@ -132,15 +133,16 @@
             ret['comment'] = 'Not enough free space'
             return ret
     if minimum_size < new_size: #change size to what asked
-        additionnal_flags = ' ' + str(new_size / block_size)
+        str_size = ' ' + str(new_size / block_size)
     else: #minimise to min possible
         minimize = '-M '
-    info = __salt__['cmd.run_all']('resize2fs -f ' + minimize + name + additionnal_flags)
+    info = __salt__['tmp_extfs.resize'](name, options=minimize,
+            size=str_size)
     if info['retcode']:
         ret['result'] = False
         ret['comment'] = 'Error while resizing {0}: {1}'.format(name, info['stderr'])
         return ret
-    if additionnal_flags:
+    if str_size:
         info = __salt__['cmd.run_all']('fallocate -l ' + str(new_size /
             block_size) + ' ' + name )
     ret['changes']['resized'] = True