Menu="OtherSettings"
Title="Docker"
Icon="dynamix.docker.manager.png"
Markdown="false"
---
<?PHP
/* Copyright 2005-2016, Lime Technology
 * Copyright 2014-2016, Guilherme Jardim, Eric Schultz, Jon Panozzo.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
// Add the Docker JSON client
require_once "$docroot/plugins/dynamix.docker.manager/include/DockerClient.php";

$docker          = new DockerClient();
$DockerUpdate    = new DockerUpdate();
$DockerTemplates = new DockerTemplates();

// Check for nodatacow flag on Docker file; display warning
$realfile = $dockercfg['DOCKER_IMAGE_FILE'];
if (file_exists($realfile)) {
  $realfile = transpose_user_path($realfile);

  if (exec("stat -c %T -f " . escapeshellarg($realfile)) == "btrfs") {
    if (shell_exec("lsattr " . escapeshellarg($realfile) . " | grep \"\\-C\"") == "") {
      echo '<p class="notice">Your existing Docker image file needs to be recreated due to an issue from an earlier beta of unRAID 6.  Failure to do so may result in your docker image suffering corruption at a later time.  Please do this NOW!</p>';
    }
  }
}
?>
<link type="text/css" rel="stylesheet" href="/webGui/styles/jquery.filetree.css">
<link type="text/css" rel="stylesheet" href="/webGui/styles/jquery.switchbutton.css">
<style>
  .errortext{color:#EF3D47;display:none;}
  .fileTree{width:305px;max-height:150px;overflow:scroll;position:absolute;z-index:100;display:none;}
  .basic{display: block;}
  .advanced{display:none;white-space: nowrap;}
</style>
<span class="status" style="margin-top: -10px;"><input type="checkbox" class="advancedview"></span>
<form id="settingsForm" method="POST" action="/update.php" target="progressFrame">
<input type="hidden" name="#file" value="<?=$docker_cfgfile;?>" />

<dl>
  <dt>Enable Docker:</dt>
  <dd>
    <select id="DOCKER_ENABLED" name="DOCKER_ENABLED" class="narrow">
    <?= mk_option($dockercfg['DOCKER_ENABLED'], 'no', 'No'); ?>
    <?= mk_option($dockercfg['DOCKER_ENABLED'], 'yes', 'Yes'); ?>
    </select> <?if ($var['fsState'] != "Started"):?><span id="arraystopped"><i class="fa fa-warning icon warning"></i> <?=($dockercfg['DOCKER_ENABLED']=='yes')?'Docker will be available after Array is Started':'Apply to activate Docker after Array is Started'?></span><?elseif (!is_dir(dirname($dockercfg['DOCKER_IMAGE_FILE'])) || !is_dir($dockercfg['DOCKER_APP_CONFIG_PATH'])):?><span class="basic" style="display:inline"><i class="fa fa-warning icon warning"></i> One or more paths do not exist (<a href="#" onclick="$('.advancedview').switchButton('option','checked',true); return false">view</a>)</span><?endif;?>
  </dd>
</dl>
<blockquote class="inline_help">
  <p>Before you can start the Docker service for the first time, please specify an image
  file for Docker to install to.  Once started, Docker will always automatically start
  after the array has been started.</p>
</blockquote>

<?if (pgrep('docker') === false):?>

<div class="advanced">
  <dl>
    <dt>Docker vdisk size:</dt>
    <dd><input id="DOCKER_IMAGE_SIZE" type="number" name="DOCKER_IMAGE_SIZE" value="<?=$dockercfg['DOCKER_IMAGE_SIZE'];?>" style="width:50px;" required="required" />GB <span id="SIZE_ERROR" class="errortext"></span></dd>
  </dl>
  <blockquote class="inline_help">
    <p>If the system needs to create a new docker image file, this is the default size to use specified in GB.</p>
    <p>To resize an existing image file, specify the new size here.  Next time the Docker service is started the file (and file system) will increased to the new size (but never decreased).</p>
  </blockquote>

  <dl>
    <dt>Docker storage location:</dt>
    <dd><input id="DOCKER_IMAGE_FILE" type="text" name="DOCKER_IMAGE_FILE" value="<?=$dockercfg['DOCKER_IMAGE_FILE'];?>" placeholder="e.g. /mnt/disk1/docker.img" data-pickcloseonfile="true" data-pickfilter="img" data-pickroot="/mnt/" data-pickfolders="true" required="required" /> <?php if (file_exists($dockercfg['DOCKER_IMAGE_FILE'])) { ?><span id="deletePanel"><label><input type="checkbox" id="deleteCheckbox" /> Delete Image File</label></span><?php } ?> <?if ($var['fsState'] != "Started"):?><span><i class="fa fa-warning icon warning"></i> Modify with caution: unable to validate path until Array is Started</span><?elseif (!is_dir(dirname($dockercfg['DOCKER_IMAGE_FILE']))):?><span><i class="fa fa-warning icon warning"></i> Path does not exist</span><?endif;?> <span id="IMAGE_ERROR" class="errortext"></span></dd>
  </dl>
  <blockquote class="inline_help">
    <p>You must specify an image file for Docker.  The system will automatically create this file when the Docker service is first started.</p>
  </blockquote>
</div>

<?else: /* IF DOCKER STARTED */?>

