consul_cloud_setup 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. 'services': [
  22. { 'name': data['role'], 'tags': tags, 'checks': [{'script': '/bin/true', 'interval': '2s'}] }
  23. ]
  24. }
  25. if data['consul_master'] == '127.0.0.1':
  26. consul_settings['bootstrap_expect'] = 1
  27. else:
  28. consul_settings['retry_join'] = [data['consul_master']]
  29. if re.match('consul', data['role']) is not None:
  30. consul_settings['server'] = True
  31. consul_settings['ui_dir'] = "/opt/consul/ui"
  32. main_config = open("/opt/consul/etc/config.json", "w")
  33. main_config.write(json.dumps(consul_settings))
  34. main_config.close()