Added support of new image_url field
[src/xds/xds-server.git] / scripts / sdks / agl / get-sdk-info
1 #!/bin/bash
2  ###########################################################################
3 # Copyright 2018 IoT.bzh
4 #
5 # author: Sebastien Douheret <sebastien@iot.bzh>
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 ###########################################################################
19
20 . $(dirname "$0")/_env-init.sh
21
22 usage() {
23     echo "Usage: $(basename $0) [-h|--help] [-f|--file <sdk-filename>] [-u|--url <https_url>] [--md5 <string>]"
24         exit 1
25 }
26
27 SDK_FILE=""
28 MD5VAL=""
29 URL=""
30 UUID=""
31
32 while [ $# -ne 0 ]; do
33     case $1 in
34         -f|--file)
35             shift
36             SDK_FILE=$1
37             ;;
38         -h|--help)
39             usage
40             ;;
41         --md5)
42             shift
43             MD5VAL=$1
44             ;;
45         -u|--url)
46             shift
47             URL=$1
48             ;;
49         --uuid)
50             shift
51             UUID=$1
52             ;;
53         *)
54             echo "Invalid argument: $1"
55             usage
56             ;;
57     esac
58     shift
59 done
60
61 if [ "${SDK_FILE}" != "" ]; then
62
63     if [ ! -f "${SDK_FILE}" ]; then
64         echo "sdk file doesn't exist"
65         exit 1
66     fi
67
68     # Criteria to consider that SDK is a valid AGL sdk:
69     #  - shell and binary file type
70     #  - "$MARKER:$" string found
71     #  - md5sum match MD5VAL if MD5VAL is set
72
73     if ! file "${SDK_FILE}" |grep shell |grep -q binary ; then
74         echo "Not expected file type"
75         exit 2
76     fi
77
78     if ! grep -aq -m1 "^MARKER:$" "${SDK_FILE}"; then
79         echo "marker not found"
80         exit 4
81     fi
82
83     if [ "${MD5VAL}" != "" ]; then
84         if ! echo "${MD5VAL} ${SDK_FILE}" | md5sum --status -c -; then
85             echo "md5sum dit not match"
86             exit 5
87         fi
88     fi
89
90     filename=$(basename "${SDK_FILE}")
91     sdkUrl=file://${SDK_FILE}
92     imageUrl=""
93     sdkDate=$(stat -c %y "${SDK_FILE}")
94     sdkSize=$(/bin/ls -sh "${SDK_FILE}" |cut -d' ' -f1)
95
96 elif [ "${URL}" != "" ]; then
97
98     filename=$(basename "${URL}")
99     sdkUrl=${URL}
100     imageUrl=""
101     sdkDate=""
102     sdkSize=""
103
104 else
105     echo "--file or --url option must be set"
106     exit 1
107 fi
108
109 # assume that sdk name follow this format :
110 #  _PROFILE_-_COMPILER_ARCH_-_TARGET_-crosssdk-_ARCH_-toolchain-_VERSION_.sh
111 # for example:
112 #  poky-agl-glibc-x86_64-agl-demo-platform-crosssdk-corei7-64-toolchain-4.0.1.sh
113
114
115 if [[ "${filename}" != *"crosssdk"* ]]; then
116     echo "malformed sdk file name"
117     exit 6
118 fi
119
120 profile=$(echo "${filename}" | sed -r 's/(.*)-glibc.*/\1/')
121 version=$(echo "${filename}" | sed -r 's/.*toolchain-(.*).sh/\1/')
122 arch=$(echo "${filename}" | sed -r 's/.*crosssdk-(.*)-toolchain.*/\1/')
123 installPath=${SDK_ROOT_DIR}/${profile}/${version}/${arch}
124
125 [ "${profile}" = "" ] && { echo "profile not set"; exit 7; }
126 [ "${version}" = "" ] && { echo "version not set"; exit 8; }
127 [ "${arch}" = "" ] && { echo " arch not set"; exit 9; }
128
129 sdkName=${profile}_${arch}_${version}
130
131 # Define a unique ID to be able to distinguish for example corei7-64 from qemux86-64
132 if [ "${UUID}" = "" ]; then
133     curInstDir=$(ls -d "${installPath}/*" 2> /dev/null)
134     if [ -d "${curInstDir}" ]; then
135         UUID="basename ${curInstDir}"
136     elif [ "$URL" != "" ]; then
137         UUID=$(echo "$URL" | md5sum |cut -d' ' -f1)
138     elif [ "$filename" != "" ]; then
139         UUID=$(echo $(basename $filename) | md5sum |cut -d' ' -f1)
140     else
141         echo "UUID value must be specify using --uuid option."
142         exit 1
143     fi
144 fi
145 installPath="${installPath}/${UUID}"
146
147 status="Not Installed"
148 if [ -d "${installPath}" ]; then
149     envFile=$(find "${installPath}" -maxdepth 1 -name "${SDK_ENV_SETUP_FILENAME}")
150     [ "${envFile}" != "" ] && status="Installed"
151 fi
152
153 sdkDef=""
154 if [ "${UUID}" != "" ]; then
155     sdkDef=$($(dirname "$0")/db-dump | jq '.[] | select(.uuid=="'${UUID}'")')
156 elif [ "${URL}" != "" ]; then
157     sdkDef=$($(dirname "$0")/db-dump | jq '.[] | select(.url=="'${URL}'")')
158 fi
159
160 if [ "${sdkDef}" != "" ]; then
161     sdkName=$(echo "$sdkDef" | jq -r .name)
162     [ "$UUID" = "" ] && UUID=$(echo "$sdkDef" | jq -r .uuid)
163     [ "$sdkUrl" = "" ] && sdkUrl=$(echo "$sdkDef" | jq -r .url)
164     [ "$imageUrl" = "" ] && imageUrl=$(echo "$sdkDef" | jq -r .image_url)
165     [ "$sdkDate" = "" ] && sdkDate=$(echo "$sdkDef" | jq -r .date)
166     [ "$sdkSize" = "" ] && sdkSize=$(echo "$sdkDef" | jq -r .size)
167     [ "$MD5VAL" = "" ] && MD5VAL=$(echo "$sdkDef" | jq -r .md5sum)
168 fi
169
170 read -r -d '' res <<- EndOfMessage
171 {
172     "name":         "${sdkName}",
173     "uuid":         "${UUID}",
174     "description":  "AGL SDK ${arch} (version ${version})",
175     "profile":      "${profile}",
176     "version":      "${version}",
177     "arch":         "${arch}",
178     "path":         "${installPath}",
179     "url":          "${sdkUrl}",
180     "image_url":    "${imageUrl}",
181     "status":       "${status}",
182     "date":         "${sdkDate}",
183     "size":         "${sdkSize}",
184     "md5sum":       "${MD5VAL}",
185     "setupFile":    "${envFile}"
186 }
187 EndOfMessage
188
189 echo "$res"
190 exit 0