Skip to content

Commit

Permalink
Merge branch 'main' into feat/18-add-source-for-single-file
Browse files Browse the repository at this point in the history
  • Loading branch information
deepu9 committed Jul 4, 2024
2 parents defe456 + e4cdbf8 commit 8633c0e
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 60 deletions.
33 changes: 27 additions & 6 deletions api/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,42 @@ type Stage struct {
Copy []Copy `json:"copy"`
Labels map[string]string `json:"labels"`
Env map[string]string `json:"env"`
Adds map[string]string `json:"adds"`
Adds []Add `json:"adds"`
Args map[string]string `json:"args"`
Runs []string `json:"runs"`
Runs Run `json:"runs"`
Expose map[string]string `json:"expose"`
Cmd []string `json:"cmd"`
Cmd Cmd `json:"cmd"`
Modules []interface{} `json:"modules"`
Entrypoint []string
Entrypoint Entrypoint
}

type Copy struct {
From string
Paths []Path
From string
Paths []Path
Workdir string
}

type Path struct {
Src string
Dst string
}

type Add struct {
SrcDst map[string]string
Workdir string
}

type Entrypoint struct {
Exec []string
Workdir string
}

type Cmd struct {
Exec []string
Workdir string
}

type Run struct {
Commands []string
Workdir string
}
189 changes: 155 additions & 34 deletions core/build.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"errors"
"fmt"
"os"
"strings"
Expand All @@ -9,6 +10,31 @@ import (
"github.com/vanilla-os/vib/api"
)

func ChangeWorkingDirectory(workdir string, containerfile *os.File) error {
if workdir != "" {
_, err := containerfile.WriteString(
fmt.Sprintf("WORKDIR %s\n", workdir),
)
if err != nil {
return err
}
}
return nil
}

func RestoreWorkingDirectory(workdir string, containerfile *os.File) error {
if workdir != "" {
_, err := containerfile.WriteString(
fmt.Sprintf("WORKDIR %s\n", "/"),
)
if err != nil {
return err
}
}

return nil
}

