Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

How to do the node maintenance turn on/off curl command in python

avatar

How can i send the following command for putting datanode in maintenance mode in python program?

curl -u user -H "X-Requested-By:ambari" -i -X PUT -d '{"RequestInfo":{"context":"Turn Off Maintenance Mode for host","query":"Hosts/host_name.in(host1)"},"Body":{"Hosts":{"maintenance_state":"OFF"}}}' http://ambari_host:8080/api/v1/clusters/clustername/hosts

I am trying it in this way

data = {"RequestInfo":{"context":"Turn OFF Maintenance Mode for host","query":"Hosts/host_name.in(host1,host2)"},"Body":{"Hosts":{"maintenance_state":"OFF"}}}
base_url = 'http://ambari_host:8080/api/v1/clusters/clustername/hosts'headers= {'Accept': 'application/json','data':data}
response = requests.put(base_url, data=json.dumps(data), auth=('username', argpaas), headers=headers)

I don't get any response back.

Thanks

Meena Rajani

1 ACCEPTED SOLUTION

avatar
Super Guru

@Meena Rajani,

The API returns empty content on success. You can check the response code to make sure that it passed.

assert response.status_code == 200

However if you still want to have more validations you can do the below

import requests
from requests.auth import HTTPBasicAuth
ambari_username = "admin"
ambari_password = "admin"
base_url = "http://ambari_host:8080/api/v1/clusters/clustername/hosts/host1"
basic_auth = HTTPBasicAuth(ambari_username, ambari_password)
headers = {'X-Requested-By' : 'ambari'}
response = requests.get(base_url, auth=basic_auth, headers=headers,verify=False)
print response.status_code
maint_state = response.json()["Hosts"]["maintenance_state"]
print maint_state 
assert maint_state == "OFF"

Thanks,

Aditya

View solution in original post

4 REPLIES 4

avatar
Super Guru

@Meena Rajani,

The API returns empty content on success. You can check the response code to make sure that it passed.

assert response.status_code == 200

However if you still want to have more validations you can do the below

import requests
from requests.auth import HTTPBasicAuth
ambari_username = "admin"
ambari_password = "admin"
base_url = "http://ambari_host:8080/api/v1/clusters/clustername/hosts/host1"
basic_auth = HTTPBasicAuth(ambari_username, ambari_password)
headers = {'X-Requested-By' : 'ambari'}
response = requests.get(base_url, auth=basic_auth, headers=headers,verify=False)
print response.status_code
maint_state = response.json()["Hosts"]["maintenance_state"]
print maint_state 
assert maint_state == "OFF"

Thanks,

Aditya

avatar

Thanks Adtiya. Let me try the above one. I forgot to write in my post earlier not only I didn't receive the status but my command didn't switch the maintenance mode.

Do you have any documentation which I can refer to?

avatar
Super Guru

I tried this and it worked for me. Please let me know how it goes for you

import requests
from requests.auth import HTTPBasicAuth
import json
ambari_username = "admin"
ambari_password = "admin"
base_url = "http://ambari_host:8080/api/v1/clusters/clustername/hosts"
basic_auth = HTTPBasicAuth(ambari_username, ambari_password)
data = {"RequestInfo":{"context":"Turn OFF Maintenance Mode for host","query":"Hosts/host_name.in(host1,host2)"},"Body":{"Hosts":{"maintenance_state":"ON"}}}
headers = {'X-Requested-By' : 'ambari'}
response = requests.put(base_url, data=json.dumps(data), auth=basic_auth, headers=headers,verify=False)
print response.status_code
assert response.status_code == 200

Thanks,

Aditya

avatar

Thanks Aditya That worked 🙂