-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsync_kmi.py
191 lines (155 loc) · 8.1 KB
/
sync_kmi.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import os
import requests
from tablemanager.models import Publish,Workspace
from wmsmanager.models import WmsLayer
from livelayermanager.models import Layer,SqlViewLayer,Datasource,WmsServer
from layergroup.models import LayerGroup
def list_borg_layers():
all_layers={"features":{},"wmslayers":{},"livelayers":{},"layergroups":{}}
layers=all_layers["features"]
for p in Publish.objects.filter(status="Enabled",completed_gt=0):
workspacename = p.workspace.name
storename = "{}_ds".format(p.workspace.name)
layers[workspacename] = layers.get(workspacename) or {}
layers[workspacename][storename] = layers[workspacename].get(storename) or []
layers[workspacename][storename].append(p.name)
layers=all_layers["wmslayers"]
for l in WmsLayer.objects.filter(status__in=['Updated', 'Published', 'CascadePublished']):
workspacename = l.server.workspace.name
storename = l.server.name
layers[workspacename] = layers.get(workspacename) or {}
layers[workspacename][storename] = layers[workspacename].get(storename) or []
layers[workspacename][storename].append(l.kmi_name or l.name)
layers=all_layers["livelayers"]
for l in Layer.objects.filter(status__in=['Updated', 'Published', 'CascadePublished']):
workspacename = l.datasource.workspace.name
storename = l.datasource.name
layers[workspacename] = layers.get(workspacename) or {}
layers[workspacename][storename] = layers[workspacename].get(storename) or []
layers[workspacename][storename].append(l.name)
for l in SqlViewLayer.objects.filter(status__in=['Updated', 'Published', 'CascadePublished']):
workspacename = l.datasource.workspace.name
storename = l.datasource.name
layers[workspacename] = layers.get(workspacename) or {}
layers[workspacename][storename] = layers[workspacename].get(storename) or []
layers[workspacename][storename].append(l.name)
"""
layers=all_layers["layergroups"]
for l in LayerGroup.objects.filter(status__in=['Updated', 'Published', 'CascadePublished']):
workspacename = l.workspace.name
layers[workspacename] = layers.get(workspacename) or []
layers[workspacename].append(l.name)
"""
return all_layers
geoserver = os.environ.get('GEOSERVER')
user = os.environ.get('GEOSERVER_USER')
password = os.environ.get('GEOSERVER_password')
def list_kmi_layers():
all_layers={"features":{},"wmslayers":{},"livelayers":{},"layergroups":{}}
res = requests.get("{}/geoserver/rest/workspaces.json".format(geoserver),auth=(user, password))
res.raise_for_status()
workspaces = res.json()
for w in workspaces.get("workspaces",{}).get("workspace",[]):
workspacename = w["name"]
res = requests.get(w["href"],auth=(user, password))
res.raise_for_status()
workspace = res.json()
res = requests.get(workspace["dataStores"],auth=(user, password))
res.raise_for_status()
datastores = res.json()
for s in datastores.get("dataStores",{}).get("dataStore",[]):
storename = s["name"]
res = requests.get(s["href"],auth=(user, password))
res.raise_for_status()
store = res.json()
res = requests.get(store["featureTypes"],auth=(user, password))
res.raise_for_status()
features = res.json()
for f in features.get("featureTypes",{}).get("featureType",[]):
layername = f["name"]
layerurl = f["href"]
all_layers["features"][workspacename] = layers.get(workspacename) or {}
all_layers["features"][workspacename][storename] = layers[workspacename].get(storename) or []
all_layers["features"][workspacename][storename].append(layername)
res = requests.get(workspace["wmsStores"],auth=(user, password))
res.raise_for_status()
wmsstores = res.json()
for s in wmsstores.get("wmsStores",{}).get("wmsStore",[]):
storename = s["name"]
res = requests.get(s["href"],auth=(user, password))
res.raise_for_status()
store = res.json()
store_capabilitiesurl = store["capabilitiesURL"]
store_user = store.get("user")
store_password = store.get("password")
res = requests.get(store["wmsLayers"],auth=(user, password))
res.raise_for_status()
layers = res.json()
for l in layers.get("wmsLayers",{}).get("wmsLayer",[]):
layername = l["name"]
layerurl = l["href"]
all_layers["wmslayers"][workspacename] = layers.get(workspacename) or {}
all_layers["wmslayers"][workspacename][storename] = layers[workspacename].get(storename) or []
all_layers["wmslayers"][workspacename][storename].append(layername)
return all_layers
def sync(dry_run=False):
borg_layers = list_borg_layers()
kmi_layers = list_kmi_layers()
stores_not_in_kmi = []
layers_not_in_kmi = []
for t,data in borg_layers.items():
kmi_data = kmi_layers.get(t) or {}
for workspace,w_data in data.items():
kmi_w_data = kmi_data.get(workspace) or {}
for store,layers in w_data.items():
if store not in kmi_w_data:
stores_not_in_kmi.append((t,workspace,store))
kmi_layers = kmi_w_data.get(store) or []
for layer in layers:
if layer not in kmi_layers:
layers_not_in_kmi.append((t,workspace,store,layer))
stores_not_in_borg = []
layers_not_in_borg = []
for t,data in kmi_layers.items():
borg_data = borg_layers.get(t) or {}
for workspace,w_data in data.items():
borg_w_data = borg_data.get(workspace) or {}
for store,layers in w_data.items():
if store not in borg_w_data:
stores_not_in_borg.append((t,workspace,store))
borg_layers = borg_w_data.get(store) or []
for layer in layers:
if layer not in borg_layers:
layers_not_in_borg.append((t,workspace,store,layer))
if stores_not_in_kmi:
print("The stores({}) are published in borg, but not exist in kmi".format(stores_not_in_kmi))
if layers_not_in_kmi:
print("The layers({}) are published in borg, but not exist in kmi".format(layers_not_in_kmi))
if stores_not_in_borg:
print("The stores({}) doesn't exist in borg, but exist in kmi".format(stores_not_in_borg))
if layers_not_in_borg:
print("The layers({}) doesn't exist in borg, but exist in kmi".format(layers_not_in_borg))
if dry_run:
return
for l in layers_not_in_borg:
print("Try to remove layer({}) from geoserver".format(l))
if dry_run:
print("Submit the delete request to delete the layer from geoserver: {0}/workspaces/{1}/wmsstores/{2}/wmslayers/{3}.xml?recurse=true".format(geoserver,l[1],l[2],l[3]))
else:
try:
res = requests.delete("{0}/workspaces/{1}/wmsstores/{2}/wmslayers/{3}.xml?recurse=true".format(geoserver,l[1],l[2],l[3]),auth=(user, password))
res.raise_for_status()
print("Succeed to remove layer({}) from geoserver.{}".format(l))
except Exception as ex:
print("Failed to remove layer({}) from geoserver.{}".format(l,ex))
for s in stores_not_in_borg:
print("Try to remove store({}) from geoserver".format(s))
if dry_run:
print("Submit the delete request to delete the layer from geoserver: {0}/workspaces/{1}/wmsstores/{2}.xml".format(geoserver,s[1],s[2]))
else:
try:
res = requests.delete("{0}/workspaces/{1}/wmsstores/{2}.xml".format(geoserver,s[1],s[2]),auth=(user, password))
res.raise_for_status()
print("Succeed to remove store({}) from geoserver.{}".format(s))
except Exception as ex:
print("Failed to remove store({}) from geoserver.{}".format(s,ex))