Browse Source

initial commit

master
Markus Bergholz 3 years ago
parent
commit
eef8a81565
  1. 5
      Makefile
  2. 14
      README.md
  3. 49
      bin/ecssh
  4. 35
      ecssh/__init__.py
  5. 19
      setup.py

5
Makefile

@ -0,0 +1,5 @@
dist:
python setup.py sdist
publish:
twine upload dist/*

14
README.md

@ -1,2 +1,16 @@
# ecssh
fill out `~/.config/ecssh.yml`
```yaml
test:
some_cluster_name:
ssh_user: ec2-user
prod: # AWS ENVIRONMENT
public_prod: # ECS CLUSTER NAME
ssh_user: ec2-user
ssh_key: ~/.ssh/aws.pem
internal_one:
ssh_user: ec2-user
ssh_key: ~/.ssh/id_ed25519
```

49
bin/ecssh

@ -0,0 +1,49 @@
#!/usr/bin/env python
import ecssh
import boto3
import yaml
import sys
import os
import subprocess
import shlex
def loadConfig(CLUSTER):
with open((os.getenv("HOME") + "/.config/ecssh.yml"), 'r') as stream:
try:
RAW_CONFIG = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
sys.exit(1)
PROFILE_CONFIG = RAW_CONFIG.get(AWS_PROFILE)
CONFIG = PROFILE_CONFIG.get(CLUSTER)
if CONFIG.get("ssh_key"):
CONFIG["ssh_key"] = "-i " + CONFIG.get("ssh_key")
return CONFIG
AWS_PROFILE = os.getenv("AWS_PROFILE", "default")
session = boto3.Session(profile_name=AWS_PROFILE)
ECS = session.client('ecs')
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)
elif len(sys.argv) == 3:
CLUSTER = sys.argv[1]
N = int(sys.argv[2])
CONFIG = loadConfig(CLUSTER)
subprocess.check_call(shlex.split(ecssh.ecssh(
CONFIG=CONFIG, ECS=ECS, EC2=EC2, CLUSTER=CLUSTER, N=N)))
else:
print(""" USAGE:
AWS_PROFILE=<ENV> ecssh <CLUSTER NANE>
AWS_PROFILE=<ENV> ecssh <CLUSTER NANE> <N>
""")

35
ecssh/__init__.py

@ -0,0 +1,35 @@
def listContainerInstances(ECS, CLUSTER):
retval = ECS.list_container_instances(cluster=CLUSTER, maxResults=100)
return retval.get("containerInstanceArns")
def listInstanceIDs(ECS, ContainerInstances, CLUSTER):
retval = ECS.describe_container_instances(
cluster=CLUSTER, containerInstances=ContainerInstances)
instances = retval.get("containerInstances")
IDs = list()
for instance in instances:
IDs.append(instance.get("ec2InstanceId"))
return IDs
def listInstanceIPs(EC2, IDs):
retval = EC2.describe_instances(InstanceIds=IDs)
IPs = list()
for item in retval.get("Reservations"):
IPs.append(item.get("Instances")[0].get("PrivateIpAddress"))
return IPs
def ecssh(CONFIG, ECS, EC2, CLUSTER=None, N=None):
containerInstances = listContainerInstances(ECS, CLUSTER=CLUSTER)
IDs = listInstanceIDs(ECS, containerInstances, CLUSTER)
IPs = listInstanceIPs(EC2=EC2, IDs=IDs)
if N:
return "ssh {KEY} {USER}@{IP}".format(KEY=CONFIG.get(
"ssh_key") or "", USER=CONFIG.get("ssh_user"), IP=IPs[N-1])
else:
for IP in IPs:
print("ssh {KEY} {USER}@{IP}".format(KEY=CONFIG.get(
"ssh_key") or "", USER=CONFIG.get("ssh_user"), IP=IP))

19
setup.py

@ -0,0 +1,19 @@
from setuptools import setup
from os import path
this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
setup(name='ecssh',
version='1',
description='accessing aws ecs instances',
long_description=long_description,
long_description_content_type='text/markdown',
url='https://git.osuv.de/m/ecssh',
author='Markus Bergholz',
author_email='markuman@gmail.com ',
license='WTFPL',
packages=['ecssh'],
scripts=['bin/ecssh'],
zip_safe=True)
Loading…
Cancel
Save