Exemplo n.º 1
0
 *
 * OpenMediaVault 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.
 */
try {
    require_once "openmediavault/autoloader.inc";
    require_once "openmediavault/globals.inc";
    require_once "openmediavault/env.inc";
    require_once "openmediavault/functions.inc";
    // Initialize the configuration engine.
    $db =& \OMV\Config\Database::getInstance();
    $xmlConfig = $db->getBackend();
    // Load and initialize the RPC services that are not handled by the
    // engine daemon.
    $directory = build_path(DIRECTORY_SEPARATOR, $GLOBALS['OMV_DOCUMENTROOT_DIR'], "rpc");
    foreach (listdir($directory, "inc") as $path) {
        require_once $path;
    }
    $rpcServiceMngr =& \OMV\Rpc\ServiceManager::getInstance();
    $rpcServiceMngr->initializeServices();
    // Initialize the data models.
    $modelMngr = \OMV\DataModel\Manager::getInstance();
    $modelMngr->load($GLOBALS['OMV_DATAMODELS_DIR']);
    $session =& \OMV\Session::getInstance();
    $session->start();
    $server = new OMV\Rpc\Proxy\Json();
 /**
  * Change the Docker daemon settings
  *
  * @param string $apiPort The new API port to use
  * @param string $absPath Absolute path where Docker files should be moved
  *
  * @return void
  *
  */
 public static function changeDockerSettings($context, $apiPort, $absPath)
 {
     self::$database = Database::getInstance();
     OMVModuleDockerUtil::stopDockerService();
     //Do some sanity checks before making changes to running config.
     //First check that there is no manual base path relocation in the file
     // /etc/default/docker
     $fileName = "/etc/default/docker";
     $data = file_get_contents($fileName);
     $lines = explode("\n", $data);
     foreach ($lines as $line) {
         if (strcmp($line, "### Do not change these lines. They are added and updated by the OMV Docker GUI plugin.") === 0) {
             break;
         } elseif (preg_match('/^[^\\#]+.*\\-g[\\s]?([^\\"]+)[\\s]?.*/', $line, $matches) && strcmp($absPath, "") !== 0) {
             OMVModuleDockerUtil::startDockerService();
             throw new OMVModuleDockerException("Docker " . "base path relocation detected in " . "configuration file\n" . "Please remove it manually " . "({$matches['1']})\n");
         }
     }
     // Next get the old settings object
     $oldSettings = self::$database->getAssoc(self::$dataModelPath);
     // Next umount old bind mount
     if (!(strcmp($oldSettings['dockermntent'], "") === 0)) {
         $oldMntent = Rpc::call("FsTab", "get", ["uuid" => $oldSettings['dockermntent']], $context);
         $me = new MountPoint($oldMntent['fsname'], $oldMntent['dir']);
         $cmd = "mount | grep /var/lib/docker/openmediavault | wc -l";
         unset($out);
         $process = new Process($cmd);
         $out = $process->execute();
         while ($out > 0) {
             $me->umount();
             $cmd = "mount | grep /var/lib/docker/openmediavault | wc -l";
             unset($out);
             $process = new Process($cmd);
             $out = $process->execute();
         }
     }
     //First update /etc/default/docker with user provided data
     $fileName = "/etc/default/docker";
     $data = file_get_contents($fileName);
     $lines = explode("\n", $data);
     $result = "";
     foreach ($lines as $line) {
         if (strcmp($line, "### Do not change these lines. They are added and updated by the OMV Docker GUI plugin.") === 0) {
             break;
         } elseif (preg_match('/^[^\\#]+.*\\-g[\\s]?([^\\"]+)[\\s]?.*/', $line, $matches) && strcmp($absPath, "") !== 0) {
             OMVModuleDockerUtil::startDockerService();
             throw new OMVModuleDockerException("Docker " . "base path relocation detected in " . "configuration file\n" . "Please remove it manually " . "({$matches['1']})\n");
         }
         $result .= $line . "\n";
     }
     $result = rtrim($result);
     $result .= "\n\n" . '### Do not change these lines. They are added ' . 'and updated by the OMV Docker GUI plugin.' . "\n";
     $result .= 'OMVDOCKER_API="-H tcp://127.0.0.1:' . $apiPort . '"' . "\n";
     if (strcmp($absPath, "") !== 0) {
         $result .= 'OMVDOCKER_IMAGE_PATH="-g /var/lib/docker/openmediavault"' . "\n";
     } else {
         $result .= 'OMVDOCKER_IMAGE_PATH=""' . "\n";
     }
     $result .= '### Do not add any configuration below this line. It will be ' . 'removed when the plugin is removed';
     file_put_contents("{$fileName}", $result);
     //Next fix OMV config backend if the base path should be relocated
     //Start by removing any old mntent entries
     $mnt = Rpc::call("FsTab", "getByDir", ["dir" => '/var/lib/docker/openmediavault'], $context);
     if ($mnt) {
         Rpc::call("FsTab", "delete", ["uuid" => $mnt['uuid']], $context);
     }
     //Next generate a new mntent entry if a shared folder is specified
     if (!(strcmp($absPath, "") === 0)) {
         $newMntent = ["uuid" => \OMV\Environment::get("OMV_CONFIGOBJECT_NEW_UUID"), "fsname" => $absPath, "dir" => "/var/lib/docker/openmediavault", "type" => "none", "opts" => "bind,defaults", "freq" => 0, "passno" => 0];
         $newMntent = Rpc::call("FsTab", "set", $newMntent, $context);
     }
     //Update settings object
     if (strcmp($absPath, "") === 0) {
         $tmpMntent = "";
     } else {
         $tmpMntent = $newMntent['uuid'];
     }
     $object = array("dockermntent" => $tmpMntent, "enabled" => $oldSettings['enabled'], "apiPort" => $oldSettings['apiPort'], "sharedfolderref" => $oldSettings['sharedfolderref']);
     $config = new ConfigObject(self::$dataModelPath);
     $config->setAssoc($object);
     self::$database->set($config);
     //Re-generate fstab entries
     $cmd = "export LANG=C; omv-mkconf fstab 2>&1";
     $process = new Process($cmd);
     $out = $process->execute();
     // Finally mount the new bind-mount entry
     if (!(strcmp($absPath, "") === 0)) {
         $me = new MountPoint($newMntent['fsname'], $newMntent['dir']);
         $me->mount();
         //Remount the bind-mount with defaults options
         $cmd = "export LANG=C; mount -o remount,bind,defaults " . $newMntent['fsname'] . " " . $newMntent['dir'] . " 2>&1";
         $process = new Process($cmd);
         $out = $process->execute();
     }
     OMVModuleDockerUtil::startDockerService();
 }