properties.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // PropDescription sets the Description unit property. See
  72. // http://www.freedesktop.org/software/systemd/man/systemd.unit#Description=
  73. func PropDescription(desc string) Property {
  74. return Property{
  75. Name: "Description",
  76. Value: dbus.MakeVariant(desc),
  77. }
  78. }
  79. func propDependency(name string, units []string) Property {
  80. return Property{
  81. Name: name,
  82. Value: dbus.MakeVariant(units),
  83. }
  84. }
  85. // PropRequires sets the Requires unit property. See
  86. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#Requires=
  87. func PropRequires(units ...string) Property {
  88. return propDependency("Requires", units)
  89. }
  90. // PropRequiresOverridable sets the RequiresOverridable unit property. See
  91. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#RequiresOverridable=
  92. func PropRequiresOverridable(units ...string) Property {
  93. return propDependency("RequiresOverridable", units)
  94. }
  95. // PropRequisite sets the Requisite unit property. See
  96. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#Requisite=
  97. func PropRequisite(units ...string) Property {
  98. return propDependency("Requisite", units)
  99. }
  100. // PropRequisiteOverridable sets the RequisiteOverridable unit property. See
  101. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#RequisiteOverridable=
  102. func PropRequisiteOverridable(units ...string) Property {
  103. return propDependency("RequisiteOverridable", units)
  104. }
  105. // PropWants sets the Wants unit property. See
  106. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#Wants=
  107. func PropWants(units ...string) Property {
  108. return propDependency("Wants", units)
  109. }
  110. // PropBindsTo sets the BindsTo unit property. See
  111. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#BindsTo=
  112. func PropBindsTo(units ...string) Property {
  113. return propDependency("BindsTo", units)
  114. }
  115. // PropRequiredBy sets the RequiredBy unit property. See
  116. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#RequiredBy=
  117. func PropRequiredBy(units ...string) Property {
  118. return propDependency("RequiredBy", units)
  119. }
  120. // PropRequiredByOverridable sets the RequiredByOverridable unit property. See
  121. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#RequiredByOverridable=
  122. func PropRequiredByOverridable(units ...string) Property {
  123. return propDependency("RequiredByOverridable", units)
  124. }
  125. // PropWantedBy sets the WantedBy unit property. See
  126. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#WantedBy=
  127. func PropWantedBy(units ...string) Property {
  128. return propDependency("WantedBy", units)
  129. }
  130. // PropBoundBy sets the BoundBy unit property. See
  131. // http://www.freedesktop.org/software/systemd/main/systemd.unit.html#BoundBy=
  132. func PropBoundBy(units ...string) Property {
  133. return propDependency("BoundBy", units)
  134. }
  135. // PropConflicts sets the Conflicts unit property. See
  136. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#Conflicts=
  137. func PropConflicts(units ...string) Property {
  138. return propDependency("Conflicts", units)
  139. }
  140. // PropConflictedBy sets the ConflictedBy unit property. See
  141. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#ConflictedBy=
  142. func PropConflictedBy(units ...string) Property {
  143. return propDependency("ConflictedBy", units)
  144. }
  145. // PropBefore sets the Before unit property. See
  146. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#Before=
  147. func PropBefore(units ...string) Property {
  148. return propDependency("Before", units)
  149. }
  150. // PropAfter sets the After unit property. See
  151. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#After=
  152. func PropAfter(units ...string) Property {
  153. return propDependency("After", units)
  154. }
  155. // PropOnFailure sets the OnFailure unit property. See
  156. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#OnFailure=
  157. func PropOnFailure(units ...string) Property {
  158. return propDependency("OnFailure", units)
  159. }
  160. // PropTriggers sets the Triggers unit property. See
  161. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#Triggers=
  162. func PropTriggers(units ...string) Property {
  163. return propDependency("Triggers", units)
  164. }
  165. // PropTriggeredBy sets the TriggeredBy unit property. See
  166. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#TriggeredBy=
  167. func PropTriggeredBy(units ...string) Property {
  168. return propDependency("TriggeredBy", units)
  169. }
  170. // PropPropagatesReloadTo sets the PropagatesReloadTo unit property. See
  171. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#PropagatesReloadTo=
  172. func PropPropagatesReloadTo(units ...string) Property {
  173. return propDependency("PropagatesReloadTo", units)
  174. }
  175. // PropRequiresMountsFor sets the RequiresMountsFor unit property. See
  176. // http://www.freedesktop.org/software/systemd/man/systemd.unit.html#RequiresMountsFor=
  177. func PropRequiresMountsFor(units ...string) Property {
  178. return propDependency("RequiresMountsFor", units)
  179. }
  180. // PropSlice sets the Slice unit property. See
  181. // http://www.freedesktop.org/software/systemd/man/systemd.resource-control.html#Slice=
  182. func PropSlice(slice string) Property {
  183. return Property{
  184. Name: "Slice",
  185. Value: dbus.MakeVariant(slice),
  186. }
  187. }