/
buildbot_net_usage_data.py
227 lines (192 loc) · 7.57 KB
/
buildbot_net_usage_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members
"""
This files implement buildbotNetUsageData options
It uses urllib2 instead of requests in order to avoid requiring another dependency for statistics feature.
urllib2 supports http_proxy already urllib2 is blocking and thus everything is done from a thread.
"""
from __future__ import absolute_import
from __future__ import print_function
from future.moves.urllib import error as urllib_error
from future.moves.urllib import request as urllib_request
import hashlib
import inspect
import json
import os
import platform
import socket
from twisted.internet import threads
from twisted.python import log
from buildbot.process.buildstep import _BuildStepFactory
from buildbot.util import unicode2bytes
from buildbot.www.config import IndexResource
# This can't change! or we will need to make sure we are compatible with all
# released version of buildbot >=0.9.0
PHONE_HOME_URL = "https://events.buildbot.net/events/phone_home"
def linux_distribution():
os_release = "/etc/os-release"
meta_data = {'ID': "unknown_linux", 'VERSION_ID': "unknown_version"}
if os.path.exists(os_release):
with open("/etc/os-release") as f:
for line in f:
try:
k, v = line.strip().split("=")
meta_data[k] = v.strip('""')
except Exception:
pass
return meta_data['ID'], meta_data['VERSION_ID']
def get_distro():
system = platform.system()
if system == "Linux":
dist = linux_distribution()
return "{}:{}".format(dist[0], dist[1])
elif system == "Windows":
dist = platform.win32_ver()
return "{}:{}".format(dist[0], dist[1])
elif system == "Java":
dist = platform.java_ver()
return "{}:{}".format(dist[0], dist[1])
elif system == "Darwin":
dist = platform.mac_ver()
return "{}".format(dist[0])
else:
return ":".join(platform.uname()[0:1])
def getName(obj):
"""This method finds the first parent class which is within the buildbot namespace
it prepends the name with as many ">" as the class is subclassed
"""
# elastic search does not like '.' in dict keys, so we replace by /
def sanitize(name):
return name.replace(".", "/")
if isinstance(obj, _BuildStepFactory):
klass = obj.factory
else:
klass = type(obj)
name = ""
klasses = (klass, ) + inspect.getmro(klass)
for klass in klasses:
if hasattr(klass, "__module__") and klass.__module__.startswith("buildbot."):
return sanitize(name + klass.__module__ + "." + klass.__name__)
else:
name += ">"
return sanitize(type(obj).__name__)
def countPlugins(plugins_uses, lst):
if isinstance(lst, dict):
lst = lst.values()
for i in lst:
name = getName(i)
plugins_uses.setdefault(name, 0)
plugins_uses[name] += 1
def basicData(master):
plugins_uses = {}
countPlugins(plugins_uses, master.config.workers)
countPlugins(plugins_uses, master.config.builders)
countPlugins(plugins_uses, master.config.schedulers)
countPlugins(plugins_uses, master.config.services)
countPlugins(plugins_uses, master.config.change_sources)
for b in master.config.builders:
countPlugins(plugins_uses, b.factory.steps)
# we hash the master's name + various other master dependent variables
# to get as much as possible an unique id
# we hash it to not leak private information about the installation such as hostnames and domain names
hashInput = (
master.name + # master name contains hostname + master basepath
socket.getfqdn() # we add the fqdn to account for people
# call their buildbot host 'buildbot'
# and install it in /var/lib/buildbot
)
hashInput = unicode2bytes(hashInput)
installid = hashlib.sha1(hashInput).hexdigest()
return {
'installid': installid,
'versions': dict(IndexResource.getEnvironmentVersions()),
'platform': {
'platform': platform.platform(),
'system': platform.system(),
'machine': platform.machine(),
'processor': platform.processor(),
'python_implementation': platform.python_implementation(),
# xBSD including osx will disclose too much information after [4] like where it was built
'version': " ".join(platform.version().split(' ')[:4]),
'distro': get_distro()
},
'plugins': plugins_uses,
'db': master.config.db['db_url'].split("://")[0],
'mq': master.config.mq['type'],
'www_plugins': list(master.config.www['plugins'].keys())
}
def fullData(master):
"""
Send the actual configuration of the builders, how the steps are agenced.
Note that full data will never send actual detail of what command is run, name of servers, etc.
"""
builders = []
for b in master.config.builders:
steps = []
for step in b.factory.steps:
steps.append(getName(step))
builders.append(steps)
return {'builders': builders}
def computeUsageData(master):
if master.config.buildbotNetUsageData is None:
return
data = basicData(master)
if master.config.buildbotNetUsageData != "basic":
data.update(fullData(master))
if callable(master.config.buildbotNetUsageData):
data = master.config.buildbotNetUsageData(data)
return data
def _sendWithUrlib(url, data):
data = json.dumps(data).encode()
clen = len(data)
req = urllib_request.Request(url, data, {
'Content-Type': 'application/json',
'Content-Length': clen
})
try:
f = urllib_request.urlopen(req)
except urllib_error.URLError:
return None
res = f.read()
f.close()
return res
def _sendWithRequests(url, data):
try:
import requests
except ImportError:
return None
r = requests.post(url, json=data)
return r.text
def _sendBuildbotNetUsageData(data):
log.msg("buildbotNetUsageData: sending {}".format(data))
# first try with requests, as this is the most stable http library
res = _sendWithRequests(PHONE_HOME_URL, data)
# then we try with stdlib, which not always work with https
if res is None:
res = _sendWithUrlib(PHONE_HOME_URL, data)
# at last stage
if res is None:
log.msg("buildbotNetUsageData: Could not send using https, "
"please `pip install 'requests[security]'` for proper SSL implementation`")
data['buggySSL'] = True
res = _sendWithUrlib(PHONE_HOME_URL.replace("https://", "http://"), data)
log.msg("buildbotNetUsageData: buildbot.net said:", res)
def sendBuildbotNetUsageData(master):
if master.config.buildbotNetUsageData is None:
return
data = computeUsageData(master)
if data is None:
return
threads.deferToThread(_sendBuildbotNetUsageData, data)