英文:
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可以很好处理的事情...
是的,当然可以通过以下方式简单实现:
-
> 具有多个“操作”的Playbook可以编排多台机器的部署,在您的Web服务器上运行一个操作,然后在数据库服务器上运行另一个操作,然后在网络基础设施上运行第三个操作,依此类推。
-
并使用
wait_for
模块 - 在继续之前等待条件等待与PostgreSQL数据库连接,以及uri
模块 - 与Web服务交互用于wait_for_http
等待JFrog Artifactory REST API的连接。
例如
- 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
-
Using Inventory basics: formats, hosts, and groups, Organizing the inventory accordingly and group by
-
Using Playbook execution
> Playbooks with multiple ‘plays’ can orchestrate multi-machine deployments, running one play on your webservers, then another play on your database servers, then a third play on your network infrastructure, and so on.
-
And using
wait_for
module – Waits for a condition before continuing for the connection to the the PostgreSQL Database Backed anduri
module – Interacts with webservices towait_for_http
for the connection to the JFrog Artifactory REST API
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论