Class: Chef::Provider::SystemdUnit

Inherits:
Chef::Provider show all
Includes:
Mixin::ShellOut, Mixin::Which
Defined in:
lib/chef/provider/systemd_unit.rb

Instance Attribute Summary

Attributes inherited from Chef::Provider

#action, #current_resource, #logger, #new_resource, #recipe_name, #run_context

Instance Method Summary collapse

Methods included from Mixin::ShellOut

apply_default_env, maybe_add_timeout, #shell_out, #shell_out!

Methods inherited from Chef::Provider

action, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #cookbook_name, #description, #events, include_resource_dsl?, include_resource_dsl_module, #initialize, #introduced, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use_inline_resources, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::Provides

#provided_as, #provides, #provides?

Methods included from Mixin::DescendantsTracker

#descendants, descendants, direct_descendants, #direct_descendants, find_descendants_by_name, #find_descendants_by_name, #inherited, store_inherited

Methods included from Mixin::LazyModuleInclude

#descendants, #include, #included

Methods included from Mixin::PowershellOut

#powershell_out, #powershell_out!

Methods included from Mixin::WindowsArchitectureHelper

#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory

Methods included from Mixin::PowershellExec

#powershell_exec

Methods included from DSL::Powershell

#ps_credential

Methods included from DSL::RegistryHelper

#registry_data_exists?, #registry_get_subkeys, #registry_get_values, #registry_has_subkeys?, #registry_key_exists?, #registry_value_exists?

Methods included from DSL::DataQuery

#data_bag, #data_bag_item, #search, #tagged?

Methods included from EncryptedDataBagItem::CheckEncrypted

#encrypted?

Methods included from DSL::PlatformIntrospection

#older_than_win_2012_or_8?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Methods included from Mixin::NotifyingBlock

#notifying_block, #subcontext_block

Methods included from DSL::DeclareResource

#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #resources, #with_run_context

Constructor Details

This class inherits a constructor from Chef::Provider

Instance Method Details

#action_createObject



60
61
62
63
64
65
66
67
# File 'lib/chef/provider/systemd_unit.rb', line 60

def action_create
  if current_resource.content != new_resource.to_ini
    converge_by("creating unit: #{new_resource.unit_name}") do
      manage_unit_file(:create)
      daemon_reload if new_resource.triggers_reload
    end
  end
end

#action_deleteObject



69
70
71
72
73
74
75
76
# File 'lib/chef/provider/systemd_unit.rb', line 69

def action_delete
  if ::File.exist?(unit_path)
    converge_by("deleting unit: #{new_resource.unit_name}") do
      manage_unit_file(:delete)
      daemon_reload if new_resource.triggers_reload
    end
  end
end

#action_disableObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/chef/provider/systemd_unit.rb', line 106

def action_disable
  if current_resource.static
    logger.trace("#{new_resource.unit_name} is a static unit, disabling is a NOP.")
  end

  if current_resource.indirect
    logger.trace("#{new_resource.unit_name} is an indirect unit, enabling is a NOP.")
  end

  if current_resource.enabled && !current_resource.static && !current_resource.indirect
    converge_by("disabling unit: #{new_resource.unit_name}") do
      systemctl_execute!(:disable, new_resource.unit_name)
      logger.info("#{new_resource} disabled")
    end
  end
end

#action_enableObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/chef/provider/systemd_unit.rb', line 90

def action_enable
  if current_resource.static
    logger.trace("#{new_resource.unit_name} is a static unit, enabling is a NOP.")
  end
  if current_resource.indirect
    logger.trace("#{new_resource.unit_name} is an indirect unit, enabling is a NOP.")
  end

  unless current_resource.enabled || current_resource.static || current_resource.indirect
    converge_by("enabling unit: #{new_resource.unit_name}") do
      systemctl_execute!(:enable, new_resource.unit_name)
      logger.info("#{new_resource} enabled")
    end
  end
end

#action_maskObject



130
131
132
133
134
135
136
137
# File 'lib/chef/provider/systemd_unit.rb', line 130

def action_mask
  unless current_resource.masked
    converge_by("masking unit: #{new_resource.unit_name}") do
      systemctl_execute!(:mask, new_resource.unit_name)
      logger.info("#{new_resource} masked")
    end
  end
end

#action_presetObject



78
79
80
81
82
# File 'lib/chef/provider/systemd_unit.rb', line 78

def action_preset
  converge_by("restoring enable/disable preset configuration for unit: #{new_resource.unit_name}") do
    systemctl_execute!(:preset, new_resource.unit_name)
  end
end

#action_reenableObject



123
124
125
126
127
128
# File 'lib/chef/provider/systemd_unit.rb', line 123

def action_reenable
  converge_by("reenabling unit: #{new_resource.unit_name}") do
    systemctl_execute!(:reenable, new_resource.unit_name)
    logger.info("#{new_resource} reenabled")
  end
end

#action_reloadObject



173
174
175
176
177
178
179
180
181
182
# File 'lib/chef/provider/systemd_unit.rb', line 173

