0

I develop a server component with Python.

I want to nail down the system requirements:

  • MIN_CPU_COUNT
  • MIN_RAM
  • ...

Is there a way (maybe in setup.py) to define something like this?

My software needs at least N CPUs and M RAM?

Why? Because we had trouble in the past because operators moved the server component to a less capable server and we could not ensure the service-level agreement.

guettli
  • 25,042
  • 81
  • 346
  • 663
  • Since you can run arbitrary Python code in `setup.py` it should be possible to perform such checks ([CPUs](http://stackoverflow.com/a/1006337/3005167), [RAM](http://stackoverflow.com/a/22103295/3005167)) directly there and bail out if the conditions are not met. I do not know if such functionality is built into distutils - I guess not, though. – MB-F Apr 12 '16 at 13:51
  • @kazemakase Yes, I could run the check in setup.py. But I like to split it: my program defines the constraints, and some other part is responsible for testing. "unittest" comes to my mind, but here it is not a unittest, it is a system-test. – guettli Apr 12 '16 at 14:04

1 Answers1

1

I implemented it this way.

Feedback welcome

from django.conf import settings
import psutil
import humanfriendly
from djangotools.utils import testutils

class Check(testutils.Check):

    @testutils.skip_if_not_prod
    def test_min_cpu_count(self):
        min_cpu_count=getattr(settings, 'MIN_CPU_COUNT', None)
        self.assertIsNotNone(min_cpu_count, 'settings.MIN_CPU_COUNT not set. Please supply a value.')
        self.assertLessEqual(min_cpu_count, psutil.cpu_count())

    @testutils.skip_if_not_prod
    def test_min_physical_memory(self):
        min_physical_memory_orig=getattr(settings, 'MIN_PHYSICAL_MEMORY', None)
        self.assertIsNotNone(min_physical_memory_orig, "settings.MIN_PHYSICAL_MEMORY not set. Please supply a value. Example: MIN_PHYSICAL_MEMORY='4G'")
        min_physical_memory_bytes=humanfriendly.parse_size(min_physical_memory_orig)
        self.longMessage=False
        self.assertLessEqual(min_physical_memory_bytes, psutil.virtual_memory().total, 'settings.MIN_PHYSICAL_MEMORY=%r is not satisfied. Total virtual memory of current hardware: %r' % (
            min_physical_memory_orig, humanfriendly.format_size(psutil.virtual_memory().total)))
guettli
  • 25,042
  • 81
  • 346
  • 663