proc_cmdline.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 proccmdline
  15. import (
  16. "errors"
  17. "fmt"
  18. "io/ioutil"
  19. "strings"
  20. "github.com/rancher/os/log"
  21. "github.com/rancher/os/config/cloudinit/datasource"
  22. "github.com/rancher/os/config/cloudinit/pkg"
  23. )
  24. const (
  25. ProcCmdlineLocation = "/proc/cmdline"
  26. ProcCmdlineCloudConfigFlag = "cloud-config-url"
  27. )
  28. type ProcCmdline struct {
  29. Location string
  30. lastError error
  31. }
  32. func NewDatasource() *ProcCmdline {
  33. return &ProcCmdline{Location: ProcCmdlineLocation}
  34. }
  35. func (c *ProcCmdline) IsAvailable() bool {
  36. var contents []byte
  37. contents, c.lastError = ioutil.ReadFile(c.Location)
  38. if c.lastError != nil {
  39. return false
  40. }
  41. cmdline := strings.TrimSpace(string(contents))
  42. _, c.lastError = findCloudConfigURL(cmdline)
  43. return (c.lastError == nil)
  44. }
  45. func (c *ProcCmdline) Finish() error {
  46. return nil
  47. }
  48. func (c *ProcCmdline) String() string {
  49. return fmt.Sprintf("%s: %s (lastError: %s)", c.Type(), c.Location, c.lastError)
  50. }
  51. func (c *ProcCmdline) AvailabilityChanges() bool {
  52. return false
  53. }
  54. func (c *ProcCmdline) ConfigRoot() string {
  55. return ""
  56. }
  57. func (c *ProcCmdline) FetchMetadata() (datasource.Metadata, error) {
  58. return datasource.Metadata{}, nil
  59. }
  60. func (c *ProcCmdline) FetchUserdata() ([]byte, error) {
  61. contents, err := ioutil.ReadFile(c.Location)
  62. if err != nil {
  63. return nil, err
  64. }
  65. cmdline := strings.TrimSpace(string(contents))
  66. url, err := findCloudConfigURL(cmdline)
  67. if err != nil {
  68. return nil, err
  69. }
  70. client := pkg.NewHTTPClient()
  71. cfg, err := client.GetRetry(url)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return cfg, nil
  76. }
  77. func (c *ProcCmdline) Type() string {
  78. return "proc-cmdline"
  79. }
  80. func findCloudConfigURL(input string) (url string, err error) {
  81. err = errors.New("cloud-config-url not found")
  82. for _, token := range strings.Split(input, " ") {
  83. parts := strings.SplitN(token, "=", 2)
  84. key := parts[0]
  85. key = strings.Replace(key, "_", "-", -1)
  86. if key != "cloud-config-url" {
  87. continue
  88. }
  89. if len(parts) != 2 {
  90. log.Printf("Found cloud-config-url in /proc/cmdline with no value, ignoring.")
  91. continue
  92. }
  93. url = parts[1]
  94. err = nil
  95. }
  96. return
  97. }