SHARE
TWEET

Untitled

a guest Mar 20th, 2017 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import urllib.request
  2. import urllib.parse
  3. from json import loads
  4. from datetime import datetime
  5.  
  6.  
  7. def get_token(username, password, server_name):
  8.  
  9.     params = urllib.parse.urlencode({
  10.         'username': username,
  11.         'password': password,
  12.         'client': 'referer',
  13.         'referer': server_name,
  14.         'expiration': 60,
  15.         'f': 'json'
  16.     })
  17.  
  18.     params = params.encode('ascii')
  19.     url = server_name + '/portal/sharing/rest/generateToken?'
  20.  
  21.     try:
  22.         response = urllib.request.urlopen(url, params)
  23.         if response.getcode() != 200:
  24.             print(response.info())
  25.         else:
  26.             response_data = loads(response.read().decode('utf-8'))
  27.             if 'error' in response_data:
  28.                 print(response_data['error']['details'])
  29.                 return
  30.             else:
  31.                 return response_data['token']
  32.     except IOError as e:
  33.         print('Please Ensure URL to Portal is Valid: ', e.__dict__)
  34.         return
  35.  
  36.  
  37. def update_service(params, folder_url, service, min_inst, max_inst):
  38.  
  39.     # Get Service Information
  40.     service_name = service['serviceName']
  41.     service_type = service['type']
  42.     service_url = folder_url + '/' + service_name + '.' + service_type
  43.     response = urllib.request.urlopen(service_url, params)
  44.     service_data = loads(response.read().decode('utf-8'))
  45.  
  46.     # If Service Min/Max Instances Do Not Match Inputs,
  47.     # Edit Service Data & Pass to Edit REST Operation
  48.     if (service_data['minInstancesPerNode'] != min_inst) or (service_data['maxInstancesPerNode'] != max_inst):
  49.  
  50.         print('Updating: ', service_name, service_type, ' | ',
  51.               service_data['minInstancesPerNode'], ' | ',
  52.               service_data['maxInstancesPerNode'])
  53.  
  54.         service_data['minInstancesPerNode'] = min_inst
  55.         service_data['maxInstancesPerNode'] = max_inst
  56.  
  57.         print('Updating: ', service_name, service_type, ' | ',
  58.               service_data['minInstancesPerNode'], ' | ',
  59.               service_data['maxInstancesPerNode'])
  60.  
  61.         edit_params = urllib.parse.urlencode({
  62.             'token': token,
  63.             'f': 'json',
  64.             'runAsync': 'true',
  65.             'service': service_data
  66.         })
  67.         edit_params = edit_params.encode('ascii')
  68.         edit_url = (service_url + '/edit')
  69.         response = urllib.request.urlopen(edit_url, edit_params)
  70.         edit_response = loads(response.read().decode('utf-8'))
  71.         print(edit_response)
  72.  
  73.  
  74. def get_services(token, server_name, server_port, protected_folders, min_inst, max_inst):
  75.  
  76.     # Collect Root Services Directory
  77.     params = urllib.parse.urlencode({
  78.         'token': token,
  79.         'f': 'json'
  80.     })
  81.     params = params.encode('ascii')
  82.     url = server_name + ':' + server_port + '/arcgis/admin/services'
  83.     response = urllib.request.urlopen(url, params)
  84.     services_directory = loads(response.read().decode('utf-8'))
  85.  
  86.     # Get Services in Root Directory & Update Services
  87.     root_services = services_directory['services']
  88.     for service in root_services:
  89.         folder_url = url + '/'
  90.         update_service(params, folder_url, service, min_inst, max_inst)
  91.  
  92.     # Get Folders in Root Services Directory & Update Services
  93.     folders = (services_directory['folders'])
  94.     for folder in folders:
  95.         if folder not in protected_folders:
  96.             folder_url = url + '/' + folder
  97.             response = urllib.request.urlopen(folder_url, params)
  98.             folder_directory = loads(response.read().decode('utf-8'))
  99.             folder_services = folder_directory['services']
  100.             for service in folder_services:
  101.                 update_service(params, folder_url, service, min_inst, max_inst)
  102.  
  103.  
  104. if __name__ == "__main__":
  105.  
  106.     start = datetime.now()
  107.     print('Script Start: ', start)
  108.  
  109.     username = 'username'
  110.     password = 'password'
  111.     server_name = 'server'
  112.     server_port = 'port'
  113.     min_inst = 0
  114.     max_inst = 1
  115.     protected_folders = ['System', 'Utilities']
  116.  
  117.     token = get_token(username, password, server_name)
  118.  
  119.     get_services(token, server_name, server_port, protected_folders, min_inst, max_inst)
  120.  
  121.     end = datetime.now()
  122.     print('Script End: ', end)
  123.     print('Script Run: ', end-start)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top