Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stackpack: Gitea #29

Merged
merged 4 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,4 @@ node_modules/
requirements.txt

/tmp
/stackpack_input.yaml
6 changes: 3 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"request": "launch",
"module": "src.stack_pack",
"args": [
"-c=CPU=512",
"-c=DBPassword=123456",
"-c=EnableS3=True",
// "-c=CPU=512",
"-c=DBPassword=hunter23456",
// "-c=EnableS3=True",
"${file}"
]
}
Expand Down
9 changes: 4 additions & 5 deletions local_testing.http
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ Content-Type: application/json

{
"configuration": {
"mattermost": {
"DBPassword": "hunter2456"
"gitea": {
}
}
}
Expand All @@ -19,8 +18,8 @@ Content-Type: application/json
"region": "us-east-1",
"assume_role_arn": "arn:aws:iam::064389020595:role/TestDeployerStackPacks",
"configuration": {
"mattermost": {
"DBPassword": "hunter2456"
"gitea": {
"DBPassword": "hunter2345"
}
}
}
Expand All @@ -31,7 +30,7 @@ GET http://localhost:3000/api/stack

###

POST http://localhost:3000/api/install
POST http://localhost:3000/api/install?regen=true

###

Expand Down
14 changes: 12 additions & 2 deletions src/api/deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
stream_deployment_events,
)
from src.stack_pack import get_stack_packs
from src.stack_pack.storage.iac_storage import IacStorage
from src.stack_pack.models.user_pack import UserPack
from src.dependencies.injection import get_iac_storage
from src.util.tmp import TempDir

router = APIRouter()

Expand All @@ -30,11 +30,19 @@ def read_zip_to_bytes(zip_file_path):
@router.post("/api/install")
async def install(
request: Request,
regen: bool = False,
):
user_id = await get_user_id(request)
user_pack = UserPack.get(user_id, user_id)
store = get_iac_storage()
iac = store.get_iac(user_pack.id)

tmp_dir = TempDir()
if regen:
logger.info("Regenerating iac for %s", user_pack.id)
stack_packs = get_stack_packs()
_, iac = await user_pack.run_pack(stack_packs, store, tmp_dir.dir)
else:
iac = store.get_iac(user_pack.id)
Comment on lines +39 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just as a note, were always going to have to regenerate if the base stack hasnt been deployed since we wont know the imports yet, so this will likely change a little soon

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, makes sense. I just made this real quick since I kept forgetting to run the PATCH before install.


sps = get_stack_packs()

Expand All @@ -56,6 +64,7 @@ async def install(
user_id,
iac,
pulumi_config,
tmp_dir,
),
)
p.start()
Expand Down Expand Up @@ -95,6 +104,7 @@ async def tear_down(
user_id,
iac,
pulumi_config,
TempDir(),
),
)
p.start()
Expand Down
9 changes: 7 additions & 2 deletions src/api/stack_packs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from src.stack_pack.models.user_pack import UserPack, UserStack
from src.stack_pack import ConfigValues, get_stack_packs, StackConfig
from src.util.tmp import TempDir

router = APIRouter()

Expand Down Expand Up @@ -44,7 +45,8 @@ async def create_stack(
assumed_role_arn=body.assumed_role_arn,
)
stack_packs = get_stack_packs()
policy = await user_pack.run_pack(stack_packs, get_iac_storage())
with TempDir() as tmp_dir:
policy, _ = await user_pack.run_pack(stack_packs, get_iac_storage(), tmp_dir)
user_pack.save()
return StackResponse(stack=user_pack.to_user_stack(), policy=policy)

