Implement addon update

This commit is contained in:
Jan Schneider 2018-01-21 01:47:07 +01:00
parent da1c9df9e4
commit fb7122669d
3 changed files with 174 additions and 87 deletions

View File

@ -29,7 +29,7 @@ namespace eval rmupdate {
variable install_log "/usr/local/addons/rmupdate/var/install.log"
variable install_lock "/usr/local/addons/rmupdate/var/install.lock"
variable log_file "/tmp/rmupdate-addon-log.txt"
variable log_level 0
variable log_level 4
variable lock_start_port 12100
variable lock_socket
variable lock_id_log_file 1
@ -510,7 +510,6 @@ proc ::rmupdate::get_latest_firmware_version {} {
proc ::rmupdate::download_firmware {version} {
variable img_dir
variable log_file
variable install_log
set image_file "${img_dir}/RaspberryMatic-${version}.img"
@ -529,7 +528,7 @@ proc ::rmupdate::download_firmware {version} {
regexp {/([^/]+)$} $download_url match archive_file
set archive_file "${img_dir}/${archive_file}"
file mkdir $img_dir
if {$log_file != ""} {
if {$install_log != ""} {
exec /usr/bin/wget "${download_url}" --show-progress --progress=dot:giga --no-check-certificate --quiet --output-document=$archive_file 2>>${install_log}
write_install_log ""
} else {
@ -733,7 +732,7 @@ proc ::rmupdate::is_firmware_up_to_date {} {
return 0
}
proc ::rmupdate::get_addon_info {{fetch_available_versions 0} {fetch_download_url 0} {as_json 0}} {
proc ::rmupdate::get_addon_info {{fetch_available_version 0} {fetch_download_url 0} {as_json 0} {addon_id ""}} {
variable rc_dir
variable addons_www_dir
array set addons {}
@ -741,6 +740,9 @@ proc ::rmupdate::get_addon_info {{fetch_available_versions 0} {fetch_download_ur
catch {
set data [exec $f info]
set id [file tail $f]
if {$addon_id != "" && $addon_id != $id} {
continue
}
set addons(${id}::id) $id
set addons(${id}::name) ""
set addons(${id}::version) ""
@ -757,7 +759,7 @@ proc ::rmupdate::get_addon_info {{fetch_available_versions 0} {fetch_download_ur
set keyl "config_url"
}
set addons(${id}::${keyl}) $value
if {$keyl == "update" && $fetch_available_versions == 1} {
if {$keyl == "update" && $fetch_available_version == 1} {
catch {
set cgi "${addons_www_dir}/[string range $value 8 end]"
set available_version [exec tclsh "$cgi"]
@ -914,6 +916,59 @@ proc ::rmupdate::get_addon_info {{fetch_available_versions 0} {fetch_download_ur
}
}
proc ::rmupdate::install_addon {addon_id} {
variable rc_dir
if {[get_running_installation] != ""} {
error "Another install process is running."
}
set_running_installation "Addon ${addon_id}"
array set addon [get_addon_info 1 1 0 $addon_id]
set download_url $addon(${addon_id}::download_url)
write_log 3 "Downloading addon from ${download_url}."
regexp {/([^/]+)$} $download_url match archive_file
set archive_file "/tmp/${archive_file}"
if {[file exists $archive_file]} {
file delete $archive_file
}
exec /usr/bin/wget "${download_url}" --no-check-certificate --quiet --output-document=$archive_file
write_log 3 "Extracting archive ${archive_file}."
set tmp_dir "/tmp/rmupdate_addon_install_${addon_id}"
if {[file exists $tmp_dir]} {
file delete -force $tmp_dir
}
file mkdir $tmp_dir
cd $tmp_dir
exec /bin/tar xzvf "${archive_file}"
write_log 3 "Running update_script"
file attributes update_script -permissions 0755
exec ./update_script noreboot
cd /tmp
file delete -force $tmp_dir
file delete $archive_file
write_log 3 "Restarting addon"
if { [catch {
exec "${rc_dir}/${addon_id}" restart
} errormsg] } {
write_log 2 "Addon restart failed: ${errormsg}"
}
write_log 3 "Addon ${addon_id} successfully installed"
set_running_installation ""
return "Addon ${addon_id} successfully installed"
}
#puts [rmupdate::get_latest_firmware_version]
#puts [rmupdate::get_firmware_info]

View File

@ -143,7 +143,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
// We are not expecting a response
},
function(xhr, ajaxOptions, thrownError) {
console.error("Firmware installation error.")
console.error("Firmware installation error: " + thrownError + ": " + xhr.responseText);
$('[data-install-firmware-version="' + running_installation + '"]').removeClass('loading');
//$('#modal-log').modal('hide');
if (running_installation) {
@ -164,8 +164,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
}
function get_firmware_info() {
$('#dimmer-firmware-info').addClass('active');
rest("GET", "/get_firmware_info", null, function(data) {
$('#firmware_info tbody').empty();
$('#firmware-info tbody').empty();
data.forEach(function(fw) {
if (fw.latest) latest_firmware = fw.version;
if (fw.installed) current_firmware = fw.version;
@ -190,7 +191,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
var cls = '';
if (fw.installed) cls = ' class="warning"';
if (fw.latest) cls = ' class="positive"';
$("#firmware_info tbody").append($('<tr' + cls + '>').append(
$("#firmware-info tbody").append($('<tr' + cls + '>').append(
$('<td>').append($('<a>', {text: fw.version, title: 'Open release info', href: fw.info_url, target: "_blank"})),
$('<td class="center aligned">').append($('<div class="ui disabled checkbox">').append($('<input type="checkbox" disabled="disabled" '+available+'>'),$('<label></label>'))),
$('<td class="center aligned">').append($('<div class="ui disabled checkbox">').append($('<input type="checkbox" disabled="disabled" '+downloaded+'>'),$('<label></label>'))),
@ -215,6 +216,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
rest("GET", "/get_running_installation", null, function(installation_info) {
set_running_installation(installation_info);
});
$('#dimmer-firmware-info').removeClass('active');
});
}
@ -227,55 +229,69 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
});
}
function install_addon(addon_id) {
if (!running_installation) {
display_message('info', 'Installing addon ' + addon_id + '.', 6000000);
$('[data-update-addon-id="' + addon_id + '"]').addClass('loading');
$('[data-update-addon-id="' + addon_id + '"]').addClass('disabled');
rest("POST", "/install_addon", JSON.stringify({"addon_id":addon_id}),
function(data) {
//console.info(data);
$('[data-update-addon-id="' + addon_id + '"]').removeClass('loading');
$('[data-update-addon-id="' + addon_id + '"]').removeClass('disabled');
$('#tr-' + addon_id).removeClass('warning');
$('#tr-' + addon_id).addClass('positive');
$('#button-update-' + addon_id).removeClass('green');
$('#button-update-' + addon_id).addClass('gray');
$('#label-version-' + addon_id).text($('#label-available-version-' + addon_id).text());
display_message('success', data, 6000000);
},
function(xhr, ajaxOptions, thrownError) {
console.error("Addon installation error: " + thrownError + ": " + xhr.responseText);
$('[data-update-addon-id="' + addon_id + '"]').removeClass('loading');
$('[data-update-addon-id="' + addon_id + '"]').removeClass('disabled');
default_error_callback(xhr, ajaxOptions, thrownError);
}
);
}
}
function get_addon_info() {
$('#dimmer-addon-info').addClass('active');
rest("GET", "/get_addon_info", null, function(data) {
$('#addon_info tbody').empty();
$('#addon-info tbody').empty();
data.forEach(function(addon) {
//if (fw.latest) latest_firmware = fw.version;
//if (fw.installed) current_firmware = fw.version;
//var color = 'yellow';
//if (fw.latest) color = 'green';
//if (fw.installed) color = 'gray';
//var disabled = (fw.image || fw.url ? '' : 'disabled');
//var binstall = $('<div class="ui '+ color +' basic '+ disabled +' button">').attr('data-install-firmware-version', fw.version).append($('<i class="sign in icon">'), 'install');
//binstall.click(function() {
// install_firmware(this.getAttribute('data-install-firmware-version'));
//});
//var bcls = '';
//if (!fw.image) bcls = 'disabled';
//if (fw.version == running_installation) bcls = 'loading';
//var bdelete = $('<div class="ui orange basic '+ bcls +' button">').attr('data-delete-version', fw.version).append($('<i class="delete icon">'), 'delete download');
//bdelete.click(function() {
// delete_firmware_image(this.getAttribute('data-delete-version'));
//});
//
//var available = (fw.url ? 'checked=""' : '')
//var downloaded = (fw.image ? 'checked=""' : '')
//var cls = '';
//if (fw.installed) cls = ' class="warning"';
//if (fw.latest) cls = ' class="positive"';
var cls = ' class="positive"';
$("#addon_info tbody").append($('<tr' + cls + '>').append(
var color = 'gray';
if (addon.available_version && (addon.version != addon.available_version)) color = 'green';
var disabled = ((addon.available_version && addon.download_url) ? '' : 'disabled');
var bupdate = $('<div class="ui '+ color +' basic '+ disabled +' button" id="button-update-' + addon.id + '">')
.attr('data-update-addon-id', addon.id)
.attr('data-update-addon-available-version', addon.available_version)
.append($('<i class="sign in icon">'), 'update');
bupdate.click(function() {
install_addon(this.getAttribute('data-update-addon-id'));
});
disabled = ((addon.config_url) ? '' : 'disabled');
var bconfig = $('<div class="ui blue basic '+ disabled +' button">')
.attr('data-addon-config-url', addon.config_url)
.append($('<i class="setting icon">'), 'open config');
bconfig.click(function() {
var win = window.open(this.getAttribute('data-addon-config-url'), '_blank');
win.focus();
});
var cls = (((!addon.available_version) || (addon.version == addon.available_version)) ? "positive" : "warning");
var available_version = ((addon.available_version) ? addon.available_version : "?");
$("#addon-info tbody").append($('<tr class="' + cls + '" id="tr-' + addon.id + '">').append(
$('<td>').append($('<label>' + addon.name + '</label>')),
$('<td>').append($('<label>' + addon.version + '</label>')),
$('<td>').append($('<label>' + addon.available_version + '</label>')),
$('<td>').append($('<label>' + addon.download_url + '</label>'))
$('<td>').append($('<label id="label-version-' + addon.id + '">' + addon.version + '</label>')),
$('<td>').append($('<label id="label-available-version-' + addon.id + '">' + available_version + '</label>')),
$('<td class="center aligned">').append(bupdate, bconfig)
));
});
//var color = "#d01919";
//if (current_firmware == latest_firmware) {
// color = "#21BA45";
//}
//$("#firmware-summary").empty();
//$("#firmware-summary").append(
// $('<div class="sub header">').html('Current installed version: <span style="color:'+ color +'">' + current_firmware + '</span>'),
// $('<div class="sub header">').html('Latest available version: ' + latest_firmware)
//);
//if (current_firmware != latest_firmware) {
// $("#firmware-summary").append(
// $('<div class="ui green basic button" style="margin-top:20px; margin-bottom:20px;">').click(install_latest_firmware).append($('<i class="sign in icon">'), 'Install latest firmware')
// );
//}
$('#dimmer-addon-info').removeClass('active');
});
}
@ -292,7 +308,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
});
get_system_info();
get_firmware_info();
//get_addon_info();
get_addon_info();
});
</script>
</head>
@ -308,44 +324,53 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<h2 class="ui dividing header">Firmwares</h2>
<div class="content" id="firmware-summary">
</div>
<table id="firmware_info" class="ui celled stackable table">
<thead>
<tr>
<th>Version</th>
<th class="center aligned">Available</th>
<th class="center aligned">Downloaded</th>
<th class="center aligned">Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<!--
<div class="dimmable">
<div id="dimmer-firmware-info" class="ui active inverted dimmer">
<div class="ui loader">Loading</div>
</div>
<table id="firmware-info" class="ui celled stackable table">
<thead>
<tr>
<th>Version</th>
<th class="center aligned">Available</th>
<th class="center aligned">Downloaded</th>
<th class="center aligned">Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div class="ui checkbox">
<input id="dryrun" type="checkbox">
<label>Perform a trial run with no changes made</label>
</div>
<br />
<div class="ui checkbox">
<input id="reboot-after-install" type="checkbox" checked="checked">
<label>Reboot system after installation</label>
</div>
</div>
<h2 class="ui dividing header">Addons</h2>
<table id="addon_info" class="ui celled stackable table">
<thead>
<tr>
<th>Name</th>
<th>Version</th>
<th class="center aligned">Available</th>
<th class="center aligned">Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
-->
<div class="dimmable">
<div id="dimmer-addon-info" class="ui active inverted dimmer">
<div class="ui loader">Loading</div>
</div>
<table id="addon-info" class="ui celled stackable table">
<thead>
<tr>
<th>Addon name</th>
<th>Installed version</th>
<th class="center aligned">Available version</th>
<th class="center aligned">Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<div class="ui checkbox">
<input id="dryrun" type="checkbox">
<label>Perform a trial run with no changes made</label>
</div>
<br />
<div class="ui checkbox">
<input id="reboot-after-install" type="checkbox" checked="checked">
<label>Reboot system after installation</label>
</div>
</div>
<div style="height:80%" id="modal-log" class="ui modal">

View File

@ -64,6 +64,13 @@ proc process {} {
} else {
error "Invalid version: ${data}"
}
} elseif {[lindex $path 1] == "install_addon"} {
regexp {\"addon_id\"\s*:\s*\"([^\"]+)\"} $data match addon_id
if { [info exists addon_id] && $addon_id != "" } {
return "\"[rmupdate::install_addon $addon_id]\""
} else {
error "Invalid addon_id: ${addon_id}"
}
} elseif {[lindex $path 1] == "delete_firmware_image"} {
regexp {\"version\"\s*:\s*\"([\d\.]+)\"} $data match version
if { [info exists version] && $version != "" } {