Skip to content

Commit

Permalink
Refactor jenkins.py (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturo-seijas authored Mar 7, 2024
1 parent 518c6a1 commit 3991064
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 224 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
channel: 1.28-strict/stable
extra-arguments: |
--kube-config ${GITHUB_WORKSPACE}/kube-config
modules: '["test_ingress.py", "test_jenkins.py", "test_k8s_agent.py", "test_machine_agent.py", "test_plugins.py", "test_proxy.py", "test_cos.py"]'
modules: '["test_cos.py", "test_ingress.py", "test_jenkins.py", "test_k8s_agent.py", "test_machine_agent.py", "test_plugins.py", "test_proxy.py"]'
pre-run-script: |
-c "sudo microk8s config > ${GITHUB_WORKSPACE}/kube-config
chmod +x tests/integration/pre_run_script.sh
Expand Down
112 changes: 39 additions & 73 deletions src-docs/jenkins.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Functions to operate Jenkins.
- **WAR_DOWNLOAD_URL**
- **SYSTEM_PROPERTY_HEADLESS**
- **SYSTEM_PROPERTY_LOGGING**
- **DEFAULT_JENKINS_CONFIG**
- **JENKINS_LOGGING_CONFIG**
- **PLUGIN_NAME_GROUP**
- **WHITESPACE**
- **VERSION_GROUP**
Expand All @@ -28,87 +30,87 @@ Functions to operate Jenkins.

---

<a href="../src/jenkins.py#L145"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/jenkins.py#L113"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `wait_ready`
## <kbd>function</kbd> `get_admin_credentials`

```python
wait_ready(timeout: int = 300, check_interval: int = 10) → None
get_admin_credentials(container: Container) → Credentials
```

Wait until Jenkins service is up.
Retrieve admin credentials.



**Args:**

- <b>`timeout`</b>: Time in seconds to wait for jenkins to become ready in 10 second intervals.
- <b>`check_interval`</b>: Time in seconds to wait between ready checks.
- <b>`container`</b>: The Jenkins workload container to interact with filesystem.



**Raises:**

- <b>`TimeoutError`</b>: if Jenkins status check did not pass within the timeout duration.
**Returns:**
The Jenkins admin account credentials.


---

<a href="../src/jenkins.py#L177"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/jenkins.py#L185"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `is_storage_ready`
## <kbd>function</kbd> `wait_ready`

```python
is_storage_ready(container: Container) → bool
wait_ready(timeout: int = 300, check_interval: int = 10) → None
```

Return whether the Jenkins home directory is mounted and owned by jenkins.
Wait until Jenkins service is up.



**Args:**

- <b>`container`</b>: The Jenkins workload container.
- <b>`timeout`</b>: Time in seconds to wait for jenkins to become ready in 10 second intervals.
- <b>`check_interval`</b>: Time in seconds to wait between ready checks.



**Raises:**

- <b>`StorageMountError`</b>: if there was an error getting storage information.



**Returns:**
True if home directory is mounted and owned by jenkins, False otherwise.
- <b>`TimeoutError`</b>: if Jenkins status check did not pass within the timeout duration.


---

<a href="../src/jenkins.py#L213"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/jenkins.py#L217"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `get_admin_credentials`
## <kbd>function</kbd> `is_storage_ready`

```python
get_admin_credentials(container: Container) → Credentials
is_storage_ready(container: Optional[Container]) → bool
```

Retrieve admin credentials.
Return whether the Jenkins home directory is mounted and owned by jenkins.



**Args:**

- <b>`container`</b>: The Jenkins workload container to interact with filesystem.
- <b>`container`</b>: The Jenkins workload container.



**Raises:**

- <b>`StorageMountError`</b>: if there was an error getting storage information.



**Returns:**
The Jenkins admin account credentials.
True if home directory is mounted and owned by jenkins, False otherwise.


---

<a href="../src/jenkins.py#L257"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/jenkins.py#L242"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `calculate_env`

Expand All @@ -126,7 +128,7 @@ Return a dictionary for Jenkins Pebble layer.

---

<a href="../src/jenkins.py#L266"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/jenkins.py#L251"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `get_version`

Expand All @@ -150,7 +152,7 @@ Get the Jenkins server version.

---

<a href="../src/jenkins.py#L451"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/jenkins.py#L436"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `bootstrap`

Expand All @@ -176,7 +178,7 @@ Initialize and install Jenkins.

---

<a href="../src/jenkins.py#L489"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/jenkins.py#L474"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `get_node_secret`

Expand Down Expand Up @@ -207,7 +209,7 @@ Get node secret from jenkins.

---

<a href="../src/jenkins.py#L546"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/jenkins.py#L531"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `add_agent_node`

Expand All @@ -233,7 +235,7 @@ Add a Jenkins agent node.

---

<a href="../src/jenkins.py#L567"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/jenkins.py#L552"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `remove_agent_node`

Expand All @@ -259,7 +261,7 @@ Remove a Jenkins agent node.

---

<a href="../src/jenkins.py#L620"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/jenkins.py#L605"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `safe_restart`

Expand All @@ -284,7 +286,7 @@ Safely restart Jenkins server after all jobs are done executing.

---

<a href="../src/jenkins.py#L645"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/jenkins.py#L630"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `get_agent_name`

Expand All @@ -308,7 +310,7 @@ Infer agent name from unit name.

---

<a href="../src/jenkins.py#L825"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/jenkins.py#L810"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `remove_unlisted_plugins`

Expand Down Expand Up @@ -339,7 +341,7 @@ Remove plugins that are not in the list of desired plugins.

---

<a href="../src/jenkins.py#L921"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/jenkins.py#L906"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `rotate_credentials`

Expand Down Expand Up @@ -416,15 +418,6 @@ Base exception for Jenkins errors.



---

## <kbd>class</kbd> `JenkinsNetworkError`
An error occurred communicating with the upstream Jenkins server.





---

## <kbd>class</kbd> `JenkinsPluginError`
Expand All @@ -434,33 +427,6 @@ An error occurred installing Jenkins plugin.



---

## <kbd>class</kbd> `JenkinsProxyError`
An error occurred configuring Jenkins proxy.





---

## <kbd>class</kbd> `JenkinsRestartError`
An error occurred trying to restart Jenkins.





---

## <kbd>class</kbd> `JenkinsUpdateError`
An error occurred trying to update Jenkins.





---

## <kbd>class</kbd> `StorageMountError`
Expand All @@ -472,7 +438,7 @@ Represents an error probing for Jenkins storage mount.

- <b>`msg`</b>: Explanation of the error.

<a href="../src/jenkins.py#L168"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/jenkins.py#L208"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `__init__`

Expand Down
4 changes: 2 additions & 2 deletions src/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def on_get_admin_password(self, event: ops.ActionEvent) -> None:
event: The event fired from get-admin-password action.
"""
container = self.charm.unit.get_container(JENKINS_SERVICE_NAME)
if not container.can_connect() or not jenkins.is_storage_ready(container):
if not jenkins.is_storage_ready(container):
event.fail("Service not yet ready.")
return
credentials = jenkins.get_admin_credentials(container)
Expand All @@ -49,7 +49,7 @@ def on_rotate_credentials(self, event: ops.ActionEvent) -> None:
event: The rotate credentials event.
"""
container = self.charm.unit.get_container(JENKINS_SERVICE_NAME)
if not container.can_connect() or not jenkins.is_storage_ready(container):
if not jenkins.is_storage_ready(container):
event.fail("Service not yet ready.")
return
try:
Expand Down
8 changes: 4 additions & 4 deletions src/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def _on_deprecated_agent_relation_joined(self, event: ops.RelationJoinedEvent) -
event: The event fired from an agent joining the relationship.
"""
container = self.charm.unit.get_container(JENKINS_SERVICE_NAME)
if not container.can_connect() or not jenkins.is_storage_ready(container):
if not jenkins.is_storage_ready(container):
logger.warning("Service not yet ready. Deferring.")
event.defer() # The event needs to be handled after Jenkins has started(pebble ready).
return
Expand Down Expand Up @@ -149,7 +149,7 @@ def _on_agent_relation_joined(self, event: ops.RelationJoinedEvent) -> None:
event: The event fired from an agent joining the relationship.
"""
container = self.charm.unit.get_container(JENKINS_SERVICE_NAME)
if not container.can_connect() or not jenkins.is_storage_ready(container):
if not jenkins.is_storage_ready(container):
logger.warning("Service not yet ready. Deferring.")
event.defer() # The event needs to be handled after Jenkins has started(pebble ready).
return
Expand Down Expand Up @@ -187,7 +187,7 @@ def _on_deprecated_agent_relation_departed(self, event: ops.RelationDepartedEven
"""
# the event unit cannot be None.
container = self.charm.unit.get_container(JENKINS_SERVICE_NAME)
if not container.can_connect() or not jenkins.is_storage_ready(container):
if not jenkins.is_storage_ready(container):
logger.warning("Relation departed before service ready.")
return

Expand Down Expand Up @@ -215,7 +215,7 @@ def _on_agent_relation_departed(self, event: ops.RelationDepartedEvent) -> None:
"""
# the event unit cannot be None.
container = self.charm.unit.get_container(JENKINS_SERVICE_NAME)
if not container.can_connect() or not jenkins.is_storage_ready(container):
if not jenkins.is_storage_ready(container):
logger.warning("Relation departed before service ready.")
return

Expand Down
8 changes: 4 additions & 4 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def __init__(self, *args: typing.Any):
self.agent_observer = agent.Observer(
self, self.state, self.agent_discovery_ingress_observer
)
self.cos_observer = cos.Observer(self)
self.ingress_observer = ingress.Observer(self, "ingress-observer", INGRESS_RELATION_NAME)
self.cos_observer = cos.Observer(self)
self.framework.observe(
self.on.jenkins_home_storage_attached, self._on_jenkins_home_storage_attached
)
Expand All @@ -78,6 +78,7 @@ def _get_pebble_layer(self, jenkins_env: jenkins.Environment) -> ops.pebble.Laye
Returns:
The pebble layer defining Jenkins service layer.
"""
env_dict = typing.cast(typing.Dict[str, str], jenkins_env)
layer: LayerDict = {
"summary": "jenkins layer",
"description": "pebble config layer for jenkins",
Expand All @@ -89,8 +90,7 @@ def _get_pebble_layer(self, jenkins_env: jenkins.Environment) -> ops.pebble.Laye
f"-D{jenkins.SYSTEM_PROPERTY_LOGGING} "
f"-jar {jenkins.EXECUTABLES_PATH}/jenkins.war",
"startup": "enabled",
# TypedDict and Dict[str,str] are not compatible.
"environment": typing.cast(typing.Dict[str, str], jenkins_env),
"environment": env_dict,
"user": jenkins.USER,
"group": jenkins.GROUP,
},
Expand Down Expand Up @@ -183,7 +183,7 @@ def _on_update_status(self, _: ops.UpdateStatusEvent) -> None:
2. Update Jenkins patch version if available and is within restart-time-range config value.
"""
container = self.unit.get_container(JENKINS_SERVICE_NAME)
if not container.can_connect() or not jenkins.is_storage_ready(container):
if not jenkins.is_storage_ready(container):
self.unit.status = ops.WaitingStatus("Waiting for container/storage.")
return

Expand Down
Loading

0 comments on commit 3991064

Please sign in to comment.