Skip to content

Commit ed47b15

Browse files
committed
test kubernetes job
Signed-off-by: vsoch <[email protected]>
1 parent c44e552 commit ed47b15

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

fractale/agent/build/agent.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from fractale.agent.base import Agent
22
import fractale.agent.build.prompts as prompts
33
from fractale.agent.context import get_context
4+
import fractale.agent.defaults as defaults
45

56
import fractale.utils as utils
67
import argparse
@@ -64,11 +65,6 @@ def add_arguments(self, subparser):
6465
def run(self, context):
6566
"""
6667
Run the agent.
67-
68-
TODO: get working here
69-
Then think about how to move between steps. We will want to be able to
70-
get initial variables from a plan for redoing steps. OR keep the last
71-
state of running it.
7268
"""
7369
import google.generativeai as genai
7470

@@ -79,7 +75,7 @@ def run(self, context):
7975
self.attempts = self.attempts or 0
8076

8177
# Map context into needed arguments and validate
82-
environment = context.get('environment', "generic cloud environment")
78+
environment = context.get('environment', defaults.environment)
8379
application = context.get('application', required=True)
8480

8581
# These are optional if we are doing a follow up build
@@ -94,7 +90,7 @@ def run(self, context):
9490
# This will either generate fresh or rebuild erroneous Dockerfile
9591
# We don't return the dockerfile because it is updated in the context
9692
context.dockerfile = self.generate_dockerfile(
97-
context.application, context.environment, error_message=error_message, dockerfile=dockerfile
93+
context.application, environment, error_message=error_message, dockerfile=dockerfile
9894
)
9995
print(Panel(context.dockerfile, title="[green]Dockerfile[/green]", border_style="green"))
10096

@@ -183,7 +179,7 @@ def build(self, dockerfile, image_name):
183179
utils.write_file(dockerfile, os.path.join(build_dir, "Dockerfile"))
184180
print(
185181
Panel(
186-
f"Attempt {self.attempts} to build image: [bold cyan]{image_name}[/bold cyan]",
182+
f"Attempt {self.attempts+1} to build image: [bold cyan]{image_name}[/bold cyan]",
187183
title="[blue]Docker Build[/blue]",
188184
border_style="blue",
189185
)

fractale/agent/defaults.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
environment = "generic cloud environment"

fractale/agent/kubernetes_job/agent.py

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from rich.panel import Panel
1515
from rich.syntax import Syntax
1616

17+
from fractale.agent.context import get_context
18+
import fractale.agent.defaults as defaults
1719
import fractale.agent.kubernetes_job.prompts as prompts
1820
import fractale.utils as utils
1921
from fractale.agent.base import Agent
@@ -66,7 +68,7 @@ def add_arguments(self, subparser):
6668
dest="agent_name",
6769
)
6870

69-
def run(self, args, extra, error_message=None, template=None, context=None, attempts=0):
71+
def run(self, context, template=None):
7072
"""
7173
Run the agent.
7274
"""
@@ -77,28 +79,44 @@ def run(self, args, extra, error_message=None, template=None, context=None, atte
7779
except KeyError:
7880
sys.exit("ERROR: GEMINI_API_KEY environment variable not set.")
7981

82+
# Init attempts. Each agent has an internal counter for total attempts
83+
self.attempts = self.attempts or 0
84+
85+
# Create or get global context
86+
context = get_context(context)
87+
error_message = context.get('error_message')
88+
8089
# If we have a context file, add to template
81-
context = None
82-
if args.context_file is not None:
83-
context = utils.read_file(args.context_file)
90+
context_file = context.get('context_file')
91+
build_context = context.get('build_context')
92+
if context_file is not None and build_context is None:
93+
build_context = utils.read_file(context_file)
94+
context.build_context = build_context
95+
96+
# These are required
97+
container = context.get('container', required=True)
98+
99+
# Assume most containers are built and loaded
100+
environment = context.get('environment', defaults.environment)
101+
no_pull = context.get('no_pull', True)
84102

85103
# This will either generate fresh or rebuild erroneous Job
86104
job_crd = self.generate_crd(
87-
container=args.container,
88-
environment=args.environment,
105+
container=container,
106+
environment=environment,
89107
error_message=error_message,
90108
template=template,
91-
context=context,
92-
no_pull=args.no_pull,
109+
context=context.build_context,
110+
no_pull=no_pull,
93111
)
94112
print(Panel(job_crd, title="[green]job.yaml[/green]", border_style="green"))
95113

96114
# Make and deploy it! Success is exit code 0.
97-
return_code, output = self.deploy(job_crd, image_name=args.container, attempt=attempts)
115+
return_code, output = self.deploy(job_crd, image_name=container)
98116
if return_code == 0:
99117
print(
100118
Panel(
101-
f"[bold green]✅ Deploy complete in {attempts} attempts[/bold green]",
119+
f"[bold green]✅ Deploy complete in {self.attempts} attempts[/bold green]",
102120
title="Success",
103121
border_style="green",
104122
)
@@ -111,24 +129,21 @@ def run(self, args, extra, error_message=None, template=None, context=None, atte
111129
border_style="red",
112130
)
113131
)
114-
attempts += 1
115132
print("\n[bold cyan] Requesting Correction from Kubernetes Job Agent[/bold cyan]")
133+
self.attempts += 1
116134

117135
# Trigger again, provide initial context and error message
136+
context.error_message = output
118137
return self.run(
119-
args,
120-
extra,
121-
error_message=output,
122138
template=job_crd,
123139
context=context,
124-
attempts=attempts,
125140
)
126141

127142
# Add generation line
128143
job_crd += "\n# Generated by fractale build agent"
129144
self.print_crd(job_crd)
130-
if args.outfile:
131-
utils.write_file(job_crd, args.outfile)
145+
if context.outfile:
146+
utils.write_file(job_crd, context.outfile)
132147
return job_crd
133148

134149
def print_crd(self, job_crd):
@@ -274,7 +289,7 @@ def cleanup_job(self, job_name, namespace):
274289
check=False,
275290
)
276291

277-
def deploy(self, job_crd, image_name, attempt):
292+
def deploy(self, job_crd, image_name):
278293
"""
279294
Deploy the Kubernetes Job.
280295
"""
@@ -320,7 +335,7 @@ def deploy(self, job_crd, image_name, attempt):
320335
utils.write_file(job_crd, job_manifest_path)
321336
print(
322337
Panel(
323-
f"Attempt {attempt} to deploy Kubernetes Job: [bold cyan]{image_name}[/bold cyan]",
338+
f"Attempt {self.attempts+1} to deploy Kubernetes Job: [bold cyan]{image_name}[/bold cyan]",
324339
title="[blue]Kubernetes Job[/blue]",
325340
border_style="blue",
326341
)

0 commit comments

Comments
 (0)