Balance the advance, first make a prototype it.
#!/usr/bin/env python#-*-coding:utf-8-*-ImportRequestsImportJSON#Define the Management node IP, port number, API version, service name, service URL of docker swarm#in late integration into automated deployments, data structures need to be streamlined, data is refined, and accurate judgment and space reclamation are added#API For more use reference: https://docs.docker.com/engine/api/v1.29/Docker_swarm_ip_port ="Ip:port"docker_swarm_api_version="v1.29"Docker_swarm_service_name="TOMCATBV"Get_service_url="http://%s/%s/services/%s"%(Docker_swarm_ip_port, docker_swarm_api_version, docker_swarm_service_name) data= { "Name":"TOMCATBV", "tasktemplate": { "Containerspec": { "Image":"Harbor_op/project_name/tomcat:20170620demo", }, "Placement": {}, "Restartpolicy": { "Condition":"on-failure", "Delay": 10000000000, "maxattempts": 10 } }, "Mode": { "Replicated": { "Replicas": 1 } }, "Updateconfig": { "Parallelism": 2, "failureaction":"Pause", }, "Rollbackconfig": { "Parallelism": 1, }, "Labels": { "Foo":"Bar" }}#Create Docker swarm service Servicesdefcreate_service (): URL="http://%s/%s/services/create"%(Docker_swarm_ip_port, docker_swarm_api_version) Data_json=json.dumps (data) R= Requests.post (URL, data=Data_json)PrintR.textifR.status_code = = 201: Print "created OK" Else: Print "Create Error"sys.exit ()#Update Docker swarm service ServicesdefUpdate_service (version_index): URL="http://%s/%s/services/%s/update?version=%s"%(Docker_swarm_ip_port, Docker_swarm_api_version, Docker_swarm_service_name, Version_index) Data_json=json.dumps (data) R= Requests.post (URL, data=Data_json)PrintVersion_index, R.text, R.status_codeifR.status_code = = 200: Print "updated OK" Else: Print "Update Error"sys.exit ()defMain (): Get_service_url="http://%s/%s/services/%s"%(Docker_swarm_ip_port, Docker_swarm_api_version, docker_swarm_service_name) R=requests.get (Get_service_url)PrintGet_service_url, R.status_code, R.textifR.status_code! = 200: Create_service ()Else: Version_index= eval (r.text) ["Version"]["Index"] Update_service (version_index)if __name__=="__main__": Main ()
How to invoke the Docker Swarm service API to create and update Services