From fd0393282881a8e42b8a8604ac5e7eef5db07ac8 Mon Sep 17 00:00:00 2001 From: Ryan McCue Date: Fri, 24 Nov 2023 11:56:19 +0000 Subject: [PATCH 1/4] Add ability to run shell as root This is necessary for installing packages/etc. --- inc/composer/class-command.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/inc/composer/class-command.php b/inc/composer/class-command.php index 4b8b1346..7b4fe010 100644 --- a/inc/composer/class-command.php +++ b/inc/composer/class-command.php @@ -69,7 +69,8 @@ protected function configure() { Run any shell command from the PHP container: exec -- eg: exec -- vendor/bin/phpcs Open a shell: - shell + shell Open a Bash shell, as the www-data user. + shell --root Open a Bash shell, as the root user. Database commands: db Log into MySQL on the Database server db sequel Generates an SPF file for Sequel Pro @@ -86,6 +87,7 @@ protected function configure() { import-uploads Copies files from `content/uploads` to s3 EOT ) + ->addOption( 'root', null, InputOption::VALUE_OPTIONAL, 'Run as root', 'debug' ) ->addOption( 'xdebug', null, InputOption::VALUE_OPTIONAL, 'Start the server with Xdebug', 'debug' ) ->addOption( 'mutagen', null, InputOption::VALUE_NONE, 'Start the server with Mutagen file sharing' ) ->addOption( 'clean', null, InputOption::VALUE_NONE, 'Remove or stop the proxy container when destroying or stopping the server' ) @@ -605,9 +607,15 @@ protected function shell( InputInterface $input, OutputInterface $output ) { $this->get_compose_command( 'ps -q php' ) ) ); + $user = ''; + if ( $input->hasParameterOption( '--root' ) ) { + $user = '-u root'; + } + passthru( sprintf( - "$command_prefix %s exec -it -e COLUMNS=%d -e LINES=%d %s /bin/bash", + "$command_prefix %s exec -it %s -e COLUMNS=%d -e LINES=%d %s /bin/bash", 'docker', + $user, $columns, $lines, trim( $php_container_id ) From 52bc320c09078fc65bff97158cae7c62044c18cb Mon Sep 17 00:00:00 2001 From: Mike Little Date: Tue, 28 Jan 2025 14:45:17 +0000 Subject: [PATCH 2/4] Add the `-r` alias too. --- inc/composer/class-command.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inc/composer/class-command.php b/inc/composer/class-command.php index 7799571e..d93aa805 100644 --- a/inc/composer/class-command.php +++ b/inc/composer/class-command.php @@ -69,8 +69,8 @@ protected function configure() { Run any shell command from the PHP container: exec -- eg: exec -- vendor/bin/phpcs Open a shell: - shell Open a Bash shell, as the www-data user. - shell --root Open a Bash shell, as the root user. + shell Open a Bash shell as the www-data user. + shell --root|-r Open a Bash shell as the root user. Database commands: db Log into MySQL on the Database server db (sequel|spf) Opens Sequel Pro/Sequel Ace with the database connection @@ -88,7 +88,7 @@ protected function configure() { import-uploads Syncs files from `content/uploads` to the S3 container. EOT ) - ->addOption( 'root', null, InputOption::VALUE_OPTIONAL, 'Run as root', 'debug' ) + ->addOption( 'root', 'r', InputOption::VALUE_OPTIONAL, 'Run as root', 'debug' ) ->addOption( 'xdebug', null, InputOption::VALUE_OPTIONAL, 'Start the server with Xdebug', 'debug' ) ->addOption( 'mutagen', null, InputOption::VALUE_NONE, 'Start the server with Mutagen file sharing' ) ->addOption( 'clean', null, InputOption::VALUE_NONE, 'Remove or stop the proxy container when destroying or stopping the server' ) @@ -622,7 +622,7 @@ protected function shell( InputInterface $input, OutputInterface $output ) { ) ); $user = ''; - if ( $input->hasParameterOption( '--root' ) ) { + if ( $input->hasParameterOption( '--root' ) || $input->hasParameterOption( '-r' ) ) { $user = '-u root'; } From ae78d325b5c41cc32606fa9eb0a6eb80c578dc75 Mon Sep 17 00:00:00 2001 From: Mike Little Date: Tue, 28 Jan 2025 15:09:58 +0000 Subject: [PATCH 3/4] Document the shell command. --- docs/cli.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/cli.md b/docs/cli.md index 719870ed..2f32f80d 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -15,6 +15,22 @@ Or to show all environment variables: composer server exec -- printenv ``` +## Shell sessions + +You can also start a shell session on the web server container using the `composer server shell` command. This will start an +interactive shell session on the web server container. + +You will normally be logged in as the `www-data` user, but you can run the shell as the root user by passing the `--root` option. + +```sh +composer server shell --root +``` + +note: The `--root` option is only available when running the shell session on local server. Any changes you make to the +container will not be carried over to the Altis environment. + +```sh + ## WP CLI Local Server provides special support for [WP CLI](https://wp-cli.org/) commands via the `composer server cli --` command. Prepend From ab5a106ebe634aee76f67d59dad2c88c6ca5ca1b Mon Sep 17 00:00:00 2001 From: Mike Little Date: Tue, 28 Jan 2025 15:09:58 +0000 Subject: [PATCH 4/4] Document the shell command. --- docs/cli.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/cli.md b/docs/cli.md index 719870ed..076b5e2e 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -15,6 +15,26 @@ Or to show all environment variables: composer server exec -- printenv ``` +## Shell sessions + +You can also start a shell session on the web server container using the `composer server shell` command. This will start an +interactive shell session on the web server container. + +### Running as root + +You will normally be logged in as the `www-data` user, but you can run the shell as the root user by passing the `--root` option. +You may want to do this to test modifications to the PHP container eg. installing PHP libraries or other packages or to carry out +any other tasks that require sudo privileges such as making directories outside the app directory + +```sh +composer server shell --root +``` + +note: The `--root` option is only available when running the shell session on local server. Any changes you make to the +container will not be carried over to the Altis environment. + +```sh + ## WP CLI Local Server provides special support for [WP CLI](https://wp-cli.org/) commands via the `composer server cli --` command. Prepend