Merge ~mertkirpici/charm-graylog:lp/2003795 into charm-graylog:master

Proposed by Mert Kirpici
Status: Merged
Approved by: Ramesh Sattaru
Approved revision: fac0de5402139254cdcaa71a2d7c0d5bf4edbc9f
Merged at revision: c167a0075da580fd96398ecb2e804ea380164903
Proposed branch: ~mertkirpici/charm-graylog:lp/2003795
Merge into: charm-graylog:master
Diff against target: 60 lines (+24/-2)
2 files modified
src/lib/charms/layer/graylog/api.py (+5/-0)
src/reactive/graylog.py (+19/-2)
Reviewer Review Type Date Requested Status
🤖 prod-jenkaas-bootstack (community) continuous-integration Approve
Erhan Sunar (community) Approve
Sudeep Bhandari Approve
BootStack Reviewers Pending
Review via email: mp+436349@code.launchpad.net

Commit message

Close LP #2003795

Description of the change

layer/graylog/api: dynamic import search path

The file lib/charms/layer/graylog/api.py is both imported by the graylog charm and the nrpe charm. While running in the nrpe context, we need to add the graylog charm's site-packages dir to the search path in order for the import mechanism to work correctly.

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
Sudeep Bhandari (sudeephb) wrote :

I tested, and the error has gone away on the first look. Thank you.
Left a small nitpick comment.

review: Approve
Revision history for this message
Erhan Sunar (esunar) :
review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision c167a0075da580fd96398ecb2e804ea380164903

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/lib/charms/layer/graylog/api.py b/src/lib/charms/layer/graylog/api.py
2index cdda27b..7445fa6 100644
3--- a/src/lib/charms/layer/graylog/api.py
4+++ b/src/lib/charms/layer/graylog/api.py
5@@ -1,9 +1,14 @@
6 """Manage graylog via its API."""
7+import importlib.util
8 import json
9 import os
10+import site
11
12 import requests
13
14+if not importlib.util.find_spec("tenacity"):
15+ site.addsitedir("%CHARM_SITEPACKAGES_DIR%")
16+
17 from tenacity import retry
18 from tenacity.retry import retry_if_exception_type
19 from tenacity.stop import stop_after_delay
20diff --git a/src/reactive/graylog.py b/src/reactive/graylog.py
21index f6199f2..640e262 100644
22--- a/src/reactive/graylog.py
23+++ b/src/reactive/graylog.py
24@@ -5,6 +5,7 @@ import os
25 import re
26 import socket
27 import subprocess
28+import sys
29 import time
30 from pathlib import Path
31 from urllib.parse import urlparse
32@@ -1329,10 +1330,26 @@ def configure_nagios(nagios):
33 perms=0o640,
34 )
35 nagios_plugins = "/usr/local/lib/nagios/plugins/"
36+ sitepackages_dir = os.path.realpath(
37+ os.path.join(
38+ hookenv.charm_dir(),
39+ "../.venv/lib/python{major}.{minor}/site-packages".format(
40+ major=sys.version_info.major, minor=sys.version_info.minor
41+ ),
42+ )
43+ )
44 for f in ("files/check_graylog_health.py", "lib/charms/layer/graylog/api.py"):
45- with open(f, "rb") as content:
46+ with open(f, "rb") as raw_content:
47 fname = os.path.join(nagios_plugins, os.path.basename(f))
48- host.write_file(fname, content.read(), perms=0o755)
49+ # see lib/charms/layer/graylog/api.py
50+ # on any other files, this operation is expected to be a no-op.
51+ content = (
52+ raw_content.read()
53+ .decode()
54+ .replace("%CHARM_SITEPACKAGES_DIR%", sitepackages_dir)
55+ .encode()
56+ )
57+ host.write_file(fname, content, perms=0o755)
58 nrpe_setup.add_check(
59 shortname="graylog_health",
60 description="Graylog Health check",

Subscribers

People subscribed via source and target branches

to all changes: