Skip to content

Commit

Permalink
Update config methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Weigel committed Jan 16, 2019
1 parent 092ae0e commit 8acdc5c
Show file tree
Hide file tree
Showing 21 changed files with 150 additions and 75 deletions.
61 changes: 59 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,64 @@ And the page at `http://localhost:8999/` will point to these two URLs.
<a name="Server_Configuration"></a>
## 4. Server Configuration

### Apache
### 4.1 `conf/config.json`

The variables `$HAPISERVERPATH`, `HAPISERVERHOME`, `$NODEEXE`, and `$PYTHONEXE` can be set in `conf/config.json` or as environment variables. These variables can be used in commands, files, and URLs in the server metadata.

The default configuration file is `conf/config.json` and this location changed using a command line argument, e.g.,

```
./hapiserver -c /tmp/config.json
```

To set variables using environment variables, use, e.g.,

```
PYTHONEXE=/opt/python/bin/python ./hapiserver
```

Variables set as environment variable take precendence over those set in `conf/config.json`.

**`$HAPISERVERPATH`** and **`$HAPISERVERHOME`**

These two variables can be used in metadata to reference a directory. For example,

```
"catalog": "$HAPISERVERHOME/mymetadata/Data.json"
```

By default, `$HAPISERVERPATH` is the installation directory (the directory containing the shell launch script `hapi-server`) and should not be changed as it is referenced in the demonstration metadata files. Modify `$HAPISERVERHOME` in `conf/config.json` to use a custom path.

All relative paths in commands in metadata files are relative to the directory where `hapi-server` was executed.

For example, if

```
/tmp/hapi-server-v0.9.2/hapi-server -f metadata/TestData.json
```

is executed from `/home/username`, the file

```
/home/username/metadata/TestData.json`
```

is read and relative paths in `TestData.json` have `/home/username/` prepended.

**`$PYTHONEXE`**

This is the command used to call Python. By default, it is `python`. If `python` is not in the path, this can be set using a relative or absolute path. Python is used by several of the demonstration catalogs.

```
"command": "$PYTHONEXE $HAPISERVERHOME/mybin/Data.py"
```

**`$NODEEXE`**

This is the command used to call NodeJS. By default, it is the command used to start the server. The start-up script looks for a NodeJS executable in `$HAPISERVERPATH/bin` and then tries `node` and then `nodejs`.


### 4.2 Apache

To expose a URL through Apache, (1) enable `mod_proxy` and `mod_proxy_http`, (2) add the following in a `<VirtualHost>` node in a [Apache Virtual Hosts](https://httpd.apache.org/docs/2.4/vhosts/examples.html) file

Expand All @@ -273,7 +330,7 @@ If serving multiple catalogs, use
</VirtualHost>
```

## Nginx
### 4.3 Nginx

For Nginx, add the following to `nginx.conf`

Expand Down
4 changes: 2 additions & 2 deletions conf/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD node index.js
EXPOSE 8081
CMD node server.js
EXPOSE 8999
8 changes: 4 additions & 4 deletions conf/server.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"HAPISERVER_BIN_DIR":"__ absolute path",
"HAPISERVER_META_DIR":"__ absolute path",
"NODE_EXE":"__ enter shell command (e.g., 'node') or absolute path",
"PYTHON_EXE":"__ enter shell command (e.g., 'python2.7') or absolute path"
"HAPISERVERPATH": "",
"HAPISERVERHOME": "",
"NODE_EXE": "",
"PYTHON_EXE": ""
}
47 changes: 33 additions & 14 deletions lib/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,48 @@ var is = require('hapi-server-verifier').is;

function ds() {return (new Date()).toISOString() + " [server] ";};

