Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker: script to retrieve static docker containers from jenkins #3423

Merged
merged 5 commits into from Apr 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,77 @@
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 "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):
server = jenkins.Jenkins('http://ci.adoptium.net:80', username=username,
password=password)
return server

def main():

# Credentials passed via commandline
username, password = sys.argv[1:3]
server = createServer(username, password)
dockerhosts = []
nodes = server.get_nodes()

# 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[0:1]:
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('../dockerhost.json', 'w') as f:
json.dump(dockerhostsFull, f, indent=4)

if __name__ == "__main__":
main()