stages:
    - build
    - test
    - merge
    - deploy    



before_script:
  - export NOW=$(date +"%Y%m%d-%H%M%S")
  - export TAR_NAME_POSTFIX=$NOW
  - echo $DOCKER_PRIV_KEY >> /builds/amichelo/chaos_bundle_compilation/deploy.key
  - mkdir -p ~/.ssh
  - echo "$CI_JOB_NAME Working directory:$PWD tag $CI_COMMIT_REF_NAME"
  - export ARCH=`uname -m`
  - eval export `cat /etc/*-release|grep -e "^VERSION_ID="|tr -d "\" "`
  - eval export `cat /etc/*-release|grep -e "^ID="|tr -d "\" "`
  - export REV_POSTFIX=$CI_COMMIT_REF_NAME-$ID-$VERSION_ID-$ARCH
  - export DISTRIB_NAME=chaos-distrib-$REV_POSTFIX
  - export DISTRIB_PREFIX=/usr/local/chaos/chaos-distrib
  - export INSTALL_DIR=$DISTRIB_PREFIX
  - export PATH=/usr/local/chaos/qt-56/bin:$PATH
  - mkdir -p $INSTALL_DIR
  - echo "Running on $ID $VERSION_ID $ARCH (distrib name '$DISTRIB_NAME')"
  - git config --global user.email andrea.michelotti@lnf.infn.it
  - git config --global user.name amichelo
  - git config --global color.ui true