<div class="advanced">
  <dl>
    <dt>Docker version:</dt>
    <dd><? $arrInfo = $docker->getInfo(); echo $arrInfo['Version']; ?></dd>
  </dl>
  <blockquote class="inline_help">
    <p>This is the docker version.</p>
  </blockquote>

  <dl>
    <dt>Docker storage location:</dt>
    <dd><?=$dockercfg['DOCKER_IMAGE_FILE'];?></dd>
  </dl>
  <blockquote class="inline_help">
    <p>This is the docker volume.</p>
  </blockquote>
</div>

<?endif;?>

<div class="advanced">
  <dl>
    <dt>Default appdata storage location:</dt>
    <dd><input id="DOCKER_APP_CONFIG_PATH" type="text" name="DOCKER_APP_CONFIG_PATH" value="<?=$dockercfg['DOCKER_APP_CONFIG_PATH'];?>" placeholder="e.g. /mnt/user/appdata" data-pickfilter="HIDE_FILES_FILTER" data-pickroot="<?= (is_dir('/mnt/user/') ? '/mnt/user/' : '/mnt/') ?>" data-pickfolders="true" /> <?if ($var['fsState'] != "Started"):?><span><i class="fa fa-warning icon warning"></i> Modify with caution: unable to validate path until Array is Started</span><?elseif (!is_dir($dockercfg['DOCKER_APP_CONFIG_PATH'])):?><span><i class="fa fa-warning icon warning"></i> Path does not exist</span><?endif;?></dd>
  </dl>
  <blockquote class="inline_help">
    <p>You can specify a folder to automatically generate and store subfolders containing configuration files for each Docker app (via the /config mapped volume).</p>
    <p>Only used when adding new Docker apps.  Editing existing Docker apps will not be affected by this setting.</p>
  </blockquote>

  <!--dl>
    <dt>Auto-map user shares to containers as /unraid:</dt>
    <dd>
      <select id="DOCKER_APP_UNRAID_PATH" name="DOCKER_APP_UNRAID_PATH" class="narrow">
      <?= mk_option($dockercfg['DOCKER_APP_UNRAID_PATH'], ($dockercfg['DOCKER_APP_UNRAID_PATH'] != '' ? $dockercfg['DOCKER_APP_UNRAID_PATH'] : '/mnt/user'), 'Yes'); ?>
      <?= mk_option($dockercfg['DOCKER_APP_UNRAID_PATH'], '', 'No'); ?>
      </select>
    </dd>
  </dl>
  <blockquote class="inline_help">
    <p>You can expose all of your user shares (/mnt/user) to a folder named /unraid within Docker containers.</p>
    <p>Only used when adding new Docker apps.  Editing existing Docker apps will not be affected by this setting.</p>
  </blockquote-->
