# Only use spaces to indent your .yml configuration.
# This is a sample build configuration for PHP.
# Check our guides at https://confluence.atlassian.com/x/e8YWN for more examples.
# -----
# You can specify a custom docker image from Dockerhub as your build environment.
image: ziomizar/drupal-lamp-test-selenium

options:
  max-time: 35

clone:
  # Make sure the repository is cloned with all references, as platform
  # otherwise rejects pushes.
  depth: full

definitions:
  caches:
    npm: docroot/themes/svv/node_modules

pipelines:
  pull-requests:
    '**': #this runs as default for any branch not elsewhere defined
      - step:
          caches:
            - composer
            - npm
          name: Run tests and build PR instance
          script:
            - git submodule update --init
            # configure ssh
            - echo $SVV_PRIVATE_KEY > ~/.ssh/id_rsa.tmp
            - base64 -di ~/.ssh/id_rsa.tmp > ~/.ssh/id_rsa
            - sudo chown -Rv seluser /home/seluser/.ssh
            - sudo chmod 600 /home/seluser/.ssh/id_rsa
            - sudo apt-get update -y
            - sudo DEBIAN_FRONTEND=noninteractive apt-get install php7.1-gd -yq
            - sudo DEBIAN_FRONTEND=noninteractive apt-get install libnotify-bin -yq

            # SELENIUM - CHROME
            # TODO: remove this packages from the docker directly
            # 2. enable just what we need
            - export DISPLAY=:99
            - Xvfb :99 -shmem -screen 0 1366x768x16 &
            # - x11vnc -passwd secret -display :99 -N -forever &
            # - selenium must be started by a non-root user otherwise chrome can't start
            # - java -jar /usr/lib/node_modules/selenium-standalone/.selenium/selenium-server/2.50.1-server.jar - Ddocrootdriver.chrome.driver='/usr/lib/node_modules/selenium-standalone/.selenium/chromedriver/2.21-x64-chromedriver' -hub http://localhost:4444/grid/register -port 4444 -browser browserName=chrome,maxInstances=5,platform=LINUX &
            - wget http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.0.jar
            - wget http://chromedriver.storage.googleapis.com/2.36/chromedriver_linux64.zip
            - unzip chromedriver_linux64.zip
            # force a crhome update to the latest version
            - wget -N https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -P ~/
            - sudo dpkg -i --force-depends ~/google-chrome-stable_current_amd64.deb
            - sudo apt-get -f install -y
            - sudo dpkg -i --force-depends ~/google-chrome-stable_current_amd64.deb
            # start selenium
            - java -client -jar selenium-server-standalone-2.53.0.jar -Ddocrootdriver.chrome.driver='chromedriver' -browser browserName=chrome,maxInstances=5,platform=LINUX &
            # - wait until slenium start and open port 4444
            - sleep 10s

            # copyng 000-default into the home fix the error reported by sed
            # temporary file can't be created also if this command run as sudo
            - sudo cp /etc/apache2/sites-available/000-default.conf /home/seluser/default.conf
            - cd /home/seluser
            - sudo chown seluser default.conf
            # TODO: move this on Dockerfile to save build time
            - sudo a2enmod rewrite
            - echo $'<VirtualHost *:80>\nServerAdmin docrootmaster@localhost\nDocumentRoot /var/www/svv.ch/docroot\n<Directory "/var/www/svv.ch/docroot">\nOptions FollowSymLinks\nAllowOverride All\nOrder allow,deny\nAllow from all\n</Directory>\n</VirtualHost>' > default.conf
            - sudo cp default.conf /etc/apache2/sites-available/000-default.conf
            - sudo /etc/init.d/mysql start
            - cd ~
            # move the repo into the docroot document root
            # TODO: change the hardcoded path with $BUILD_DIR
            - sudo mkdir /var/www/svv.ch
            - sudo cp -aRv /opt/atlassian/pipelines/agent/build/* /var/www/svv.ch
            - sudo rm -Rv /var/www/html
            - cd /var/www/
            - sudo mkdir /var/www/svv.ch/docroot/sites/default/files
            - sudo chmod -Rv 777 /var/www/svv.ch/docroot/sites/default/files
            - sudo chown -R seluser /var/www/
            # restart apache just when all the permissions and folder are configured
            - sudo /etc/init.d/apache2 restart
            # copy settings : station_drupal_database is an environment variable configured on bitbucket backend.
            - touch /var/www/svv.ch/docroot/sites/default/settings.local.php
            - echo "<?php \$databases['default']['default'] = array ('database' => 'svv','username' => 'svv','password' => 'password','host' => 'localhost','port' => '3306','driver' => 'mysql','prefix' => '','collation' => 'utf8mb4_general_ci',);" | tee /var/www/svv.ch/docroot/sites/default/settings.local.php > /dev/null
            - cat /var/www/svv.ch/docroot/sites/default/settings.local.php
            - sudo mysql -uroot -e 'create database svv'
            - sudo mysql -uroot -e "CREATE USER 'svv'@'localhost' IDENTIFIED BY 'password'";
            - sudo mysql -uroot -e "GRANT ALL PRIVILEGES ON * . * TO 'svv'@'localhost';";
            - cd /var/www/svv.ch
            # composer has been added to git, no need to run composer update
            - composer install

            # install drush launcher according to https://github.com/drush-ops/drush-launcher
            - wget -O drush.phar https://github.com/drush-ops/drush-launcher/releases/download/0.6.0/drush.phar
            - chmod +x drush.phar
            - sudo mv drush.phar /usr/local/bin/drush

            - cd /var/www/svv.ch
            - cd docroot
            - drush status

            # Sync DB from production.
            - cd /var/www/svv.ch/docroot
            - drush sql-sync --source-dump=/tmp/pipeline.sql.gz --target-dump=/tmp/pipeline.sql.gz @svv.pipeline @self -vvv -y

            - drush status
            - drush cr -y
            - drush updb -y
            - drush cr -y
            - drush config-import -y

            # debug process list
            - ps aux
            # debug open ports
            - netstat -nat

            # build assets inside the template folder
            - cd /var/www/svv.ch/docroot/themes/svv/
            # update to the latest verison of npm before proceed
            - sudo npm cache clean
            - sudo npm install -g n
            - sudo n stable
            - sudo npm install -g npm
            - node -v
            - npm -v
            # install webpack
            #- sudo npm install --save-dev webpack
            - sudo npm install webpack
            - sudo npm install --unsafe-perm
            - sudo npm run build

            # clean drupal cache again
            - sudo chown -R seluser /var/www/
            - drush cr

            # -q parameter means quite. This is needed for debugging reasons
            # without it, its not possible print the output of the error log
            - wget -q http://localhost/ || true

            - cp /var/www/svv.ch/docroot/.htaccess.local /var/www/svv.ch/docroot/.htaccess

            # print any error if presents
            - sudo tail -n 100 /var/log/apache2/error.log

            # behat configuration
            - cd /var/www/svv.ch
            - cp /var/www/svv.ch/behat-local.yml.example /var/www/svv.ch/behat-local.yml
            - sed -i 's/svv.loc/localhost/g' /var/www/svv.ch/behat-local.yml

            # DEBUG INFOS
            # - cat /var/www/svv.ch/docroot/behat-local.yml
            # - cat /var/www/svv.ch/docroot/behat.yml
            # - wget http://localhost:4444/wd/hub

            - cd /var/www/svv.ch
            - vendor/behat/behat/bin/behat -v

            # These variables are created manually in the bitbucket repo.
            - export PLATFORMSH_CLI_TOKEN=$PF_API_TOKEN
            # Finally some more variables for stuff that's used in scripts.
            - export PROJECT_DOCROOT=$BITBUCKET_CLONE_DIR/docroot
            - export PROJECT_NAME=svv
            - export SCRIPTS_DIR=$BITBUCKET_CLONE_DIR/scripts/pipelines
            - export PLATFORM_GIT_REMOTE=hvlxjignu2rwk@git.eu.platform.sh:hvlxjignu2rwk.git
            # If all tests and code linting passed, create a DB backup, a git
            # tag and deploy to platform.
            - sh $SCRIPTS_DIR/platform.deploy.sh
            - bash $SCRIPTS_DIR/activate-platform-instance.sh
  branches:
    master:
      - step:
          caches:
            - composer
            - npm
          name: Test and deploy to platform
          script:
            - git submodule update --init
            # configure ssh
            - echo $SVV_PRIVATE_KEY > ~/.ssh/id_rsa.tmp
            - base64 -di ~/.ssh/id_rsa.tmp > ~/.ssh/id_rsa
            - sudo chown -Rv seluser /home/seluser/.ssh
            - sudo chmod 600 /home/seluser/.ssh/id_rsa
            - sudo apt-get update -y
            - sudo DEBIAN_FRONTEND=noninteractive apt-get install php7.1-gd -yq
            - sudo DEBIAN_FRONTEND=noninteractive apt-get install libnotify-bin -yq

            # SELENIUM - CHROME
            # TODO: remove this packages from the docker directly
            # 2. enable just what we need
            - export DISPLAY=:99
            - Xvfb :99 -shmem -screen 0 1366x768x16 &
            # - x11vnc -passwd secret -display :99 -N -forever &
            # - selenium must be started by a non-root user otherwise chrome can't start
            # - java -jar /usr/lib/node_modules/selenium-standalone/.selenium/selenium-server/2.50.1-server.jar - Ddocrootdriver.chrome.driver='/usr/lib/node_modules/selenium-standalone/.selenium/chromedriver/2.21-x64-chromedriver' -hub http://localhost:4444/grid/register -port 4444 -browser browserName=chrome,maxInstances=5,platform=LINUX &
            - wget http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.0.jar
            - wget http://chromedriver.storage.googleapis.com/2.36/chromedriver_linux64.zip
            - unzip chromedriver_linux64.zip
            # force a crhome update to the latest version
            - wget -N https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -P ~/
            - sudo dpkg -i --force-depends ~/google-chrome-stable_current_amd64.deb
            - sudo apt-get -f install -y
            - sudo dpkg -i --force-depends ~/google-chrome-stable_current_amd64.deb
            # start selenium
            - java -client -jar selenium-server-standalone-2.53.0.jar -Ddocrootdriver.chrome.driver='chromedriver' -browser browserName=chrome,maxInstances=5,platform=LINUX &
            # - wait until slenium start and open port 4444
            - sleep 10s

            # copyng 000-default into the home fix the error reported by sed
            # temporary file can't be created also if this command run as sudo
            - sudo cp /etc/apache2/sites-available/000-default.conf /home/seluser/default.conf
            - cd /home/seluser
            - sudo chown seluser default.conf
            # TODO: move this on Dockerfile to save build time
            - sudo a2enmod rewrite
            - echo $'<VirtualHost *:80>\nServerAdmin docrootmaster@localhost\nDocumentRoot /var/www/svv.ch/docroot\n<Directory "/var/www/svv.ch/docroot">\nOptions FollowSymLinks\nAllowOverride All\nOrder allow,deny\nAllow from all\n</Directory>\n</VirtualHost>' > default.conf
            - sudo cp default.conf /etc/apache2/sites-available/000-default.conf
            - sudo /etc/init.d/mysql start
            - cd ~
            # move the repo into the docroot document root
            # TODO: change the hardcoded path with $BUILD_DIR
            - sudo mkdir /var/www/svv.ch
            - sudo cp -aRv /opt/atlassian/pipelines/agent/build/* /var/www/svv.ch
            - sudo rm -Rv /var/www/html
            - cd /var/www/
            - sudo mkdir /var/www/svv.ch/docroot/sites/default/files
            - sudo chmod -Rv 777 /var/www/svv.ch/docroot/sites/default/files
            - sudo chown -R seluser /var/www/
            # restart apache just when all the permissions and folder are configured
            - sudo /etc/init.d/apache2 restart
            # copy settings : station_drupal_database is an environment variable configured on bitbucket backend.
            - touch /var/www/svv.ch/docroot/sites/default/settings.local.php
            - echo "<?php \$databases['default']['default'] = array ('database' => 'svv','username' => 'svv','password' => 'password','host' => 'localhost','port' => '3306','driver' => 'mysql','prefix' => '','collation' => 'utf8mb4_general_ci',);" | tee /var/www/svv.ch/docroot/sites/default/settings.local.php > /dev/null
            - cat /var/www/svv.ch/docroot/sites/default/settings.local.php
            - sudo mysql -uroot -e 'create database svv'
            - sudo mysql -uroot -e "CREATE USER 'svv'@'localhost' IDENTIFIED BY 'password'";
            - sudo mysql -uroot -e "GRANT ALL PRIVILEGES ON * . * TO 'svv'@'localhost';";
            - cd /var/www/svv.ch
            # composer has been added to git, no need to run composer update
            - composer install

            # install drush launcher according to https://github.com/drush-ops/drush-launcher
            - wget -O drush.phar https://github.com/drush-ops/drush-launcher/releases/download/0.6.0/drush.phar
            - chmod +x drush.phar
            - sudo mv drush.phar /usr/local/bin/drush

            - cd /var/www/svv.ch
            - cd docroot
            - drush status

            # Sync DB from production.
            - cd /var/www/svv.ch/docroot
            - drush sql-sync --source-dump=/tmp/pipeline.sql.gz --target-dump=/tmp/pipeline.sql.gz @svv.pipeline @self -vvv -y

            - drush status
            - drush cr -y
            - drush updb -y
            - drush cr -y
            - drush config-import -y

            # debug process list
            - ps aux
            # debug open ports
            - netstat -nat

            # build assets inside the template folder
            - cd /var/www/svv.ch/docroot/themes/svv/
            # update to the latest verison of npm before proceed
            - sudo npm cache clean
            - sudo npm install -g n
            - sudo n stable
            - sudo npm install -g npm
            - node -v
            - npm -v
            # install webpack
            #- sudo npm install --save-dev webpack
            - sudo npm install webpack
            - sudo npm install --unsafe-perm
            - sudo npm run build

            # clean drupal cache again
            - sudo chown -R seluser /var/www/
            - drush cr

            # -q parameter means quite. This is needed for debugging reasons
            # without it, its not possible print the output of the error log
            - wget -q http://localhost/ || true

            - cp /var/www/svv.ch/docroot/.htaccess.local /var/www/svv.ch/docroot/.htaccess

            # print any error if presents
            - sudo tail -n 100 /var/log/apache2/error.log

            # behat configuration
            - cd /var/www/svv.ch
            - cp /var/www/svv.ch/behat-local.yml.example /var/www/svv.ch/behat-local.yml
            - sed -i 's/svv.loc/localhost/g' /var/www/svv.ch/behat-local.yml

            # DEBUG INFOS
            # - cat /var/www/svv.ch/docroot/behat-local.yml
            # - cat /var/www/svv.ch/docroot/behat.yml
            # - wget http://localhost:4444/wd/hub

            - cd /var/www/svv.ch
            - vendor/behat/behat/bin/behat -v
            # These variables are created manually in the bitbucket repo.
            - export PLATFORMSH_CLI_TOKEN=$PF_API_TOKEN
            # Finally some more variables for stuff that's used in scripts.
            - export PROJECT_DOCROOT=$BITBUCKET_CLONE_DIR/docroot
            - export PROJECT_NAME=svv
            - export SCRIPTS_DIR=$BITBUCKET_CLONE_DIR/scripts/pipelines
            - export PLATFORM_GIT_REMOTE=hvlxjignu2rwk@git.eu.platform.sh:hvlxjignu2rwk.git
            # If all tests and code linting passed, create a DB backup, a git
            # tag and deploy to platform.
            - sh $SCRIPTS_DIR/platform.deploy.sh
  # add this tag to a commit to run specific test without deploy
  tags:
    run-test-*:
      # server configuration
      - step:
          caches:
            - composer
            - npm
          name: Test
          script: # Modify the commands below to build your repository.
            - git submodule update --init
            # configure ssh
            - echo $SVV_PRIVATE_KEY > ~/.ssh/id_rsa.tmp
            - base64 -di ~/.ssh/id_rsa.tmp > ~/.ssh/id_rsa
            - sudo chown -Rv seluser /home/seluser/.ssh
            - sudo chmod 600 /home/seluser/.ssh/id_rsa
            - sudo apt-get update -y
            - sudo DEBIAN_FRONTEND=noninteractive apt-get install php7.1-gd -yq
            - sudo DEBIAN_FRONTEND=noninteractive apt-get install libnotify-bin -yq

            # SELENIUM - CHROME
            # TODO: remove this packages from the docker directly
            # 2. enable just what we need
            - export DISPLAY=:99
            - Xvfb :99 -shmem -screen 0 1366x768x16 &
            # - x11vnc -passwd secret -display :99 -N -forever &
            # - selenium must be started by a non-root user otherwise chrome can't start
            # - java -jar /usr/lib/node_modules/selenium-standalone/.selenium/selenium-server/2.50.1-server.jar - Ddocrootdriver.chrome.driver='/usr/lib/node_modules/selenium-standalone/.selenium/chromedriver/2.21-x64-chromedriver' -hub http://localhost:4444/grid/register -port 4444 -browser browserName=chrome,maxInstances=5,platform=LINUX &
            - wget http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.0.jar
            - wget http://chromedriver.storage.googleapis.com/2.36/chromedriver_linux64.zip
            - unzip chromedriver_linux64.zip
            # force a crhome update to the latest version
            - wget -N https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -P ~/
            - sudo dpkg -i --force-depends ~/google-chrome-stable_current_amd64.deb
            - sudo apt-get -f install -y
            - sudo dpkg -i --force-depends ~/google-chrome-stable_current_amd64.deb
            # start selenium
            - java -client -jar selenium-server-standalone-2.53.0.jar -Ddocrootdriver.chrome.driver='chromedriver' -browser browserName=chrome,maxInstances=5,platform=LINUX &
            # - wait until slenium start and open port 4444
            - sleep 10s

            # copyng 000-default into the home fix the error reported by sed
            # temporary file can't be created also if this command run as sudo
            - sudo cp /etc/apache2/sites-available/000-default.conf /home/seluser/default.conf
            - cd /home/seluser
            - sudo chown seluser default.conf
            # TODO: move this on Dockerfile to save build time
            - sudo a2enmod rewrite
            - echo $'<VirtualHost *:80>\nServerAdmin docrootmaster@localhost\nDocumentRoot /var/www/svv.ch/docroot\n<Directory "/var/www/svv.ch/docroot">\nOptions FollowSymLinks\nAllowOverride All\nOrder allow,deny\nAllow from all\n</Directory>\n</VirtualHost>' > default.conf
            - sudo cp default.conf /etc/apache2/sites-available/000-default.conf
            - sudo /etc/init.d/mysql start
            - cd ~
            # move the repo into the docroot document root
            # TODO: change the hardcoded path with $BUILD_DIR
            - sudo mkdir /var/www/svv.ch
            - sudo cp -aRv /opt/atlassian/pipelines/agent/build/* /var/www/svv.ch
            - sudo rm -Rv /var/www/html
            - cd /var/www/
            - sudo mkdir /var/www/svv.ch/docroot/sites/default/files
            - sudo chmod -Rv 777 /var/www/svv.ch/docroot/sites/default/files
            - sudo chown -R seluser /var/www/
            # restart apache just when all the permissions and folder are configured
            - sudo /etc/init.d/apache2 restart
            # copy settings : station_drupal_database is an environment variable configured on bitbucket backend.
            - touch /var/www/svv.ch/docroot/sites/default/settings.local.php
            - echo "<?php \$databases['default']['default'] = array ('database' => 'svv','username' => 'svv','password' => 'password','host' => 'localhost','port' => '3306','driver' => 'mysql','prefix' => '','collation' => 'utf8mb4_general_ci',);" | tee /var/www/svv.ch/docroot/sites/default/settings.local.php > /dev/null
            - cat /var/www/svv.ch/docroot/sites/default/settings.local.php
            - sudo mysql -uroot -e 'create database svv'
            - sudo mysql -uroot -e "CREATE USER 'svv'@'localhost' IDENTIFIED BY 'password'";
            - sudo mysql -uroot -e "GRANT ALL PRIVILEGES ON * . * TO 'svv'@'localhost';";
            - cd /var/www/svv.ch
            # composer has been added to git, no need to run composer update
            - composer install

            # install drush launcher according to https://github.com/drush-ops/drush-launcher
            - wget -O drush.phar https://github.com/drush-ops/drush-launcher/releases/download/0.6.0/drush.phar
            - chmod +x drush.phar
            - sudo mv drush.phar /usr/local/bin/drush

            - cd /var/www/svv.ch
            - cd docroot
            - drush status

            # Sync DB from production.
            - cd /var/www/svv.ch/docroot
            - drush sql-sync --source-dump=/tmp/pipeline.sql.gz --target-dump=/tmp/pipeline.sql.gz @svv.pipeline @self -vvv -y

            - drush status
            - drush cr -y
            - drush updb -y
            - drush cr -y
            - drush config-import -y

            # debug process list
            - ps aux
            # debug open ports
            - netstat -nat

            # build assets inside the template folder
            - cd /var/www/svv.ch/docroot/themes/svv/
            # update to the latest verison of npm before proceed
            - sudo npm cache clean
            - sudo npm install -g n
            - sudo n stable
            - sudo npm install -g npm
            - node -v
            - npm -v
            # install webpack
            #- sudo npm install --save-dev webpack
            - sudo npm install webpack
            - sudo npm install --unsafe-perm
            - sudo npm run build

            # clean drupal cache again
            - sudo chown -R seluser /var/www/
            - drush cr

            # -q parameter means quite. This is needed for debugging reasons
            # without it, its not possible print the output of the error log
            - wget -q http://localhost/ || true

            - cp /var/www/svv.ch/docroot/.htaccess.local /var/www/svv.ch/docroot/.htaccess

            # print any error if presents
            - sudo tail -n 100 /var/log/apache2/error.log

            # behat configuration
            - cd /var/www/svv.ch
            - cp /var/www/svv.ch/behat-local.yml.example /var/www/svv.ch/behat-local.yml
            - sed -i 's/svv.loc/localhost/g' /var/www/svv.ch/behat-local.yml

            # DEBUG INFOS
            # - cat /var/www/svv.ch/docroot/behat-local.yml
            # - cat /var/www/svv.ch/docroot/behat.yml
            # - wget http://localhost:4444/wd/hub

            - cd /var/www/svv.ch
            - vendor/behat/behat/bin/behat -v