ubuntu1604_x86_64:
  stage: build
  image: baltig.infn.it:4567/amichelo/chaos_bundle_compilation:1604
  script:
    - repo init -u git@baltig.infn.it:chaos-lnf-control/chaos_repo_bundle.git -b $CI_COMMIT_REF_NAME
    - repo sync
    - cmake -DCHAOS_WAN=ON -DCHAOS_CCS=ON -DCERN_ROOT=$INSTALL_DIR -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR . |& tee $DISTRIB_PREFIX/bundle.configuration.log
    - make  -j 5 install |& tee $DISTRIB_PREFIX/bundle.compilation.log
    - cd chaosframework/ccs;qmake;echo "* compiling CCS";make -j 5 >& $DISTRIB_PREFIX/ccs.compilation.log ;cp ccs $INSTALL_DIR/bin;cd ../../
    - scripts/strip_symbols.sh $INSTALL_DIR
    - tar cfz $DISTRIB_NAME.tar.gz -C / $DISTRIB_PREFIX
  artifacts:
        name: "$DISTRIB_NAME.tar.gz"
        paths:
           - ./*.tar.gz
        expire_in: 1 day

ubuntu1604_test_x86_64:
  stage: test
#  tags:
#    - chaos  
  image: baltig.infn.it:4567/amichelo/chaos_bundle_compilation:1604
  script:
    - /tmp/chaos_start.sh
    - tar xfz $DISTRIB_NAME.tar.gz -C /
    - source $DISTRIB_PREFIX/chaos_env.sh
    - $DISTRIB_PREFIX/tools/chaos_test.sh
    - git clone ssh://git@baltig.infn.it/chaos-lnf-control/chaos-common-test.git
    - cd chaos-common-test;git checkout  $CI_COMMIT_REF_NAME
    - cd js
    - ./node_modules/mocha/bin/mocha

  after_script:
    - tar cvfz test-log.tar.gz -C /usr/local/chaos/chaos-distrib log
    - scp test-log.tar.gz chaosweb@opensource.lnf.infn.it:/var/www/html/binary/chaos/$ARCH/
  artifacts:
        name: "Logs-$CI_JOB_NAME"
        when: always
        paths:
          - ./test-log.tar.gz
        expire_in: 7 day

ubuntu1404_x86_64:
  stage: build
  image: baltig.infn.it:4567/amichelo/chaos_bundle_compilation:1404
  script:
    - repo init -u git@baltig.infn.it:chaos-lnf-control/chaos_repo_bundle.git -b $CI_COMMIT_REF_NAME
    - repo sync
    - cmake -DCHAOS_WAN=ON -DCHAOS_CCS=ON -DCERN_ROOT=$INSTALL_DIR -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR . |& tee $DISTRIB_PREFIX/bundle.configuration.log
    - make  -j 5 install |& tee $DISTRIB_PREFIX/bundle.compilation.log
    - cd chaosframework/ccs;qmake;echo "* compiling CCS";make -j 5 >& $DISTRIB_PREFIX/ccs.compilation.log;cp ccs $INSTALL_DIR/bin;cd ../../
    - scripts/strip_symbols.sh $INSTALL_DIR
    - tar cfz $DISTRIB_NAME.tar.gz -C / $DISTRIB_PREFIX    
  artifacts:
        name: "$DISTRIB_NAME.tar.gz"
        paths:
          - ./*.tar.gz
        expire_in: 1 day


ubuntu1404_test_x86_64:
  stage: test
#  tags:
#    - chaos  
  image: baltig.infn.it:4567/amichelo/chaos_bundle_compilation:1404
  script:
    - /tmp/chaos_start.sh
    - tar xfz $DISTRIB_NAME.tar.gz -C /
    - source $DISTRIB_PREFIX/chaos_env.sh
    - $DISTRIB_PREFIX/tools/chaos_test.sh
    - git clone ssh://git@baltig.infn.it/chaos-lnf-control/chaos-common-test.git
    - cd chaos-common-test;git checkout  $CI_COMMIT_REF_NAME
    - cd js
    - ./node_modules/mocha/bin/mocha
  after_script:
    - tar cvfz test-log.tar.gz -C /usr/local/chaos/chaos-distrib log
    - scp test-log.tar.gz chaosweb@opensource.lnf.infn.it:/var/www/html/binary/chaos/$ARCH/
  artifacts:
        name: "Logs-$CI_JOB_NAME"
        when: always
        paths:
          - test-log.tar.gz
        expire_in: 7 day

build_arm_linux26:
  stage: build
  image: baltig.infn.it:4567/amichelo/chaos_bundle_compilation:1604 
  script:
    - export PATH=/usr/local/chaos/i686-nptl-linux-gnu/bin/:$PATH
    - export DISTRIB_NAME=chaos-distrib-$CI_COMMIT_REF_NAME-$CI_JOB_NAME
    - export INSTALL_DIR=/builds/amichelo/chaos_bundle_compilation/distrib/$DISTRIB_NAME
    - mkdir -p $INSTALL_DIR
    - repo init -u git@baltig.infn.it:chaos-lnf-control/chaos_repo_bundle.git -b $CI_COMMIT_REF_NAME
    - repo sync
    - export PATH=/usr/local/chaos/gcc-arm-infn-linux26/bin/:$PATH
    - cmake . -DCHAOS_STATIC=ON -DCHAOS_TARGET=arm-linux-2.6 -DCHAOS_CDS=OFF -DCHAOS_MDS=OFF -DCHAOS_WAN=OFF -DCHAOS_EXAMPLES=OFF -DCHAOS_ENABLE_C11=OFF -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR
    - make  -j 4 install |& tee $INSTALL_DIR/bundle.compilation.log
    - arm-infn-linux-gnueabi-strip `find $INSTALL_DIR/bin/ -type f`
    - arm-infn-linux-gnueabi-strip `find $INSTALL_DIR/lib/ -name "*.a" -type f`
    - tar cfz $DISTRIB_NAME.tar.gz -C $INSTALL_DIR/..  $DISTRIB_NAME
  artifacts:
        name: "$DISTRIB_NAME.tar.gz"
        paths:
           - ./*.tar.gz
        expire_in: 1 day

build_i686_static_linux26:
  stage: build
  image: baltig.infn.it:4567/amichelo/chaos_bundle_compilation:1604 
  script:
    - repo init -u git@baltig.infn.it:chaos-lnf-control/chaos_repo_bundle.git -b $CI_COMMIT_REF_NAME
    - repo sync
    - export PATH=/usr/local/chaos/i686-nptl-linux-gnu/bin/:$PATH
    - export DISTRIB_NAME=chaos-distrib-$CI_COMMIT_REF_NAME-$CI_JOB_NAME
    - export INSTALL_DIR=/builds/amichelo/chaos_bundle_compilation/distrib/$DISTRIB_NAME
    - mkdir -p $INSTALL_DIR
    - cmake . -DCHAOS_STATIC=ON -DCHAOS_TARGET=i686-linux26 -DCHAOS_CDS=OFF -DCHAOS_MDS=OFF -DCHAOS_WAN=OFF -DCHAOS_EXAMPLES=OFF -DCHAOS_ENABLE_C11=OFF -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR |& tee $INSTALL_DIR/bundle.configuration.log
    - make  -j 4 install |& tee $INSTALL_DIR/bundle.compilation.log
    - i686-nptl-linux-gnu-strip `find $INSTALL_DIR/bin/ -type f`
    - i686-nptl-linux-gnu-strip `find $INSTALL_DIR/lib/ -name "*.a" -type f`
    - tar cfz $DISTRIB_NAME.tar.gz -C $INSTALL_DIR/..  $DISTRIB_NAME
  artifacts:
        name: "$DISTRIB_NAME.tar.gz"
        paths:
           - ./*.tar.gz
        expire_in: 1 day

build_i686_dynamic_linux26:
  stage: build
  image: baltig.infn.it:4567/amichelo/chaos_bundle_compilation:1604 
  script:
    - export DISTRIB_NAME=chaos-distrib-$CI_COMMIT_REF_NAME-$CI_JOB_NAME
    - export INSTALL_DIR=/builds/amichelo/chaos_bundle_compilation/distrib/$DISTRIB_NAME
    - mkdir -p $INSTALL_DIR
    - repo init -u git@baltig.infn.it:chaos-lnf-control/chaos_repo_bundle.git -b $CI_COMMIT_REF_NAME
    - repo sync
    - export PATH=/usr/local/chaos/i686-nptl-linux-gnu/bin/:$PATH
    - cmake . -DCHAOS_TARGET=i686-linux26 -DCHAOS_CDS=OFF -DCHAOS_MDS=OFF -DCHAOS_WAN=OFF -DCHAOS_EXAMPLES=OFF -DCHAOS_ENABLE_C11=OFF -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR |& tee $INSTALL_DIR/bundle.configuration.log
    - make  -j 4 install|& tee $INSTALL_DIR/bundle.compilation.log
    - tar cfz $DISTRIB_NAME.tar.gz -C $INSTALL_DIR/..  $DISTRIB_NAME
  artifacts:
        name: "$DISTRIB_NAME.tar.gz"
        paths:
           - ./*.tar.gz
        expire_in: 1 day


check_merge_request:
  stage: merge
  image: baltig.infn.it:4567/amichelo/chaos_bundle_compilation:1404
  script:
    - scripts/accept_merge_request.sh $CI_COMMIT_REF_NAME


deploy_stage_ubuntu1404:
  tags:
    - chaos
  stage: deploy
  image: baltig.infn.it:4567/amichelo/chaos_bundle_compilation:1404
  script:
    - scp $DISTRIB_NAME.tar.gz chaosweb@opensource.lnf.infn.it:/var/www/html/binary/chaos/$ARCH/
    - if [ $CI_COMMIT_REF_NAME == "development" ];then echo "deploying in LNF development infrastructure"; tar xfz $DISTRIB_NAME.tar.gz -C /;cd $DISTRIB_PREFIX;source chaos_env.sh;./tools/chaos_deploy.sh tools/config/lnf/test/chaos-infrastructure.txt;fi
    
deploy_stage_ubuntu1604:
  tags:
    - chaos
  stage: deploy
  image: baltig.infn.it:4567/amichelo/chaos_bundle_compilation:1604
  script:
    - scp $DISTRIB_NAME.tar.gz chaosweb@opensource.lnf.infn.it:/var/www/html/binary/chaos/$ARCH/


deploy_stage_i686:
  tags:
    - chaos
  stage: deploy
  image: baltig.infn.it:4567/amichelo/chaos_bundle_compilation:1404
  script:
    - export DISTRIB_NAME=chaos-distrib-$CI_COMMIT_REF_NAME-build_i686_static_linux26
    - scp $DISTRIB_NAME.tar.gz chaosweb@opensource.lnf.infn.it:/var/www/html/binary/chaos/i686/
    - export DISTRIB_NAME=chaos-distrib-$CI_COMMIT_REF_NAME-build_i686_dynamic_linux26
    - scp $DISTRIB_NAME.tar.gz chaosweb@opensource.lnf.infn.it:/var/www/html/binary/chaos/i686/

deploy_stage_arm:
  tags:
    - chaos
  stage: deploy
  image: baltig.infn.it:4567/amichelo/chaos_bundle_compilation:1404
  script:
    - export DISTRIB_NAME=chaos-distrib-$CI_COMMIT_REF_NAME-build_arm_linux26
    - scp $DISTRIB_NAME.tar.gz chaosweb@opensource.lnf.infn.it:/var/www/html/binary/chaos/arm/




