Automate FTP transfer with shell script
Android build process takes a very long time, especially on low end computers, after build complete, I wanna transfer the final image to ftp server for further automation tests, here’s the solution with curl command.
Automate ftp upload with curl
#!/bin/bash
serverip=192.168.1.89
rand=`shuf -i 2000-6000 -n 1`
image_file=$1
image_path=$(find out/target/product/ -name $image_file)
if [ -z "$image_path" ]; then
echo "Image file not ready, check later"
exit 0
fi
file_exist=$(curl --netrc --head ftp://$serverip/$image_file |grep Modified)
if [[ $file_exist == *"Modified"* ]]; then
# Rename remote file if exists
curl --netrc --ftp-create-dirs -Q "-RNFR $image_file" \
-Q "-RNTO $image_file.$rand" ftp://$serverip
fi
# Upload new file
echo "Uploading image file"
curl --netrc --upload-file $image_path ftp://$serverip
echo "Image file uploaded"
mv $image_path $image_path.$rand
Save above script to ftpupload.sh, copy to $HOME/.local/bin, then create .netrc in $HOME direcotry, and change permission to 600:
machine 192.168.1.89
login ftpuser
password ftpuser
Now we can execute the script in aosp project, and wait for upload complete.
Download image file from ftp
serverip=192.168.1.89
rand=`shuf -i 2000-6000 -n 1`
image_file=$1
# List files on ftp server
curl --netrc ftp://$serverip
curl --netrc ftp://$serverip/$image_file -o "/tmp/$image_file"
Automation inside Citrix
For security reason, you may work inside citrix, for me, the ftp server inside citrix is isolated from outside ftp, I can download files with different ip address, in this case, we do not know when the build process ends and also when upload completes.
Fortunately, there is a solution, not perfect, but it works. I found if wrong command typed in the terminal, there will be a bell ring, and this can be detected in ubuntu that running Citrix receiver, by checking the status of /proc/asound/cardX/pcmXp/subX/status, when the bell rings the status will become to state RUNNING, you may wanna use pacmd which seems better than cat the status file:
pacmd list-sink-inputs
Here is the script I used for image downloading:
serverip=192.168.1.89
rand=`shuf -i 2000-6000 -n 1`
image_file=$1
while true
do
pacmd list-sink-inputs | grep 'state: RUNNING'
if [ $? -eq 0 ]; then
echo "Image ready"
break
fi
sleep 1
done
# List files on ftp server
curl --netrc ftp://$serverip
curl --netrc ftp://$serverip/$image_file -o "/tmp/$image_file"
Now fire and wait for image file ready. Oh, don’t forget make some noise with
echo -e '\007'
or echo -e '\a'
at the end of upload script described in
upload with curl
section.