feat(jenkins_node_info): add info module

This commit is contained in:
2025-02-08 15:40:05 +01:00
parent ab4b7bafe7
commit 93a9e70a81
5 changed files with 243 additions and 0 deletions

View File

@ -0,0 +1,44 @@
import xml.etree.ElementTree as ET
from typing import Optional
from dataclasses import dataclass
import requests
from pathspec import PathSpec
class Jenkins:
@dataclass
class NodeInfo:
server_url: str
name: str
secret: Optional[str]
work_dir: PathSpec
internal_dir: PathSpec
server_url: str
def __init__(self, server_url, api_token):
self.server_url = server_url
self.api_token = api_token
def _log_in(self, username: str, password: str) -> (str, str):
response = requests.get(f"{self.server_url}/crumbIssuer/api/json")
response.raise_for_status()
payload = response.json()
return payload["crumbRequestField"], payload["crumb"]
def get_node_jnlp(self, node_name) -> str:
response = requests.get(
f"{self.server_url}/manage/computer/{node_name}/slave-agent.jnlp"
)
response.raise_for_status()
return response.text
def get_node_info(self, node_name: str) -> NodeInfo:
jnlp_info_raw = self.get_node_jnlp(node_name)
tree = ET.ElementTree(ET.fromstring(jnlp_info_raw))
arguments = tree.findall("./application-desc/")
(node_secret, node_name, _, work_dir, _, internal_dir, _, url) = [
arg.text for arg in arguments[:7]
]
return Jenkins.NodeInfo(url, node_name, node_secret, work_dir, internal_dir)

View File

View File

@ -0,0 +1,107 @@
# pylint: disable=E0401
from __future__ import absolute_import, annotations, division, print_function
__metaclass__ = type # pylint: disable=C0103
from typing import TYPE_CHECKING
from ansible.module_utils.basic import AnsibleModule
from plugins.module_utils.Jenkins import Jenkins
if TYPE_CHECKING:
from typing import Optional, Dict, Any
DOCUMENTATION = r"""
---
module: jenkins_node
short_description: Retrieve Jenkins node information
# If this is part of a collection, you need to use semantic versioning,
# i.e. the version is of the form "2.5.0" and not "2.4".
version_added: "0.0.1"
description: This is my longer description explaining my test module.
options:
name:
description: The name of the jenkins node.
required: true
type: str
server:
description: URL of the jenkins instance
required: true
type: str
api_token:
description: Jenkins API token
required: true
type: str
author:
- transcaffeine (@transcaffeine)
"""
EXAMPLES = r"""
# Pass in a message
- name: Test with a message
my_namespace.my_collection.my_test:
name: hello world
# pass in a message and have changed true
- name: Test with a message and changed output
my_namespace.my_collection.my_test:
name: hello world
new: true
# fail the module
- name: Test failure of the module
my_namespace.my_collection.my_test:
name: fail me
"""
RETURN = r"""
# These are examples of possible return values, and in general should use other names for return values.
name:
description: The name of the jenkins node
type: str
returned: always
sample: 'jenkins-agent-jdk21-alpine'
secret:
description: The secret of the agent
type: str
returned: always
sample: 'secretverylongstringwith64chars'
work_dir:
description: The local working directory of the jenkins agent
type: str
returned: always
"""
def run_module():
module_args = dict(
name=dict(type="str", required=True),
server=dict(type="str", required=True),
api_token=dict(type="str", required=True),
)
result = dict(changed=False, original_message="", message="")
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
jenkins = Jenkins(module.params["url"], module.params["token"])
node = jenkins.get_node_info(module.params["name"])
result["name"] = node.name
result["secret"] = node.secret
result["work_dir"] = node.work_dir
# in the event of a successful module execution, you will want to
# simple AnsibleModule.exit_json(), passing the key/value results
module.exit_json(**result)
def main():
run_module()
if __name__ == "__main__":
main()