Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zxwing committed Apr 4, 2015
1 parent 9ae40a0 commit c9ef052
Show file tree
Hide file tree
Showing 342 changed files with 41,512 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
*.py[co]

# Packages
*.egg
*.egg-info
dist
build
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg
zstack.log
*~

# Installer logs
pip-log.txt

# Unit test / coverage reports
.coverage
.tox

#Translations
*.mo

#Mr Developer
.mr.developer.cfg
kvmagent/puppet/kvmpuppet/files/*
*.swp
*management-server.log
test-result
.idea/*
*.iml
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
zstack-utility
==============
18 changes: 18 additions & 0 deletions agentcli/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>agentcli</name>
<comment></comment>
<projects>
<project>zstacklib</project>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>
8 changes: 8 additions & 0 deletions agentcli/.pydevproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?><pydev_project>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/${PROJECT_DIR_NAME}</path>
</pydev_pathproperty>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
</pydev_project>
1 change: 1 addition & 0 deletions agentcli/agentcli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#
174 changes: 174 additions & 0 deletions agentcli/agentcli/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
'''
@author: frank
'''

from zstacklib.utils import http
from zstacklib.utils import shell
from zstacklib.utils import uuidhelper
import sys
import os
import optparse
import re
import readline
import traceback
import string


class Completer(object):

COMMANDS = ['file', 'cmd']
RE_SPACE = re.compile('.*\s+$', re.M)

def _listdir(self, root):
"List directory 'root' appending the path separator to subdirs."
res = []
for name in os.listdir(root):
path = os.path.join(root, name)
if os.path.isdir(path):
name += os.sep
res.append(name)
return res

def _complete_path(self, path=None):
"Perform completion of filesystem path."
if not path:
return self._listdir('.')
dirname, rest = os.path.split(path)
tmp = dirname if dirname else '.'
res = [os.path.join(dirname, p)
for p in self._listdir(tmp) if p.startswith(rest)]
# more than one match, or single match which does not exist (typo)
if len(res) > 1 or not os.path.exists(path):
return res
# resolved to a single directory, so return list of files below it
if os.path.isdir(path):
return [os.path.join(path, p) for p in self._listdir(path)]
# exact file match terminates this completion
return [path + ' ']

def complete_file(self, args):
"Completions for the 'extra' command."
if not args:
return self._complete_path('.')
# treat the last arg as a path and complete it
return self._complete_path(args[-1])

def complete(self, text, state):
"Generic readline completion entry point."
buffer = readline.get_line_buffer()
line = readline.get_line_buffer().split()
# show all commands
if not line:
return [c + ' ' for c in self.COMMANDS][state]
# account for last argument ending in a space
if self.RE_SPACE.match(buffer):
line.append('')
# resolve command to the implementation function
cmd = line[0].strip()
if cmd in self.COMMANDS:
impl = getattr(self, 'complete_%s' % cmd)
args = line[1:]
if args:
return (impl(args) + [None])[state]
return [cmd + ' '][state]
results = [c + ' ' for c in self.COMMANDS if c.startswith(cmd)] + [None]
return results[state]

class CliError(Exception):
'''Cli Error'''

class Cli(object):
def __init__(self, options):
self.options = options
self.agent_ip = options.ip
self.agent_port = options.port
self.cip = options.cip

self.http_server = http.HttpServer(port=10086)
self.http_server.register_sync_uri('/result', self.callback)
self.http_server.start_in_thread()
print ""

comp = Completer()
readline.set_completer_delims(' \t\n;')
readline.set_completer(comp.complete)
readline.parse_and_bind("tab: complete")

def callback(self, req):
print req[http.REQUEST_BODY]

def print_error(self, err):
print '\033[91m' + err + '\033[0m'

def do_command(self, line):
def from_file(tokens):
file_path = tokens[1]
file_path = os.path.abspath(file_path)
if not os.path.exists(file_path):
self.print_error('cannot find file %s' % file_path)
return

with open(file_path, 'r') as fd:
text = fd.read()

path, json_str = text.split('>>', 1)
path = path.strip(' \t\n\r')
json_str = json_str.strip(' \t\n\r')

args = {}
if len(tokens) > 2:
for token in tokens[2:]:
k, v = token.split('=', 1)
args[k] = v

tmp = string.Template(json_str)
json_str = tmp.substitute(args)
url = 'http://%s:%s/%s/' % (self.agent_ip, self.agent_port, path)
callback_url = 'http://%s:%s/%s/' % (self.cip, 10086, 'result')
rsp = http.json_post(url, json_str, headers={http.TASK_UUID:uuidhelper.uuid(), http.CALLBACK_URI:callback_url})
print rsp


def from_text(tokens):
pass

tokens = line.split()
cmd = tokens[0]
if cmd == 'file':
from_file(tokens)
elif cmd == 'call':
from_text(tokens)
else:
self.print_error('unkonwn command: %s. only "file" or "call" allowed' % cmd)

def run(self):
while True:
try:
line = raw_input('>>>')
if line:
self.do_command(line)
except CliError as clierr:
self.print_error(str(clierr))
except (EOFError, KeyboardInterrupt):
print ''
self.http_server.stop()
sys.exit(1)
except:
content = traceback.format_exc()
self.print_error(content)

def main():
parser = optparse.OptionParser()
parser.add_option("-p", "--port", dest="port", help="port for agent server")
parser.add_option("-i", "--ip", dest="ip", default='127.0.0.1', help="ip for agent server")
parser.add_option("-c", "--callback-ip", dest="cip", default='127.0.0.1', help="ip for callback http server")
(options, args) = parser.parse_args()
if not options.port:
parser.print_help()
parser.error('--port must be specified')

Cli(options).run()

if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions agentcli/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[egg_info]
tag_build = dev
tag_svn_revision = true
26 changes: 26 additions & 0 deletions agentcli/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from setuptools import setup, find_packages
import sys, os

version = '0.1'

setup(name='agentcli',
version=version,
description="cli tool for agent test",
long_description="""\
""",
classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
keywords='zstack agent cli',
author='Frank Zhang',
author_email='[email protected]',
url='http://zstack.org',
license='Apache License 2',
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
include_package_data=True,
zip_safe=True,
install_requires=[
# -*- Extra requirements: -*-
],
entry_points="""
# -*- Entry points: -*-
""",
)
18 changes: 18 additions & 0 deletions apibinding/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>apibinding</name>
<comment></comment>
<projects>
<project>zstacklib</project>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>
10 changes: 10 additions & 0 deletions apibinding/.pydevproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?>

<pydev_project>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/apibinding</path>
</pydev_pathproperty>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
</pydev_project>
1 change: 1 addition & 0 deletions apibinding/apibinding/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#
Loading

0 comments on commit c9ef052

Please sign in to comment.