Exemplo n.º 1
0
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()
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
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'))
Exemplo n.º 4
0
def select_provider():
    """
    Check whether a provider is already selected, otherwise select one based on the dna
    :return: None
    :rtype: None
    """
    if not config.get('chosen_provider'):
        logger.log("No provider chosen yet", log_name)

        all_providers = dna.vps
        excluded_providers = config.get('excluded_providers')
        available_providers = list(
            set(all_providers.keys()) - set(excluded_providers))
        providers = {
            k: all_providers[k]
            for k in all_providers if k in available_providers
        }

        if providers >= 1 and sum(providers.values()) > 0:
            providers = DNA.normalize_excluded(providers)
            choice = cloudomate_controller.pick_provider(providers)
            config.set('chosen_provider', choice)
        logger.log("Provider chosen: %s" % str(config.get('chosen_provider')),
                   log_name)
        config.save()
Exemplo n.º 5
0
def pick_provider(providers):
    provider = DNA.choose_provider(providers)
    gateway = cloudomate_providers[provider].gateway
    option, price, currency = pick_option(provider)
    btc_price = gateway.estimate_price(
        cloudomate.wallet.get_price(
            price, currency)) + cloudomate.wallet.get_network_fee()
    return provider, option, btc_price
Exemplo n.º 6
0
def pick_provider(providers):
    """
    This method picks a provider based on the DNA o the agent.
    :param providers:
    :return:
    """
    provider = DNA.choose_provider(providers)
    gateway = get_vps_providers()[provider].get_gateway()
    option, price, currency = pick_option(provider)
    btc_price = gateway.estimate_price(wallet_util.get_price(price, currency))
    return provider, option, btc_price
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
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!")
Exemplo n.º 10
0
def update_choice(config, dna):
    all_providers = dna.vps
    excluded_providers = config.get('excluded_providers')
    available_providers = list(
        set(all_providers.keys()) - set(excluded_providers))
    providers = {
        k: all_providers[k]
        for k in all_providers if k in available_providers
    }
    print("Providers: %s" % providers)
    if providers >= 1 and sum(providers.values()) > 0:
        providers = DNA.normalize_excluded(providers)
        choice = (provider, option, price) = pick_provider(providers)
        config.set('chosen_provider', choice)
        print("First provider: %s" % provider)
Exemplo n.º 11
0
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()
Exemplo n.º 12
0
    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
