-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5fa94eb
commit ccb5c5f
Showing
14 changed files
with
578 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# EC2 Instance Manager | ||
|
||
# Table of contents | ||
|
||
<!--ts--> | ||
- [EC2 Instance Manager](#ec2-instance-manager) | ||
- [Table of contents](#table-of-contents) | ||
- [Python Version](#python-version) | ||
- [Third Party Libraries and Dependencies](#third-party-libraries-and-dependencies) | ||
- [Usage](#usage) | ||
- [Examples](#examples) | ||
<!--te--> | ||
|
||
## Python Version | ||
Python 3.9+ are supported and tested | ||
|
||
|
||
## Third Party Libraries and Dependencies | ||
|
||
The following libraries will be installed when you install the client library: | ||
* [typer](https://github.com/tiangolo/typer) | ||
* [boto3](https://github.com/boto/boto3) | ||
|
||
## Usage | ||
|
||
To start using the library you need to setup a list of ENV variables with your AWS credentials as follows: | ||
```sh | ||
export AWS_DEFAULT_REGION=<...> | ||
export AWS_ACCESS_KEY_ID=<...> | ||
export AWS_SECRET_ACCESS_KEY=<...> | ||
``` | ||
Then you can install a library with pip: | ||
|
||
```sh | ||
pip install . | ||
``` | ||
|
||
If you plan to do a local developent you may also want install it in [editable mode](https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/#working-in-development-mode). | ||
```sh | ||
pip install --editable . | ||
``` | ||
|
||
Afther the library is installed you can simply start it with --help option for retrieveing the further insructions | ||
```sh | ||
ecmgm --help | ||
``` | ||
|
||
## Examples | ||
|
||
Here is the list of example commands: | ||
|
||
Create a VM, using existing AWS SSH key | ||
```sh | ||
ecmgm create <vm-name> <image> --osnick <osnick> --ssh-key-name <ssh-keypair-name> | ||
``` | ||
|
||
Retrieve VM description | ||
```sh | ||
ecmgm describe <ami-id> | ||
``` | ||
|
||
Teardown VM | ||
```sh | ||
ecmgm teardown <ami-id> | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
__app_name__ = "ecmgm" | ||
__version__ = "0.1.0" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from ecmgm import cli, __app_name__ | ||
|
||
|
||
def main(): | ||
cli.app(prog_name=__app_name__) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import getpass | ||
from typing import Optional | ||
|
||
import boto3 | ||
import typer | ||
from rich.console import Console, Group | ||
from rich.panel import Panel | ||
from rich.prompt import Confirm | ||
from rich.syntax import Syntax | ||
from rich.table import Table | ||
|
||
from ecmgm import __app_name__, __version__, schemas, utils | ||
|
||
app = typer.Typer() | ||
console = Console() | ||
|
||
|
||
@app.command() | ||
def describe(instance_id: str = typer.Argument(..., help="EC2 Instance Id")): | ||
ec2 = boto3.client("ec2") | ||
table = Table(title="Instance details") | ||
table.add_column("Attribute", style="magenta") | ||
table.add_column("Value", justify="left", style="cyan") | ||
|
||
response = ec2.describe_instances( | ||
InstanceIds=[ | ||
instance_id, | ||
], | ||
) | ||
|
||
for k, v in response["Reservations"][0]["Instances"][0].items(): | ||
table.add_row(k, str(v)) | ||
|
||
console.print(table) | ||
|
||
|
||
@app.command() | ||
def teardown(instance_id: str = typer.Argument(..., help="EC2 Instance Id")): | ||
instance = utils.terminate_instance(instance_id) | ||
with console.status("[bold green]Waiting for EC2 Instance being terminated..."): | ||
instance.wait_until_terminated() | ||
|
||
|
||
@app.command() | ||
def create( | ||
name: str = typer.Argument(..., help="Virtual machine name"), | ||
os_image: schemas.OsImage = typer.Argument(..., help="OS image"), | ||
instance_type: schemas.InstanceTypes = typer.Option( | ||
schemas.InstanceTypes.small.value, help="Instance types" | ||
), | ||
instance_arch: schemas.InstanceArch = typer.Option( | ||
schemas.InstanceArch.x86_64.value, help="Instance Arch" | ||
), | ||
osnick: str = typer.Option("", help="Optionally filter images by specified osnick"), | ||
ssh_key_name: str = typer.Option( | ||
"", help="You can specify your SSH key name in case its already created in AWS" | ||
), | ||
): | ||
ec2 = boto3.client("ec2") | ||
image_filter_query = schemas.OS_SEARCH_MAPPING[os_image] | ||
|
||
if osnick: | ||
image_filter_query += f"{osnick}*" | ||
|
||
with console.status( | ||
f"[bold green]Searching for image. Search params: query: {image_filter_query} arch: {instance_arch} osnick: {osnick}..." | ||
): | ||
images = ec2.describe_images( | ||
Filters=[ | ||
{ | ||
"Name": "architecture", | ||
"Values": [ | ||
instance_arch, | ||
], | ||
}, | ||
{"Name": "root-device-type", "Values": ["ebs"]}, | ||
{"Name": "state", "Values": ["available"]}, | ||
{"Name": "virtualization-type", "Values": ["hvm"]}, | ||
{"Name": "hypervisor", "Values": ["xen"]}, | ||
{"Name": "image-type", "Values": ["machine"]}, | ||
{ | ||
"Name": "name", | ||
"Values": [image_filter_query], | ||
}, | ||
], | ||
Owners=["amazon"], | ||
) | ||
|
||
sortedAmis = sorted(images["Images"], key=lambda x: x["CreationDate"], reverse=True) | ||
target_image = sortedAmis[0] | ||
panel_group = Group( | ||
Panel( | ||
target_image["ImageId"], | ||
title="ami-id", | ||
), | ||
Panel(target_image["Description"], title="Description"), | ||
Panel(target_image["ImageLocation"], title="Image"), | ||
) | ||
console.print("Image found, details:") | ||
console.print(Panel(panel_group)) | ||
|
||
is_continue = Confirm.ask("Do you want to continue?") | ||
if not is_continue: | ||
quit() | ||
|
||
if not ssh_key_name: | ||
console.print(f"No SSH key was specified, creating new") | ||
private_key_name = f"{getpass.getuser()}-ec2-{os_image.value}-key" | ||
private_key_filename = f"{getpass.getuser()}-ec2-{os_image.value}-key-file.pem" | ||
key_pair = utils.create_key_pair(private_key_name, private_key_filename) | ||
console.print(f"Created a key pair [bold cyan]{key_pair.key_name}[/bold cyan]") | ||
|
||
instance = utils.create_instance( | ||
target_image["ImageId"], | ||
name, | ||
instance_type, | ||
ssh_key_name or key_pair.key_name, | ||
["redis-io-group"], | ||
) | ||
|
||
with console.status("[bold green]Waiting EC2 Instance to start..."): | ||
instance.wait_until_running() | ||
# updating instance attributes to obtain public ip/dns immediately | ||
instance.reload() | ||
|
||
private_key_filename = private_key_filename if not ssh_key_name else "your-key.pem" | ||
panel_group = Group( | ||
Panel("You can now connect to your ec2 machine :thumbs_up:\n"), | ||
Panel( | ||
Syntax( | ||
f"ssh -i {private_key_filename} {schemas.OS_DEFAULT_USER_MAP[os_image]}@{instance.public_dns_name}", | ||
"shell", | ||
theme="monokai", | ||
), | ||
title="Command", | ||
), | ||
Panel( | ||
Syntax( | ||
f"# you also might need to make key to be only readable by you\nchmod 400 {private_key_filename}\n" | ||
f"# You may use that instance id [{instance.id}] in other CLI commands like\n" | ||
"# Get VM details\n" | ||
f"ec2 describe {instance.id}\n" | ||
"# Terminate VM\n" | ||
f"ec2 teardown {instance.id}", | ||
"shell", | ||
theme="monokai", | ||
), | ||
title="Tip", | ||
), | ||
) | ||
console.print(Panel(panel_group)) | ||
|
||
|
||
def _version_callback(value: bool) -> None: | ||
if value: | ||
typer.echo(f"{__app_name__} v{__version__}") | ||
raise typer.Exit() | ||
|
||
|
||
@app.callback() | ||
def main( | ||
version: Optional[bool] = typer.Option( | ||
None, | ||
"--version", | ||
"-v", | ||
help="Show the application's version and exit.", | ||
callback=_version_callback, | ||
is_eager=True, | ||
) | ||
) -> None: | ||
return |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Metadata-Version: 2.1 | ||
Name: ecmgm | ||
Version: 0.1.0 | ||
Classifier: Programming Language :: Python :: 3 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
README.md | ||
pyproject.toml | ||
setup.cfg | ||
setup.py | ||
ecmgm/ecmgm.egg-info/PKG-INFO | ||
ecmgm/ecmgm.egg-info/SOURCES.txt | ||
ecmgm/ecmgm.egg-info/dependency_links.txt | ||
ecmgm/ecmgm.egg-info/not-zip-safe | ||
ecmgm/ecmgm.egg-info/top_level.txt |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from enum import Enum | ||
|
||
|
||
class InstanceTypes(str, Enum): | ||
""" | ||
Instance vCPU* Mem (GiB) | ||
t2.nano 1 0.5 | ||
t2.micro 1 1 | ||
t2.small 1 2 | ||
t2.medium 2 4 | ||
t2.large 2 8 | ||
t2.xlarge 4 16 | ||
t2.2xlarge 8 32 | ||
""" | ||
|
||
nano = "t2.nano" | ||
micro = "t2.micro" | ||
small = "t2.small" | ||
medium = "t2.medium" | ||
large = "t2.large" | ||
xlarge = "t2.xlarge" | ||
doublelarge = "t2.2xlarge" | ||
|
||
|
||
class InstanceArch(str, Enum): | ||
i386 = "i386" | ||
x86_64 = "x86_64" | ||
x86_64_mac = "x86_64_mac" | ||
arm64 = "arm64" | ||
|
||
|
||
class OsImage(str, Enum): | ||
windows = "windows" | ||
ubuntu = "ubuntu" | ||
debian = "debian" | ||
suse = "suse" | ||
amazon_linux = "amazon_linux" | ||
redhat = "redhat" | ||
macos = "macos" | ||
|
||
|
||
OS_SEARCH_MAPPING = { | ||
OsImage.windows: "*Windows*", | ||
OsImage.ubuntu: "*ubuntu*/images/*", | ||
OsImage.debian: "*debian", | ||
OsImage.suse: "*suse*", | ||
OsImage.amazon_linux: "amzn2-ami-hvm-*", | ||
OsImage.redhat: "*RHEL*", | ||
OsImage.macos: "*macos*", | ||
} | ||
|
||
OS_DEFAULT_USER_MAP = {OsImage.ubuntu: "ubuntu", OsImage.amazon_linux: "ec2"} |
Oops, something went wrong.