Application restart automation on Linux servers based on condition

huangapple go评论72阅读模式
英文:

Application restart automation on Linux servers based on condition

问题

我们有一个要求,在给定以下条件的情况下启动托管在Linux机器上的JFrog工具服务。

我们的JFrog Artifactory和JFrog Xray托管在不同的服务器上。这两个应用程序的数据库托管在一个PostgreSQL数据库服务器上。

我们需要确保在两个JFrog应用程序启动之前,数据库服务器处于在线和可访问状态。操作顺序应该是数据库服务器上线并可访问,然后启动Artifactory服务,一旦Artifactory启动,我们需要启动Xray服务。

我们尝试使用一个Shell脚本和Azure DevOps管道,但由于我们无法启动JFrog生产服务器,所以被阻止了。Azure DevOps代理未正确检查服务状态并运行必要的启动脚本。

我们在组织中使用Ansible Tower。问题是,Ansible Playbooks是否能很好地处理这个问题,还是我们应该考虑其他方法?

英文:

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?

答案1

得分: 2

Sure, here's the translated content:

> ...开始我们的...服务托管...在某些条件下...
>
> ...如果DB服务器正常运行并且从应用服务器可以访问DB...则需要启动Artifactory服务,一旦Artifactory正常运行,需要启动Xray服务...
>
> ...这是否是Ansible playbook可以很好处理的事情...

是的,当然可以通过以下方式简单实现:


例如

- hosts: artifactory
  become: false
  gather_facts: false

  tasks:

  - name: 测试连接
    wait_for:
      host: postgresql.example.com
      port: 5432
      state: drained         # 端口应该是打开的
      delay: 0               # 第一次检查之前没有等待时间(秒)
      timeout: 3             # 超时后停止检查(秒)
      active_connection_states: SYN_RECV

  - debug:
      msg: "其他任务,例如启动Artifactory"

- hosts: xray
  become: false
  gather_facts: false

  tasks:

  - name: 测试REST API连接
    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              # 重试n次
    delay: 30                # 每次调用之间的暂停时间(秒)

  - debug:
      msg: "其他任务 ..."

建议查看所有wait_for_http的示例,并定义在何种状态、端点和JFrog Artifactory REST API的结果被视为GOOD/STARTED/UP。例如,可以使用system/ping端点,它只返回OK作为如何使用Artifactory健康检查的结果。

英文:

> ... 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.

答案2

得分: 1

你是否已经查看了位于 https://jfrog.com/help/r/jfrog-installation-setup-documentation/enable-postgresql-connectivity-from-remote-servers 的文档?

如果您使用脚本路由,可以使用类似 pg_isready -h someremotehost 的命令来确定数据库服务器的状态(详见 https://www.postgresql.org/docs/current/app-pg-isready.html 了解更多信息)。

要确定Artifactory何时可用,您可以使用curl访问端点(详见 https://jfrog.com/knowledge-base/artifactory-how-to-use-artifactory-health-check/ 了解更多信息)。

这里是提供的一个示例:

curl -uadminuser:Password  -XGET http://localhost:8081/artifactory/api/system/ping -H 'Content-Type: text/plain';

输出响应:OK

https://stackoverflow.com/questions/35069027/docker-wait-for-postgresql-to-be-running 这个主题中有一些可能有帮助的提示。

英文:

Have you reviewed the documentation at https://jfrog.com/help/r/jfrog-installation-setup-documentation/enable-postgresql-connectivity-from-remote-servers?

If you use the script route, you can use something like pg_isready -h someremotehost to determine status of the db server (see https://www.postgresql.org/docs/current/app-pg-isready.html for more).

To determine when artifactory is up you could curl the endpoint (see https://jfrog.com/knowledge-base/artifactory-how-to-use-artifactory-health-check/ for more.)

Here is one of the provided examples:

curl -uadminuser:Password  -XGET http://localhost:8081/artifactory/api/system/ping -H 'Content-Type: text/plain'

Output Response : OK

There are some potentially helpful hints in this thread https://stackoverflow.com/questions/35069027/docker-wait-for-postgresql-to-be-running

huangapple
  • 本文由 发表于 2023年5月11日 04:42:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定