This repository has been archived by the owner on Feb 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodels.py
89 lines (68 loc) · 3 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
from django.db import models
from django.contrib import admin
from django.utils.text import slugify
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from django.utils.safestring import mark_safe
import os, subprocess
# This stuff needs to be configured for your server!!!
WEBAPP_DIR = '/Users/ryan/homebrew/Cellar/tomcat/7.0.39/libexec/webapps'
TOMCAT_MANAGER_PATH = 'localhost:8080/manager/text'
TOMCAT_MANAGER = "tomcat" # must have role "manager-text"
TOMCAT_MANAGER_PASSWORD = "secret"
DOMAIN = "data.usgin.org"
# Don't change this one though
GEOSERVER_WAR_PATH = os.path.join(os.path.dirname(__file__), "geoserver", "geoserver.war")
# ...or these
TOMCAT_DEPLOY_URL = "http://%s:%s@%s/deploy" % (TOMCAT_MANAGER, TOMCAT_MANAGER_PASSWORD, TOMCAT_MANAGER_PATH)
TOMCAT_UNDEPLOY_URL = "http://%s:%s@%s/undeploy" % (TOMCAT_MANAGER, TOMCAT_MANAGER_PASSWORD, TOMCAT_MANAGER_PATH)
class GeoserverInstance(models.Model):
class Meta:
ordering = [ "name" ]
name = models.CharField(max_length=50)
def __unicode__(self):
return slugify(self.name)
def war_name(self):
return "%s.war" % self.__unicode__()
def instance_admin_url(self):
return "http://%s/%s/web/" % (DOMAIN, self.__unicode__())
def instance_admin(self):
return mark_safe("<a href='%s'>%s</a>" % (self.instance_admin_url(), self.instance_admin_url()))
def pretty_instance_url(self):
return mark_safe("<a href='%s'>%s</a>" % (self.instance_admin_url(), self.__unicode__()))
def war_destination(self):
return os.path.join(WEBAPP_DIR, self.war_name())
def copy_war(self):
"""Copy a war file to the WEBAPP_DIR"""
params = [ "cp", GEOSERVER_WAR_PATH, self.war_destination() ]
return subprocess.call(params)
def delete_war(self):
"""Remove a war file from the WEBAPP_DIR"""
params = [ "rm", self.war_destination() ]
return subprocess.call(params)
def deploy_war(self):
"""POST a war file to Tomcat"""
params = [
"curl",
"--upload-file",
GEOSERVER_WAR_PATH,
"%s?path=/%s&update=true" % (TOMCAT_DEPLOY_URL, self.__unicode__())
]
return subprocess.call(params)
def undeploy_war(self):
"""Undeploy a WAR file from Tomcat"""
params = [
"curl",
"%s?path=/%s" %(TOMCAT_UNDEPLOY_URL, self.__unicode__())
]
return subprocess.call(params)
@receiver(post_save, sender=GeoserverInstance)
def instance_instantiator(sender, instance, created, **kwargs):
if created:
out = instance.deploy_war()
@receiver(post_delete, sender=GeoserverInstance)
def instance_deinstantiator(sender, instance, **kwargs):
out = instance.undeploy_war()
class GeoserverInstanceAdmin(admin.ModelAdmin):
list_display = [ '__unicode__', 'instance_admin' ]
admin.site.register(GeoserverInstance, GeoserverInstanceAdmin)