Skip to content
Snippets Groups Projects
Commit 3003ff8b authored by baldwinlouie's avatar baldwinlouie Committed by Yas Naoi
Browse files

Issue #3093046 by baldwinlouie, yas: Refactor the admin settings of docker module

parent e05394e9
No related branches found
No related tags found
No related merge requests found
docker_unix_socket: '' docker_unix_socket: '/var/run/docker.sock'
docker_api_version: '1.39'
...@@ -3,3 +3,5 @@ docker.settings: ...@@ -3,3 +3,5 @@ docker.settings:
mapping: mapping:
docker_unix_socket: docker_unix_socket:
type: string type: string
docker_api_version:
type: string
<?php
/**
* @file
* Install file for docker module.
*/
/**
* Set the default unix socket and api version.
*/
function docker_update_8201() {
$config = \Drupal::configFactory()->getEditable('docker.settings');
$config->set('docker_api_version', '1.39');
$config->set('docker_unix_socket', '/var/run/docker.sock');
$config->save();
}
...@@ -73,11 +73,29 @@ class DockerAdminSettings extends ConfigFormBase { ...@@ -73,11 +73,29 @@ class DockerAdminSettings extends ConfigFormBase {
'#type' => 'textfield', '#type' => 'textfield',
'#title' => $this->t('Unix Socket'), '#title' => $this->t('Unix Socket'),
'#description' => $this->t('Local docker unix socket. Docker must be '#description' => $this->t('Local docker unix socket. Docker must be
installed on the Cloud Orchestrator server.'), installed on the Cloud Orchestrator server. For example: /var/run/docker.sock'),
'#required' => TRUE, '#required' => TRUE,
'#default_value' => $config->get('docker_unix_socket'), '#default_value' => $config->get('docker_unix_socket'),
]; ];
$form['docker']['local_docker']['docker_api_version'] = [
'#type' => 'textfield',
'#title' => $this->t('API Version'),
'#description' => $this->t('Docker remote api version. For example, 1.39.
Leave blank to use the latest version.'),
'#default_value' => $config->get('docker_api_version'),
];
$form['docker']['local_docker']['description'] = [
'#type' => 'markup',
'#markup' => '<strong>' . $this->t('NOTE:') . '</strong>' . $this->t('
In order for Drupal to access the Docker Remote API, the web server user
(ex: www-data) must be part of the Docker unix group or has sudo access.
For example, if the web server is on Ubuntu and the web server user is
www-data, run this command: `sudo usermod -a -G docker www-data`.
Make sure to restart the web server.'),
];
return parent::buildForm($form, $form_state); return parent::buildForm($form, $form_state);
} }
...@@ -85,9 +103,13 @@ class DockerAdminSettings extends ConfigFormBase { ...@@ -85,9 +103,13 @@ class DockerAdminSettings extends ConfigFormBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validateForm(array &$form, FormStateInterface $form_state) { public function validateForm(array &$form, FormStateInterface $form_state) {
if ($this->docker->isDockerUp($form_state->getValue('docker_unix_socket')) == FALSE) { if ($this->docker->isDockerUp(
$form_state->getValue('docker_unix_socket'),
$form_state->getValue('docker_api_version')) == FALSE
) {
// Set an error if docker is unreachable. // Set an error if docker is unreachable.
$form_state->setErrorByName('docker_unix_socket', $this->t('Docker unreachable')); $form_state->setErrorByName('docker_unix_socket', $this->t('Docker
unreachable. Please check unix socket and api version.'));
} }
} }
...@@ -97,6 +119,7 @@ class DockerAdminSettings extends ConfigFormBase { ...@@ -97,6 +119,7 @@ class DockerAdminSettings extends ConfigFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->configFactory()->getEditable('docker.settings'); $config = $this->configFactory()->getEditable('docker.settings');
$config->set('docker_unix_socket', $form_state->getValue('docker_unix_socket')); $config->set('docker_unix_socket', $form_state->getValue('docker_unix_socket'));
$config->set('docker_api_version', $form_state->getValue('docker_api_version'));
$config->save(); $config->save();
parent::submitForm($form, $form_state); parent::submitForm($form, $form_state);
} }
......
...@@ -22,7 +22,7 @@ class DockerService implements DockerServiceInterface { ...@@ -22,7 +22,7 @@ class DockerService implements DockerServiceInterface {
* *
* @var string * @var string
*/ */
protected $apiVersion = '1.39'; protected $apiVersion = '';
/** /**
* The return format of API requests. * The return format of API requests.
...@@ -105,6 +105,7 @@ class DockerService implements DockerServiceInterface { ...@@ -105,6 +105,7 @@ class DockerService implements DockerServiceInterface {
// Set the unix socket. // Set the unix socket.
$this->unixSocket = $config_factory->get('docker.settings')->get('docker_unix_socket'); $this->unixSocket = $config_factory->get('docker.settings')->get('docker_unix_socket');
$this->apiVersion = $config_factory->get('docker.settings')->get('docker_api_version');
} }
/** /**
...@@ -212,6 +213,13 @@ class DockerService implements DockerServiceInterface { ...@@ -212,6 +213,13 @@ class DockerService implements DockerServiceInterface {
$this->unixSocket = $unix_socket; $this->unixSocket = $unix_socket;
} }
/**
* {@inheritdoc}
*/
public function setApiVersion($api_version) {
$this->apiVersion = $api_version;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -329,18 +337,21 @@ class DockerService implements DockerServiceInterface { ...@@ -329,18 +337,21 @@ class DockerService implements DockerServiceInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function isDockerUp($unix_socket = '') { public function isDockerUp($unix_socket = '', $api_version = '') {
$is_available = FALSE; $is_available = FALSE;
try { try {
if (!empty($unix_socket)) { if (!empty($unix_socket)) {
$this->setUnixSocket($unix_socket); $this->setUnixSocket($unix_socket);
} }
if (!empty($api_version)) {
$this->setApiVersion($api_version);
}
$this->listImages(); $this->listImages();
$is_available = TRUE; $is_available = TRUE;
} }
catch (DockerServiceException $e) { catch (DockerServiceException $e) {
// Set an error if docker is unreachable. // Set an error if docker is unreachable.
$this->logError('Remote API Connection', $e, $this->t('Docker unreachable'), FALSE); $this->logError('Remote API Connection', $e, $this->t('Docker unreachable. @error', ['@error' => $e->getMessage()]), FALSE);
} }
return $is_available; return $is_available;
} }
...@@ -379,7 +390,11 @@ class DockerService implements DockerServiceInterface { ...@@ -379,7 +390,11 @@ class DockerService implements DockerServiceInterface {
*/ */
private function buildEndpoint($operation, $https = FALSE) { private function buildEndpoint($operation, $https = FALSE) {
$endpoint = $https == TRUE ? 'https' : 'http'; $endpoint = $https == TRUE ? 'https' : 'http';
$endpoint .= ":/$this->apiVersion/$operation"; $endpoint .= "://locahost";
if (!empty($this->apiVersion)) {
$endpoint .= "/v{$this->apiVersion}";
}
$endpoint .= "/$operation";
return $endpoint; return $endpoint;
} }
......
...@@ -79,6 +79,14 @@ interface DockerServiceInterface { ...@@ -79,6 +79,14 @@ interface DockerServiceInterface {
*/ */
public function setUseSocket($use_socket); public function setUseSocket($use_socket);
/**
* Set api version.
*
* @param string $api_version
* The api version.
*/
public function setApiVersion($api_version);
/** /**
* Set the unix socket path. * Set the unix socket path.
* *
...@@ -103,10 +111,12 @@ interface DockerServiceInterface { ...@@ -103,10 +111,12 @@ interface DockerServiceInterface {
* *
* @param string $unix_socket * @param string $unix_socket
* Docker unix socket to check. * Docker unix socket to check.
* @param string $api_version
* Api version to check.
* *
* @return bool * @return bool
* TRUE if docker is up. * TRUE if docker is up.
*/ */
public function isDockerUp($unix_socket = ''); public function isDockerUp($unix_socket = '', $api_version = '');
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment