properties.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. Copyright 2013 CoreOS Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package dbus
  14. import (
  15. "github.com/godbus/dbus"
  16. )
  17. // From the systemd docs:
  18. //
  19. // The properties array of StartTransientUnit() may take many of the settings
  20. // that may also be configured in unit files. Not all parameters are currently
  21. // accepted though, but we plan to cover more properties with future release.
  22. // Currently you may set the Description, Slice and all dependency types of
  23. // units, as well as RemainAfterExit, ExecStart for service units,
  24. // TimeoutStopUSec and PIDs for scope units, and CPUAccounting, CPUShares,
  25. // BlockIOAccounting, BlockIOWeight, BlockIOReadBandwidth,
  26. // BlockIOWriteBandwidth, BlockIODeviceWeight, MemoryAccounting, MemoryLimit,
  27. // DevicePolicy, DeviceAllow for services/scopes/slices. These fields map
  28. // directly to their counterparts in unit files and as normal D-Bus object
  29. // properties. The exception here is the PIDs field of scope units which is
  30. // used for construction of the scope only and specifies the initial PIDs to
  31. // add to the scope object.
  32. type Property struct {
  33. Name string
  34. Value dbus.Variant
  35. }
  36. type PropertyCollection struct {
  37. Name string
  38. Properties []Property
  39. }
  40. type execStart struct {
  41. Path string // the binary path to execute
  42. Args []string // an array with all arguments to pass to the executed command, starting with argument 0
  43. UncleanIsFailure bool // a boolean whether it should be considered a failure if the process exits uncleanly
  44. }
  45. // PropExecStart sets the ExecStart service property. The first argument is a
  46. // slice with the binary path to execute followed by the arguments to pass to
  47. // the executed command. See
  48. // http://www.freedesktop.org/software/systemd/man/systemd.service.html#ExecStart=
  49. func PropExecStart(command []string, uncleanIsFailure bool) Property {
  50. execStarts := []execStart{
  51. execStart{
  52. Path: command[0],
  53. Args: command,
  54. UncleanIsFailure: uncleanIsFailure,
  55. },
  56. }
  57. return Property{
  58. Name: "ExecStart",
  59. Value: dbus.MakeVariant(execStarts),
  60. }
  61. }
  62. // PropRemainAfterExit sets the RemainAfterExit service property. See
  63. // http://www.freedesktop.org/software/systemd/man/systemd.service.html#RemainAfterExit=
  64. func PropRemainAfterExit(b bool) Property {
  65. return Property{
  66. Name: "RemainAfterExit",
  67. Value: dbus.MakeVariant(b),
  68. }
  69. }
  70. // PropDescription sets the Description unit property. See
  71. // http://www.freedesktop.org/software/systemd/man/systemd.unit#Description=
  72. func PropDescription(desc string) Property {
  73. return Property{
  74. Name: "Description",
  75. Value: dbus.MakeVariant(desc),
  76. }
  77. }
  78. func propDependency(name string, units []string) Property {
  79. return Property{
  80. Name: name,
  81. Value: dbus.MakeVariant(units),
  82. }
  83. }
  84. // PropRequires sets the Requires unit property. See
  85. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#Requires=
  86. func PropRequires(units ...string) Property {
  87. return propDependency("Requires", units)
  88. }
  89. // PropRequiresOverridable sets the RequiresOverridable unit property. See
  90. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#RequiresOverridable=
  91. func PropRequiresOverridable(units ...string) Property {
  92. return propDependency("RequiresOverridable", units)
  93. }
  94. // PropRequisite sets the Requisite unit property. See
  95. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#Requisite=
  96. func PropRequisite(units ...string) Property {
  97. return propDependency("Requisite", units)
  98. }
  99. // PropRequisiteOverridable sets the RequisiteOverridable unit property. See
  100. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#RequisiteOverridable=
  101. func PropRequisiteOverridable(units ...string) Property {
  102. return propDependency("RequisiteOverridable", units)
  103. }
  104. // PropWants sets the Wants unit property. See
  105. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#Wants=
  106. func PropWants(units ...string) Property {
  107. return propDependency("Wants", units)
  108. }
  109. // PropBindsTo sets the BindsTo unit property. See
  110. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#BindsTo=
  111. func PropBindsTo(units ...string) Property {
  112. return propDependency("BindsTo", units)
  113. }
  114. // PropRequiredBy sets the RequiredBy unit property. See
  115. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#RequiredBy=
  116. func PropRequiredBy(units ...string) Property {
  117. return propDependency("RequiredBy", units)
  118. }
  119. // PropRequiredByOverridable sets the RequiredByOverridable unit property. See
  120. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#RequiredByOverridable=
  121. func PropRequiredByOverridable(units ...string) Property {
  122. return propDependency("RequiredByOverridable", units)
  123. }
  124. // PropWantedBy sets the WantedBy unit property. See
  125. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#WantedBy=
  126. func PropWantedBy(units ...string) Property {
  127. return propDependency("WantedBy", units)
  128. }
  129. // PropBoundBy sets the BoundBy unit property. See
  130. // http://www.freedesktop.org/software/systemd/main/systemd.unit.html#BoundBy=
  131. func PropBoundBy(units ...string) Property {
  132. return propDependency("BoundBy", units)
  133. }
  134. // PropConflicts sets the Conflicts unit property. See
  135. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#Conflicts=
  136. func PropConflicts(units ...string) Property {
  137. return propDependency("Conflicts", units)
  138. }
  139. // PropConflictedBy sets the ConflictedBy unit property. See
  140. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#ConflictedBy=
  141. func PropConflictedBy(units ...string) Property {
  142. return propDependency("ConflictedBy", units)
  143. }
  144. // PropBefore sets the Before unit property. See
  145. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#Before=
  146. func PropBefore(units ...string) Property {
  147. return propDependency("Before", units)
  148. }
  149. // PropAfter sets the After unit property. See
  150. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#After=
  151. func PropAfter(units ...string) Property {
  152. return propDependency("After", units)
  153. }
  154. // PropOnFailure sets the OnFailure unit property. See
  155. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#OnFailure=
  156. func PropOnFailure(units ...string) Property {
  157. return propDependency("OnFailure", units)
  158. }
  159. // PropTriggers sets the Triggers unit property. See
  160. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#Triggers=
  161. func PropTriggers(units ...string) Property {
  162. return propDependency("Triggers", units)
  163. }
  164. // PropTriggeredBy sets the TriggeredBy unit property. See
  165. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#TriggeredBy=
  166. func PropTriggeredBy(units ...string) Property {
  167. return propDependency("TriggeredBy", units)
  168. }
  169. // PropPropagatesReloadTo sets the PropagatesReloadTo unit property. See
  170. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#PropagatesReloadTo=
  171. func PropPropagatesReloadTo(units ...string) Property {
  172. return propDependency("PropagatesReloadTo", units)
  173. }
  174. // PropRequiresMountsFor sets the RequiresMountsFor unit property. See
  175. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#RequiresMountsFor=
  176. func PropRequiresMountsFor(units ...string) Property {
  177. return propDependency("RequiresMountsFor", units)
  178. }
  179. // PropSlice sets the Slice unit property. See
  180. // http://www.freedesktop.org/software/systemd/man/systemd.resource-control.html#Slice=
  181. func PropSlice(slice string) Property {
  182. return Property{
  183. Name: "Slice",
  184. Value: dbus.MakeVariant(slice),
  185. }
  186. }