diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /defaults |
Publish
Diffstat (limited to 'defaults')
-rw-r--r-- | defaults/.gitignore | 13 | ||||
-rw-r--r-- | defaults/CMakeLists.txt | 8 | ||||
-rw-r--r-- | defaults/OWNERS | 2 | ||||
-rw-r--r-- | defaults/README | 1 | ||||
-rw-r--r-- | defaults/pom.xml | 80 | ||||
-rw-r--r-- | defaults/src/.gitignore | 3 | ||||
-rw-r--r-- | defaults/src/apps/printdefault/.gitignore | 4 | ||||
-rw-r--r-- | defaults/src/apps/printdefault/CMakeLists.txt | 8 | ||||
-rw-r--r-- | defaults/src/apps/printdefault/printdefault.cpp | 23 | ||||
-rw-r--r-- | defaults/src/main/java/com/yahoo/vespa/defaults/Defaults.java | 96 | ||||
-rw-r--r-- | defaults/src/main/java/com/yahoo/vespa/defaults/package-info.java | 7 | ||||
-rw-r--r-- | defaults/src/test/java/com/yahoo/vespa/defaults/DefaultsTestCase.java | 20 | ||||
-rw-r--r-- | defaults/src/vespa/.gitignore | 3 | ||||
-rw-r--r-- | defaults/src/vespa/CMakeLists.txt | 7 | ||||
-rw-r--r-- | defaults/src/vespa/defaults.cpp | 144 | ||||
-rw-r--r-- | defaults/src/vespa/defaults.h | 42 |
16 files changed, 461 insertions, 0 deletions
diff --git a/defaults/.gitignore b/defaults/.gitignore new file mode 100644 index 00000000000..d2a86c8103d --- /dev/null +++ b/defaults/.gitignore @@ -0,0 +1,13 @@ +.classpath +.project +.settings +archive +build +log +target +*.iml +*.ipr +*.iws +/pom.xml.build +Makefile +Testing diff --git a/defaults/CMakeLists.txt b/defaults/CMakeLists.txt new file mode 100644 index 00000000000..f45e4938a74 --- /dev/null +++ b/defaults/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_define_module( + LIBS + src/vespa + + APPS + src/apps/printdefault +) diff --git a/defaults/OWNERS b/defaults/OWNERS new file mode 100644 index 00000000000..6c96073cde8 --- /dev/null +++ b/defaults/OWNERS @@ -0,0 +1,2 @@ +arnej27959 +bratseth diff --git a/defaults/README b/defaults/README new file mode 100644 index 00000000000..0311224c4c9 --- /dev/null +++ b/defaults/README @@ -0,0 +1 @@ +Module for basic configuration defaults in Vespa. diff --git a/defaults/pom.xml b/defaults/pom.xml new file mode 100644 index 00000000000..7909947416a --- /dev/null +++ b/defaults/pom.xml @@ -0,0 +1,80 @@ +<?xml version="1.0"?> +<!-- Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. --> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>com.yahoo.vespa</groupId> + <artifactId>parent</artifactId> + <version>6-SNAPSHOT</version> + <relativePath>../parent/pom.xml</relativePath> + </parent> + <groupId>com.yahoo.vespa</groupId> + <artifactId>defaults</artifactId> + <packaging>container-plugin</packaging> + <version>6-SNAPSHOT</version> + <name>defaults</name> + <description> + Library with the defaults for Vespa. + </description> + + <repositories> + <repository> + <id>apache-org</id> + <name>apache.org Repository for Maven</name> + <url>https://repository.apache.org/content/groups/public</url> + <releases> + <updatePolicy>never</updatePolicy> + </releases> + <snapshots> + <enabled>false</enabled> + </snapshots> + </repository> + </repositories> + + <dependencies> + <dependency> + <groupId>com.yahoo.vespa</groupId> + <artifactId>annotations</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>com.yahoo.vespa</groupId> + <artifactId>bundle-plugin</artifactId> + <extensions>true</extensions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-deploy-plugin</artifactId> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <compilerArgs> + <arg>-Xlint:all</arg> + <arg>-Werror</arg> + </compilerArgs> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-install-plugin</artifactId> + <configuration> + <updateReleaseInfo>true</updateReleaseInfo> + </configuration> + </plugin> + </plugins> + </build> +</project> diff --git a/defaults/src/.gitignore b/defaults/src/.gitignore new file mode 100644 index 00000000000..3e338e617f9 --- /dev/null +++ b/defaults/src/.gitignore @@ -0,0 +1,3 @@ +Makefile.ini +config_command.sh +defaults.mak diff --git a/defaults/src/apps/printdefault/.gitignore b/defaults/src/apps/printdefault/.gitignore new file mode 100644 index 00000000000..8d38a74c5ad --- /dev/null +++ b/defaults/src/apps/printdefault/.gitignore @@ -0,0 +1,4 @@ +Makefile +.depend +vespa-print-default +defaults_vespa-print-default_app diff --git a/defaults/src/apps/printdefault/CMakeLists.txt b/defaults/src/apps/printdefault/CMakeLists.txt new file mode 100644 index 00000000000..644077eb701 --- /dev/null +++ b/defaults/src/apps/printdefault/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(defaults_vespa-print-default_app + SOURCES + printdefault.cpp + INSTALL bin + DEPENDS + vespadefaults +) diff --git a/defaults/src/apps/printdefault/printdefault.cpp b/defaults/src/apps/printdefault/printdefault.cpp new file mode 100644 index 00000000000..0cb4bb1bf19 --- /dev/null +++ b/defaults/src/apps/printdefault/printdefault.cpp @@ -0,0 +1,23 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/defaults.h> +#include <stdio.h> +#include <string.h> + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, "usage: %s <variable>\n", argv[0]); + fprintf(stderr, " the variable must be 'home' or 'portbase' currently\n"); + return 1; + } + if (strcmp(argv[1], "home") == 0) { + printf("%s\n", vespa::Defaults::vespaHome()); + return 0; + } else if (strcmp(argv[1], "portbase") == 0) { + printf("%d\n", vespa::Defaults::vespaPortBase()); + return 0; + } else { + fprintf(stderr, "Unknown variable '%s'\n", argv[1]); + return 1; + } +} diff --git a/defaults/src/main/java/com/yahoo/vespa/defaults/Defaults.java b/defaults/src/main/java/com/yahoo/vespa/defaults/Defaults.java new file mode 100644 index 00000000000..bbca3c3aa34 --- /dev/null +++ b/defaults/src/main/java/com/yahoo/vespa/defaults/Defaults.java @@ -0,0 +1,96 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.defaults; + +import java.util.Optional; +import java.util.logging.Logger; + +/** + * The defaults of basic Vespa configuration variables. + * Use Defaults.getDefaults() to access the defaults of this runtime environment. + * + * @author arnej27959 + * @author bratseth + */ +public class Defaults { + + private static final Logger log = Logger.getLogger(Defaults.class.getName()); + private static final Defaults defaults = new Defaults(); + + private final String vespaHome; + private final int vespaWebServicePort; + private final int vespaPortBase; + + private Defaults() { + vespaHome = findVespaHome(); + vespaWebServicePort = findVespaWebServicePort(); + vespaPortBase = 19000; // TODO + } + + private String findVespaHome() { + Optional<String> vespaHomeEnv = Optional.ofNullable(System.getenv("VESPA_HOME")); + if ( ! vespaHomeEnv.isPresent() || vespaHomeEnv.get().trim().isEmpty()) { + log.info("VESPA_HOME not set, using /opt/vespa/"); + return "/opt/vespa/"; + } + String vespaHome = vespaHomeEnv.get(); + if ( ! vespaHome.endsWith("/")) + vespaHome = vespaHome + "/"; + return vespaHome; + } + + private int findVespaWebServicePort() { + Optional<String> vespaWebServicePortString = Optional.ofNullable(System.getenv("VESPA_WEB_SERVICE_PORT")); + if ( ! vespaWebServicePortString.isPresent() || vespaWebServicePortString.get().trim().isEmpty()) { + log.info("VESPA_WEB_SERVICE_PORT not set, using 8080"); + return 8080; + } + try { + return Integer.parseInt(vespaWebServicePortString.get()); + } + catch (NumberFormatException e) { + throw new IllegalArgumentException("VESPA_WEB_SERVICE_PORT must be an integer, was '" + + vespaWebServicePortString.get() + "'"); + } + } + + /** + * Returns the path to the root under which Vespa should read and write files, ending by "/". + * + * @return the vespa home directory, ending by "/" + */ + public String vespaHome() { return vespaHome; } + + /** + * Returns an absolute path produced by prepending vespaHome to the argument if it is relative. + * If the path starts by "/" (absolute) or "./" (explicitly relative - useful for tests), + * it is returned as-is. + * + * @param path the path to prepend vespaHome to unless it is absolute + * @return the given path string with the root path given from + * vespaHome() prepended, unless the given path is absolute, in which + * case it is be returned as-is + */ + public String underVespaHome(String path) { + if (path.startsWith("/")) return path; + if (path.startsWith("./")) return path; + return vespaHome() + path; + } + + /** + * Returns the port number where Vespa web services should be available. + * + * @return the vespa webservice port + */ + public int vespaWebServicePort() { return vespaWebServicePort; } + + /** + * Returns the base for port numbers where the Vespa services should listen. + * + * @return the vespa base number for ports + */ + public int vespaPortBase() { return vespaPortBase; } + + /** Returns the defaults of this runtime environment */ + public static Defaults getDefaults() { return defaults; } + +} diff --git a/defaults/src/main/java/com/yahoo/vespa/defaults/package-info.java b/defaults/src/main/java/com/yahoo/vespa/defaults/package-info.java new file mode 100644 index 00000000000..b1a42c3596f --- /dev/null +++ b/defaults/src/main/java/com/yahoo/vespa/defaults/package-info.java @@ -0,0 +1,7 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +@ExportPackage +@PublicApi +package com.yahoo.vespa.defaults; + +import com.yahoo.api.annotations.PublicApi; +import com.yahoo.osgi.annotation.ExportPackage; diff --git a/defaults/src/test/java/com/yahoo/vespa/defaults/DefaultsTestCase.java b/defaults/src/test/java/com/yahoo/vespa/defaults/DefaultsTestCase.java new file mode 100644 index 00000000000..28cdc4e699d --- /dev/null +++ b/defaults/src/test/java/com/yahoo/vespa/defaults/DefaultsTestCase.java @@ -0,0 +1,20 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.defaults; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * @author arnej27959 + * @author bratseth + */ +public class DefaultsTestCase { + + @Test + public void testUnderVespaHome() { + assertEquals("/opt/vespa/my/relative/path", Defaults.getDefaults().underVespaHome("my/relative/path")); + assertEquals("/my/absolute/path", Defaults.getDefaults().underVespaHome("/my/absolute/path")); + assertEquals("./my/explicit/relative/path", Defaults.getDefaults().underVespaHome("./my/explicit/relative/path")); + } + +} diff --git a/defaults/src/vespa/.gitignore b/defaults/src/vespa/.gitignore new file mode 100644 index 00000000000..75d87529258 --- /dev/null +++ b/defaults/src/vespa/.gitignore @@ -0,0 +1,3 @@ +Makefile +.depend +lib*.so* diff --git a/defaults/src/vespa/CMakeLists.txt b/defaults/src/vespa/CMakeLists.txt new file mode 100644 index 00000000000..46d8a7bfb26 --- /dev/null +++ b/defaults/src/vespa/CMakeLists.txt @@ -0,0 +1,7 @@ +# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_library(vespadefaults + SOURCES + defaults.cpp + INSTALL lib64 + DEPENDS +) diff --git a/defaults/src/vespa/defaults.cpp b/defaults/src/vespa/defaults.cpp new file mode 100644 index 00000000000..387f28df542 --- /dev/null +++ b/defaults/src/vespa/defaults.cpp @@ -0,0 +1,144 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include "defaults.h" + +#include <stdlib.h> +#include <dirent.h> +#include <stdio.h> +#include <string.h> +#include <string> +#include <unistd.h> + +namespace { + +const char *defaultHome = "/opt/vespa/"; +char computedHome[PATH_MAX]; +int defaultWebServicePort = 8080; +int defaultPortBase = 19000; + +void findDefaults() { + static int hasRun = 0; + if (hasRun) return; + const char *env = getenv("VESPA_HOME"); + if (env != NULL) { + DIR *dp = NULL; + if (*env == '/') { + dp = opendir(env); + } + if (dp != NULL) { + size_t len = strlen(env); + if (env[len-1] == '/') { + // already ends with slash + defaultHome = env; + } else { + // append slash + strncpy(computedHome, env, PATH_MAX); + strncat(computedHome, "/", PATH_MAX); + defaultHome = computedHome; + } + // fprintf(stderr, "debug\tVESPA_HOME is '%s'\n", defaultHome); + closedir(dp); + } else { + fprintf(stderr, "warning\tbad VESPA_HOME '%s' (ignored)\n", env); + } + } + env = getenv("VESPA_WEB_SERVICE_PORT"); + if (env != NULL && *env != '\0') { + char *endptr = NULL; + long p = strtol(env, &endptr, 10); + if (endptr != NULL && *endptr == '\0') { + defaultWebServicePort = p; + // fprintf(stderr, "debug\tdefault web service port is '%ld'\n", p); + } else { + fprintf(stderr, "warning\tbad VESPA_WEB_SERVICE_PORT '%s' (ignored)\n", env); + } + } + env = getenv("VESPA_PORT_BASE"); + if (env != NULL && *env != '\0') { + char *endptr = NULL; + long p = strtol(env, &endptr, 10); + if (endptr != NULL && *endptr == '\0') { + defaultPortBase = p; + // fprintf(stderr, "debug\tdefault port base is '%ld'\n", p); + } else { + fprintf(stderr, "warning\tbad VESPA_PORT_BASE '%s' (ignored)\n", env); + } + } + hasRun = 1; +} + +} + +namespace vespa { + +std::string myPath(const char *argv0) +{ + if (argv0[0] == '/') return argv0; + + const char *pathEnv = getenv("PATH"); + if (pathEnv != 0) { + std::string path = pathEnv; + size_t pos = 0; + size_t colon; + do { + colon = path.find(':', pos); + std::string pre = path.substr(pos, colon-pos); + pos = colon+1; + if (pre[0] == '/') { + pre.append("/"); + pre.append(argv0); + if (access(pre.c_str(), X_OK) == 0) { + return pre; + } + } + } while (colon != std::string::npos); + } + return argv0; +} + +void +Defaults::bootstrap(const char *argv0) +{ + if (getenv("VESPA_HOME") == 0) { + std::string path = myPath(argv0); + size_t slash = path.rfind('/'); + if (slash != std::string::npos) { + path.resize(slash); + slash = path.rfind('/', slash-1); + if (slash != std::string::npos) { + const char *dirname = path.c_str() + slash; + if (strncmp(dirname, "/bin", 4) == 0 || + strncmp(dirname, "/sbin", 5) == 0) + { + path.resize(slash); + } + } + std::string setting = "VESPA_HOME"; + setting.append("="); + setting.append(path); + putenv(&setting[0]); + } + } +} + +const char * +Defaults::vespaHome() +{ + findDefaults(); + return defaultHome; +} + +int +Defaults::vespaWebServicePort() +{ + findDefaults(); + return defaultWebServicePort; +} + +int +Defaults::vespaPortBase() +{ + findDefaults(); + return defaultPortBase; +} + +} // namespace vespa diff --git a/defaults/src/vespa/defaults.h b/defaults/src/vespa/defaults.h new file mode 100644 index 00000000000..5d8d0c06520 --- /dev/null +++ b/defaults/src/vespa/defaults.h @@ -0,0 +1,42 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#pragma once + +namespace vespa { + +/** + * This is a class containing the defaults for basic Vespa configuration variables. + **/ +class Defaults { +public: + /** + * bootstrap VESPA_HOME (if unset) from argv[0] + **/ + static void bootstrap(const char *argv0); + + /** + * Compute the path prefix where Vespa files will live; + * the return value ends with a "/" so you can just append + * the relative pathname to the file(s) you want. + * + * @return the vespa home directory, ending by "/" + **/ + static const char *vespaHome(); + + /** + * Compute the port number where the Vespa webservice + * container should be available. + * + * @return the vespa webservice port + **/ + static int vespaWebServicePort(); + + /** + * Compute the base for port numbers where the Vespa services + * should listen. + * + * @return the vespa base number for ports + **/ + static int vespaPortBase(); +}; + +} // namespace vespa |