I am having issues with getting a list of Services PRIOR to stopping and starting them. I am trying to get this info two different ways and getting errors on both...
One of the examples is coming from here:
Any ideas what I am doing wrong?
from arcgis.gis import GIS
import arcgis.gis.admin
nonProdPortal = "https://xxxx.xxx.gov/portal"
loginName = "xxxxx"
loginPass = "xxxxx"
gis = GIS(nonProdPortal, loginName, loginPass, verify_cert=False)
gis_servers = gis.admin.servers.list()
#To stop specific service(s)
for server in gis_servers:
for service in server.services.list():
#print(service.properties.serviceName)
if service.properties.serviceName == "TMPD_AGO":
#service.stop()
print("Stopping TMPD_AGO")
ERROR:
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\server\_common\_base.py", line 93, in __getattr__
"'%s' object has no attribute '%s'" % (type(self).__name__, name)
AttributeError: 'Server' object has no attribute 'services'
from arcgis import gis
nonProdPortal = "https://xxxxx.gov/portal"
loginName = "xxxx"
loginPass = "xxxx"
mygis = gis.GIS(nonProdPortal, username=loginName, password=loginPass, verify_cert=False)
#mygis = GIS(nonProdPortal, loginName, loginPass, verify_cert=False)
servers = mygis.admin.servers.list()
one_server = servers[0]
services_on_that_server = one_server.services.list()
one_of_those_services = services_on_that_server[0]
# provided your service is running to begin with
print(one_of_those_services.status)
ERROR:
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\server\_common\_base.py", line 93, in __getattr__
"'%s' object has no attribute '%s'" % (type(self).__name__, name)
AttributeError: 'Server' object has no attribute 'services'
Solved! Go to Solution.
I got it... leaving some junk in here that is commented out that still does stuff...
BUT basically I can point to a Stand Alone Server and:
It would be nice to read into that LIST OBject and get the specific service name and use that to start and stop services... Where I could say IF the service name = XXX stop it and leave the others in the folder alone...
I have not figure out how to do that yet... as a list object is being sent back to me.
EDIT: While I was posting @EarlMedina posted the solution to get the name... I UPDATED the code below to print that out but did not build the IF THEN yet...
THANKS @EarlMedina
from arcgis.gis import GIS
import arcgis.gis.server
def listServices():
nonProdPortal = "https://xxx.xxxx.xxx.gov/portal"
loginName = "xxxx"
loginPass = "xxxx"
gis = GIS(nonProdPortal, loginName, loginPass, verify_cert=False)
gis_servers = gis.admin.servers.list()
print(gis_servers)
print("")
server1 = gis_servers[0]
print(server1)
print("")
foldersList = server1.services.folders
print(foldersList)
# LIST FOLDERS and THEN ALL THE SERVICES IN THOSE FOLDERS
#for folder in foldersList:
# foldername = folder
# print(foldername)
# servicesList = server1.services.list(folder=foldername)
# for servicename in servicesList:
# servicenameValue = servicename
# print("---" + str(servicenameValue))
servicesList2 = server1.services.list(folder='DEQ_DEV')
for service in servicesList2:
# GIVE ME THE SERVICE NAME
service_name = service.iteminformation.properties["name"]
print(service_name)
print(service)
status = service.status
print(status)
#service.stop()
#service.start()
status = service.status
print(status)
if __name__ == '__main__':
listServices()
Is the account you are logging in with an administrator account?
If you see my last post... I got a list of servers and say one had http and one https .. I changed my code to point the second index and it works fine... but this is pointing to a hosted server and I want my non prod portal server. Its using the same credentials...
I dont think this is a system admin but its got elevated permissions.
The error does not seem to be permissions... its telling me that the http server that services is not in the server object...
But then it runs on the hosted server fine.
OK Weird thing.... I printed my servers and noticed that the main server does not have HTTPS and the hosted server does have HTTPS
I changed my code to point the the hosted Feature Server because that has HTTPS in the name...and the code runs...
Changed the index to point to the second server: server1 = gis_servers[1]
Is that the reason or does this code only run on a Hosted Server?
gis = GIS(nonProdPortal, loginName, loginPass, verify_cert=False)
gis_servers = gis.admin.servers.list()
print(gis_servers)
print("")
server1 = gis_servers[1]
print(server1)
validate = gis.admin.servers.validate()
print("Is the server valid? " + str(validate))
foldersList = server1.services.folders
print(foldersList)
print("")
print("-----------")
print("")
servicesList = server1.services.list()
print(servicesList)
Results:
[<Server at http://xxx.xxxxx.gov/env/admin>, <Server at https://xxxx.xxxx.gov/hosting/admin>]
<Server at https://xxxxx.xxxxx.gov/hosting/admin>
Is the server valid? True
['Hosted', 'System', 'Utilities', '/']
-----------
[<Service at https://xxxl.xx.xxxx.gov/hosting/admin/services/bmpReportTool.GPServer>,
<Service at https://xxxl.xx.xxxx.gov/hosting/admin/services/ImportCurveData.GPServer>,
<Service at https://xxxl.xx.xxxx.gov/hosting/admin/services/npdesReportTool.GPServer>,
<Service at https://xxxl.xx.xxxx.gov/hosting/admin/services/SWPPP_Annual_Dev.MapServer>,
<Service at https://xxxl.xx.xxxx.gov/hosting/admin/services/SWPPP_Monthly_Dev.MapServer>]
For standalone servers, you want to be using the arcgis.gis.server module instead of arcgis.gis.admin. Update your code to use the correct module and it should work for the standalone servers.
Tells me Server not defined
I updated my IMPORT to arcgis.gis.server
from arcgis.gis import GIS
import arcgis.gis.server
def listServices():
loginName = "xxxx"
loginPass = "xxxxx"
server_base_url = "https://xxxxx.gov/portal/admin/"
gis_server = Server(url=f"{server_base_url}/web_adaptor/admin",
token_url=f"{server_base_url}/web_adaptor/tokens/generateToken",
username=loginName,
password=loginPass)
foldersList = gis_server.services.folders
print(foldersList)
OK I think I made progress...
So the serviceList2 returns a list object? but I am just looking for the Service Name... how do I get that?
Returned:
<Service at https://xxx.cov.xxx.gov:6443/arcgis/admin/services/DEQ_DEV/VDEQ_Petro_Release_DEV.MapServer>
Looking for
VDEQ_Petro_Release_DEV
from arcgis.gis import GIS
import arcgis.gis.server
def listServices():
nonProdPortal = "https://xxx.gov/portal"
loginName = "xxx"
loginPass = "xxxxx"
gis = GIS(nonProdPortal, loginName, loginPass, verify_cert=False)
gis_servers = gis.admin.servers.list()
server1 = gis_servers[0]
print(server1)
servicesList2 = server1.services.list(folder='DEQ_DEV')[0]
print(servicesList2)
The name you can find in the service iteminformation:
service.iteminformation.properties["name"]
Hey @EarlMedina do I mark your replay above regarding import arcgis.gis.server module as the solution and leave my full working code below as a reply? Want to make sure you get credit for this as you steered me in the correct direction ????
Oh, it doesn't matter. Whatever is most useful to anyone out there. I'm just here to help out.