我试图使用PyChef从厨师服务器删除一个节点,但我得到一个错误如下

时间:2016-09-12 06:01:17

标签: python chef pychef

我正在尝试使用PyChef删除Chef服务器上的节点,但是下面给出了一个错误。这是我的剧本:

import json
import requests
import chef
import base64
from chef import Node
from chef import auth
from chef.rsa import Key
from chef.api import ChefAPI
from chef import api
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()

key = open('/root/chef-repo/.chef/gs-validator.pem', "r").read()
key =Key(key)

hashed_body = auth.sha1_base64('')
#print hashed_body

from datetime import datetime
timestamp = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
#print timestamp

headers={}
req=auth.canonical_request('GET','here-is-my url-of server',hashed_body,timestamp,'here-is-client-name')
sig =auth. _ruby_b64encode(key.private_encrypt(req))
#print sig
for i, line in enumerate(sig):
headers['x-ops-authorization-%s'%(i+1)] = line
#print headers
#print "--------------",headers.get('x-ops-authorization-%s'%(4),False)

X1= headers.get('x-ops-authorization-%s'%(1),False)
X2 = headers.get('x-ops-authorization-%s'%(2),False)
X3 = headers.get('x-ops-authorization-%s'%(3),False)
X4 = headers.get('x-ops-authorization-%s'%(4),False)
X5 = headers.get('x-ops-authorization-%s'%(5),False)

headers = {'Method':'GET','X-Ops-Content-Hash':hashed_body,'content-    type':'application/json','accept':'application/json','X-Ops-Timestamp':timestamp,'X-Ops-UserId':'gs-validator','X-Ops-Authorization-1':X1,'X-Ops-Authorization-2':X2,'X-Ops-Authorization-3':X3,'X-Ops-Authorization-4':X4,'X-Ops-Authorization-5':X5}

with chef.ChefAPI(url, '/root/chef-repo/.chef/gs-validator.pem', 'gs-    validator',headers = headers,ssl_verify = False):
   node = Node('node-name')
   print node.delete()  

错误:

Traceback (most recent call last):
File "delchef.py", line 55, in <module>
print node.delete()
File "/usr/local/lib/python2.7/dist-packages/chef/base.py", line 117, in   delete
api.api_request('DELETE', self.url)
File "/usr/local/lib/python2.7/dist-packages/chef/api.py", line 217, in  api_request
response = self.request(method, path, headers, data)
File "/usr/local/lib/python2.7/dist-packages/chef/api.py", line 207, in request
raise ChefServerError.from_error(response.reason, code=response.status_code)
chef.exceptions.ChefServerNotFoundError: Object Not Found

当我打印而不是node.delete()时:
    node.attributes

it gives me this output:
chef.node.NodeAttributes object at 0x7fb2c11e7390

提前谢谢..请建议我这个错误的可能解决方案。

1 个答案:

答案 0 :(得分:0)

PyChef已经为你做了所有的身份验证标题,你为什么要重复这些事情?对于特定问题,404表示您尝试删除的节点不存在。您可以使用Node.list()查看服务器上存在哪些节点。