Exemplo n.º 13
0
class TestDna(unittest.TestCase):

    test_dna = None

    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 = {}

    def tearDown(self):
        pass

    def test_add_provider(self):
        self.test_dna.add_provider("provider1")
        self.assertEqual(self.test_dna.vps, {'provider1': 0.5})

    def test_remove_provider(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.remove_provider("provider1")
        self.assertEqual({}, self.test_dna.vps)

    def test_normalize(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.normalize()
        self.assertEqual({'provider1': 1}, self.test_dna.vps)

    def test_mutate_false(self):
        self.test_dna.add_provider("provider1")
        self.assertFalse(self.test_dna.mutate("provider2"))

    def test_mutate_true(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.mutate("provider1")
        self.assertEqual({'provider1': 1.5}, self.test_dna.vps)

    def test_demutate_false(self):
        self.test_dna.add_provider("provider1")
        self.assertFalse(self.test_dna.demutate("provider2"))

    def test_demutate_enough(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.rate = 0.5
        self.test_dna.demutate("provider1")
        self.assertEqual({'provider1': 0.0}, self.test_dna.vps)

    def test_demutate_not_enough(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.rate = 0.2
        self.test_dna.demutate("provider1")
        self.test_dna.rate = 0.4
        self.test_dna.demutate("provider1")
        self.assertEqual({'provider1': 0.3}, self.test_dna.vps)

    def test_denormalize(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.test_dna.normalize()

        self.test_dna.length = 7
        self.test_dna.denormalize()

        self.assertEqual({'provider1': 3.5, 'provider2': 3.5}, self.test_dna.vps)

    @mock.patch('random.uniform', return_value=0.7)
    def test_choose_provider(self, mock1):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.assertEqual(self.test_dna.choose_provider(self.test_dna.vps), 'provider2')

    def test_exclude(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.test_dna.add_provider("provider3")
        new = self.test_dna.exclude("provider2")

        self.assertEqual({'provider1': 0.5, 'provider3': 0.5}, new)
        self.assertEqual({'provider1': 0.5, 'provider2': 0.5, 'provider3': 0.5}, self.test_dna.vps)

    def test_normalize_excluded(self):
        test_dict = {'provider1': 0.5}
        res_dict = self.test_dna.normalize_excluded(test_dict)
        self.assertEqual({'provider1': 1}, res_dict)

    @mock.patch('plebnet.agent.dna.DNA.write_dictionary', return_value=None)
    def test_positive_evolve(self, mock):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.test_dna.rate = 0.25
        self.test_dna.positive_evolve("provider2")

        result = self.test_dna.exclude("provider2")
        self.assertEqual({'provider1': 0.4}, result)

    @mock.patch('plebnet.agent.dna.DNA.write_dictionary', return_value=None)
    def test_negative_evolve(self, mock):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.test_dna.rate = 0.5
        self.test_dna.negative_evolve("provider2")
        self.assertEqual({'provider1': 1.0, 'provider2': 0.0}, self.test_dna.vps)

    @mock.patch('plebnet.agent.dna.DNA.write_dictionary', return_value=None)
    def test_set_and_get_own_provider(self, mock):
        self.test_dna.set_own_provider("provider2")
        self.assertEqual("provider2", self.test_dna.get_own_provider())

    @mock.patch('plebnet.agent.dna.DNA.write_dictionary', return_value=None)
    def test_evolve_positive(self, mock):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.test_dna.set_own_provider("provider2")

        self.test_dna.evolve(True)
        self.assertEqual({'provider1': 0.25, 'provider2': 0.75}, self.test_dna.vps)

    @mock.patch('plebnet.agent.dna.DNA.write_dictionary', return_value=None)
    def test_evolve_negative(self, mock):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.test_dna.set_own_provider("provider2")

        self.test_dna.rate = 0.5
        self.test_dna.evolve(False)
        self.assertEqual({'provider1': 1.0, 'provider2': 0.0}, self.test_dna.vps)
Exemplo n.º 14
0
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!")
Exemplo n.º 15
0
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()
Exemplo n.º 16
0
class TestDna(unittest.TestCase):

    test_dna = None

    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 = {}

    def tearDown(self):
        pass

    def test_add_provider(self):
        self.test_dna.add_provider("provider1")
        self.assertEqual(self.test_dna.vps, {'provider1': 0.5})

    def test_remove_provider(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.remove_provider("provider1")
        self.assertEqual({}, self.test_dna.vps)

    def test_normalize(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.normalize()
        self.assertEqual({'provider1': 1}, self.test_dna.vps)

    def test_mutate_false(self):
        self.test_dna.add_provider("provider1")
        self.assertFalse(self.test_dna.mutate("provider2"))

    def test_mutate_true(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.mutate("provider1")
        self.assertEqual({'provider1': 1.5}, self.test_dna.vps)

    def test_demutate_false(self):
        self.test_dna.add_provider("provider1")
        self.assertFalse(self.test_dna.demutate("provider2"))

    def test_demutate_enough(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.rate = 0.5
        self.test_dna.demutate("provider1")
        self.assertEqual({'provider1': 0.0}, self.test_dna.vps)

    def test_demutate_not_enough(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.rate = 0.2
        self.test_dna.demutate("provider1")
        self.test_dna.rate = 0.4
        self.test_dna.demutate("provider1")
        self.assertEqual({'provider1': 0.3}, self.test_dna.vps)

    def test_denormalize(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.test_dna.normalize()

        self.test_dna.length = 7
        self.test_dna.denormalize()

        self.assertEqual({
            'provider1': 3.5,
            'provider2': 3.5
        }, self.test_dna.vps)

    @mock.patch('random.uniform', return_value=0.7)
    def test_choose_provider(self, mock1):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.assertEqual(self.test_dna.choose_provider(self.test_dna.vps),
                         'provider2')

    def test_exclude(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.test_dna.add_provider("provider3")
        new = self.test_dna.exclude("provider2")

        self.assertEqual({'provider1': 0.5, 'provider3': 0.5}, new)
        self.assertEqual({
            'provider1': 0.5,
            'provider2': 0.5,
            'provider3': 0.5
        }, self.test_dna.vps)

    def test_normalize_excluded(self):
        test_dict = {'provider1': 0.5}
        res_dict = self.test_dna.normalize_excluded(test_dict)
        self.assertEqual({'provider1': 1}, res_dict)

    @mock.patch('plebnet.agent.dna.DNA.write_dictionary', return_value=None)
    def test_positive_evolve(self, mock):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.test_dna.rate = 0.25
        self.test_dna.positive_evolve("provider2")

        result = self.test_dna.exclude("provider2")
        self.assertEqual({'provider1': 0.4}, result)

    @mock.patch('plebnet.agent.dna.DNA.write_dictionary', return_value=None)
    def test_negative_evolve(self, mock):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.test_dna.rate = 0.5
        self.test_dna.negative_evolve("provider2")
        self.assertEqual({
            'provider1': 1.0,
            'provider2': 0.0
        }, self.test_dna.vps)

    @mock.patch('plebnet.agent.dna.DNA.write_dictionary', return_value=None)
    def test_set_and_get_own_provider(self, mock):
        self.test_dna.set_own_provider("provider2")
        self.assertEqual("provider2", self.test_dna.get_own_provider())

    @mock.patch('plebnet.agent.dna.DNA.write_dictionary', return_value=None)
    def test_evolve_positive(self, mock):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.test_dna.set_own_provider("provider2")

        self.test_dna.evolve(True)
        self.assertEqual({
            'provider1': 0.25,
            'provider2': 0.75
        }, self.test_dna.vps)

    @mock.patch('plebnet.agent.dna.DNA.write_dictionary', return_value=None)
    def test_evolve_negative(self, mock):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.test_dna.set_own_provider("provider2")

        self.test_dna.rate = 0.5
        self.test_dna.evolve(False, "provider1")
        self.assertEqual({
            'provider1': 0.0,
            'provider2': 1.0
        }, self.test_dna.vps)

    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
Exemplo n.º 17
0
class TestDna(unittest.TestCase):

    test_dna = None

    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 = {}

    def tearDown(self):
        pass

    def test_add_provider(self):
        self.test_dna.add_provider("provider1")
        self.assertEqual(self.test_dna.vps, {'provider1': 0.5})

    def test_remove_provider(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.remove_provider("provider1")
        self.assertEqual({}, self.test_dna.vps)

    def test_normalize(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.normalize()
        self.assertEqual({'provider1': 1}, self.test_dna.vps)

    def test_mutate_false(self):
        self.test_dna.add_provider("provider1")
        self.assertFalse(self.test_dna.mutate("provider2"))

    def test_mutate_true(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.mutate("provider1")
        self.assertEqual({'provider1': 1.5}, self.test_dna.vps)

    def test_demutate_false(self):
        self.test_dna.add_provider("provider1")
        self.assertFalse(self.test_dna.demutate("provider2"))

    def test_demutate_enough(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.rate = 0.5
        self.test_dna.demutate("provider1")
        self.assertEqual({'provider1': 0.0}, self.test_dna.vps)

    def test_demutate_not_enough(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.rate = 0.2
        self.test_dna.demutate("provider1")
        self.test_dna.rate = 0.4
        self.test_dna.demutate("provider1")
        self.assertEqual({'provider1': 0.3}, self.test_dna.vps)

    def test_denormalize(self):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.test_dna.normalize()

        self.test_dna.length = 7
        self.test_dna.denormalize()

        self.assertEqual({
            'provider1': 3.5,
            'provider2': 3.5
        }, self.test_dna.vps)

    @mock.patch('random.uniform', return_value=0.7)
    def test_choose_provider(self, mock1):
        self.test_dna.add_provider("provider1")
        self.test_dna.add_provider("provider2")
        self.assertEqual(self.test_dna.choose_provider(self.test_dna.vps),
                         'provider2')
Exemplo n.º 18
0
 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 = {}