forked from zstackio/zstack-utility
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
342 changed files
with
41,512 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
zstack-utility | ||
============== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[egg_info] | ||
tag_build = dev | ||
tag_svn_revision = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: -*- | ||
""", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# |
Oops, something went wrong.