Skip to content

Commit

Permalink
systemd: Inform about not met conditions
Browse files Browse the repository at this point in the history
When starting service some condition can fail and service won't be
started. Now in Cockpit this can be very confusing, since clicking
`start` button does not seem to do anything.

After this patch failed conditions are listed.

Signed-off-by: Matej Marusak <mmarusak@redhat.com>
  • Loading branch information
marusak committed Aug 6, 2017
1 parent c60ae29 commit 315e9ef
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/systemd/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,14 @@ $(function() {
template_description = cockpit.format(_("This unit is an instance of the $0 template."), link);
}

var conditions = cur_unit.Conditions;
var not_met_conditions = [];
for (var i = 0; i < conditions.length; i++) {
if (conditions[i][4] < 0) {
not_met_conditions.push(cockpit.format(_("Condition $0=$1 was not met"), conditions[i][0], conditions[i][3]));
}
}

var text = mustache.render(unit_template,
{
Unit: cur_unit,
Expand All @@ -643,6 +651,7 @@ $(function() {
TemplateDescription: template_description,
UnitButton: unit_action_btn,
FileButton: file_action_btn,
NotMetConditions: not_met_conditions,
});
$('#service-unit').html(text);
$('#service-unit-action').on('click', "[data-action]", unit_action);
Expand Down
17 changes: 17 additions & 0 deletions pkg/systemd/services.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@
{{/TemplateDescription}}
</table>
</div>
{{#NotMetConditions.length}}
<div class="list-group-item cond-fail">
<table>
<tr>
<td>
<span translate>Condition failed</span>
<ul>
{{#NotMetConditions}}
<li>{{.}}</li>
{{/NotMetConditions}}
</ul>
</td>
<td></td>
</tr>
</table>
</div>
{{/NotMetConditions.length}}
</div>
</div>
</script>
Expand Down
59 changes: 59 additions & 0 deletions test/verify/check-services
Original file line number Diff line number Diff line change
Expand Up @@ -481,5 +481,64 @@ WantedBy=default.target

self.allow_restart_journal_messages()

@skipImage("Not available with Cockpit 138", "rhel-7-4")
def testConditions(self):
m = self.machine
b = self.browser
m.write("/etc/systemd/system/condtest.service",
"""
[Unit]
Description=Test Service
ConditionDirectoryNotEmpty=/var/tmp/empty
[Service]
ExecStart=/bin/sh -c "while true; do sleep 5; done"
[Install]
WantedBy=multi-user.target
""")

m.execute("mkdir -p /var/tmp/empty")
m.execute("rm -rf /var/tmp/empty/*")

# After writing files out tell systemd about them
m.execute("systemctl daemon-reload")
self.machine.execute("systemctl start multi-user.target")

# This does not work for not enabled services. See:
# https://github.com/systemd/systemd/issues/2234
self.machine.execute("systemctl enable condtest")

self.login_and_go("/system/services")

def svc_sel(service):
return 'tr[data-goto-unit="%s"]' % service

unit_action_btn = '#service-valid .list-group-item:nth-child(1) .btn-group'

# Selects Services tab
b.click('#services-filter :nth-child(2)')
b.wait_present(svc_sel('condtest.service'))
b.click(svc_sel("condtest.service"))
b.wait_visible('#service-valid')
b.wait_in_text('#service-valid', "inactive")
b.wait_action_btn(unit_action_btn, "Start")
b.click_action_btn(unit_action_btn)

b.wait_present("#service-unit div.cond-fail")
b.wait_in_text("#service-unit div.cond-fail", "ConditionDirectoryNotEmpty")

# If the condition succeeds the message disappears
m.execute("touch /var/tmp/empty/non-empty")

b.wait_visible('#service-valid')
b.wait_in_text('#service-valid', "inactive")
b.wait_action_btn(unit_action_btn, "Start")
b.click_action_btn(unit_action_btn)
b.wait_in_text('#service-valid', "active")

b.wait_not_present("#service-unit div.cond-fail")


if __name__ == '__main__':
test_main()

0 comments on commit 315e9ef

Please sign in to comment.