Skip to main content

SDK for hangar cloud.

Project description

Hangar Documentation

Tutorial

Prerequisites

  • Atleast Python 3.10.2 installed
  • AWS account
  • Github account
  • Hangar account with an api key

Setup

  1. Install Hangar CLI
pip install hangar-cli
  1. Make a new python file with function named hangar_run

  2. Create a new scope

from hangar_sdk.base import HangarScope

def hangar_run():
    scope = HangarScope(
        name="my-first-scope",
        region="us-west-2",
        roleAssumptions=[],
    )
   
  1. Create a new asset
from hangar_sdk.base import HangarScope, Asset, GitHubRepo

def hangar_run():
    scope = HangarScope(
        name="my-first-scope",
        region="us-west-2",
        roleAssumptions=[],
    )
    asset = Asset(
            scope=scope,
            name="<your-name>-my-first-bucket",
            source=GitHubRepo(
                scope=scope,
                repo="your-repo",
                branch="main",
                name="my-repo",
            ),
        )

  1. Create a new builder

from hangar_sdk.base import HangarScope, Asset, GitHubRepo, BuildkitBuilder

def hangar_run():
    scope = HangarScope(
        name="my-first-scope",
        region="us-west-2",
        roleAssumptions=[],
    )
    asset = Asset(
            scope=scope,
            name="<your-name>-my-first-bucket",
            source=GitHubRepo(
                scope=scope,
                repo="your-repo",
                branch="main",
                name="my-repo",
            ),
        )
    builder = BuildkitBuilder(
            scope=scope,
            name="my-builder",
        )

  1. Create a new registry
from hangar_sdk.base import HangarScope, Asset, GitHubRepo, BuildkitBuilder, Registry

def hangar_run():
    scope = HangarScope(
        name="my-first-scope",
        region="us-west-2",
        roleAssumptions=[],
    )
    asset = Asset(
            scope=scope,
            name="<your-name>-my-first-bucket",
            source=GitHubRepo(
                scope=scope,
                repo="your-repo",
                branch="main",
                name="my-repo",
            ),
        )
    builder = BuildkitBuilder(
            scope=scope,
            name="my-builder",
        )
    registry = Registry(
            scope=scope,
            name="my-registry",
        )

  1. Create a new container builder

from hangar_sdk.base import HangarScope, Asset, GitHubRepo, BuildkitBuilder, Registry, ContainerBuilder

def hangar_run():
    scope = HangarScope(
        name="my-first-scope",
        region="us-west-2",
        roleAssumptions=[],
    )
    asset = Asset(
            scope=scope,
            name="<your-name>-my-first-bucket",
            source=GitHubRepo(
                scope=scope,
                repo="your-repo",
                branch="main",
                name="my-repo",
            ),
        )
    builder = BuildkitBuilder(
            scope=scope,
            name="my-builder",
        )
    registry = Registry(
            scope=scope,
            name="my-registry",
        )
    containerBuilder = ContainerBuilder(
            name="my-container-builder",
            asset=asset,
            builder=builder,
            registry=registry,
        )

  1. Create a new container and service
from hangar_sdk.base import HangarScope, Asset, GitHubRepo, BuildkitBuilder, Registry, ContainerBuilder, Container, Service

def hangar_run():
    scope = HangarScope(
        name="my-first-scope",
        region="us-west-2",
        roleAssumptions=[],
    )
    asset = Asset(
            scope=scope,
            name="<your-name>-my-first-bucket",
            source=GitHubRepo(
                scope=scope,
                repo="your-repo",
                branch="main",
                name="my-repo",
            ),
        )
    builder = BuildkitBuilder(
            scope=scope,
            name="my-builder",
        )
    registry = Registry(
            scope=scope,
            name="my-registry",
        )
    containerBuilder = ContainerBuilder(
            name="my-container-builder",
            asset=asset,
            builder=builder,
            registry=registry,
        )
    container = Container(
            source=registry,
            tag="latest",
        )
    service = Service(
            name="my-service",
            container=container,
        )

  1. Create a new cluster
from hangar_sdk.base import HangarScope, Asset, GitHubRepo, BuildkitBuilder, Registry, ContainerBuilder, Container, Service, Cluster

def hangar_run():
    scope = HangarScope(
        name="my-first-scope",
        region="us-west-2",
        roleAssumptions=[],
    )
    asset = Asset(
            scope=scope,
            name="<your-name>-my-first-bucket",
            source=GitHubRepo(
                scope=scope,
                repo="your-repo",
                branch="main",
                name="my-repo",
            ),
        )
    builder = BuildkitBuilder(
            scope=scope,
            name="my-builder",
        )
    registry = Registry(
            scope=scope,
            name="my-registry",
        )
    containerBuilder = ContainerBuilder(
            name="my-container-builder",
            asset=asset,
            builder=builder,
            registry=registry,
        )
    container = Container(
            source=registry,
            tag="latest",
        )
    service = Service(
            name="my-service",
            container=container,
        )
    cluster = Cluster(
            name="my-cluster",
            services=[service],
        )

  1. Resolve the cluster and builder

