diff --git a/actions/image_partition_action.go b/actions/image_partition_action.go index e2a81ffc..2430f4c0 100644 --- a/actions/image_partition_action.go +++ b/actions/image_partition_action.go @@ -102,6 +102,10 @@ checks in boot time. By default is set to `true` allowing checks on boot. ext2, ext3, ext4 and xfs. - partuuid -- GPT partition UUID string. +A version 5 UUID can be easily generated using the uuid5 template function +{{ uuid5 $namespace $data }} $namespace should be a valid UUID and $data can be +any string, to generate reproducible UUID value pass a fixed value of namespace +and data. - extendedoptions -- list of additional filesystem extended options which need to be enabled for the partition. diff --git a/actions/recipe.go b/actions/recipe.go index e27d2790..adc706f0 100644 --- a/actions/recipe.go +++ b/actions/recipe.go @@ -34,6 +34,7 @@ The following custom template functions are available: - sector: Returns the argument * 512 (convential sector size) e.g. `{{ sector 64 }}` - escape: Shell escape the argument `{{ escape $var }}` +- uuid5: Generates fixed UUID value `{{ uuid5 $random-uuid $text }}` - functions from [slim-sprig](https://go-task.github.io/slim-sprig/) Mandatory properties for recipe: @@ -88,6 +89,7 @@ import ( "log" "strings" "reflect" + "github.com/google/uuid" ) /* the YamlAction just embed the Action interface and implements the @@ -158,6 +160,11 @@ func escape(s string) string { return shellescape.Quote(s) } +func uuid5(namespace string, data string) string { + id := uuid.NewSHA1(uuid.MustParse(namespace), []byte(data)) + return id.String() +} + func DumpActionStruct(iface interface{}) string { var a []string @@ -245,6 +252,7 @@ func (r *Recipe) Parse(file string, printRecipe bool, dump bool, templateVars .. funcs := template.FuncMap{ "sector": sector, "escape": escape, + "uuid5": uuid5, } t.Funcs(funcs) diff --git a/tests/templating/test.yaml b/tests/templating/test.yaml index af703e7f..92adba4f 100644 --- a/tests/templating/test.yaml +++ b/tests/templating/test.yaml @@ -35,3 +35,18 @@ actions: description: sprig conversion function chroot: false command: test 42 -eq {{ 0x2a | int }} + +# expected.uuid5 contains value generated with $namespace and $text1 +{{ $namespace := or .namespace "6c9d1418-8fa9-11ee-b4ca-325096b39f47" }} +{{ $text1 := or .text1 "ver1.0" }} +{{ $text2 := or .text2 "ver2.0" }} +{{ $expected := or .expected "3deb2a0e-692c-5a37-93c0-1123948bf5ae" }} + + - action: run + description: test uuid5 generate same value when input is same + chroot: false + command: test {{ uuid5 $namespace $text1 }} = {{ $expected }} + - action: run + description: test uuid5 generate different value when input is different + chroot: false + command: test {{ uuid5 $namespace $text2 }} != {{ $expected }}