コード例 #1
0
def main():
    """Commandline utility for configuring Cyclozzo Nodes.
    """
    # path to the master configuration file
    config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 
                               'settings.yaml')
    config = ApplicationConfiguration(config_file)
    fabfile.update_roles(config)

    # define commandline options
    usage = 'cyclozzo-lite [options]'
    parser = OptionParser(usage)
    parser.add_option('-e', '--exchange-keys', action='store_true', 
                      default=False,
                      help='Exchange SSH keys between Master and Slaves',
                      dest='exchange_keys')
    parser.add_option('-t', '--settings', action='store_true', default=False,
                      help='Configuration file for Cyclozzo node settings.')
    parser.add_option('-c', '--configure', action='store_true', default=False,
                      help='Configure Cyclozzo Master/Client Services')
    parser.add_option('-f', '--format', default=False, dest='format',
                      help='Format DFS and DS. Options: [dfs, ds, all]',
                      choices=['ds', 'dfs', 'all'])
    parser.add_option('-s', '--start', default=False, dest='start',
                      help='Start Cyclozzo Cluster/Application. ' \
                      'Options: [cluster, application]',
                      choices=['cluster', 'application'])
    parser.add_option('-k', '--stop', default=False, dest='stop',
                      help='Stop Cyclozzo Cluster/Application Services. ' \
                      'Options: [cluster, application]',
                      choices=['cluster', 'application'])
    parser.add_option('--status', default=False, dest='status',
                      help='List Cluster/Application status. ' \
                      'Options: [cluster, application]',
                      choices=['cluster', 'application'])

    parser.add_option('--dir',  help='Application Directory', dest='app_dir')
    parser.add_option('--port', help='Listen port number', type='int')
    parser.add_option('--debug', action='store_true', default=False,
                        help='Run the application in foreground')


    # parse the options
    (options, args) = parser.parse_args()

    if options.exchange_keys:
        fabfile.exchange_keys()

    elif options.settings:
        print config_file

    elif options.configure:
        if not config.slaves:
            config.slaves=[]
        # configure hadoop and hypertable on (this) master
        configure_hadoop(primary=config.master, 
                         secondary=config.slaves)
        configure_hypertable(primary=config.master, 
                             secondary=config.slaves)
        # configure Redis for pubsub
        configure_redis()
        fabfile.rsync()
        if options.format == 'dfs' or options.format == 'all':
            fabfile.start_hadoop_namenode()
            fabfile.start_hadoop_datanode()
            fabfile.format_hadoop_datanode()
            fabfile.format_hadoop_namenode()
            fabfile.stop_hadoop_datanode()
            fabfile.stop_hadoop_namenode()
            print '--> Waiting for Name/Datanode to stop (5 secs)'
            sleep(5)
        if options.format == 'ds' or options.format == 'all':
            fabfile.start_hadoop_namenode()
            fabfile.start_hadoop_datanode()
            fabfile.start_dfsbrokers()
            fabfile.format_hypertable()
            fabfile.stop_hadoop_datanode()
            fabfile.stop_hadoop_namenode()
            print '--> Waiting for Name/Datanode to stop (5 secs)'
            sleep(5)
                    
    elif options.start == 'cluster':
        fabfile.start()

    elif options.stop == 'cluster':
        fabfile.stop()

    elif options.start == 'application':
        if not options.app_dir or not options.port:
            print 'Missing arguments: --app_dir, --port'
            parser.print_help()
        else:
            print 'Starting application from %s on port %d' % \
                            (options.app_dir, options.port)
            appserver_yaml = os.path.join(
                                          os.path.dirname(                                                     
                                                    os.path.abspath(
                                                    appserver.__file__
                                                    )
                                                          ), 
                                          'appserver.yaml')
            appserver_yaml = ApplicationConfiguration(appserver_yaml)
            if options.debug:
                appserver_yaml.debug_mode = True
            daemon = AppDaemon(appserver_yaml, 
                               options.app_dir, 
                               None, 
                               options.port, 
                               None, 
                               None
                               )
            if options.debug:
                daemon.run_application()
            else:
                daemon.start()

    elif options.stop == 'application':
        if not options.app_dir:
            print 'Missing arguments: --app_dir'
            parser.print_help()
        else:
            pid = get_pid(options.app_dir)
            if pid == 0:
                print 'No instance running'
                sys.exit(1)
            print 'Stopping instance with pid %d' % pid
            os.kill(pid, signal.SIGTERM)

    elif options.status == 'application':
        if not options.app_dir:
            print 'Missing arguments: --app_dir'
            parser.print_help()
        pid = get_pid(options.app_dir)
        running = os.path.exists('/proc/%d' %pid)
        if pid and running:
            print 'Application with process id %d is running' %pid
        elif pid:
            print 'Application with process id %d is stopped' %pid
        else:
            print 'Application is not running'
    elif options.status == 'cluster':
        print 'Cluster status not available.'
    else:
        parser.print_help()