forked from toolshed/abra
		
	WIP: add compose updating to recipe upgrade
This commit is contained in:
		| @ -228,6 +228,7 @@ var recipeUpgradeCommand = &cli.Command{ | ||||
| 			if err := survey.AskOne(prompt, &upgradeTag); err != nil { | ||||
| 				logrus.Fatal(err) | ||||
| 			} | ||||
| 			config.UpdateAppComposeTag(recipe, image, upgradeTag) | ||||
| 		} | ||||
|  | ||||
| 		return nil | ||||
|  | ||||
| @ -14,6 +14,8 @@ import ( | ||||
| 	loader "coopcloud.tech/abra/client/stack" | ||||
| 	"github.com/docker/cli/cli/command/stack/options" | ||||
| 	composetypes "github.com/docker/cli/cli/compose/types" | ||||
| 	"github.com/docker/distribution/reference" | ||||
| 	"github.com/sirupsen/logrus" | ||||
| ) | ||||
|  | ||||
| // Type aliases to make code hints easier to understand | ||||
| @ -241,3 +243,49 @@ func GetAppComposeFiles(recipe string) (*composetypes.Config, error) { | ||||
|  | ||||
| 	return compose, nil | ||||
| } | ||||
|  | ||||
| func UpdateAppComposeTag(recipe, image, tag string) error { | ||||
| 	pattern := fmt.Sprintf("%s/%s/compose**yml", APPS_DIR, recipe) | ||||
| 	composeFiles, err := filepath.Glob(pattern) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	for _, composeFile := range composeFiles { | ||||
| 		opts := options.Deploy{Composefiles: []string{composeFile}} | ||||
| 		compose, err := loader.LoadComposefile(opts) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 		for _, service := range compose.Services { | ||||
| 			if service.Image == "" { | ||||
| 				continue // may be a compose.$optional.yml file | ||||
| 			} | ||||
|  | ||||
| 			img, _ := reference.ParseNormalizedNamed(service.Image) | ||||
| 			if err != nil { | ||||
| 				logrus.Fatal(err) | ||||
| 			} | ||||
| 			composeImage := reference.Path(img.(reference.Named)) | ||||
| 			composeTag := img.(reference.NamedTagged).Tag() | ||||
|  | ||||
| 			if image == composeImage { | ||||
| 				bytes, err := ioutil.ReadFile(composeFile) | ||||
| 				if err != nil { | ||||
| 					logrus.Fatal(err) | ||||
| 				} | ||||
|  | ||||
| 				old := fmt.Sprintf("%s:%s", composeImage, composeTag) | ||||
| 				new := fmt.Sprintf("%s:%s", composeImage, tag) | ||||
| 				replacedBytes := strings.Replace(string(bytes), old, new, -1) | ||||
|  | ||||
| 				if err := ioutil.WriteFile(compose.Filename, []byte(replacedBytes), 0644); err != nil { | ||||
| 					return err | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user