begin convertion to salt states:
authorPaul Tonelli <paul.tonelli@logilab.fr>
Thu, 21 Mar 2013 14:43:41 +0100
changeset 14 6c75dfac2047
parent 13 5a3437449dc8
child 15 9256bbfc10d6
begin convertion to salt states: - added additionnal sls files for pre and post states (before and after flavorisation) - rename sls and minion files for ami_creator / minion
_modules/vm_create.py
ami-creator.minion
ami-creator.post.sls
ami_creator.pre.sls
ami_creator.top.sls
bootstrap-script.sh
init-server.sls
minion.patch
top.sls
variation.minion
variation.sls
variation.top.sls
--- a/_modules/vm_create.py	Tue Mar 05 15:16:27 2013 +0100
+++ b/_modules/vm_create.py	Thu Mar 21 14:43:41 2013 +0100
@@ -10,7 +10,8 @@
 APT_PROXY_FILENAME = '/etc/apt/apt.conf.d/01proxy'
 POLICY_FILENAME = '/usr/sbin/policy-rc.d'
 POLICY_CONTENT =  'exit 101'
-DEFAULT_VARIATION_NAME = 'init-client.sls'
+DEFAULT_VARIATION_NAME = 'variation.sls'
+DEFAULT_MOUNT_DIR = 'variation'
 
 from os import listdir, remove, stat, environ
 import salt.utils
@@ -101,9 +102,10 @@
             + ' --wildcards -C ' + path + ' "*.img" ')
     images = _get_images(path)
     assert len(images) == 1
+    __salt__['file.find']('/mnt',iname='*.img')
     image_name = images[0]
     resize_and_check(path, image_name, new_size * 1024)
-    image_dirname = image_name.replace('.img','')
+    image_dirname = DEFAULT_MOUNT_DIR
     log.info(__salt__['file.makedirs'](path + '/' + image_dirname + '/'))
     log.info(__salt__['mount.mount'](path + '/' + image_dirname, path + '/'
             + image_name, opts='loop'))
@@ -111,9 +113,9 @@
             fstype='proc'))
     log.info(__salt__['mount.mount'](path + '/' + image_dirname + '/dev/pts',
             'none', fstype='devpts'))
-    return image_dirname, image_name
+    return image_name
 
-def update_image(chroot_dirname, create_proxy=True, create_policy=True):
+def update_image(chroot_dirname):
     '''
     update image using apt-get. Doing so, it creates a few files.
     In order to be executed in chroot, you should use is with
@@ -124,11 +126,8 @@
         salt '*' vm_create.resize_and_check /mnt raring_image.tar.gz
     '''
     files_to_delete = []
-    if bool(create_proxy):
-        _create_apt_proxy(chroot_dirname
-            + '/etc/apt/apt.conf.d/01proxy')
-    if bool(create_policy):
-        _create_policy_ubuntu(chroot_dirname)
+    _create_apt_proxy(chroot_dirname + '/etc/apt/apt.conf.d/01proxy')
+    _create_policy_ubuntu(chroot_dirname)
     log.info(__salt__['file.remove'](chroot_dirname + "/etc/resolv.conf"))
     ################ bug here ####################"
     print '/etc/resolv.conf' +", "+ chroot_dirname + '/etc'
@@ -146,7 +145,7 @@
 
     salt.utils.copyfile('/etc/salt/srv/' + variation_filename, chroot_dirname + '/etc/salt/srv')
     __salt__['file.patch'](chroot_dirname + '/etc/salt/minion', '/etc/salt/srv/minion.patch')
-#    log.info(_chroot_exec(chroot_dirname, 'salt-call  --local state.highstate'))
+    log.info(_chroot_exec(chroot_dirname, 'salt-call  --local state.highstate'))
 
 
 def get_initrd_kernel(path, image_dirname):
