lapentafd | 6360bbb | 2024-04-05 09:49:44 +0100 | [diff] [blame] | 1 | # ========================LICENSE_START================================= |
| 2 | # O-RAN-SC |
| 3 | # |
| 4 | # Copyright (C) 2024: OpenInfra Foundation Europe |
| 5 | # ======================================================================== |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # ============LICENSE_END================================================= |
| 18 | |
| 19 | #!/bin/bash |
| 20 | |
| 21 | checkJava() { |
| 22 | if ! command -v java >/dev/null 2>&1; then |
| 23 | echo "Java is not installed. Please install Java." |
| 24 | echo "Suggested fix for ubuntu:" |
| 25 | echo "sudo apt install default-jdk" |
| 26 | exit 1 |
| 27 | else |
| 28 | echo "Java is installed." |
| 29 | fi |
| 30 | } |
| 31 | |
| 32 | checkMaven() { |
| 33 | if mvn -v >/dev/null 2>&1; then |
| 34 | echo "Maven is installed." |
| 35 | else |
| 36 | echo "Maven is not installed. Please install Maven." |
| 37 | echo "Suggested fix for ubuntu:" |
| 38 | echo "sudo apt install maven" |
| 39 | exit 1 |
| 40 | fi |
| 41 | } |
| 42 | |
| 43 | checkDocker() { |
| 44 | if ! docker -v > /dev/null 2>&1; then |
| 45 | echo "Docker is not installed. Please install Docker." |
| 46 | echo "Suggested fix for ubuntu:" |
| 47 | echo "sudo apt-get update" |
| 48 | echo "sudo apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release" |
| 49 | echo "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg" |
| 50 | echo "echo \"deb [arch=\$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \$(lsb_release -cs) stable\" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null" |
| 51 | echo "sudo apt-get update" |
| 52 | echo "sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin" |
| 53 | echo "sudo usermod -aG docker \$USER" |
| 54 | echo "newgrp docker" |
| 55 | exit 1 |
| 56 | else |
| 57 | echo "Docker is installed." |
| 58 | fi |
| 59 | } |
| 60 | |
| 61 | checkDockerCompose() { |
| 62 | if ! docker-compose -v > /dev/null 2>&1; then |
| 63 | echo "docker-compose is not installed. Please install docker-compose" |
| 64 | echo "Suggested fix for ubuntu:" |
| 65 | echo "sudo apt-get install docker-compose-plugin" |
| 66 | exit 1 |
| 67 | else |
| 68 | echo "docker-compose is installed." |
| 69 | fi |
| 70 | } |
| 71 | |
lapentafd | 84525a0 | 2024-04-26 13:11:58 +0100 | [diff] [blame] | 72 | # Function to wait for a Docker container to be running and log a specific string with a maximum timeout of 20 minutes |
lapentafd | 6360bbb | 2024-04-05 09:49:44 +0100 | [diff] [blame] | 73 | wait_for_container() { |
| 74 | local container_name="$1" |
| 75 | local log_string="$2" |
lapentafd | 84525a0 | 2024-04-26 13:11:58 +0100 | [diff] [blame] | 76 | local timeout=1200 # Timeout set to 20 minutes (20 minutes * 60 seconds) |
| 77 | |
| 78 | local start_time=$(date +%s) |
| 79 | local end_time=$((start_time + timeout)) |
lapentafd | 6360bbb | 2024-04-05 09:49:44 +0100 | [diff] [blame] | 80 | |
| 81 | while ! docker inspect "$container_name" &>/dev/null; do |
| 82 | echo "Waiting for container '$container_name' to be created..." |
| 83 | sleep 5 |
lapentafd | 84525a0 | 2024-04-26 13:11:58 +0100 | [diff] [blame] | 84 | if [ "$(date +%s)" -ge "$end_time" ]; then |
| 85 | echo "Timeout: Container creation exceeded 20 minutes." |
| 86 | exit 1 |
| 87 | fi |
lapentafd | 6360bbb | 2024-04-05 09:49:44 +0100 | [diff] [blame] | 88 | done |
| 89 | |
| 90 | while [ "$(docker inspect -f '{{.State.Status}}' "$container_name")" != "running" ]; do |
| 91 | echo "Waiting for container '$container_name' to be running..." |
| 92 | sleep 5 |
lapentafd | 84525a0 | 2024-04-26 13:11:58 +0100 | [diff] [blame] | 93 | if [ "$(date +%s)" -ge "$end_time" ]; then |
| 94 | echo "Timeout: Container start exceeded 20 minutes." |
| 95 | exit 1 |
| 96 | fi |
lapentafd | 6360bbb | 2024-04-05 09:49:44 +0100 | [diff] [blame] | 97 | done |
| 98 | |
| 99 | # Check container logs for the specified string |
| 100 | while ! docker logs "$container_name" 2>&1 | grep "$log_string"; do |
| 101 | echo "Waiting for '$log_string' in container logs of '$container_name'..." |
| 102 | sleep 5 |
lapentafd | 84525a0 | 2024-04-26 13:11:58 +0100 | [diff] [blame] | 103 | if [ "$(date +%s)" -ge "$end_time" ]; then |
| 104 | echo "Timeout: Log string not found within 20 minutes." |
| 105 | exit 1 |
| 106 | fi |
lapentafd | 6360bbb | 2024-04-05 09:49:44 +0100 | [diff] [blame] | 107 | done |
| 108 | } |
| 109 | |
lapentafd | 84525a0 | 2024-04-26 13:11:58 +0100 | [diff] [blame] | 110 | |
lapentafd | 6360bbb | 2024-04-05 09:49:44 +0100 | [diff] [blame] | 111 | space() { |
| 112 | echo "" |
| 113 | echo "++++++++++++++++++++++++++++++++++++++++++++++++++++" |
| 114 | echo "" |
| 115 | } |