consul_cloud_setup_post_1.2 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/python
  2. import json
  3. import urllib2
  4. import sys
  5. import re
  6. URL = 'http://169.254.169.254/latest/user-data'
  7. try:
  8. data = json.load(urllib2.urlopen(URL))
  9. except:
  10. print >> sys.stderr, 'Was not able to connect to the Amazon API'
  11. sys.exit(2)
  12. # Python doesn't like a list of 1
  13. tags = []
  14. tags.append(data['role'])
  15. consul_settings = {
  16. 'data_dir': '/opt/consul/data',
  17. 'client_addr': '0.0.0.0',
  18. 'datacenter': data['domain'],
  19. 'domain': data['domain'],
  20. 'log_level': "INFO",
  21. 'enable_script_checks': True,
  22. 'service': [
  23. { 'name': data['role'], 'tags': tags, 'checks': [{'args': ['/bin/true'], 'interval': '2s'}] }
  24. ]
  25. }
  26. if data['consul_master'] == '127.0.0.1':
  27. consul_settings['bootstrap_expect'] = 1
  28. else:
  29. consul_settings['retry_join'] = [data['consul_master']]
  30. if re.match('consul', data['role']) is not None:
  31. consul_settings['server'] = True
  32. main_config = open("/opt/consul/etc/config.json", "w")
  33. main_config.write(json.dumps(consul_settings))
  34. main_config.close()