Skip to content

Commit

Permalink
docker: script to retrieve static docker containers from jenkins (#3423)
Browse files Browse the repository at this point in the history
* docker: python file to retrieve docker static containers from jenkins

* add exceptions and help

---------

Co-authored-by: Martijn Verburg <martijnverburg@gmail.com>
  • Loading branch information
Haroon-Khel and karianna committed Apr 29, 2024
1 parent ee79a02 commit dfd14fe
Show file tree
Hide file tree
Showing 2 changed files with 384 additions and 0 deletions.
295 changes: 295 additions & 0 deletions ansible/DockerInventory.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
[
{
"name": "dockerhost-azure-ubuntu2204-x64-1",
"ip": "52.180.147.157",
"containers": [
{
"nodeName": "test-docker-alpine314-x64-1",
"port": "32769"
},
{
"nodeName": "test-docker-alpine319-x64-4",
"port": "32781"
},
{
"nodeName": "test-docker-fedora39-x64-2",
"port": "32779"
},
{
"nodeName": "test-docker-ubuntu2004-x64-2",
"port": "32768"
},
{
"nodeName": "test-docker-ubuntu2004-x64-5",
"port": "32780"
},
{
"nodeName": "test-docker-ubuntu2204-x64-2",
"port": "32778"
}
],
"containersCount": 6
},
{
"name": "dockerhost-azure-ubuntu2204-x64-2",
"ip": "20.83.24.86",
"containers": [
{
"nodeName": "test-docker-alpine319-x64-2",
"port": "32774"
},
{
"nodeName": "test-docker-alpine319-x64-3",
"port": "32771"
},
{
"nodeName": "test-docker-debian12-x64-3",
"port": "32770"
},
{
"nodeName": "test-docker-debian12-x64-4",
"port": "32773"
},
{
"nodeName": "test-docker-ubuntu2004-x64-4",
"port": "32768"
},
{
"nodeName": "test-docker-ubuntu2204-x64-6",
"port": "32769"
},
{
"nodeName": "test-docker-ubuntu2204-x64-7",
"port": "32772"
}
],
"containersCount": 7
},
{
"name": "dockerhost-equinix-ubuntu2004-armv8-1",
"ip": "147.75.35.203",
"containers": [
{
"nodeName": "test-docker-alpine319-armv8-2",
"port": "32775"
},
{
"nodeName": "test-docker-alpine319-armv8-4",
"port": "32777"
},
{
"nodeName": "test-docker-fedora39-armv8l-1",
"port": "2234"
},
{
"nodeName": "test-docker-sles15-armv8l-1",
"port": "2236"
},
{
"nodeName": "test-docker-ubuntu1804-armv8l-4",
"port": "32774"
},
{
"nodeName": "test-docker-ubuntu2004-armv7l-1",
"port": "2322"
},
{
"nodeName": "test-docker-ubuntu2004-armv7l-2",
"port": "2323"
},
{
"nodeName": "test-docker-ubuntu2004-armv7l-3",
"port": "2332"
},
{
"nodeName": "test-docker-ubuntu2004-armv8l-1",
"port": "32770"
},
{
"nodeName": "test-docker-ubuntu2004-armv8l-2",
"port": "32771"
},
{
"nodeName": "test-docker-ubuntu2004-armv8l-3",
"port": "32772"
},
{
"nodeName": "test-docker-ubuntu2204-armv8l-2",
"port": "32773"
},
{
"nodeName": "test-docker-ubuntu2310-armv8l-1",
"port": "32776"
}
],
"containersCount": 13
},
{
"name": "dockerhost-equinix-ubuntu2204-armv8-1",
"ip": "139.178.86.243",
"containers": [
{
"nodeName": "test-docker-alpine314-armv8-3",
"port": "2214"
},
{
"nodeName": "test-docker-alpine319-armv8-1",
"port": "2232"
},
{
"nodeName": "test-docker-alpine319-armv8-3",
"port": "32772"
},
{
"nodeName": "test-docker-debain12-armv8l-1",
"port": "2231"
},
{
"nodeName": "test-docker-ubuntu2004-armv7l-4",
"port": "2230"
},
{
"nodeName": "test-docker-ubuntu2004-armv7l-5",
"port": "2229"
},
{
"nodeName": "test-docker-ubuntu2004-armv7l-6",
"port": "2227"
},
{
"nodeName": "test-docker-ubuntu2204-armv8-1",
"port": "32769"
},
{
"nodeName": "test-docker-ubuntu2204-armv8-2",
"port": "32770"
},
{
"nodeName": "test-docker-ubuntu2204-armv8-3",
"port": "32771"
}
],
"containersCount": 10
},
{
"name": "dockerhost-marist-ubuntu2204-s390x-1",
"ip": "148.100.74.237",
"containers": [
{
"nodeName": "test-docker-sles12-s390x-1",
"port": "32769"
},
{
"nodeName": "test-docker-sles15-s390x-1",
"port": "32768"
}
],
"containersCount": 2
},
{
"name": "dockerhost-osuosl-ubuntu2004-ppc64le-1",
"ip": "140.211.168.214",
"containers": [
{
"nodeName": "docker-osuosl-ubuntu2004-ppc64le-1",
"port": "22"
},
{
"nodeName": "test-docker-fedora39-ppc64le-1",
"port": "2229"
},
{
"nodeName": "test-docker-ubuntu2004-ppc64le-1",
"port": "2228"
},
{
"nodeName": "test-docker-ubuntu2204-ppc64le-3",
"port": "2230"
}
],
"containersCount": 4
},
{
"name": "dockerhost-osuosl-ubuntu2204-aarch64-1",
"ip": "140.211.167.67",
"containers": [
{
"nodeName": "test-docker-centos8-armv8-1",
"port": "2224"
},
{
"nodeName": "test-docker-ubuntu2204-armv8-4",
"port": "2226"
}
],
"containersCount": 2
},
{
"name": "dockerhost-rise-ubuntu2204-aarch64-1",
"ip": "34.72.108.242",
"containers": [],
"containersCount": 0
},
{
"name": "dockerhost-skytap-ubuntu2004-ppc64le-1",
"ip": "20.61.136.212",
"containers": [
{
"nodeName": "test-docker-debian11-ppc64le-1",
"port": "32768"
},
{
"nodeName": "test-docker-debian11-ppc64le-2",
"port": "32769"
},
{
"nodeName": "test-docker-debian11-ppc64le-3",
"port": "32770"
},
{
"nodeName": "test-docker-debian11-ppc64le-4",
"port": "32773"
},
{
"nodeName": "test-docker-ubuntu2204-ppc64le-1",
"port": "32771"
},
{
"nodeName": "test-docker-ubuntu2204-ppc64le-2",
"port": "32772"
}
],
"containersCount": 6
},
{
"name": "dockerhost-skytap-ubuntu2204-x64-1",
"ip": "20.61.136.254",
"containers": [
{
"nodeName": "test-docker-alpine319-x64-1",
"port": "32787"
},
{
"nodeName": "test-docker-centos7-x64-1",
"port": "32782"
},
{
"nodeName": "test-docker-debian12-x64-1",
"port": "32778"
},
{
"nodeName": "test-docker-fedora39-x64-1",
"port": "32779"
},
{
"nodeName": "test-docker-ubi8-x64-3",
"port": "32790"
},
{
"nodeName": "test-docker-ubuntu2004-x64-3",
"port": "32783"
}
],
"containersCount": 6
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import sys
import json
import jenkins

def getIP(nodeConfig):
find1 = nodeConfig.find("<host>") + 6
find2 = nodeConfig.find("</host>")
if find1 > 5 and find2 > -1:
ip = nodeConfig[find1:find2]
return ip
else:
return "No ip"

def getNodePort(nodeConfig):
find1 = nodeConfig.find("<port>") + 6
find2 = nodeConfig.find("</port>")
if find1 > 5 and find2 > -1:
port = nodeConfig[find1:find2]
return port
else:
return "No port"

def getLabel(nodeConfig):
find1 = nodeConfig.find("<label>") + 7
find2 = nodeConfig.find("</label>")
if find1 > 5 and find2 > -1:
labels = nodeConfig[find1:find2]
return labels
else:
return "No labels"

def createServer(username, password):
url = "http://ci.adoptium.net:80"
server = jenkins.Jenkins(url, username=username,
password=password)
return server

def help():
print("Help:\n"
" This script is used to retrieve dockerhost and static docker container information from the Adoptium Jenkins server hosted at https://ci.adoptium.net\n"
" Usage: python3 updateDockerStaticInventory.py $username $jenkinsAPItoken\n"
" The results are dumped into DockerInventory.json in the same directory from which the script was executed\n")

def main():
# Credentials passed via commandline
try:
username, password = sys.argv[1:3]
server = createServer(username, password)
dockerhosts = []
nodes = server.get_nodes()
except ValueError:
print("\nERROR:This script takes one username and one api token\n")
help()
sys.exit(1)

# Get a list of dockerhost machines
for node in nodes:
if node["name"].find("dockerhost") > -1:
dockerhost = node["name"]
ip = getIP(server.get_node_config(dockerhost))
dockerhosts.append({"name":dockerhost, "ip":ip})

dockerhostsFull = []

# Get static docker containers, group with dockerhosts
for dockerhost in dockerhosts:
print(dockerhost)
containers = []
for node in nodes:
try:
nodeConfig = server.get_node_config(node["name"])
nodeIP = getIP(nodeConfig)
nodePort = getNodePort(nodeConfig)
if nodeIP == dockerhost["ip"] and node["name"] != dockerhost["name"]:
nodeObject = {"nodeName": node["name"], "port": nodePort}
containers.append(nodeObject)
except jenkins.NotFoundException:
continue

dockerhostsFull.append({"name": dockerhost["name"], "ip": dockerhost["ip"], "containers": containers, "containersCount": len(containers)})

print(json.dumps(dockerhostsFull, indent=4))

# Write output to file
with open('../DockerInventory.json', 'w') as f:
json.dump(dockerhostsFull, f, indent=4)

if __name__ == "__main__":
main()

0 comments on commit dfd14fe

Please sign in to comment.