apiVersion: v1
kind: ConfigMap
metadata:
  name: wait-script
data:
  wait-to-start.sh: |-
    #!/bin/bash
    
    # Zuul needs to be able to connect to the remote systems in order to
    # start.
    apk add mariadb-client curl git jq

    wait_for_mysql() {
    echo "Wait for mysql to start"
        for i in $(seq 1 120); do
            if nslookup mariadb; then
                echo "Service resolved"
                break
            else
                echo "Could not reslove service"
                sleep 1
            fi
        if [ $i -eq 120 ]; then
          exit 1
        fi
        done

        for i in $(seq 1 120); do
            if nc -z -w5 mariadb 3306; then
                echo "Socket is opened"
                break
            else
                echo "Socket closed"
                sleep 1
            fi
        if [ $i -eq 120 ]; then
          exit 1
        fi
        done

        for i in $(seq 1 120); do
            if mysql -h mariadb -u zuul -psecret -e exit; then
                echo "Connected to mysql"
                return 0
            else
                echo "Colud not connect to mysql. Host, username or password are wrong."
                sleep 1
            fi
        done
        return 1
    }
    
    wait_for_mysql
  wait-for-scheduler-start.sh: |-
    wait_for_scheduler() {
    echo "Wait for mysql to start"
        for i in $(seq 1 120); do
            if nc -z -w5 scheduler 4730; then
                echo "Socket is opened"
                return 0
            else
                echo "Socket closed"
                sleep 1
            fi
        done
        return 1
    }
   
    wait_for_scheduler 
  project.config: |-
    [project]
    	description = Access inherited by all other projects.
    [receive]
    	requireContributorAgreement = false
    	requireSignedOffBy = false
    	requireChangeId = true
    	enableSignedPush = false
    [submit]
    	mergeContent = true
    [capability]
    	administrateServer = group Administrators
    	priority = batch group Non-Interactive Users
    	streamEvents = group Non-Interactive Users
    [access "refs/*"]
    	read = group Administrators
    	read = group Anonymous Users
    [access "refs/for/*"]
    	addPatchSet = group Registered Users
    	forgeAuthor = group Registered Users
    	forgeCommitter = group Registered Users
    [access "refs/for/refs/*"]
    	push = group Registered Users
    	pushMerge = group Registered Users
    [access "refs/heads/*"]
    	create = group Administrators
    	create = group Project Owners
    	forgeAuthor = group Registered Users
    	forgeCommitter = group Administrators
    	forgeCommitter = group Project Owners
    	forgeCommitter = group Registered Users
    	push = +force group Administrators
    	push = group Project Owners
    	label-Code-Review = -2..+2 group Registered Users
    	label-Verified = -2..+2 group Administrators
    	label-Verified = -2..+2 group Non-Interactive Users
    	label-Workflow = -1..+1 group Registered Users
    	submit = group Administrators
    	submit = group Project Owners
    	submit = group Non-Interactive Users
    	editTopicName = +force group Administrators
    	editTopicName = +force group Project Owners
    [access "refs/meta/config"]
    	exclusiveGroupPermissions = read
    	read = group Administrators
    	read = group Project Owners
    	create = group Administrators
    	create = group Project Owners
    	push = group Administrators
    	push = group Project Owners
    	label-Code-Review = -2..+2 group Administrators
    	label-Code-Review = -2..+2 group Project Owners
    	submit = group Administrators
    	submit = group Project Owners
    [access "refs/tags/*"]
    	create = group Administrators
    	create = group Project Owners
    	createTag = group Administrators
    	createTag = group Project Owners
    	createSignedTag = group Administrators
    	createSignedTag = group Project Owners
    [label "Code-Review"]
    	function = MaxWithBlock
    	defaultValue = 0
    	copyMinScore = true
    	copyAllScoresOnTrivialRebase = true
    	value = -2 This shall not be merged
    	value = -1 I would prefer this is not merged as is
    	value =  0 No score
    	value = +1 Looks good to me, but someone else must approve
    	value = +2 Looks good to me, approved
    [label "Verified"]
    	function = MaxWithBlock
    	value = -2 Fails
    	value = -1 Does not seem to work
    	value =  0 No score
    	value = +1 Works for me
    	value = +2 Verified
    	defaultValue = 0
    [label "Workflow"]
    	function = MaxWithBlock
    	value = -1 Work in progress
    	value =  0 Ready for reviews
    	value = +1 Approved
    	defaultValue = 0
