mirror of
https://github.com/EliasKotlyar/Xiaomi-Dafang-Hacks.git
synced 2025-09-08 23:20:48 +02:00
Autoupgrade (#1374)
* New menu organisation * Improve upgrade process by creation a VERSION file * Add commit info in status overview * Improve update process with VERSION file * Typo fixies
This commit is contained in:
@@ -18,11 +18,15 @@ DESTOVERRIDE="/tmp/Update"
|
||||
EXCLUDEFILTER="*.conf|*.user|passwd|shadow"
|
||||
GITHUBURL="https://api.github.com/repos"
|
||||
GITHUBURLRAW="https://raw.githubusercontent.com"
|
||||
CURL="/system/sdcard/bin/curl -k"
|
||||
CURL="/system/sdcard/bin/curl -k -L"
|
||||
JQ="/system/sdcard/bin/jq"
|
||||
SHA="/system/sdcard/bin/openssl dgst -sha256"
|
||||
BASENAME="/system/sdcard/bin/busybox basename"
|
||||
FIND="/system/sdcard/bin/busybox find"
|
||||
VERSION_FILE='/system/sdcard/VERSION'
|
||||
COMMITS_FILE='/tmp/.lastcommit'
|
||||
|
||||
|
||||
|
||||
TMPFILE=/tmp/update.tmp
|
||||
BACKUPEXT=.backup
|
||||
@@ -125,13 +129,6 @@ ismatch()
|
||||
|
||||
echo notmatch
|
||||
}
|
||||
##########################################################################
|
||||
# Return the current (last) commit from the specified repo and branch
|
||||
getCurrentCommitDateFromRemote()
|
||||
{
|
||||
LASTCOMMITDATE=$(${CURL} -s ${GITHUBURL}/${REPO}/commits/${BRANCH} | grep date | head -1 | cut -d'"' -f 4)
|
||||
echo ${LASTCOMMITDATE}
|
||||
}
|
||||
|
||||
##########################################################################
|
||||
# Print the files from repo of the folder $1
|
||||
@@ -168,6 +165,12 @@ countdownreboot()
|
||||
action reboot
|
||||
}
|
||||
##########################################################################
|
||||
# Generate VERSION file
|
||||
generateVersionFile ()
|
||||
{
|
||||
echo "{\"date\":\"${REMOTECOMMITDATE}\",\"branch\":\"${BRANCH}\",\"commit\":\"${REMOTECOMMITID}\"}" > $VERSION_FILE
|
||||
}
|
||||
##########################################################################
|
||||
# Script real start
|
||||
|
||||
while [ $# -gt 0 ]
|
||||
@@ -221,6 +224,12 @@ done
|
||||
|
||||
log "Starting AutoUpdate on branch ${BRANCH}"
|
||||
|
||||
######################################################""
|
||||
# Get date and last commit ID from Github
|
||||
$(${CURL} -s ${GITHUBURL}/${REPO}/commits/${BRANCH} --output $COMMITS_FILE)
|
||||
REMOTECOMMITDATE=$(${JQ} -r '.commit .author .date' ${COMMITS_FILE})
|
||||
REMOTECOMMITID=$(${JQ} -r '.sha[0:7]' ${COMMITS_FILE} )
|
||||
|
||||
if [ ${_FORCE} = 1 ]; then
|
||||
log "Forcing update."
|
||||
fi
|
||||
@@ -235,11 +244,22 @@ fi
|
||||
|
||||
action "rm -rf ${DESTOVERRIDE} 2>/dev/null"
|
||||
|
||||
|
||||
|
||||
log "Getting list of remote files."
|
||||
FIRST=$(${CURL} -s ${GITHUBURL}/${REPO}/contents/${REMOTEFOLDER}?ref=${BRANCH})
|
||||
FILES=$(getfiles "${FIRST}")
|
||||
if [ -f "$VERSION_FILE" ]; then
|
||||
LOCALCOMMITID=$(${JQ} -r .commit ${VERSION_FILE})
|
||||
if [ ${LOCALCOMMITID} = ${REMOTECOMMITID} ]; then
|
||||
logerror "You have already lastest version"
|
||||
exit 1
|
||||
else
|
||||
echo "Need to upgrade from ${LOCALCOMMITID} to ${REMOTECOMMITID}"
|
||||
log "Getting list of remote files."
|
||||
FILES=$(${CURL} -s ${GITHUBURL}/${REPO}/compare/${LOCALCOMMITID}...${REMOTECOMMITID} | ${JQ} -r '.files[].raw_url' | grep ${REMOTEFOLDER})
|
||||
fi
|
||||
else
|
||||
echo "Version file missing. Upgrade to last commit ${REMOTECOMMITID}"
|
||||
log "Getting list of remote files."
|
||||
FIRST=$(${CURL} -s ${GITHUBURL}/${REPO}/contents/${REMOTEFOLDER}?ref=${BRANCH})
|
||||
FILES=$(getfiles "${FIRST}")
|
||||
fi
|
||||
|
||||
if [ $_PROGRESS = 1 ]; then
|
||||
_NBTOTALFILES=$(echo $FILES | wc -w)
|
||||
@@ -252,8 +272,7 @@ for i in ${FILES}
|
||||
do
|
||||
progress
|
||||
# String to remove to get the local path
|
||||
REMOVE="${GITHUBURLRAW}/${REPO}/${BRANCH}/${REMOTEFOLDER}/"
|
||||
LOCALFILE="${i#$REMOVE}"
|
||||
LOCALFILE=$(echo ${i} | awk -F ${REMOTEFOLDER}/ '{print $2}')
|
||||
# Remove files that match the filter
|
||||
res=$(ismatch ${LOCALFILE})
|
||||
if [ "$res" == "match" ]; then
|
||||
@@ -351,8 +370,7 @@ if [ -d ${DESTOVERRIDE} ] && [ $(ls -l ${DESTOVERRIDE}/* | wc -l 2>/dev/null) >
|
||||
action "rm -Rf ${DESTOVERRIDE}/* 2>/dev/null"
|
||||
|
||||
# Everythings was OK, save the date
|
||||
echo -n $(getCurrentCommitDateFromRemote) > /system/sdcard/.lastCommitDate
|
||||
echo " ## ${BRANCH} branch" >> /system/sdcard/.lastCommitDate
|
||||
generateVersionFile
|
||||
echo "--------------- Reboot ------------"
|
||||
if [ ${_FORCEREBOOT} = 1 ]; then
|
||||
countdownreboot
|
||||
@@ -365,7 +383,6 @@ if [ -d ${DESTOVERRIDE} ] && [ $(ls -l ${DESTOVERRIDE}/* | wc -l 2>/dev/null) >
|
||||
fi
|
||||
fi
|
||||
else
|
||||
echo -n $(getCurrentCommitDateFromRemote) > /system/sdcard/.lastCommitDate
|
||||
echo " ## ${BRANCH} branch" >> /system/sdcard/.lastCommitDate
|
||||
generateVersionFile
|
||||
echo "No files to update."
|
||||
fi
|
||||
|
@@ -684,3 +684,20 @@ reboot_system() {
|
||||
remount_sdcard() {
|
||||
mount -o remount,rw /system/sdcard
|
||||
}
|
||||
|
||||
# Check commit between VERSION file and hithub
|
||||
check_commit() {
|
||||
if [ -s /system/sdcard/VERSION ]; then
|
||||
localcommit=$(/system/sdcard/bin/jq -r .commit /system/sdcard/VERSION)
|
||||
localbranch=$(/system/sdcard/bin/jq -r .branch /system/sdcard/VERSION)
|
||||
remotecommit=$(/system/sdcard/bin/curl -s https://api.github.com/repos/EliasKotlyar/Xiaomi-Dafang-Hacks/commits/${localbranch} | /system/sdcard/bin/jq -r '.sha[0:7]')
|
||||
if [ ${localcommit} = ${remotecommit} ]; then
|
||||
echo "${localcommit} ( No update available)"
|
||||
else
|
||||
commitbehind=$(/system/sdcard/bin/curl -s https://api.github.com/repos/EliasKotlyar/Xiaomi-Dafang-Hacks/compare/${remotecommit}...${localcommit} | /system/sdcard/bin/jq -r '.behind_by')
|
||||
echo "${localcommit} ( ${commitbehind} commits behind Github)"
|
||||
fi
|
||||
else
|
||||
echo "No version file"
|
||||
fi
|
||||
}
|
@@ -7,8 +7,15 @@
|
||||
MAC_SIMPLE=$(cat /sys/class/net/wlan0/address | tr -d :)
|
||||
MANUFACTURER="Xiaomi"
|
||||
MODEL="Dafang"
|
||||
VER=`cat /system/sdcard/.lastCommitDate`
|
||||
|
||||
JQ="/system/sdcard/bin/jq -r"
|
||||
if [ -s "/system/sdcard/VERSION" ]; then
|
||||
V_DATE=$(${JQ} .date /system/sdcard/VERSION)
|
||||
V_BRANCH=$(${JQ} .branch /system/sdcard/VERSION)
|
||||
V_COMMIT=$(${JQ} .commit /system/sdcard/VERSION)
|
||||
VER="${V_DATE} - ${V_BRANCH} - ${V_COMMIT}"
|
||||
else
|
||||
VER="Need upgrade to have VERSION file"
|
||||
fi
|
||||
MQTT_COMMAND="/system/sdcard/bin/mosquitto_pub.bin -h "$HOST" -p "$PORT" -u "$USER" -P "$PASS" -t"
|
||||
DEVICE_INFO="\"device\": {\"identifiers\": \"$MAC_SIMPLE\", \"connections\": [[\"mac\", \"$MAC\"]], \"manufacturer\": \"$MANUFACTURER\", \"model\": \"$MODEL\", \"name\": \"$DEVICE_NAME\", \"sw_version\": \"$VER\"}"
|
||||
|
||||
|
@@ -727,7 +727,21 @@ if [ -n "$F_cmd" ]; then
|
||||
detect_model
|
||||
return
|
||||
;;
|
||||
|
||||
check_update)
|
||||
if [ -s /system/sdcard/VERSION ]; then
|
||||
localcommit=$(/system/sdcard/bin/jq -r .commit /system/sdcard/VERSION)
|
||||
localbranch=$(/system/sdcard/bin/jq -r .branch /system/sdcard/VERSION)
|
||||
remotecommit=$(/system/sdcard/bin/curl -s https://api.github.com/repos/EliasKotlyar/Xiaomi-Dafang-Hacks/commits/${localbranch} | /system/sdcard/bin/jq -r '.sha[0:7]')
|
||||
if [ ${localcommit} = ${remotecommit} ]; then
|
||||
echo -n 0
|
||||
else
|
||||
echo -n 1
|
||||
fi
|
||||
else
|
||||
echo -n 1
|
||||
fi
|
||||
return
|
||||
;;
|
||||
*)
|
||||
echo "Unsupported command '$F_cmd'"
|
||||
;;
|
||||
|
@@ -150,9 +150,15 @@ if [ -n "$F_cmd" ]; then
|
||||
echo $(hostname);
|
||||
;;
|
||||
|
||||
version)
|
||||
echo $(cat /system/sdcard/.lastCommitDate);
|
||||
;;
|
||||
version)
|
||||
if [ -s "/system/sdcard/VERSION" ]; then
|
||||
V_BRANCH=$(/system/sdcard/bin/jq -r .branch /system/sdcard/VERSION)
|
||||
V_COMMIT=$(/system/sdcard/bin/jq -r .commit /system/sdcard/VERSION)
|
||||
echo "commit <b>${V_COMMIT}</b> from branch <b>${V_BRANCH}</b>"
|
||||
else
|
||||
echo "Need to update to create a version file."
|
||||
fi
|
||||
;;
|
||||
|
||||
*)
|
||||
echo "Unsupported command '$F_cmd'"
|
||||
|
@@ -47,9 +47,18 @@ cat << EOF
|
||||
<td> $(detect_model) </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td> Firmware Version </td>
|
||||
<td> $(if [ -s "/system/sdcard/.lastCommitDate" ]; then cat /system/sdcard/.lastCommitDate; else echo "Update for version string"; fi) </td>
|
||||
<td> Firmware date </td>
|
||||
<td> $(if [ -s "/system/sdcard/VERSION" ]; then /system/sdcard/bin/jq -r .date /system/sdcard/VERSION; else echo "Never updated. Make an update to get version."; fi) </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td> Firmware branch </td>
|
||||
<td> $(if [ -s "/system/sdcard/VERSION" ]; then /system/sdcard/bin/jq -r .branch /system/sdcard/VERSION; else echo "Never updated. Make an update to get version."; fi) </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td> Firmware commit </td>
|
||||
<td> $(if [ -s "/system/sdcard/VERSION" ]; then $(check_commit; else echo "Never updated. Make an update to get version."; fi) </td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td> Kernel Version </td>
|
||||
<td> $(/system/sdcard/bin/busybox uname -v) </td>
|
||||
|
@@ -1,5 +1,6 @@
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function() {
|
||||
$(document).ready(function() {
|
||||
checkversion();
|
||||
update(true)
|
||||
});
|
||||
|
||||
@@ -16,6 +17,20 @@
|
||||
}
|
||||
};
|
||||
|
||||
function checkversion(result) {
|
||||
$.ajax({
|
||||
'url': 'cgi-bin/action.cgi?cmd=check_update'
|
||||
}).done(function(result){
|
||||
if (result == 0) {
|
||||
var div = document.getElementById("no_updatebox");
|
||||
|
||||
} else {
|
||||
var div = document.getElementById("updatebox");
|
||||
}
|
||||
div.style.display = '';
|
||||
div.style.visibility = 'visible';
|
||||
});
|
||||
}
|
||||
|
||||
function update(onStart) {
|
||||
$.ajax({
|
||||
@@ -94,11 +109,16 @@
|
||||
<header class='card-header'>
|
||||
<p class='card-header-title'>Update</p>
|
||||
</header>
|
||||
<div class='card-content'>
|
||||
<div class='card-content' style="display: none; visibility: hidden;" id='no_updatebox'>
|
||||
<div class="field is-horizontal">
|
||||
<p>You have already the lastest version.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class='card-content' style="display: none; visibility: hidden;" id='updatebox'>
|
||||
<div class="field is-horizontal">
|
||||
<p>You can replace the running firmware on this camera by the latest available from its <a target='_blank' href="https://github.com/EliasKotlyar/Xiaomi-Dafang-Hacks">Github repository</a>. Settings will be retained after update.</p>
|
||||
</div>
|
||||
<div class='card-content'>
|
||||
<div class='card-content' id=update>
|
||||
<div class="columns">
|
||||
<div class="column">
|
||||
<div class="control">
|
||||
@@ -111,7 +131,8 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class='u'>
|
||||
<h4 class="title is-4">Please note: at the end of this process the camera will reboot without notice</h4>
|
||||
<progress id=progress name=progress class="progress is-danger" value=0 max="100"></progress>
|
||||
<h1 id=message class="title is-1 has-text-centered"></h1>
|
||||
|
Reference in New Issue
Block a user