Skip to content

Commit

Permalink
Make CLI usable for preview
Browse files Browse the repository at this point in the history
  • Loading branch information
zachgiordano committed Nov 7, 2023
1 parent def574a commit 9362f9c
Show file tree
Hide file tree
Showing 14 changed files with 2,529 additions and 1,184 deletions.
49 changes: 44 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,56 @@ poetry install
poetry run gt
```

## Tests

```shell
poetry run pytest
```

## Griptape Cloud

Set environment variables:

```shell
export GRIPTAPE_CLOUD_USERNAME="<USERNAME>"
export GRIPTAPE_CLOUD_PASSWORD="<PASSWORD>"
export GRIPTAPE_CLOUD_API_KEY=<api_key>
```

Note: You can create an API Key using the [frontend](https://cloud-staging.griptape.ai/).

Configure the Cloud:

```shell
poetry run gt cloud configure
```

Note: Copy and paste the full 'name: ID' string in the terminal to select

Use the cloud command to make API calls:

```shell
poetry run gt cloud list-organizations
```

Login
Create an App from the template using the app command:

```shell
poetry run gt cloud login
```
poetry run gt app new --directory ~/workplace demo_app
```

Test an App locally:

```shell
poetry run gt app run --directory ~/workplace/demo_app --arg "what is griptape?"
```

Create an App on the Cloud:

```shell
poetry run gt cloud create-app --name "Demo App"
```

Create a Deployment for the App on the Cloud using the App ID:

```shell
poetry run gt cloud create-deployment --app-id ee95ba10-9e29-4759-b357-dc513821c5b2 --directory ~/workplace/demo_app
```
37 changes: 1 addition & 36 deletions griptape/cli/commands/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from griptape.artifacts import TextArtifact
from griptape.cli.core.app import App
from griptape.cli.core.app_deployer import AppDeployer
from griptape.cli.core.cloud_client import CloudClient
from griptape.cli.core.structure_runner import StructureRunner

from griptape.cli.core.utils.constants import DEFAULT_ENDPOINT_URL
Expand Down Expand Up @@ -64,39 +65,3 @@ def run(arg: list[str], directory: str) -> TextArtifact:

structure_runner = StructureRunner(arg=arg, app_directory=directory)
structure_runner.run()


@app.command(name="deploy")
@click.option(
"--directory",
"-d",
type=str,
help="Directory where app resides. Defaults to current directory.",
default=os.getcwd(),
show_default=True,
)
@click.option(
"--endpoint-url",
"-e",
type=str,
required=False,
help="Override default endpoint url",
default=DEFAULT_ENDPOINT_URL,
)
@click.option(
"--name",
"-n",
type=str,
help="Griptape Cloud app name",
default=None,
required=False,
)
def deploy(directory: str, endpoint_url: str, name: str) -> None:
"""
Deploy a Griptape app to Griptape Cloud.
"""

app_deployer = AppDeployer(
app_directory=directory, endpoint_url=endpoint_url, name=name
)
app_deployer.deploy()
204 changes: 193 additions & 11 deletions griptape/cli/commands/cloud.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import json
import os
from typing import Optional
import click
from click import echo
from griptape.cli.core.utils.auth import get_auth_token
from griptape.cli.core.app_deployer import AppDeployer
from griptape.cli.core.cloud_client import CloudClient
from griptape.cli.core.utils.constants import DEFAULT_ENDPOINT_URL


@click.group()
Expand All @@ -9,24 +14,201 @@ def cloud(ctx):
pass


@cloud.command(name="login")
@cloud.command(name="configure")
@click.option(
"--relogin",
"-r",
is_flag=True,
help="Set flag to force relogin",
"--endpoint-url",
"-e",
type=str,
required=False,
help="Override default endpoint url",
default=DEFAULT_ENDPOINT_URL,
)
def configure(endpoint_url: str) -> None:
try:
cloud_client = CloudClient(endpoint_url=endpoint_url)
organizations = cloud_client.list_organizations().json()["organizations"]
organization_choices = click.Choice(
[f"{org['name']}: {org['organization_id']}" for org in organizations]
)
value = click.prompt(
"Select the organization you want to use\n",
type=organization_choices,
show_choices=True,
)
index = organization_choices.choices.index(value)
org = organizations[index]

