def setup(args): logger.log("Setting up PlebNet") # Prepare Cloudomate if args.test_net: settings.wallets_testnet("1") settings.settings.write() fake_generator.generate_child_account() # TODO: change --> Prepare plebnet config = PlebNetConfig() config.set('expiration_date', time.time() + 30 * plebnet_settings.TIME_IN_DAY) config.save() # handle the DNA dna = DNA() dna.read_dictionary(cloudomate_controller.get_vps_providers()) dna.write_dictionary() # Prepare the IRC Client irc_handler.init_irc_client() irc_handler.start_irc_client() logger.success("PlebNet is ready to roll!")
def check(): """ The main function to run every interval :return: None :rtype: None """ global config, dna logger.log("Checking PlebNet", log_name) config = PlebNetConfig() # TODO: DNA static singular maken --> dan kan dit weg dna = DNA() dna.read_dictionary() # these require time to setup, continue in the next iteration if not check_tribler(): return if not check_tunnel_helper(): return # Prepare Cloudomate if not settings.wallets_initiate_once(): create_wallet() select_provider() update_offer() attempt_purchase() install_vps()
def check(): """ The method is the main function which should run periodically. It controls the behaviour of the agent, starting Tribler and buying servers. """ global config, dna logger.log("Checking PlebNet", log_name) # Read general configuration if settings.wallets_testnet_created(): os.environ['TESTNET'] = '1' config = PlebNetConfig() dna = DNA() dna.read_dictionary() # check if own vpn is installed before continuing if not check_vpn_install(): logger.error("!!! VPN is not installed, child may get banned !!!", "Plebnet Check") # Requires time to setup, continue in the next iteration. if not check_tribler(): return if not settings.wallets_initiate_once(): create_wallet() select_provider() # These need a matchmaker, otherwise agent will be stuck waiting. if market_controller.has_matchmakers(): update_offer() attempt_purchase() install_vps()
def setup(args): print("Setting up PlebNet") cloudomatecontroller.generate_config() config = PlebNetConfig() config.set('expiration_date', time.time() + 30 * TIME_IN_DAY) config.save() dna = DNA() dna.read_dictionary() dna.write_dictionary() twitter.tweet_arrival() create_wallet()
def attempt_purchase(): """ Check if rich enough to buy a server, and if so, do so :return: None :rtype: None """ # try to purchase the chosen vps. (provider, option, _) = config.get('chosen_provider') if settings.wallets_testnet(): domain = 'TBTC' else: domain = 'BTC' if market_controller.get_balance(domain) >= cloudomate_controller.calculate_price(provider, option): logger.log("Try to buy a new server from %s" % provider, log_name) success = cloudomate_controller.purchase_choice(config) if success == plebnet_settings.SUCCESS: # evolve yourself positively if you are successful DNA().evolve(True) elif success == plebnet_settings.FAILURE: # evolve provider negatively if not successful DNA().evolve(False)
def setUp(self, mock): if os.path.isfile(test_log_file): os.remove(test_log_file) if os.path.isfile(test_child_file): os.remove(test_child_file) self.test_dna = DNA() fake_generator.generate_child_account() global test_account test_account.read_settings(test_child_file)
def send_child_creation_mail(ip, rootpw, success, config, user_options, transaction_hash): mail_message = 'IP: %s\n' % ip mail_message += 'Root password: %s\n' % rootpw mail_message += 'Success: %s\n' % success mail_message += 'Transaction_hash: %s\n' % transaction_hash mail_dna = DNA() mail_dna.read_dictionary() mail_message += '\nDNA\n%s\n' % json.dumps(mail_dna.dictionary) mail_message += '\nConfig\n%s\n' % json.dumps(config.config) send_mail( mail_message, user_options.get('firstname') + ' ' + user_options.get('lastname'))
def setup(args): """ This method should only be called once and is responsible for the initial setup of the PlebNet agent. All necessary configuration files are created and IRC communication is started. :param args: If running in Testnet mode. """ global dna, config logger.log("Setting up PlebNet") # Prepare the DNA configuration dna = DNA() if args.test_net: settings.wallets_testnet("1") settings.settings.write() dna.read_dictionary({ 'proxhost': cloudomate_controller.get_vps_providers()['proxhost'] }) else: dna.read_dictionary(cloudomate_controller.get_vps_providers()) if 'proxhost' in dna.vps.keys(): dna.remove_provider('proxhost') dna.write_dictionary() if args.exit_node: logger.log("Running as exitnode") settings.tribler_exitnode('1') # Prepare first child configuration fake_generator.generate_child_account() # Set general info about the PlebNet agent settings.irc_nick(settings.irc_nick_def() + str(random.randint(1000, 10000))) config = PlebNetConfig() config.set('expiration_date', time.time() + 30 * plebnet_settings.TIME_IN_DAY) # Prepare the IRC Client irc_handler.init_irc_client() irc_handler.start_irc_client() if dna.get_own_tree() == '': logger.log("tree set to %s" % settings.irc_nick()) dna.set_own_tree(settings.irc_nick()) config.save() logger.success("PlebNet is ready to roll!")
def test_read_dictionary(self): initial_dict = { 'Self': 'unknown', 'parent': 'unknown', 'transaction_hash': '', 'VPS': 'TEST' } self.ex = os.path.exists self.ini = DNA.create_initial_dict self.wri = DNA.write_dictionary os.path.exists = MagicMock(return_value=None) DNA.create_initial_dict = MagicMock(return_value=initial_dict) DNA.write_dictionary = MagicMock() dna = DNA() dna.read_dictionary() self.assertEqual(dna.vps, 'TEST') os.path.exists = self.ex DNA.create_initial_dict = self.ini DNA.write_dictionary = self.wri
def check(args): """ Check whether conditions for buying new server are met and proceed if so :param args: :return: """ print("Checking") config = PlebNetConfig() dna = DNA() dna.read_dictionary() if not tribler_running(): print("Tribler not running") success = start_tribler() print(success) # Now give tribler time to startup return success # TEMP TO SEE EXITNODE PERFORMANCE, tunnel_helper should merge with market or other way around if not os.path.isfile(os.path.join(TRIBLER_HOME, 'twistd2.pid')): env = os.environ.copy() env['PYTHONPATH'] = TRIBLER_HOME try: subprocess.call([ 'twistd', '--pidfile=twistd2.pid', 'tunnel_helper', '-x', '-M' ], cwd=TRIBLER_HOME, env=env) return True except CalledProcessError: return False # TEMP TO SEE EXITNODE PERFORMANCE if not config.get('chosen_provider'): print("Choosing new provider") update_choice(config, dna) config.save() if config.time_since_offer() > TIME_IN_HOUR: print("Calculating new offer") update_offer(config, dna) config.save() if config.get('chosen_provider'): (provider, option, _) = config.get('chosen_provider') if marketapi.get_btc_balance() >= calculate_price(provider, option): print("Purchase server") transaction_hash, provider = purchase_choice(config) if transaction_hash: config.get('transactions').append(transaction_hash) # evolve yourself positively if you are successfull own_provider = get_own_provider(dna) evolve(own_provider, dna, True) else: # evolve provider negatively if not succesfull evolve(provider, dna, False) config.save() return install_available_servers(config, dna) config.save()
def setUp(self): self.test_dna = DNA() self.test_dna.rate = 1 self.test_dna.length = 0.0 self.test_dna.dictionary = {} self.test_dna.vps = {}