Ejemplo n.º 1
0
def options(provider):
    opts = []
    try:
        opts = provider.get_options()
    except:
        logger.log(provider.get_metadata()[0] + " options failed", "cloudomate_controller")
    return opts
Ejemplo n.º 2
0
    def keep_running(self, buffer):
        try:
            buffer += self.irc.recv(2048).decode()
            lines = str.split(buffer, "\r\n")
            buffer = lines.pop()

            for line in lines:
                logger.log("Received IRC message: " + line)

            for line in lines:
                self.handle_line(line)

        except KeyboardInterrupt:
            st = "QUIT :I have to go for now!"
            self.irc.send(st)
            raise
        except:
            title = "A error occurred in IRCbot %s" % sys.exc_info()[0]
            body = traceback.format_exc()
            logger.error(title)
            logger.error(body)
            git_issuer.handle_error(title, body)
            self.send_msg(title)

        return buffer
Ejemplo n.º 3
0
def attempt_purchase():
    """
    Check if enough money to buy a server, and if so, do so,
    """
    (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) +
            cloudomate_controller.calculate_price_vpn()):
        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)
            logger.log(
                "Purchasing vps for child %s successful" %
                (dna.get_own_tree() + '.' + str(config.get('child_index'))))
            # purchase VPN with same config if server allows for it
            if cloudomate_controller.get_vps_providers(
            )[provider].TUN_TAP_SETTINGS:
                attempt_purchase_vpn()
        elif success == plebnet_settings.FAILURE:
            # Evolve provider negatively if not successful
            dna.evolve(False, provider)

        config.increment_child_index()
        fake_generator.generate_child_account()
        config.set('chosen_provider', None)
        config.save()
Ejemplo n.º 4
0
    def __init__(self):
        logger.log("preparing an IRC connection")

        # load required settings once
        settings = plebnet_settings.get_instance()
        self.server = settings.irc_server()
        self.timeout = settings.irc_timeout()
        self.channel = settings.irc_channel()
        self.port = settings.irc_port()

        if settings.irc_nick() == settings.irc_nick_def():
            settings.irc_nick(settings.irc_nick_def() +
                              str(random.randint(1000, 10000)))

        self.nick = settings.irc_nick()
        self.ident = "plebber"
        self.gecos = "Plebbot version 2.14"

        self.irc = None
        self.init_time = time.time()
        self.last_beat = time.time()

        # prep reply functions
        self.responses = {}
        self.add_response("alive", self.msg_alive)
        self.add_response("error", self.msg_error)
        self.add_response("host", self.msg_host)
        self.add_response("init", self.msg_init)
        self.add_response("joke", self.msg_joke)

        # start running the IRC server
        logger.log("start running an IRC connection on " + self.server + " " +
                   self.channel)
        self.run()
Ejemplo n.º 5
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()
Ejemplo n.º 6
0
 def sell_reputation(self):
     if self.config.time_to_expiration() <= plebnet_settings.TIME_IN_DAY:
         available_mb = self.get_available_mb()
         if available_mb == 0:
             logger.log("No MB available", log_name)
             return
         self.update_offer(available_mb)
Ejemplo n.º 7
0
def check_tunnel_helper():
    """
    Temporary function to track the data stream processed by Tribler
    :return: None
    :rtype: None
    """
    # TEMP TO SEE EXITNODE PERFORMANCE, tunnel_helper should merge with market or other way around
    if not os.path.isfile(
            os.path.join(settings.tribler_home(),
                         settings.tunnelhelper_pid())):
        logger.log("Starting tunnel_helper", log_name)
        env = os.environ.copy()
        env['PYTHONPATH'] = settings.tribler_home()
        try:
            subprocess.call(
                [
                    'twistd', '--pidfile=' + settings.tunnelhelper_pid(),
                    'tunnel_helper', '-x', '-m'
                ],  #, '-M'],
                cwd=settings.tribler_home(),
                env=env)
            return True
        except subprocess.CalledProcessError as e:
            logger.error(e.output, log_name)
            return False
    return True
