def purchase_choice_vpn(config): provider = plebnet_settings.get_instance().vpn_host() provider_instance = get_vpn_providers()[provider](child_account()) # no need to generate new child config wallet = TriblerWallet( plebnet_settings.get_instance().wallets_testnet_created()) c = cloudomate_providers['vpn'][provider] configurations = c.get_options() # option is assumbed to be the first vpn provider option option = configurations[0] transaction_hash = provider_instance.purchase(wallet, option) if not transaction_hash: logger.warning("Failed to purchase vpn") return plebnet_settings.FAILURE if False: logger.warning("Insufficient funds to purchase server") return plebnet_settings.UNKNOWN config.get('bought').append( (provider, transaction_hash, config.get('child_index'))) config.get('transactions').append(transaction_hash) config.save() return plebnet_settings.SUCCESS
def create_gist(username, password): try: # the log files filename = plebnet_settings.get_instance().logger_file() content = open(filename, 'r').read() # Our url to create issues via POST url = 'https://api.github.com/gists' # Create an authenticated session to create the issue session = requests.Session() session.auth = (username, password) # Create our issue gist = { "description": "the description for this gist", "public": True, "files": { "logfile.txt": { "content": content } } } r = session.post(url, json.dumps(gist)) if r.status_code == 201: logger.success('Successfully created gist') else: logger.warning('Could not create gist') logger.log(r.content, 'Response:') return r.json()['url'], r.json()['html_url'] except: logger.error(sys.exc_info()[0], "git_issuer gist") logger.error(traceback.format_exc())
def purchase_choice_vpn(config): provider = plebnet_settings.get_instance().vpn_host() provider_instance = get_vpn_providers()[provider](child_account()) # no need to generate new child config wallet = TriblerWallet(plebnet_settings.get_instance().wallets_testnet_created()) c = cloudomate_providers['vpn'][provider] configurations = c.get_options() # option is assumbed to be the first vpn provider option option = configurations[0] try: transaction_hash = provider_instance.purchase(wallet, option) except: title = "Failed to purchase vpn: %s" % sys.exc_info()[0] body = traceback.format_exc() logger.error(title) logger.error(body) git_issuer.handle_error(title, body) git_issuer.handle_error("Failed to purchase server", sys.exc_info()[0], ['crash']) return plebnet_settings.FAILURE if not transaction_hash: logger.warning("VPN probably purchased, but transaction hash not returned") config.get('bought').append((provider, option, transaction_hash, config.get('child_index'))) config.get('transactions').append(transaction_hash) config.save() return plebnet_settings.SUCCESS
def create_gist(filename=None): """ This method can be used to send a file to github via gist. :param filename: the file to send, if left empty, the log file is send :type filename: String """ # Only execute if PlebNet is activated settings = plebnet_settings.get_instance() if not settings.github_active(): return if not filename: filename = settings.logger_file() try: # Collect variables username = settings.github_username() password = settings.github_password() bot_name = settings.irc_nick() # Get the log files content = open(filename, 'r').read() # Our url to create issues via POST url = 'https://api.github.com/gists' # Create an authenticated session to create the issue session = requests.Session() session.auth = (username, password) # Create our issue gist = { "description": "The logfile for %s" % bot_name, "public": True, "files": { "logfile.txt": { "content": content } } } r = session.post(url, json.dumps(gist)) # Inform about the results if r.status_code == 201: logger.success('Successfully created gist') else: logger.warning('Could not create gist') logger.log(r.content, 'Response:') return r.json()['url'], r.json()['html_url'] except: logger.error(sys.exc_info()[0], "git_issuer gist") logger.error(traceback.format_exc()) return None, None
def test_add_multiple_logs(self): logger.log(msg1) logger.warning(msg2) logger.success(msg3) logger.error(msg4) f = open(logfile) for line in f: if msg1 in line: self.assertTrue("INFO" in line) if msg2 in line: self.assertTrue("WARNING" in line) if msg3 in line: self.assertTrue("INFO" in line) if msg4 in line: self.assertTrue("ERROR" in line)
def create_issue(title, body, labels): """ This method creates a github issue when called. :param title: The title of the issue :type title: String :param body: The body text of the issue :type body: String :param labels: The labels which should be attached to the issue :type labels: String[] """ # Only execute if PlebNet is activated settings = plebnet_settings.get_instance() if not settings.github_active(): return try: # Collect variables username = settings.github_username() password = settings.github_password() repo_owner = settings.github_owner() repo_name = settings.github_repo() # Our url to create issues via POST url = 'https://api.github.com/repos/%s/%s/issues' % (repo_owner, repo_name) # Create an authenticated session to create the issue session = requests.Session() session.auth = (username, password) # Create our issue issue = {'title': title, 'body': body, 'labels': labels} # Add the issue to our repository r = session.post(url, json.dumps(issue)) # Inform about the results if r.status_code == 201: logger.success('Successfully created Issue "%s"' % title) else: logger.warning('Could not create Issue "%s"' % title) logger.log(r.content, 'Response:') except: logger.error(sys.exc_info()[0], "git_issuer send") logger.error(traceback.format_exc())
def create_issue(username, password, repo_owner, repo_name, title, body, labels): try: # Our url to create issues via POST url = 'https://api.github.com/repos/%s/%s/issues' % (repo_owner, repo_name) # Create an authenticated session to create the issue session = requests.Session() session.auth = (username, password) # Create our issue issue = {'title': title, 'body': body, 'labels': labels} # Add the issue to our repository r = session.post(url, json.dumps(issue)) if r.status_code == 201: logger.success('Successfully created Issue "%s"' % title) else: logger.warning('Could not create Issue "%s"' % title) logger.log(r.content, 'Response:') except: logger.error(sys.exc_info()[0], "git_issuer send") logger.error(traceback.format_exc())
def purchase_choice(config): """ Purchase the cheapest provider in chosen_providers. If buying is successful this provider is moved to bought. In any case the provider is removed from choices. :param config: config :return: plebnet_settings errorcode """ (provider, option, _) = config.get('chosen_provider') provider_instance = cloudomate_providers['vps'][provider](child_account()) wallet = TriblerWallet( plebnet_settings.get_instance().wallets_testnet_created()) vps_option = get_vps_option(provider, option) try: transaction_hash = provider_instance.purchase(wallet, vps_option) except: title = "Failed to purchase server: %s" % sys.exc_info()[0] body = traceback.format_exc() logger.error(title) logger.error(body) git_issuer.handle_error(title, body) git_issuer.handle_error("Failed to purchase server", sys.exc_info()[0], ['crash']) return plebnet_settings.FAILURE # Cloudomate should throw an exception when purchase fails. The transaction hash is not in fact required, # and even when cloudomate fails to return it, the purchase itself could have been successful. if not transaction_hash: logger.warning( "Server probably purchased, but transaction hash not returned") config.get('bought').append( (provider, option, transaction_hash, config.get('child_index'))) config.get('transactions').append(transaction_hash) config.set('chosen_provider', None) config.save() return plebnet_settings.SUCCESS
def purchase_choice(config): """ Purchase the cheapest provider in chosen_providers. If buying is successful this provider is moved to bought. In any case the provider is removed from choices. :param config: config :return: success """ (provider, option, _) = config.get('chosen_provider') provider_instance = cloudomate_providers['vps'][provider](child_account()) PlebNetConfig().increment_child_index() fake_generator.generate_child_account() wallet = TriblerWallet( plebnet_settings.get_instance().wallets_testnet_created()) c = cloudomate_providers['vps'][provider] configurations = c.get_options() option = configurations[option] transaction_hash, _ = provider_instance.purchase(wallet, option) if not transaction_hash: logger.warning("Failed to purchase server") return plebnet_settings.FAILURE # TODO: how to spot the difference? if False: logger.warning("Insufficient funds to purchase server") return plebnet_settings.UNKNOWN config.get('bought').append( (provider, transaction_hash, config.get('child_index') - 1)) config.get('transactions').append(transaction_hash) config.set('chosen_provider', None) config.save() return plebnet_settings.SUCCESS