github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/cloudinit/cloudinit.go (about) 1 // Copyright 2011, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 // The cloudinit package implements a way of creating 5 // a cloud-init configuration file. 6 // See https://help.ubuntu.com/community/CloudInit. 7 package cloudinit 8 9 import ( 10 "bytes" 11 "text/template" 12 13 yaml "launchpad.net/goyaml" 14 ) 15 16 // Config represents a set of cloud-init configuration options. 17 type Config struct { 18 attrs map[string]interface{} 19 } 20 21 // New returns a new Config with no options set. 22 func New() *Config { 23 return &Config{make(map[string]interface{})} 24 } 25 26 // Render returns the cloud-init configuration as a YAML file. 27 func (cfg *Config) Render() ([]byte, error) { 28 data, err := yaml.Marshal(cfg.attrs) 29 if err != nil { 30 return nil, err 31 } 32 return append([]byte("#cloud-config\n"), data...), nil 33 } 34 35 func (cfg *Config) set(opt string, yes bool, value interface{}) { 36 if yes { 37 cfg.attrs[opt] = value 38 } else { 39 delete(cfg.attrs, opt) 40 } 41 } 42 43 // AptSource is an apt(8) source, comprising a source location, 44 // with an optional Key, and optional apt_preferences(5). 45 type AptSource struct { 46 Source string `yaml:"source"` 47 Key string `yaml:"key,omitempty"` 48 Prefs *AptPreferences `yaml:"-"` 49 } 50 51 // AptPreferences is a set of apt_preferences(5) compatible 52 // preferences for an apt source. It can be used to override the 53 // default priority for the source. Path where the file will be 54 // created (usually in /etc/apt/preferences.d/). 55 type AptPreferences struct { 56 Path string 57 Explanation string 58 Package string 59 Pin string 60 PinPriority int 61 } 62 63 // FileContents generates an apt_preferences(5) file from the fields 64 // in prefs. 65 func (prefs *AptPreferences) FileContents() string { 66 const prefTemplate = ` 67 Explanation: {{.Explanation}} 68 Package: {{.Package}} 69 Pin: {{.Pin}} 70 Pin-Priority: {{.PinPriority}} 71 ` 72 var buf bytes.Buffer 73 t := template.Must(template.New("").Parse(prefTemplate[1:])) 74 err := t.Execute(&buf, prefs) 75 if err != nil { 76 panic(err) 77 } 78 return buf.String() 79 } 80 81 // command represents a shell command. 82 type command struct { 83 literal string 84 args []string 85 } 86 87 // GetYAML implements yaml.Getter 88 func (t *command) GetYAML() (tag string, value interface{}) { 89 if t.args != nil { 90 return "", t.args 91 } 92 return "", t.literal 93 } 94 95 type SSHKeyType string 96 97 const ( 98 RSAPrivate SSHKeyType = "rsa_private" 99 RSAPublic SSHKeyType = "rsa_public" 100 DSAPrivate SSHKeyType = "dsa_private" 101 DSAPublic SSHKeyType = "dsa_public" 102 )