From fe0ffdc839954920c800fdc1f80b3c75c8ba3528 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Wed, 24 Oct 2018 12:35:15 +0530 Subject: [PATCH 1/2] Add docker volume functions Signed-off-by: Riddhesh Sanghvi --- php/class-ee-docker.php | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/php/class-ee-docker.php b/php/class-ee-docker.php index a91fcf2e5..7dcde88c3 100644 --- a/php/class-ee-docker.php +++ b/php/class-ee-docker.php @@ -1,5 +1,7 @@ specifies the name of volume to be created. + * $volumes[$key]['path_to_symlink'] => specifies the path to symlink the created volume. + * @param bool $update_to_docker_prefix Update the prefix in dockerized style. + */ + public static function create_volumes( $prefix, $volumes, $update_to_docker_prefix = true ) { + + $volume_prefix = $update_to_docker_prefix ? self::get_docker_style_prefix( $prefix ) : $prefix; + $fs = new Filesystem(); + + // This command will get the root directory for Docker, generally `/var/lib/docker`. + $launch = EE::launch( "docker info 2> /dev/null | awk '/Docker Root Dir/ {print $4}'" ); + $docker_root_dir = trim( $launch->stdout ); + + foreach ( $volumes as $volume ) { + $fs->mkdir( dirname( $volume['path_to_symlink'] ) ); + EE::exec( + sprintf( + 'docker volume create \ + --label "org.label-schema.vendor=EasyEngine" \ + --label "io.easyengine.site=%s" \ + %s_%s', + $prefix, + $volume_prefix, + $volume['name'] + ) + ); + $fs->symlink( sprintf( '%s/volumes/%s_%s/_data', $docker_root_dir, $volume_prefix, $volume['name'] ), $volume['path_to_symlink'] ); + } + } + + /** + * Function to get all the volumes with a specific label. + * + * @param string $label The label to search for. + * + * @return array Found containers. + */ + public static function get_volumes_by_label( $label ) { + $launch = EE::launch( sprintf( 'docker volume ls --filter="label=org.label-schema.vendor=EasyEngine" --filter="label=io.easyengine.site=%s" -q', $label ) ); + + return explode( PHP_EOL, trim( $launch->stdout ) ); + } } From a8febcfc2d29d1cbd7057bd16ca36e9daa8304fc Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Wed, 24 Oct 2018 12:58:24 +0530 Subject: [PATCH 2/2] Filter and remove empty results Signed-off-by: Riddhesh Sanghvi --- php/class-ee-docker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/class-ee-docker.php b/php/class-ee-docker.php index 7dcde88c3..287e63679 100644 --- a/php/class-ee-docker.php +++ b/php/class-ee-docker.php @@ -273,6 +273,6 @@ public static function create_volumes( $prefix, $volumes, $update_to_docker_pref public static function get_volumes_by_label( $label ) { $launch = EE::launch( sprintf( 'docker volume ls --filter="label=org.label-schema.vendor=EasyEngine" --filter="label=io.easyengine.site=%s" -q', $label ) ); - return explode( PHP_EOL, trim( $launch->stdout ) ); + return array_filter( explode( PHP_EOL, trim( $launch->stdout ) ), 'trim' ); } }