Beispiel #1
0
function main()
{
    // Check Env
    CommandLineUtils::checkRequiredExtentions();
    // Options
    $helpOpt = new Option(null, 'help');
    $helpOpt->setDescription('Print usage information.');
    $zookeeperOpt = new Option(null, 'zookeeper', Getopt::REQUIRED_ARGUMENT);
    $zookeeperOpt->setDescription('REQUIRED: The connection string for the zookeeper connection in the form host:port.' . 'Multiple URLS can be given to allow fail-over.');
    $createOpt = new Option(null, 'create');
    $createOpt->setDescription('Create a new config.');
    $deleteOpt = new Option(null, 'delete');
    $deleteOpt->setDescription('Delete a config');
    $listOpt = new Option(null, 'list');
    $listOpt->setDescription('List all available configs.');
    $hostnameOpt = new Option(null, 'hostname', Getopt::REQUIRED_ARGUMENT);
    $hostnameOpt->setDescription('The hostname of machine which holds log files');
    $hostnameOpt->setValidation(function ($value) {
        $ret = AdminUtils::isHostnameValid($value);
        return $ret['valid'];
    });
    $log_pathOpt = new Option(null, 'log_path', Getopt::REQUIRED_ARGUMENT);
    $log_pathOpt->setDescription('The log file path, like "/usr/local/apache2/logs/access_log.%Y%m%d"');
    $log_pathOpt->setValidation(function ($value) {
        $ret = AdminUtils::isFilePathValid($value);
        return $ret['valid'];
    });
    $topicOpt = new Option(null, 'topic', Getopt::REQUIRED_ARGUMENT);
    $topicOpt->setDescription('The topic of messages to be sent.');
    $topicOpt->setValidation(function ($value) {
        return AdminUtils::isTopicValid($value);
    });
    $partitionOpt = new Option(null, 'partition', Getopt::REQUIRED_ARGUMENT);
    $partitionOpt->setDescription('The partition of messages to be sent.' . '-1 : random' . 'n(>=0): partition n');
    $partitionOpt->setDefaultValue('-1');
    $partitionOpt->setValidation(function ($value) {
        return is_numeric($value);
    });
    $keyOpt = new Option(null, 'key', Getopt::REQUIRED_ARGUMENT);
    $keyOpt->setDescription('The key of messages to be sent.');
    $keyOpt->setDefaultValue('');
    $keyOpt->setValidation(function ($value) {
        return is_string($value);
    });
    $requiredAcksOpt = new Option(null, 'required_acks', Getopt::REQUIRED_ARGUMENT);
    $requiredAcksOpt->setDescription('Required ack number');
    $requiredAcksOpt->setDefaultValue('1');
    $requiredAcksOpt->setValidation(function ($value) {
        return is_numeric($value);
    });
    $compression_codecOpt = new Option(null, 'compression_codec', Getopt::REQUIRED_ARGUMENT);
    $compression_codecOpt->setDescription("Optional compression method of messages: " . implode(", ", AdminUtils::$COMPRESSION_CODECS));
    $compression_codecOpt->setDefaultValue('none');
    $compression_codecOpt->setValidation(function ($value) {
        return AdminUtils::isCompressionCodecValid($value);
    });
    $batchsizeOpt = new Option(null, 'batchsize', Getopt::REQUIRED_ARGUMENT);
    $batchsizeOpt->setDescription('The batch size of messages to be sent');
    $batchsizeOpt->setDefaultValue('1000');
    $batchsizeOpt->setValidation(function ($value) {
        return is_numeric($value) && (int) $value > 0;
    });
    $follow_lastOpt = new Option(null, 'follow_last', Getopt::REQUIRED_ARGUMENT);
    $follow_lastOpt->setDescription('If set to "false", when restarting logkafka process, 
                          the log_path formatted with current time will be collect;
                          If set to "true", when restarting logkafka process, the last 
                          collecting file will be collected continually');
    $follow_lastOpt->setDefaultValue('true');
    $follow_lastOpt->setValidation(function ($value) {
        return in_array($value, array('true', 'false'));
    });
    $message_timeout_msOpt = new Option(null, 'message_timeout_ms', Getopt::REQUIRED_ARGUMENT);
    $message_timeout_msOpt->setDescription('Local message timeout. This value is only enforced locally 
                          and limits the time a produced message waits for successful delivery. 
                          A time of 0 is infinite.');
    $message_timeout_msOpt->setDefaultValue('0');
    $message_timeout_msOpt->setValidation(function ($value) {
        return is_numeric($value) && (int) $value >= 0;
    });
    $validOpt = new Option(null, 'valid', Getopt::REQUIRED_ARGUMENT);
    $validOpt->setDescription('Enable now or not');
    $validOpt->setDefaultValue('true');
    $validOpt->setValidation(function ($value) {
        return in_array($value, array('true', 'false'));
    });
    $parser = new Getopt(array($zookeeperOpt, $createOpt, $deleteOpt, $listOpt, $hostnameOpt, $log_pathOpt, $topicOpt, $partitionOpt, $keyOpt, $requiredAcksOpt, $compression_codecOpt, $batchsizeOpt, $follow_lastOpt, $message_timeout_msOpt, $validOpt));
    try {
        $parser->parse();
        // Error handling and --help functionality omitted for brevity
        $listOptVal = 0;
        $createOptVal = 0;
        $deleteOptVal = 0;
        if ($parser["list"]) {
            $listOptVal = 1;
        }
        if ($parser["create"]) {
            $createOptVal = 1;
        }
        if ($parser["delete"]) {
            $deleteOptVal = 1;
        }
    } catch (UnexpectedValueException $e) {
        echo "Error: " . $e->getMessage() . "\n";
        echo $parser->getHelpText();
        exit(1);
    }
    // actions
    $actions = $listOptVal + $createOptVal + $deleteOptVal;
    if ($actions != 1) {
        CommandLineUtils::printUsageAndDie($parser, "Command must include exactly one action: --list, --create, or --delete");
    }
    // check args
    checkArgs($parser);
    $zkClient = new Zookeeper($parser['zookeeper']);
    AdminUtils::checkZkState($zkClient);
    try {
        if ($parser['create']) {
            createConfig($zkClient, $parser);
        } else {
            if ($parser['delete']) {
                deleteConfig($zkClient, $parser);
            } else {
                if ($parser['list']) {
                    listConfig($zkClient, $parser);
                }
            }
        }
    } catch (LogConfException $e) {
        echo "Caught LogConfException ('{$e->getMessage()}')\n{$e}\n";
    } catch (Exception $e) {
        echo "Caught Exception ('{$e->getMessage()}')\n{$e}\n";
    }
}