workspace.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. "io/ioutil"
  17. "path"
  18. "strings"
  19. "github.com/rancher/os/config/cloudinit/config"
  20. "github.com/rancher/os/config/cloudinit/system"
  21. )
  22. func PrepWorkspace(workspace string) error {
  23. if err := system.EnsureDirectoryExists(workspace); err != nil {
  24. return err
  25. }
  26. scripts := path.Join(workspace, "scripts")
  27. if err := system.EnsureDirectoryExists(scripts); err != nil {
  28. return err
  29. }
  30. return nil
  31. }
  32. func PersistScriptInWorkspace(script config.Script, workspace string) (string, error) {
  33. scriptsPath := path.Join(workspace, "scripts")
  34. tmp, err := ioutil.TempFile(scriptsPath, "")
  35. if err != nil {
  36. return "", err
  37. }
  38. tmp.Close()
  39. relpath := strings.TrimPrefix(tmp.Name(), workspace)
  40. file := system.File{File: config.File{
  41. Path: relpath,
  42. RawFilePermissions: "0744",
  43. Content: string(script),
  44. }}
  45. return system.WriteFile(&file, workspace)
  46. }
  47. func PersistUnitNameInWorkspace(name string, workspace string) error {
  48. file := system.File{File: config.File{
  49. Path: path.Join("scripts", "unit-name"),
  50. RawFilePermissions: "0644",
  51. Content: name,
  52. }}
  53. _, err := system.WriteFile(&file, workspace)
  54. return err
  55. }