Ejemplo n.º 1
0
Archivo: azure.py Proyecto: gomes-/alx
class Azure():
    def __init__(self):
        self.tbl_name = 'alxserver'
        self.tbl_net = "net"
        self.tbl_info = "info"
        self.tbl_node = "node"

        self.q_name = 'alxserver'
        self.msg_delete_ids = {self.q_name: {},
                               }
        self.msg_no_process = {self.q_name: {},
                               }

        self.msg_template = {"from": "",
                             "from-ip": "",
                             "to": "",
                             "cmd": "",
                             }

        self.msg_key_na = _('Key not available')

        self.msg_ttl = 60 * 60

        self.msg_delete_ids_ttl = 60 * 60 * 3

        try:

            key = alxlib.key.Key()
            if os.path.isfile(key.get_path()):
                sys.path.insert(0, key.get_dir())

                import alxkey

                self.key = alxkey.alxkey_azure
                self.tbl = TableService(account_name=self.key['AZURE_STORAGE_ACCOUNT_NAME'],
                                        account_key=self.key['AZURE_ACCESS_KEY'])

            else:
                raise (self.msg_key_na)
        except:
            raise (self.msg_key_na)


    # Connection
    def connect_sqs(self):
        try:
            self.q = QueueService(account_name=self.key['AZURE_STORAGE_ACCOUNT_NAME'],
                                  account_key=self.key['AZURE_ACCESS_KEY'])

            self.q.create_queue(self.q_name)
            return self.q
        except:
            logging.critical(_("Connection Failure: possibly bad key"))
            return None

    # msg
    def msg_get_all(self):
        try:

            self.connect_sqs()
            msgs = self.q.peek_messages(self.q_name, numofmessages=16)

            queue_metadata = self.q.get_queue_metadata(self.q_name)
            count = queue_metadata['x-ms-approximate-messages-count']

            logging.info("Checking queue {0}, {1} message".format(self.q_name, count))

            if count == 0:
                return None
            else:
                return msgs

        except Exception as e:
            logging.critical(_("MSG check error"))

        return None

    def msg_send(self, dict):
        try:
            self.connect_sqs()
            body = self.msg_encode(dict)
            self.q.put_message(self.q_name, body, messagettl=self.msg_ttl)
            #print(encode.decode())

        except Exception as e:
            logging.critical(_("Message creation failure: msg_send()"))

    def msg_encode(self, dict):
        try:
            j = json.dumps(dict, ensure_ascii=False)
            body = base64.b64encode(j.encode()).decode()
            return body
        except Exception as e:
            logging.critical(_("Message creation failure: msg_encode()"))

    def msg_decode(self, body):
        try:
            dict = eval(base64.b64decode(body.encode()).decode())
            return dict
        except:
            logging.critical(_("Message decode failure: msg_decode()"))
            return None

    def msg_delete(self):
        try:
            if len(self.msg_delete_ids[self.q_name]) > 0:
                self.connect_sqs()
                msgs = self.q.get_messages(self.q_name, numofmessages=16)
                for msg in msgs:
                    if self.msg_delete_ids[self.q_name].get(msg.message_id, None) is not None:
                        self.q.delete_message(self.q_name, msg.message_id, msg.pop_receipt)
                        del self.msg_delete_ids[self.q_name][msg.message_id]
                        logging.info("Deleting msg {0}".format(msg.message_id))

                for key, value in self.msg_delete_ids[self.q_name].items():
                    seconds = (
                        datetime.datetime.fromtimestamp(self.get_timestamp_now()) - datetime.datetime.fromtimestamp(
                            float(value))).seconds
                    if seconds > self.msg_delete_ids_ttl:
                        del self.msg_delete_ids[self.q_name][key]

        except:
            logging.critical(_("Message delete failure: msg_delete()"))


    def process_my_msg(self, func, msgs):
        try:
            if msgs is not None:
                for msg in msgs:
                    body = msg.message_text
                    if body is not None:
                        dict = self.msg_decode(body)
                        if dict["to"] == "*" or dict["to"] == format(socket.gethostname()):
                            if self.msg_no_process[self.q_name].get(msg.message_id, None) is None:
                                self.msg_no_process[self.q_name][msg.message_id] = dict['creation-time']
                                func(msg, dict)
                            else:
                                logging.debug("Ignore msg ...{0}".format(msg.message_id))
        except BaseException as e:
            logging.critical(_("MSG process error: process_my_msg() {0}").format(e))


    #Server
    def server_run(self):
        while True:
            try:

                self.update_net()

                """

                msgs = self.msg_get_all()
                self.process_my_msg(lambda x, y: self.server_msg_process(x, y), msgs)
                time.sleep(int(self.key["AZURE_POLL"]))

                self.msg_delete()

                for key, value in self.msg_no_process[self.q_name].items():
                    seconds = (
                    datetime.datetime.fromtimestamp(self.get_timestamp_now()) - datetime.datetime.fromtimestamp(
                        float(value))).seconds
                    if seconds > self.msg_delete_ids_ttl:
                        del self.msg_no_process[self.q_name][key]
                logging.debug("msg_no_process->{0}".format(self.msg_no_process))"""


            except Exception as e:
                logging.critical("server_run->while {0}".format(e))
                #print(e)
                raise ()

    def server_msg_process(self, msg, dict):
        try:
            if dict["cmd"] == "ping":
                logging.info("Processing ... {0}".format(msg.message_id))
                self.pong_send(dict["from"], msg.message_id)
                if dict["to"] == format(socket.gethostname()):
                    #self.q.delete_message(self.q_name, msg.message_id, msg.pop_receipt)
                    self.msg_delete_ids[self.q_name][msg.message_id] = dict['creation-time']


        except BaseException as e:
            logging.critical(_("MSG process error: server_cmd() {0}").format(e))

    #Client
    def client_print(self):
        try:
            msgs = self.msg_get_all()
            self.process_my_msg(lambda x, y: self.client_msg_process(x, y), msgs)
            self.msg_delete()
            #time.sleep(5)
        except:
            raise ()

    def client_msg_process(self, msg, dict):
        try:
            if dict["cmd"] == "pong":
                self.msg_delete_ids[self.q_name][msg.message_id] = dict['creation-time']
                #self.q.delete_message(self.q_name, msg.message_id, msg.pop_receipt)
                import datetime

                print("reply\t\t{0}\t\t{1}\t\t{2}".format(dict["from"],
                                                          dict["from-ip"],
                                                          self.get_time(float(dict['creation-time']))
                                                          ))
                logging.debug("client_msg_process creation-time {0}".format(dict['creation-time']))

                #print(self.get_time(msg.attributes['ApproximateFirstReceiveTimestamp']))
                #print(datetime.datetime.fromtimestamp(time.time(int(msg.attributes["ApproximateFirstReceiveTimestamp"]))).strftime('%Y-%m-%d %H:%M:%S'))


        except BaseException as e:
            logging.critical(_("MSG process error: client_msg_process() {0}").format(e))


    #cmd
    def ping(self, count, timeout):

        self.connect_sqs()
        print(_("Sending ping ..."))
        self.ping_send(count)
        print(_("Waiting for reply ..."))
        time.sleep(timeout)
        self.client_print()
        print(_("Timeout"))

    def ping_send(self, count):
        try:

            import copy, alxlib.node.info

            dict = copy.deepcopy(self.msg_template)
            dict["from"] = format(socket.gethostname())
            dict["from-ip"] = alxlib.node.info.Info().get_ip()
            dict["to"] = "*"
            dict["cmd"] = "ping"
            dict["creation-time"] = str(self.get_timestamp_now())

            for i in range(0, count):
                self.msg_send(dict)

        except Exception as e:
            logging.critical(_("Message creation failure: ping_send()"))

    def pong_send(self, to, replyToId):
        try:

            import copy, alxlib.node.info

            dict = copy.deepcopy(self.msg_template)
            dict["from"] = format(socket.gethostname())
            dict["from-ip"] = alxlib.node.info.Info().get_ip()
            dict["to"] = to
            dict["reply-to-id"] = replyToId
            dict["cmd"] = "pong"
            dict['creation-time'] = str(self.get_timestamp_now())

            self.msg_send(dict)

        except:
            logging.critical(_("Message creation failure"))


    #helper

    def get_time(self, timestamp):
        try:
            return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))
        except:
            return ""

    def get_timestamp_now(self):
        return time.time()

    #table

    def tbl_update(self, name, p, r, d):
        try:
            d["updatetime"] = str(self.get_timestamp_now())
            self.tbl.create_table(name)
            self.tbl.insert_or_merge_entity(name, p, r, d)
        except Exception as e:
            logging.critical("Error update_tbl {0}".format(e))


    def tbl_row_query(self, name, q, n=1000, next_partition_key_=None, next_row_key_=None):
        try:
            self.tbl.create_table(name)

            rows = self.tbl.query_entities(name, filter=q, top=n,
                                           next_partition_key=next_partition_key_,
                                           next_row_key=next_row_key_)
            return rows
        except Exception as e:
            return None

    def entity2dict(self, e):
        try:
            keys = dir(e)
            d = {}
            for key in keys:
                d[key] = getattr(e, key, "")
            return d
        except:
            return None

    def update_net(self):
        try:
            info = Info().get_all()
            self.tbl_update(self.tbl_name, self.tbl_net, info["hostname"], info)
        except:
            logging.warning("Error update_net")

    def wrap_text(self, text, max_width):
        if text is not None:
            from textwrap import wrap

            return '\n'.join(wrap(text, max_width))
        else:
            return ""


    def print_list(self):
        try:
            rows = self.tbl_row_query(self.tbl_name, "PartitionKey eq 'net'")
            #print(dir(row))

            from colorclass import Color, Windows

            from terminaltables import AsciiTable

            Windows.enable(auto_colors=True, reset_atexit=True)

            table_data = [[Color('{autocyan}Hostname'),
                           Color('{autocyan}Last Reply'),
                           Color('{autocyan}IP'),
                           Color('{autocyan}OS'),
                           Color('{autocyan}OS Release'),
                           Color('{autocyan}OS Version'),
                           Color('{autocyan}System'),
                           Color('{autocyan}Processor'),
                           ]
                          ]

            max_wrap=10

            for row in rows:
                #d = self.entity2dict(row)
                d = row.__dict__
                time = alxlib.time_help.format_from_timestamp(d['Timestamp'])
                li = [d['hostname'], time, d['ip'], d['os'], d['osrelease'],
                      self.wrap_text(d['osversion'], max_wrap), d["system"],
                      self.wrap_text(d["processor"], max_wrap), ]
                table_data.append(li)

            table = AsciiTable(table_data)

            table.table_data = table_data

            print(table.table)
        except Exception as e:
            logging.warning("Error print_list")
            print(e)
