-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5d87db
commit cb67750
Showing
7 changed files
with
131 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters