Homestead run command as root

Over my Homestead configuration, I try to make my after.sh script to auto-configure xdebug so over a box update or re-creation to be able to enfroce my configuration for it without the need to redo it manually all over the time.

The script is as follows:

#!/bin/sh

echo "Configuring Xdebug"
ip=$(netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10)
xdebug_config="/etc/php/$(php -v | head -n 1 | awk '{print $2}'|cut -c 1-3)/mods-available/xdebug.ini"

echo "IP for the xdebug to connect back: ${ip}"
echo "Xdebug Configuration path: ${xdebug_config}"
echo "Port for the Xdebug to connect back: ${XDEBUG_PORT}"
echo "Optimize for ${IDE} ide"
first_line=$(head -n1 ${xdebug_config})

if [ $IDE=='atom' ]; then
  echo "Configuring xdebug for ATOM ide"
  sudo cat <<EOL >${xdebug_config}
${first_line}
xdebug.remote_enable = 1
xdebug.remote_host=${ip}
xdebug.remote_port = ${XDEBUG_PORT}
xdebug.max_nesting_level = 1000
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart=true
xdebug.remote_log=xdebug.log
EOL
fi

And my Homestead.yml is as follows:

ip: 192.168.10.10
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
timeout: 120

keys:
    - ~/.ssh/id_rsa
folders:
    -
        map: /home/pcmagas/Kwdikas/php/apps/ellakcy_member_app/
        to: /home/vagrant/code
sites:
    -
        map: homestead.test
        to: /home/vagrant/code/web
        type: symfony

databases:
    - homestead
    - homestead-test

variables:
  - key: database_host
    value: 127.0.0.1
  - key: database_port
    value: 3306
  - key: database_name
    value: homestead
  - key: database_user
    value: homestead
  - key: database_password
    value: secret
  - key: smtp_host
    value: localhost
  - key: smtp_port
    value: 1025
  - key: smtp_user
    value: [email protected]
  - key: IDE
    value: atom
  - key: XDEBUG_PORT
    value: 9091

name: ellakcy-member-app
hostname: ellakcy-member-app

I have setup the following extra enviromental variables:

  - key: IDE
    value: atom
  - key: XDEBUG_PORT
    value: 9091

So I can offer a fine-grained configuration for the xdebug.

But when I run vagrant provision Iget the following error (in order to save space I did nbot put the whole output):

ellakcy-member-app: /tmp/vagrant-shell: 37: /tmp/vagrant-shell: cannot create /etc/php/7.2/mods-available/xdebug.ini: Permission denied

That is caused by he command:

sudo cat <<EOL >${xdebug_config}
${first_line}
xdebug.remote_enable = 1
xdebug.remote_host=${ip}
xdebug.remote_port = ${XDEBUG_PORT}
xdebug.max_nesting_level = 1000
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart=true
xdebug.remote_log=xdebug.log
EOL

So I want to know how I can auto-configure settings for Homestead Vagrant box? (for examplethe xdebug config one)

Answer

The option for now it to run the whole script as root. By changing the following option to the Vagrantfile:

    config.vm.provision "shell", path: afterScriptPath, privileged: **false**, keep_color: true

To

    config.vm.provision "shell", path: afterScriptPath, privileged: **true**, keep_color: true

But it won’t be able to read the enviromental varialbed from Homestead.yml.

Leave a Reply

Your email address will not be published. Required fields are marked *