forked from adoptium/infrastructure
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docker: Automate deletion of docker static Jenkins nodes (adoptium#3561)
* initial * initial * finished deleteNodes script * script works with ansible * add no_log to python script task to hide variable from log * add comments * update comment * typo * cherry pick docs pr into this one * Change http to https * Change port to 443 --------- Co-authored-by: Martijn Verburg <[email protected]>
- Loading branch information
1 parent
3c06472
commit 18ae259
Showing
5 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,5 @@ | |
tags: deploy | ||
- role: deploy_container | ||
tags: deploy | ||
- role: remove_container | ||
tags: remove |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
ansible/playbooks/AdoptOpenJDK_Unix_Playbook/roles/remove_container/scripts/deleteNodes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import jenkins | ||
import sys | ||
import json | ||
import requests | ||
|
||
def createServer(username, password): | ||
url = "https://ci.adoptium.net:443" | ||
server = jenkins.Jenkins(url, username=username, | ||
password=password) | ||
return server | ||
|
||
def returnPort(inventory, nodeName): | ||
# Search dockerInventory.json for port numbers, return port number | ||
for dockerhost in inventory: | ||
for node in dockerhost["containers"]: | ||
if node["nodeName"] == nodeName: | ||
port = node["port"] | ||
return port | ||
|
||
def deleteJenkinsNode(nodeName, USERNAME, TOKEN): | ||
# Post request to doDelete jenkins url | ||
headers = {"Content-Type": "application/xml"} | ||
auth = (USERNAME, TOKEN) | ||
deleteURL = f'https://ci.adoptium.net/computer/{nodeName}/doDelete' | ||
r = requests.post(url=deleteURL, auth=auth, headers=headers) | ||
|
||
if r.status_code == 200: | ||
print(f'\n{nodeName} deleted\n') | ||
else: | ||
print(f'\nSomething went wrong. Check to see if {nodeName} is deleted\n') | ||
sys.exit(1) | ||
|
||
def main(): | ||
USERNAME,TOKEN = sys.argv[1:3] | ||
nodeList = sys.argv[3].split(',') | ||
|
||
server = createServer(USERNAME, TOKEN) | ||
|
||
with open('../../DockerInventory.json') as file: | ||
inventory = json.load(file) | ||
|
||
deletedNodesPorts = [] | ||
isNotIdleNodes = [] | ||
# Delete in Jenkins first | ||
for node in nodeList: | ||
testNode = server.get_node_info(node) | ||
if testNode['idle']: | ||
deleteJenkinsNode(node, USERNAME, TOKEN) | ||
deletedNodesPorts.append(returnPort(inventory, node)) | ||
else: | ||
isNotIdleNodes.append(node) | ||
|
||
if len(isNotIdleNodes) > 0: | ||
print(f'\nList of nodes that were not deleted: {isNotIdleNodes}\n') | ||
|
||
print(*deletedNodesPorts) | ||
|
||
if __name__ == "__main__": | ||
main() |
11 changes: 11 additions & 0 deletions
11
...le/playbooks/AdoptOpenJDK_Unix_Playbook/roles/remove_container/tasks/delete_container.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
|
||
# Get the container ID using the port number found in DockerhostInventory.json | ||
- name: Get container ID of container running on {{ docker_port }} | ||
shell: docker ps | grep {{ docker_port }} | sed 's/ /\n/g' | head -n 1 | ||
register: docker_container_ID | ||
|
||
- name: Stop and remove container {{ docker_container_ID.stdout }} | ||
community.docker.docker_container: | ||
name: "{{ docker_container_ID.stdout }}" | ||
state: absent |
24 changes: 24 additions & 0 deletions
24
ansible/playbooks/AdoptOpenJDK_Unix_Playbook/roles/remove_container/tasks/main.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
# The deleteNodes.py script deletes the nodes in Jenkins | ||
# Ansible then removes the docker containers on the dockerhost via the delete_container role | ||
# If the node is not idle in Jenkins, the node is not deleted | ||
|
||
# no_log hides the api_token from the output | ||
- name: Run deleteNodes script | ||
script: scripts/deleteNodes.py {{ jenkins_username }} {{ jenkins_api_token }} {{ delete_nodes }} | ||
args: | ||
executable: python3 | ||
delegate_to: localhost | ||
no_log: true | ||
register: docker_ports | ||
|
||
- name: Set docker_ports variable | ||
set_fact: | ||
ports_list: "{{ docker_ports.stdout_lines[0].split(' ') | list }}" | ||
|
||
# Iterate over each machine, removes container on dockerhost machine | ||
- name: Run delete_container.yml for every docker port | ||
include_tasks: delete_container.yml | ||
loop: "{{ ports_list }}" | ||
loop_control: | ||
loop_var: docker_port |