Skip to content

Commit e19cf6c

Browse files
authored
Make scanners use kibble.ini instead of config.yaml (#122)
This PR moved watson, azure, picoapi and git configuration to kibble.ini config file. It fixes also reading ES configuration. In this way all Kibble configuration is in single place.
1 parent f8d731c commit e19cf6c

14 files changed

+324
-337
lines changed

kibble.ini

+20
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ scratchdir = /tmp
2525
# each node will gat 1/4th of all jobs to work on.
2626
balance =
2727

28+
[git]
29+
# Comma-separated branch names
30+
wanted_branches =
31+
32+
# Watson/BlueMix configuration for sentiment analysis, if applicable
33+
[watson]
34+
username =
35+
password =
36+
api = https://gateway-location.watsonplatform.net/tone-analyzer/api
37+
38+
# Azure Text Analysis API configuration, if applicable
39+
[azure]
40+
apikey =
41+
location = west-us
42+
43+
# picoAPI Text Analysis configuration
44+
[picoapi]
45+
key =
46+
47+
2848
[elasticsearch]
2949
# Elasticsearch database name
3050
dbname = kibble

kibble/scanners/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ The Kibble Scanners collect information for the Kibble Suite.
33

44
## Setup instructions:
55

6-
- Edit conf/config.yaml to match your Kibble service
6+
- Edit kibble.ini to match your Kibble service
77

88
## How to run:
99

kibble/scanners/brokers/kibbleES.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,13 @@ class KibbleBit:
119119
""" KibbleBit class with direct ElasticSearch access """
120120

121121
def __init__(self, broker, organisation, tid):
122-
self.config = broker.config
123122
self.organisation = organisation
124123
self.broker = broker
125124
self.json_queue = []
126125
self.queueMax = 1000 # Entries to keep before bulk pushing
127126
self.pluginname = ""
128127
self.tid = tid
129-
self.dbname = self.broker.config["elasticsearch"]["database"]
128+
self.dbname = conf.get("elasticsearch", "database")
130129

131130
def __del__(self):
132131
""" On unload/delete, push the last chunks of data to ES """
@@ -144,7 +143,7 @@ def pprint(self, string, err=False):
144143
def update_source(self, source):
145144
""" Updates a source document, usually with a status update """
146145
self.broker.DB.index(
147-
index=self.broker.config["elasticsearch"]["database"],
146+
index=self.dbname,
148147
doc_type="source",
149148
id=source["sourceID"],
150149
body=source,
@@ -153,7 +152,7 @@ def update_source(self, source):
153152
def get(self, doctype, docid):
154153
""" Fetches a document from the DB """
155154
doc = self.broker.DB.get(
156-
index=self.broker.config["elasticsearch"]["database"],
155+
index=self.dbname,
157156
doc_type=doctype,
158157
id=docid,
159158
)
@@ -164,14 +163,14 @@ def get(self, doctype, docid):
164163
def exists(self, doctype, docid):
165164
""" Checks whether a document already exists or not """
166165
return self.broker.DB.exists(
167-
index=self.broker.config["elasticsearch"]["database"],
166+
index=self.dbname,
168167
doc_type=doctype,
169168
id=docid,
170169
)
171170

172171
def index(self, doctype, docid, document):
173172
""" Adds a new document to the index """
174-
dbname = self.broker.config["elasticsearch"]["database"]
173+
dbname = self.dbname
175174
self.broker.DB.index(index=dbname, doc_type=doctype, id=docid, body=document)
176175

177176
def append(self, t, doc):
@@ -195,7 +194,7 @@ def bulk(self):
195194
js = entry
196195
doc = js
197196
js["@version"] = 1
198-
dbname = self.broker.config["elasticsearch"]["database"]
197+
dbname = self.dbname
199198
if self.broker.noTypes:
200199
dbname += "_%s" % js["doctype"]
201200
js_arr.append(
@@ -233,6 +232,7 @@ def __init__(self, broker, org):
233232

234233
self.broker = broker
235234
self.id = org
235+
self.dbname = conf.get("elasticsearch", "database")
236236

237237
def sources(self, sourceType=None, view=None):
238238
""" Get all sources or sources of a specific type for an org """
@@ -241,7 +241,7 @@ def sources(self, sourceType=None, view=None):
241241
mustArray = [{"term": {"organisation": self.id}}]
242242
if view:
243243
res = self.broker.DB.get(
244-
index=self.broker.config["elasticsearch"]["database"],
244+
index=self.dbname,
245245
doc_type="view",
246246
id=view,
247247
)
@@ -252,7 +252,7 @@ def sources(self, sourceType=None, view=None):
252252
mustArray.append({"term": {"type": sourceType}})
253253
# Run the search, fetch all results, 9999 max. TODO: Scroll???
254254
res = self.broker.DB.search(
255-
index=self.broker.config["elasticsearch"]["database"],
255+
index=self.dbname,
256256
doc_type="source",
257257
size=9999,
258258
body={"query": {"bool": {"must": mustArray}}, "sort": {"sourceURL": "asc"}},

kibble/scanners/config.yaml

-39
This file was deleted.

kibble/scanners/scanners/git-census.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import tempfile
2525
import time
2626

27+
from kibble.configuration import conf
28+
2729
title = "Census Scanner for Git"
2830
version = "0.1.0"
2931

@@ -50,7 +52,7 @@ def scan(kibble_bit, source):
5052
rid = source["sourceID"]
5153
url = source["sourceURL"]
5254
rootpath = "%s/%s/git" % (
53-
kibble_bit.config["scanner"]["scratchdir"],
55+
conf.get("scanner", "scratchdir"),
5456
source["organisation"],
5557
)
5658
gpath = os.path.join(rootpath, rid)

kibble/scanners/scanners/git-evolution.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import subprocess
2424
import time
2525

26+
from kibble.configuration import conf
2627
from kibble.scanners.utils import sloc
2728

2829
title = "Git Evolution Scanner"
@@ -138,7 +139,7 @@ def scan(kibble_bit, source):
138139

139140
rid = source["sourceID"]
140141
rootpath = "%s/%s/git" % (
141-
kibble_bit.config["scanner"]["scratchdir"],
142+
conf.get("scanner", "scratchdir"),
142143
source["organisation"],
143144
)
144145
gpath = os.path.join(rootpath, rid)
@@ -158,7 +159,7 @@ def scan(kibble_bit, source):
158159
rid = source["sourceID"]
159160
url = source["sourceURL"]
160161
rootpath = "%s/%s/git" % (
161-
kibble_bit.config["scanner"]["scratchdir"],
162+
conf.get("scanner", "scratchdir"),
162163
source["organisation"],
163164
)
164165
gpath = os.path.join(rootpath, rid)

kibble/scanners/scanners/git-sloc.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import subprocess
2020
import time
2121

22+
from kibble.configuration import conf
2223
from kibble.scanners.utils import git, sloc
2324

2425
""" Source Lines of Code counter for Git """
@@ -42,7 +43,7 @@ def scan(kibble_bit, source):
4243
rid = source["sourceID"]
4344
url = source["sourceURL"]
4445
rootpath = "%s/%s/git" % (
45-
kibble_bit.config["scanner"]["scratchdir"],
46+
conf.get("scanner", "scratchdir"),
4647
source["organisation"],
4748
)
4849
gpath = os.path.join(rootpath, rid)
@@ -58,7 +59,7 @@ def scan(kibble_bit, source):
5859
kibble_bit.update_source(source)
5960

6061
try:
61-
branch = git.defaultBranch(source, gpath)
62+
branch = git.default_branch(source, gpath)
6263
subprocess.call("cd %s && git checkout %s" % (gpath, branch), shell=True)
6364
except: # pylint: disable=bare-except
6465
kibble_bit.pprint("SLoC counter failed to find main branch for %s!!" % url)

kibble/scanners/scanners/git-sync.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import subprocess
2020
import time
2121

22+
from kibble.configuration import conf
2223
from kibble.scanners.utils import git
2324

2425
title = "Sync plugin for Git repositories"
@@ -41,7 +42,7 @@ def scan(kibble_bit, source):
4142
path = source["sourceID"]
4243
url = source["sourceURL"]
4344
rootpath = "%s/%s/git" % (
44-
kibble_bit.config["scanner"]["scratchdir"],
45+
conf.get("scanner", "scratchdir"),
4546
source["organisation"],
4647
)
4748

@@ -79,7 +80,7 @@ def scan(kibble_bit, source):
7980
kibble_bit.pprint("Repo %s exists, fetching changes..." % datapath)
8081

8182
# Do we have a default branch here?
82-
branch = git.defaultBranch(source, datapath, kibble_bit)
83+
branch = git.default_branch(source, datapath)
8384
if len(branch) == 0:
8485
source["default_branch"] = branch
8586
source["steps"]["sync"] = {

kibble/scanners/scanners/ponymail-kpe.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import time
2020

2121
from kibble.scanners.utils import jsonapi, kpe
22+
from kibble.settings import AZURE_ENABLED, PICOAPI_ENABLED, WATSON_ENABLED
2223

2324
"""
2425
This is a Kibble scanner plugin for Apache Pony Mail sources.
@@ -64,7 +65,7 @@ def scan(kibble_bit, source):
6465
kibble_bit.update_source(source)
6566
return
6667

67-
if not "azure" in kibble_bit.config and not "picoapi" in kibble_bit.config:
68+
if not AZURE_ENABLED and not PICOAPI_ENABLED:
6869
kibble_bit.pprint(
6970
"No Azure/picoAPI creds configured, skipping key phrase extraction"
7071
)
@@ -110,12 +111,12 @@ def scan(kibble_bit, source):
110111
bodies.append(body)
111112
if bodies:
112113
KPEs = None
113-
if "watson" in kibble_bit.config:
114+
if WATSON_ENABLED:
114115
pass # Haven't written this yet
115-
elif "azure" in kibble_bit.config:
116-
KPEs = kpe.azureKPE(kibble_bit, bodies)
117-
elif "picoapi" in kibble_bit.config:
118-
KPEs = kpe.picoKPE(kibble_bit, bodies)
116+
elif AZURE_ENABLED:
117+
KPEs = kpe.azure_kpe(kibble_bit, bodies)
118+
elif PICOAPI_ENABLED:
119+
KPEs = kpe.pico_kpe(kibble_bit, bodies)
119120
if not KPEs:
120121
kibble_bit.pprint("Hit rate limit, not trying further emails for now.")
121122

kibble/scanners/scanners/ponymail-tone.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import re
2222
import time
2323

24+
from kibble.configuration import conf
2425
from kibble.scanners.utils import jsonapi, tone
26+
from kibble.settings import AZURE_ENABLED, PICOAPI_ENABLED, WATSON_ENABLED
2527

2628
title = "Tone/Mood Scanner plugin for Apache Pony Mail"
2729
version = "0.1.0"
@@ -61,11 +63,7 @@ def scan(kibble_bit, source):
6163
kibble_bit.update_source(source)
6264
return
6365

64-
if (
65-
not "watson" in kibble_bit.config
66-
and not "azure" in kibble_bit.config
67-
and not "picoapi" in kibble_bit.config
68-
):
66+
if not WATSON_ENABLED and not AZURE_ENABLED and not PICOAPI_ENABLED:
6967
kibble_bit.pprint(
7068
"No Watson/Azure/picoAPI creds configured, skipping tone analyzer"
7169
)
@@ -110,12 +108,12 @@ def scan(kibble_bit, source):
110108
bodies.append(body)
111109
if bodies:
112110
moods = None
113-
if "watson" in kibble_bit.config:
114-
moods = tone.watsonTone(kibble_bit, bodies)
115-
elif "azure" in kibble_bit.config:
116-
moods = tone.azureTone(kibble_bit, bodies)
117-
elif "picoapi" in kibble_bit.config:
118-
moods = tone.picoTone(kibble_bit, bodies)
111+
if WATSON_ENABLED:
112+
moods = tone.watson_tone(kibble_bit, bodies)
113+
elif AZURE_ENABLED:
114+
moods = tone.azure_tone(kibble_bit, bodies)
115+
elif PICOAPI_ENABLED:
116+
moods = tone.pico_tone(kibble_bit, bodies)
119117
if not moods:
120118
kibble_bit.pprint("Hit rate limit, not trying further emails for now.")
121119

kibble/scanners/utils/git.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,20 @@
1717

1818
""" This is the Kibble git utility plugin """
1919

20-
import os
2120
import re
2221
import subprocess
23-
import sys
2422

23+
from kibble.configuration import conf
2524

26-
def defaultBranch(source, datapath, KibbleBit=None):
25+
26+
def default_branch(source, datapath):
2727
""" Tries to figure out what the main branch of a repo is """
28-
wanted_branches = ["master", "main", "trunk"]
29-
branch = ""
3028
# If we have an override of branches we like, use 'em
31-
if KibbleBit and KibbleBit.config.get("git"):
32-
wanted_branches = KibbleBit.config["git"].get(
33-
"wanted_branches", wanted_branches
34-
)
29+
wanted_branches = conf.get("git", "wanted_branches", fallback=None)
30+
if wanted_branches:
31+
wanted_branches = wanted_branches.split(",")
32+
else:
33+
wanted_branches = ["master", "main", "trunk"]
3534

3635
# For each wanted branch, in order, look for it in our clone,
3736
# and return the name if found.

0 commit comments

Comments
 (0)