@cryptotaxi247 / netdata-1 / commits / 88c6daad7

Correct version check in UI (#5429)

* Correct version check in UI. Support stable and nightly release channel. * Use github releases instead of latest versions, get nightlies from GCS * Prevent cross-origin errors by using the google API

Chris Akritidis committed Feb 20, 2019 at 19:56 UTC 88c6daad7965c10797a7badde4cd6851d35647cd
2 files changed +107 -15
web/api/formatters/charts2json.c
+34
@@ -4,6 +4,38 @@
4
5 // generate JSON for the /api/v1/charts API call
6
7 +static inline const char* get_release_channel() {
8 + static int use_stable = -1;
9 +
10 + if (use_stable == -1) {
11 + char filename[FILENAME_MAX + 1];
12 + snprintfz(filename, FILENAME_MAX, "%s/.environment", netdata_configured_user_config_dir);
13 + procfile *ff = procfile_open(filename, "=", PROCFILE_FLAG_DEFAULT);
14 + if(!ff) {
15 + use_stable=1;
16 + } else {
17 + procfile_set_quotes(ff, "'\"");
18 + ff = procfile_readall(ff);
19 + if(!ff) {
20 + use_stable=1;
21 + } else {
22 + unsigned int i;
23 + for(i = 0; i < procfile_lines(ff); i++) {
24 + if (!procfile_linewords(ff, i)) continue;
25 +
26 + if (!strcmp(procfile_lineword(ff, i, 0), "RELEASE_CHANNEL") && !strcmp(procfile_lineword(ff, i, 1), "stable")) {
27 + use_stable = 1;
28 + break;
29 + }
30 + }
31 + procfile_close(ff);
32 + if (use_stable == -1) use_stable = 0;
33 + }
34 + }
35 + }
36 + return (use_stable)?"stable":"nightly";
37 +}
38 +
39 void charts2json(RRDHOST *host, BUFFER *wb) {
40 static char *custom_dashboard_info_js_filename = NULL;
41 size_t c, dimensions = 0, memory = 0, alarms = 0;
@@ -17,6 +49,7 @@ void charts2json(RRDHOST *host, BUFFER *wb) {
49 buffer_sprintf(wb, "{\n"
50 "\t\"hostname\": \"%s\""
51 ",\n\t\"version\": \"%s\""
52 + ",\n\t\"release_channel\": \"%s\""
53 ",\n\t\"os\": \"%s\""
54 ",\n\t\"timezone\": \"%s\""
55 ",\n\t\"update_every\": %d"
@@ -25,6 +58,7 @@ void charts2json(RRDHOST *host, BUFFER *wb) {
58 ",\n\t\"charts\": {"
59 , host->hostname
60 , host->program_version
61 + , get_release_channel()
62 , host->os
63 , host->timezone
64 , host->rrd_update_every
web/gui/main.js
+73 -15
@@ -1037,6 +1037,7 @@ var options = {
1037 data: null,
1038 hostname: 'netdata_server', // will be overwritten by the netdata server
1039 version: 'unknown',
1040 + release_channel: 'unknown',
1041 hosts: [],
1042
1043 duration: 0, // the default duration of the charts
@@ -2700,6 +2701,7 @@ function initializeDynamicDashboardWithData(data) {
2701 options.hostname = data.hostname;
2702 options.data = data;
2703 options.version = data.version;
2704 + options.release_channel = data.release_channel;
2705 netdataDashboard.os = data.os;
2706
2707 if (typeof data.hosts !== 'undefined') {
@@ -2832,12 +2834,33 @@ function versionsMatch(v1, v2) {
2834 if (v1 == v2) {
2835 return true;
2836 } else {
2835 - var s1=v1.split('-');
2836 - var s2=v2.split('-');
2837 - if (s1.length !== s2.length) return false;
2838 - if (s1.length === 4) s1.pop();
2839 - if (s2.length === 4) s2.pop();
2840 - return (s1.join('-') === s2.join('-'));
2837 + let s1=v1.split('.');
2838 + let s2=v2.split('.');
2839 + // Check major version
2840 + let n1 = parseInt(s1[0].substring(1,2),10);
2841 + let n2 = parseInt(s2[0].substring(1,2), 10);
2842 + if ( n1 < n2 ) return false;
2843 + else if ( n1 > n2 ) return true;
2844 +
2845 + // Check minor version
2846 + n1 = parseInt(s1[1],10);
2847 + n2 = parseInt(s2[1],10);
2848 + if ( n1 < n2 ) return false;
2849 + else if ( n1 > n2 ) return true;
2850 +
2851 + // Split patch: format could be e.g. 0-22-nightly
2852 + s1=s1[2].split('-');
2853 + s2=s2[2].split('-');
2854 +
2855 + n1 = parseInt(s1[0],10);
2856 + n2 = parseInt(s2[0],10);
2857 + if ( n1 < n2 ) return false;
2858 + else if ( n1 > n2 ) return true;
2859 +
2860 + n1 = (s1.length > 1) ? parseInt(s1[1],10) : 0;
2861 + n2 = (s2.length > 1) ? parseInt(s2[1],10) : 0;
2862 + if ( n1 < n2 ) return false;
2863 + else return true;
2864 }
2865 }
2866
@@ -2845,26 +2868,61 @@ function getGithubLatestVersion(callback) {
2868 versionLog('Downloading latest version id from github...');
2869
2870 $.ajax({
2848 - url: 'https://api.github.com/repositories/10744183/contents/packaging/version?ref=master',
2871 + url: 'https://api.github.com/repos/netdata/netdata/releases/latest',
2872 async: true,
2873 cache: false
2874 })
2875 .done(function (data) {
2853 - data = atob(data.content).replace(/(\r\n|\n|\r| |\t)/gm, "");
2854 - versionLog('Latest version from github is ' + data);
2876 + data = data.tag_name.replace(/(\r\n|\n|\r| |\t)/gm, "");
2877 + versionLog('Latest stable version from github is ' + data);
2878 callback(data);
2879 })
2880 .fail(function () {
2858 - versionLog('Failed to download the latest version id from github!');
2881 + versionLog('Failed to download the latest stable version id from github!');
2882 callback(null);
2883 });
2884 }
2885
2886 +function getGCSLatestVersion(callback) {
2887 + versionLog('Downloading latest version id from GCS...');
2888 + $.ajax({
2889 + url: "https://www.googleapis.com/storage/v1/b/netdata-nightlies/o/latest-version.txt",
2890 + async: true,
2891 + cache: false
2892 + })
2893 + .done(function (response) {
2894 + $.ajax({
2895 + url: response.mediaLink,
2896 + async: true,
2897 + cache: false
2898 + })
2899 + .done(function (data) {
2900 + data = data.replace(/(\r\n|\n|\r| |\t)/gm, "");
2901 + versionLog('Latest nightly version from GCS is ' + data);
2902 + callback(data);
2903 + })
2904 + .fail(function (xhr, textStatus, errorThrown) {
2905 + versionLog('Failed to download the latest nightly version id from GCS!');
2906 + callback(null);
2907 + });
2908 + })
2909 + .fail(function (xhr, textStatus, errorThrown) {
2910 + versionLog('Failed to download the latest nightly version from GCS!');
2911 + callback(null);
2912 + });
2913 +}
2914 +
2915 +
2916 function checkForUpdateByVersion(force, callback) {
2864 - getGithubLatestVersion(function (sha2) {
2917 + if (options.release_channel === 'stable') {
2918 + getGithubLatestVersion(function (sha2) {
2919 callback(options.version, sha2);
2866 - });
2867 -
2920 + });
2921 + } else {
2922 + getGCSLatestVersion(function (sha2) {
2923 + callback(options.version, sha2);
2924 + });
2925 + }
2926 return null;
2927 }
2928
@@ -2896,10 +2954,10 @@ function notifyForUpdate(force) {
2954 versionLog('<p><big>Failed to get your netdata version!</big></p><p>You can always get the latest netdata from <a href="https://github.com/netdata/netdata" target="_blank">its github page</a>.</p>');
2955 } else if (sha2 === null) {
2956 save = false;
2899 - versionLog('<p><big>Failed to get the latest netdata version github.</big></p><p>You can always get the latest netdata from <a href="https://github.com/netdata/netdata" target="_blank">its github page</a>.</p>');
2957 + versionLog('<p><big>Failed to get the latest netdata version.</big></p><p>You can always get the latest netdata from <a href="https://github.com/netdata/netdata" target="_blank">its github page</a>.</p>');
2958 } else if (versionsMatch(sha1, sha2)) {
2959 save = true;
2902 - versionLog('<p><big>You already have the latest netdata!</big></p><p>No update yet?<br/>Probably, we need some motivation to keep going on!</p><p>If you haven\'t already, <a href="https://github.com/netdata/netdata" target="_blank">give netdata a <b><i class="fas fa-star"></i></b> at its github page</a>.</p>');
2960 + versionLog('<p><big>You already have the latest netdata!</big></p><p>No update yet?<br/>We probably need some motivation to keep going on!</p><p>If you haven\'t already, <a href="https://github.com/netdata/netdata" target="_blank">give netdata a <b><i class="fas fa-star"></i></b> at its github page</a>.</p>');
2961 } else {
2962 save = true;
2963 var compare = 'https://docs.netdata.cloud/changelog/';