Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. # Demonstrates how to modify the min and max instances for a service
  2. # For Http calls
  3. import httplib, urllib, json
  4.  
  5. # For system tools
  6. import sys
  7.  
  8. # For reading passwords without echoing
  9. import getpass
  10.  
  11.  
  12. # Defines the entry point into the script
  13. def main(argv=None):
  14. # Print some info
  15. print
  16. print "This tool is a sample script that resets the minimum and maximum instances allowed for a service."
  17. print
  18.  
  19. username = "arcgisadmin" # raw_input("Enter user name: ")
  20. password = "<fancypassword>" # getpass.getpass("Enter password: ")
  21.  
  22. # Ask for server name
  23. serverName = "<serverurl.com>" # raw_input("Enter Server name: ")
  24. serverPort = 443
  25.  
  26. print r"Enter the service name in the format <folder>/<name>.<type>."
  27. service = "<serverfolder>/<servicename>MapServer"
  28. # minInstances = raw_input("Enter the new minimum: ")
  29. # maxInstances = raw_input("Enter the new maximum: ")
  30.  
  31.  
  32.  
  33. # Get a token
  34. token = getToken(username, password, serverName, serverPort)
  35. if token == "":
  36. print "Could not generate a token with the username and password provided."
  37. return
  38.  
  39. serviceURL = "/test/admin/services/" + service
  40.  
  41. # This request only needs the token and the response formatting parameter
  42. params = urllib.urlencode({'token': token, 'f': 'json'})
  43.  
  44. headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
  45.  
  46. # Connect to service to get its current JSON definition
  47. httpConn = httplib.HTTPSConnection(serverName, serverPort)
  48. httpConn.request("POST", serviceURL, params, headers)
  49.  
  50. # Read response
  51. response = httpConn.getresponse()
  52. if (response.status != 200):
  53. httpConn.close()
  54. print "Could not read service information."
  55. return
  56. else:
  57. data = response.read()
  58.  
  59. # Check that data returned is not an error object
  60. if not assertJsonSuccess(data):
  61. print "Error when reading service information. " + str(data)
  62. else:
  63. print "Service information read successfully. Now changing properties..."
  64.  
  65. # Deserialize response into Python object
  66. dataObj = json.loads(data)
  67. httpConn.close()
  68.  
  69. # Edit desired properties of the service
  70. # dataObj["minInstancesPerNode"] = minInstancesNum
  71. # dataObj["maxInstancesPerNode"] = maxInstancesNum
  72. dataObj["clusterName"]="default"
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80. # Serialize back into JSON
  81. updatedSvcJson = json.dumps(dataObj)
  82.  
  83. # Call the edit operation on the service. Pass in modified JSON.
  84. editSvcURL = "/test/admin/services/" + service + "/edit"
  85. params = urllib.urlencode({'token': token, 'f': 'json', 'service': updatedSvcJson})
  86. httpConn.request("POST", editSvcURL, params, headers)
  87.  
  88. # Read service edit response
  89. editResponse = httpConn.getresponse()
  90. if (editResponse.status != 200):
  91. httpConn.close()
  92. print "Error while executing edit."
  93. return
  94. else:
  95. editData = editResponse.read()
  96.  
  97. # Check that data returned is not an error object
  98. if not assertJsonSuccess(editData):
  99. print "Error returned while editing service" + str(editData)
  100. else:
  101. print "Service edited successfully."
  102.  
  103. httpConn.close()
  104.  
  105. return
  106.  
  107.  
  108. # A function to generate a token given username, password and the adminURL.
  109.  
  110. def getToken(username, password, serverName, serverPort):
  111. # Token URL is typically http://server[:port]/test/admin/generateToken
  112. tokenURL = "/test/admin/generateToken"
  113.  
  114. params = urllib.urlencode({'username': username, 'password': password, 'client': 'requestip', 'f': 'json'})
  115.  
  116. headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
  117.  
  118. # Connect to URL and post parameters
  119. httpConn = httplib.HTTPSConnection(serverName, serverPort)
  120. httpConn.request("POST", tokenURL, params, headers)
  121.  
  122. # Read response
  123. response = httpConn.getresponse()
  124. if (response.status != 200):
  125. httpConn.close()
  126. print "Error while fetching tokens from admin URL. Please check the URL and try again."
  127. return
  128. else:
  129. data = response.read()
  130. httpConn.close()
  131.  
  132. # Check that data returned is not an error object
  133. if not assertJsonSuccess(data):
  134. return
  135.  
  136. # Extract the token from it
  137. token = json.loads(data)
  138. return token['token']
  139.  
  140.  
  141. # A function that checks that the input JSON object
  142.  
  143.  
  144. # is not an error object.
  145.  
  146. def assertJsonSuccess(data):
  147. obj = json.loads(data)
  148. if 'status' in obj and obj['status'] == "error":
  149. print "Error: JSON object returns an error. " + str(obj)
  150. return False
  151. else:
  152. return True
  153.  
  154.  
  155. # Script start
  156. if __name__ == "__main__":
  157. sys.exit(main(sys.argv[1:]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement