def get_local_host_ip(): (result, obj, err) = admin_helper('get-network-status', {}) network_status = obj if result != 'SUCCESS': address = '169.254.0.3' else: address = network_status['ipv4']['address'] return address
def shutdown(self, *args, **kwargs): '''Shutdown Entire Ixia System ''' err_msg = None try: (result,obj,err_msg) = admin_helper('shutdown-system',{}) return {"status":"running","messages":[{"is_error": False, "header": "System shutdown in progress", "content": obj or err_msg}]} except: return self._failure_message(err_msg)
def apply_updates(self): """ Apply any ready update """ try: terms = [Update.state == 'DOWNLOADING'] downloads = self.db.query(Update).filter(and_(*terms)).first() if downloads: LOGGER.warn('Download id={0} still in-progress.'.format(downloads.id)) return 'FAILURE', 'Update download still in-progress' terms = [Update.state == 'READY'] update = self.db.query(Update).filter(and_(*terms)).one() # Update applied state in case updates cause restart update.state = 'APPLIED' update.applied_date = datetime.now() self.db.flush() args = { 'background': 1, 'offline': 1 if update.offline else 0, 'cacheonly': 1 if not update.offline else 0 } message = None LOGGER.info('Applying updates for id={0}; latest_build={1}; args={2}'.format( update.id, update.latest_build, str(args))) transaction.commit() (result, obj, err) = admin_helper('get-updates', args) if result == 'SUCCESS': LOGGER.info('Apply updates helper succeeded.') else: LOGGER.error('Apply updates failed. err={0}'.format(err)) if 'log' in obj: message = obj['log'] return result, message except NoResultFound: LOGGER.info('No updates to apply.') return 'FAILURE', 'No updates to apply.' except Exception as e: LOGGER.exception(e)
def get_global_settings(request): # JSON feed that is responsible for the local_chassis # network information in the administration area. do_reload = request.params.get('reload', 0) # Try cache first #if not do_reload and 'global_settings' in request.session: # return request.session['global_settings'] items = {} # Get some utils for getting the global settings back to the UI global_start = time.time() ixiacrlogger.debug('Start: get_global_settings') try: (result, obj, err) = admin_helper('get-network-config', {}) network_config = obj items.update({'host': network_config['address'], 'netmask': network_config['netmask'], 'gateway': network_config['gateway'], 'hostname': network_config['hostname'], 'mac_address': ''}) build_number = get_build_number() items.update(dict({'build_number': build_number or "Unknown"})) # Check for updates #updater = Updater(db) #available_updates, newest_build = updater.get_update_info() items['updates'] = {'available_updates': 'true', 'newest_build': '1.00.0002'} stop = time.time() ixiacrlogger.debug('End: get_global_settings completed at %.3f seconds' % float(stop - global_start)) request.session['global_settings'] = items return items except Exception, e: ixiacrlogger.exception('Exception: get_global_settings -- ' + format(e)) return {"result": "FAILURE", "messages": [{"is_error": True, "header": "Failed", "content": "FAILED: {0}" .format(e)}]}
def get_disk_info(request): # JSON feed that is responsible for the disk info disk_info = {} # Get some utils for getting the global settings back to the UI ixiacrlogger.debug('Start: get_disk_info') try: (result, disk_info, err) = admin_helper('get-disk-info', {}) return disk_info except Exception, e: ixiacrlogger.exception('Exception: get_disk_info -- ' + format(e)) return {"result": "FAILURE", "messages": [{"is_error": True, "header": "Failed", "content": "FAILED: {0}" .format(e)}]}
def get_ixiacr_logs(self): """Generate a diagnostic bundle """ ixiacrlogger.debug('Entering: IxiaAdminHandler.get_ixiacr_logs') self.messages = [] try: (result, obj, err) = admin_helper('generate-diag-bundle',{}) ixiacrlogger.debug('Exiting: IxiaAdminHandler.get_ixiacr_logs') return {'result': 'SUCCESS', "messages": [{"is_error": False, "header": "Success", "content": obj}]} except Exception, e: ixiacrlogger.exception('IxiaAdminHandler.get_ixiacr_logs: ' 'Exception. %s' % e.message.encode('utf-8')) self.messages.append(dict({'is_error': True, 'header': 'Failed', 'content': e.message.encode('utf-8')})) return {'result': 'FAILURE', 'messages': self.messages}
def download_updates(self): LOGGER.info('Starting download...') kbytes_per_second_limit = get_global_config_options().get_option_value('system', 'update-kb-rate-limit') if kbytes_per_second_limit: kbytes_per_second_limit = int(kbytes_per_second_limit) args = {'offline': 0} if kbytes_per_second_limit: args['bandwidth'] = kbytes_per_second_limit LOGGER.info('bandwidth specified; kbytes_per_second_limit={0}'.format(kbytes_per_second_limit)) (result, obj, err) = admin_helper('download-updates', args) if result == 'SUCCESS': # At this point we have finished the download. pass else: LOGGER.error('download-updates failed: err={0}'.format(err)) raise Exception('Download failed')
def queue_admin_helper(command, data): """ Run admin helper in task queue """ return admin_helper(command, data)
def check_updates(self, force_offline_check=False): offline = False if not force_offline_check: LOGGER.info('Checking network for updates...') (result, obj, err) = admin_helper('list-updates', {'offline': 0}) if result == 'FAILURE': LOGGER.error('Network updated failed. err={0}'.format(err)) return else: LOGGER.info('Checking offline for updates...') (result, obj, err) = admin_helper('list-updates', {'offline': 1}) if result == 'FAILURE': LOGGER.error('Offline update failed. err={0}'.format(err)) return else: offline = True packages = obj.get('packages', None) available = len(packages) if available > 0: latest = max([int(v[1].split('-')[1]) for v in packages]) # Load from database most recent version # Is that version same as 'latest' # YES-> do nothing # NO-> update database and proceed with download cur_build = get_build_number() # If we have updates, we must call download_updates() LOGGER.info('Updates are available: current_build: {0} newest_build: {1}'.format(cur_build, latest)) # Mark any 'unapplied' updates as outdated # This leaves applied updates in the history updates = self.db.query(Update).order_by(Update.id.desc()).all() for pos, update in enumerate(updates): if update.state in ['AVAILABLE', 'DOWNLOADING', 'READY']: LOGGER.debug('Marking update state=OUTDATED; id={0}; latest_build={1}; prev_state={2}'.format( update.id, update.latest_build, update.state)) update.state = 'OUTDATED' if pos >= MAX_UPDATE_HISTORY: LOGGER.debug('Removing old update record; id={0}'.format(update.id)) self.db.delete(update) # Look for an update matching the current found update build or create it terms = [Update.latest_build == str(latest)] cur_update = self.db.query(Update).filter(and_(*terms)).first() if not cur_update: cur_update = Update() cur_update.latest_build = latest self.db.add(cur_update) # Update status to downloading, since we called list-updates cur_update.offline = offline cur_update.available_updates = available cur_update.details = json.dumps({'packages': packages}) if not cur_update.offline: cur_update.state = 'DOWNLOADING' cur_update.download_started = datetime.now() cur_update.download_finished = None self.db.flush() self.download_updates() self.db.add(cur_update) cur_update.download_finished = datetime.now() duration = (cur_update.download_finished - cur_update.download_started).seconds LOGGER.info('Download completed; secs={0}'.format(duration)) # Updates are now ready to be applied by admin cur_update.state = 'READY' transaction.commit() else: LOGGER.info('Updates are not available')
def _admin_helper(self, helper, json): return admin_helper(helper, json, debug_raw=True)