Esempio n. 1
0
def doit(args):
    if len(args) < 2:
        print(
            'usage: python runcommand.py nameofcommand [nameofminer] [commandparameter]'
        )
        APP.shutdown(1)

    cmd = args[1]
    if len(args) == 2:
        #single command, no miner specified
        if cmd == 'alert':
            APP.trybroadcast(
                cmd, '{0}: runcommand called on {1}'.format(APP.now(), cmd))
        else:
            APP.trypublish(
                cmd, '{0}: runcommand called on {1}'.format(APP.now(), cmd))
        print('sent command {0}'.format(cmd))
    else:
        minertofind = args[2]
        miner = findminerbyname(minertofind)
        cmdparam = ''
        if len(args) > 3:
            cmdparam = args[3]
        if not QueueName.has_value(cmd):
            print('Queue {0} is not valid'.format(cmd))
            sys.exit(1)

        if cmd:
            qmess = MinerCommand(cmd, cmdparam)
            msg = APP.createmessagecommand(miner, qmess)
            APP.bus.publish(queue_name=cmd, msg=msg)
            print('sent command {0} for miner {1}'.format(cmd, miner.name))
Esempio n. 2
0
 def make_minermessage(self, data):
     '''reconstitute a minermessage'''
     miner = None
     command = None
     minerstats = None
     minerpool = None
     if 'miner' in data:
         #miner comes in as dict instead of entity when there is an error in the schema
         if isinstance(data['miner'], Miner):
             miner = data['miner']
         else:
             miner = Miner(**data['miner'])
     if 'command' in data:
         if isinstance(data['command'], MinerCommand):
             command = data['command']
         else:
             command = MinerCommand(**data['command'])
     if 'minerstats' in data:
         #minerstats = MinerStatistics(Miner=miner,**data['minerstats'])
         minerstats = data['minerstats']
     if 'minerpool' in data:
         #minerpool = MinerCurrentPool(Miner=miner, **data['minerpool'])
         minerpool = data['minerpool']
     entity = MinerMessage(miner, command, minerstats, minerpool)
     return entity
Esempio n. 3
0
def add_entry_for_rule(entries, rule):
    cmd_restart = MinerCommand('restart', '')
    if rule.action == 'alert':
        entries.addalert(RULES.addalert(RULES.app.stamp(rule.parameter)))
    elif rule.action == 'restart':
        entries.add(QueueName.Q_RESTART,
                    RULES.app.createmessagecommand(rule.miner, cmd_restart))
        entries.addalert(
            RULES.addalert(
                RULES.app.stamp('Restarted {0}'.format(rule.miner.name))))
    else:
        RULES.app.logerror('did not process broken rule {0}'.format(
            rule.parameter))
Esempio n. 4
0
 def test_minermessage(self):
     sch = messaging.messages.MinerMessageSchema()
     entity = messaging.messages.MinerMessage(self.make_miner())
     entity.command = MinerCommand('test', 'test')
     entity.minerpool = MinerCurrentPool(entity.miner,
                                         'test pool',
                                         'test worker',
                                         allpools={})
     entity.minerstats = domain.minerstatistics.MinerStatistics(
         entity.miner, datetime.datetime.utcnow(), 0, 1, 0, 99, 98, 97, 123,
         '', '', '')
     j = sch.dumps(entity).data
     reentity = sch.loads(j).data
     self.assertTrue(isinstance(reentity, messaging.messages.MinerMessage))
Esempio n. 5
0
def doit(args):
    if len(args) < 2:
        print('usage: python runcommand.py nameofcommand [nameofminer] [commandparameter]')
        APP.shutdown(1)

    cmd = args[1]
    if len(args) == 2:
        #single command, no miner specified
        queue_command = Queue(cmd, APP.getservice('rabbit'))
        queue_command.publish('{0} runcommand called on {1}'.format(APP.now(), cmd))
        queue_command.close()
        print('sent command {0}'.format(cmd))
    else:
        minertofind = args[2]
        cmdparam = ''
        if len(args) > 3:
            cmdparam = args[3]
        miners = MinerRepository()
        miner = miners.getminerbyname(minertofind, APP.getconfigfilename('config/miners.conf'))
        if miner is None:
            miner = APP.getknownminerbyname(minertofind)
        if miner is None:
            miner = APP.getminer(Miner(name=minertofind))
        if miner is None:
            print('Miner {0} does not exist'.format(minertofind))
            sys.exit(1)
        qnames = QueueName()
        if not qnames.isvalidqname(cmd):
            print('Queue {0} is not valid'.format(cmd))
            sys.exit(1)
        queue_command = Queue(cmd, APP.getservice('rabbit'))
        #TODO: cleanup logic here. when to call app and when to override with just miner and command
        if cmd:
            qmess = MinerCommand(cmd, cmdparam)
            msg = APP.createmessagecommand(miner, qmess)
            queue_command.publish(msg)
        else:
            queue_command.publish(APP.messageencode(miner))
        queue_command.close()
        print('sent command {0} for miner {1}'.format(cmd, miner.name))
Esempio n. 6
0
def rules(miner, minerstats, minerpool):
    '''this runs the rules'''
    entries = QueueEntries()
    if miner is None or minerstats is None:
        return entries
    savedminer = RULES.app.getminer(miner)
    cmd_restart = MinerCommand('restart', '')
    broken = []
    for ruleparms in RULES.app.ruleparameters():
        rule = MinerStatisticsRule(savedminer, minerstats, ruleparms)
        if rule.isbroken():
            broken += rule.brokenrules

    if broken:
        #TODO: could raise broken rule event???
        for rule in broken:
            log = MinerLog()
            log.createdate = datetime.datetime.utcnow()
            log.minerid = rule.miner.key()
            log.minername = rule.miner.name
            log.action = rule.parameter
            RULES.app.log_mineractivity(log)

            if rule.action == 'alert':
                entries.addalert(
                    RULES.addalert(RULES.app.stamp(rule.parameter)))
            elif rule.action == 'restart':
                entries.add(
                    QueueName.Q_RESTART,
                    RULES.app.createmessagecommand(rule.miner, cmd_restart))
                entries.addalert(
                    RULES.addalert(
                        RULES.app.stamp('Restarted {0}'.format(
                            rule.miner.name))))
            else:
                RULES.app.logerror('did not process broken rule {0}'.format(
                    rule.parameter))

    return entries
Esempio n. 7
0
 def make_command(self, data):
     return MinerCommand(**data)
Esempio n. 8
0
 def test_minercommand(self):
     sch = messaging.schema.MinerCommandSchema()
     cmd = MinerCommand()
     j = sch.dumps(cmd).data
     recommand = sch.loads(j).data
     self.assertTrue(isinstance(recommand, MinerCommand))
Esempio n. 9
0
 def test_miner_command(self):
     command = MinerCommand()
     self.assertTrue(command.command == '')
     self.assertTrue(command.parameter == '')