Ejemplo n.º 1
0
def all(ips, actions):
    states = []
    for ip in ips:
        print ip
        initial_state = ping(str(ip))
        state = {}
        state["ip"] = ip
        state["state"] = initial_state
        state["flapping"] = 0
        states.append(state)
        State.checkComputers(ip, actions, initial_state)

    # print states
    return states
Ejemplo n.º 2
0
    def test_commit(self):
        '''
        test local portion of commit operations (create env.to_remote)
        '''

        self.args.new = True
        self.args.delete = False
        self.args.list = ['a_new_list']
        self.args.type = 'block'
        self.args.action = 'block'
        self.args.match = 'exact'
        self.args.variable = None
        self.args.block_length = None

        # create a new environment, populate it with our list
        env = Environment(self.args)
        env.mock_remote = True
        Lists(self.args, env)

        # add items to the list
        self.args.add = True
        self.args.remove = False
        self.args.clean = False
        self.args.removeall = False
        self.args.item = ['!10.0.0.0/8']
        self.args.file = None
        Items(self.args, env)

        self.args.item = ['2a04:4e42:10::313/128']
        Items(self.args, env)

        # commit to 'remote' service
        State().commit(env, 'remote')

        # ensure SERVICEID is copied into env.to_remote
        self.assertEqual(env.to_remote['service_id'], 'SERVICEID')
        # ensure snippet contains config block and logic for our list
        self.assertRegex(
            env.to_remote['snippet']['content'],
            r'\n#fastlyblocklist_list {"name": "a_new_list".*"items": \[\]}\n')
        self.assertRegex(
            env.to_remote['snippet']['content'],
            r'\n\s*if \(var\.ip ~ fastlyblocklist_a_new_list\) {\n')
        # ensure our list is converted to ACL
        self.assertEqual(env.to_remote['acls'][0]['name'],
                         'fastlyblocklist_a_new_list')
        self.assertEqual(env.to_remote['acls'][0]['items'][0]['ip'],
                         '10.0.0.0')
        self.assertEqual(env.to_remote['acls'][0]['items'][0]['negated'], '1')
        self.assertEqual(env.to_remote['acls'][0]['items'][0]['subnet'], 8)
        self.assertEqual(env.to_remote['acls'][0]['items'][1]['ip'],
                         '2a04:4e42:10::313')
        self.assertEqual(env.to_remote['acls'][0]['items'][1]['negated'], '0')
        self.assertEqual(env.to_remote['acls'][0]['items'][1]['subnet'], 128)
Ejemplo n.º 3
0
    def test_save(self):
        '''
        test create, save, and load of a config file
        '''

        # create a new config file
        Environment(self.args)

        # load existing config file, change something
        self.args.init = False
        self.args.save = True
        self.args.service = ['SERVICE1', 'SERVICE2']
        env = Environment(self.args)

        # save the changes using State().save()
        State().save(env)

        # load the modified config file
        self.args.service = []
        env = Environment(self.args)

        # ensure updated config is loaded
        self.assertEqual(len(env.config['services']), 2)
Ejemplo n.º 4
0
data = False
path = "/do/kana/www/"

"""
Get ip from database on Kana
@todo make it usable without kana
"""

# Get port
with open('/do/computers/python/port', 'r') as port_file:
    content = port_file.read()
    socket_port = int(content.strip())
Socket.Start(socket_port)
print "Socket Started"

ips, actions = State.getIP()
ip_states = Ping.all(ips, actions)

try:
    while True:
        time.sleep(sleep_between_ping)

        for i, ip_state in enumerate(ip_states):
            state, result = Ping.checkState(ip_state)
            ip_states[i] = state
            if(result):
                data = state["ip"]
                if state["state"]:
                    State.checkComputers(state["ip"], actions, 1)
                    state = "on"
                else:
Ejemplo n.º 5
0
# Get configuration
with open('/user/config/radio/radio.json') as settings_file:
    settings = json.load(settings_file)

serial_port = settings["port"]
serial_speed = int(settings["speed"])

Socket.Start(socket_port)


"""
State management
"""

codes, actions = State.getCodes()


"""
Json (to move to JsonParser)
"""


def is_json(jsonLine):
    try:
        json_object = json.loads(jsonLine)
    except ValueError, e:
        return False
    return True

"""
Ejemplo n.º 6
0
    def test_sync(self):
        '''
        test local portion of sync operations (env.from_remote to env.config)
        '''
        # create a new environment
        env = Environment(self.args)
        env.mock_remote = True
        # create an env.from_remote object
        env.from_remote = {
            'service_id':
            'REMOTESERVICEID',
            'version':
            1,
            'snippet': {
                'name':
                'REMOTE_SNIPPET_NAME',
                'type':
                'recv',
                'priority':
                10,
                'content':
                '#fastlyblocklist_list {"name": "my_test_list", '
                '"type": "block", "action_block": true, '
                '"action_log": true, "action_none": false, '
                '"match": "exact", "variable": null, '
                '"block_length": 600, "items": []}\n'
            },
            'acls': [{
                'name':
                'fastlyblocklist_my_test_list',
                'items': [{
                    'comment': '',
                    'subnet': 8,
                    'service_id': 'REMOTESERVICEID',
                    'negated': '1',
                    'deleted_at': None,
                    'ip': '10.0.0.0'
                }, {
                    'comment': '',
                    'subnet': None,
                    'service_id': 'REMOTESERVICEID',
                    'negated': '0',
                    'deleted_at': None,
                    'ip': '2a04:4e42:10::313'
                }]
            }],
            'dicts': []
        }

        # sync env.from_remote to local env.config
        State().sync(env, 'remote')

        # ensure remote made it into our local config
        self.assertEqual(env.config['services'][0]['id'], 'REMOTESERVICEID')
        self.assertEqual(env.config['services'][0]['snippet_name'],
                         'REMOTE_SNIPPET_NAME')
        self.assertEqual(env.config['lists'][0]['name'], 'my_test_list')
        self.assertEqual(env.config['lists'][0]['type'], 'block')
        self.assertTrue(env.config['lists'][0]['action_block'])
        self.assertEqual(env.config['lists'][0]['items'][0], '!10.0.0.0/8')
        self.assertEqual(env.config['lists'][0]['items'][1],
                         '2a04:4e42:10::313/128')
Ejemplo n.º 7
0
    def to_json(self):
        return json.dumps({
            'asks': self.asks,
            'bids': self.bids,
        })


def update(msg, state):
    if isinstance(msg, PlaceOrder):
        return state.place_order(msg.order)
    else:
        raise Exception("unknown message: " + str(msg))


app = Flask(__name__)
app.state = State(OrderBook.empty(), update)


@app.route('/')
def hello():
    return "Hello World!"


@app.route('/orderbook')
def orderbook():
    return Response(
        status=200,
        response=app.state.snapshot().to_json(),
        mimetype='application/json',
    )