Browse Source

add help and version output, when no cluster is defined, list all clusters

master 2
Markus Bergholz 3 years ago
parent
commit
3189df9c09
  1. 27
      bin/ecssh
  2. 9
      ecssh/__init__.py
  3. 2
      setup.py

27
bin/ecssh

@ -6,7 +6,7 @@ import sys
import os
import subprocess
import shlex
import json
def loadConfig(CLUSTER):
with open((os.getenv("HOME") + "/.config/ecssh.yml"), 'r') as stream:
@ -22,6 +22,13 @@ def loadConfig(CLUSTER):
CONFIG["ssh_key"] = "-i " + CONFIG.get("ssh_key")
return CONFIG
def help():
print("""
USAGE:
AWS_PROFILE=<ENV> ecssh <CLUSTER NANE>
AWS_PROFILE=<ENV> ecssh <CLUSTER NANE> <N>
""")
AWS_PROFILE = os.getenv("AWS_PROFILE", "default")
session = boto3.Session(profile_name=AWS_PROFILE)
@ -31,10 +38,15 @@ EC2 = session.client('ec2')
if __name__ == "__main__":
if len(sys.argv) == 2:
CLUSTER = sys.argv[1]
CONFIG = loadConfig(CLUSTER)
ecssh.ecssh(CONFIG=CONFIG, ECS=ECS,
EC2=EC2, CLUSTER=CLUSTER, N=None)
if sys.argv[1] in ["--help", "-h"]:
help()
elif sys.argv[1] in ["--version", "-v"]:
print("2")
else:
CLUSTER = sys.argv[1]
CONFIG = loadConfig(CLUSTER)
ecssh.ecssh(CONFIG=CONFIG, ECS=ECS,
EC2=EC2, CLUSTER=CLUSTER, N=None)
elif len(sys.argv) == 3:
CLUSTER = sys.argv[1]
N = int(sys.argv[2])
@ -42,8 +54,5 @@ if __name__ == "__main__":
subprocess.check_call(shlex.split(ecssh.ecssh(
CONFIG=CONFIG, ECS=ECS, EC2=EC2, CLUSTER=CLUSTER, N=N)))
else:
print(""" USAGE:
ecssh.listClusters(ECS=ECS)
AWS_PROFILE=<ENV> ecssh <CLUSTER NANE>
AWS_PROFILE=<ENV> ecssh <CLUSTER NANE> <N>
""")

9
ecssh/__init__.py

@ -1,3 +1,5 @@
import json
def listContainerInstances(ECS, CLUSTER):
retval = ECS.list_container_instances(cluster=CLUSTER, maxResults=100)
return retval.get("containerInstanceArns")
@ -20,6 +22,13 @@ def listInstanceIPs(EC2, IDs):
IPs.append(item.get("Instances")[0].get("PrivateIpAddress"))
return IPs
def listClusters(ECS):
retval = ECS.list_clusters(maxResults=100)
CLUSTERS = list()
for cluster in retval.get("clusterArns"):
CLUSTERS.append(cluster.split("/")[-1])
print(json.dumps(CLUSTERS))
def ecssh(CONFIG, ECS, EC2, CLUSTER=None, N=None):
containerInstances = listContainerInstances(ECS, CLUSTER=CLUSTER)

2
setup.py

@ -6,7 +6,7 @@ with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
setup(name='ecssh',
version='1',
version='2',
description='accessing aws ecs instances',
long_description=long_description,
long_description_content_type='text/markdown',

Loading…
Cancel
Save