Merge "ICS sample producer and consumer"
[nonrtric.git] / sample-services / ics-producer-consumer / utils.sh
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
72 # Function to wait for a Docker container to be running and log a specific string
73 wait_for_container() {
74     local container_name="$1"
75     local log_string="$2"
76
77     while ! docker inspect "$container_name" &>/dev/null; do
78         echo "Waiting for container '$container_name' to be created..."
79         sleep 5
80     done
81
82     while [ "$(docker inspect -f '{{.State.Status}}' "$container_name")" != "running" ]; do
83         echo "Waiting for container '$container_name' to be running..."
84         sleep 5
85     done
86
87     # Check container logs for the specified string
88     while ! docker logs "$container_name" 2>&1 | grep "$log_string"; do
89         echo "Waiting for '$log_string' in container logs of '$container_name'..."
90         sleep 5
91     done
92 }
93
94 space() {
95     echo ""
96     echo "++++++++++++++++++++++++++++++++++++++++++++++++++++"
97     echo ""
98 }