|
| 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 | +} |
0 commit comments