added a new state module with the possibility to move a file locally
authorPaul Tonelli <paul.tonelli@logilab.fr>
Tue, 05 Mar 2013 11:27:38 +0100
changeset 15 9256bbfc10d6
parent 14 6c75dfac2047
child 16 5cb811380247
added a new state module with the possibility to move a file locally
_states/tmp_func.py
ami_creator.pre.sls
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/_states/tmp_func.py	Tue Mar 05 11:27:38 2013 +0100
@@ -0,0 +1,36 @@
+import salt.utils
+import os.path
+
+def local_managed(name, source):
+    ret = {'name': name,
+            'changes': {},
+            'result': True,
+            'comment':  ''}
+    if __opts__['test']:
+        ret['result'] = None
+        ret['comment'] = 'Local_copyfile will copy {0} to {1} if they are different'.format(source, name)
+        return ret
+    if os.path.isdir(name):
+        ret['result'] = False
+        ret['comment'] = "destination is a directory"
+    if os.path.isdir(name) or os.path.isdir(source):
+        ret['result'] = False
+        ret['comment'] = "Source is a directory"
+    if not os.path.exists(source):
+        ret['result'] = False
+        ret['comment'] = "Source does not exist"
+    stats_source = __salt__['file.stats'](source, 'md5')
+    if ret['result'] and os.path.exists(name):
+        stats_destination = __salt__['file.stats'](name, 'md5')
+    else:
+        stats_destination = {'sum':''}
+    if ret['result'] and stats_source['sum'] != stats_destination['sum']:
+        try:
+            salt.utils.copyfile(source, name)
+            ret['changes']['copy'] = 'copied {0} to {1} with md5sum = {2}'.format(source, name, stats_source['sum'])
+        except IOError:
+            ret['comment'] = 'IOError'
+            ret['result'] = False
+    return ret
+
+
--- a/ami_creator.pre.sls	Thu Mar 21 14:43:41 2013 +0100
+++ b/ami_creator.pre.sls	Tue Mar 05 11:27:38 2013 +0100
@@ -17,7 +17,7 @@
         - target: /etc/salt/srv
 
 
-hg_img_available:
+#hg_img_available:
 
 ### Mounts ###
 mounted_variation:
@@ -47,7 +47,9 @@
         - require:
             - mount.mounted: mounted_variation
 
-proxy_file:
+
+### Temporary Files ###
+01proxy:
     file.managed:
         - name: /mnt/variation/etc/apt/apt.conf.d/01proxy
         - source: salt://others/01proxy
@@ -56,7 +58,7 @@
             - mount.mounted: mounted_variation
 
 
-policy:
+policy-rc.d:
     file.managed:
         - name: /mnt/variation/usr/sbin/policy-rc.d
         - source: salt://others/policy-rc.d
@@ -64,4 +66,9 @@
         - require:
             - mount.mounted: mounted_variation
 
-
+resolv.conf:
+    tmp_func.local_managed:
+        - name: /mnt/variation/etc/resolv.conf
+        - source: /etc/resolv.conf
+        - require:
+            - mount.mounted: mounted_variation