Server installation

Overview

This manual describes how to deploy DeviceHive cloud server to the machine either locally or in datacenter with ssh access for production, testing and development purpose.

Getting stable DeviceHive from docker hub

We have published a DeviceHive docker container so you can utilize docker's virtualization features with DeviceHive.
Check out DeviceHive on Docker Hub with the instructions on
how to use it. You can check dockerfile implementation as well as the script for setting up a new instance running
under nginx on DeviceHive Docker

In a few minutes instance will be deployed and started. Endpoint is available at  [http://127.0.0.1/api/rest](http://127.0.0.1/api/rest), admin control panel at [http://127.0.0.1/admin](http://127.0.0.1/admin) and [swagger](http://swagger.io/) at [http://127.0.0.1/api/swagger](http://127.0.0.1/api/swagger)
See [this](#section-default-server-credentials) section for the default server credentials.


[block:image]
{
  "images": [
    {
      "image": [
        "https://files.readme.io/1d8490e-srv.png",
        "srv.png",
        1515,
        575,
        "#eae4e0"
      ],
      "caption": "Server is up and running"
    }
  ]
}
[/block]

# Build DeviceHive from source code
In order to run the DeviceHive framework from source code you must have the following components installed and configured:
* [PostgreSQL 9.1](http://www.postgresql.org/download/) or above.
* [Apache Kafka 0.10.0.0](http://kafka.apache.org/downloads.html) or above.
* [Oracle JDK 8](http://www.oracle.com/technetwork/java/javase/downloads/index.html) or [OpenJDK 8](http://openjdk.java.net/)
* [Maven](http://maven.apache.org/download.cgi)
* [DeviceHiveJava source files](https://github.com/devicehive/devicehive-java-server). This is the main part of the [DeviceHive] framework
* Setup environment variables

Running Apache Kafka
-----------------------
Start Zookeeper and Apache Kafka brokers as explained at official documentation (http://kafka.apache.org/documentation.html#quickstart).

If your Kafka and Zookeeper are installed on different machines, or on non-default ports, you will have to update the server configuration. To do so, open 

devicehive-backend/src/main/resources/application.properties 

devicehive-frontend/src/main/resources/application.properties

In all 3 files, find the zookeeper.connect (zookeeper's contact point) and bootstrap.servers (list of Kafka brokers) properties and update them to the correct hostnames and ports.

Build packages
--------------
Download source code from [GitHub](https://github.com/devicehive/devicehive-java-server) using "Download ZIP" button.
It should always point to recent stable or beta release, but you always can get any other tag or branch.
It also can be done using one of [Git version control client](http://git-scm.com/downloads/guis) or git command line tool.
If you prefer git, clone project using command

`git clone https://github.com/devicehive/devicehive-java-server.git`

After that you can switch to the tag or branch you need. The list of all available releases can be found at
https://github.com/devicehive/devicehive-java-server/releases
Execute following command from ${devicehive-java-server-directory}.

`mvn clean package`

If there are no errors, compilation and packaging are completed and you can go to the next step.

Starting database
---------------------
Download and install PostgreSQL (see https://wiki.postgresql.org/wiki/Detailed_installation_guides) 
By default, DH expects a user with the username `postgres` and the password `12345`. You can change this in the DH configuration file (devicehive-rdbms-dao/src/main/resources/application-persistence.properties).

Create database with the name `devicehive` with the `postgres` user. This user should be the owner of the database.

Database schema will be initialized on application startup.

Setup environment variables
---------------------
The only variable that needs to be setup at the moment is a JWT secret.
Create a variable called "JWT_SECRET" and set there a value, that should meet your security risks.
Recommended key length is 128 bits, or more.

At the system start up, if JWT secret was set, it would be saved in the DB, in other case, it would be auto-generated and saved in the DB as well.
Each time system starts, it checks the environment variable first, then checks the value from DB, and after all generates a new key. So, unless the DB would not be recreated, already generated tokens would be valid.
But still, using a system variable is a highly recommended option.

Running application
---------------------
To start the application, you have to start the backend and the frontend. To do this, first run following command:

`java -jar ${devicehive-java-server-directory}/devicehive-backend/target/devicehive-backend-<version>-boot.jar`
 
Wait for the application to start, then run: 

`java -jar ${devicehive-java-server-directory}/devicehive-frontend/target/devicehive-frontend-<version>-boot.jar`

This will start embedded undertow application server on default port 8080 and deploy DeviceHive application.

# Default server credentials
For server version 2.x default login is `dhadmin` and password is `dhadmin_#911`
For server version 3.x default JWT access token is:

eyJhbGciOiJIUzI1NiJ9.eyJwYXlsb2FkIjp7InVzZXJJZCI6MSwiYWN0aW9ucyI6WyIqIl0sIm5ldHdvcmtJZHMiOlsiKiJdLCJkZXZpY2VJZHMiOlsiKiJdLCJleHBpcmF0aW9uIjoxNTI4NTAyNDAwMDAwLCJ0b2tlblR5cGUiOiJBQ0NFU1MifX0.W4cX40NBF2qohlJ6xBK9yRdY_QT3DHP8jvVFesVzDWg

and refresh token is:

eyJhbGciOiJIUzI1NiJ9.eyJwYXlsb2FkIjp7InVzZXJJZCI6MSwiYWN0aW9ucyI6WyIqIl0sIm5ldHdvcmtJZHMiOlsiKiJdLCJkZXZpY2VJZHMiOlsiKiJdLCJleHBpcmF0aW9uIjoxNTI4NTAyNDAwMDAwLCJ0b2tlblR5cGUiOiJSRUZSRVNIIn19.Cg03uxEUaMoAYx7mbp8LcbdZOHyyhnU5W8mIfK7rN5g