@@ -178,7 +177,8 @@
 
         salt '*' vm_create.create_variation raring_image.tar.gz vanilla
     '''
-    image_dirname, image_name =  mount_image(path, archive_name)
+    image_name =  mount_image(path, archive_name)
+    image_dirname = DEFAULT_MOUNT_DIR
     update_image(path + '/' + image_dirname)
     variate_image(path + '/' + image_dirname, variation_filename)
     kernel_name, ramdisk_name = get_initrd_kernel(path, image_dirname)
@@ -194,12 +194,12 @@
         id_image = __salt__['openstack_client.upload_to_glance'](
                 path, image_name, 'ami', 'kernel_id='
                 + id_kernel + ' ramdisk_id=' + id_ramdisk)
+        log.info("Reverting to initial state")
+        revert_all(path, (kernel_name, ramdisk_name))
     else:
         log.info('Not uploading to glance')
-    log.info("kernel name " + kernel_name)
-    log.info("ramdisk name " + ramdisk_name)
-    log.info("Reverting to initial state")
-    revert_all(path, (kernel_name, ramdisk_name))
+        log.info("kernel name " + kernel_name)
+        log.info("ramdisk name " + ramdisk_name)
     return True
 
 def _get_images(path):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ami-creator.minion	Thu Mar 21 14:43:41 2013 +0100
@@ -0,0 +1,34 @@
+# top file to execute if startup_states is 'top'
+#top_file: ''
+
+#####     File Directory Settings    #####
+##########################################
+# The Salt Minion can redirect all file server operations to a local directory,
+# this allows for the same state tree that is on the master to be used if
+# copied completely onto the minion. This is a literal copy of the settings on
+# the master but used to reference a local directory on the minion.
+
+# Set the file client, the client defaults to looking on the master server for
+# files, but can be directed to look at the local file directory setting
+# defined below by setting it to local.
+file_client: local
+
+# The file directory works on environments passed to the minion, each environment
+# can have multiple root directories, the subdirectories in the multiple file
+# roots cannot match, otherwise the downloaded files will not be able to be
+# reliably ensured. A base environment is required to house the top file.
+
+file_roots:
+  base:
+    - /etc/salt/srv
+
+#####  keystone / glance params   ######
+########################################
+
+keystone.user: username
+keystone.password: password 
+keystone.tenant: tenand
+keystone.tenant_id: 00000000000000000000000000000000
+keystone.auth_url: 'http://control.example.com:5000/v2.0'
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ami-creator.post.sls	Thu Mar 21 14:43:41 2013 +0100
@@ -0,0 +1,4 @@
+# /srv/salt/init_machine.sls
+#
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ami_creator.pre.sls	Thu Mar 21 14:43:41 2013 +0100
@@ -0,0 +1,67 @@
+# /srv/salt/init_machine.sls
+#
+
+base_pkgs:
+    pkg.installed:
+        - pkgs:
+            - salt-minion
+            - python
+            - mercurial
+            - tar
+            - glance
+#          - eca2ools
+
+hg_ami_creation:
+    hg.latest:
+        - name: http://hg.logilab.fr/users/ptonelli/srv_salt
+        - target: /etc/salt/srv
+
+
+hg_img_available:
+
+### Mounts ###
+mounted_variation:
+    mount.mounted:
+        - device: /mnt/variation.img
+        - name: /mnt/variation
+        - fstype: ext4
+        - mkmnt: False
+        - opts:
+            - loop
+
+chroot_proc:
+    mount.mounted:
+        - device: none
+        - name: /mnt/variation/proc
+        - fstype: proc
+        - mkmnt: False
+        - require:
+            - mount.mounted: mounted_variation
+
+chroot_devpts:
+    mount.mounted:
+        - device: none
+        - name: /mnt/variation/dev/pts
+        - fstype: devpts
+        - mkmnt: False
+        - require:
+            - mount.mounted: mounted_variation
+
+proxy_file:
+    file.managed:
+        - name: /mnt/variation/etc/apt/apt.conf.d/01proxy
+        - source: salt://others/01proxy
+        - mode: 644
+        - require:
+            - mount.mounted: mounted_variation
+
+
+policy:
+    file.managed:
+        - name: /mnt/variation/usr/sbin/policy-rc.d
+        - source: salt://others/policy-rc.d
+        - mode: 755
+        - require:
+            - mount.mounted: mounted_variation
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ami_creator.top.sls	Thu Mar 21 14:43:41 2013 +0100
@@ -0,0 +1,4 @@
+# /srv/salt/top.sls
+base:
+  '*':
+    - init-server
--- a/bootstrap-script.sh	Tue Mar 05 15:16:27 2013 +0100
+++ b/bootstrap-script.sh	Thu Mar 21 14:43:41 2013 +0100
@@ -15,6 +15,6 @@
 aptitude install salt-minion
 patch /etc/salt/minion minion.patch
 mkdir /etc/salt/srv
-cp -r _modules _states init-server.sls top.sls /etc/salt/srv 
+cp -r _modules _states ami-creator.sls top.sls /etc/salt/srv 
 salt-call state.highstate
 salt-call vm_create.create_flavor $image_name gna
--- a/init-server.sls	Tue Mar 05 15:16:27 2013 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,19 +0,0 @@
-# /srv/salt/init_machine.sls
-my_pkgs:
-  pkg.installed:
-    - pkgs:
-      - salt-minion
-      - python
-      - mercurial
-      - tar
-      - glance
-#      - eca2ools
-
-my_flavors:
-  flavors.created:
-    - value: gna
-
-http://hg.logilab.fr/users/ptonelli/srv_salt:
-   hg.latest:
-     - target: /mnt/srv
-
--- a/minion.patch	Tue Mar 05 15:16:27 2013 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,12 +0,0 @@
-226c226
-< #file_client: remote
----
-> file_client: local
-244,246c244,246
-< #file_roots:
-< #  base:
-< #    - /srv/salt
----
-> file_roots:
->   base:
->     - /etc/salt/srv
--- a/top.sls	Tue Mar 05 15:16:27 2013 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,4 +0,0 @@
-# /srv/salt/top.sls
-base:
-  '*':
-    - init-server
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/variation.minion	Thu Mar 21 14:43:41 2013 +0100
@@ -0,0 +1,25 @@
+# top file to execute if startup_states is 'top'
+#top_file: ''
+
+#####     File Directory Settings    #####
+##########################################
+# The Salt Minion can redirect all file server operations to a local directory,
+# this allows for the same state tree that is on the master to be used if
+# copied completely onto the minion. This is a literal copy of the settings on
+# the master but used to reference a local directory on the minion.
+
+# Set the file client, the client defaults to looking on the master server for
+# files, but can be directed to look at the local file directory setting
+# defined below by setting it to local.
+file_client: local
+
+# The file directory works on environments passed to the minion, each environment
+# can have multiple root directories, the subdirectories in the multiple file
+# roots cannot match, otherwise the downloaded files will not be able to be
+# reliably ensured. A base environment is required to house the top file.
+
+file_roots:
+  base:
+    - /etc/salt/srv
+
+
--- a/variation.sls	Tue Mar 05 15:16:27 2013 +0100
+++ b/variation.sls	Thu Mar 21 14:43:41 2013 +0100
@@ -6,8 +6,8 @@
       - python
       - mercurial
 
-http://hg.logilab.fr/users/ptonelli/automatic_ami_creation:
+http://hg.logilab.fr/users/ptonelli/srv_salt:
    hg.latest:
-     - target: 
+     - target: /mnt
 
-salt.states.file.absent(name)
+#salt.states.file.absent(name)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/variation.top.sls	Thu Mar 21 14:43:41 2013 +0100
@@ -0,0 +1,4 @@
+# /srv/salt/top.sls
+base:
+  '*':
+    - variation