Skip to content

Commit 72da275

Browse files
committed
add set recipe cmd to set the wish to a reference yaml file
1 parent 1ce7a6f commit 72da275

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

daemon/cmd/set_recipe.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
7+
log "github.com/sirupsen/logrus"
8+
"github.com/spf13/cobra"
9+
10+
"github.com/cvmfs/docker-graphdriver/daemon/lib"
11+
)
12+
13+
func init() {
14+
rootCmd.AddCommand(setRecipeCmd)
15+
}
16+
17+
// We start by converting the YamlRecipe in a set of images, that we add to the catalog of images.
18+
var setRecipeCmd = &cobra.Command{
19+
Use: "set-recipe",
20+
Short: "It changes the whish list to match the recipe, indepotent action",
21+
Args: cobra.MinimumNArgs(1),
22+
Run: func(cmd *cobra.Command, args []string) {
23+
24+
data, err := ioutil.ReadFile(args[0])
25+
if err != nil {
26+
lib.LogE(err).Fatal("Impossible to read the recipe file")
27+
}
28+
29+
actualWishes, err := lib.GetAllWish()
30+
if err != nil {
31+
lib.LogE(err).Fatal("Impossible to get all the wishes from the db")
32+
}
33+
fmt.Println(actualWishes)
34+
recipe, err := lib.ParseYamlRecipeV1(data)
35+
if err != nil {
36+
lib.LogE(err).Fatal("Impossible to parse the recipe file")
37+
}
38+
39+
toAddWish := []lib.Wish{}
40+
toRemoveWish := []lib.Wish{}
41+
42+
for _, newWish := range recipe.Wishes {
43+
alreadyPresent := false
44+
for _, oldWish := range actualWishes {
45+
if newWish.Equal(oldWish) {
46+
alreadyPresent = true
47+
break
48+
}
49+
}
50+
if !alreadyPresent {
51+
toAddWish = append(toAddWish, newWish)
52+
}
53+
}
54+
for _, oldWish := range actualWishes {
55+
toKeep := false
56+
for _, newWish := range recipe.Wishes {
57+
if oldWish.Equal(newWish) {
58+
toKeep = true
59+
break
60+
}
61+
}
62+
if toKeep == false {
63+
toRemoveWish = append(toRemoveWish, oldWish)
64+
}
65+
}
66+
67+
fmt.Println(recipe)
68+
fmt.Println(toAddWish)
69+
fmt.Println(toRemoveWish)
70+
for _, newWish := range toAddWish {
71+
err = lib.AddWish(newWish.InputImage, newWish.OutputImage, newWish.CvmfsRepo)
72+
if err != nil {
73+
input, _ := lib.GetImageById(newWish.InputImage)
74+
output, _ := lib.GetImageById(newWish.OutputImage)
75+
lib.LogE(err).WithFields(log.Fields{"input image": input.WholeName(), "repo": newWish.CvmfsRepo, "output image": output.WholeName()}).Warning("Error in adding a wish to the database")
76+
}
77+
}
78+
79+
for _, oldWish := range toRemoveWish {
80+
input, _ := lib.GetImageById(oldWish.InputImage)
81+
output, _ := lib.GetImageById(oldWish.OutputImage)
82+
n, err := lib.DeleteWish(oldWish.Id)
83+
if err != nil {
84+
lib.LogE(err).WithFields(log.Fields{"input image": input.WholeName(), "repo": oldWish.CvmfsRepo, "output image": output.WholeName()}).Warning("Error in removing a wish")
85+
}
86+
if n > 1 {
87+
lib.LogE(err).Warning("Remove more than one line from the database while removing a wish, should not happen")
88+
}
89+
}
90+
91+
},
92+
}

daemon/lib/recipe.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package lib
2+
3+
import (
4+
"strings"
5+
6+
log "github.com/sirupsen/logrus"
7+
8+
"gopkg.in/yaml.v2"
9+
)
10+
11+
type YamlRecipeV1 struct {
12+
Version int `yaml:"version"`
13+
User string `yaml:"user"`
14+
CVMFSRepo string `yaml:"cvmfs_repo"`
15+
OutputFormat string `yaml:"output_format"`
16+
Input []string `yaml:"input"`
17+
}
18+
19+
type Recipe struct {
20+
Wishes []Wish
21+
}
22+
23+
func ParseYamlRecipeV1(data []byte) (Recipe, error) {
24+
recipeYamlV1 := YamlRecipeV1{}
25+
err := yaml.Unmarshal(data, &recipeYamlV1)
26+
if err != nil {
27+
return Recipe{}, err
28+
}
29+
recipe := Recipe{}
30+
for _, inputImage := range recipeYamlV1.Input {
31+
input, err := ParseImage(inputImage)
32+
if err != nil {
33+
LogE(err).WithFields(log.Fields{"image": inputImage}).Warning("Impossible to parse the image")
34+
continue
35+
}
36+
output := formatOutputImage(recipeYamlV1.OutputFormat, input)
37+
wish, err := CreateWish(inputImage, output, recipeYamlV1.CVMFSRepo, "", recipeYamlV1.User)
38+
if err != nil {
39+
switch err.(type) {
40+
case *WishAlreadyInDBError:
41+
recipe.Wishes = append(recipe.Wishes, wish)
42+
default:
43+
LogE(err).Warning("Error in creating the wish")
44+
continue
45+
}
46+
} else {
47+
recipe.Wishes = append(recipe.Wishes, wish)
48+
}
49+
}
50+
return recipe, nil
51+
}
52+
53+
func formatOutputImage(OutputFormat string, inputImage Image) string {
54+
55+
s := strings.Replace(OutputFormat, "$(scheme)", inputImage.Scheme, 5)
56+
s = strings.Replace(s, "$(registry)", inputImage.Registry, 5)
57+
s = strings.Replace(s, "$(repository)", inputImage.Repository, 5)
58+
s = strings.Replace(s, "$(digest)", inputImage.Digest, 5)
59+
s = strings.Replace(s, "$(tag)", inputImage.Tag, 5)
60+
s = strings.Replace(s, "$(reference)", inputImage.GetReference(), 5)
61+
s = strings.Replace(s, "$(image)", inputImage.Repository+inputImage.GetReference(), 5)
62+
63+
return s
64+
}

0 commit comments

Comments
 (0)