github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cloudconfig/providerinit/renderers/common.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Copyright 2015 Cloudbase Solutions SRL 3 // Licensed under the AGPLv3, see LICENCE file for details. 4 5 package renderers 6 7 import ( 8 "encoding/base64" 9 "fmt" 10 11 "github.com/juju/juju/cloudconfig" 12 "github.com/juju/juju/cloudconfig/cloudinit" 13 "github.com/juju/utils" 14 ) 15 16 // ToBase64 just transforms whatever userdata it gets to base64 format 17 func ToBase64(data []byte) []byte { 18 buf := make([]byte, base64.StdEncoding.EncodedLen(len(data))) 19 base64.StdEncoding.Encode(buf, data) 20 return buf 21 } 22 23 // WinEmbedInScript for now is used on windows and it returns a powershell script 24 // which has the userdata embedded as base64(gzip(userdata)) 25 func WinEmbedInScript(udata []byte) []byte { 26 encUserdata := ToBase64(utils.Gzip(udata)) 27 return []byte(fmt.Sprintf(cloudconfig.UserdataScript, encUserdata)) 28 } 29 30 // AddPowershellTags adds <powershell>...</powershell> to it's input 31 func AddPowershellTags(udata []byte) []byte { 32 return []byte(`<powershell>` + 33 string(udata) + 34 `</powershell>`) 35 } 36 37 // Decorator is a function that can be used as part of a rendering pipeline. 38 type Decorator func([]byte) []byte 39 40 // RenderYAML renders the given cloud-config as YAML, and then passes the 41 // YAML through the given decorators. 42 func RenderYAML(cfg cloudinit.RenderConfig, ds ...Decorator) ([]byte, error) { 43 out, err := cfg.RenderYAML() 44 if err != nil { 45 return nil, err 46 } 47 return applyDecorators(out, ds), nil 48 } 49 50 // RenderScript renders the given cloud-config as a script, and then passes the 51 // script through the given decorators. 52 func RenderScript(cfg cloudinit.RenderConfig, ds ...Decorator) ([]byte, error) { 53 out, err := cfg.RenderScript() 54 if err != nil { 55 return nil, err 56 } 57 return applyDecorators([]byte(out), ds), nil 58 } 59 60 func applyDecorators(out []byte, ds []Decorator) []byte { 61 for _, d := range ds { 62 out = d(out) 63 } 64 return out 65 }