/*
 * This file is part of Cockpit.
 *
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * Cockpit is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * Cockpit is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
 */

define([
    "jquery",
    "base1/cockpit",
    "base1/mustache",
    "docker/client",
    "docker/overview",
    "docker/details",
    "docker/image",
    "translated!base1/po",
], function($, cockpit, Mustache, client, overview, container_details, image_details, po) {
    cockpit.locale(po);
    var _ = cockpit.gettext;
    var C_ = cockpit.gettext;

    /* CURTAIN
     */

    var curtain_tmpl;

    function init_curtain(client, navigate) {
        curtain_tmpl = $("#curtain-tmpl").html();
        Mustache.parse(curtain_tmpl);

        $(client).on('failure', function (event, error) {
            show_curtain(error);
        });

        $('#curtain').on('click', '[data-action=docker-start]', function () {
            show_curtain(null);
            cockpit.spawn([ "systemctl", "start", "docker" ], { "superuser": "try" }).
                done(function () {
                    client.close();
                    client.connect().done(navigate);
                }).
                fail(function (error) {
                    show_curtain(cockpit.format(_("Failed to start Docker: $0"), error));
                });
        });

        $('#curtain').on('click', '[data-action=docker-connect]', function () {
            show_curtain(null);
            client.close();
            client.connect().done(navigate);
        });
    }

    function show_curtain(ex) {
        var info = { };

        if (ex === null) {
            info.connecting = true;
        } else if (typeof ex == "string") {
            info.other = ex;
            console.warn(ex);
        } else if (ex.problem == "not-found") {
            info.notfound = true;
        } else if (ex.problem == "access-denied") {
            info.denied = true;
        } else {
            info.other = ex.toString();
            console.warn(ex);
        }

        $('#curtain').html(Mustache.render(curtain_tmpl, info));
        $('body > div').hide();
        $('#curtain').show();
        $("body").show();
    }

    function hide_curtain() {
        $('#curtain').hide();
    }

    /* INITIALIZATION AND NAVIGATION
     */

    function init() {
        var docker_client;
        var overview_page;
        var container_details_page;
        var image_details_page;

        function navigate() {
            var path = cockpit.location.path;

            $("body").show();
            hide_curtain();
            if (path.length === 0) {
                container_details_page.hide();
                image_details_page.hide();
                overview_page.show();
            } else if (path.length === 1) {
                overview_page.hide();
                image_details_page.hide();
                container_details_page.show(path[0]);
            } else if (path.length === 2 && path[0] == "image") {
                overview_page.hide();
                container_details_page.hide();
                image_details_page.show(path[1]);
            } else { /* redirect */
                console.warn("not a containers location: " + path);
                cockpit.location = '';
            }
        }

        cockpit.translate();

        docker_client = client.init();
        init_curtain(docker_client, navigate);
        overview_page = overview.init(docker_client);
        container_details_page = container_details.init(docker_client);
        image_details_page = image_details.init(docker_client);

        show_curtain(null);
        docker_client.connect().done(navigate);
        $(cockpit).on("locationchanged", navigate);
    }

    return init;
});

@@ -876,24 +876,46 @@
 ker/image%22,%0A
+    %22system/service%22,%0A
     %22transla
@@ -1014,16 +1014,25 @@
 details,
+ service,
  po) %7B%0A 
@@ -1134,24 +1134,24 @@
 IN%0A     */%0A%0A
-
     var curt
@@ -1159,16 +1159,66 @@
 in_tmpl;
+%0A    var docker_service = service.proxy(%22docker%22);
 %0A%0A    fu
@@ -1576,80 +1576,29 @@
     
-cockpit.spawn(%5B %22systemctl%22, %22start%22, %22docker%22 %5D, %7B %22superuser%22: %22try%22 %7D
+docker_service.start(
 ).%0A 

Use system/service to start docker