function configVars() {
function configVars(configfile) {

if (configVars.config) {
return configVars.config;
}

configd = {}; // Default configuration
configd["HAPISERVER_BIN_DIR"] = path.normalize(__dirname + "/../bin");
configd["HAPISERVER_META_DIR"] = path.normalize(__dirname + "/../metadata");
configd["NODE_EXE"] = process.execPath;
var env = process.env;

configd = {};
// Set defaults
configd["HAPISERVERPATH"] = path.normalize(__dirname + "/..");
configd["HAPISERVERHOME"] = configd["HAPISERVERPATH"];
configd["NODEEXE"] = process.execPath;
if (commandExistsSync("python")) {
configd["PYTHON_EXE"] = "python";
configd["PYTHONEXE"] = "python";
} else {
configd["PYTHON_EXE"] = path.normalize(__dirname + "/../bin/python");
configd["PYTHONEXE"] = path.normalize(__dirname + "/../bin/python");
}
var configfile = path.normalize(__dirname + "/../conf/server.json");

// Override defaults using configuration file variables
var config = require(configfile);
console.log(ds() + "Reading " + configfile);
for (key in config) {
if (config[key] !== "" && config[key].slice(0,2) !== "__") {
configd[key] = config[key]; // Update default
}
}

// Override defaults and config file with shell environment variables
if (env.HAPISERVERPATH) {
configd["HAPISERVERPATH"] = env.HAPISERVERPATH;
}
if (env.HAPISERVERHOME) {
configd["HAPISERVERHOME"] = env.HAPISERVERHOME;
}
if (env.NODEEXE) {
configd["NODEEXE"] = env.NODEEXE;
}
if (env.PYTHONEXE) {
configd["PYTHONEXE"] = env.PYTHONEXE;
}

configVars.config = configd;
return configd;

Expand All @@ -47,11 +66,10 @@ exports.configVars = configVars;
function replaceConfigVars(com) {
config = configVars();
if (typeof(com) === "string") {
com = com.replace("${cwd}", process.cwd());
com = com.replace("$(HAPISERVER_BIN_DIR)", config["HAPISERVER_BIN_DIR"]);
com = com.replace("$(HAPISERVER_META_DIR)", config["HAPISERVER_META_DIR"]);
com = com.replace("$(NODE_EXE)", config["NODE_EXE"]);
com = com.replace("$(PYTHON_EXE)", config["PYTHON_EXE"]);
com = com.replace("$HAPISERVERPATH", config["HAPISERVERPATH"]);
com = com.replace("$HAPISERVERHOME", config["HAPISERVERHOME"]);
com = com.replace("$NODEEXE", config["NODEEXE"]);
com = com.replace("$PYTHONEXE", config["PYTHONEXE"]);
return com;
} else {
for (var i=0;i < com.length; i++) {
Expand Down Expand Up @@ -107,7 +125,7 @@ function getmetadata(str) {
}
//console.log(obj)
} else {
// TODO: Try to read as JSON first!
// TODO: Try to read as JSON first.
// Attempt to execute; if failure, assume it is a file.
console.log(ds() + "Trying " + str + " as command line command.");
if (commandExistsSync(str.split(" ")[0])) {
Expand Down Expand Up @@ -307,6 +325,7 @@ function prepmetadata(catalogfile,HAPIVERSION,FORCE,VERIFIER,PLOTSERVER) {
// will be ignored. Need to catch this error.

if (json.data.file) {
json.data.file = replaceConfigVars(json.data.file);
if (!fs.existsSync(json.data.file)) {
console.log(ds() + clc.red(json.data.file + " referenced in " + catalogid + " not found."));
process.exit(1);
Expand Down
4 changes: 0 additions & 4 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ function ds() {return (new Date()).toISOString() + " [server] ";};
function commands(commandarr,catalog) {
for (var i = 0;i < commandarr.length;i++) {
command = commandarr[i].command;
// TODO: Substitute paths.
//console.log(teststr);
console.log(ds() + "Running " + catalog + " test command " + i + ".");
try {
var out = require('child_process')
Expand All @@ -19,8 +17,6 @@ function commands(commandarr,catalog) {
}
commandarr[i].testnumber = i;
commandarr[i].catalog = catalog;
//commandarr[i].command = command;
//console.log(out.toString());
testoutput(commandarr[i],out.toString());
}
}
Expand Down
6 changes: 3 additions & 3 deletions metadata/AutoplotExample1.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"data": {
"command": "java -Djava.awt.headless=true -cp bin/autoplot.jar org.autoplot.AutoplotDataServer --uri=http://autoplot.org/data/autoplot.cdf?BGSM -f hapi-data 2> >( sed '/Autoplot version/d' >&2 )",
"command": "java -Djava.awt.headless=true -cp $(HAPISERVER_BIN_DIR)/autoplot.jar org.autoplot.AutoplotDataServer --uri=http://autoplot.org/data/autoplot.cdf?BGSM -f hapi-data 2> >( sed '/Autoplot version/d' >&2 )",
"contact": "rweigel@gmu.edu",
"testcommands": [
{"command": "java -Djava.awt.headless=true -cp bin/autoplot.jar org.autoplot.AutoplotDataServer --uri=http://autoplot.org/data/autoplot.cdf?BGSM -f hapi-data | python lib/subset.py --start 2006-01-01T00:00:00.000Z --stop 2006-01-01T23:00:00.000Z --columns 1,2","Nlines": 23}
{"command": "java -Djava.awt.headless=true -cp $(HAPISERVER_BIN_DIR)/autoplot.jar org.autoplot.AutoplotDataServer --uri=http://autoplot.org/data/autoplot.cdf?BGSM -f hapi-data | python lib/subset.py --start 2006-01-01T00:00:00.000Z --stop 2006-01-01T23:00:00.000Z --columns 1,2","Nlines": 23}
],
"testurls": [
{"url": "${server}/data?id=ACE&parameters=Bx__GSM_&time.min=2006-01-01T00:00:00.000Z&time.max=2006-01-02T00:00:00.000Z&attach=false","Nlines": 24}
Expand All @@ -13,7 +13,7 @@
[
{
"id": "ACE",
"info": "java -Djava.awt.headless=true -cp bin/autoplot.jar org.autoplot.AutoplotDataServer --uri='http://autoplot.org/data/autoplot.cdf?BGSM' -f hapi-info"
"info": "java -Djava.awt.headless=true -cp $(HAPISERVER_BIN_DIR)/autoplot.jar org.autoplot.AutoplotDataServer --uri='http://autoplot.org/data/autoplot.cdf?BGSM' -f hapi-info"
}
]
}
4 changes: 2 additions & 2 deletions metadata/AutoplotExample2.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"data": {
"command": "bash ./bin/AutoplotExample2.sh ${parameters} ${start} ${stop} ${cwd}",
"command": "bash $(HAPISERVER_BIN_DIR)/AutoplotExample2.sh ${parameters} ${start} ${stop} ${cwd}",
"formats": ["csv"],
"contact": "rweigel@gmu.edu",
"testcommands": [
{"command": "bash ./bin/AutoplotExample2.sh Temperature 2018-01-06T00:00:00.000Z 2018-01-07T00:00:00.000Z ${cwd}", "Nlines": 1423}
{"command": "bash $(HAPISERVER_BIN_DIR)/AutoplotExample2.sh Temperature 2018-01-06T00:00:00.000Z 2018-01-07T00:00:00.000Z ${cwd}", "Nlines": 1423}
],
"testurls": [
{"url": "http://localhost:8999/AutoplotExample2/hapi/data?id=10.CF3744000800&parameters=Temperature&time.min=2018-01-06Z&time.max=2018-01-07T00:00:00.000Z", "Nlines": 1423}
Expand Down
4 changes: 2 additions & 2 deletions metadata/Example0.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"landing": ""
},
"data": {
"command": "python ./bin/Example.py",
"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py",
"contact": "rweigel@gmu.edu",
"testcommands": [
{"command": "python ./bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
{"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
],
"testurls": [
{"url": "${server}/data/?id=dataset1&parameters=vector&time.min=1970-01-01T00:00Z&time.max=1970-01-01T00:10Z", "Nlines": 10},
Expand Down
4 changes: 2 additions & 2 deletions metadata/Example1.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"data": {
"command": "python ./bin/Example.py --start ${start} --stop ${stop}",
"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --start ${start} --stop ${stop}",
"contact": "rweigel@gmu.edu",
"testcommands": [
{"command": "python ./bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
{"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
],
"testurls": [
{"url": "${server}/data/?id=dataset1&parameters=vector&time.min=1970-01-01T00:00Z&time.max=1970-01-01T00:10Z", "Nlines": 10},
Expand Down
4 changes: 2 additions & 2 deletions metadata/Example2.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"data": {
"command": "python ./bin/Example.py --params ${parameters} --start ${start} --stop ${stop} --fmt ${format}",
"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --params ${parameters} --start ${start} --stop ${stop} --fmt ${format}",
"contact": "rweigel@gmu.edu",
"formats": ["csv","binary"],
"testcommands": [
{"command": "python ./bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
{"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
],
"testurls": [
{"url": "${server}/data/?id=dataset1&parameters=vector&time.min=1970-01-01T00:00Z&time.max=1970-01-01T00:10Z", "Nlines": 10},
Expand Down
8 changes: 4 additions & 4 deletions metadata/Example3.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"data": {
"command": "python bin/Example.py --id ${id} --params ${parameters} --start ${start} --stop ${stop} --fmt ${format}",
"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --id ${id} --params ${parameters} --start ${start} --stop ${stop} --fmt ${format}",
"formats": ["csv","binary"],
"contact": "rweigel@gmu.edu",
"testcommands": [
{"command": "python ./bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
{"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
],
"testurls": [
{"url": "${server}/data/?id=dataset1&parameters=scalar&time.min=1970-01-01T00:00Z&time.max=1970-01-01T00:10Z", "Nlines": 10},
Expand All @@ -16,12 +16,12 @@
{
"id": "dataset1",
"title": "Simple dataset generated by Python",
"info": "metadata/Example3/dataset1_info.json"
"info": "$HAPISERVERPATH/metadata/Example3/dataset1_info.json"
},
{
"id": "dataset2",
"title": "Simple dataset generated by Python",
"info": "metadata/Example3/dataset1_info.json"
"info": "$HAPISERVERPATH/metadata/Example3/dataset1_info.json"
}
]
}
8 changes: 4 additions & 4 deletions metadata/Example4.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"data": {
"command": "python bin/Example.py --id ${id} --params ${parameters} --start ${start} --stop ${stop} --fmt ${format}",
"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --id ${id} --params ${parameters} --start ${start} --stop ${stop} --fmt ${format}",
"formats": ["csv","binary"],
"contact": "rweigel@gmu.edu",
"testcommands": [
{"command": "python ./bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
{"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
],
"testurls": [
{"url": "${server}/data/?id=dataset1&parameters=scalar&time.min=1970-01-01T00:00Z&time.max=1970-01-01T00:10Z", "Nlines": 10},
Expand All @@ -16,12 +16,12 @@
{
"id": "dataset1",
"title": "Simple dataset generated by Python",
"info": "cat metadata/Example4/dataset1_info.json"
"info": "cat $HAPISERVERPATH/metadata/Example4/dataset1_info.json"
},
{
"id": "dataset2",
"title": "Simple dataset generated by Python",
"info": "cat metadata/Example4/dataset1_info.json"
"info": "cat $HAPISERVERPATH/metadata/Example4/dataset1_info.json"
}
]
}
6 changes: 3 additions & 3 deletions metadata/Example5.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"data": {
"command": "python bin/Example.py --id ${id} --params ${parameters} --start ${start} --stop ${stop} --fmt ${format}",
"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --id ${id} --params ${parameters} --start ${start} --stop ${stop} --fmt ${format}",
"formats": ["csv","binary"],
"contact": "rweigel@gmu.edu",
"testcommands": [
{"command": "python ./bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
{"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
],
"testurls": [
{"url": "${server}/data/?id=dataset1&parameters=scalar&time.min=1970-01-01T00:00Z&time.max=1970-01-01T00:10Z", "Nlines": 10},
{"url": "${server}/data/?id=dataset1&parameters=scalar&time.min=1970-12-31T23:50Z&time.max=1971-01-01Z", "Nlines": 10}
]
},
"catalog" : "metadata/Example5/catalog.json"
"catalog" : "$HAPISERVERPATH/metadata/Example5/catalog.json"
}
6 changes: 3 additions & 3 deletions metadata/Example6.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"data": {
"command": "python bin/Example.py --id ${id} --params ${parameters} --start ${start} --stop ${stop} --fmt ${format}",
"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --id ${id} --params ${parameters} --start ${start} --stop ${stop} --fmt ${format}",
"formats": ["csv","binary"],
"contact": "rweigel@gmu.edu",
"testcommands": [
{"command": "python ./bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
{"command": "$PYTHONEXE $HAPISERVERPATH/bin/Example.py --params scalar --start 1970-01-01T00:00:00.000000000Z --stop 1970-01-01T00:10:00.000000000Z","Nlines": 10}
],
"testurls": [
{"url": "${server}/data/?id=dataset1&parameters=scalar&time.min=1970-01-01T00:00Z&time.max=1970-01-01T00:10Z", "Nlines": 10},
{"url": "${server}/data/?id=dataset1&parameters=scalar&time.min=1970-12-31T23:50Z&time.max=1971-01-01Z", "Nlines": 10}
]
},
"catalog" : "cat metadata/Example5/catalog.json"
"catalog" : "cat $HAPISERVERPATH/metadata/Example5/catalog.json"
}
2 changes: 1 addition & 1 deletion metadata/Example8.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"data": {
"file": "public/data/Example/Example.csv",
"file": "$HAPISERVERPATH/public/data/Example/Example.csv",
"fileformat": "csv",
"contact": "rweigel@gmu.edu",
"testurls": [
Expand Down
4 changes: 2 additions & 2 deletions metadata/INTERMAGNET.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"data": {
"command": "python ./bin/INTERMAGNET.py --id ${id} --start ${start} --stop ${stop}",
"command": "$PYTHONEXE $HAPISERVERPATH/bin/INTERMAGNET.py --id ${id} --start ${start} --stop ${stop}",
"contact": "rweigel@gmu.edu",
"testurls": [
{"url": "${server}/data?id=minute/definitive/ams&parameters=X&time.min=1991-01-01Z&time.max=1991-01-01T00:10:00.000Z", "Nlines": 10}
]
},
"catalog": "metadata/INTERMAGNET/INTERMAGNET-catalog.json"
"catalog": "$HAPISERVERPATH/metadata/INTERMAGNET/INTERMAGNET-catalog.json"
}

0 comments on commit 8acdc5c

Please sign in to comment.