diff --git a/README.md b/README.md index aa03b79..5fc5429 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,10 @@ Scripts A collection of bash script usefull for common tasks like tomcat startup or watchdog. -See the wiki for more info on how to install and use them. \ No newline at end of file +See the wiki for more info on how to install and use them. + +COMMAND for updating layers +--------------------------- +```python3 update_xml_file.py 'http://localhost:8080/geoserver/gwc/rest/layers/' USERNAME PASSWORD ./layers.txt ./tile_layer_template.xml.txt``` + +System Argument needs to pass: (USERNAME, PASSWORD, LAYERS FILE, XML FILE) \ No newline at end of file diff --git a/utils/update_layers/layers.txt b/utils/update_layers/layers.txt new file mode 100644 index 0000000..8011709 --- /dev/null +++ b/utils/update_layers/layers.txt @@ -0,0 +1 @@ +["nurc:Pk50095","spearfish","sf:restricted","nurc:Img_Sample","tasmania","sf:archsites","tiger-ny","topp:states","topp:tasmania_state_boundaries","nurc:Arc_Sample","sf:bugsites","tiger:giant_polygon","topp:tasmania_water_bodies","tiger:poly_landmarks","topp:tasmania_cities","sf:sfdem","tiger:tiger_roads","nurc:mosaic","tiger:poi","topp:tasmania_roads","sf:streams","sf:roads"] diff --git a/utils/update_layers/requriments.txt b/utils/update_layers/requriments.txt new file mode 100644 index 0000000..630fa86 --- /dev/null +++ b/utils/update_layers/requriments.txt @@ -0,0 +1,2 @@ +requests==2.27.1 +xmltodict==0.12.0 diff --git a/utils/update_layers/tile_layer_template.xml.txt b/utils/update_layers/tile_layer_template.xml.txt new file mode 100644 index 0000000..cfdf4f7 --- /dev/null +++ b/utils/update_layers/tile_layer_template.xml.txt @@ -0,0 +1,39 @@ + + + {{layer_id}} + true + true + {{layer_name}} + + image/png + image/jpeg + image/png8 + + + + EPSG:4326 + + + 7.475217105133489 + 35.21695657422812 + 19.380075671969546 + 45.893769848344476 + + + + + + 4 + 4 + + 0 + 0 + + + STYLES + + + + 0 + + \ No newline at end of file diff --git a/utils/update_layers/update_xml_file.py b/utils/update_layers/update_xml_file.py new file mode 100755 index 0000000..dd75a0a --- /dev/null +++ b/utils/update_layers/update_xml_file.py @@ -0,0 +1,78 @@ +""" +Use this script + +COMMAND: +python3 update_xml_file.py 'http://localhost:8080/geoserver/gwc/rest/layers/' USERNAME PASSWORD ./layers.txt ./tile_layer_template.xml.txt + +System Argument needs to pass: +USERNAME +PASSWORD +LAYERS FILE +XML FILE + +""" + + +import sys +import ast +import json +import requests +import xmltodict +from requests.auth import HTTPBasicAuth + + +if len(sys.argv) > 6 or len(sys.argv) < 6: + raise Exception("Expected 6 arguments are not given.") + +URL = sys.argv[1] +username = sys.argv[2] +password = sys.argv[3] +layer_file = sys.argv[4] +xml_file = sys.argv[5] + +if not (username and password and layer_file and xml_file): + raise Exception( + "System Argument needs to pass: (USERNAME, PASSWORD, LAYERS FILE, XML FILE)" + ) +elif ".xml" not in xml_file: + raise Exception("Please pass xml file here.") +elif ".txt" not in layer_file: + raise Exception("Please pass txt file for layers.") +elif "layers" not in URL: + raise Exception("Please pass correct layer URL.") + +f = open(layer_file, "r") +layers = f.read() +layers_list = ast.literal_eval(layers) + +for layer_name in layers_list: + layerName = layer_name + response = requests.get( + URL + layerName + ".xml", auth=HTTPBasicAuth(username, password) + ) + + if response.status_code == 401: + raise Exception("Incorrect USERNAME or PASSWORD") + + xpars = xmltodict.parse(response.text) + + layerId = xpars["GeoServerLayer"]["id"] + + f = open(xml_file, "r") + data = f.read() + + file = xmltodict.parse(data) + file["GeoServerLayer"]["id"] = layerId + file["GeoServerLayer"]["name"] = layerName + + headers = {"Content-Type": "application/json"} + + response = requests.request( + "PUT", + URL + layerName, + headers=headers, + data=json.dumps(file), + auth=HTTPBasicAuth(username, password), + ) + + print(response.text)