workspace.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. return system.EnsureDirectoryExists(scripts)
  28. }
  29. func PersistScriptInWorkspace(script config.Script, workspace string) (string, error) {
  30. scriptsPath := path.Join(workspace, "scripts")
  31. tmp, err := ioutil.TempFile(scriptsPath, "")
  32. if err != nil {
  33. return "", err
  34. }
  35. tmp.Close()
  36. relpath := strings.TrimPrefix(tmp.Name(), workspace)
  37. file := system.File{File: config.File{
  38. Path: relpath,
  39. RawFilePermissions: "0744",
  40. Content: string(script),
  41. }}
  42. return system.WriteFile(&file, workspace)
  43. }
  44. func PersistUnitNameInWorkspace(name string, workspace string) error {
  45. file := system.File{File: config.File{
  46. Path: path.Join("scripts", "unit-name"),
  47. RawFilePermissions: "0644",
  48. Content: name,
  49. }}
  50. _, err := system.WriteFile(&file, workspace)
  51. return err
  52. }