</div>

<div class="advanced">
  <dl>
    <dt>Template Authoring Mode:</dt>
    <dd>
    <select id="DOCKER_AUTHORING_MODE" name="DOCKER_AUTHORING_MODE" class="narrow">
    <?= mk_option($dockercfg['DOCKER_AUTHORING_MODE'], 'no', 'No'); ?>
    <?= mk_option($dockercfg['DOCKER_AUTHORING_MODE'], 'yes', 'Yes'); ?>
    </select>
    </dd>
  </dl>
  <blockquote class="inline_help">
    <p>If set to <b>Yes</b>, when creating/editing containers the interface will be present with some extra fields related to template authoring.</p>
  </blockquote>
</div>

<dl>
  <dt>&nbsp;</dt>
  <dd><input id="applyBtn" type="button" value="Apply"><input type="button" value="Done" onclick="done()"></dd>
</dl>

</form>
<form id="removeForm" method="POST" action="/update.php" target="progressFrame">
  <input type="hidden" name="#command" value="/plugins/dynamix.docker.manager/scripts/docker_rm" />
</form>
<?if (pgrep('docker') !== false):?>
<div class="advanced">
  <div id="title"><span class="left"><img src="/plugins/dynamix.docker.manager/icons/vcard.png" class="icon">Docker volume info</span></div>

  <dl>
    <dt>btrfs filesystem show:</dt>
    <dd><?="<pre>".shell_exec("btrfs filesystem show /var/lib/docker")."</pre>"?></dd>
  </dl>

  <form method="POST" action="/update.php" target="progressFrame">
  <?exec("$docroot/webGui/scripts/btrfs_scrub status /var/lib/docker", $scrub_status, $retval);?>

  <dl>
    <dt>btrfs scrub status:</dt>
    <dd><?="<pre>".implode("\n", $scrub_status)."</pre>"?></dd>
  </dl>

  <?if ($retval != 0):?>
  <input type="hidden" name="#command" value="/webGui/scripts/btrfs_scrub">
  <input type="hidden" name="#arg[1]" value="start">
  <input type="hidden" name="#arg[2]" value="/var/lib/docker">
  <input type="hidden" name="#arg[3]" value="-r">

  <dl>
    <dt>&nbsp;</dt>
    <dd><input type="submit" value="Scrub"><label><input type="checkbox" name="#arg[3]" value=""> Correct file system errors</label></dd>
  </dl>
  <blockquote class="inline_help">
    <p><b>Scrub</b> runs the <i>btrfs scrub</i> program to check file system integrity.</p>
    <p>If repair is needed you should check the <i>Correct file system errors</i> and run a second Scrub pass; this will permit <i>btrfs scrub</i> to fix the file system.</p>
  </blockquote>

  <?else:?>
  <input type="hidden" name="#command" value="/webGui/scripts/btrfs_scrub">
  <input type="hidden" name="#arg[1]" value="cancel">
  <input type="hidden" name="#arg[2]" value="/var/lib/docker">

  <dl>
    <dt>&nbsp;</dt>
    <dd><input type="button" value="Refresh" onclick="refresh()"><input type="submit" value="Cancel"> <i>Running</i></dd>
  </dl>
  <blockquote class="inline_help">
    <p><b>Cancel</b> will cancel the Scrub operation in progress.</p>
  </blockquote>

  </form>
  <?endif;?>
