Search code examples
web-applicationsupdatessalt-project

Stop-update-start with saltstack highstate?


Saltstack is responsible for updating web application code (uwsgi, Python, if it matters). The idea is to update the code from an archive. It would be nice to use a single salt command for that - state.highstate.

The problem is, that during update the application code should not be running, so service.stop and service.start are run around the state.highstate.

Is there any way to include this update logic into highstate? Or maybe there is some other command, which is more suitable?


Solution

  • Found in the Mastering SaltStack, 2nd edition by Joseph Hall the following.

    ..., consider a web application that makes use of Apache. When the codebase on a production server changes, Apache should be turned off, so as to avoid errors with the code that has not yet finished being installed.

    The key is to use prereq, here is book's example adapted to my archive case:

    my_service: 
      service: 
        - running
        - name: my_service
        - watch: 
          - cmd: my_archive
    
    my_archive: 
      cmd: 
        - run
        - name: "/bin/tar -zxf my.tar.gz -C {{ code_dir }}"
        - require:
          - file: my.tar.gz
        ... 
    
    shutdown_my_service: 
      service: 
        - dead 
        - name: my_service 
        - prereq: 
          - cmd: my_archive
    

    (I have not checked it exactly as written here, but it's easy to see the idea)