import time
from helperFunctions import *

from azure.storage import Entity, QueueService

#import spidev CHANGE THIS BACK ON FOR PI DEPLOYMENT

#spi = spidev.SpiDev()
#spi.open(0,0)

myaccount = getAccount()
mykey = getKey()

queue_service = QueueService(account_name=myaccount, account_key=mykey)

queue_service.create_queue('mlqueue')

i = 0

periods = ('a', 'b', 'c', 'd')


#def analog_read(channel):
#        r = spi.xfer2([1, (8 + channel) << 4, 0])
#        adc_out = ((r[1]&3) << 8) + r[2]
#        return adc_out

#a_r = analog_read


a_r = generateRandom
Ejemplo n.º 3
0
import time
import redis
import spidev
from tokens import *


spi = spidev.SpiDev()
spi.open(0,0)

myaccount = getAccount()
mykey = getKey()

#table_service = TableService(account_name=myaccount, account_key=mykey)
queue_service = QueueService(account_name=myaccount, account_key=mykey)

queue_service.create_queue('acceldata')

i = 0

#TableSlotList = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, $

periods = ('a', 'b', 'c', 'd')

#table_service.insert_or_replace_entity(accel2, accel, tableSlot, periodSlot)

record = {}

#records = {'aX': generateX(),'aY': generateY(),'aZ': generateZ(),'bX': generateX(),'bY': generateY(),'bZ': generateZ(), 'cX': generateX(),'$

