Search code examples
linuxshellazure-devopsansibleartifactory

Application restart automation on Linux servers based on condition


We have a requirement to start our JFrog tools services hosted on Linux machines given certain conditions as noted below.

We have JFrog Artifactory and JFrog Xray hosted on separate servers. Both of these applications have databases hosted in a PostgreSQL database server.

We need to ensure that the database server is up and accessible before the two JFrog applications start. The order of operations needs to be that the database server comes online and is accessible, then the Artifactory service needs to be started, and once Artifactory is up, we need to have the Xray service started.

We have tried using a shell script along with an Azure DevOps pipeline, but we got blocked as we cant bring our JFrog production servers up. The Azure DevOps agents are not properly checking the status of the services and running the necessary startup scripts.

We use Ansible Tower in our organization. The question, is this something that Ansible playbooks would handle nicely, or should we be looking at other approaches?


Solution

  • ... start our ... services hosted ... on certain conditions ...

    ... if the DB server is up and the DB is accessible from the App servers ... the Artifactory service need to be started and once Artifactory is up, Xray service need to be started ...

    ... is this something that Ansible playbooks would handle nicely ...

    Yes, of course, this can simply be achieved by


    For example

    - hosts: artifactory
      become: false
      gather_facts: false
    
      tasks:
    
      - name: Test connection
        wait_for:
          host: postgresql.example.com
          port: 5432
          state: drained         # Port should be open
          delay: 0               # No wait before first check (sec)
          timeout: 3             # Stop checking after timeout (sec)
          active_connection_states: SYN_RECV
    
      - debug:
          msg: "Several other tasks, in example starting Artifactory"
    
    - hosts: xray
      become: false
      gather_facts: false
    
      tasks:
    
      - name: Test REST API connection
        URI:
          url: https://repository.example.com/artifactory/system/ping
          method: GET
          status_code: 200
          return_content: true
        register: result
        until: result.status == 200 and result.content == 'OK'
        retries: 10              # Retry n times
        delay: 30                # Pause between each call (sec)
    
      - debug:
          msg: "Several other tasks ..."
    

    It is recommended to have a look at all examples of wait_for_http and define before which state, endpoint and result of JFrog Artifactory REST API is considered as GOOD/STARTED/UP. So, in example, one could use the endpoint system/ping which just returns an OK for a How to use Artifactory Health Check.