Skip to content

Instantly share code, notes, and snippets.

@bkruger99
Last active March 4, 2021 23:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bkruger99/b5f80cc20e79aae19dcecc89942fab10 to your computer and use it in GitHub Desktop.
Save bkruger99/b5f80cc20e79aae19dcecc89942fab10 to your computer and use it in GitHub Desktop.
ES7 Initial Bootstrap Script - use at your own risk - use tags and ec2 discovery. install boto3 somewhere, ensure you have the cluster name set in the config and ec2 discovery configured.
#!/usr/bin/env python
# It's up to the enduser to make a backup of their config incase things break. If you're bootstrapping, this should only run once.
# You'll also need to install boto3 somehow. This should be reasonable python version agnostic.
import boto3
import requests
import yaml
import json
# src and dst file
esfile = '/etc/elasticsearch/elasticsearch.yml'
# Read our current yaml
esyml = yaml.load(open(esfile))
hosts = list()
# Get our region from metadata because scalability and laziness.
r = requests.get(
"http://169.254.169.254/latest/dynamic/instance-identity/document")
response_json = r.json()
region = response_json.get('region')
# Get the instances filtering on master and our cluster name.
client = boto3.client('ec2', region_name=region)
response = client.describe_instances(
Filters=[
{'Name': 'instance-state-name',
'Values': [
'running'
]
},
{'Name': 'tag:ES_CLUSTER',
'Values': [
esyml['cluster.name']
]
},
{'Name': 'tag:ES_MASTER',
'Values': [
'true'
]
}
])
# Get node names. (because filters, right?)
for i in response['Reservations']:
for j in i['Instances']:
for k in j['Tags']:
if k.get('Key') == 'ES_NODE':
hosts.append(k.get('Value'))
hosts.sort()
# Ensure previous values are nuked
try:
del esyml['cluster.initial_master_nodes']
except:
pass
esyml['cluster.initial_master_nodes'] = hosts
# Make your own backups
with open(esfile, 'w') as outfile:
yaml.dump(esyml, outfile, default_flow_style=False)
@DaveCTurner
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment