python-jcli 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
jcli/sdk/system.py ADDED
@@ -0,0 +1,202 @@
1
+ """Jenkins System SDK functions.
2
+
3
+ All functions take a :class:`~jcli.sdk.client.JenkinsClient` as the first argument.
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ from typing import TYPE_CHECKING, Any
9
+
10
+ if TYPE_CHECKING:
11
+ from jcli.sdk.client import JenkinsClient
12
+
13
+ # ------------------------------------------------------------------
14
+ # Public API
15
+ # ------------------------------------------------------------------
16
+
17
+
18
+ def get_system_info(client: "JenkinsClient") -> dict[str, Any]:
19
+ """Get Jenkins system information.
20
+
21
+ Calls ``GET /api/json`` and extracts version from the ``X-Jenkins``
22
+ response header.
23
+
24
+ Args:
25
+ client: Authenticated Jenkins API client.
26
+
27
+ Returns:
28
+ Dictionary with keys:
29
+ - ``version``: Jenkins version string (from ``X-Jenkins`` header).
30
+ - ``num_executors``: Total number of executors.
31
+ - ``mode``: Executor mode (e.g. ``NORMAL``, ``EXCLUSIVE``).
32
+ - ``quietingDown``: Whether Jenkins is in quiet-down mode.
33
+ - ``slaveAgentPort``: Port for JNLP agents.
34
+ - ``num_nodes``: Total number of connected nodes (from ``computer``).
35
+ - ``num_jobs``: Total number of jobs.
36
+ - ``raw``: Full API response data.
37
+ """
38
+ resp = client.request("GET", "/api/json")
39
+ data: dict[str, Any] = resp.json()
40
+
41
+ version = resp.headers.get("X-Jenkins", "unknown")
42
+
43
+ # Count nodes from computer list if available
44
+ num_nodes = 0
45
+ computer = data.get("computer", [])
46
+ if isinstance(computer, list):
47
+ num_nodes = len(computer)
48
+
49
+ return {
50
+ "version": version,
51
+ "num_executors": data.get("numExecutors", 0),
52
+ "mode": data.get("mode", ""),
53
+ "quietingDown": data.get("quietingDown", False),
54
+ "slaveAgentPort": data.get("slaveAgentPort", 0),
55
+ "num_nodes": num_nodes,
56
+ "num_jobs": len(data.get("jobs", [])),
57
+ "raw": data,
58
+ }
59
+
60
+
61
+ def safe_restart(client: "JenkinsClient") -> bool:
62
+ """Trigger a safe restart of Jenkins.
63
+
64
+ Calls ``POST /safeRestart``. Jenkins waits for running builds to
65
+ complete before restarting.
66
+
67
+ Args:
68
+ client: Authenticated Jenkins API client.
69
+
70
+ Returns:
71
+ ``True`` on success (HTTP 200/302).
72
+ """
73
+ resp = client.request("POST", "/safeRestart")
74
+ return resp.ok
75
+
76
+
77
+ def quiet_down(client: "JenkinsClient", reason: str = "") -> bool:
78
+ """Put Jenkins into quiet-down mode.
79
+
80
+ In quiet-down mode, Jenkins stops scheduling new builds. Running
81
+ builds continue until finished. Calls ``POST /quietDown``.
82
+
83
+ Args:
84
+ client: Authenticated Jenkins API client.
85
+ reason: Optional reason shown on the quiet-down banner.
86
+
87
+ Returns:
88
+ ``True`` on success (HTTP 200/302).
89
+ """
90
+ params: dict[str, str] | None = None
91
+ if reason:
92
+ params = {"reason": reason}
93
+ resp = client.request("POST", "/quietDown", params=params)
94
+ return resp.ok
95
+
96
+
97
+ def cancel_quiet_down(client: "JenkinsClient") -> bool:
98
+ """Cancel quiet-down mode, allowing Jenkins to resume scheduling builds.
99
+
100
+ Calls ``POST /cancelQuietDown``.
101
+
102
+ Args:
103
+ client: Authenticated Jenkins API client.
104
+
105
+ Returns:
106
+ ``True`` on success (HTTP 200/302).
107
+ """
108
+ resp = client.request("POST", "/cancelQuietDown")
109
+ return resp.ok
110
+
111
+
112
+ def run_script(client: "JenkinsClient", script: str) -> Any:
113
+ """Execute a Groovy script on the Jenkins server.
114
+
115
+ Calls ``POST /scriptText`` with form-encoded script content.
116
+
117
+ Args:
118
+ client: Jenkins API client.
119
+ script: Groovy script text.
120
+
121
+ Returns:
122
+ Script output as text.
123
+ """
124
+ resp = client.post_data("/scriptText", data={"script": script})
125
+ return resp.text
126
+
127
+
128
+ def get_system_load(client: "JenkinsClient") -> dict[str, Any]:
129
+ """Get current system load statistics.
130
+
131
+ Calls ``GET /queue/api/json`` for the build queue and
132
+ ``GET /computer/api/json`` for executor usage.
133
+
134
+ Args:
135
+ client: Authenticated Jenkins API client.
136
+
137
+ Returns:
138
+ Dictionary with keys:
139
+ - ``queue_length``: Number of items in the build queue.
140
+ - ``total_executors``: Total executors across all nodes.
141
+ - ``busy_executors``: Currently busy executors.
142
+ - ``idle_executors``: Currently idle executors.
143
+ """
144
+ queue_data = client.get_json("/queue/api/json")
145
+ queue_items = queue_data.get("items", [])
146
+ queue_length = len(queue_items) if isinstance(queue_items, list) else 0
147
+
148
+ computer_data = client.get_json("/computer/api/json")
149
+ computers = computer_data.get("computer", [])
150
+ total_executors = 0
151
+ busy_executors = 0
152
+ if isinstance(computers, list):
153
+ for c in computers:
154
+ total_executors += c.get("numExecutors", 0)
155
+ busy = c.get("busyExecutors", 0)
156
+ if busy:
157
+ busy_executors += busy
158
+
159
+ idle_executors = total_executors - busy_executors
160
+
161
+ return {
162
+ "queue_length": queue_length,
163
+ "total_executors": total_executors,
164
+ "busy_executors": busy_executors,
165
+ "idle_executors": idle_executors,
166
+ }
167
+
168
+
169
+ def list_users(client: "JenkinsClient") -> Any:
170
+ """List all Jenkins users.
171
+
172
+ Calls ``GET /asynchPeople/api/json``.
173
+
174
+ Args:
175
+ client: Authenticated Jenkins API client.
176
+
177
+ Returns:
178
+ Parsed JSON with users list.
179
+ """
180
+ return client.get_json("/asynchPeople/api/json")
181
+
182
+
183
+ def generate_token(
184
+ client: "JenkinsClient",
185
+ username: str,
186
+ token_name: str = "jcli-generated",
187
+ ) -> Any:
188
+ """Generate a new API token for a user.
189
+
190
+ Calls ``POST /user/{name}/descriptorByName/jenkins.security.ApiTokenProperty/generateNewToken``.
191
+
192
+ Args:
193
+ client: Authenticated Jenkins API client.
194
+ username: Jenkins username.
195
+ token_name: Name/label for the new token.
196
+
197
+ Returns:
198
+ Parsed JSON with token data including the token value.
199
+ """
200
+ path = f"/user/{username}/descriptorByName/jenkins.security.ApiTokenProperty/generateNewToken"
201
+ data = {"newTokenName": token_name}
202
+ return client.post_data(path, data=data).json()
jcli/sdk/view.py ADDED
@@ -0,0 +1,94 @@
1
+ """SDK functions for Jenkins View management."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Any
6
+
7
+ from jcli.sdk.client import JenkinsClient
8
+ from jcli.sdk.exceptions import JenkinsAPIError
9
+
10
+
11
+ def list_views(client: JenkinsClient) -> list[dict[str, Any]]:
12
+ """List all views on the Jenkins server.
13
+
14
+ Args:
15
+ client: Authenticated Jenkins client.
16
+
17
+ Returns:
18
+ List of view dicts, each with ``name``, ``url``, and ``jobs`` keys.
19
+
20
+ Raises:
21
+ JenkinsAPIError: If the API request fails.
22
+ """
23
+ data = client.get_json("/api/json", tree="views[name,url,jobs[name]]")
24
+ return data.get("views", [])
25
+
26
+
27
+ def get_view(client: JenkinsClient, name: str) -> dict[str, Any]:
28
+ """Get details for a single Jenkins view.
29
+
30
+ Args:
31
+ client: Authenticated Jenkins client.
32
+ name: View name.
33
+
34
+ Returns:
35
+ View detail dict.
36
+
37
+ Raises:
38
+ JenkinsAPIError: If the API request fails.
39
+ JenkinsNotFoundError: If the view doesn't exist.
40
+ """
41
+ return client.get_json(f"/view/{name}/api/json")
42
+
43
+
44
+ def create_view(
45
+ client: JenkinsClient,
46
+ name: str,
47
+ config_xml: str,
48
+ ) -> None:
49
+ """Create a new Jenkins view.
50
+
51
+ Args:
52
+ client: Authenticated Jenkins client.
53
+ name: View name to create.
54
+ config_xml: XML configuration for the view.
55
+
56
+ Raises:
57
+ JenkinsAPIError: If the API request fails (e.g. view already exists).
58
+ """
59
+ client.post_xml(f"/createView?name={name}", config_xml)
60
+
61
+
62
+ def update_view(
63
+ client: JenkinsClient,
64
+ name: str,
65
+ config_xml: str | bytes,
66
+ ) -> Any:
67
+ """Update a view's configuration.
68
+
69
+ Args:
70
+ client: Jenkins API client.
71
+ name: View name.
72
+ config_xml: New XML configuration.
73
+
74
+ Returns:
75
+ Response from Jenkins.
76
+
77
+ Raises:
78
+ JenkinsAPIError: If the API request fails.
79
+ """
80
+ return client.post_xml(f"/view/{name}/config.xml", config_xml)
81
+
82
+
83
+ def delete_view(client: JenkinsClient, name: str) -> None:
84
+ """Delete an existing Jenkins view.
85
+
86
+ Args:
87
+ client: Authenticated Jenkins client.
88
+ name: View name to delete.
89
+
90
+ Raises:
91
+ JenkinsAPIError: If the API request fails.
92
+ JenkinsNotFoundError: If the view doesn't exist.
93
+ """
94
+ client.post_data(f"/view/{name}/doDelete", data="")
@@ -0,0 +1,73 @@
1
+ ---
2
+ name: jcli
3
+ version: 1.0.0
4
+ description: |
5
+ jcli — Jenkins CLI 工具,通过命令行管理 Jenkins 服务器。
6
+ allowed-tools:
7
+ - Bash
8
+ - Read
9
+ ---
10
+
11
+ # jcli — Jenkins CLI 工具
12
+
13
+ 通过命令行管理 Jenkins 服务器。支持 Job、构建、节点、插件、凭据、Pipeline、视图、系统管理等全功能操作。
14
+
15
+ ## 前提条件
16
+
17
+ - Python >= 3.10
18
+ - Jenkins 2.x 或更高版本
19
+ - 已安装 jcli:`pip install -e /data/git-project/jcli`
20
+
21
+ ## 输出格式
22
+
23
+ ```bash
24
+ jcli -f table job list # 彩色表格(默认)
25
+ jcli -f json job list # JSON 输出
26
+ jcli -f yaml job list # YAML 输出
27
+ ```
28
+
29
+ ## 内置技能
30
+
31
+ | 技能 | 说明 |
32
+ |------|------|
33
+ | [jcli-config](config/SKILL.md) | 配置管理 - Jenkins 连接配置、多实例管理 |
34
+ | [jcli-job](job/SKILL.md) | Job 管理 - Job 创建、删除、复制、启用/禁用 |
35
+ | [jcli-build](build/SKILL.md) | 构建管理 - 构建触发、查看、停止、队列 |
36
+ | [jcli-node](node/SKILL.md) | 节点管理 - Jenkins Agent 节点管理 |
37
+ | [jcli-plugin](plugin/SKILL.md) | 插件管理 - 插件安装、卸载、查看 |
38
+ | [jcli-credential](credential/SKILL.md) | 凭据管理 - 凭据创建、删除、查看 |
39
+ | [jcli-pipeline](pipeline/SKILL.md) | Pipeline 管理 - 阶段查看、日志获取、验证 |
40
+ | [jcli-view](view/SKILL.md) | 视图管理 - 视图创建、删除、查看 |
41
+ | [jcli-system](system/SKILL.md) | 系统管理 - 系统信息、重启、安静模式 |
42
+
43
+ ## Skills 命令
44
+
45
+ ```bash
46
+ jcli skills list # 列出所有可用技能
47
+ jcli skills list --installed # 列出已安装技能
48
+ jcli skills list --bundled # 列出内置技能
49
+ jcli skills install <name> # 安装技能
50
+ jcli skills install <name> --force # 强制安装(覆盖已有)
51
+ jcli skills uninstall <name> # 卸载技能
52
+ jcli skills get <name> # 查看技能详情
53
+ ```
54
+
55
+ ## 调试
56
+
57
+ ```bash
58
+ jcli -d job list # 启用调试日志
59
+ ```
60
+
61
+ ## Shell 补全
62
+
63
+ ```bash
64
+ jcli completion show bash # Bash 补全脚本
65
+ jcli completion show zsh # Zsh 补全脚本
66
+ jcli completion show fish # Fish 补全脚本
67
+ ```
68
+
69
+ ## 项目路径
70
+
71
+ - 源码: `/data/git-project/jcli`
72
+ - 配置: `~/.jcli/config.yaml`
73
+ - 技能文档: `/data/git-project/jcli/skills/jcli/`
@@ -0,0 +1,81 @@
1
+ ---
2
+ name: jcli-config
3
+ version: 1.0.0
4
+ description: |
5
+ jcli 配置管理 - 管理 Jenkins 连接配置,支持多实例管理。
6
+ allowed-tools:
7
+ - Bash
8
+ - Read
9
+ ---
10
+
11
+ # jcli-config
12
+
13
+ 管理 jcli 的连接配置,支持多 Jenkins 实例。
14
+
15
+ ## 前提条件
16
+
17
+ - Python >= 3.10
18
+ - Jenkins 2.x 或更高版本
19
+ - 已安装 jcli:`pip install -e /data/git-project/jcli`
20
+
21
+ ## 初始化配置
22
+
23
+ ```bash
24
+ jcli config init
25
+ ```
26
+
27
+ ## 配置文件位置
28
+
29
+ `~/.jcli/config.yaml`
30
+
31
+ ## 配置结构
32
+
33
+ ```yaml
34
+ active_profile: default
35
+ profiles:
36
+ default:
37
+ url: https://jenkins.example.com
38
+ username: admin
39
+ api_token: your-api-token-here
40
+ description: Default Jenkins instance
41
+ ```
42
+
43
+ ## 多实例管理
44
+
45
+ ```bash
46
+ # 添加新 profile
47
+ jcli config add dev --url https://jenkins-dev.example.com --username admin
48
+
49
+ # 修改配置
50
+ jcli config set dev api_token your-token
51
+ jcli config set dev description "Development Jenkins"
52
+
53
+ # 切换 profile
54
+ jcli config use dev
55
+
56
+ # 查看配置
57
+ jcli config show
58
+ jcli config list
59
+ ```
60
+
61
+ ## 环境变量覆盖
62
+
63
+ | 变量 | 覆盖字段 |
64
+ |------|---------|
65
+ | `JCLI_URL` | url |
66
+ | `JCLI_USERNAME` | username |
67
+ | `JCLI_API_TOKEN` | api_token |
68
+ | `JCLI_PROFILE` | active_profile |
69
+
70
+ ## 命令参考
71
+
72
+ ```bash
73
+ jcli config init # 初始化配置文件
74
+ jcli config show # 查看当前配置
75
+ jcli config show --profile dev # 查看指定 profile
76
+ jcli config list # 列出所有 profiles
77
+ jcli config add <name> --url URL # 添加新 profile
78
+ jcli config set <name> <field> <value> # 修改配置
79
+ jcli config use <name> # 切换 active profile
80
+ jcli config delete <name> # 删除 profile
81
+ ```
@@ -0,0 +1,47 @@
1
+ ---
2
+ name: jcli-credential
3
+ version: 1.0.0
4
+ description: |
5
+ jcli 凭据管理 - 管理 Jenkins 凭据的创建、删除、查看等操作。
6
+ allowed-tools:
7
+ - Bash
8
+ - Read
9
+ ---
10
+
11
+ # jcli-credential
12
+
13
+ 管理 Jenkins 凭据的创建、删除、查看等操作。
14
+
15
+ ## 前提条件
16
+
17
+ - Python >= 3.10
18
+ - Jenkins 2.x 或更高版本
19
+ - 已安装 jcli:`pip install -e /data/git-project/jcli`
20
+
21
+ ## 命令参考
22
+
23
+ ```bash
24
+ jcli credential list # 列出凭据
25
+ jcli credential get <id> # 查看凭据详情
26
+ jcli credential create <id> -f config.xml # 从 XML 创建凭据
27
+ jcli credential delete <id> # 删除凭据
28
+ ```
29
+
30
+ ## 常见用例
31
+
32
+ ### 查看凭据
33
+
34
+ ```bash
35
+ # 列出所有凭据
36
+ jcli credential list
37
+
38
+ # 查看特定凭据详情
39
+ jcli credential get my-credential-id
40
+ ```
41
+
42
+ ### 创建凭据
43
+
44
+ ```bash
45
+ # 从 XML 配置文件创建凭据
46
+ jcli credential create my-credential-id -f credential-config.xml
47
+ ```
@@ -0,0 +1,55 @@
1
+ ---
2
+ name: jcli-job
3
+ version: 1.0.0
4
+ description: |
5
+ jcli Job 管理 - 管理 Jenkins Job 的创建、删除、复制、启用/禁用等操作。
6
+ allowed-tools:
7
+ - Bash
8
+ - Read
9
+ ---
10
+
11
+ # jcli-job
12
+
13
+ 管理 Jenkins Job 的创建、删除、复制、启用/禁用等操作。
14
+
15
+ ## 前提条件
16
+
17
+ - Python >= 3.10
18
+ - Jenkins 2.x 或更高版本
19
+ - 已安装 jcli:`pip install -e /data/git-project/jcli`
20
+
21
+ ## 命令参考
22
+
23
+ ```bash
24
+ jcli job list # 列出所有 Job
25
+ jcli job get <name> # 查看 Job 详情
26
+ jcli job create <name> -f config.xml # 从 XML 创建 Job
27
+ jcli job delete <name> # 删除 Job(需确认)
28
+ jcli job delete <name> --yes # 删除 Job(跳过确认)
29
+ jcli job copy <from> <to> # 复制 Job
30
+ jcli job enable <name> # 启用 Job
31
+ jcli job disable <name> # 禁用 Job
32
+ jcli job config <name> # 查看 Job XML 配置
33
+ ```
34
+
35
+ ## 常见用例
36
+
37
+ ### 导出 Job 配置
38
+
39
+ ```bash
40
+ jcli job config my-job > job-config.xml
41
+ ```
42
+
43
+ ### 批量导出所有 Job 配置
44
+
45
+ ```bash
46
+ jcli -f json job list | jq -r '.[].name' | while read job; do
47
+ jcli job config "$job" > "${job}.xml"
48
+ done
49
+ ```
50
+
51
+ ### 从 XML 创建 Job
52
+
53
+ ```bash
54
+ jcli job create my-new-job -f job-config.xml
55
+ ```
@@ -0,0 +1,51 @@
1
+ ---
2
+ name: jcli-node
3
+ version: 1.0.0
4
+ description: |
5
+ jcli 节点管理 - 管理 Jenkins Agent 节点的上线、下线、删除等操作。
6
+ allowed-tools:
7
+ - Bash
8
+ - Read
9
+ ---
10
+
11
+ # jcli-node
12
+
13
+ 管理 Jenkins Agent 节点的上线、下线、删除等操作。
14
+
15
+ ## 前提条件
16
+
17
+ - Python >= 3.10
18
+ - Jenkins 2.x 或更高版本
19
+ - 已安装 jcli:`pip install -e /data/git-project/jcli`
20
+
21
+ ## 命令参考
22
+
23
+ ```bash
24
+ jcli node list # 列出所有节点
25
+ jcli node get <name> # 查看节点详情
26
+ jcli node delete <name> # 删除节点
27
+ jcli node toggle <name> --message "维护" # 节点离线
28
+ jcli node toggle <name> # 节点上线
29
+ ```
30
+
31
+ ## 常见用例
32
+
33
+ ### 节点维护
34
+
35
+ ```bash
36
+ # 节点离线(带维护原因)
37
+ jcli node toggle agent-01 --message "系统升级维护"
38
+
39
+ # 节点恢复上线
40
+ jcli node toggle agent-01
41
+ ```
42
+
43
+ ### 查看节点状态
44
+
45
+ ```bash
46
+ # 查看所有节点状态
47
+ jcli node list
48
+
49
+ # 查看特定节点详情
50
+ jcli node get agent-01
51
+ ```
@@ -0,0 +1,58 @@
1
+ ---
2
+ name: jcli-pipeline
3
+ version: 1.0.0
4
+ description: |
5
+ jcli Pipeline 管理 - 管理 Jenkins Pipeline 的阶段查看、日志获取、Jenkinsfile 验证等操作。
6
+ allowed-tools:
7
+ - Bash
8
+ - Read
9
+ ---
10
+
11
+ # jcli-pipeline
12
+
13
+ 管理 Jenkins Pipeline 的阶段查看、日志获取、Jenkinsfile 验证等操作。
14
+
15
+ ## 前提条件
16
+
17
+ - Python >= 3.10
18
+ - Jenkins 2.x 或更高版本
19
+ - 已安装 jcli:`pip install -e /data/git-project/jcli`
20
+
21
+ ## 命令参考
22
+
23
+ ```bash
24
+ jcli pipeline stages <job> <build> # 查看阶段信息
25
+ jcli pipeline log <job> <build> <node-id> # 查看步骤日志
26
+ jcli pipeline validate -f Jenkinsfile # 验证 Jenkinsfile
27
+ jcli pipeline pending <job> <build> # 查看待处理输入
28
+ ```
29
+
30
+ ## 常见用例
31
+
32
+ ### 查看 Pipeline 阶段
33
+
34
+ ```bash
35
+ # 查看构建的各个阶段
36
+ jcli pipeline stages my-job 42
37
+ ```
38
+
39
+ ### 查看特定阶段日志
40
+
41
+ ```bash
42
+ # 查看特定节点/阶段的日志
43
+ jcli pipeline log my-job 42 15
44
+ ```
45
+
46
+ ### 验证 Jenkinsfile
47
+
48
+ ```bash
49
+ # 验证 Jenkinsfile 语法
50
+ jcli pipeline validate -f Jenkinsfile
51
+ ```
52
+
53
+ ### 查看待处理输入
54
+
55
+ ```bash
56
+ # 查看 Pipeline 中等待人工确认的步骤
57
+ jcli pipeline pending my-job 42
58
+ ```