-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_soe.py
143 lines (112 loc) · 4.19 KB
/
update_soe.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
"""
-------------------------------------------------------------------------------
Name: update_soe.py
Purpose: Update server object extension from command line
Author: Josh Werts (@jwerts) ([email protected])
Created: 4/12/15
Copyright: (c) Josh Werts 2015
License: MIT
-------------------------------------------------------------------------------
"""
from __future__ import print_function
# 3rd-party library: requests
# http://docs.python-requests.org/en/latest/
# pip install requests
import requests
PROTOCOL = "http://"
HOST = "localhost"
USER = "siteadmin"
PASSWORD = "yourpassword"
# note that services are suffixed by type when passed to admin REST API
SERVICES = [r"service_folder\service_name.MapServer"]
# path to rebuilt SOE file
SOE_FILE = r"C:\path_to_your_project\bin\Debug\yourSOE.soe"
class AGSRestError(Exception): pass
class ServerError(Exception): pass
def _validate_response(response):
""" Tests response for HTTP 200 code, tests that response is json,
and searches for typical AGS error indicators in json.
Raises an exception if response does not validate successfully.
"""
if not response.ok:
raise ServerError("Server Error: {}".format(response.text))
try:
response_json = response.json()
if "error" in response_json:
raise AGSRestError(response_json["error"])
if "status" in response_json and response_json["status"] != "success":
error = response_json["status"]
if "messages" in response_json:
for message in response_json["messages"]:
error += "\n" + message
raise AGSRestError(error)
except ValueError:
print(response.text)
raise ServerError("Server returned HTML: {}".format(response.text))
def _get_token(username, password):
""" Returns token from server """
token_url = "{protocol}{host}/arcgis/tokens/".format(
protocol=PROTOCOL, host=HOST)
data = { "f": "json",
"username": username,
"password": password,
"client": "requestip",
"expiration": 5 }
response = requests.post(token_url, data)
_validate_response(response)
token = response.json()['token']
return token
def _upload_soe_file(soe_path, token):
""" Uploads .soe file to ArcGIS Server and returns itemID from
uploaded file
"""
upload_url = "{protocol}{host}/arcgis/admin/uploads/upload?f=json".format(
protocol=PROTOCOL, host=HOST)
with open(soe_path, 'rb') as soe_file:
files = {'itemFile': soe_file}
data = {
"token": token
}
response = requests.post(upload_url, data, files=files)
_validate_response(response)
response_json = response.json()
item_id = response_json['item']['itemID']
return item_id
def _update_soe(item_id, token):
""" Updates SOE based on uploaded files itemID """
update_url = "{protocol}{host}/arcgis/admin/services/types/extensions/update".format(
protocol=PROTOCOL, host=HOST)
data = {
"f": "json",
"token": token,
"id": item_id
}
response = requests.post(update_url, data)
_validate_response(response)
def _start_services(services, token):
""" starts ArcGIS Server services """
start_services_url = "{protocol}{host}/arcgis/admin/services/{service}/start"
for service in services:
url = start_services_url.format(protocol=PROTOCOL,
host=HOST,
service=service)
print("Starting {}".format(service))
data = {
"f": "json",
"token": token,
}
response = requests.post(url, data)
_validate_response(response)
print("Started!")
if __name__ == "__main__":
print("Retrieving token...")
token = _get_token(USER, PASSWORD)
print("Retrieved: {}".format(token))
print("Uploading SOE...")
item_id = _upload_soe_file(SOE_FILE, token)
print("Uploaded: {}".format(item_id))
print("Updating SOE...")
_update_soe(item_id, token)
print("Updated!")
print("Starting services...")
_start_services(SERVICES, token)