unit.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 system
  15. import (
  16. "fmt"
  17. "path"
  18. "path/filepath"
  19. "strings"
  20. "github.com/rancher/os/config/cloudinit/config"
  21. )
  22. type UnitManager interface {
  23. PlaceUnit(unit Unit) error
  24. PlaceUnitDropIn(unit Unit, dropIn config.UnitDropIn) error
  25. EnableUnitFile(unit Unit) error
  26. RunUnitCommand(unit Unit, command string) (string, error)
  27. MaskUnit(unit Unit) error
  28. UnmaskUnit(unit Unit) error
  29. DaemonReload() error
  30. }
  31. // Unit is a top-level structure which embeds its underlying configuration,
  32. // config.Unit, and provides the system-specific Destination(), Type(), and
  33. // Group().
  34. type Unit struct {
  35. config.Unit
  36. }
  37. // Type returns the extension of the unit (everything that follows the final
  38. // period).
  39. func (u Unit) Type() string {
  40. ext := filepath.Ext(u.Name)
  41. return strings.TrimLeft(ext, ".")
  42. }
  43. // Group returns "network" or "system" depending on whether or not the unit is
  44. // a network unit or otherwise.
  45. func (u Unit) Group() string {
  46. switch u.Type() {
  47. case "network", "netdev", "link":
  48. return "network"
  49. default:
  50. return "system"
  51. }
  52. }
  53. // Destination builds the appropriate absolute file path for the Unit. The root
  54. // argument indicates the effective base directory of the system (similar to a
  55. // chroot).
  56. func (u Unit) Destination(root string) string {
  57. return path.Join(u.prefix(root), u.Name)
  58. }
  59. // DropInDestination builds the appropriate absolute file path for the
  60. // UnitDropIn. The root argument indicates the effective base directory of the
  61. // system (similar to a chroot) and the dropIn argument is the UnitDropIn for
  62. // which the destination is being calculated.
  63. func (u Unit) DropInDestination(root string, dropIn config.UnitDropIn) string {
  64. return path.Join(u.prefix(root), fmt.Sprintf("%s.d", u.Name), dropIn.Name)
  65. }
  66. func (u Unit) prefix(root string) string {
  67. dir := "etc"
  68. if u.Runtime {
  69. dir = "run"
  70. }
  71. return path.Join(root, dir, "systemd", u.Group())
  72. }