Homestead pass parameters to after.sh for xdebug autoconfigure

224 Views Asked by At

I put the following to after.sh to autoconfigure the Xdebug form project:

#!/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"

if [ $IDE=='atom' ]; then
  echo "Configuring xdebug for ATOM ide"

  if [ -z ${xdebug_config} ]; then

    sudo touch ${xdebug_config}
  fi

  sudo cat <<EOL >${xdebug_config}
zend_extension = xdebug.so
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

Also I have the following settings to Homestead.yaml:

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

But for some reason it cannot read the values from enviromental variables defined in Homestead.yml as seen in the following output:

ellakcy-member-app: IP for the xdebug to connect back: 10.0.2.2

ellakcy-member-app: Xdebug Configuration path: /etc/php/7.2/mods-available/xdebug.ini

ellakcy-member-app: Port for the Xdebug to connect back:

ellakcy-member-app: Optimize for ide

ellakcy-member-app: Configuring xdebug for ATOM ide

As you can see it fails to read values from the IDE and XDEBUG_PORT do you knwo why and how I can fix that?

2

There are 2 best solutions below

1
On BEST ANSWER

In my case I tried the approach of having a file named xdebug.conf where I place anything that the default xdebug.conf needs to get rewritten:

zend_extension = xdebug.so
xdebug.remote_enable = 1
xdebug.remote_host = $ip
xdebug.remote_port = 9091
xdebug.max_nesting_level = 1000
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart=true
xdebug.remote_log=xdebug.log

The $ip indicates the auto-replaced value with the correct ip in order for the xdebug to get connected into. The script that actually updates the xdebug configuration with the appropriate values is this one in my after.sh

#!/bin/sh
code_path="/home/vagrant/code"
cd $code_path

# Some other bootstrapping

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 "Xdebug config file ${xdebug_config}"

if [ -f "${code_path}/xdebug.conf" ]; then

  echo "Specifying the ip with ${ip}"
  sed "s/\$ip/${ip}/g" xdebug.conf > xdebug.conf.tmp

  echo "Moving Into ${xdebug_config}"
  cat xdebug.conf.tmp
  sudo cp ./xdebug.conf.tmp ${xdebug_config}
else
  echo "File not found"
fi

The last step is to .gitignore any xdebug.conf* file. So now the developer has to create his own xdebug.conf.

0
On

You can put a parse_yaml.sh:

#!/bin/sh
parse_yaml() {
   local prefix=$2
   local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
   sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
        -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p"  $1 |
   awk -F$fs '{
      indent = length($1)/2;
      vname[indent] = $2;
      for (i in vname) {if (i > indent) {delete vname[i]}}
      if (length($3) > 0) {
         vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
         printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
      }
   }'
}

And into after.sh

#!/bin/sh

# include parse_yaml function
. parse_yaml.sh

# read yaml file
eval $(parse_yaml zconfig.yml "config_")

# access yaml content
echo $config_development_database

thanks -> https://gist.github.com/pkuczynski/8665367