Skip to content

Commit b732418

Browse files
committed
Merge pull request #39 from paliarush/Email-Logging
Added email logging support (#9)
2 parents b8bbc66 + 01ddea2 commit b732418

15 files changed

+90
-32
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2323

2424
### Added
2525

26+
- Added setup and upgrade cron scripts to crontab
27+
- Added logging of all emails in HTML format to `vagrant-magento/log/email`
2628
- Added host wrapper script for bin/magento command on guest
2729
- Added ability to modify guest config files (PHP, Apache etc) directly from host IDE
2830
- Added ability to choose if PhpStorm configs should be removed during project reinitialization

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* [Use Magento CLI (bin/magento)](#use-magento-cli-binmagento)
2020
* [Debugging with XDebug](#debugging-with-xdebug)
2121
* [Connecting to MySQL DB](#connecting-to-mysql-db)
22+
* [View emails sent by Magento](#view-emails-sent-by-magento)
2223
* [Accessing PHP and other config files](#accessing-php-and-other-config-files)
2324
* [Switch between PHP 5.6 and 7.0](#switch-between-php-56-and-70)
2425
* [Multiple Magento instances](#multiple-magento-instances)
@@ -134,6 +135,7 @@ Note, that semantic versioning is only used for `x.0` branches (not for `develop
134135
1. Make sure that you used `vagrant-magento` directory as project root in PHP Storm (not `vagrant-magento/magento2ce`)
135136
1. If code is not synchronized properly on Windows hosts (or when NFS mode is disabled in [config.yaml](etc/config.yaml.dist) explicitly), make sure that PhpStorm is running before making any changes in the code. This is important because otherwise PhpStorm will not be able to detect changes and upload them to the guest machine
136137
1. Please make sure that currently installed software, specified in [requirements section](#requirements), meets minimum version requirement
138+
1. If MySQL fails to start and Magento reinstallation fails with `ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (13)`, try to run login to virtual machine using `vagrant ssh` and then run `sudo dpkg-reconfigure mysql-server-5.6`, then `sudo service mysql restart.`
137139

138140
## Day-to-day development scenarios
139141

@@ -212,6 +214,10 @@ To debug Magento Setup script, go to [Magento installation script](scripts/guest
212214

213215
Answer can be found [here](https://github.com/paliarush/magento2-vagrant-for-developers/issues/8)
214216

217+
### View emails sent by Magento
218+
219+
All emails are saved to 'vagrant-magento/log/email' in HTML format.
220+
215221
### Accessing PHP and other config files
216222

217223
It is possible to view/modify majority of guest machine config files directly from IDE on the host. They will be accessible in [etc/guest](etc/guest) directory only when guest machine is running. The list of accessible configs includes: PHP, Apache, Mysql, Varnish, RabbitMQ.
@@ -231,17 +237,15 @@ Unique IP address, SSH port and domain name will be generated for each new insta
231237

232238
### Reset environment
233239

234-
It is possible to reset project environment to default state, which you usually get just after project initialization. The following command will delete vagrant box, vagrant project settings and PhpStorm project settings. After that it will initialize project from scratch. Magento 2 code base (`magento2ce` directory) and [etc/config.yaml](etc/config.yaml.dist) will stay untouched, but guest config files (located in [etc/guest](etc/guest)) will be removed.
240+
It is possible to reset project environment to default state, which you usually get just after project initialization. The following command will delete vagrant box and vagrant project settings. After that it will initialize project from scratch. Magento 2 code base (`magento2ce` directory) and [etc/config.yaml](etc/config.yaml.dist) and PhpStorm settings will stay untouched, but guest config files (located in [etc/guest](etc/guest)) will be cleared.
235241

236242
Go to 'vagrant-magento' created earlier and run in command line:
237243

238244
```
239245
bash init_project.sh -f
240246
```
241247

242-
You can reset project settings and Magento 2 code base at the same time. Magento 2 code base will be deleted and then cloned from the repositories specified in [etc/config.yaml](etc/config.yaml.dist)
243-
244-
Go to 'vagrant-magento' created earlier and run in command line:
248+
It is possible to reset Magento 2 code base at the same time. Magento 2 code base will be deleted and then cloned from the repositories specified in [etc/config.yaml](etc/config.yaml.dist)
245249

246250
```
247251
bash init_project.sh -fc
@@ -251,6 +255,10 @@ To reset PhpStorm project configuration, in addition to `-f` specify `-p` option
251255

252256
```
253257
bash init_project.sh -fp
254-
OR
258+
```
259+
260+
Ultimate project reset can be achieved by combining all available flags:
261+
262+
```
255263
bash init_project.sh -fcp
256264
```

Vagrantfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Vagrant.configure(VAGRANT_API_VERSION) do |config|
4949
config.vm.synced_folder '.', '/vagrant', disabled: true
5050
config.vm.synced_folder './etc', '/vagrant/etc', mount_options: ["dmode=775,fmode=664"]
5151
config.vm.synced_folder './scripts', '/vagrant/scripts'
52+
config.vm.synced_folder './log', '/vagrant/log'
5253
config.vm.synced_folder './.idea', '/vagrant/.idea', create: true
5354
if use_nfs_for_synced_folders
5455
guest_magento_dir = host_magento_dir

init_project.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ if [[ ${force_project_cleaning} -eq 1 ]]; then
5252
rm -rf "${vagrant_dir}/.vagrant" "${vagrant_dir}/etc/guest"
5353
mkdir "${vagrant_dir}/etc/guest"
5454
mv "${vagrant_dir}/etc/.gitignore.back" "${vagrant_dir}/etc/guest/.gitignore"
55+
cd "${vagrant_dir}/log" && mv email/.gitignore email_gitignore.back && rm -rf email && mkdir email && mv email_gitignore.back email/.gitignore
5556
if [[ ${force_codebase_cleaning} -eq 1 ]]; then
5657
rm -rf "${magento_ce_dir}"
5758
fi

log/email/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

m-switch-to-ce

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ php_executable=$(bash "${vagrant_dir}/scripts/host/get_path_to_php.sh")
99
# Enable trace printing and exit on the first error
1010
set -ex
1111

12-
if [ ! -f ${magento_ee_dir}/app/etc/aliases_to_classes_map.json ]; then
12+
if [[ ! -f ${magento_ee_dir}/app/etc/aliases_to_classes_map.json ]]; then
1313
echo "EE codebase is not available"
1414
exit 0
1515
fi
1616

17-
if [ ! -f ${magento_ce_dir}/app/etc/aliases_to_classes_map.json ]; then
17+
if [[ ! -f ${magento_ce_dir}/app/etc/aliases_to_classes_map.json ]]; then
1818
echo "Already switched to CE"
1919
exit 0
2020
fi
@@ -27,10 +27,10 @@ git checkout composer.lock
2727

2828
if [[ ${host_os} == "Windows" || $(bash "${vagrant_dir}/scripts/get_config_value.sh" "guest_use_nfs") == 0 ]]; then
2929
# Prevent issues on Windows with incorrect symlinks to files
30-
if [ -f ${magento_ce_dir}/app/etc/aliases_to_classes_map.json ]; then
30+
if [[ -f ${magento_ce_dir}/app/etc/aliases_to_classes_map.json ]]; then
3131
rm ${magento_ce_dir}/app/etc/aliases_to_classes_map.json
3232
fi
33-
if [ -f ${magento_ce_dir}/app/etc/enterprise/di.xml ] && [ ! -L ${magento_ce_dir}/app/etc/enterprise ]; then
33+
if [[ -f ${magento_ce_dir}/app/etc/enterprise/di.xml ]] && [[ ! -L ${magento_ce_dir}/app/etc/enterprise ]]; then
3434
rm ${magento_ce_dir}/app/etc/enterprise/di.xml
3535
rmdir ${magento_ce_dir}/app/etc/enterprise
3636
fi

m-switch-to-ee

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ php_executable=$(bash "${vagrant_dir}/scripts/host/get_path_to_php.sh")
99
# Enable trace printing and exit on the first error
1010
set -ex
1111

12-
if [ ! -f ${magento_ee_dir}/app/etc/aliases_to_classes_map.json ]; then
12+
if [[ ! -f ${magento_ee_dir}/app/etc/aliases_to_classes_map.json ]]; then
1313
echo "EE codebase is not available"
1414
exit 0
1515
fi
1616

17-
if [ -f ${magento_ce_dir}/app/etc/aliases_to_classes_map.json ]; then
17+
if [[ -f ${magento_ce_dir}/app/etc/aliases_to_classes_map.json ]]; then
1818
echo "Already switched to EE"
1919
exit 0
2020
fi
@@ -26,11 +26,11 @@ cp ${magento_ee_dir}/composer.lock ${magento_ce_dir}/composer.lock
2626

2727
if [[ ${host_os} == "Windows" || $(bash "${vagrant_dir}/scripts/get_config_value.sh" "guest_use_nfs") == 0 ]]; then
2828
# Prevent issues on Windows with incorrect symlinks to files
29-
if [ -f ${magento_ee_dir}/app/etc/aliases_to_classes_map.json ] && [ -L ${magento_ce_dir}/app/etc/aliases_to_classes_map.json ]; then
29+
if [[ -f ${magento_ee_dir}/app/etc/aliases_to_classes_map.json ]] && [[ -L ${magento_ce_dir}/app/etc/aliases_to_classes_map.json ]]; then
3030
rm ${magento_ce_dir}/app/etc/aliases_to_classes_map.json
3131
cp ${magento_ee_dir}/app/etc/aliases_to_classes_map.json ${magento_ce_dir}/app/etc/aliases_to_classes_map.json
3232
fi
33-
if [ -f ${magento_ee_dir}/app/etc/enterprise/di.xml ] && [ -L ${magento_ce_dir}/app/etc/enterprise ]; then
33+
if [[ -f ${magento_ee_dir}/app/etc/enterprise/di.xml ]] && [[ -L ${magento_ce_dir}/app/etc/enterprise ]]; then
3434
rm ${magento_ce_dir}/app/etc/enterprise
3535
mkdir ${magento_ce_dir}/app/etc/enterprise
3636
cp ${magento_ee_dir}/app/etc/enterprise/di.xml ${magento_ce_dir}/app/etc/enterprise/di.xml

scripts/guest/link_configs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ function process_configs () {
55
configs=$2
66
for config in "${configs[@]}"
77
do
8-
if [ ! -d /vagrant/etc/guest/${config} ] && [ ! -f /vagrant/etc/guest/${config} ]; then
9-
if [ -d ${configs_path}/${config} ] || [ -f ${configs_path}/${config} ]; then
8+
if [[ ! -d /vagrant/etc/guest/${config} ]] && [[ ! -f /vagrant/etc/guest/${config} ]]; then
9+
if [[ -d ${configs_path}/${config} ]] || [[ -f ${configs_path}/${config} ]]; then
1010
sudo rm -rf "${configs_path}/${config}.back"
1111
sudo cp -rp ${configs_path}/${config} "${configs_path}/${config}.back"
1212
sudo mv ${configs_path}/${config} /vagrant/etc/guest/${config}

scripts/guest/log_email

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
3+
email_log_dir=$1
4+
if [[ ! -d ${email_log_dir} ]]; then
5+
mkdir -p "${email_log_dir}"
6+
fi
7+
8+
# Construct email file path
9+
current_time=$( date +"%Y-%m-%d_%H-%M-%S" )
10+
# Add random suffix to file name just in case there several emails sent in one second
11+
random_suffix=$(( RANDOM % 100 ))
12+
email_file_path="${email_log_dir}/${current_time}_${random_suffix}"
13+
14+
raw_email_output=$(cat)
15+
16+
# Add email topic to file name
17+
pattern="Subject: (.*)\sX-PHP-Originating-Script:"
18+
if [[ "${raw_email_output}" =~ ${pattern} ]]; then
19+
email_topic=${BASH_REMATCH[1]}
20+
email_file_path="${email_file_path}_${email_topic}"
21+
fi
22+
email_file_path="${email_file_path}.html"
23+
24+
# Output content
25+
echo ${raw_email_output} > "${email_file_path}"
26+
27+
# Process raw content of an email to make it a valid HTML
28+
sed -i "s|.*<\!DOCTYPE|<\!DOCTYPE|g" "${email_file_path}"
29+
sed -i "s|=3D|=|g" "${email_file_path}"
30+
sed -i "s|=0A||g" "${email_file_path}"
31+
sed -i "s|= ||g" "${email_file_path}"

scripts/guest/m-reinstall

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ install_cmd="./bin/magento setup:install \
6363
--use-rewrites=1"
6464

6565
# Configure Rabbit MQ
66-
if [ -f "${MAGENTO_ROOT}/app/etc/enterprise/di.xml" ] && [ -d "${MAGENTO_ROOT}/app/code/Magento/Amqp" ]; then
66+
if [[ -f "${MAGENTO_ROOT}/app/etc/enterprise/di.xml" ]] && [[ -d "${MAGENTO_ROOT}/app/code/Magento/Amqp" ]]; then
6767
install_cmd="${install_cmd} \
6868
--amqp-host=${setupOptions[amqp_host]} \
6969
--amqp-port=${setupOptions[amqp_port]} \
@@ -78,7 +78,9 @@ php ${install_cmd}
7878
# php -d xdebug.remote_host=192.168.10.1 -d xdebug.idekey=PHPSTORM -d xdebug.remote_connect_back=0 -d xdebug.remote_autostart=1 ${install_cmd}
7979

8080
# Enable Magento cron jobs
81-
echo "* * * * * php ${MAGENTO_ROOT}/bin/magento cron:run &" | crontab -u vagrant -
81+
echo "* * * * * php ${MAGENTO_ROOT}/bin/magento cron:run &
82+
* * * * * php ${MAGENTO_ROOT}/update/cron.php &
83+
* * * * * php ${MAGENTO_ROOT}/bin/magento setup:cron:run &" | crontab -u vagrant -
8284

8385
use_nfs=$(bash "${vagrant_dir}/scripts/get_config_value.sh" "guest_use_nfs")
8486
is_windows_host=${IS_WINDOWS_HOST}

0 commit comments

Comments
 (0)