Created
January 30, 2020 07:29
-
-
Save WinstonN/12c52ab1bcfe2794b8c4d8a2f496bb59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def setup_host_group(context, environment, filter, tags='private_ip_address'): | |
""" | |
Setup hosts group from EC2 instances using boto3 | |
""" | |
hosts = extract_instances_information(context, environment, filter, tags) | |
config = get_config() | |
connection = [Connection(host=host, config=config) for host in hosts] | |
return connection | |
def get_local_ip_information(): | |
print(c("---", cyan)) | |
print(c("Your current IP Address is: ", white, attrs=['bold'])) | |
os.system("wget -O - -q icanhazip.com") | |
print(c("---", cyan)) | |
def get_config(): | |
""" | |
Set correct config for connections | |
""" | |
config = Config( | |
runtime_ssh_path=ssh_config_file_path, | |
# overrides={ | |
# 'sudo': { | |
# 'user': 'www' | |
# } | |
# } | |
) | |
return config | |
@task | |
def get_hosts_info(context, environment, filter, tags=None): | |
""" | |
Get information about hosts | |
:param environment: The environment to target (Staging, Development, Production) | |
:param filter: The filter wildcard string to run the commands | |
:param tags: Tags to get information from - see https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#instance | |
""" | |
if environment not in environments: | |
raise Exception("Tags cannot be empty for this method") | |
# get hosts info | |
extract_instances_information(context, environment, filter, tags) | |
@task | |
def report(context, environment, filter): | |
""" | |
Hosts report their status | |
:param environment: The environment to target (Staging, Development, Production) | |
:param filter: The filter wildcard string to run the commands | |
""" | |
connection = setup_host_group(context, environment, filter) | |
group = ThreadingGroup.from_connections(connection) | |
try: | |
group.run('uname -a; uptime; df -h;') | |
except exceptions.GroupException as e: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment