1+ require_relative 'default'
2+
13module Common
24
35 # General
@@ -33,14 +35,10 @@ def self.daemon(ctx, name)
3335 end
3436 end
3537
36- def self . application ( ctx , name , user : nil , group : nil ,
38+ def self . application ( ctx , name , user : Default . user ( ctx ) . to_s , group : Default . group ( ctx ) . to_s ,
3739 exec : nil , cwd : nil , unit : { } , actions : [ :enable , :start ] , subscribe : nil , reload : 'systemd_reload' ,
3840 restart : 'on-failure' , restart_delay : 10 , restart_limit : 10 , restart_max : 600 ,
3941 verify : true , verify_timeout : 60 , verify_interval : 5 , verify_cmd : "systemctl is-active --quiet #{ name } " )
40- user ||= Default . user ( ctx )
41- group ||= Default . group ( ctx )
42- user = user . to_s
43- group = group . to_s
4442
4543 if exec
4644 daemon ( ctx , reload )
@@ -69,7 +67,7 @@ def self.application(ctx, name, user: nil, group: nil,
6967
7068 # Mask default application service
7169 conflicts = %Q(systemctl list-unit-files '*#{ File . basename ( exec . split . first ) } *.service' --no-legend | awk '$2!="masked" {print $1}')
72- Ctx . dsl ( ctx ) . execute "mask_conflicts_ #{ name } " do
70+ Ctx . dsl ( ctx ) . execute "application_mask_ #{ name } " do
7371 command "#{ conflicts } | xargs -r -IUNIT sh -c 'systemctl stop UNIT && systemctl mask UNIT'"
7472 only_if conflicts
7573 returns [ 0 , 123 ] # already masked returns '123'
@@ -81,15 +79,22 @@ def self.application(ctx, name, user: nil, group: nil,
8179 group 'root'
8280 mode '0664'
8381 content unit_content
82+ notifies :run , "execute[application_reset_#{ name } ]" , :immediately
8483 notifies :run , "execute[#{ reload } ]" , :immediately
8584 notifies :restart , "service[#{ name } ]" , :delayed
8685 end
8786
8887 end
8988
89+ Ctx . dsl ( ctx ) . execute "application_reset_#{ name } " do
90+ command "systemctl reset-failed #{ name } .service"
91+ only_if "systemctl is-failed --quiet #{ name } .service"
92+ action :nothing
93+ end
94+
9095 if actions . include? ( :force_restart )
91- Ctx . dsl ( ctx ) . execute "force_restart_ #{ name } " do
92- command "systemctl reset-failed #{ name } ; systemctl stop #{ name } || true && sleep 1 && systemctl start #{ name } "
96+ Ctx . dsl ( ctx ) . execute "application_restart_ #{ name } " do
97+ command "systemctl stop #{ name } || true && sleep 1 && systemctl start #{ name } "
9398 action :run
9499 end
95100 else
@@ -107,11 +112,12 @@ def self.application(ctx, name, user: nil, group: nil,
107112 ( is_active = Mixlib ::ShellOut . new ( verify_cmd ) ) . run_command
108113 is_active . exitstatus . zero? ? ( ok = true ; break ) : ( sleep verify_interval )
109114 end
110- Logs . error! ( "service '#{ name } ' failed health check" ) unless ok
115+ raise ( Logs . debug ( "service '#{ name } ' failed health check" , [
116+ Mixlib ::ShellOut . new ( "systemctl status #{ name } --no-pager" ) . run_command . stdout . strip ,
117+ Mixlib ::ShellOut . new ( "journalctl -u #{ name } --no-pager" ) . run_command . stdout . strip ,
118+ ] , level : :error ) ) unless ok
111119 end
112- action :nothing
113- subscribes :run , "service[#{ name } ]" , :delayed if verify
114- subscribes :run , "file[/etc/systemd/system/#{ name } .service]" , :delayed if verify
120+ only_if { verify }
115121 end
116122 end
117123
0 commit comments