Merge lp:~james-page/charm-helpers/lp1537155 into lp:charm-helpers

Proposed by James Page
Status: Merged
Merged at revision: 525
Proposed branch: lp:~james-page/charm-helpers/lp1537155
Merge into: lp:charm-helpers
Diff against target: 73 lines (+30/-7)
3 files modified
charmhelpers/contrib/openstack/context.py (+11/-7)
test_requirements.txt (+1/-0)
tests/contrib/openstack/test_os_contexts.py (+18/-0)
To merge this branch: bzr merge lp:~james-page/charm-helpers/lp1537155
Reviewer Review Type Date Requested Status
Liam Young (community) Approve
Review via email: mp+284411@code.launchpad.net

Description of the change

Support number of cpu detection on Ubuntu 16.04

The version of psutil in 16.04 does not have NUM_CPUS.

Use the new cpu_count function if present, otherwise use NUM_CPUS.

To post a comment you must log in.
Revision history for this message
Liam Young (gnuoy) wrote :

Approved

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/contrib/openstack/context.py'
2--- charmhelpers/contrib/openstack/context.py 2016-01-06 22:04:12 +0000
3+++ charmhelpers/contrib/openstack/context.py 2016-01-29 09:25:13 +0000
4@@ -90,6 +90,12 @@
5 from charmhelpers.contrib.openstack.utils import get_host_ip
6 from charmhelpers.core.unitdata import kv
7
8+try:
9+ import psutil
10+except ImportError:
11+ apt_install('python-psutil', fatal=True)
12+ import psutil
13+
14 CA_CERT_PATH = '/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt'
15 ADDRESS_TYPES = ['admin', 'internal', 'public']
16
17@@ -1258,13 +1264,11 @@
18
19 @property
20 def num_cpus(self):
21- try:
22- from psutil import NUM_CPUS
23- except ImportError:
24- apt_install('python-psutil', fatal=True)
25- from psutil import NUM_CPUS
26-
27- return NUM_CPUS
28+ # NOTE: use cpu_count if present (16.04 support)
29+ if hasattr(psutil, 'cpu_count'):
30+ return psutil.cpu_count()
31+ else:
32+ return psutil.NUM_CPUS
33
34 def __call__(self):
35 multiplier = config('worker-multiplier') or 0
36
37=== modified file 'test_requirements.txt'
38--- test_requirements.txt 2015-12-11 17:02:13 +0000
39+++ test_requirements.txt 2016-01-29 09:25:13 +0000
40@@ -17,3 +17,4 @@
41 netifaces==0.10 # trusty is 0.8, but using py3 compatible version for tests.
42 Jinja2==2.6 # precise
43 six==1.1 # precise
44+psutil==1.2.1 # trusty
45
46=== modified file 'tests/contrib/openstack/test_os_contexts.py'
47--- tests/contrib/openstack/test_os_contexts.py 2016-01-06 22:04:12 +0000
48+++ tests/contrib/openstack/test_os_contexts.py 2016-01-29 09:25:13 +0000
49@@ -2367,6 +2367,24 @@
50 self.assertEquals(context.NotificationDriverContext()(),
51 {'notifications': 'True'})
52
53+ @patch.object(context, 'psutil')
54+ def test_workerconfig_context_xenial(self, _psutil):
55+ self.config.side_effect = fake_config({
56+ 'worker-multiplier': 1,
57+ })
58+ _psutil.cpu_count.return_value = 4
59+ worker = context.WorkerConfigContext()
60+ self.assertTrue(worker.num_cpus, 4)
61+
62+ @patch.object(context, 'psutil')
63+ def test_workerconfig_context_trusty(self, _psutil):
64+ self.config.side_effect = fake_config({
65+ 'worker-multiplier': 2,
66+ })
67+ _psutil.NUM_CPUS = 4
68+ worker = context.WorkerConfigContext()
69+ self.assertTrue(worker.num_cpus, 2)
70+
71 def test_workerconfig_context_noconfig(self):
72 self.config.return_value = None
73 with patch.object(context.WorkerConfigContext, 'num_cpus', 2):

Subscribers

People subscribed via source and target branches