environments = cloud_client.list_environments(
organization_id=org["organization_id"]
).json()["environments"]

environment_choices = click.Choice(
[f"{env['name']}: {env['environment_id']}" for env in environments]
)
value = click.prompt(
"Select the environment you want to use\n",
type=environment_choices,
show_choices=True,
)
index = environment_choices.choices.index(value)
env = environments[index]

profile_name = click.prompt(
"Enter a name for this profile",
type=str,
show_default=True,
default="default",
)

cloud_client._write_gt_cloud_profile(
profile_name=profile_name,
organization_id=org["organization_id"],
environment_id=env["environment_id"],
)

echo(f"Successfully configured profile {profile_name}!")

except Exception as e:
echo(f"{e}")


@cloud.command(name="list-organizations")
@click.option(
"--endpoint-url",
"-e",
type=str,
required=False,
help="Override default endpoint url",
default=DEFAULT_ENDPOINT_URL,
)
def list_organizations(endpoint_url: str):
"""
List Griptape Cloud Organizations.
"""

cloud_client = CloudClient(endpoint_url=endpoint_url)
response = cloud_client.list_organizations()
echo(response.json())


@cloud.command(name="list-environments")
@click.option(
"--organization-id",
"-o",
type=str,
required=True,
help="Organization ID to list environments for",
)
@click.option(
"--endpoint-url",
"-e",
type=str,
required=False,
help="Override default endpoint url",
default=DEFAULT_ENDPOINT_URL,
)
def list_organizations(organization_id: str, endpoint_url: str):
"""
List Griptape Cloud Organizations.
"""

cloud_client = CloudClient(endpoint_url=endpoint_url)
response = cloud_client.list_environments(organization_id=organization_id)
echo(response.json())


@cloud.command(name="create-app")
@click.option(
"--name",
"-n",
type=str,
help="Griptape Cloud app name",
required=True,
)
@click.option(
"--endpoint-url",
"-e",
type=str,
required=False,
help="Override default endpoint url",
default="http://localhost:8000",
default=DEFAULT_ENDPOINT_URL,
)
@click.option(
"--environment-id",
"-v",
type=str,
help="Griptape Cloud environment id",
default=None,
required=False,
)
def login(relogin: bool, endpoint_url: str) -> None:
@click.option(
"--profile",
"-p",
type=str,
help="Griptape Cloud profile name",
default=None,
required=False,
)
def create_app(
name: str, endpoint_url: str, environment_id: Optional[str], profile: Optional[str]
) -> None:
"""
Create a Griptape App on the Griptape Cloud.
"""
app_data = {
"name": name,
}
cloud_client = CloudClient(endpoint_url=endpoint_url, profile=profile)
response = cloud_client.create_app(app_data=app_data, environment_id=environment_id)
echo(response.json())


@cloud.command(name="create-deployment")
@click.option(
"--app-id",
"-a",
type=str,
help="Griptape Cloud app id",
required=True,
)
@click.option(
"--directory",
"-d",
type=str,
help="Directory where app resides. Defaults to current directory.",
default=os.getcwd(),
show_default=True,
)
@click.option(
"--endpoint-url",
"-e",
type=str,
required=False,
help="Override default endpoint url",
default=DEFAULT_ENDPOINT_URL,
)
def create_deployment(app_id: str, directory: str, endpoint_url: str) -> None:
"""
Deploy a Griptape App to Griptape Cloud.
"""

cloud_client = CloudClient(endpoint_url=endpoint_url)

app_deployer = AppDeployer(
app_directory=directory,
endpoint_url=endpoint_url,
cloud_client=cloud_client,
)
source = app_deployer.get_deployment_source()

deployment_data = {"source": {"zip_file": {"base64_content": source}}}

try:
get_auth_token(relogin, endpoint_url)
echo("Login succeeded")
response = cloud_client.create_deployment(
deployment_data=deployment_data, app_id=app_id
)
if response.status_code != 202:
raise Exception(response.json())
except Exception as e:
echo(f"{e}")
echo(f"Unable to create deployment: {e}", err=True)
raise e
Loading

0 comments on commit 9362f9c

Please sign in to comment.