Expand Down Expand Up @@ -76,7 +78,10 @@ async def update_stack(

if body.configuration:
stack_packs = get_stack_packs()
policy = await user_pack.run_pack(stack_packs, get_iac_storage())
with TempDir() as tmp_dir:
policy, _ = await user_pack.run_pack(
stack_packs, get_iac_storage(), tmp_dir
)

return {"stack": user_pack.to_user_stack(), "policy": policy}

Expand Down
4 changes: 0 additions & 4 deletions src/dependencies/injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
from src.stack_pack.storage.iac_storage import IacStorage


def create_sts_client():
return boto3.client("sts")


if os.getenv("IAC_BUCKET", None) is None:
s3_resource = boto3.resource(
"s3",
Expand Down
102 changes: 55 additions & 47 deletions src/deployer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Dict
import uuid
from fastapi import Request
from src.dependencies.injection import create_sts_client
from src.deployer.pulumi.builder import AppBuilder
from src.deployer.pulumi.deployer import AppDeployer
from src.deployer.models.deployment import (
Expand All @@ -15,6 +14,7 @@
from src.util.logging import logger
from multiprocessing import Queue
from queue import Empty
from src.util.tmp import TempDir


# This would be a dictionary, database, or some other form of storage in a real application
Expand All @@ -29,6 +29,7 @@ async def build_and_deploy(
user: str,
iac: bytes,
pulumi_config: dict[str, str],
tmp_dir: str,
):
deployment_id = str(uuid.uuid4())
pulumi_stack = PulumiStack(
Expand All @@ -49,28 +50,28 @@ async def build_and_deploy(

pulumi_stack.save()
deployment.save()
with AppBuilder(create_sts_client()) as builder:
stack = builder.prepare_stack(iac, pulumi_stack)
builder.configure_aws(stack, assume_role_arn, region)
for k, v in pulumi_config.items():
stack.set_config(k, auto.ConfigValue(v, secret=True))
deployer = AppDeployer(
stack,
)
result_status, reason = await deployer.deploy(q)
await q.put("Done")
pulumi_stack.update(
actions=[
PulumiStack.status.set(result_status.value),
PulumiStack.status_reason.set(reason),
]
)
deployment.update(
actions=[
Deployment.status.set(result_status.value),
Deployment.status_reason.set(reason),
]
)
builder = AppBuilder(tmp_dir)
stack = builder.prepare_stack(iac, pulumi_stack)
builder.configure_aws(stack, assume_role_arn, region)
for k, v in pulumi_config.items():
stack.set_config(k, auto.ConfigValue(v, secret=True))
deployer = AppDeployer(
stack,
)
result_status, reason = await deployer.deploy(q)
await q.put("Done")
pulumi_stack.update(
actions=[
PulumiStack.status.set(result_status.value),
PulumiStack.status_reason.set(reason),
]
)
deployment.update(
actions=[
Deployment.status.set(result_status.value),
Deployment.status_reason.set(reason),
]
)


def run_build_and_deploy(
Expand All @@ -80,13 +81,17 @@ def run_build_and_deploy(
user: str,
iac: bytes,
pulumi_config: dict[str, str],
tmp_dir: TempDir,
):
new_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_loop)
new_loop.run_until_complete(
build_and_deploy(q, region, assume_role_arn, user, iac, pulumi_config)
build_and_deploy(
q, region, assume_role_arn, user, iac, pulumi_config, tmp_dir.dir
)
)
new_loop.close()
tmp_dir.cleanup()


async def run_destroy(
Expand All @@ -96,6 +101,7 @@ async def run_destroy(
user: str,
iac: bytes,
pulumi_config: dict[str, str],
tmp_dir: str,
):
deployment_id = str(uuid.uuid4())
pulumi_stack = PulumiStack(
Expand All @@ -116,28 +122,28 @@ async def run_destroy(

pulumi_stack.save()
deployment.save()
with AppBuilder(create_sts_client()) as builder:
stack = builder.prepare_stack(iac, pulumi_stack)
for k, v in pulumi_config.items():
stack.set_config(k, auto.ConfigValue(v, secret=True))
builder.configure_aws(stack, assume_role_arn, region)
deployer = AppDeployer(
stack,
)
result_status, reason = await deployer.destroy_and_remove_stack(q)
await q.put("Done")
pulumi_stack.update(
actions=[
PulumiStack.status.set(result_status.value),
PulumiStack.status_reason.set(reason),
]
)
deployment.update(
actions=[
Deployment.status.set(result_status.value),
Deployment.status_reason.set(reason),
]
)
builder = AppBuilder(tmp_dir)
stack = builder.prepare_stack(iac, pulumi_stack)
for k, v in pulumi_config.items():
stack.set_config(k, auto.ConfigValue(v, secret=True))
builder.configure_aws(stack, assume_role_arn, region)
deployer = AppDeployer(
stack,
)
result_status, reason = await deployer.destroy_and_remove_stack(q)
await q.put("Done")
pulumi_stack.update(
actions=[
PulumiStack.status.set(result_status.value),
PulumiStack.status_reason.set(reason),
]
)
deployment.update(
actions=[
Deployment.status.set(result_status.value),
Deployment.status_reason.set(reason),
]
)


def run_destroy_loop(
Expand All @@ -147,13 +153,15 @@ def run_destroy_loop(
user: str,
iac: bytes,
pulumi_config: dict[str, str],
tmp_dir: TempDir,
):
new_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_loop)
new_loop.run_until_complete(
run_destroy(q, region, assume_role_arn, user, iac, pulumi_config)
run_destroy(q, region, assume_role_arn, user, iac, pulumi_config, tmp_dir.dir)
)
new_loop.close()
tmp_dir.cleanup()


async def stream_deployment_events(request: Request, id: str):
Expand Down
12 changes: 2 additions & 10 deletions src/deployer/pulumi/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,8 @@


class AppBuilder:
def __init__(self, sts_client):
self.sts_client = sts_client
self.tmpdir = TempDir()

def __enter__(self):
self.output_dir = self.tmpdir.__enter__()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.tmpdir.__exit__(exc_type, exc_val, exc_tb)
def __init__(self, output_dir: str):
self.output_dir = output_dir

def prepare_stack(self, iac: bytes, pulumi_stack: PulumiStack) -> auto.Stack:
self.create_output_dir(iac)
Expand Down
6 changes: 3 additions & 3 deletions src/engine_service/engine_commands/export_iac.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ class ExportIacRequest(NamedTuple):

async def export_iac(request: ExportIacRequest):
tmp_dir = request.tmp_dir
dir = Path(tmp_dir)
dir = Path(tmp_dir).absolute()

args = []

with open(dir / "graph.yaml", "w") as file:
file.write(request.input_graph)
args.append("--input-graph")
args.append(f"{tmp_dir}/graph.yaml")
args.append(f"{dir}/graph.yaml")

args.extend(
[
"--provider",
"pulumi",
"--output-dir",
tmp_dir,
str(dir),
"--app-name",
request.name,
]
Expand Down
Loading
Loading