def tunnel(ctx, tunnel_name): """ Tunnel through an EC2 instance in the ECS cluster. The parameters for this command should be found in a tunnels: top-level section in the yaml file, in the format: \b tunnels: - name: my_tunnel service: my_service host: config.MY_TUNNEL_DESTINATION_HOST port: 3306 local_port: 8888 where config.MY_TUNNEL_DESTINATION_HOST is the value of MY_TUNNEL_DESTINATION_HOST for this service in the AWS Parameter Store. The host value could also just be a hostname. """ config = ctx.obj['CONFIG'] yml = config.get_section_item('tunnels', tunnel_name) service_name = yml['service'] service = FriendlyServiceFactory.new(service_name, config=config) host = _interpolate_tunnel_info(yml['host'], service) port = int(_interpolate_tunnel_info(yml['port'], service)) local_port = int(_interpolate_tunnel_info(yml['local_port'], service)) ssh = SSHConfig(service, config=config).get_ssh() ssh.tunnel(host, local_port, port)
def docker_exec(ctx, service_name, verbose): """ SSH to an EC2 instance in the cluster defined in the service named SERVICE_NAME, then run docker exec on the appropriate container. """ service = FriendlyServiceFactory.new(service_name, config=ctx.obj['CONFIG']) ssh = SSHConfig(service, config=ctx.obj['CONFIG']).get_ssh() ssh.docker_exec(verbose=verbose)
def ssh(ctx, service_name, verbose): """ If the service SERVICE_NAME has any running tasks, randomly choose one of the container instances on which one of those tasks is running and ssh into it. If the service SERVICE_NAME has no running tasks, randomly choose one of the container instances in the cluster on which the service is defined. """ service = FriendlyServiceFactory.new(service_name, config=ctx.obj['CONFIG']) ssh = SSHConfig(service, config=ctx.obj['CONFIG']).get_ssh() ssh.ssh(verbose=verbose)
def cluster_run(ctx, service_name): """ Run a command on each of the individual EC2 systems in the ECS cluster running SERVICE_NAME. """ command = click.prompt('Command to run') service = FriendlyServiceFactory.new(service_name, config=ctx.obj['CONFIG']) ssh = SSHConfig(service, config=ctx.obj['CONFIG']).get_ssh() responses = ssh.cluster_run([command]) for index, response in enumerate(responses): click.echo(click.style("Instance {}".format(index + 1), bold=True)) click.echo("Success: {}".format(response[0])) click.echo(response[1])
def cluster_ssh(ctx, service_name): """ SSH to the specified EC2 system in the ECS cluster running SERVICE_NAME. """ service = FriendlyServiceFactory.new(service_name, config=ctx.obj['CONFIG']) instances = service.get_instances() for index, instance in enumerate(instances): click.echo("Instance {}: {} ({})".format(index + 1, instance.name, instance.id)) choice = click.prompt("Which instance to ssh to?", type=int) if choice > len(instances) or choice < 1: click.echo("That is not a valid instance.") return instance = instances[choice - 1] ssh = SSHConfig(service, config=ctx.obj['CONFIG']).get_ssh() ssh.ssh(instance=instance)