|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use Symfony\Component\Filesystem\Filesystem; |
| 4 | + |
3 | 5 | class EE_DOCKER { |
4 | 6 |
|
5 | 7 | /** |
@@ -214,4 +216,63 @@ public static function service_exists( $service, $site_fs_path ) { |
214 | 216 |
|
215 | 217 | return in_array( $service, $services, true ); |
216 | 218 | } |
| 219 | + |
| 220 | + /** |
| 221 | + * Gets a dockerized prefix created for site. |
| 222 | + * |
| 223 | + * @param string $site_url Name of the site. |
| 224 | + * |
| 225 | + * @return string prefix derived from the name. |
| 226 | + */ |
| 227 | + public static function get_docker_style_prefix( $site_url ) { |
| 228 | + return str_replace( [ '.', '-' ], '', $site_url ); |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Function to create external docker volumes and related symlinks. |
| 233 | + * |
| 234 | + * @param string $prefix Prefix by volumes have to be created. |
| 235 | + * @param array $volumes The volumes to be created. |
| 236 | + * $volumes[$key]['name'] => specifies the name of volume to be created. |
| 237 | + * $volumes[$key]['path_to_symlink'] => specifies the path to symlink the created volume. |
| 238 | + * @param bool $update_to_docker_prefix Update the prefix in dockerized style. |
| 239 | + */ |
| 240 | + public static function create_volumes( $prefix, $volumes, $update_to_docker_prefix = true ) { |
| 241 | + |
| 242 | + $volume_prefix = $update_to_docker_prefix ? self::get_docker_style_prefix( $prefix ) : $prefix; |
| 243 | + $fs = new Filesystem(); |
| 244 | + |
| 245 | + // This command will get the root directory for Docker, generally `/var/lib/docker`. |
| 246 | + $launch = EE::launch( "docker info 2> /dev/null | awk '/Docker Root Dir/ {print $4}'" ); |
| 247 | + $docker_root_dir = trim( $launch->stdout ); |
| 248 | + |
| 249 | + foreach ( $volumes as $volume ) { |
| 250 | + $fs->mkdir( dirname( $volume['path_to_symlink'] ) ); |
| 251 | + EE::exec( |
| 252 | + sprintf( |
| 253 | + 'docker volume create \ |
| 254 | + --label "org.label-schema.vendor=EasyEngine" \ |
| 255 | + --label "io.easyengine.site=%s" \ |
| 256 | + %s_%s', |
| 257 | + $prefix, |
| 258 | + $volume_prefix, |
| 259 | + $volume['name'] |
| 260 | + ) |
| 261 | + ); |
| 262 | + $fs->symlink( sprintf( '%s/volumes/%s_%s/_data', $docker_root_dir, $volume_prefix, $volume['name'] ), $volume['path_to_symlink'] ); |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + /** |
| 267 | + * Function to get all the volumes with a specific label. |
| 268 | + * |
| 269 | + * @param string $label The label to search for. |
| 270 | + * |
| 271 | + * @return array Found containers. |
| 272 | + */ |
| 273 | + public static function get_volumes_by_label( $label ) { |
| 274 | + $launch = EE::launch( sprintf( 'docker volume ls --filter="label=org.label-schema.vendor=EasyEngine" --filter="label=io.easyengine.site=%s" -q', $label ) ); |
| 275 | + |
| 276 | + return array_filter( explode( PHP_EOL, trim( $launch->stdout ) ), 'trim' ); |
| 277 | + } |
217 | 278 | } |
0 commit comments