def action_reload
  if current_resource.active
    converge_by("reloading unit: #{new_resource.unit_name}") do
      systemctl_execute!(:reload, new_resource.unit_name, default_env: false)
      logger.info("#{new_resource} reloaded")
    end
  else
    logger.trace("#{new_resource.unit_name} is not active, skipping reload.")
  end
end

#action_reload_or_restartObject



191
192
193
194
195
196
# File 'lib/chef/provider/systemd_unit.rb', line 191

def action_reload_or_restart
  converge_by("reload-or-restarting unit: #{new_resource.unit_name}") do
    systemctl_execute!("reload-or-restart", new_resource.unit_name, default_env: false)
    logger.info("#{new_resource} reload-or-restarted")
  end
end

#action_reload_or_try_restartObject



198
199
200
201
202
203
# File 'lib/chef/provider/systemd_unit.rb', line 198

def action_reload_or_try_restart
  converge_by("reload-or-try-restarting unit: #{new_resource.unit_name}") do
    systemctl_execute!("reload-or-try-restart", new_resource.unit_name, default_env: false)
    logger.info("#{new_resource} reload-or-try-restarted")
  end
end

#action_restartObject



166
167
168
169
170
171
# File 'lib/chef/provider/systemd_unit.rb', line 166

def action_restart
  converge_by("restarting unit: #{new_resource.unit_name}") do
    systemctl_execute!(:restart, new_resource.unit_name, default_env: false)
    logger.info("#{new_resource} restarted")
  end
end

#action_revertObject



84
85
86
87
88
# File 'lib/chef/provider/systemd_unit.rb', line 84

def action_revert
  converge_by("reverting to vendor version of unit: #{new_resource.unit_name}") do
    systemctl_execute!(:revert, new_resource.unit_name)
  end
end

#action_startObject



148
149
150
151
152
153
154
155
# File 'lib/chef/provider/systemd_unit.rb', line 148

def action_start
  unless current_resource.active
    converge_by("starting unit: #{new_resource.unit_name}") do
      systemctl_execute!(:start, new_resource.unit_name, default_env: false)
      logger.info("#{new_resource} started")
    end
  end
end

#action_stopObject



157
158
159
160
161
162
163
164
# File 'lib/chef/provider/systemd_unit.rb', line 157

def action_stop
  if current_resource.active
    converge_by("stopping unit: #{new_resource.unit_name}") do
      systemctl_execute!(:stop, new_resource.unit_name, default_env: false)
      logger.info("#{new_resource} stopped")
    end
  end
end

#action_try_restartObject



184
185
186
187
188
189
# File 'lib/chef/provider/systemd_unit.rb', line 184

def action_try_restart
  converge_by("try-restarting unit: #{new_resource.unit_name}") do
    systemctl_execute!("try-restart", new_resource.unit_name, default_env: false)
    logger.info("#{new_resource} try-restarted")
  end
end

#action_unmaskObject



139
140
141
142
143
144
145
146
# File 'lib/chef/provider/systemd_unit.rb', line 139

def action_unmask
  if current_resource.masked
    converge_by("unmasking unit: #{new_resource.unit_name}") do
      systemctl_execute!(:unmask, new_resource.unit_name)
      logger.info("#{new_resource} unmasked")
    end
  end
end

#active?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/chef/provider/systemd_unit.rb', line 205

def active?
  systemctl_execute("is-active", new_resource.unit_name).exitstatus == 0
end

#define_resource_requirementsObject



51
52
53
54
55
56
57
58
# File 'lib/chef/provider/systemd_unit.rb', line 51

def define_resource_requirements
  super

  requirements.assert(:create) do |a|
    a.assertion { IniParse.parse(new_resource.to_ini) }
    a.failure_message "Unit content is not valid INI text"
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/chef/provider/systemd_unit.rb', line 209

def enabled?
  systemctl_execute("is-enabled", new_resource.unit_name).exitstatus == 0
end

#indirect?Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/chef/provider/systemd_unit.rb', line 221

def indirect?
  systemctl_execute("is-enabled", new_resource.unit_name).stdout.include?("indirect")
end

#load_current_resourceObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chef/provider/systemd_unit.rb', line 35

def load_current_resource
  @current_resource = Chef::Resource::SystemdUnit.new(new_resource.name)

  current_resource.unit_name(new_resource.unit_name)
  current_resource.content(::File.read(unit_path)) if ::File.exist?(unit_path)
  current_resource.user(new_resource.user)
  current_resource.enabled(enabled?)
  current_resource.active(active?)
  current_resource.masked(masked?)
  current_resource.static(static?)
  current_resource.indirect(indirect?)
  current_resource.triggers_reload(new_resource.triggers_reload)

  current_resource
end

#masked?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/chef/provider/systemd_unit.rb', line 213

def masked?
  systemctl_execute("status", new_resource.unit_name).stdout.include?("masked")
end

#static?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/chef/provider/systemd_unit.rb', line 217

def static?
  systemctl_execute("is-enabled", new_resource.unit_name).stdout.include?("static")
end