user_data.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package initialize
  15. import (
  16. "errors"
  17. "log"
  18. "github.com/rancher/os/config/cloudinit/config"
  19. )
  20. var (
  21. ErrIgnitionConfig = errors.New("not a config (found Ignition)")
  22. )
  23. func ParseUserData(contents string) (interface{}, error) {
  24. if len(contents) == 0 {
  25. return nil, nil
  26. }
  27. switch {
  28. case config.IsScript(contents):
  29. log.Printf("Parsing user-data as script")
  30. return config.NewScript(contents)
  31. case config.IsCloudConfig(contents):
  32. log.Printf("Parsing user-data as cloud-config")
  33. cc, err := config.NewCloudConfig(contents)
  34. if err != nil {
  35. return nil, err
  36. }
  37. if err := cc.Decode(); err != nil {
  38. return nil, err
  39. }
  40. return cc, nil
  41. case config.IsIgnitionConfig(contents):
  42. return nil, ErrIgnitionConfig
  43. default:
  44. return nil, errors.New("Unrecognized user-data format")
  45. }
  46. }