forked from Azimut-Prod/azimut-gestion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
168 lines (109 loc) · 4.83 KB
/
models.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
from django.db import models
from django.conf import settings
import hashlib
from main.models import User
class Server(models.Model):
name = models.CharField(max_length=255)
keymanger_name = models.CharField(max_length=255, unique=True)
is_vm = models.BooleanField()
external_ip = models.IPAddressField(blank=True, null=True)
internal_ip = models.IPAddressField(blank=True, null=True)
vm_host = models.ForeignKey('Server', blank=True, null=True, related_name='server_set')
ngnix_server = models.ForeignKey('Server', blank=True, null=True, related_name='ngnixed_server_set')
mysql_server = models.ForeignKey('Server', blank=True, null=True, related_name='mysqled_server_set')
ssh_connection_string_from_gestion = models.CharField(max_length=255, blank=True, null=True)
ssh_connection_string_from_backup = models.CharField(max_length=255, blank=True, null=True)
external_interface = models.CharField(max_length=15, blank=True, null=True)
is_proxmox = models.BooleanField()
proxmox_node_name = models.CharField(max_length=255, blank=True, null=True, default='')
external_hostname_for_vms_creation = models.CharField(max_length=255, blank=True, null=True)
hostname_for_vms_creation = models.CharField(max_length=255, blank=True, null=True, default='')
samba_management = models.BooleanField(default=False)
samba_base_folder = models.CharField(max_length=255, blank=True, null=True, default='')
logstash_shipper = models.BooleanField(default=False)
bind_server = models.BooleanField(default=False, help_text='Include a bind server')
users_owning_the_server = models.ManyToManyField(User, blank=True, null=True, help_text='Keys of users will be allowed and he will be able do display the server page details')
notes = models.TextField(blank=True, null=True)
def all_ports(self):
"""Return all ports (forwarded from and to)"""
liste = []
for port in self.portstoforward.all():
liste.append(port)
for port in self.portsforwared.all():
liste.append(port)
return liste
def all_hosts(self):
"""Return all hosts (forwarded from and to)"""
liste = []
for host in self.hoststoforward.all():
liste.append(host)
for host in self.hostforwarded.all():
liste.append(host)
return liste
def __unicode__(self):
return self.name
def ip_for_proxmox(self):
"""Return IP to use for proxmox."""
return self.ssh_connection_string_from_gestion.split('@')[-1]
def get_host_for_fabric(self):
"""Return the host to use for fabric"""
port = 22
splited_cox = self.ssh_connection_string_from_gestion.split(' ')
if '-p' in splited_cox:
next_is_port = False
for x in splited_cox:
if next_is_port:
next_is_port = False
port = x
if x == '-p':
next_is_port = True
cox = splited_cox[-1]
else:
cox = splited_cox[-1]
return cox + ':' + str(port)
def get_external_port(self):
"""Get the port"""
port = 22
splited_cox = self.ssh_connection_string_from_backup.split(' ')
if '-p' in splited_cox:
next_is_port = False
for x in splited_cox:
if next_is_port:
next_is_port = False
port = x
if x == '-p':
next_is_port = True
return str(port)
def random_proxmox_password(self):
"""Return a unique but hard to guess password for new VMs"""
b = ''
if self.vm_host:
b = str(self.vm_host.pk)
return hashlib.sha224(b + settings.VM_PASS_SECRET + str(self.pk)).hexdigest()
def get_users_list(self):
"""Return the list of users"""
retour = ''
for u in self.serveruser_set.all():
retour += u.name + ','
return retour[:-1]
def last_runs(self):
"""Return the last runs of fabrics scripts"""
return self.task_set.order_by('-creation_date').all()[:10]
def get_vms(self):
return self.server_set.order_by('name').all()
class ServerUser(models.Model):
server = models.ForeignKey(Server)
name = models.CharField(max_length=255)
def __unicode__(self):
return self.name + '@' + self.server.name
class SshKey(models.Model):
server = models.ForeignKey(Server)
user = models.CharField(max_length=255)
key = models.TextField()
def get_comment(self):
bonus = ''
if len(self.key.split(' ')) > 2:
bonus = ' (' + self.key.split(' ')[2].strip() + ')'
return 'Key #' + str(self.pk) + bonus
def __unicode__(self):
return self.user + '@' + self.server.name