type: install
id: jitsi
version: 1.7
name: Jitsi Video Conferencing
baseUrl: https://raw.githubusercontent.com/jelastic-jps/jitsi/master
logo: /images/logo_small.svg?sanitize=true

description: 
  text: Get your security and privacy in communication with Jitsi Video Conferencing Server. Jitsi is a free cross-platform software for instant text messaging (IM), voice messaging (Voice over IP, VoIP) and video chat.
  short: Host your own Jitsi server

targetRegions:
  type: vz7

requiredFeatures: extip

categories:
- apps/dev-and-admin-tools
- apps/collaboration
  
globals: 
  user: admin 
  password: ${fn.password}

nodes:
  nodeType: dockerengine
  nodeGroup: cp
  cloudlets: 48
  extip: true
  displayName: Jitsi Server
  addons: change-domain

settings:
  domain: 
    fields: 
      - name: displayfield
        type: displayfield
        hideLabel: true
        markup: Make sure the the new domain is bound already to the environment via CNAME or via A Record 
      - name: domain
        hideLabel: true
        caption: Domain
        type: string
        vtype: extdomain


onInstall:
  - enableAudio
  - setSndOwnership
  - setup

actions:
  enableAudio:
    script: |
      var params = {envName:"${env.envName}"}, appid = "ext", script = "EnableAudio",
          resp = jelastic.dev.scripting.Eval(appid, session, script, params);
      return resp.response ? resp.response:resp;      

  setup:      
    - cmd[cp]: |-
        echo '/root' >> /etc/jelastic/redeploy.conf
        git clone https://github.com/jelastic/docker-jitsi-meet && cd docker-jitsi-meet;
        cp env.example .env;

        #check if region is in public suffix list
        DOMAIN=${env.domain}; REGION=${DOMAIN#*.}
        USE_STAGING=1
        if wget -qO- https://github.com/publicsuffix/list/raw/master/public_suffix_list.dat | grep -q "$REGION"; then USE_STAGING=0; fi

        #Adjusting configs
        sed -i.bak \
          -e "s|HTTP_PORT=.*|HTTP_PORT=80|g" \
          -e "s|HTTPS_PORT=.*|HTTPS_PORT=443|g" \
          -e "s|#PUBLIC_URL=.*|PUBLIC_URL=https://${env.domain}|g" \
          -e "s|#ENABLE_LETSENCRYPT=.*|ENABLE_LETSENCRYPT=1|g" \
          -e "s|#LETSENCRYPT_DOMAIN=.*|LETSENCRYPT_DOMAIN=${env.domain}|g" \
          -e "s|#LETSENCRYPT_EMAIL=.*|LETSENCRYPT_EMAIL=${user.email}|g" \
          -e "s|#LETSENCRYPT_USE_STAGING=.*|LETSENCRYPT_USE_STAGING=${USE_STAGING}|g" \
          -e "s|#ENABLE_GUESTS=.*|ENABLE_GUESTS=1|g" \
          -e "s|#AUTH_TYPE=.*|AUTH_TYPE=internal|g" \
          -e "s|#ENABLE_HTTP_REDIRECT=.*|ENABLE_HTTP_REDIRECT=1|g" \
          -e "s|#ENABLE_RECORDING=.*|ENABLE_RECORDING=1|g" \
          -e "s|#XMPP_CROSS_DOMAIN=.*|XMPP_CROSS_DOMAIN=true|g" \
          .env

        [ "${settings.loadtest:}" ] && { 
          sed -i -e "s|#_JAVA_OPTIONS|_JAVA_OPTIONS|g" .env
        } || {
          #enable AUTH if it's not a load test
          sed -i -e "s|#ENABLE_AUTH=.*|ENABLE_AUTH=1|g" .env
        }

        ./gen-passwords.sh;

        mkdir -p ~/.jitsi-meet-cfg/{web/letsencrypt,transcripts,prosody/config,prosody/prosody-plugins-custom,jicofo,jvb,jigasi,jibri};
        sed -i '/:\/dev\/shm/a \            - ${CONFIG}\/jibri\/\.asoundrc:\/home\/jibri\/\.asoundrc' ~/docker-jitsi-meet/jibri.yml

        printf 'pcm.amix {
          type dmix
          ipc_key 219345
          slave.pcm "hw:Loopback,0,0"
        }

        pcm.asnoop {
          type dsnoop
          ipc_key 219346
          slave.pcm "hw:Loopback,0,1"
        }

        pcm.aduplex {
          type asym
          playback.pcm "amix"
          capture.pcm "asnoop"
        }
        pcm.ploop {
          type plug
          slave.pcm "hw:Loopback,1,1"
        }

        pcm.cloop {
          type dsnoop
          ipc_key 219348
          slave.pcm "hw:Loopback,1,0"
        }

        pcm.bmix {
          type dmix
          ipc_key 219347
          slave.pcm "hw:Loopback_1,0,0"
        }

        pcm.bsnoop {
          type dsnoop
          ipc_key 219348
          slave.pcm "hw:Loopback,1,0"
        }

        pcm.bduplex {
          type asym
          playback.pcm "bmix"
          capture.pcm "bsnoop"
        }

        pcm.pjsua {
          type plug
          slave.pcm "bduplex"
        }

        pcm.!default {
          type plug
          slave.pcm "aduplex"
        }' > ~/.jitsi-meet-cfg/jibri/.asoundrc

        docker-compose -f docker-compose.yml -f jibri.yml up -d --quiet-pull

        #Checking of container is up and running 
        until [ "`/usr/bin/docker inspect -f {{.State.Running}} docker-jitsi-meet-prosody-1`"=="true" ]; do
          sleep 1
          echo '.'
        done;
        until [[ -f /root/.jitsi-meet-cfg/prosody/config/prosody.cfg.lua ]] ; do
          sleep 1
          echo '.'
        done
        #Adding admin user 
        docker-compose exec -T prosody prosodyctl --config /config/prosody.cfg.lua register "${globals.user}" meet.jitsi "${globals.password}"
        [ "${settings.loadtest:}" ] && {
          yum install -y java-1.8.0-openjdk-devel
          wget https://github.com/jelastic-jps/jitsi/raw/jibri/test/GCStats.jar
          java -jar GCStats.jar
        }
                
        echo OK
    - env.file.AddFavorite:
        nodeGroup: cp
        path: /root/.jitsi-meet-cfg/jibri/recordings
        keyword: RECORDINGS
        isDir: true
    - env.file.AddFavorite:
        nodeGroup: cp
        path: /root
        keyword: home
        isDir: true
        
  setSndOwnership:    
     - cmd[cp]: |-
        CMD="chown 999 -R /dev/snd"
        echo $CMD >> /etc/rc.d/rc.local
        $CMD
        
