---
title: "mapdata/ci/scripts/mapDownload.sh"
author: "jXee"
date: "2017-06-02T12:17:09Z"
tags: []
categories: ["编程语言"]
slug: "mapdata-ci-scripts-mapdownload-sh"
draft: false
lastmod: "2017-06-02T16:15:18Z"
id: 2377493
url: "https://jxee.iteye.com/blog/2377493"
---

#!/bin/bash

 

######################################################################

\# Download map data files from xxx Maps website: 

\# 1. download the fileDownloadable.xml which contains downloading info

\# 2. parse fileDownloadable.xml to get the filenames, urls and md5sum

\# 3. carry out the file downloading

\# 4. make md5sum against downloaded files and save result to file

\# 5. check md5sum to make sure downloaded files valid

######################################################################

 

set -e

 

\## Expected env variables xxx\_USERNAME and xxx\_PASSWORD

if \[\[ ! ($xxx\_USERNAME && $xxx\_PASSWORD) ]]; then

  echo "===&gt; Error: Expected env variable xxx\_USERNAME and xxx\_PASSWORD!"

  exit 1

fi

 

\## install wget if not already installed in the docker container

apt-get update && apt-get -y install wget

wget --version | awk 'NR==1 {print}'

md5sum --version | grep 'GNU'

 

DIR\_DOWNLOADED="downloaded"

FILES\_DOWNLOADABLE=filesDownloadable.xml

FILES\_DOWNLOADABLE\_URI="https://xxx.xxx.com/control/navt/login?username=$xxx\_USERNAME&password=$xxx\_PASSWORD&action=authenticate&accountID=xxxxxx&nextURL=%2Fcontrol%2Fnavt%2FfilesDownloadable%3Faction%3Dxml%26limitDays%3D10"

 

echo "===&gt; Cleaning up any existing files and folders"

if \[\[ -d $DIR\_DOWNLOADED ]]; then

rm -rf $DIR\_DOWNLOADED

echo "===&gt; Info: deleted existing folder $DIR\_DOWNLOADED"

fi

 

if \[\[ -f verifymd5.tmp ]]; then

rm verifymd5.tmp

echo "===&gt; Info: deleted existing file verifymd5.tmp"

fi

 

if \[\[ -f verifymd5.txt ]]; then

rm verifymd5.txt

echo "===&gt; Info: deleted existing file verifymd5.txt"

fi

 

echo "===&gt; xxx maps username: $xxx\_USERNAME, xxx maps password: $xxx\_PASSWORD"

echo "===&gt; Downloding $FILES\_DOWNLOADABLE: url=$FILES\_DOWNLOADABLE\_URI"

 

wget -q -L -O $FILES\_DOWNLOADABLE "$FILES\_DOWNLOADABLE\_URI"

 

if ! \[\[ -e $FILES\_DOWNLOADABLE && -s $FILES\_DOWNLOADABLE ]]; then

  echo "===&gt; Error: $FILES\_DOWNLOADABLE was not downloaded or empty!"

  exit 1

fi

 

if \[\[ $IS\_DEV == "true" ]]; then

  echo "===&gt; Info: Print filesDownloadable.xml"

  cat $FILES\_DOWNLOADABLE

fi;

 

awk 'BEGIN {filename=""; md5=""; link=""}

     /&lt;FileName&gt;/ {filename=$0; gsub("&lt;FileName&gt;&lt;!\\\\\[CDATA\\\\\[", "", filename); gsub("\\\\]\\\\]&gt;&lt;/FileName&gt;", "", filename); gsub("\[ \\t]", "", filename)}

     /&lt;DownloadLink&gt;/ {link=$0; gsub("&lt;DownloadLink&gt;&lt;!\\\\\[CDATA\\\\\[", "", link); gsub("\\\\]\\\\]&gt;&lt;/DownloadLink&gt;", "", link); gsub("\[ \\t]", "", link); print "wget -L -O \\"" filename "\\" \\"" link"\\"" &gt; "dcmd.sh"}

     /&lt;MD5CheckSum&gt;/ {md5=$0; gsub("&lt;MD5CheckSum&gt;&lt;!\\\\\[CDATA\\\\\[", "", md5); gsub("\\\\]\\\\]&gt;&lt;/MD5CheckSum&gt;", "", md5); gsub("\[ \\t]", "", md5); print md5 " " filename &gt; "md5.txt"}

' $FILES\_DOWNLOADABLE

 

if ! \[\[ -e dcmd.sh && -s dcmd.sh && -e md5.txt && -s md5.txt ]]; then

  echo "===&gt; Warn: dcmd.sh and/or md5.txt was not created or empty! There might be no files available for downloading."

  exit 0

fi

 

if \[\[ $IS\_DEV == "true" ]]; then

  cat dcmd.sh | awk 'NR&lt;3 {print &gt; "dcmd.sh"}'

  echo "===&gt; Info: Print reduced wget command list in dcmd.sh"

  cat dcmd.sh

  

  cat md5.txt | awk 'NR&lt;3 {print &gt; "md5.txt"}'

  echo "===&gt; Info: Print reduced hashes in md5.txt"

  cat md5.txt

fi

 

\# Allow 60s to manually remove large files from download cmd - comment out in production

#echo "===&gt; Sleeping for 60 seconds... you can manully remove unwanted files now..."

#sleep 60

 

mkdir -p $DIR\_DOWNLOADED

cd $DIR\_DOWNLOADED

 

echo "===&gt; Downloading xxx Maps data files to folder $DIR\_DOWNLOADED ... this may take a while..."

source ../dcmd.sh

 

echo "===&gt; Creating md5sum for downloaded files"

for afile in *

do

  if \[\[ -f $afile ]]; then

    md5sum $afile &gt;&gt; ../verifymd5.tmp

  fi

done

 

cd ..

awk '{gsub("\\\*",""); print $1 " " $2 &gt; "verifymd5.txt"}' verifymd5.tmp 

 

echo "===&gt; Verify verifiymd5.txt against md5.txt ..."

while read -r line

do 

  if grep -q "$line" md5.txt; then

    echo "===&gt; Info: md5sum checked OK: $line"

  else

    echo "===&gt; Error: md5sum does not match: $line"

    exit 1

  fi

done &lt; verifymd5.txt

 

\# Get s3 version from pipeline input

S3\_VERSION=$(cat version/number)

tar -czvf mapload/mapload-${S3\_VERSION}.tgz $DIR\_DOWNLOADED

echo "===&gt; Info: zipped up files to upload. s3 version number: $S3\_VERSION"

 

ls -l $DIR\_DOWNLOADED

ls -l mapload

 

echo "===&gt; xxx Maps data downloaded successfully"

 

exit 0