Last active
August 5, 2020 08:13
-
-
Save mschiu77/fec414223beee73010ae94c1815080b5 to your computer and use it in GitHub Desktop.
Offline MVP python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import configparser | |
import dbus | |
import sys | |
import os | |
import shlex | |
import subprocess | |
import tempfile | |
import time | |
import shutil | |
import gi | |
gi.require_version("Gtk", "3.0") | |
from gi.repository import Gtk | |
#import dbusmock | |
class MVPCollector(): | |
OFFLINE_METRICS_DIR = 'eos-metrics-data-1' | |
METRICS_CACHE_DIR = '/var/cache/metrics' | |
OFFLINE_METRICS_DEVICE_PATH = None | |
OFFLINE_METRICS_DATA_PATH = None | |
TRACKING_ID_PATH = '/etc/metrics/tracking-id' | |
MACHINE_ID_PATH = '/etc/machine-id' | |
def __init__(self, storage_path=None): | |
if storage_path is None: | |
print('Please assign the metrics data storage path') | |
else: | |
self.OFFLINE_METRICS_DEVICE_PATH= storage_path | |
def check_available_space(self): | |
print('Not done yet') | |
def check_if_duplicate(self): | |
''' | |
Check if directory name (tracking id) is duplicate | |
''' | |
print('Not done') | |
def create_folder_for_machine(self): | |
target_dir = os.path.join(self.OFFLINE_METRICS_DEVICE_PATH, self.OFFLINE_METRICS_DIR) | |
if os.path.exists(target_dir) is False: | |
os.mkdir(target_dir) | |
if os.path.exists(self.TRACKING_ID_PATH) is True: | |
f = open(self.TRACKING_ID_PATH) | |
self.OFFLINE_METRICS_DATA_PATH = os.path.join(target_dir, f.read(32)) | |
if os.path.exists(self.OFFLINE_METRICS_DATA_PATH) is False: | |
os.mkdir(self.OFFLINE_METRICS_DATA_PATH) | |
else: | |
subprocess.check_output(['sudo', 'rm', '-rf', self.OFFLINE_METRICS_DATA_PATH]) | |
os.mkdir(self.OFFLINE_METRICS_DATA_PATH) | |
f.close() | |
elif os.path.exists(self.MACHINE_ID_PATH) is True: | |
f = open(self.MACHINE_ID_PATH) | |
self.OFFLINE_METRICS_DATA_PATH = os.path.join(target_dir, f.read(32)) | |
if os.path.exists(self.OFFLINE_METRICS_DATA_PATH) is False: | |
os.mkdir(self.OFFLINE_METRICS_DATA_PATH) | |
else: | |
subprocess.check_output(['sudo', 'rm', '-rf', self.OFFLINE_METRICS_DATA_PATH]) | |
os.mkdir(self.OFFLINE_METRICS_DATA_PATH) | |
f.close() | |
else: | |
print('There is no metrics data available') | |
def copy_tracking_id(self): | |
if os.path.exists(self.TRACKING_ID_PATH) is True: | |
shutil.copy(self.TRACKING_ID_PATH, self.OFFLINE_METRICS_DATA_PATH) | |
elif os.path.exists(self.MACHINE_ID_PATH) is True: | |
shutil.copy(self.MACHINE_ID_PATH, self.OFFLINE_METRICS_DATA_PATH) | |
else: | |
printf('No tracking ID found') | |
def copy_metrics_data(self): | |
files = ['boot_offset_metafile', 'network_send_file', 'variants.dat', 'variants.dat.metadata'] | |
for file in files: | |
shutil.copy(os.path.join(self.METRICS_CACHE_DIR, file), self.OFFLINE_METRICS_DATA_PATH, follow_symlinks=False) | |
def check_disk_usage(self): | |
print('Constructing') | |
def copy_data(self): | |
self.create_folder_for_machine() | |
self.copy_tracking_id() | |
self.copy_metrics_data() | |
class MVPUploader(): | |
_METRICS_IFACE = 'com.endlessm.Metrics.EventRecorderServer' | |
OFFLINE_METRICS_DIR = 'eos-metrics-data' | |
OFFLINE_TMP_METRICS_DIR = '/var/tmp/metrics' | |
OFFLINE_STORAGE_PATH = None | |
def __init__(self, storage_path=None): | |
if storage_path is None: | |
print('Please give the metrics data storage path') | |
else: | |
self.OFFLINE_STORAGE_PATH = storage_path | |
self.test_dir = tempfile.TemporaryDirectory( | |
prefix='eos-metrics-data') | |
# dbusmock.DBusTestCase.start_system_bus() | |
# self.dbus_con = dbusmock.DBusTestCase.get_dbus(system_bus=True) | |
# (polkit_popen, polkit_obj) = dbusmock.DBusTestCase.spawn_server_template('polkitd') | |
def check_upload_result(self): | |
''' | |
Check if the upload successful or not | |
''' | |
print('Not done') | |
def remove_offline_metrics(self): | |
''' | |
Remove the data after upload done | |
''' | |
print('Not done') | |
def prepare_tmp_cache_directory(self): | |
print(self.test_dir.name) | |
#os.mkdir(self.OFFLINE_TMP_METRICS_DIR) | |
def copy_metrics_data_and_upload(self): | |
eos_metrics_data_dir=os.path.join(self.OFFLINE_STORAGE_PATH, self.OFFLINE_METRICS_DIR) | |
for target in os.listdir(eos_metrics_data_dir): | |
# TODO: only iterates for directories | |
print("Directory %s:" % target) | |
os.mkdir(self.OFFLINE_TMP_METRICS_DIR) | |
target_dir=os.path.join(eos_metrics_data_dir, target) | |
for file in os.listdir(os.path.join(eos_metrics_data_dir, target)): | |
shutil.copy(os.path.join(target_dir, file), self.OFFLINE_TMP_METRICS_DIR) | |
subprocess.Popen(['sudo', 'chown', '-R', 'metrics:metrics', self.OFFLINE_TMP_METRICS_DIR]) | |
persistent_cache_dir_arg = '--persistent-cache-directory=' + self.OFFLINE_TMP_METRICS_DIR | |
self.daemon = subprocess.Popen(['sudo', '-u', 'metrics', '/lib/eos-event-recorder-daemon/eos-metrics-event-recorder', persistent_cache_dir_arg]) | |
time.sleep(3) | |
upload_result=subprocess.check_output(['/usr/bin/eos-upload-metrics']) | |
print(upload_result) | |
#self.daemon.kill() | |
subprocess.check_output(['sudo', 'killall', '/lib/eos-event-recorder-daemon/eos-metrics-event-recorder']) | |
# dbusmock.DBusTestCase.wait_for_bus_object('com.endlessm.Metrics', '/com/endlessm/Metrics', system_bus=True) | |
subprocess.check_output(['sudo', 'rm', '-rf', self.OFFLINE_TMP_METRICS_DIR]) | |
print("Trying doing upload") | |
def __del__(self): | |
''' | |
Remove temporary cache data | |
''' | |
self.test_dir.cleanup() | |
# shutil.rmtree(self.OFFLINE_TMP_METRICS_DIR) | |
subprocess.check_output(['sudo', 'rm', '-rf', self.OFFLINE_TMP_METRICS_DIR]) | |
METRICS_SYSTEMD_SERVICE = 'eos-metrics-event-recorder.service' | |
class OfflineMVPWindow(Gtk.Window): | |
def __init__(self): | |
super().__init__() | |
self.init_app_ui() | |
def init_app_ui(self): | |
grid = Gtk.Grid() | |
self.add(grid) | |
collectBtn = Gtk.Button(label="Collect") | |
collectBtn.set_size_request(120, 50) | |
collectBtn.connect("clicked", self.on_collect_clicked) | |
uploadBtn = Gtk.Button(label="Upload") | |
uploadBtn.set_size_request(120, 50) | |
uploadBtn.connect("clicked", self.on_upload_clicked) | |
grid.attach(collectBtn, 0, 0, 1, 1) | |
grid.attach(uploadBtn, 1, 0, 1, 1) | |
self.set_border_width(10) | |
self.set_title("Offline MVP tool") | |
self.set_default_size(280, 100) | |
self.connect("destroy", self.on_destroy) | |
def on_destroy(self, widget): | |
subprocess.check_output(['/usr/bin/systemctl', 'restart', METRICS_SYSTEMD_SERVICE]) | |
Gtk.main_quit() | |
def on_collect_clicked(self, widget): | |
subprocess.check_output(['/usr/bin/systemctl', 'stop', METRICS_SYSTEMD_SERVICE]) | |
##location = os.path.dirname(os.path.realpath(sys.argv[0])) | |
##collector = MVPCollector(location) # TODO: find the real one | |
collector = MVPCollector('/run/media/dev/BIOSUP') | |
collector.copy_data() | |
def on_upload_clicked(self, widget): | |
subprocess.check_output(['/usr/bin/systemctl', 'stop', METRICS_SYSTEMD_SERVICE]) | |
##location = os.path.dirname(os.path.realpath(sys.argv[0])) | |
##uploader = MVPUploader(location) # TODO: find the real one | |
uploader = MVPUploader('/run/media/dev/BIOSUP') | |
uploader.prepare_tmp_cache_directory() | |
uploader.copy_metrics_data_and_upload() | |
def main(): | |
##location = os.path.dirname(os.path.realpath(sys.argv[0])) | |
##uploader = MVPUploader(location) # TODO: find the real one | |
#uploader = MVPUploader('/run/media/dev/BIOSUP') | |
win = OfflineMVPWindow() | |
win.show_all() | |
Gtk.main() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment