- name: 概要信息
debug:
msg:
- "应用名: {{ application_name }}"
- "工作空间: {{ workspace_path }}"
- "服务名: {{ service_name }}"
- "是否重新创建服务: {{ is_recreate_service }}"
- "服务副本数量: {{ replicas }}"
- "镜像名: {{ image_name }}"
- "激活的Profiles: {{ active_profiles }}"
- "Swarm 网络名称: {{ lvdoo_network }}"
- name: 登录到仓库,准备拉取镜像
shell: "docker login -u root -p 123456 {{ hub_host }}"
- name: 拉取镜像
shell: "docker pull {{ image_name }}"
ignore_errors: yes
- name: 移除正在运行的Swarm服务
shell: "docker service rm {{ service_name }}"
when: is_recreate_service
ignore_errors: yes
- name: 检查Swarm此前是否已经创建该服务
shell: "docker service ls"
register: "docker_service_check_output"
- name: 创建新的Swarm服务 | 如果当前服务尚未创建
shell: "docker service create --name {{ service_name }} --network {{ lvdoo_network }}
--replicas {{ replicas }} --update-failure-action rollback --restart-max-attempts 3
--update-parallelism 1 --update-delay 10s --update-monitor 10s -e
'SPRING_PROFILES_ACTIVE={{ active_profiles }}' -e JAVA_OPTS='-Xmx512m'
--with-registry-auth {{ image_name }}"
when: 'service_name not in docker_service_check_output.stdout'
- name: 暴露端口 | 如果当前服务需要暴露指定端口
shell: "docker service update --publish-add published={{ application_exposed_port }},target={{ application_startup_port }} {{ service_name }} "
when: not( (application_exposed_port is undefined) or (application_exposed_port is none) or (application_exposed_port | trim == '') )
- name: 开启XXL-Job端口 | 如果当前服务指定了xxl-job端口
shell: "docker service update --publish-add published={{ xxl_port_to_expose }},target={{ xxl_port_in_container }} {{ service_name }} "
when: xxl_port_to_expose is defined
- name: 更新Swarm服务 | 如果当前服务此前已创建
shell: "docker service update {{ service_name }} --image {{ image_name }} --update-delay 10s"
when: 'service_name in docker_service_check_output.stdout'
- name: 暂停10秒,等待服务启动并准备收集日志
pause:
seconds: 10
- name: 检查服务启动状态
shell: "docker service ps {{ service_name }} -f 'desired-state=running' | grep -v ID "
register: "docker_swarm_service_status"
- name: 当前服务状态
debug:
msg:
- "{{ docker_swarm_service_status.stdout | to_yaml }}"
- name: 检查服务是否已经启动
fail:
msg: "服务未启动成功,请检查。"
when: docker_swarm_service_status.stdout == ""