mirror of
https://github.com/ansible/awx.git
synced 2026-03-16 00:17:29 -02:30
44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
# Copyright (c) 2015 Ansible, Inc.
|
|
# All Rights Reserved.
|
|
|
|
import sys
|
|
|
|
from django.db import models
|
|
|
|
|
|
class HostManager(models.Manager):
|
|
"""Custom manager class for Hosts model."""
|
|
|
|
def active_count(self):
|
|
"""Return count of active, unique hosts for licensing."""
|
|
try:
|
|
return self.order_by('name').distinct('name').count()
|
|
except NotImplementedError: # For unit tests only, SQLite doesn't support distinct('name')
|
|
return len(set(self.values_list('name', flat=True)))
|
|
|
|
class InstanceManager(models.Manager):
|
|
"""A custom manager class for the Instance model.
|
|
|
|
Provides "table-level" methods including getting the currently active
|
|
instance or role.
|
|
"""
|
|
def me(self):
|
|
"""Return the currently active instance."""
|
|
# If we are running unit tests, return a stub record.
|
|
if len(sys.argv) >= 2 and sys.argv[1] == 'test':
|
|
return self.model(id=1,
|
|
hostname='localhost',
|
|
uuid='00000000-0000-0000-0000-000000000000')
|
|
|
|
# If we can determine the instance we are on then return
|
|
# that, otherwise None which would be the standalone
|
|
# case
|
|
# TODO: Replace, this doesn't work if the hostname
|
|
# is different from the Instance.name
|
|
# node = self.filter(hostname=socket.gethostname())
|
|
return self.all()[0]
|
|
|
|
def my_role(self):
|
|
# NOTE: TODO: Likely to repurpose this once standalone ramparts are a thing
|
|
return "tower"
|