Ejemplo n.º 8
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!")
Ejemplo n.º 9
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()
Ejemplo n.º 10
0
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())
Ejemplo n.º 11
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()
Ejemplo n.º 12
0
def start():
    """
    Start tribler
    :return:
    """
    env = os.environ.copy()
    env['PYTHONPATH'] = setup.tribler_home()
    try:
        if setup.wallets_testnet():
            success = subprocess.call([
                'twistd', '--pidfile=' + setup.tribler_pid(), 'plebnet', '-p',
                '8085'
                '--testnet'
            ],
                                      cwd=setup.tribler_home(),
                                      env=env)
        else:
            success = subprocess.call([
                'twistd', '--pidfile=' + setup.tribler_pid(), 'plebnet', '-p',
                '8085'
            ],
                                      cwd=setup.tribler_home(),
                                      env=env)

        if not success:
            logger.error('Failed to start Tribler', "tribler_controller")
            return False
        logger.success('Tribler is started', "tribler_controller")

        logger.log('market running: ' +
                   str(market_controller.is_market_running()))
        return True
    except subprocess.CalledProcessError as e:
        logger.error(e.output, "Tribler starter", "tribler_controller")
        return False
Ejemplo n.º 13
0
def start():
    """
    Starts Tribler by using the twistd plugin.
    :return: boolean representing the success of starting Tribler
    """
    env = os.environ.copy()
    env['PYTHONPATH'] = setup.tribler_home()

    command = [
        'twistd', '--pidfile=' + setup.tribler_pid(), 'plebnet', '-p', '8085'
    ]

    if setup.wallets_testnet():
        command.append('--testnet')

    if setup.tribler_exitnode():
        command.append('--exitnode')

    try:
        exitcode = subprocess.call(command, cwd=setup.tribler_home(), env=env)

        if exitcode != 0:
            logger.error('Failed to start Tribler', "tribler_controller")
            return False
        logger.success('Tribler is started', "tribler_controller")
        logger.log('testnet: ' + str(setup.wallets_testnet()))
        return True
    except subprocess.CalledProcessError as e:
        logger.error(e.output, "tribler_controller")
        return False
Ejemplo n.º 14
0
def get_balance(domain):
    logger.log('The market is running' + str(is_market_running()), "get " + domain + " balance")
    try:
        r = requests.get('http://localhost:8085/wallets/' + domain + '/balance')
        balance = r.json()
        return balance['balance']['available']
    except ConnectionError:
        return False
Ejemplo n.º 15
0
 def update_offer(self, mb_amount, timeout=plebnet_settings.TIME_IN_HOUR):
     """
     Check if "timeout" has passed since the last offer made, if passed create a new offer.
     """
     if self.config.time_since_offer() > timeout:
         logger.log("Calculating new offer", log_name)
         self.config.save()
         return self.create_offer(mb_amount, timeout)
Ejemplo n.º 16
0
def update_offer():
    """
    Check if an hour as passed since the last offer made, if passed calculate a new offer.
    """
    if config.time_since_offer() > plebnet_settings.TIME_IN_HOUR:
        logger.log("Calculating new offer", log_name)
        cloudomate_controller.update_offer(config)
        config.save()
Ejemplo n.º 17
0
def calculate_price_vpn(vpn_provider='azirevpn'):
    logger.log('vpn provider: %s' % vpn_provider, "cloudomate_controller")
    # option is assumed to be the first one
    vpn_option = options(get_vpn_providers()[vpn_provider])[0]
    gateway = get_vpn_providers()[vpn_provider].get_gateway()
    btc_price = gateway.estimate_price(
        wallet_util.get_price(vpn_option.price, 'EUR'))
    return btc_price
Ejemplo n.º 18
0
def calculate_price(provider, option):
    logger.log('provider: %s option: %s' % (provider, option),
               "cloudomate_controller")
    vps_option = options(cloudomate_providers['vps'][provider])[option]

    gateway = cloudomate_providers['vps'][provider].get_gateway()
    btc_price = gateway.estimate_price(
        cloudomate.wallet.get_price(
            vps_option.price, 'USD')) + cloudomate.wallet.get_network_fee()
    return btc_price
Ejemplo n.º 19
0
def update_offer():
    """
    check if the stored prices for the selected provider should be updated.
    This does not have to happen every iteration as they do not change that often
    :return: None
    :rtype: None
    """
    if config.time_since_offer() > plebnet_settings.TIME_IN_HOUR:
        logger.log("Calculating new offer", log_name)
        cloudomate_controller.update_offer(config)
        config.save()
Ejemplo n.º 20
0
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
Ejemplo n.º 21
0
 def init_irc(self):
     try:
         logger.log("start running an IRC connection on " + self.server + " " + self.channel)
         self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         self.irc.connect((self.server, self.port))
     except:
         title = "A error occurred in IRCbot init_irc %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("A error occurred in IRCbot", sys.exc_info()[0], ['crash'])