def analog_read(channel):
        r = spi.xfer2([1, (8 + channel) << 4, 0])
from azure.storage import TableService, Entity, QueueService
import time
import redis
from tokens import *
from helperFUNctions import *

myaccount = getAccount()
mykey = getKey()

table_service = TableService(account_name=myaccount, account_key=mykey)
queue_service = QueueService(account_name=myaccount, account_key=mykey)

queue_service.create_queue(getQueueName())

i = 0

TableSlotList = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

periods = ("a", "b", "c", "d")

# table_service.insert_or_replace_entity(accel2, accel, tableSlot, periodSlot)

record = {}


def analog_read(channel):
    if channel == 0:
        return generateRandom("x")
    if channel == 1:
        return generateRandom("y")
    if channel == 2:
Ejemplo n.º 5
0
class TodoList(object):

    def __init__(self, initdir=None):

        self.queue_service = QueueService(account_name='barttuesday2', account_key='djfmQs5bJjtA7Oinbzf5vn89iQf6EGdP6DTNAIS30Qc9xqllP1ifgJFtWE8YHs5SobyOl5Kk1xepaPbbyagNPg==')
        self.sw_queue = self.queue_service.create_queue('swqueue')

        self.top = Tk()
        self.label = Label(self.top, text='my Messages')
        self.label.pack()

        self.cwd = StringVar(self.top)

        self.dirl = Label(self.top, fg='blue', font=('Helvietica', 12, 'bold'))
        self.dirl.pack()

        self.dirfm = Frame(self.top)
        self.dirsb = Scrollbar(self.dirfm)
        self.dirsb.pack(side=RIGHT, fill=Y)

        self.dirs = Listbox(self.dirfm, height=35, 
                width=100, yscrollcommand=self.dirsb.set)
        self.dirs.bind('<Double-1>', self.setDirAndGo)
        self.dirsb.config(command=self.dirs.yview)
        self.dirs.pack(side=LEFT, fill=BOTH)
        self.dirfm.pack()

        self.dirn = Entry(self.top, width=50,
                textvariable=self.cwd)
        self.dirn.bind('<Return>', self.doLS);
        self.dirn.pack()

        self.bfm = Frame(self.top)
        self.clr = Button(self.bfm, text='Clear',
                command=self.clrDir,
                activeforeground='white',
                activebackground='blue')

        self.ls = Button(self.bfm,
                text='List Messages',
                command=self.doLS,
                activeforeground='white',
                activebackground='green')

        self.sm = Button(self.bfm,
                text='Send Message',
                command=self.doSM,
                activeforeground='white',
                activebackground='green')

        self.quit = Button(self.bfm, text='Quit',
                command=self.top.quit,
                activeforeground='white',
                activebackground='red')

        self.clr.pack(side=LEFT)
        self.sm.pack(side=LEFT)
        self.ls.pack(side=LEFT)
        self.quit.pack(side=LEFT)
        self.bfm.pack()

        if initdir:
            self.cwd.set(os.curdir)
            self.doLS()

    def clrDir(self, ev=None):
        self.dirs.delete(0, END)
        self.cwd.set('')
        self.queue_service.clear_messages('swqueue')
        self.queue_service.put_message('swqueue', 'learn queues, tables and blobs on azure')
        self.queue_service.put_message('swqueue', 'research DRM format and implement in dbg ext')
        self.queue_service.put_message('swqueue', 'buy island in France')

    def setDirAndGo(self, ev=None):
        top = Toplevel()
        top.title("About this application...")
        msg = Message(top, text="message details")
        msg.pack()
        button = Button(top, text="Dismiss", command=top.destroy)
        button.pack()

        pass

    def doSM(self, ev=None):
        msg = self.dirn.get()
        print("put message ", msg);
        self.queue_service.put_message('swqueue', msg)

    def doLS(self, ev=None):
        result = self.queue_service.get_messages('swqueue', numofmessages=7)
        print("Number of messages: ", len(result))
        for message in result:
            msg = message.insertion_time + ": " + message.message_text
            self.dirs.insert(END, msg)
            #self.queue_service.update_message('swqueue', message.message_id, message.message_text, visibilitytimeout=0, )
        self.dirs.config(selectbackground='LightSkyBlue')
import time
from helperFUNctions import *

from azure.storage import Entity, QueueService

# import spidev CHANGE THIS BACK ON FOR PI DEPLOYMENT

# spi = spidev.SpiDev()
# spi.open(0,0)

myaccount = getAccount()
mykey = getKey()

queue_service = QueueService(account_name=myaccount, account_key=mykey)

queue_service.create_queue("mlqueue")

i = 0

periods = ("a", "b", "c", "d")


# def analog_read(channel):
#        r = spi.xfer2([1, (8 + channel) << 4, 0])
#        adc_out = ((r[1]&3) << 8) + r[2]
#        return adc_out

# a_r = analog_read


a_r = generateRandom