def get_page(self, request): # let's see what we know about the environment log('getting home page') clear_action_updates() # last block in django database; may be different from last in blockchain last_block_updated = state.get_last_block_updated() # ready if blockchain-backup has processed some blockchain data bcb_run_already = last_block_updated > 0 bin_dir_ok = preferences.bin_dir_ok() data_dir = preferences.get_data_dir() backup_dir_ok, backup_dir_error = preferences.backup_dir_ok() backup_dir = preferences.get_backup_dir() last_bcb_version = state.get_latest_bcb_version() current_core_version = bitcoin_utils.get_bitcoin_version() last_core_version = state.get_latest_core_version() if bcb_run_already: data_dir_ok, __ = preferences.data_dir_ok() else: if data_dir and os.path.exists(data_dir): data_dir_ok, __ = preferences.data_dir_ok() else: data_dir_ok = False bitcoin_utils.check_for_updates() params = { 'data_dir': data_dir, 'data_dir_ok': data_dir_ok, 'backup_dir': backup_dir, 'backup_dir_ok': backup_dir_ok, 'backup_dir_error': backup_dir_error, 'last_bcb_version': last_bcb_version, 'bcb_up_to_date': last_bcb_version >= BLOCKCHAIN_BACKUP_VERSION, 'last_core_version': last_core_version, 'core_up_to_date': last_core_version >= current_core_version, 'need_backup': bitcoin_utils.get_next_backup_time() < now(), 'last_backed_up_time': state.get_last_backed_up_time(), } #log('params: {}'.format(params)) response = get_home_page_response(request, bcb_run_already, bin_dir_ok, params) return response
def clean_data_dir(self): ''' Clean the data directory. >>> from tempfile import gettempdir >>> prefs = PreferencesForm() >>> prefs.cleaned_data = {'data_dir': os.path.join(gettempdir(), 'bitcoin/data')} >>> prefs.clean_data_dir() '/tmp/bitcoin/data' >>> prefs.cleaned_data = {'data_dir': None} >>> data_dir = prefs.clean_data_dir() >>> data_dir.endswith('.bitcoin') True ''' data_dir = self.cleaned_data['data_dir'] if data_dir is None: data_dir = os.path.join(getdir(), '.bitcoin') if not os.path.exists(data_dir): os.makedirs(data_dir) self.cleaned_data['data_dir'] = data_dir ok, error = data_dir_ok(data_dir) if ok and error is None: data_dir = self.cleaned_data['data_dir'] else: self.log(error) raise ValidationError(error) return data_dir
def check_dirs_ok(self, backup_dates): ''' Check that all the data dirs are ok. ''' error_message = more_error_msg = None backup_dir_ok, backup_error = preferences.backup_dir_ok() if backup_dir_ok: good_backup = backup_dates log(f'backup_dates {backup_dates}') else: log(backup_error) data_dir_ok, data_error = preferences.data_dir_ok() if not preferences.bin_dir_ok(): error_message = BAD_BIN_DIR elif not data_dir_ok: error_message = BAD_DATA_DIR + ' ' + data_error elif not backup_dir_ok: error_message = BAD_BACKUP_DIR + ' ' + backup_error elif not good_backup: log(f'no good backups in {backup_dates}') error_message = self.NO_GOOD_BACKUP more_error_msg = self.REMINDER log(f'error_message {error_message}') log(f'more_error_msg {more_error_msg}') return error_message, more_error_msg
def test_preferences(self): ''' Configuring the prefernces when nothing has been configured. ''' test_utils.set_new_preferences(None) test_utils.set_new_state(None) client = Client(HTTP_X_FORWARDED_FOR='localhost') response = client.post( '/bitcoin/preferences/', { 'data_dir': '/tmp/bitcoin/data/', 'bin_dir': '/tmp/bitcoin/bin/', 'backup_schedule': 1, 'backup_levels': 2, 'backup_dir': '/tmp/bitcoin/data/backups/', 'extra_args': '', }) # if successful post, redirected to the home page self.assertEqual(response.status_code, 302) self.assertEqual(response.url, '/') # verify the preferences; the form should # have checked, but we'll double check self.assertTrue(preferences.data_dir_ok()) self.assertTrue(preferences.backup_dir_ok()) self.assertTrue(preferences.bin_dir_ok())
def check_good_post(self, response): ''' Check the post had good data. ''' # if successful post, redirected to the home page self.assertEqual(response.status_code, 302) self.assertEqual(response.url, '/') # verify the preferences; the form should # have checked, but we'll double check self.assertTrue(preferences.data_dir_ok()) self.assertTrue(preferences.backup_dir_ok()) self.assertTrue(preferences.bin_dir_ok())
def ready(self): ''' Check that no conflicting app is running. Returns: True if ready to start. False if any other app that conflicts with this app is running or the preferences aren't configured properly. >>> uab = UpdateAndBackup() >>> uab.ready() <BLANKLINE> Auto update and backup ready True ''' app_ready = False error = None if core_utils.is_bitcoin_qt_running(): error = 'Bitcoin QT is running.' elif core_utils.is_bitcoin_tx_running(): error = 'Bitcoin TX is running.' elif core_utils.is_bitcoind_running(): error = 'BitcoinD is running.' else: dir_ok, error = data_dir_ok() if dir_ok: dir_ok = bin_dir_ok() if not dir_ok: error = 'Executable dir does not exist' app_ready = dir_ok if error: message = error if error.endswith('running'): message = f'{error} Stop it before restarting this app again.' else: message = f'{error} Start Blockchain Backup to adjust the preferences.' else: message = 'Update and Backup Bitcoin Blockchain Ready' log(message) print(f'\n{message}') return app_ready
def get_page(self, request): global backup_task log('backing up blockchain') clear_action_updates() # check that no other bitcoin-core app is running if bitcoin_utils.is_bitcoin_core_running(): message = NO_BACKUP_IF_CORE_RUNNING response = warn_core_running(request, message=message) # tell user if another app is running elif bitcoin_utils.is_restore_running(): response = warn_bcb_app_running(request, app=constants.RESTORE_PROGRAM) # tell user if another task is running elif accessing_wallet() or updating() or restoring(): response = warn_bcb_task_running(request) # tell user if backups have been disabled elif not state.get_backups_enabled(): response = HttpResponseRedirect('/bitcoin/change_backup_status/') log('tried to backup when backups disabled') log(f'response: {response}') else: SUBNOTICE1 = 'If you need to stop the backup, then <a class="btn btn-secondary " href="/bitcoin/interrupt_backup/"' SUBNOTICE2 = 'role="button" id="stop-button" title="Click to stop backing up the blockchain">click here</a>.' SUBNOTICE = f'{SUBNOTICE1} {SUBNOTICE2}' context = bitcoin_utils.get_blockchain_context() data_dir_ok, error = preferences.data_dir_ok() if not preferences.bin_dir_ok(): context['notice'] = BAD_BIN_DIR context['subnotice'] = '' elif not data_dir_ok: context['notice'] = BAD_DATA_DIR context['subnotice'] = error else: context['header'] = "Backing up bitcoin's blockchain" context[ 'notice'] = 'WARNING: Stopping the backup could damage the ability to restore the blockchain.' context['subnotice'] = SUBNOTICE context['progress'] = 'Starting to back up the blockchain' context['update_interval'] = '1000' if backing_up(): log('already backing_up blockchain') else: from blockchain_backup.bitcoin.backup import BackupTask backup_task = BackupTask() backup_task.start() log('backup started') # make sure the button doesn't appear any more bitcoin_utils.send_socketio_message('button', ' ') response = render(request, 'bitcoin/backup.html', context=context) return response
def get_page(self, request): global update_task log('trying to update blockchain') clear_action_updates() # check that no other bitcoin-core app is running if (bitcoin_utils.is_bitcoin_qt_running() or bitcoin_utils.is_bitcoin_tx_running() or (bitcoin_utils.is_bitcoind_running() and not updating())): response = warn_core_running(request) # tell user if another blockchain_backup app is running elif bitcoin_utils.is_backup_running( ) or bitcoin_utils.is_restore_running(): response = warn_bcb_app_running(request) # tell user if another task is running elif accessing_wallet() or backing_up() or restoring(): response = warn_bcb_task_running(request) else: NOTICE1 = 'WARNING: Don\'t shut down your computer before you <a class="btn btn-secondary " role="button"' NOTICE2 = 'id="stop_button" href="/bitcoin/interrupt_update/" title="Click to stop updating the blockchain">Stop update</a>' NOTICE = f'{NOTICE1} {NOTICE2}' context = bitcoin_utils.get_blockchain_context() data_dir_ok, error = preferences.data_dir_ok() if not preferences.bin_dir_ok(): context['notice'] = BAD_BIN_DIR log(BAD_BIN_DIR) elif not data_dir_ok: context['notice'] = BAD_DATA_DIR log(error) else: context['header'] = "Updating Bitcoin's blockchain" context[ 'notice'] = 'WARNING: Don\'t shut down your computer before you <a class="btn btn-secondary " role="button" id="stop_button" href="/bitcoin/interrupt_update/" title="Click to stop updating the blockchain">Stop update</a>' context[ 'subnotice'] = 'Shutting down before this window says it is safe <em>could damage the blockchain</em>.' context['progress'] = 'Starting to update the blockchain' context['update_interval'] = '5000' if updating(): log('already updating blockchain') else: from blockchain_backup.bitcoin.update import UpdateTask update_task = UpdateTask() update_task.start() log('UpdateTask started') # make sure the button doesn't appear any more bitcoin_utils.send_socketio_message('button', ' ') log(f'context: {context}') response = render(request, 'bitcoin/update.html', context=context) return response
def get_page(self, request): global accessing_wallet_task log('accessing bitcoin interactively') clear_action_updates() # it's ok if bitcoin-qt is running in our task if (bitcoin_utils.is_bitcoind_running() or bitcoin_utils.is_bitcoin_tx_running() or (bitcoin_utils.is_bitcoin_qt_running() and not accessing_wallet())): response = warn_core_running(request) # tell user if another app is running elif bitcoin_utils.is_backup_running( ) or bitcoin_utils.is_restore_running(): response = warn_bcb_app_running(request) # tell user if another task is running elif updating() or backing_up() or restoring(): response = warn_bcb_task_running(request) else: SUBNOTICE1 = "Waiting until you exit Bitcoin-QT.<p>Bitcoin-QT will start in another window." SUBNOTICE2 = "You can send and receive transactions from that window." SUBNOTICE3 = "After you exit Bitcoin-QT, Blockchain Backup will continue updating the blockchain until it's time to back it up." SUBNOTICE4 = "Don't forget to back up your wallet routinely." SUBNOTICE = f'{SUBNOTICE1} {SUBNOTICE2} {SUBNOTICE3} {SUBNOTICE4}' context = bitcoin_utils.get_blockchain_context() data_dir_ok, error = preferences.data_dir_ok() if not preferences.bin_dir_ok(): context['notice'] = BAD_BIN_DIR elif not data_dir_ok: context['notice'] = BAD_DATA_DIR context['subnotice'] = error else: context[ 'header'] = "Accessing Bitcoin Core Wallet Interactively" context[ 'notice'] = 'WARNING: Do not shut down your computer until Bitcoin-QT stops completely.' context['subnotice'] = SUBNOTICE if accessing_wallet(): log('already accessing wallet') else: from blockchain_backup.bitcoin.access_wallet import AccessWalletTask accessing_wallet_task = AccessWalletTask() accessing_wallet_task.start() log('access wallet started') # make sure the button doesn't appear any more bitcoin_utils.send_socketio_message('button', ' ') response = render(request, 'bitcoin/access_wallet.html', context=context) return response