2013-03-01 04:39:01 +04:00
from django . db import models
2013-03-14 00:29:51 +04:00
from django . db . models import CASCADE , SET_NULL , PROTECT
2013-03-14 01:57:25 +04:00
from django . utils . translation import ugettext_lazy as _
2013-03-01 04:39:01 +04:00
2013-03-14 00:29:51 +04:00
# TODO: jobs and events model TBD
# TODO: reporting model TBD
2013-03-13 21:09:36 +04:00
2013-03-01 04:39:01 +04:00
class CommonModel ( models . Model ) :
2013-03-13 21:09:36 +04:00
'''
common model for all object types that have these standard fields
'''
2013-03-01 04:39:01 +04:00
class Meta :
abstract = True
2013-03-13 21:09:36 +04:00
name = models . TextField ( )
description = models . TextField ( )
2013-03-01 04:39:01 +04:00
creation_date = models . DateField ( )
2013-03-13 23:15:35 +04:00
tags = models . ManyToManyField ( ' Tag ' , related_name = ' %(class)s _tags ' )
audit_trail = models . ManyToManyField ( ' AuditTrail ' , related_name = ' %(class)s _audit_trails ' )
2013-03-14 00:29:51 +04:00
active = models . BooleanField ( default = True )
2013-03-13 23:15:35 +04:00
class Tag ( models . Model ) :
'''
any type of object can be given a search tag
'''
2013-03-14 00:29:51 +04:00
class Meta :
app_label = ' main '
2013-03-13 21:09:36 +04:00
2013-03-13 23:15:35 +04:00
name = models . TextField ( )
2013-03-13 21:09:36 +04:00
class AuditTrail ( CommonModel ) :
'''
changing any object records the change
'''
2013-03-14 00:29:51 +04:00
class Meta :
app_label = ' main '
2013-03-13 21:09:36 +04:00
resource_type = models . TextField ( )
2013-03-14 00:29:51 +04:00
modified_by = models . ForeignKey ( ' User ' , on_delete = SET_NULL , null = True )
2013-03-13 23:15:35 +04:00
delta = models . TextField ( ) # FIXME: switch to JSONField
2013-03-13 21:09:36 +04:00
detail = models . TextField ( )
comment = models . TextField ( )
2013-03-14 00:29:51 +04:00
tag = models . ForeignKey ( ' Tag ' , on_delete = SET_NULL , null = True )
2013-03-01 04:39:01 +04:00
2013-03-13 21:09:36 +04:00
class Organization ( CommonModel ) :
'''
organizations are the basic unit of multi - tenancy divisions
'''
2013-03-14 00:29:51 +04:00
class Meta :
app_label = ' main '
2013-03-13 21:09:36 +04:00
2013-03-13 23:15:35 +04:00
users = models . ManyToManyField ( ' User ' , related_name = ' organizations ' )
admins = models . ManyToManyField ( ' User ' , related_name = ' admin_of_organizations ' )
projects = models . ManyToManyField ( ' Project ' , related_name = ' organizations ' )
2013-03-13 21:09:36 +04:00
class Inventory ( CommonModel ) :
'''
an inventory source contains lists and hosts .
'''
2013-03-14 00:29:51 +04:00
class Meta :
app_label = ' main '
2013-03-14 01:57:25 +04:00
verbose_name_plural = _ ( ' inventories ' )
2013-03-13 21:09:36 +04:00
2013-03-14 00:29:51 +04:00
organization = models . ForeignKey ( Organization , null = True , on_delete = SET_NULL , related_name = ' inventories ' )
2013-03-13 21:09:36 +04:00
class Host ( CommonModel ) :
'''
A managed node
'''
2013-03-14 00:29:51 +04:00
class Meta :
app_label = ' main '
2013-03-13 21:09:36 +04:00
2013-03-14 00:29:51 +04:00
inventory = models . ForeignKey ( ' Inventory ' , null = True , on_delete = SET_NULL , related_name = ' hosts ' )
2013-03-13 21:09:36 +04:00
class Group ( CommonModel ) :
'''
A group of managed nodes . May belong to multiple groups
'''
2013-03-14 00:29:51 +04:00
class Meta :
app_label = ' main '
2013-03-13 21:09:36 +04:00
2013-03-14 00:29:51 +04:00
inventory = models . ForeignKey ( ' Inventory ' , null = True , on_delete = SET_NULL , related_name = ' groups ' )
2013-03-13 23:15:35 +04:00
parents = models . ManyToManyField ( ' self ' , related_name = ' children ' )
2013-03-13 21:09:36 +04:00
2013-03-13 23:15:35 +04:00
# FIXME: audit nullables
# FIXME: audit cascades
2013-03-13 21:09:36 +04:00
2013-03-13 23:15:35 +04:00
class VariableData ( CommonModel ) :
2013-03-13 21:09:36 +04:00
'''
2013-03-13 23:15:35 +04:00
A set of host or group variables
2013-03-13 21:09:36 +04:00
'''
2013-03-14 00:29:51 +04:00
class Meta :
app_label = ' main '
2013-03-14 01:57:25 +04:00
verbose_name_plural = _ ( ' variable data ' )
2013-03-13 21:09:36 +04:00
2013-03-14 00:29:51 +04:00
host = models . ForeignKey ( ' Host ' , null = True , default = None , blank = True , on_delete = CASCADE , related_name = ' variable_data ' )
group = models . ForeignKey ( ' Group ' , null = True , default = None , blank = True , on_delete = CASCADE , related_name = ' variable_data ' )
2013-03-13 23:15:35 +04:00
data = models . TextField ( ) # FIXME: JsonField
2013-03-13 21:09:36 +04:00
class User ( CommonModel ) :
'''
Basic user class
'''
2013-03-14 00:29:51 +04:00
class Meta :
app_label = ' main '
2013-03-13 21:09:36 +04:00
# FIXME: how to integrate with Django auth?
2013-03-13 23:15:35 +04:00
auth_user = models . OneToOneField ( ' auth.User ' , related_name = ' application_user ' )
2013-03-13 21:09:36 +04:00
class Credential ( CommonModel ) :
'''
A credential contains information about how to talk to a remote set of hosts
Usually this is a SSH key location , and possibly an unlock password .
If used with sudo , a sudo password should be set if required .
'''
2013-03-14 00:29:51 +04:00
class Meta :
app_label = ' main '
2013-03-13 21:09:36 +04:00
2013-03-14 00:29:51 +04:00
user = models . ForeignKey ( ' User ' , null = True , default = None , blank = True , on_delete = SET_NULL , related_name = ' credentials ' )
project = models . ForeignKey ( ' Project ' , null = True , default = None , blank = True , on_delete = SET_NULL , related_name = ' credentials ' )
team = models . ForeignKey ( ' Team ' , null = True , default = None , blank = True , on_delete = SET_NULL , related_name = ' credentials ' )
2013-03-13 23:15:35 +04:00
ssh_key_path = models . TextField ( blank = True , default = ' ' )
ssh_key_data = models . TextField ( blank = True , default = ' ' ) # later
ssh_key_unlock = models . TextField ( blank = True , default = ' ' )
ssh_password = models . TextField ( blank = True , default = ' ' )
sudo_password = models . TextField ( blank = True , default = ' ' )
2013-03-13 21:09:36 +04:00
class Team ( CommonModel ) :
'''
A team is a group of users that work on common projects .
'''
2013-03-14 00:29:51 +04:00
class Meta :
app_label = ' main '
2013-03-13 23:15:35 +04:00
projects = models . ManyToManyField ( ' Project ' , related_name = ' teams ' )
users = models . ManyToManyField ( ' User ' , related_name = ' teams ' )
organization = models . ManyToManyField ( ' Organization ' , related_name = ' teams ' )
2013-03-13 21:09:36 +04:00
class Project ( CommonModel ) :
'''
A project represents a playbook git repo that can access a set of inventories
'''
2013-03-14 00:29:51 +04:00
class Meta :
app_label = ' main '
2013-03-13 21:09:36 +04:00
2013-03-13 23:15:35 +04:00
inventories = models . ManyToManyField ( ' Inventory ' , related_name = ' projects ' )
2013-03-13 21:09:36 +04:00
local_repository = models . TextField ( )
scm_type = models . TextField ( )
default_playbook = models . TextField ( )
class Permission ( CommonModel ) :
'''
A permission allows a user , project , or team to be able to use an inventory source .
'''
2013-03-14 00:29:51 +04:00
class Meta :
app_label = ' main '
2013-03-13 21:09:36 +04:00
2013-03-14 00:29:51 +04:00
user = models . ForeignKey ( ' User ' , null = True , on_delete = SET_NULL , related_name = ' permissions ' )
project = models . ForeignKey ( ' Project ' , null = True , on_delete = SET_NULL , related_name = ' permissions ' )
team = models . ForeignKey ( ' Team ' , null = True , on_delete = SET_NULL , related_name = ' permissions ' )
2013-03-13 23:15:35 +04:00
job_type = models . TextField ( )
2013-03-13 21:09:36 +04:00
2013-03-13 23:15:35 +04:00
# TODO: other job types (later)
2013-03-13 21:09:36 +04:00
class LaunchJob ( CommonModel ) :
2013-03-13 23:15:35 +04:00
'''
a launch job is a request to apply a project to an inventory source with a given credential
'''
2013-03-14 00:29:51 +04:00
class Meta :
app_label = ' main '
2013-03-13 21:09:36 +04:00
2013-03-14 00:29:51 +04:00
inventory = models . ForeignKey ( ' Inventory ' , on_delete = SET_NULL , null = True , default = None , blank = True , related_name = ' launch_jobs ' )
credential = models . ForeignKey ( ' Credential ' , on_delete = SET_NULL , null = True , default = None , blank = True , related_name = ' launch_jobs ' )
project = models . ForeignKey ( ' Project ' , on_delete = SET_NULL , null = True , default = None , blank = True , related_name = ' launch_jobs ' )
user = models . ForeignKey ( ' User ' , on_delete = SET_NULL , null = True , default = None , blank = True , related_name = ' launch_jobs ' )
2013-03-13 21:09:36 +04:00
job_type = models . TextField ( )
2013-03-13 23:15:35 +04:00
2013-03-13 21:09:36 +04:00
# TODO: Events
2013-03-01 04:39:01 +04:00
2013-03-13 23:15:35 +04:00
class LaunchJobStatus ( CommonModel ) :
2013-03-14 00:29:51 +04:00
class Meta :
app_label = ' main '
2013-03-14 01:57:25 +04:00
verbose_name_plural = _ ( ' launch job statuses ' )
2013-03-13 23:15:35 +04:00
2013-03-14 00:29:51 +04:00
launch_job = models . ForeignKey ( ' LaunchJob ' , null = True , on_delete = SET_NULL , related_name = ' launch_job_statuses ' )
2013-03-13 23:15:35 +04:00
status = models . IntegerField ( )
result_data = models . TextField ( )
# TODO: reporting (MPD)
2013-03-01 04:39:01 +04:00