Skip to content

Commit

Permalink
add new "compile" command
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkobrombin committed May 6, 2023
1 parent f5d87db commit cb67750
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func buildCommand(cmd *cobra.Command, args []string) error {
}

recipePath := args[0]
err := core.BuildRecipe(recipePath)
_, err := core.BuildRecipe(recipePath)
if err != nil {
return err
}
Expand Down
34 changes: 34 additions & 0 deletions cmd/compile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/vanilla-os/vib/core"
)

func NewCompileCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "compile",
Short: "Compile a recipe",
RunE: compileCommand,
}
cmd.Flags().SetInterspersed(false)

return cmd
}

func compileCommand(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("no recipe path or runtime specified")
}

recipePath := args[0]
runtime := args[1]
err := core.CompileRecipe(recipePath, runtime)
if err != nil {
return err
}

return nil
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var rootCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(NewBuildCommand())
rootCmd.AddCommand(NewValidateCommand())
rootCmd.AddCommand(NewCompileCommand())
}

func Execute() error {
Expand Down
14 changes: 7 additions & 7 deletions core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,43 @@ import (
)

// BuildRecipe builds a Containerfile from a recipe path
func BuildRecipe(recipePath string) error {
func BuildRecipe(recipePath string) (Recipe, error) {
// load the recipe
recipe, err := LoadRecipe(recipePath)
if err != nil {
return err
return Recipe{}, err
}

fmt.Printf("Building recipe %s\n", recipe.Name)

// resolve (and download) the sources
modules, sources, err := ResolveSources(recipe)
if err != nil {
return err
return Recipe{}, err
}

// move them to the sources directory so they can be
// used by the modules during the build
err = MoveSources(recipe, sources)
if err != nil {
return err
return Recipe{}, err
}

// build the modules*
// * actually just build the commands that will be used
// in the Containerfile to build the modules
cmds, err := BuildModules(recipe, modules)
if err != nil {
return err
return Recipe{}, err
}

// build the Containerfile
err = BuildContainerfile(recipe, cmds)
if err != nil {
return err
return Recipe{}, err
}

return nil
return *recipe, nil
}

// BuildContainerfile builds a Containerfile from a recipe
Expand Down
85 changes: 85 additions & 0 deletions core/compile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package core

import (
"fmt"
"os"
"os/exec"
)

// CompileRecipe compiles a recipe into a runnable image.
func CompileRecipe(recipePath string, runtime string) error {
recipe, err := BuildRecipe(recipePath)
if err != nil {
return err
}

storePath := fmt.Sprintf("%s/%s", os.Getenv("HOME"), ".vib/store")
if _, err := os.Stat(storePath); os.IsNotExist(err) {
err = os.MkdirAll(storePath, 0755)
if err != nil {
return err
}
}

switch runtime {
case "docker":
err = compileDocker(recipe, storePath)
if err != nil {
return err
}
case "podman":
err = compilePodman(recipe, storePath)
if err != nil {
return err
}
case "buildah":
return fmt.Errorf("buildah not implemented yet")
default:
return fmt.Errorf("no runtime specified and the prometheus library is not implemented yet")
}

fmt.Printf("Image %s built successfully\n", recipe.Id)
fmt.Printf("Remember to point %s to %s\n", runtime, storePath)

return nil
}

func compileDocker(recipe Recipe, storePath string) error {
docker, err := exec.LookPath("docker")
if err != nil {
return err
}

cmd := exec.Command(
docker, "build",
"-t", recipe.Id,
"-f", recipe.Containerfile,
"--root", storePath,
".",
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = recipe.ParentPath

return cmd.Run()
}

func compilePodman(recipe Recipe, storePath string) error {
podman, err := exec.LookPath("podman")
if err != nil {
return err
}

cmd := exec.Command(
podman, "build",
"-t", recipe.Id,
"-f", recipe.Containerfile,
"--root", storePath,
".",
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = recipe.ParentPath

return cmd.Run()
}
1 change: 1 addition & 0 deletions core/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
type Recipe struct {
Base string `json:"base"`
Name string
Id string
Labels map[string]string `json:"labels"`
Args map[string]string `json:"args"`
Runs []string `json:"runs"`
Expand Down
3 changes: 2 additions & 1 deletion example/example.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
base: debian:sid-slim
name: Vanilla Core
name: Vib Example
id: vib-example
labels:
maintainer: Vanilla OS Contributors
args:
Expand Down

0 comments on commit cb67750

Please sign in to comment.