Skip to content

Commit 5d5f5aa

Browse files
authored
Merge pull request #1700 from mrrobot47/global/compose-exec
Add helper function for skip-tty support
2 parents d05add8 + e7559fc commit 5d5f5aa

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

php/class-ee-docker.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public static function disconnect_site_network_from( $site_name, $from_container
183183
* @return bool success.
184184
*/
185185
public static function docker_compose_up( $dir, $services = [] ) {
186-
$fs = new Filesystem();
186+
187187
$chdir_return_code = chdir( $dir );
188188
if ( $chdir_return_code ) {
189189
if ( empty( $services ) ) {
@@ -198,6 +198,51 @@ public static function docker_compose_up( $dir, $services = [] ) {
198198
return false;
199199
}
200200

201+
/**
202+
* Function to exec and run commands into the containers.
203+
*
204+
* @param string $command Command to exec.
205+
* @param string $service Service to exec command into.
206+
* @param string $shell Shell in which exec command will be executed.
207+
* @param string $user User to execute command into.
208+
* @param String $dir Path to docker-compose.yml.
209+
* @param bool $shell_wrapper If shell wrapper should be enabled or not.
210+
* @param bool $exit_on_error To exit or not on error.
211+
* @param array $exec_obfuscate Data to be obfuscated from log.
212+
* @param bool $echo_stdout Output stdout of exec if true.
213+
* @param bool $echo_stderr Output stderr of exec if true.
214+
*
215+
* @return bool success.
216+
*/
217+
public static function docker_compose_exec( $command = '', $service = '', $shell = 'sh', $user = '', $dir = '', $shell_wrapper = false, $exit_on_error = false, $exec_obfuscate = [], $echo_stdout = false, $echo_stderr = false ) {
218+
219+
if ( ! empty( $dir ) ) {
220+
$chdir_return_code = chdir( $dir );
221+
} else {
222+
$chdir_return_code = true;
223+
}
224+
225+
$skip_tty = \EE::get_runner()->config['skip-tty'];
226+
$tty = empty( $skip_tty ) ? '' : '-T';
227+
228+
if ( $chdir_return_code ) {
229+
230+
$user_string = '';
231+
if ( $user ) {
232+
$user_string = empty( $user ) ? '' : "--user='$user'";
233+
}
234+
235+
if ( $shell_wrapper ) {
236+
return EE::exec( \EE_DOCKER::docker_compose_with_custom() . " exec $tty $user_string $service $shell -c \"$command\"", $echo_stdout, $echo_stderr, $exec_obfuscate, $exit_on_error );
237+
} else {
238+
return EE::exec( \EE_DOCKER::docker_compose_with_custom() . " exec $tty $user_string $service $command", $echo_stdout, $echo_stderr, $exec_obfuscate, $exit_on_error );
239+
}
240+
241+
}
242+
243+
return false;
244+
}
245+
201246
/**
202247
* Function to check if a network exists
203248
*

php/config-spec.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@
5252
'desc' => 'Whether to add sysctl config in docker-compose.',
5353
),
5454

55+
'skip-tty' => array(
56+
'runtime' => '=<true/false>',
57+
'file' => '<bool>',
58+
'default' => false,
59+
'desc' => 'Skip tty allocation for remote command execution.',
60+
),
61+
5562
'ee_installer_version' => array(
5663
'file' => '<path>',
5764
'default' => null,

0 commit comments

Comments
 (0)