// BuildRecipe builds a Containerfile from a recipe path
func BuildRecipe(recipePath string) (api.Recipe, error) {
// load the recipe
Expand Down Expand Up @@ -82,10 +108,31 @@ func BuildContainerfile(recipe *api.Recipe) error {
// COPY
if len(stage.Copy) > 0 {
for _, copy := range stage.Copy {
for _, path := range copy.Paths {
_, err = containerfile.WriteString(
fmt.Sprintf("COPY --from=%s %s %s\n", copy.From, path.Src, path.Dst),
)
if len(copy.Paths) > 0 {
err = ChangeWorkingDirectory(copy.Workdir, containerfile)
if err != nil {
return err
}

for _, path := range copy.Paths {
if copy.From != "" {
_, err = containerfile.WriteString(
fmt.Sprintf("COPY --from=%s %s %s\n", copy.From, path.Src, path.Dst),
)
if err != nil {
return err
}
} else {
_, err = containerfile.WriteString(
fmt.Sprintf("COPY %s %s\n", path.Src, path.Dst),
)
if err != nil {
return err
}
}
}

err = RestoreWorkingDirectory(copy.Workdir, containerfile)
if err != nil {
return err
}
Expand Down Expand Up @@ -125,10 +172,22 @@ func BuildContainerfile(recipe *api.Recipe) error {

// RUN(S)
if !stage.SingleLayer {
for _, cmd := range stage.Runs {
_, err = containerfile.WriteString(
fmt.Sprintf("RUN %s\n", cmd),
)
if len(stage.Runs.Commands) > 0 {
err = ChangeWorkingDirectory(stage.Runs.Workdir, containerfile)
if err != nil {
return err
}

for _, cmd := range stage.Runs.Commands {
_, err = containerfile.WriteString(
fmt.Sprintf("RUN %s\n", cmd),
)
if err != nil {
return err
}
}

err = RestoreWorkingDirectory(stage.Runs.Workdir, containerfile)
if err != nil {
return err
}
Expand All @@ -146,12 +205,28 @@ func BuildContainerfile(recipe *api.Recipe) error {
}

// ADDS
for key, value := range stage.Adds {
_, err = containerfile.WriteString(
fmt.Sprintf("ADD %s %s\n", key, value),
)
if err != nil {
return err
if len(stage.Adds) > 0 {
for _, add := range stage.Adds {
if len(add.SrcDst) > 0 {
err = ChangeWorkingDirectory(add.Workdir, containerfile)
if err != nil {
return err
}

for key, value := range add.SrcDst {
_, err = containerfile.WriteString(
fmt.Sprintf("ADD %s %s\n", key, value),
)
if err != nil {
return err
}
}
}

err = RestoreWorkingDirectory(add.Workdir, containerfile)
if err != nil {
return err
}
}
}

Expand All @@ -174,63 +249,108 @@ func BuildContainerfile(recipe *api.Recipe) error {
continue
}

err = ChangeWorkingDirectory(cmd.Workdir, containerfile)
if err != nil {
return err
}

_, err = containerfile.WriteString(
fmt.Sprintf("RUN %s\n", cmd.Command),
)
if err != nil {
return err
}

err = RestoreWorkingDirectory(cmd.Workdir, containerfile)
if err != nil {
return err
}
}
}

// SINGLE LAYER
if stage.SingleLayer {
unifiedCmd := "RUN "
if len(stage.Runs.Commands) > 0 {
err = ChangeWorkingDirectory(stage.Runs.Workdir, containerfile)
if err != nil {
return err
}

for i, cmd := range stage.Runs {
unifiedCmd += cmd
if i != len(stage.Runs)-1 {
unifiedCmd := "RUN "

for i, cmd := range stage.Runs.Commands {
unifiedCmd += cmd
if i != len(stage.Runs.Commands)-1 {
unifiedCmd += " && "
}
}

if len(cmds) > 0 {
unifiedCmd += " && "
}
}

if len(cmds) > 0 {
unifiedCmd += " && "
}
for i, cmd := range cmds {
if cmd.Workdir != stage.Runs.Workdir {
return errors.New("Workdir mismatch")
}
unifiedCmd += cmd.Command
if i != len(cmds)-1 {
unifiedCmd += " && "
}
}

for i, cmd := range cmds {
unifiedCmd += cmd.Command
if i != len(cmds)-1 {
unifiedCmd += " && "
if len(unifiedCmd) > 4 {
_, err = containerfile.WriteString(fmt.Sprintf("%s\n", unifiedCmd))
if err != nil {
return err
}
}
}

if len(unifiedCmd) > 4 {
_, err = containerfile.WriteString(fmt.Sprintf("%s\n", unifiedCmd))
err = RestoreWorkingDirectory(stage.Runs.Workdir, containerfile)
if err != nil {
return err
}
}
}

// CMD
if len(stage.Cmd) > 0 {
err = ChangeWorkingDirectory(stage.Cmd.Workdir, containerfile)
if err != nil {
return err
}

if len(stage.Cmd.Exec) > 0 {
_, err = containerfile.WriteString(
fmt.Sprintf("CMD [\"%s\"]\n", strings.Join(stage.Cmd, "\",\"")),
fmt.Sprintf("CMD [\"%s\"]\n", strings.Join(stage.Cmd.Exec, "\",\"")),
)
if err != nil {
return err
}

err = RestoreWorkingDirectory(stage.Cmd.Workdir, containerfile)
if err != nil {
return err
}
}

// ENTRYPOINT
if len(stage.Entrypoint) > 0 {
err = ChangeWorkingDirectory(stage.Entrypoint.Workdir, containerfile)
if err != nil {
return err
}

if len(stage.Entrypoint.Exec) > 0 {
_, err = containerfile.WriteString(
fmt.Sprintf("ENTRYPOINT [\"%s\"]\n", strings.Join(stage.Entrypoint, "\",\"")),
fmt.Sprintf("ENTRYPOINT [\"%s\"]\n", strings.Join(stage.Entrypoint.Exec, "\",\"")),
)
if err != nil {
return err
}

err = RestoreWorkingDirectory(stage.Entrypoint.Workdir, containerfile)
if err != nil {
return err
}
}
}

Expand All @@ -255,6 +375,7 @@ func BuildModules(recipe *api.Recipe, modules []interface{}) ([]ModuleCommand, e
cmds = append(cmds, ModuleCommand{
Name: module.Name,
Command: cmd,
Workdir: module.Workdir,
})
}

Expand Down Expand Up @@ -285,7 +406,7 @@ func BuildModule(recipe *api.Recipe, moduleInterface interface{}) (string, error
}

moduleBuilders := map[string]func(interface{}, *api.Recipe) (string, error){
"shell": BuildShellModule,
"shell": BuildShellModule,
"includes": func(interface{}, *api.Recipe) (string, error) { return "", nil },
}

Expand Down
15 changes: 8 additions & 7 deletions core/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/mitchellh/mapstructure"
"github.com/vanilla-os/vib/api"

"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -94,7 +93,7 @@ func LoadRecipe(path string) (*api.Recipe, error) {
// the includes directory is the place where we store all the
// files to be included in the container, this is useful for
// example to include configuration files. Each file must follow
// the File Hierachy Standard (FHS) and be placed in the correct
// the File Hierarchy Standard (FHS) and be placed in the correct
// directory. For example, if you want to include a file in
// /etc/nginx/nginx.conf you must place it in includes/etc/nginx/nginx.conf
// so it will be copied to the correct location in the container
Expand All @@ -109,11 +108,13 @@ func LoadRecipe(path string) (*api.Recipe, error) {

for i, stage := range recipe.Stages {
// here we check if the extra Adds path exists
for src := range stage.Adds {
fullPath := filepath.Join(filepath.Dir(recipePath), src)
_, err = os.Stat(fullPath)
if os.IsNotExist(err) {
return nil, err
for _, add := range stage.Adds {
for src := range add.SrcDst {
fullPath := filepath.Join(filepath.Dir(recipePath), src)
_, err = os.Stat(fullPath)
if os.IsNotExist(err) {
return nil, err
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions core/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "C"

type Module struct {
Name string `json:"name"`
Workdir string
Type string `json:"type"`
Modules []map[string]interface{}
Content []byte // The entire module unparsed as a []byte, used by plugins
Expand All @@ -18,6 +19,7 @@ type IncludesModule struct {
type ModuleCommand struct {
Name string
Command string
Workdir string
}

type Plugin struct {
Expand Down
Loading

0 comments on commit 8633c0e

Please sign in to comment.