Skip to content

Commit

Permalink
agent init: Generate without secrets by default
Browse files Browse the repository at this point in the history
This closes #28
  • Loading branch information
discordianfish committed Feb 4, 2023
1 parent adb1720 commit 5fbaf8e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 36 deletions.
1 change: 1 addition & 0 deletions pkg/cmd/agent/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func NewInitCmd(logger *log.Logger) *cobra.Command {
Args: cobra.ExactArgs(1),
}
cmd.Flags().StringVar(&config.Python.Version, "python.version", config.Python.Version, "Python version to use")
cmd.Flags().BoolVar(&config.Secret, "secret", config.Secret, "Include secret in agent")

return cmd
}
2 changes: 1 addition & 1 deletion pkg/container/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func NewDockerRunner(logger log.Logger, client *client.Client, autoRemove bool)
func (r *DockerRunner) Pull(c *Container, output *os.File) error {
reader, err := r.Client.ImagePull(context.TODO(), c.Image, types.ImagePullOptions{})
if err != nil {
return fmt.Errorf("couldn't pull image %s: %w:\nTo disable pulling the image on start, retry with --pull=false", c.Image, err)
return fmt.Errorf("couldn't pull image %s: %w:\nTo disable pulling the image on start, retry with --images.pull=false", c.Image, err)
}
defer reader.Close()

Expand Down
39 changes: 12 additions & 27 deletions pkg/diambra/agents/README.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,16 @@ This is a sample agent consisting of the following files:

- [agent.py](agent.py) - The agent code
- [requirements.txt](requirements.txt) - The dependencies for the agent
{{ if .Secret }}
- [Dockerfile](Dockerfile) - To build the image with all agent dependencies


## Usage

### Create public image with dependencies

1. Edit `requirements.txt` to add your dependencies
2. Build the image with `docker build -t registry/image .`
1. You can use any public registry for this, like [quay.io](quay.io) or [dockerhub](dockerhub.com)
3. Push the image to the registry with `docker push registry/image`

This image needs to be public so that it can be pulled by the DIAMBRA platform.

### Create private agent and model
1. Edit agent.py to add your code
2. Train your agent
3. Host your agent and model somewhere where it can be accessed by the DIAMBRA platform
1. You can use any service that provides the files via https, like [github](github.com),
[gitlab](gitlab.com) or [huggingface](huggingface.co)
4. Edit [submission.yaml](submission.yaml) and:
1. Specify the image you created in the previous step
2. Add your agent and model urls, using {{`{{ .Secrets.<secret_name> }}`}} to reference secrets

**DO NOT ADD SECRETS DIRECTLY TO THE MANIFEST. THEY WILL BE PUBLICLY VISIBLE.**

### Submit agent
Run `diambra agent submit --manifest submission.yaml -s <secret_name>=<secret_value>` to submit your agent.
- [submission.yaml](submission.yaml) - The manifest including (secret) sources
{{ else }}
- [Dockerfile](Dockerfile) - To build the image with agent and all dependencies
{{ end }}

## Submit agent
{{ if .Secret }}
Run `diambra agent submit --submission.manifest submission.yaml -s <secret_name>=<secret_value>` to submit your agent.
{{ else }}
Run `diambra agent submit registy/image:tag` to submit your agent.
{{ end }}
5 changes: 5 additions & 0 deletions pkg/diambra/agents/dockerfile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ RUN apt-get -qy update && \
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

{{ if not .Secret }}
COPY . .
ENTRYPOINT [ "python", "/app/agent.py" ]
{{ end }}
43 changes: 38 additions & 5 deletions pkg/diambra/agents/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Config struct {
Python PythonConfig
BaseImage BaseImageConfig
Arena ArenaConfig
Secret bool
}

func NewConfig(logger log.Logger) (*Config, error) {
Expand Down Expand Up @@ -102,13 +103,24 @@ func WriteFile(logger log.Logger, path, name, tmpl string, config *Config) error
return err
}
diffs := differ.DiffMain(new.String(), string(old), true)
if len(diffs) > 1 {
level.Info(logger).Log("msg", name+" has local changes, skipping:", "name", name)
fmt.Println(differ.DiffPrettyText(diffs))
if len(diffs) < 2 {
level.Info(logger).Log("msg", "Skipping "+name+", content identical", "file", name)
return nil
}

level.Info(logger).Log("msg", name+" has local changes:", "name", name)
fmt.Println(differ.DiffPrettyText(diffs))
level.Info(logger).Log("msg", "Overwrite "+name+"? [y/N]", "name", name)

var answer string
// FIXME: There must be a better way to do this
if _, err := fmt.Scanln(&answer); err != nil && err.Error() != "unexpected newline" {
return fmt.Errorf("couldn't read answer: %w", err)
}
if answer != "y" {
level.Info(logger).Log("msg", "Skipping "+name, "name", name)
return nil
}
level.Info(logger).Log("msg", "Skipping "+name+", content identical", "file", name)
return nil
}
fh, err := os.Create(filepath.Join(path, name))
if err != nil {
Expand All @@ -122,6 +134,27 @@ func Generate(logger log.Logger, path string, config *Config) error {
if err := os.MkdirAll(path, 0755); err != nil {
return err
}
if config.Secret {
return generateWithSecrets(logger, path, config)
}
return generateWithoutSecrets(logger, path, config)
}

func generateWithoutSecrets(logger log.Logger, path string, config *Config) error {
for name, tmpl := range map[string]string{
"Dockerfile": DockerfileTemplate,
"requirements.txt": RequirementsTxt,
"agent.py": AgentPyTemplate,
"README.md": ReadmeTemplate,
} {
if err := WriteFile(logger, path, name, tmpl, config); err != nil {
return err
}
}
return nil
}

func generateWithSecrets(logger log.Logger, path string, config *Config) error {
for name, tmpl := range map[string]string{
"Dockerfile": DockerfileTemplate,
"requirements.txt": RequirementsTxt,
Expand Down
5 changes: 2 additions & 3 deletions pkg/diambra/agents/submission.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ image: your/image:latest
command:
- python
- "/sources/agent.py"
- play
- "/sources/model.zip"
sources:
agent.py: https://ziemke:{{`{{.Secrets.token}}`}}@example.com/agent.py
model.zip: https://ziemke:{{`{{.Secrets.token}}`}}@example.com/model.zip
agent.py: https://username:{{`{{.Secrets.token}}`}}@example.com/agent.py
model.zip: https://username:{{`{{.Secrets.token}}`}}@example.com/model.zip

0 comments on commit 5fbaf8e

Please sign in to comment.