This file is indexed.

/usr/share/pyshared/oneconf/networksync/__init__.py is in oneconf 0.2.8.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 2011 Canonical
#
# Authors:
#  Didier Roche
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; version 3.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

from gi.repository import GObject
import json
import logging
import os
import time

from oneconf.enums import MIN_TIME_WITHOUT_ACTIVITY
import utils
from netstatus import NetworkStatusWatcher
from ssohandler import LoginBackendDbusSSO

from paths import (ONECONF_CACHE_DIR, OTHER_HOST_FILENAME, HOST_DATA_FILENAME, PENDING_UPLOAD_FILENAME,
                  PACKAGE_LIST_PREFIX, LOGO_PREFIX, LAST_SYNC_DATE_FILENAME, WEBCATALOG_SILO_SOURCE)

from piston_mini_client.failhandlers import APIError
from httplib import BadStatusLine
from httplib2 import socket, ServerNotFoundError, RedirectLimit

LOG = logging.getLogger(__name__)

class SyncHandler(GObject.GObject):
    '''Handle sync request with the server from the dbus service'''

    def __init__(self, hosts, package_handler=None, infraclient=None, dbusemitter=None):
        GObject.GObject.__init__(self)

        self._netstate = NetworkStatusWatcher()
        self._sso_login = LoginBackendDbusSSO()
        self._can_sync = False
        self.credential = None
        self.hosts = hosts
        self.infraclient = infraclient
        self.package_handler = package_handler
        
        if dbusemitter:
            self.emit_new_hostlist = dbusemitter.hostlist_changed
            self.emit_new_packagelist = dbusemitter.packagelist_changed
            self.emit_new_logo = dbusemitter.logo_changed
            self.emit_new_latestsync = dbusemitter.latestsync_changed

        self._netstate.connect("changed", self._network_state_changed)
        self._sso_login.connect("login-result", self._sso_login_result)
        

    def _refresh_can_sync(self):
        '''compute current syncable state before asking for refresh the value'''
        new_can_sync = (self.credential is not None) and self._netstate.connected
        if self._can_sync == new_can_sync:
            return
        self._can_sync = new_can_sync

        # we can now start syncing (as it's a new status), adding the timeout
        if self._can_sync:
            self.process_sync()
            # adding the timeout only if we are not on a single sync
            if not "ONECONF_SINGLE_SYNC" in os.environ or not os.environ["ONECONF_SINGLE_SYNC"]:
                GObject.timeout_add_seconds(MIN_TIME_WITHOUT_ACTIVITY, self.process_sync)

    def _sso_login_result(self, sso_login, credential):
        if credential == self.credential:
            return

        self.credential = credential
        # Prepare the authenticated infraclient
        if self.credential and not self.infraclient:
            from piston_mini_client.auth import OAuthAuthorizer
            from infraclient_pristine import WebCatalogAPI
            from oneconf.distributor import get_distro
            service_root = get_distro().ONECONF_SERVER
            authorizer = OAuthAuthorizer(token_key=credential['token'],
                token_secret=credential['token_secret'],
                consumer_key=credential['consumer_key'],
                consumer_secret=credential['consumer_secret'],
                oauth_realm='Ubuntu Software Center')
            self.infraclient = WebCatalogAPI(service_root=service_root,
                                             auth=authorizer)
        self._refresh_can_sync()

    def _network_state_changed(self, netstate, connected):
        self._refresh_can_sync()

    def check_if_refresh_needed(self, old_data, new_data, hostid, key):
        '''Return if data dictionnary needs to be refreshed'''
        need_refresh = False
        LOG.debug("Check if %s needs to be refreshed for %s" % (key, hostid))
        try:
            if old_data[hostid]['%s_checksum' % key] != new_data[hostid]['%s_checksum' % key]:
                need_refresh = True
        except KeyError:
            # there was no old_data, if the new ones are not none, refresh
            if new_data[hostid]['%s_checksum' % key]:
                need_refresh = True
        if need_refresh:
            LOG.debug("Refresh new %s" % key)
        return need_refresh

    def check_if_push_needed(self, local_data, distant_data, key):
        '''Return if data dictionnary needs to be refreshed

            Contrary to refresh needed, we are sure that the host is registered.
            However the local checksum can be null, telling that no refresh is needed'''
        LOG.debug("Check if %s for current host need to be pushed to infra" % key)
        try:
            need_push = (local_data['%s_checksum' % key] and (local_data['%s_checksum' % key] != distant_data['%s_checksum' % key]))
        except KeyError:
            need_push = True
        if need_push:
            LOG.debug("Push new %s" % key)
        return need_push

    def emit_new_hostlist(self):
        '''this signal will be bound at init time'''
        LOG.warning("emit_new_hostlist not bound to anything")
        
    def emit_new_packagelist(self, hostid):
        '''this signal will be bound at init time'''
        LOG.warning("emit_new_packagelist(%s) not bound to anything" % hostid)

    def emit_new_logo(self, hostid):
        '''this signal will be bound at init time'''
        LOG.warning("emit_new_logo(%s) not bound to anything" % hostid)
        
    def emit_new_latestsync(self, timestamp):
        '''this signal will be bound at init time'''
        LOG.warning("emit_new_lastestsync(%s) not bound to anything" % timestamp)

    def process_sync(self):
        '''start syncing what's needed if can sync
        
        process sync can be either started directly, or when can_sync changed'''
        
        # we can't no more sync, removing the timeout
        if not self._can_sync:
            return False
        LOG.debug("Start processing sync")

        # Check server connection
        try:
            if self.infraclient.server_status() != 'ok':
                LOG.error("WebClient server answering but not available")
                return True
        except (APIError, socket.error, ValueError, ServerNotFoundError, BadStatusLine, RedirectLimit), e:
            LOG.error ("WebClient server answer error: %s", e)
            return True

        # Try to do every other hosts pending changes first (we will get fresh
        # data then)
        try:
            pending_upload_filename = os.path.join(self.hosts.get_currenthost_dir(), PENDING_UPLOAD_FILENAME)
            with open(pending_upload_filename, 'r') as f:
                pending_changes = json.load(f)
            for hostid in pending_changes.keys():
                # now do action depending on what needs to be refreshed
                try:
                    # we can only remove distant machines for now, not register new ones
                    try:
                        if not pending_changes[hostid].pop('share_inventory'):
                            LOG.debug("Removing machine %s requested as a pending change" % hostid)
                            self.infraclient.delete_machine(machine_uuid=hostid)
                    except APIError, e:
                        LOG.error("WebClient server doesn't want to remove hostid (%s): %s" % (hostid, e))
                        pending_changes[hostid]['share_inventory'] = False # append it again to be done
                except KeyError:
                    pass
                # after all changes, is hostid still relevant?
                if not pending_changes[hostid]:
                    pending_changes.pop(hostid)
            # no more change, remove the file
            if not pending_changes:
                LOG.debug("No more pending changes remaining, removing the file")
                os.remove(pending_upload_filename)
            # update the remaining tasks
            else:
                utils.save_json_file_update(pending_upload_filename, pending_changes)
        except IOError:
            pass
        except ValueError:
            LOG.warning("The pending file is broken, ignoring")

        current_hostid = self.hosts.current_host['hostid']
        old_hosts = self.hosts.other_hosts
        hostlist_changed = None
        packagelist_changed = []
        logo_changed = []

        # Get all machines
        try:
            full_hosts_list = self.infraclient.list_machines()
        except APIError, e:
            LOG.error ("Invalid machine list from server, stopping sync: %s" % e)
            return True
        other_hosts = {}
        distant_current_host = {}
        for machine in full_hosts_list:
            hostid = machine.pop("uuid")
            if hostid != current_hostid:
                other_hosts[hostid] = machine
            else:
                distant_current_host = machine

        # now refresh packages list for every hosts
        for hostid in other_hosts:
            # init the list as the infra can not send it            
            if not "packages_checksum" in other_hosts[hostid]:
                other_hosts[hostid]["packages_checksum"] = None
            packagelist_filename = os.path.join(self.hosts.get_currenthost_dir(), '%s_%s' % (PACKAGE_LIST_PREFIX, hostid))
            if self.check_if_refresh_needed(old_hosts, other_hosts, hostid, 'packages'):
                try:
                    new_package_list = self.infraclient.list_packages(machine_uuid=hostid)
                    utils.save_json_file_update(packagelist_filename, new_package_list)
                    # if already loaded, unload the package cache
                    if self.package_handler:
                        try:
                           self.package_handler.package_list[hostid]['valid'] = False
                        except KeyError:
                            pass
                    packagelist_changed.append(hostid)
                except APIError, e:
                    LOG.error ("Invalid package data from server: %s", e)
                    try:
                        old_checksum = old_hosts[hostid]['packages_checksum']
                    except KeyError:
                        old_checksum = None
                    other_hosts[hostid]['packages_checksum'] = old_checksum

            # refresh the logo for every hosts as well
            # WORKING but not wanted on the isd side for now
            #if self.check_if_refresh_needed(old_hosts, other_hosts, hostid, 'logo'):
            #    try:
            #        logo_content = self.infraclient.get_machine_logo(machine_uuid=hostid)
            #        logo_file = open(os.path.join(self.hosts.get_currenthost_dir(), "%s_%s.png" % (LOGO_PREFIX, hostid)), 'wb+')
            #        logo_file.write(self.infraclient.get_machine_logo(machine_uuid=hostid))
            #        logo_file.close()
            #        logo_changed.append(hostid)
            #    except APIError, e:
            #        LOG.error ("Invalid data from server: %s", e)
            #        try:
            #            old_checksum = old_hosts[hostid]['logo_checksum']
            #        except KeyError:
            #            old_checksum = None
            #        other_hosts[hostid]['logo_checksum'] = old_checksum

        # Now that the package list and logo are successfully downloaded, save
        # the hosts metadata there. This removes as well the remaining package list and logo
        LOG.debug("Check if other hosts metadata needs to be refreshed")
        if other_hosts != old_hosts:
            LOG.debug("Refresh new host")
            hostlist_changed = True
            other_host_filename = os.path.join(ONECONF_CACHE_DIR, current_hostid, OTHER_HOST_FILENAME)
            utils.save_json_file_update(other_host_filename, other_hosts)
            self.hosts.update_other_hosts()

        # now push current host
        if not self.hosts.current_host['share_inventory']:
            LOG.debug("Ensure that current host is not shared")
            try:
                self.infraclient.delete_machine(machine_uuid=current_hostid)
            except APIError, e:
                # just a debug message as it can be already not shared
                LOG.debug ("Can't delete current host from infra: %s" % e)
        else:
            LOG.debug("Push current host to infra now")
            # check if current host changed
            try:
                if self.hosts.current_host['hostname'] != distant_current_host['hostname']:
                    try:
                        self.infraclient.update_machine(machine_uuid=current_hostid, hostname=self.hosts.current_host['hostname'])
                        LOG.debug ("Host data refreshed")
                    except APIError, e:
                        LOG.error ("Can't update machine: %s", e)
            except KeyError:
                try:
                    self.infraclient.update_machine(machine_uuid=current_hostid, hostname=self.hosts.current_host['hostname'])
                    LOG.debug ("New host registered done")
                    distant_current_host = {'packages_checksum': None, 'logo_checksum': None}
                except APIError, e:
                    LOG.error ("Can't register new host: %s", e)
            
            # local package list
            if self.check_if_push_needed(self.hosts.current_host, distant_current_host, 'packages'):
                local_packagelist_filename = os.path.join(self.hosts.get_currenthost_dir(), '%s_%s' % (PACKAGE_LIST_PREFIX, current_hostid))
                try:
                    with open(local_packagelist_filename, 'r') as f:
                        self.infraclient.update_packages(machine_uuid=current_hostid, packages_checksum=self.hosts.current_host['packages_checksum'], package_list=json.load(f))
                except (APIError, IOError), e:
                        LOG.error ("Can't push current package list: %s", e)
                        
            # local logo
            # WORKING but not wanted on the isd side for now
            #if self.check_if_push_needed(self.hosts.current_host, distant_current_host, 'logo'):
            #    logo_file = open(os.path.join(self.hosts.get_currenthost_dir(), "%s_%s.png" % (LOGO_PREFIX, current_hostid))).read()
            #    try:
            #        self.infraclient.update_machine_logo(machine_uuid=current_hostid, logo_checksum=self.hosts.current_host['logo_checksum'], logo_content=logo_file)
            #        LOG.debug ("refresh done")
            #    except APIError, e:
            #        LOG.error ("Error while pushing current logo: %s", e)

        # write the last sync date
        timestamp = str(time.time())
        content = {"last_sync":  timestamp}
        utils.save_json_file_update(os.path.join(self.hosts.get_currenthost_dir(), LAST_SYNC_DATE_FILENAME), content)

        # send dbus signal if needed events (just now so that we don't block on remaining operations)
        if hostlist_changed:
            self.emit_new_hostlist()
        for hostid in packagelist_changed:
            self.emit_new_packagelist(hostid)
        for hostid in logo_changed:
            self.emit_new_logo(hostid)
        self.emit_new_latestsync(timestamp)

        # continue syncing in the main loop
        return True
        

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    from dbus.mainloop.glib import DBusGMainLoop
    DBusGMainLoop(set_as_default=True)
    
    from hosts import Hosts
    import sys
    from infraclient_fake import WebCatalogAPI

    os.environ["ONECONF_SINGLE_SYNC"] = "True"

    sync_handler = SyncHandler(Hosts(), infraclient=WebCatalogAPI(WEBCATALOG_SILO_SOURCE))
    loop = GObject.MainLoop() 
    GObject.timeout_add_seconds(15, loop.quit)

    loop.run()