Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pacogomez committed Dec 9, 2017
1 parent 83d2d77 commit 503972c
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 129 deletions.
2 changes: 2 additions & 0 deletions test-requirements.txt
Expand Up @@ -3,3 +3,5 @@ flake8
hacking
nose
nose-testconfig
yapf

149 changes: 71 additions & 78 deletions vsphere_guest_run/vgr.py
Expand Up @@ -12,7 +12,6 @@
from tabulate import tabulate
from vsphere_guest_run.vsphere import VSphere


CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])


Expand All @@ -21,32 +20,30 @@ def abort_if_false(ctx, param, value):
ctx.abort()


@click.group(context_settings=CONTEXT_SETTINGS,
invoke_without_command=True)
@click.group(context_settings=CONTEXT_SETTINGS, invoke_without_command=True)
@click.pass_context
@click.option('-d',
'--debug',
is_flag=True,
default=False,
help='Enable debug')
@click.option('-u',
'--url',
metavar='<user:pass@host>',
required=True,
envvar='VGR_URL',
help='ESXi or vCenter URL')
@click.option('-s/-i',
'--verify-ssl-certs/--no-verify-ssl-certs',
required=False,
default=True,
help='Verify SSL certificates')
@click.option('-w',
'--disable-warnings',
is_flag=True,
required=False,
default=False,
help='Do not display warnings when not verifying SSL ' +
'certificates')
@click.option(
'-d', '--debug', is_flag=True, default=False, help='Enable debug')
@click.option(
'-u',
'--url',
metavar='<user:pass@host>',
required=True,
envvar='VGR_URL',
help='ESXi or vCenter URL')
@click.option(
'-s/-i',
'--verify-ssl-certs/--no-verify-ssl-certs',
required=False,
default=True,
help='Verify SSL certificates')
@click.option(
'-w',
'--disable-warnings',
is_flag=True,
required=False,
default=False,
help='Do not display warnings when not verifying SSL ' + 'certificates')
def vgr(ctx, debug, url, verify_ssl_certs, disable_warnings):
"""vSphere Guest Run
Expand Down Expand Up @@ -85,10 +82,13 @@ def vgr(ctx, debug, url, verify_ssl_certs, disable_warnings):
if disable_warnings:
pass
else:
click.secho('InsecureRequestWarning: '
'Unverified HTTPS request is being made. '
'Adding certificate verification is strongly '
'advised.', fg='yellow', err=True)
click.secho(
'InsecureRequestWarning: '
'Unverified HTTPS request is being made. '
'Adding certificate verification is strongly '
'advised.',
fg='yellow',
err=True)
requests.packages.urllib3.disable_warnings()
tokens = url.split(':')
vc_user = tokens[0]
Expand All @@ -99,18 +99,14 @@ def vgr(ctx, debug, url, verify_ssl_certs, disable_warnings):
if len(vc_password) > 0:
vc_password += '@'
vc_password += token
vs = VSphere(vc_host,
vc_user,
vc_password,
verify=verify_ssl_certs)
vs = VSphere(vc_host, vc_user, vc_password, verify=verify_ssl_certs)
ctx.obj = {}
ctx.obj['vs'] = vs


@vgr.command(short_help='show info')
@click.pass_context
@click.argument('vm-moid',
required=False)
@click.argument('vm-moid', required=False)
def info(ctx, vm_moid):
"""Show info"""
vs = ctx.obj['vs']
Expand All @@ -120,11 +116,10 @@ def info(ctx, vm_moid):
click.secho('%s' % vs.service_instance.content.about)
else:
vm = vs.get_vm_by_moid(vm_moid)
click.echo(highlight(json.dumps(vs.vm_to_dict(vm),
indent=4,
sort_keys=True),
lexers.JsonLexer(),
formatters.TerminalFormatter()))
click.echo(
highlight(
json.dumps(vs.vm_to_dict(vm), indent=4, sort_keys=True),
lexers.JsonLexer(), formatters.TerminalFormatter()))


@vgr.command(short_help='show version')
Expand All @@ -136,20 +131,17 @@ def version(ctx):


def print_command(cmd, level=0):
click.echo(' '+(' '*level*2)+' ', nl=False)
click.echo(' ' + (' ' * level * 2) + ' ', nl=False)
click.echo(cmd.name)
if type(cmd) == click.core.Group:
for k in sorted(cmd.commands.keys()):
print_command(cmd.commands[k], level+1)
print_command(cmd.commands[k], level + 1)


@vgr.command(short_help='show help')
@click.pass_context
@click.option('-t',
'--tree',
is_flag=True,
default=False,
help='show commands tree')
@click.option(
'-t', '--tree', is_flag=True, default=False, help='show commands tree')
def help(ctx, tree):
"""Show help"""
if tree:
Expand All @@ -160,28 +152,29 @@ def help(ctx, tree):

@vgr.command(short_help='run command in guest')
@click.pass_context
@click.argument('vm_moid',
metavar='<vm-moid>',
envvar='VGR_VM_MOID')
@click.option('guest_user',
'-g',
'--guest-user',
metavar='<guest-user>',
envvar='VGR_GUEST_USER',
help='Guest OS user name')
@click.option('guest_password',
'-p',
'--guest-password',
metavar='<guest-password>',
envvar='VGR_GUEST_PASSWORD',
help='Guest OS password')
@click.option('rm_cmd',
'-r',
'--rm',
default='/bin/rm',
metavar='<rm-cmd>',
envvar='VGR_RM_CMD',
help='rm cmd')
@click.argument('vm_moid', metavar='<vm-moid>', envvar='VGR_VM_MOID')
@click.option(
'guest_user',
'-g',
'--guest-user',
metavar='<guest-user>',
envvar='VGR_GUEST_USER',
help='Guest OS user name')
@click.option(
'guest_password',
'-p',
'--guest-password',
metavar='<guest-password>',
envvar='VGR_GUEST_PASSWORD',
help='Guest OS password')
@click.option(
'rm_cmd',
'-r',
'--rm',
default='/bin/rm',
metavar='<rm-cmd>',
envvar='VGR_RM_CMD',
help='rm cmd')
@click.argument('command')
def run(ctx, vm_moid, guest_user, guest_password, command, rm_cmd):
try:
Expand All @@ -191,14 +184,14 @@ def run(ctx, vm_moid, guest_user, guest_password, command, rm_cmd):
pass
vm = vs.get_vm_by_moid(vm_moid)
result = vs.execute_program_in_guest(
vm,
guest_user,
guest_password,
command,
wait_for_completion=True,
wait_time=1,
get_output=True,
rm_cmd=rm_cmd)
vm,
guest_user,
guest_password,
command,
wait_for_completion=True,
wait_time=1,
get_output=True,
rm_cmd=rm_cmd)
stdout = result[1].content.decode()
stderr = result[2].content.decode()
if len(stderr) > 0:
Expand Down

0 comments on commit 503972c

Please sign in to comment.