properties.go 8.5 KB

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