from hangar_sdk.base import HangarScope, Asset, GitHubRepo, BuildkitBuilder, Registry, ContainerBuilder, Container, Service, Cluster

def hangar_run():
    scope = HangarScope(
        name="my-first-scope",
        region="us-west-2",
        roleAssumptions=[],
    )
    asset = Asset(
            scope=scope,
            name="<your-name>-my-first-bucket",
            source=GitHubRepo(
                scope=scope,
                repo="your-repo",
                branch="main",
                name="my-repo",
            ),
        )
    builder = BuildkitBuilder(
            scope=scope,
            name="my-builder",
        )
    registry = Registry(
            scope=scope,
            name="my-registry",
        )
    containerBuilder = ContainerBuilder(
            name="my-container-builder",
            asset=asset,
            builder=builder,
            registry=registry,
        )
    container = Container(
            source=registry,
            tag="latest",
        )
    service = Service(
            name="my-service",
            container=container,
        )
    cluster = Cluster(
            name="my-cluster",
            services=[service],
        )
    asyncio.run(containerBuilder.resolve())
    asyncio.run(cluster.resolve())
  1. Run the hangar cli
hangar up <file>
  1. Trigger a build

from hangar_sdk.base import HangarScope, Asset, GitHubRepo, BuildkitBuilder, Registry, ContainerBuilder, Container, Service, Cluster

def hangar_run():
    scope = HangarScope(
        name="my-first-scope",
        region="us-west-2",
        roleAssumptions=[],
    )
    asset = Asset(
            scope=scope,
            name="<your-name>-my-first-bucket",
            source=GitHubRepo(
                scope=scope,
                repo="your-repo",
                branch="main",
                name="my-repo",
            ),
        )
    builder = BuildkitBuilder(
            scope=scope,
            name="my-builder",
        )
    registry = Registry(
            scope=scope,
            name="my-registry",
        )
    containerBuilder = ContainerBuilder(
            name="my-container-builder",
            asset=asset,
            builder=builder,
            registry=registry,
        )
    container = Container(
            source=registry,
            tag="latest",
        )
    service = Service(
            name="my-service",
            container=container,
        )
    cluster = Cluster(
            name="my-cluster",
            services=[service],
        )
    asyncio.run(containerBuilder.resolve())
    asyncio.run(cluster.resolve())


    asset.sync(token="<your-github-token>")
    containerBuilder.build()
  1. Run the hangar cli to create all the resources
hangar up <file>
  1. Once you are done delete all resources. Note: This doesnt delete all resources immediately but marks them for deletion. The resources will be deleted by hangar in atmost 10 minutes.
hangar down <file>

You can view your constructs, logs and other data in the hangar cloud console here.

Hangar CLI

Hangar CLI is a command line tool that allows you to interact with Hangar. It allows you to create and manage scopes, assets, and constructs. It also allows you to trigger builds on constructs.

Installation

pip install hangar-cli

HangarScope

HangarScope(
        region: str,
        name: str,
        roleAssumptions: list = [],
        api_key: str | None = None,
        endpoint: str = None,
    )

Hangar constructs or resources are grouped into scopes. A scope contains the configuration of where the resources will be deployed. The region provided to scope will be used as the aws region where all resources in that scope will be deployed to. The name provided to scope will be used as the name of the scope. The name of the scope is used to create a unique namespace for all resources in that scope. The roleAssumptions is a list of role arns that hangar will assume successively to create resources in that scope. The api_key is the hangar api key used to authenticate with hangar. The endpoint is the hangar api endpoint. If not provided, it will default to the hangar cloud endpoint.

Constructs

Cloud resources are created and represented by constructs. Unlike constructs in terraform, CDK and pulumi, constructs in Hangar not just create the resource but also allow you to interact with it. For example, the code build construct not only creates a code build instance but also allows you to trigger a build on it. Another difference hangar will reconcile any configuration drift in cloud resources from your definition. For example, if soemone else changes the configuration of a code build instance, hangar will reconcile it back to your definition. To actually create a construct you need to call the resolve method. Not every construct needs to be reolved only those that are not depended by any other contruct.

Actions

Actions are methods on constructs that allow you to interact with those cloud resources. For example, the build action on the code build construct allows you to trigger a build on the code build instance.

Asset

Asset(
    scope : HangarScope,
    name : str,
    source : SourceInterface
)

Actions:

asset = Asset(...)
asset.sync(token : str)

Any type of source code is an Asset. The Asset contsruct is used to define an asset which can then be consumed by other constructs. Currently the only type of Asset supported is a GitHub Repo. Support for local directories will be added soon.

The construct supports the sync action. If the source is a public github repo not onboarded to hangar, then sync requires you to pass a github token. If the source is a private github repo onboarded to hangar, then sync does not require a github token. The sync action pulls the source code from the source and stores it in an S3 bucket. The sync action is idempotent. If the source code has not changed, then the sync action will not do anything.

GithubRepo