Ejemplo n.º 22
0
def check_tribler():
    """
    Check whether Tribler is running and configured properly
    :return: True if tribler is running, False otherwise
    :rtype: Boolean
    """
    if tribler_controller.running():
        logger.log("Tribler is already running", log_name)
        return True
    else:
        tribler_controller.start()
        return False
Ejemplo n.º 23
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 qtable, config
    logger.log("Setting up PlebNet")

    # 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() + TIME_ALIVE)

    # Prepare the QTable configuration
    qtable = QTable()

    if args.test_net:
        settings.wallets_testnet("1")
        qtable.read_dictionary({
            'proxhost':
            cloudomate_controller.get_vps_providers()['proxhost']
        })
    else:
        providers = cloudomate_controller.get_vps_providers()

        if providers.has_key('proxhost'):
            del providers["proxhost"]

        # Create QTable if it does not exist
        qtable.read_dictionary(providers)

    if args.exit_node:
        logger.log("Running as exitnode")
        settings.tribler_exitnode('1')

    settings.settings.write()

    # Prepare first child configuration
    fake_generator.generate_child_account()

    # Prepare the IRC Client
    irc_handler.init_irc_client()
    irc_handler.start_irc_client()

    config.save()

    # add learning_consumer as a consumer for qtable channel in addressbook
    qtable.address_book.receiver.register_consumer("qtable", learning_consumer)

    logger.success("PlebNet is ready to roll!")
Ejemplo n.º 24
0
    def heartbeat(self):
        """
        This method sends a heartbeat to the IRC server when it is called.

        """
        timer = time.time()
        elapsed_time = timer - self.last_beat

        if elapsed_time > self.timeout:
            self.last_beat = timer
            time_str = time.strftime("%H:%M:%S", time.gmtime(timer - self.init_time))
            logger.log("IRC is still running - alive for " + time_str)
            self.send_msg("IRC is still running - alive for %s" % time_str)
Ejemplo n.º 25
0
def get_balance(domain):
    """
    Get the balance of the current wallet.
    :param domain: the wallet type BTC, TBTC or MB
    :return: the balance
    """
    logger.log('The market is running' + str(is_market_running()), "get " + domain + " balance")
    try:
        r = requests.get('http://localhost:8085/wallets/' + domain + '/balance')
        balance = r.json()
        return balance['balance']['available']
    except ConnectionError:
        return False
Ejemplo n.º 26
0
def init_irc_client(args=None):
    """
    This method initializes the IRC client by setting up the proper rights to run the files.
    :param args: The remaining commands from the commandline, can be neglected.
    :type args: String
    """

    logger.log("the IRC client is initializing", "init_irc_client")

    subprocess.call('sudo chmod +x %s' % PATH_TO_CLIENT, shell=True)
    subprocess.call('sudo chmod +x %s' % PATH_TO_DEAMON, shell=True)

    logger.log("the IRC client is initialized", "init_irc_client")
Ejemplo n.º 27
0
def calculate_price(provider, option):
    """
    Calculate the price of the chosen server to buy.
    :param provider: the provider chosen
    :param option: the option at the provider chosen
    :return: the price
    """
    logger.log('provider: %s option: %s' % (provider, option),
               "cloudomate_controller")
    vps_option = options(cloudomate_providers['vps'][provider])[option]
    gateway = cloudomate_providers['vps'][provider].get_gateway()
    btc_price = gateway.estimate_price(
        wallet_util.get_price(vps_option.price, 'USD'))
    return btc_price
Ejemplo n.º 28
0
def create_wallet():
    if settings.wallets_testnet():
        # attempt to create testnet wallet
        logger.log("create Testnet wallet", "setup")
        x = wallet_controller.create_wallet('TBTC')
        if x:
            settings.wallets_testnet_created("1")
            settings.wallets_initiate_once("1")
            settings.settings.write()
    else:
        # attempt to create bitcoin wallet
        y = wallet_controller.create_wallet('BTC')
        if y:
            settings.wallets_initiate_once("1")
            settings.settings.write()
 def apply(self):
     """
     If there are no transaction history the strategy is useless so it defaults to LastDaySell
     """
     if len(self.transactions
            ) == 0:  # Fallback to last_day_sell if can't apply strategy
         logger.log(
             "No transactions saved. Defaulting to Last Day Sell strategy",
             self.log_name)
         return LastDaySell().apply()
     self.time_accumulated += ITERATION_TIME_DIFF
     self.bid = self.sell_reputation()
     self.write_iteration_info()
     from plebnet.agent.core import attempt_purchase
     attempt_purchase()
Ejemplo n.º 30
0
    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)