From 2a69a78d58f5845d09ced41d8c1646bb516f3ba8 Mon Sep 17 00:00:00 2001 From: yaowei Date: Tue, 22 Dec 2015 23:32:36 +0800 Subject: [PATCH] Init agent, common, unittest. --- .gitignore | 4 ++ setup.py | 28 ++++++++++ stetho/agent/__init__.py | 0 stetho/agent/agent.py | 77 +++++++++++++++++++++++++++ stetho/agent/common/__init__.py | 0 stetho/agent/common/utils.py | 31 +++++++++++ stetho/common/__init__.py | 0 stetho/common/log.py | 28 ++++++++++ tests/__init__.py | 0 tests/unit/__init__.py | 0 tests/unit/agent/__init__.py | 0 tests/unit/agent/common/__init__.py | 0 tests/unit/agent/common/test_utils.py | 38 +++++++++++++ tests/unit/agent/test_agent.py | 14 +++++ tests/unit/common/__init__.py | 0 tests/unit/common/test_log.py | 28 ++++++++++ 16 files changed, 248 insertions(+) create mode 100644 .gitignore create mode 100644 setup.py create mode 100644 stetho/agent/__init__.py create mode 100644 stetho/agent/agent.py create mode 100644 stetho/agent/common/__init__.py create mode 100644 stetho/agent/common/utils.py create mode 100644 stetho/common/__init__.py create mode 100644 stetho/common/log.py create mode 100644 tests/__init__.py create mode 100644 tests/unit/__init__.py create mode 100644 tests/unit/agent/__init__.py create mode 100644 tests/unit/agent/common/__init__.py create mode 100644 tests/unit/agent/common/test_utils.py create mode 100644 tests/unit/agent/test_agent.py create mode 100644 tests/unit/common/__init__.py create mode 100644 tests/unit/common/test_log.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c5b5e3a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.pyc +stetho.egg-info/ +build/ +dist/ diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..5fd5c25 --- /dev/null +++ b/setup.py @@ -0,0 +1,28 @@ +# Copyright 2015 UnitedStack, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from setuptools import setup, find_packages + +setup(name='stetho', + version="0.1.0", + packages = find_packages(), + zip_safe = False, + description = "stetho", + author = "UnitedStackSDN", + author_email = "unitedstack-sdn@googlegroups.com", + license = "APL", + keywords = ("stetho", "egg"), + platforms = "Independant", + url = "https://www.ustack.com") diff --git a/stetho/agent/__init__.py b/stetho/agent/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stetho/agent/agent.py b/stetho/agent/agent.py new file mode 100644 index 0000000..8ab4e02 --- /dev/null +++ b/stetho/agent/agent.py @@ -0,0 +1,77 @@ +# Copyright 2015 UnitedStack, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import os +import sys +import zerorpc + +# Listening endpoint +LISTEN_PROTOCOL = 'tcp' +LISTEN_ADDR = '0.0.0.0' +LISTEN_PORT = '9698' + + +class AgentApi(object): + """Agent Api. + """ + + def __init__(self): + pass + + def check_ports_on_br(self, bridge, ports=[]): + """ovs-vsctl list-ports bridge + """ + pass + + def ping(self, ips, boardcast=False, + count=2, timeout=2, interface=None): + """ping host -c 2 -W 2 + packet loss + """ + pass + + def add_vlan_to_interface(self, interface, vlan_id): + """ip link add link + """ + pass + + def get_interface_info(self, interface): + """ifconfig + """ + pass + + def setup_link(self, interface, cidr): + """IP addr and ifup + check net_cidr + 10.0.0.0/24 -> 10.0.0.64/24 + """ + pass + + def teardown_link(self, interface): + """ip link + """ + pass + + +def main(): + # log + args = sys.argv[1:] + s = zerorpc.Server(AgentApi()) + endpoint = '%s://%s:%s' % (LISTEN_PROTOCOL, LISTEN_ADDR, LISTEN_PORT) + s.bind(endpoint) + s.run() + +if __name__ == '__main__': + main() diff --git a/stetho/agent/common/__init__.py b/stetho/agent/common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stetho/agent/common/utils.py b/stetho/agent/common/utils.py new file mode 100644 index 0000000..384b409 --- /dev/null +++ b/stetho/agent/common/utils.py @@ -0,0 +1,31 @@ +# Copyright 2015 UnitedStack, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import time +import shlex +import subprocess +from stetho.common import log + +log = log.get_logger('/opt/stack/logs/stetho-agent.log') + + +def execute(cmd, time=None, shell=False): + try: + log.info(cmd) + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=shell) + out = p.stdout.readlines() + return [line.strip() for line in out] + except Exception as e: + log.exception(e) diff --git a/stetho/common/__init__.py b/stetho/common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stetho/common/log.py b/stetho/common/log.py new file mode 100644 index 0000000..ea239fd --- /dev/null +++ b/stetho/common/log.py @@ -0,0 +1,28 @@ +# Copyright 2015 UnitedStack, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import logging + +FORMAT = '%(asctime)s %(filename)s %(levelname)s %(message)s' +DATEFMT = '%d %b %Y %H:%M:%S' + + +def get_logger(filename, format=FORMAT, + datefmt=DATEFMT, filemod='a+', + level=logging.DEBUG): + logging.basicConfig(level=level, format=format, datefmt=datefmt, + filename=filename, filemod=filemod) + log = logging.getLogger() + return log diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/agent/__init__.py b/tests/unit/agent/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/agent/common/__init__.py b/tests/unit/agent/common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/agent/common/test_utils.py b/tests/unit/agent/common/test_utils.py new file mode 100644 index 0000000..db207b7 --- /dev/null +++ b/tests/unit/agent/common/test_utils.py @@ -0,0 +1,38 @@ +# Copyright 2015 UnitedStack, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import unittest + +from stetho.agent.common import utils + + +class TestUtils(unittest.TestCase): + + def setUp(self): + self.test_file = self.get_temp_file_path('test_execute.tmp') + open(self.test_file, 'w+').close() + + def test_execute(self): + expected = "%s\n" % self.test_file + result = utils.execute(["ls", self.test_file]) + self.assertEqual(result.pop(), expected.strip('\n')) + + def get_temp_file_path(self, filename, root=None): + root = '/tmp/%s' + return root % filename + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/unit/agent/test_agent.py b/tests/unit/agent/test_agent.py new file mode 100644 index 0000000..48c2e79 --- /dev/null +++ b/tests/unit/agent/test_agent.py @@ -0,0 +1,14 @@ +# Copyright 2015 UnitedStack, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. diff --git a/tests/unit/common/__init__.py b/tests/unit/common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/common/test_log.py b/tests/unit/common/test_log.py new file mode 100644 index 0000000..5054ed5 --- /dev/null +++ b/tests/unit/common/test_log.py @@ -0,0 +1,28 @@ +# Copyright 2015 UnitedStack, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import unittest +import logging + +from stetho.common import log + + +class TestLog(unittest.TestCase): + def test_get_logger(self): + log_test = log.get_logger(filename='test') + self.assertEqual(type(log_test), type(logging.getLogger())) + +if __name__ == '__main__': + unittest.main()