我在centos上使用digitalocean脚本,但它似乎不起作用。 https://www.digitalocean.com/community/tutorials/how-to-set-up-highly-available-haproxy-servers-with-keepalived-and-floating-ips-on-ubuntu-14-04
Traceback (most recent call last):
File "/usr/local/bin/assign-ip", line 33, in <module>
usage()
File "/usr/local/bin/assign-ip", line 12, in usage
print('{} [Floating IP] [Droplet ID]'.format(sys.argv[0]))
ValueError: zero length field name in format
这是sh脚本调用
` export DO_TOKEN='xxxxxxxxxxx'
IP='xxx.xxx.xxx.xxx'
ID=$(curl -s 169.254.169.254/metadata/v1/id)
HAS_FLOATING_IP=$(curl -s http://169.254.169.254/metadata/v1/floating_ip/ipv4/active)
if [ $HAS_FLOATING_IP = "false" ]; then
n=0
while [ $n -lt 10 ]
do
python /usr/local/bin/assign-ip $IP $ID && break
n=$((n+1))
sleep 3
done
fi`
这里是assign-ip脚本
`#!/usr/bin/python
import os
import sys
import requests
import json
api_base = 'https://api.digitalocean.com/v2'
def usage():
print('{} [Floating IP] [Droplet ID]'.format(sys.argv[0]))
print('\nYour DigitialOcean API token must be in the "DO_TOKEN"'
' environmental variable.')
def main(floating_ip, droplet_id):
payload = {'type': 'assign', 'droplet_id': droplet_id}
headers = {'Authorization': 'Bearer {}'.format(os.environ['DO_TOKEN']),
'Content-type': 'application/json'}
url = api_base + "/floating_ips/{}/actions".format(floating_ip)
r = requests.post(url, headers=headers, data=json.dumps(payload))
resp = r.json()
if 'message' in resp:
print('{0}: {1}'.format(resp['id'], resp['message']))
sys.exit(1)
else:
print('Moving IP address: {}'.format(resp['action']['status']))
if __name__ == "__main__":
if 'DO_TOKEN' not in os.environ or not len(sys.argv) > 2:
usage()
sys.exit()
main(sys.argv[1], sys.argv[2])
`
答案 0 :(得分:2)
偶然发现了这一点。我实际上在上面写了the Python script。它链接的教程针对的是Ubuntu 14.04。您看到的错误是由于CentOS 6附带Python 2.6,而脚本使用的功能仅适用于Python 2.7。 gist containing it已更新为与Python 2.6和CentOS 6兼容。
具体来说,错误是由于省略了字符串格式中的位置参数。例如:
print('{} [Floating IP] [Droplet ID]'.format(sys.argv[0]))
在Python&lt; 2.6,这需要明确。 E.g:
print('{0} [Floating IP] [Droplet ID]'.format(sys.argv[0]))
版本2.7中的更改:位置参数说明符可以省略,因此
'{} {}'
等同于'{0} {1}'
。