---
apiVersion: batch/v1
kind: Job
metadata:
  name: utils
spec:
  backoffLimit: 1
  template:
    metadata:
      name: utils
    spec:
      restartPolicy: "Never"
      volumes:
      - name: sharedvolume
        persistentVolumeClaim:
          claimName: shared-pvc
      - name: dockersocket
        hostPath:
          path: /var/run/docker.sock
      containers:
      - name: cryptogen
        image: hyperledger/fabric-tools:1.4.4
        imagePullPolicy: Always
        command: ["sh", "-c", 
        "echo 'Cryptogen Starts'; 
        ls -l /shared/artifacts; 
        while [ ! -d /shared/artifacts ]; 
        do 
          echo Waiting for configFiles; 
          sleep 1; done; 
          cryptogen generate --config /shared/artifacts/crypto-config.yaml 
            && cp -r crypto-config /shared/ 
            && for file in $(find /shared/ -iname *_sk);
        do 
          echo $file;
          dir=$(dirname $file); 
          echo ${dir}; mv ${dir}/*_sk ${dir}/key.pem;
        done 
        && find /shared -type d | xargs chmod a+rx 
        && find /shared -type f | xargs chmod a+r 
        && touch /shared/status_cryptogen_complete;
        "]
        volumeMounts:
        - mountPath: /shared
          name: sharedvolume
      - name: configtxgen
        image: hyperledger/fabric-tools:1.4.4
        imagePullPolicy: Always
        command: ["sh", "-c", 
        "echo 'Configtxgen Starts'; 
        ls -l /shared; 
        while [ ! -f /shared/status_cryptogen_complete ]; 
        do 
          echo Waiting for cryptogen; 
          sleep 1; 
        done; 
        mkdir -p ./shared/crypto-config/peerOrganizations/idemix.coj.go.th/msp; 
        cp ./shared/crypto-config/IssuerPublicKey ./shared/crypto-config/peerOrganizations/idemix.coj.go.th/msp/IssuerPublicKey; 
        cp ./shared/crypto-config/RevocationPublicKey ./shared/crypto-config/peerOrganizations/idemix.coj.go.th/msp/RevocationPublicKey;
        sleep 1;
        cp /shared/artifacts/configtx.yaml /shared/; 
        cd /shared/; 
        export FABRIC_CFG_PATH=$PWD; 
        configtxgen -profile SoloOrgsEtcdRaftOrdererGenesis -outputBlock genesis.block 
        && find /shared -type d | xargs chmod a+rx 
        && find /shared -type f | xargs chmod a+r 
        && touch /shared/status_configtxgen_complete 
        && rm /shared/status_cryptogen_complete;"]
        volumeMounts:
        - mountPath: /shared
          name: sharedvolume
      tolerations:
      - key: dedicated
        operator: Exists
        effect: NoSchedule
