Skip to content

Commit

Permalink
efron: checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ef0xa committed Aug 23, 2024
1 parent 252c051 commit 9012cb3
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 4 deletions.
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.23 AS builder
RUN apt-get update
# do the thing with cache directories from my article, I'll do it later
WORKDIR /app

COPY main.go main.go
COPY go.mod go.mod
COPY go.sum go.sum
RUN --mount=type=cache,target=/go/pkg/mod/ go mod download
COPY . .
# build our application
RUN --mount=type=cache,target=/root/.cache/go-build GOOS=linux GOARCH=amd64 go build -o runpodctl .
RUN --mount=type=cache,target=/root/.cache/go-build GOOS=linux GOARCH=amd64 go build -o configure-github-action ./cmd/configure-github-action

FROM ubuntu:latest AS runner
COPY --from=builder /app/runpodctl /app/runpodctl
COPY --from=builder /app/configure-github-action /app/configure-github-action
36 changes: 36 additions & 0 deletions cmd/configure-github-action/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: "build-dockerfile"
on: [push]
jobs:
docker-build:
runs-on: efrondev/test-runpod-runner
steps:
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: checkout
uses: actions/checkout@v4

- name: generate-config-if-necessary
run: |
/app/configure-github-action
- name:

# Build and push step
- name: Build and push
uses: docker/build-push-action@v4
with:
push: true
tags: ${{ vars.DOCKERHUB_REPO }}/${{ vars.DOCKERHUB_IMG }}:${{ (github.event_name == 'release' && github.event.release.tag_name) || (github.event_name == 'workflow_dispatch' && github.event.inputs.image_tag) || 'dev' }}

- name: deploy-endpoint
run: |
runpodctl project deploy --mode=dockerfile
70 changes: 70 additions & 0 deletions cmd/configure-github-action/configure-github-action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// configure-github-action is a small tool that generates a Dockerfile and endpoint.toml file from a runpod.toml file.
// If you already have those two, this is a no-op.
package main

import (
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)

type EndpointConfig struct{}

func main() {
dir := "."
if len(os.Args) > 1 {
dir = os.Args[1]
}
log.SetPrefix("configure-github-action: ")
log.Printf("start: dir=%s", dir)
if err := build(context.TODO(), dir); err != nil {
log.Fatalf("fail: %v", err)
}
log.Printf("done")
}

// if they have dockerfile+endpoint.toml, use that
// otherwise, use runpod.toml to generate the dockerfile
func build(ctx context.Context, dir string) error {
exists := func(path string) bool {
_, err := os.Stat(filepath.Join(".", path))
return err == nil
}
// runpodctl commmands only work on current directory, so we chdir
oldDir, _ := os.Getwd()
os.Chdir(dir)
defer os.Chdir(oldDir)

switch { // bounds checks
case !exists("runpod.toml") && !exists("Dockerfile") && !exists("endpoint.toml"):
return errors.New("no runpod.toml, Dockerfile, or endpoint.toml found... did you choose the right directory?")
case exists("runpod.toml") && (exists("Dockerfile") || exists("endpoint.toml")):
return errors.New("expected no Dockerfile or endpoint.toml when runpod.toml is present")
case exists("runpod.toml"):
log.Print("found runpod.toml")

for _, cmdAndArgs := range [][]string{
{"runpodctl", "project", "generate-endpoint-config"},
{"runpodctl", "project", "build-dockerfile"},
} {
log.Printf("start %v", strings.Join(cmdAndArgs, " "))
cmd := exec.CommandContext(ctx, cmdAndArgs[0], cmdAndArgs[1:]...)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("%v: %v", strings.Join(cmdAndArgs, " "), err)
}
}
log.Printf("generated Dockerfile and endpoint.toml")
return nil
case exists("Dockerfile") && exists("endpoint.toml"):
log.Print("found pre-existing Dockerfile and endpoint.toml")
return nil
default:
return errors.New("expected either 'runpod.toml' or both 'Dockerfile' and 'endpoint.toml' to exist")
}
}
2 changes: 1 addition & 1 deletion cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ func init() {
projectCmd.AddCommand(project.StartProjectCmd)
projectCmd.AddCommand(project.DeployProjectCmd)
projectCmd.AddCommand(project.DeployProjectFromEndpointConfigCmd)
projectCmd.AddCommand(project.BuildProjectCmd)
projectCmd.AddCommand(project.BuildProjectDockerfileCmd)
projectCmd.AddCommand(project.GenerateEndpointConfigCmd)
}
7 changes: 4 additions & 3 deletions cmd/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,9 @@ var GenerateEndpointConfigCmd = &cobra.Command{
}
},
}
var BuildProjectCmd = &cobra.Command{
Use: "build",

var BuildProjectDockerfileCmd = &cobra.Command{
Use: "build-dockerfile",
Args: cobra.ExactArgs(0),
Short: "builds Dockerfile for current project",
Long: "builds a local Dockerfile for the project in the current folder. You can use this Dockerfile to build an image and deploy it to any API server.",
Expand Down Expand Up @@ -388,5 +389,5 @@ func init() {
NewProjectCmd.Flags().StringVarP(&modelType, "type", "t", "", "Specify the model type for the project.")

StartProjectCmd.Flags().BoolVar(&showPrefixInPodLogs, "prefix-pod-logs", true, "Include the Pod ID as a prefix in log messages from the project Pod.")
BuildProjectCmd.Flags().BoolVar(&includeEnvInDockerfile, "include-env", false, "Incorporate environment variables defined in runpod.toml into the generated Dockerfile.")
BuildProjectDockerfileCmd.Flags().BoolVar(&includeEnvInDockerfile, "include-env", false, "Incorporate environment variables defined in runpod.toml into the generated Dockerfile.")
}
Binary file added configure-github-action
Binary file not shown.
7 changes: 7 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

local: version
go build -o bin/runpodctl .
go build -o bin/configure-github-action ./cmd/configure-github-action

release: buildall strip compress

Expand All @@ -16,16 +17,22 @@ strip:

android-arm64: version
env GOOS=android GOARCH=arm64 go build -o bin/runpodctl-android-arm64 .
env GOOS=android GOARCH=arm64 go build -o bin/configure-github-action-android-arm64 ./cmd/configure-github-action
linux-amd64: version
env GOOS=linux GOARCH=amd64 go build -o bin/runpodctl-linux-amd64 .
env GOOS=linux GOARCH=amd64 go build -o bin/configure-github-action-linux-amd64 ./cmd/configure-github-action
darwin-arm64: version
env GOOS=darwin GOARCH=arm64 go build -o bin/runpodctl-darwin-arm64 .
env GOOS=darwin GOARCH=arm64 go build -o bin/configure-github-action-darwin-arm64 ./cmd/configure-github-action
windows-amd64: version
env GOOS=windows GOARCH=amd64 go build -o bin/runpodctl-windows-amd64.exe .
env GOOS=windows GOARCH=amd64 go build -o bin/configure-github-action-windows-amd64.exe ./cmd/configure-github-action
windows-arm64: version
env GOOS=windows GOARCH=arm64 go build -o bin/runpodctl-windows-arm64.exe .
env GOOS=windows GOARCH=arm64 go build -o bin/configure-github-action-windows-arm64.exe ./cmd/configure-github-action
darwin-amd64: version
env GOOS=darwin GOARCH=amd64 go build -o bin/runpodctl-darwin-amd64 .
env GOOS=darwin GOARCH=amd64 go build -o bin/configure-github-action-darwin-amd64 ./cmd/configure-github-action

lint: version
golangci-lint run
Expand Down

0 comments on commit 9012cb3

Please sign in to comment.