GitHubRepo(
    scope : HangarScope,
    name : str,
    repo : str,
    branch : str,
)

(implements source interface)

To reference your private Github Repo make sure you have onboarded your github repo here. For any public repo, when using the sync action on asset, make sure to pass your github token.

BuildKit Builder

BuildkitBuilder(
    scope : HangarScope,
    name : str
)

This construct defines a BuildKit container used to build Docker images consumed by ContainerBuilder construct.

Registry

Registry(
    scope : HangarScope,
    name : str
)

(implements the registry interface)

Refers to an ECR registry container registry. Stores docker images.

Container Builder

ContainerBuilder(
    name: str,
    asset: Asset,
    builder: BuilderInterface,
    registry: RegistryInterface,
)

Actions:

containerBuilder = ContainerBuilder(...)
containerBuilder.build()

Creates a CodeBuild instance that consumes an asset, builder and a registry. When triggered using the 'build' action, it pulls the asset, builds it using the builder, and pushes image to registry.

The construct supports the build action. The build action triggers a build on the code build instance. The build action is idempotent. If the source code has not changed, then the build action will not do anything.

Container

class PortMappings(BaseModel):
    containerPort: int
    hostPort: int

Container(
    source: Registry
    tag: str
    startCommand: str [optional]
    portMappings: List[PortMappings] [optional])

Creates a container itself from an image in a registry. Can override the start command, best and port mappings.

Service

Service(
    name: str,
    container: Container,
    envVars: Dict[str, str] [optional]
)

Creates an ECS service. Consists of a container that it runs as part of the service. Also allos definition of env vars for the service.

Cluster

Cluster(
    name: str,
    services: List[Service],
)

Creates an ECS cluster. Consists of a list of services that run as part of the cluster.

Security Group

class Ingress(BaseModel):
    fromPort: int
    toPort: int
    protocol: str
    cidrIp: List[str]

class Egress(BaseModel):
    fromPort: int
    toPort: int
    protocol: str
    cidrIp: List[str]


SecurityGroup(
    scope: HangarScope,
    name: str,
    ingresses: List[Ingress],
    egresses: List[Egress],
)

Creates a security group. Consists of a list of ingress and egress rules. An ingress rule specifies what traffic is allowed through the security group. An egress rule specifies what traffic is allowed out of the security group.

Instance

Instance(
    scope: HangarScope,
    name: str,
    instanceType: str,
    securityGroups: List[SecurityGroup],
    path_to_key: str,
)

Actions:

instance = Instance(...)
output = instance.run_command(<command>)
print(output)
 {
    "stdout": <stdout>,
    "stderr": <stderr>,
    "command": <command>
 }

for output in instance.run_commands():
    print(output)
    {
        "stdout": <stdout>,
        "stderr": <stderr>,
        "command": <command>
    }

Creates an EC2 instance or a VM with ubuntu. Consists of an instance type, security groups and a path to a key pair. The instance type specifies the type of instance to create. The security groups specify what traffic is allowed through the instance. The path to key pair specifies the path to the key pair used to ssh into the instance. If the ssh variable is set to true, the the security group will have an ingress rule that allows ssh traffic through the instance even if it is not specified in the security groups. The instance will also have a public ip address.

The construct supports the run_command and run_commands actions. These actions ssh into the instance and execute the given shell commands. The run_command action runs a single command on the instance. The run_commands action runs a list of commands on the instance. The run_command and run_commands actions are not idempotent. WARNING: These commands could lead to different outputs everytime.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hangar_sdk-0.0.11.tar.gz (38.5 kB view details)

Uploaded Source

Built Distribution

hangar_sdk-0.0.11-py3-none-any.whl (40.0 kB view details)

Uploaded Python 3

File details

Details for the file hangar_sdk-0.0.11.tar.gz.

File metadata

  • Download URL: hangar_sdk-0.0.11.tar.gz
  • Upload date:
  • Size: 38.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.5.0-17-generic

File hashes

Hashes for hangar_sdk-0.0.11.tar.gz
Algorithm Hash digest
SHA256 b3f56b1fbf36b6cb75a27003bc89699414607fe38a2e506a2b092c758cae27a7
MD5 5106b9f1f45d63fe834657789c8d7cb5
BLAKE2b-256 4cfda490cd83bd11fe471c77c43b34ab6d0055bd49c5ecfeb81dbb45799c65c0

See more details on using hashes here.

File details

Details for the file hangar_sdk-0.0.11-py3-none-any.whl.

File metadata

  • Download URL: hangar_sdk-0.0.11-py3-none-any.whl
  • Upload date:
  • Size: 40.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.5.0-17-generic

File hashes

Hashes for hangar_sdk-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 ad6af093e69f3b1e527a9037bd3389d7b9abbefdabed4e5a8324220f7938097f
MD5 63d1919a423d5436a659d03b0433aacb
BLAKE2b-256 b480b6aa8e893e19bf7b3e44537731fdc31434ffca7f35c6ae8e3a7d30bf2422

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page