import argparse
import os
from playback import config
from fabric.api import *
from fabric.colors import red
import sys
from playback.cli import cli_description
from playback import __version__
from playback import main



def prepare_host(user, hosts):
    from playback import prepare_host
    try:
        remote = prepare_host.PrepareHost(user, hosts)
    except AttributeError:
        print red('No hosts found. Please using --hosts param.')
        parser.print_help()
        sys.exit(1)

    # host networking
    execute(remote.setup_external_interface)

    # ntp
    execute(remote.setup_ntp)

    # openstack packages
    execute(remote.set_openstack_repository)

def gen_pass():
    os.system('openssl rand -hex 10')
    
def cmd(user, hosts, run):
    from playback import cmd
    try:
        remote = cmd.Cmd(user, hosts)
    except AttributeError:
        print red('No hosts found. Please using --hosts param.')
        sys.exit(1)
    execute(remote.cmd, run)
     
def parser():
    p = argparse.ArgumentParser(description=cli_description+'this command used for provision OpenStack basic environments')
    p.add_argument('-v', '--version', action='version', version=__version__)
    p.add_argument('--user', help='the target user', action='store', dest='user')
    p.add_argument('--hosts', help='the target address', action='store', dest='hosts')
    
    s = p.add_subparsers(dest="subparser_name", help="commands")
    
    def prepare_host_f(args):
        prepare_host(args.user, args.hosts.split(','))
    prepare_host_parser = s.add_parser('prepare-host', help='prepare the OpenStack environment')
    prepare_host_parser.set_defaults(func=prepare_host_f)
    
    def gen_pass_f(args):
        gen_pass()
    gen_pass_parser = s.add_parser('gen-pass', help='generate the password')
    gen_pass_parser.set_defaults(func=gen_pass_f)
    
    def cmd_f(args):
        cmd(args.user, args.hosts.split(','), args.run)
    cmd_parser = s.add_parser('cmd', help='run command line on the target host')
    cmd_parser.add_argument('--run', help='the command running on the remote node', action='store', default=None, dest='run')
    cmd_parser.set_defaults(func=cmd_f)
    
    return p



if __name__ == '__main__':
    main()
    
@@ -200,32 +200,37 @@
 _%0D%0Afrom playback
+.main
  import main%0D%0A%0D%0A

Fix import error