addons:
  - id: change-domain    
    name: Domain Configuration    
    description: You can change your current domain and issue valid Let's Encrypt certificates.
    permanent: true
    globals:
      TOO_MANY_CERTS: too many certificates
    buttons:    
      - confirmText: Are you sure you want to proceed?
        loadingText: Changing domain...
        action: changeDomain
        caption: Change
        successText: The domain has been updated successfully!
        settings: domain
        title: Please specify the new domain name 
      - confirmText: Are you sure you want to update SSL certificates?
        action: updateSSL
        caption: Update SSL
        successText: The SSL certificates has been updated successfully!

    actions:
      changeDomain:
        - cmd[cp]: |-
            rm -f ~/.jitsi-meet-cfg/web/nginx/ssl.conf ~/.jitsi-meet-cfg/web/config.js            
            cd docker-jitsi-meet
            
            SSL_DOMAIN="${settings.domain}"
            DOMAIN="${env.domain}"; REGION=${DOMAIN#*.}
            USE_STAGING=0
            if [[ "$SSL_DOMAIN" == *"$DOMAIN"* ]]; then   
              USE_STAGING=1
              #check if region is in public suffix list              
              if wget -qO- https://github.com/publicsuffix/list/raw/master/public_suffix_list.dat | grep -q "$REGION"; then USE_STAGING=0; fi
            fi
                        
            sed -i.bak \
              -e "s|LETSENCRYPT_DOMAIN=.*|LETSENCRYPT_DOMAIN=${settings.domain}|g" \
              -e "s|LETSENCRYPT_EMAIL=.*|LETSENCRYPT_EMAIL=${user.email}|g" \
              -e "s|LETSENCRYPT_USE_STAGING=.*|LETSENCRYPT_USE_STAGING=${USE_STAGING}|g" \
              -e "s|PUBLIC_URL=.*|PUBLIC_URL=https://${settings.domain}|g" \
              .env
            docker-compose -f docker-compose.yml -f jibri.yml up -d --force-recreate
            
      updateSSL:
          - cmd [cp]: |-
              appid=$(docker ps  | grep 'jitsi/web' | awk '{print $1}')
              resp=$(docker exec ${appid} /config/acme.sh/acme.sh --cron --force --home '/config/acme.sh' 2>&1)
              echo $resp | grep -q 'Cert success' && exit 0 || {
                echo $resp | grep -q '${globals.TOO_MANY_CERTS}' && { echo '${globals.TOO_MANY_CERTS}' && exit 0; }
              }
            user: root
          - script: |-
              if ("${response.out}" == "${globals.TOO_MANY_CERTS}") {
                return {
                  result: 22,
                  type: "warning",
                  message: "Too many certificates already issued for this exact set of domains. See [https://letsencrypt.org/docs/rate-limits/](https://letsencrypt.org/docs/rate-limits/)"
                }
              }
              
              return {
                result: 0
              }
    
success: | 
  **Jitsi Server**: [https://${env.domain}/](https://${env.domain}/)  
  **User**: ${globals.user}  
  **Password**: ${globals.password}    

  Useful links:
  * [Secure, Simple and Scalable Video Conferencing with Jitsi](https://jelastic.com/blog/jitsi-video-conferencing/)