Skip to content

Commit

Permalink
feat: runpodctl project build to emit dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
DireLines committed Jan 10, 2024
1 parent 6db2d7e commit 7844b5d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ func init() {
projectCmd.AddCommand(project.NewProjectCmd)
projectCmd.AddCommand(project.StartProjectCmd)
projectCmd.AddCommand(project.DeployProjectCmd)
// projectCmd.AddCommand(project.BuildProjectCmd)
projectCmd.AddCommand(project.BuildProjectCmd)
}
10 changes: 6 additions & 4 deletions cmd/project/exampleDockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# AUTOGENERATED Dockerfile using runpodctl project build

# Base image -> https://github.com/runpod/containers/blob/main/official-templates/base/Dockerfile
# DockerHub -> https://hub.docker.com/r/runpod/base/tags
FROM <<BASE_IMAGE>>
Expand All @@ -8,8 +10,8 @@ FROM <<BASE_IMAGE>>

# Python dependencies
COPY <<REQUIREMENTS_PATH>> /requirements.txt
RUN python3.11 -m pip install --upgrade pip && \
python3.11 -m pip install --upgrade -r /requirements.txt --no-cache-dir && \
RUN python<<PYTHON_VERSION>> -m pip install --upgrade pip && \
python<<PYTHON_VERSION>> -m pip install --upgrade -r /requirements.txt --no-cache-dir && \
rm /requirements.txt

# NOTE: The base image comes with multiple Python versions pre-installed.
Expand All @@ -18,5 +20,5 @@ RUN python3.11 -m pip install --upgrade pip && \

# Add src files (Worker Template)
ADD src .

CMD python3.11 -u <<HANDLER_PATH>>
<<SET_ENV_VARS>>
CMD python<<PYTHON_VERSION>> -u <<HANDLER_PATH>>
38 changes: 38 additions & 0 deletions cmd/project/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ func mapToApiEnv(env map[string]string) []*api.PodEnv {
}
return podEnv
}
func formatAsDockerEnv(env map[string]string) string {
result := ""
for k, v := range env {
result += fmt.Sprintf("ENV %s=%s\n", k, v)
}
return result
}

func startProject(networkVolumeId string) error {
//parse project toml
Expand Down Expand Up @@ -478,3 +485,34 @@ func deployProject(networkVolumeId string) (endpointId string, err error) {
}
return deployedEndpointId, nil
}

func buildProjectDockerfile() {
//parse project toml
config := loadProjectConfig()
projectConfig := config.Get("project").(*toml.Tree)
runtimeConfig := config.Get("runtime").(*toml.Tree)
//build Dockerfile
dockerfileBytes, _ := dockerfileTemplate.ReadFile("exampleDockerfile")
dockerfile := string(dockerfileBytes)
//base image: from toml
dockerfile = strings.ReplaceAll(dockerfile, "<<BASE_IMAGE>>", projectConfig.Get("base_image").(string))
//pip requirements
dockerfile = strings.ReplaceAll(dockerfile, "<<REQUIREMENTS_PATH>>", runtimeConfig.Get("requirements_path").(string))
dockerfile = strings.ReplaceAll(dockerfile, "<<PYTHON_VERSION>>", runtimeConfig.Get("python_version").(string))
//cmd: start handler
dockerfile = strings.ReplaceAll(dockerfile, "<<HANDLER_PATH>>", runtimeConfig.Get("handler_path").(string))
if includeEnvInDockerfile {
dockerEnv := formatAsDockerEnv(createEnvVars(config))
dockerfile = strings.ReplaceAll(dockerfile, "<<SET_ENV_VARS>>", "\n"+dockerEnv)
} else {
dockerfile = strings.ReplaceAll(dockerfile, "<<SET_ENV_VARS>>", "")
}
//save to Dockerfile in project directory
projectFolder, _ := os.Getwd()
dockerfilePath := filepath.Join(projectFolder, "Dockerfile")
os.WriteFile(dockerfilePath, []byte(dockerfile), 0644)
//print next steps
//docker build
//dockerhub push
//go to runpod url and deploy
}
33 changes: 12 additions & 21 deletions cmd/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var modelType string
var modelName string
var initCurrentDir bool
var setDefaultNetworkVolume bool
var includeEnvInDockerfile bool

const inputPromptPrefix string = " > "

Expand Down Expand Up @@ -46,10 +47,6 @@ func promptChoice(message string, choices []string, defaultChoice string) string
}
return s
}
func setDefaultNetVolume(projectId string, networkVolumeId string) {
viper.Set(fmt.Sprintf("project_volumes.%s", projectId), networkVolumeId)
viper.WriteConfig()
}

func selectNetworkVolume() (networkVolumeId string, err error) {
networkVolumes, err := api.GetNetworkVolumes()
Expand Down Expand Up @@ -225,22 +222,15 @@ var DeployProjectCmd = &cobra.Command{
},
}

// var BuildProjectCmd = &cobra.Command{
// Use: "build",
// Args: cobra.ExactArgs(0),
// Short: "build Docker image for current project",
// Long: "build a Docker image for the Runpod project in the current folder",
// Run: func(cmd *cobra.Command, args []string) {
// //parse project toml
// //build Dockerfile
// //base image: from toml
// //run setup.sh for system deps
// //pip install requirements
// //cmd: start handler
// //docker build
// //print next steps
// },
// }
var BuildProjectCmd = &cobra.Command{
Use: "build",
Args: cobra.ExactArgs(0),
Short: "build Dockerfile for current project",
Long: "build a Dockerfile for the Runpod project in the current folder",
Run: func(cmd *cobra.Command, args []string) {
buildProjectDockerfile()
},
}

func init() {
NewProjectCmd.Flags().StringVarP(&projectName, "name", "n", "", "project name")
Expand All @@ -249,5 +239,6 @@ func init() {
NewProjectCmd.Flags().BoolVarP(&initCurrentDir, "init", "i", false, "use the current directory as the project directory")

StartProjectCmd.Flags().BoolVar(&setDefaultNetworkVolume, "select-volume", false, "select a new default network volume for current project")
DeployProjectCmd.Flags().BoolVar(&setDefaultNetworkVolume, "select-volume", false, "select a new default network volume for current project")
BuildProjectCmd.Flags().BoolVar(&includeEnvInDockerfile, "include-env", false, "include environment variables from runpod.toml in generated Dockerfile")

}
1 change: 1 addition & 0 deletions cmd/project/starter_templates/default/.runpodignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Similar to .gitignore
# Matches will not be synce to the development pod or cause the development pod to reload.

Dockerfile
1 change: 1 addition & 0 deletions cmd/project/starter_templates/llama2/.runpodignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Similar to .gitignore
# Matches will not be synce to the development pod or cause the development pod to reload.

Dockerfile

0 comments on commit 7844b5d

Please sign in to comment.