Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

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

Solved Go to solution
Highlighted

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

New Contributor

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

Accepted Solutions

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

@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

4 REPLIES 4

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

@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

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

New Contributor

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?

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

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

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

New Contributor

Thanks Aditya That worked :)

Don't have an account?
Coming from Hortonworks? Activate your account here