script: move Script into config package

This commit is contained in:
Alex Crawford 2014-09-27 11:11:57 -07:00
parent 3e2823df1b
commit 6e2db882e6
6 changed files with 38 additions and 20 deletions

View File

@ -45,6 +45,17 @@ type CloudConfig struct {
NetworkConfig string `yaml:"-"` NetworkConfig string `yaml:"-"`
} }
func IsCloudConfig(userdata string) bool {
header := strings.SplitN(userdata, "\n", 2)[0]
// Explicitly trim the header so we can handle user-data from
// non-unix operating systems. The rest of the file is parsed
// by yaml, which correctly handles CRLF.
header = strings.TrimSuffix(header, "\r")
return (header == "#cloud-config")
}
// NewCloudConfig instantiates a new CloudConfig from the given contents (a // NewCloudConfig instantiates a new CloudConfig from the given contents (a
// string of YAML), returning any error encountered. It will ignore unknown // string of YAML), returning any error encountered. It will ignore unknown
// fields but log encountering them. // fields but log encountering them.

16
config/script.go Normal file
View File

@ -0,0 +1,16 @@
package config
import (
"strings"
)
type Script []byte
func IsScript(userdata string) bool {
header := strings.SplitN(userdata, "\n", 2)[0]
return strings.HasPrefix(header, "#!")
}
func NewScript(userdata string) (Script, error) {
return Script(userdata), nil
}

View File

@ -180,7 +180,7 @@ func main() {
userdata := env.Apply(string(userdataBytes)) userdata := env.Apply(string(userdataBytes))
var ccm, ccu *config.CloudConfig var ccm, ccu *config.CloudConfig
var script *system.Script var script *config.Script
if ccm, err = initialize.ParseMetaData(string(metadataBytes)); err != nil { if ccm, err = initialize.ParseMetaData(string(metadataBytes)); err != nil {
fmt.Printf("Failed to parse meta-data: %v\n", err) fmt.Printf("Failed to parse meta-data: %v\n", err)
os.Exit(1) os.Exit(1)
@ -203,7 +203,7 @@ func main() {
switch t := ud.(type) { switch t := ud.(type) {
case *config.CloudConfig: case *config.CloudConfig:
ccu = t ccu = t
case system.Script: case config.Script:
script = &t script = &t
} }
} }
@ -362,7 +362,7 @@ func selectDatasource(sources []datasource.Datasource) datasource.Datasource {
} }
// TODO(jonboulle): this should probably be refactored and moved into a different module // TODO(jonboulle): this should probably be refactored and moved into a different module
func runScript(script system.Script, env *initialize.Environment) error { func runScript(script config.Script, env *initialize.Environment) error {
err := initialize.PrepWorkspace(env.Workspace()) err := initialize.PrepWorkspace(env.Workspace())
if err != nil { if err != nil {
fmt.Printf("Failed preparing workspace: %v\n", err) fmt.Printf("Failed preparing workspace: %v\n", err)

View File

@ -17,32 +17,25 @@
package initialize package initialize
import ( import (
"fmt" "errors"
"log" "log"
"strings"
"github.com/coreos/coreos-cloudinit/config" "github.com/coreos/coreos-cloudinit/config"
"github.com/coreos/coreos-cloudinit/system"
) )
func ParseUserData(contents string) (interface{}, error) { func ParseUserData(contents string) (interface{}, error) {
if len(contents) == 0 { if len(contents) == 0 {
return nil, nil return nil, nil
} }
header := strings.SplitN(contents, "\n", 2)[0]
// Explicitly trim the header so we can handle user-data from switch {
// non-unix operating systems. The rest of the file is parsed case config.IsScript(contents):
// by yaml, which correctly handles CRLF.
header = strings.TrimSpace(header)
if strings.HasPrefix(header, "#!") {
log.Printf("Parsing user-data as script") log.Printf("Parsing user-data as script")
return system.Script(contents), nil return config.NewScript(contents)
} else if header == "#cloud-config" { case config.IsCloudConfig(contents):
log.Printf("Parsing user-data as cloud-config") log.Printf("Parsing user-data as cloud-config")
return config.NewCloudConfig(contents) return config.NewCloudConfig(contents)
} else { default:
return nil, fmt.Errorf("Unrecognized user-data header: %s", header) return nil, errors.New("Unrecognized user-data format")
} }
} }

View File

@ -38,7 +38,7 @@ func PrepWorkspace(workspace string) error {
return nil return nil
} }
func PersistScriptInWorkspace(script system.Script, workspace string) (string, error) { func PersistScriptInWorkspace(script config.Script, workspace string) (string, error) {
scriptsPath := path.Join(workspace, "scripts") scriptsPath := path.Join(workspace, "scripts")
tmp, err := ioutil.TempFile(scriptsPath, "") tmp, err := ioutil.TempFile(scriptsPath, "")
if err != nil { if err != nil {

View File

@ -41,8 +41,6 @@ type Unit struct {
config.Unit config.Unit
} }
type Script []byte
// Destination builds the appropriate absolute file path for // Destination builds the appropriate absolute file path for
// the Unit. The root argument indicates the effective base // the Unit. The root argument indicates the effective base
// directory of the system (similar to a chroot). // directory of the system (similar to a chroot).