execfile('common.py')

connectToServer()

editCustom()
startEdit()

try:
  props = {'origin-server': otdOriginServer, 
           'listener-port': otdListenerPort, 
           'configuration': otdConfig, 
           'server-name': 'elasticity-otd'}
   
  otd_createConfiguration(props)
  
  props={'configuration': otdConfig, 'machine': machineName }
  otd_createInstance(props)

  save()
  activate()
   
  startEdit()

  # Set auto-discovery == true
  healthCheckProps={'configuration': otdConfig, 
             'origin-server-pool': "origin-server-pool-1", 
             'dynamic-server-discovery' : 'true'}
  outprops=otd_setHealthCheckProperties(healthCheckProps)
  print "Origin Server pool health check properties: " + str(outprops)
  
  # Set round-robin load balancing
  lbProps={'configuration': otdConfig, 
             'origin-server-pool': "origin-server-pool-1", 
             'load-distribution' : 'round-robin'}
  outprops=otd_setOriginServerPoolProperties(lbProps)
  print "Origin Server pool properties: " + str(outprops)
  
  save()
  activate()
except: 
  if editService.getConfigurationManager().isEditor():
    print "Exception occurred, canceling edit session and any unsaved changes"
    editService.getConfigurationManager().cancelEdit()
  raise

#start("otd_elasticity-otd_localhost","SystemComponent")
print "Starting OTD instance"
try:
  print "Starting OTD instance..."
  start(otdInstance,"SystemComponent")
except:
  print ""
  print "Error starting " + otdInstance + ", likely already running or not yet configured"
  print ""
  
state(otdInstance)
 
