Search in sources :

Example 1 with ConfigurationException

use of com.nextdoor.bender.config.ConfigurationException in project bender by Nextdoor.

the class BaseHandler method init.

/**
 * Loads @{link com.nextdoor.bender.config.Configuration} from a resource file and initializes
 * classes.
 *
 * @param ctx function context as specified when function is invoked by lambda.
 * @throws HandlerException error while loading the @{link
 *         com.nextdoor.bender.config.Configuration}.
 */
public void init(Context ctx) throws HandlerException {
    /*
     * Function alias is the last part of the Function ARN
     */
    String alias = null;
    String[] tokens = ctx.getInvokedFunctionArn().split(":");
    if (tokens.length == 7) {
        alias = "$LATEST";
    } else if (tokens.length == 8) {
        alias = tokens[7];
    }
    BenderLayout.ALIAS = alias;
    BenderLayout.VERSION = ctx.getFunctionVersion();
    /*
     * Create a new monitor and then get a static copy of it
     */
    monitor = Monitor.getInstance();
    monitor.addTag("functionName", ctx.getFunctionName());
    monitor.addTag("functionVersion", alias);
    String configFile;
    /*
     * TODO: Replace this to always use env vars. Code was written prior to
     * lambda env vars existing.
     */
    if (System.getenv("BENDER_CONFIG") != null) {
        configFile = System.getenv("BENDER_CONFIG");
    } else if (CONFIG_FILE == null) {
        configFile = "/config/" + alias;
    } else {
        configFile = CONFIG_FILE;
    }
    logger.info(String.format("Bender Initializing (config: %s)", configFile));
    try {
        if (configFile.startsWith("s3://")) {
            config = BenderConfig.load(s3ClientFactory, new AmazonS3URI(configFile));
        } else {
            config = BenderConfig.load(configFile);
        }
    } catch (ConfigurationException e) {
        throw new HandlerException("Error loading configuration: " + e.getMessage(), e);
    }
    HandlerResources handlerResources;
    try {
        handlerResources = new HandlerResources(config);
    } catch (ClassNotFoundException e) {
        throw new HandlerException("Unable to load resource: " + e.getMessage(), e);
    }
    /*
     * Register reporters
     */
    monitor.addReporters(handlerResources.getReporters());
    /*
     * Init other things
     */
    wrapper = handlerResources.getWrapperFactory().newInstance();
    ser = handlerResources.getSerializerProcessor();
    setIpcService(new IpcSenderService(handlerResources.getTransportFactory()));
    sources = new ArrayList<Source>(handlerResources.getSources().values());
    initialized = true;
}
Also used : IpcSenderService(com.nextdoor.bender.ipc.IpcSenderService) ConfigurationException(com.nextdoor.bender.config.ConfigurationException) AmazonS3URI(com.amazonaws.services.s3.AmazonS3URI) Source(com.nextdoor.bender.config.Source) HandlerResources(com.nextdoor.bender.config.HandlerResources)

Example 2 with ConfigurationException

use of com.nextdoor.bender.config.ConfigurationException in project bender by Nextdoor.

the class GeoIpOperationFactory method setConf.

@Override
public void setConf(AbstractConfig config) {
    this.config = (GeoIpOperationConfig) config;
    AmazonS3Client client = this.s3Factory.newInstance();
    AmazonS3URI uri = new AmazonS3URI(this.config.getGeoLiteDb());
    GetObjectRequest req = new GetObjectRequest(uri.getBucket(), uri.getKey());
    S3Object obj = client.getObject(req);
    try {
        this.databaseReader = new DatabaseReader.Builder(obj.getObjectContent()).withCache(new CHMCache()).build();
    } catch (IOException e) {
        throw new ConfigurationException("Unable to read " + this.config.getGeoLiteDb(), e);
    }
}
Also used : AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) ConfigurationException(com.nextdoor.bender.config.ConfigurationException) CHMCache(com.maxmind.db.CHMCache) DatabaseReader(com.maxmind.geoip2.DatabaseReader) S3Object(com.amazonaws.services.s3.model.S3Object) IOException(java.io.IOException) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) AmazonS3URI(com.amazonaws.services.s3.AmazonS3URI)

Example 3 with ConfigurationException

use of com.nextdoor.bender.config.ConfigurationException in project bender by Nextdoor.

the class ValidateSchema method main.

public static void main(String[] args) throws ParseException, InterruptedException, IOException {
    /*
     * Parse cli arguments
     */
    Options options = new Options();
    options.addOption(Option.builder().longOpt("schema").hasArg().desc("Filename to output schema to. Default: schema.json").build());
    options.addOption(Option.builder().longOpt("configs").hasArgs().desc("List of config files to validate against schema.").build());
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(options, args);
    String schemaFilename = cmd.getOptionValue("schema", "schema.json");
    String[] configFilenames = cmd.getOptionValues("configs");
    /*
     * Validate config files against schema
     */
    BenderSchema schema = new BenderSchema(new File(schemaFilename));
    boolean hasFailures = false;
    for (String configFilename : configFilenames) {
        StringBuilder sb = new StringBuilder();
        Files.lines(Paths.get(configFilename), StandardCharsets.UTF_8).forEach(p -> sb.append(p + "\n"));
        System.out.println("Attempting to validate " + configFilename);
        try {
            ObjectMapper mapper = BenderConfig.getObjectMapper(configFilename);
            BenderConfig.validate(sb.toString(), mapper, schema);
            System.out.println("Valid");
        } catch (ConfigurationException e) {
            System.out.println("Invalid");
            e.printStackTrace();
        }
    }
    if (hasFailures) {
        System.exit(1);
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) BenderSchema(com.nextdoor.bender.config.BenderConfig.BenderSchema) ConfigurationException(com.nextdoor.bender.config.ConfigurationException) CommandLineParser(org.apache.commons.cli.CommandLineParser) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultParser(org.apache.commons.cli.DefaultParser)

Aggregations

ConfigurationException (com.nextdoor.bender.config.ConfigurationException)3 AmazonS3URI (com.amazonaws.services.s3.AmazonS3URI)2 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)1 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)1 S3Object (com.amazonaws.services.s3.model.S3Object)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CHMCache (com.maxmind.db.CHMCache)1 DatabaseReader (com.maxmind.geoip2.DatabaseReader)1 BenderSchema (com.nextdoor.bender.config.BenderConfig.BenderSchema)1 HandlerResources (com.nextdoor.bender.config.HandlerResources)1 Source (com.nextdoor.bender.config.Source)1 IpcSenderService (com.nextdoor.bender.ipc.IpcSenderService)1 File (java.io.File)1 IOException (java.io.IOException)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 DefaultParser (org.apache.commons.cli.DefaultParser)1 Options (org.apache.commons.cli.Options)1