</div>
<?endif;?>
<script src="/webGui/javascript/jquery.filetree.js"></script>
<script src="/webGui/javascript/jquery.switchbutton.js"></script>
<script>
$(function() {
  $("#applyBtn").click(function(){
    if ($("#deleteCheckbox").length && $("#deleteCheckbox").is(":checked")) {
      $("#removeForm").submit();
      return;
    }

    if ($("#DOCKER_IMAGE_SIZE").length && $("#DOCKER_IMAGE_FILE").length) {
      var isError = false;
      var size = $("#DOCKER_IMAGE_SIZE").val();
      var target = $("#SIZE_ERROR");
      if (! $.isNumeric( size )){
        target.fadeIn().html('Error: value must be a number.');
        isError = true;
      } else {
        target.fadeOut();
      }
      var image = $("#DOCKER_IMAGE_FILE").val();

      target = $("#IMAGE_ERROR");
      $.getJSON( "/plugins/dynamix.docker.manager/include/UpdateConfig.php?is_dir=" + image).done(function( json ) {
        if (json.is_dir === true){
          target.fadeIn().html('Error: must be a file; directory provided.');
          isError = true;
        } else {
          target.fadeOut();
        }

        if (!isError) {$("#settingsForm").submit();}
      });
      return;
    }

    $("#settingsForm").submit();
  });

  $("#DOCKER_ENABLED").change(function changeService() {
    if ($(this).val()=='yes') {
      $('#arraystopped').fadeIn('slow');
    } else {
      $('#arraystopped').fadeOut('fast');
    }
  });
  if ($("#DOCKER_ENABLED").val()!='yes') $('#arraystopped').hide();

  if ($("#DOCKER_IMAGE_FILE").length) {
    $("#DOCKER_IMAGE_FILE").on("input change", function(){
      $("#IMAGE_ERROR").fadeOut();
      $("#applyBtn").prop("disabled", false);
      <? if (file_exists($dockercfg['DOCKER_IMAGE_FILE'])) { ?>
      if ($(this).val() != "<?=$dockercfg['DOCKER_IMAGE_FILE']?>") {
        $("#deleteCheckbox").prop("disabled", true).attr("checked", false);
        $("#deletePanel").fadeOut();
      } else {
        $("#deleteCheckbox").attr("checked", false).prop("disabled", false);
        $("#deletePanel").fadeIn();
      }
      <? } ?>
    });

<?if ($var['fsState'] == "Started"):?>
    $("#DOCKER_IMAGE_FILE").fileTreeAttach(null, null, function(folder) {
      $("#DOCKER_IMAGE_FILE").val(folder + 'docker.img').change();
    });
<?endif;?>

    $("#deleteCheckbox").change(function(){
      var checked = $(this).is(":checked");
      $("#DOCKER_ENABLED").prop("disabled", checked).val('no');
      $("#DOCKER_IMAGE_SIZE").prop("disabled", checked);
      $("#DOCKER_IMAGE_FILE").prop("disabled", checked).val("<?=$dockercfg['DOCKER_IMAGE_FILE']?>");
      $("#DOCKER_APP_CONFIG_PATH").prop("disabled", checked);
      $("#DOCKER_APP_UNRAID_PATH").prop("disabled", checked);
      $("#applyBtn").val(checked ? "Delete" : "Apply").removeAttr('disabled');
    });
  }

  if ($.cookie('dockersettings_view_mode') == 'advanced') {
    $('.advanced').show();
    $('.basic').hide();
  }
  $('.advancedview').switchButton({
    labels_placement: "left",
    on_label: 'Advanced View',
    off_label: 'Basic View',
    checked: $.cookie('dockersettings_view_mode') == 'advanced'
  });
  $('.advancedview').change(function () {
    $('.advanced').toggle('slow');
    $('.basic').toggle('slow');
    $.cookie('dockersettings_view_mode', $('.advancedview').is(':checked') ? 'advanced' : 'basic', { expires: 3650 });
  });

  showStatus('pid','docker');

  <?if ($var['fsState'] == "Started"):?>
    $("#DOCKER_APP_CONFIG_PATH").fileTreeAttach();
  <?endif;?>
});
</script>
