~ubuntu-installer/ubiquity/trunk

« back to all changes in this revision

Viewing changes to ubiquity/telemetry.py

  • Committer: Jean-Baptiste Lallement
  • Date: 2018-03-15 14:08:00 UTC
  • mfrom: (6592.2.6 ubiquity3)
  • Revision ID: jean-baptiste.lallement@ubuntu.com-20180315140800-iq939idf77zpw7br
* Added telemetry feature to collect installation metrics and optional reporting later on. Thanks didrocks

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8; Mode: Python; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
 
 
3
# Copyright (C) 2018 Canonical Ltd.
 
4
#
 
5
# Functions useful for the final install.py script and for ubiquity
 
6
# plugins to use
 
7
#
 
8
# This program is free software; you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation; either version 2 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# This program is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with this program; if not, write to the Free Software
 
20
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
21
 
 
22
 
 
23
import json
 
24
import os
 
25
import stat
 
26
import syslog
 
27
import time
 
28
 
 
29
from ubiquity.misc import raise_privileges
 
30
 
 
31
START_INSTALL_STAGE_TAG = 'start_install'
 
32
 
 
33
 
 
34
def get():
 
35
    """Return a singleton _Telemetry instance."""
 
36
    if _Telemetry._telemetry is None:
 
37
        _Telemetry._telemetry = _Telemetry()
 
38
    return _Telemetry._telemetry
 
39
 
 
40
 
 
41
class _Telemetry():
 
42
 
 
43
    _telemetry = None
 
44
 
 
45
    def __init__(self):
 
46
        self._metrics = {}
 
47
        self._stages_hist = {}
 
48
        self._start_time = time.time()
 
49
        self.add_stage('start')
 
50
        self._dest_path = '/target/var/log/installer/telemetry'
 
51
        try:
 
52
            with open('/cdrom/.disk/info') as f:
 
53
                self._metrics['Media'] = f.readline()
 
54
        except FileNotFoundError:
 
55
            self._metrics['Media'] = 'unknown'
 
56
 
 
57
    def add_stage(self, stage_name):
 
58
        """Record installer stage with current time"""
 
59
        self._stages_hist[int(time.time() - self._start_time)] = stage_name
 
60
 
 
61
    def set_installer_type(self, installer_type):
 
62
        """Record installer type"""
 
63
        self._metrics['Type'] = installer_type
 
64
 
 
65
    def set_partition_method(self, method):
 
66
        """Record anynomized partition method"""
 
67
        self._metrics['PartitionMethod'] = method
 
68
 
 
69
    @raise_privileges
 
70
    def done(self, db):
 
71
        """Close telemetry collection
 
72
 
 
73
        Set as installation done, add additional info and save to
 
74
        destination file"""
 
75
        self.add_stage('done')
 
76
 
 
77
        self._metrics['DownloadUpdates'] = db.get('ubiquity/download_updates')
 
78
        self._metrics['Language'] = db.get('localechooser/languagelist')
 
79
        self._metrics['Minimal'] = db.get('ubiquity/minimal_install')
 
80
        self._metrics['RestrictedAddons'] = db.get('ubiquity/use_nonfree')
 
81
        self._metrics['Stages'] = self._stages_hist
 
82
 
 
83
        target_dir = os.path.dirname(self._dest_path)
 
84
        try:
 
85
            if not os.path.exists(target_dir):
 
86
                os.makedirs(target_dir)
 
87
            with open(self._dest_path, 'w') as f:
 
88
                json.dump(self._metrics, f)
 
89
            os.chmod(self._dest_path,
 
90
                     stat.S_IRUSR | stat.S_IWUSR |
 
91
                     stat.S_IRGRP | stat.S_IROTH)
 
92
        except OSError as e:
 
93
            syslog.syslog(syslog.LOG_ERR,
 
94
                          "Exception while storing telemetry data: " + str(e))
 
95
 
 
96
# vim:ai:et:sts=4:tw=80:sw=4: