From 0e31cd4cb4131405063029f25dcf2558c637453d Mon Sep 17 00:00:00 2001 From: volodymyr-babak Date: Wed, 31 May 2017 12:48:35 +0300 Subject: [PATCH] Added postgres docker contrainer Fixed build to run with sql enabled true --- .../server/ThingsboardServerApplication.java | 2 +- .../service/mail/DefaultMailService.java | 10 ++- .../src/main/resources/thingsboard.yml | 6 +- .../dao/cassandra/CassandraCluster.java | 2 + .../CassandraBaseComponentDescriptorDao.java | 2 + .../dao/sql/attributes/JpaAttributesDao.java | 46 +++++++++++++ .../JpaBaseComponentDescriptorDao.java | 9 ++- .../dao/sql/settings/JpaAdminSettingsDao.java | 9 ++- .../dao/sql/timeseries/JpaTimeseriesDao.java | 65 +++++++++++++++++++ .../CassandraBaseTimeseriesDao.java | 2 + docker/.env | 9 ++- docker/cassandra/Makefile | 2 +- docker/docker-compose.static.yml | 3 + docker/docker-compose.yml | 8 +++ docker/postgres/Dockerfile | 21 ++++++ docker/postgres/Makefile | 10 +++ docker/postgres/init-user-db.sh | 22 +++++++ docker/tb-cassandra-schema/Makefile | 2 +- docker/tb/Makefile | 2 +- docker/zookeeper/Makefile | 2 +- pom.xml | 4 ++ 21 files changed, 219 insertions(+), 19 deletions(-) create mode 100644 dao/src/main/java/org/thingsboard/server/dao/sql/attributes/JpaAttributesDao.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/sql/timeseries/JpaTimeseriesDao.java create mode 100644 docker/postgres/Dockerfile create mode 100644 docker/postgres/Makefile create mode 100644 docker/postgres/init-user-db.sh diff --git a/application/src/main/java/org/thingsboard/server/ThingsboardServerApplication.java b/application/src/main/java/org/thingsboard/server/ThingsboardServerApplication.java index 5e00ac4a11..b70d9522c8 100644 --- a/application/src/main/java/org/thingsboard/server/ThingsboardServerApplication.java +++ b/application/src/main/java/org/thingsboard/server/ThingsboardServerApplication.java @@ -26,7 +26,7 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.Arrays; -@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) +@EnableAutoConfiguration @SpringBootApplication @EnableSwagger2 @ComponentScan({"org.thingsboard.server"}) diff --git a/application/src/main/java/org/thingsboard/server/service/mail/DefaultMailService.java b/application/src/main/java/org/thingsboard/server/service/mail/DefaultMailService.java index d4c0decf3e..f4dfb096e3 100644 --- a/application/src/main/java/org/thingsboard/server/service/mail/DefaultMailService.java +++ b/application/src/main/java/org/thingsboard/server/service/mail/DefaultMailService.java @@ -66,9 +66,13 @@ public class DefaultMailService implements MailService { @Override public void updateMailConfiguration() { AdminSettings settings = adminSettingsService.findAdminSettingsByKey("mail"); - JsonNode jsonConfig = settings.getJsonValue(); - mailSender = createMailSender(jsonConfig); - mailFrom = jsonConfig.get("mailFrom").asText(); + if (settings != null) { + JsonNode jsonConfig = settings.getJsonValue(); + mailSender = createMailSender(jsonConfig); + mailFrom = jsonConfig.get("mailFrom").asText(); + } else { + throw new IncorrectParameterException("Failed to date mail configuration. Settings not found!"); + } } private JavaMailSenderImpl createMailSender(JsonNode jsonConfig) { diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml index 6bbce15a8c..c797cf1433 100644 --- a/application/src/main/resources/thingsboard.yml +++ b/application/src/main/resources/thingsboard.yml @@ -232,13 +232,13 @@ spring: data: jpa: repositories: - enabled: "false" + enabled: "true" jpa: show-sql: "false" - generate-ddl: "false" + generate-ddl: "true" database-platform: "org.hibernate.dialect.PostgreSQLDialect" hibernate: - ddl-auto: "none" + ddl-auto: "create" datasource: driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.postgresql.Driver}" url: "${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/thingsboard}" diff --git a/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraCluster.java b/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraCluster.java index efe05f8fb8..6bffb3e3b4 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraCluster.java +++ b/dao/src/main/java/org/thingsboard/server/dao/cassandra/CassandraCluster.java @@ -25,6 +25,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @@ -37,6 +38,7 @@ import java.util.List; @Component @Slf4j @Data +@ConditionalOnProperty(prefix = "cassandra", value = "enabled", havingValue = "true") public class CassandraCluster { private static final String COMMA = ","; diff --git a/dao/src/main/java/org/thingsboard/server/dao/component/CassandraBaseComponentDescriptorDao.java b/dao/src/main/java/org/thingsboard/server/dao/component/CassandraBaseComponentDescriptorDao.java index 0426a3372a..0c7984e254 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/component/CassandraBaseComponentDescriptorDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/component/CassandraBaseComponentDescriptorDao.java @@ -21,6 +21,7 @@ import com.datastax.driver.core.querybuilder.QueryBuilder; import com.datastax.driver.core.querybuilder.Select; import com.datastax.driver.core.utils.UUIDs; import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.id.ComponentDescriptorId; import org.thingsboard.server.common.data.page.TextPageLink; @@ -45,6 +46,7 @@ import static com.datastax.driver.core.querybuilder.QueryBuilder.select; */ @Component @Slf4j +@ConditionalOnProperty(prefix = "cassandra", value = "enabled", havingValue = "true", matchIfMissing = false) public class CassandraBaseComponentDescriptorDao extends CassandraAbstractSearchTextDao implements ComponentDescriptorDao { @Override diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/JpaAttributesDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/JpaAttributesDao.java new file mode 100644 index 0000000000..d4f1caa369 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/attributes/JpaAttributesDao.java @@ -0,0 +1,46 @@ +package org.thingsboard.server.dao.sql.attributes; + +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.ResultSetFuture; +import com.google.common.util.concurrent.ListenableFuture; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Component; +import org.thingsboard.server.common.data.id.EntityId; +import org.thingsboard.server.common.data.kv.AttributeKvEntry; +import org.thingsboard.server.dao.attributes.AttributesDao; + +import java.util.Collection; +import java.util.List; +import java.util.Optional; + +@Component +@Slf4j +@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true") +public class JpaAttributesDao implements AttributesDao { + + @Override + public ListenableFuture> find(EntityId entityId, String attributeType, String attributeKey) { + return null; + } + + @Override + public ListenableFuture> find(EntityId entityId, String attributeType, Collection attributeKey) { + return null; + } + + @Override + public ListenableFuture> findAll(EntityId entityId, String attributeType) { + return null; + } + + @Override + public ResultSetFuture save(EntityId entityId, String attributeType, AttributeKvEntry attribute) { + return null; + } + + @Override + public ListenableFuture> removeAll(EntityId entityId, String scope, List keys) { + return null; + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/component/JpaBaseComponentDescriptorDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/component/JpaBaseComponentDescriptorDao.java index fec30b9d3f..bbc442e537 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/component/JpaBaseComponentDescriptorDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/component/JpaBaseComponentDescriptorDao.java @@ -57,11 +57,14 @@ public class JpaBaseComponentDescriptorDao extends JpaAbstractSearchTextDao saveIfNotExist(ComponentDescriptor component) { + if (component.getId() == null) { + component.setId(new ComponentDescriptorId(UUID.randomUUID())); + } boolean exists = componentDescriptorRepository.findOne(component.getId().getId()) != null; - if (exists) { - return Optional.empty(); + if (!exists) { + return Optional.of(save(component)); } - return Optional.of(save(component)); + return Optional.empty(); } @Override diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/settings/JpaAdminSettingsDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/settings/JpaAdminSettingsDao.java index b1e2326f41..10dab03e57 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/settings/JpaAdminSettingsDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/settings/JpaAdminSettingsDao.java @@ -15,8 +15,11 @@ */ package org.thingsboard.server.dao.sql.settings; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.AdminSettings; import org.thingsboard.server.dao.DaoUtil; import org.thingsboard.server.dao.model.sql.AdminSettingsEntity; @@ -27,9 +30,9 @@ import java.util.UUID; import static org.thingsboard.server.dao.model.ModelConstants.ADMIN_SETTINGS_COLUMN_FAMILY_NAME; -/** - * Created by Valerii Sosliuk on 5/6/2017. - */ +@Component +@Slf4j +@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true") public class JpaAdminSettingsDao extends JpaAbstractDao implements AdminSettingsDao{ @Autowired diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/timeseries/JpaTimeseriesDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/timeseries/JpaTimeseriesDao.java new file mode 100644 index 0000000000..e114d3dccd --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/timeseries/JpaTimeseriesDao.java @@ -0,0 +1,65 @@ +package org.thingsboard.server.dao.sql.timeseries; + +import com.datastax.driver.core.ResultSetFuture; +import com.datastax.driver.core.Row; +import com.google.common.util.concurrent.ListenableFuture; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Component; +import org.thingsboard.server.common.data.id.EntityId; +import org.thingsboard.server.common.data.kv.TsKvEntry; +import org.thingsboard.server.common.data.kv.TsKvQuery; +import org.thingsboard.server.dao.timeseries.TimeseriesDao; + +import java.util.List; + +@Component +@Slf4j +@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true") +public class JpaTimeseriesDao implements TimeseriesDao { + + @Override + public long toPartitionTs(long ts) { + return 0; + } + + @Override + public ListenableFuture> findAllAsync(EntityId entityId, List queries) { + return null; + } + + @Override + public ResultSetFuture findLatest(EntityId entityId, String key) { + return null; + } + + @Override + public ResultSetFuture findAllLatest(EntityId entityId) { + return null; + } + + @Override + public ResultSetFuture save(EntityId entityId, long partition, TsKvEntry tsKvEntry) { + return null; + } + + @Override + public ResultSetFuture savePartition(EntityId entityId, long partition, String key) { + return null; + } + + @Override + public ResultSetFuture saveLatest(EntityId entityId, TsKvEntry tsKvEntry) { + return null; + } + + @Override + public TsKvEntry convertResultToTsKvEntry(Row row) { + return null; + } + + @Override + public List convertResultToTsKvEntryList(List rows) { + return null; + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/timeseries/CassandraBaseTimeseriesDao.java b/dao/src/main/java/org/thingsboard/server/dao/timeseries/CassandraBaseTimeseriesDao.java index d6c7443944..4932e60ec2 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/timeseries/CassandraBaseTimeseriesDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/timeseries/CassandraBaseTimeseriesDao.java @@ -25,6 +25,7 @@ import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.kv.*; @@ -51,6 +52,7 @@ import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; */ @Component @Slf4j +@ConditionalOnProperty(prefix = "cassandra", value = "enabled", havingValue = "true", matchIfMissing = false) public class CassandraBaseTimeseriesDao extends CassandraAbstractAsyncDao implements TimeseriesDao { //@Value("${cassandra.query.min_aggregation_step_ms}") diff --git a/docker/.env b/docker/.env index 1c7e512569..7fa0ea81df 100644 --- a/docker/.env +++ b/docker/.env @@ -1,7 +1,12 @@ +# cassandra environment variables CASSANDRA_DATA_DIR=/home/docker/cassandra_volume -# cassandra schema container environment variables +# cassandra schema environment variables CREATE_SCHEMA=true ADD_SYSTEM_DATA=false ADD_DEMO_DATA=false -CASSANDRA_URL=cassandra \ No newline at end of file +CASSANDRA_URL=cassandra + +# postgres environment variables +POSTGRES_DATA_DIR=/home/docker/postgres_volume +POSTGRES_PASSWORD=postgres \ No newline at end of file diff --git a/docker/cassandra/Makefile b/docker/cassandra/Makefile index 15eb3ed04b..f277e9e543 100644 --- a/docker/cassandra/Makefile +++ b/docker/cassandra/Makefile @@ -7,4 +7,4 @@ build: push: build docker push ${PROJECT}/${APP}:${VERSION} - docker push ${PROJECT}/${APP}:latest \ No newline at end of file + docker push ${PROJECT}/${APP}:latest diff --git a/docker/docker-compose.static.yml b/docker/docker-compose.static.yml index 5471cb2339..1071a75317 100644 --- a/docker/docker-compose.static.yml +++ b/docker/docker-compose.static.yml @@ -24,3 +24,6 @@ services: zk: ports: - "2181:2181" + postgres: + ports: + - "5432:5432" \ No newline at end of file diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 8367abcd9b..29f162928c 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -46,3 +46,11 @@ services: ports: - "2181" restart: always + postgres: + image: "thingsboard/postgres:1.2.4" + ports: + - "5432" + environment: + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} + volumes: + - "${POSTGRES_DATA_DIR}:/var/lib/postgresql/data" \ No newline at end of file diff --git a/docker/postgres/Dockerfile b/docker/postgres/Dockerfile new file mode 100644 index 0000000000..ab997750ff --- /dev/null +++ b/docker/postgres/Dockerfile @@ -0,0 +1,21 @@ +# +# Copyright © 2016-2017 The Thingsboard Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +FROM postgres:9.6 + +ADD init-user-db.sh /docker-entrypoint-initdb.d/init-user-db.sh + +CMD ["postgres"] \ No newline at end of file diff --git a/docker/postgres/Makefile b/docker/postgres/Makefile new file mode 100644 index 0000000000..179eb6cfc5 --- /dev/null +++ b/docker/postgres/Makefile @@ -0,0 +1,10 @@ +VERSION=1.2.4 +PROJECT=thingsboard +APP=postgres + +build: + docker build --pull -t ${PROJECT}/${APP}:${VERSION} -t ${PROJECT}/${APP}:latest . + +push: build + docker push ${PROJECT}/${APP}:${VERSION} + docker push ${PROJECT}/${APP}:latest diff --git a/docker/postgres/init-user-db.sh b/docker/postgres/init-user-db.sh new file mode 100644 index 0000000000..0efc89af45 --- /dev/null +++ b/docker/postgres/init-user-db.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# +# Copyright © 2016-2017 The Thingsboard Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +set -e + +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL + CREATE DATABASE thingsboard; +EOSQL \ No newline at end of file diff --git a/docker/tb-cassandra-schema/Makefile b/docker/tb-cassandra-schema/Makefile index 68625f4d2d..2262c50611 100644 --- a/docker/tb-cassandra-schema/Makefile +++ b/docker/tb-cassandra-schema/Makefile @@ -11,4 +11,4 @@ build: push: build docker push ${PROJECT}/${APP}:${VERSION} - docker push ${PROJECT}/${APP}:latest \ No newline at end of file + docker push ${PROJECT}/${APP}:latest diff --git a/docker/tb/Makefile b/docker/tb/Makefile index 0d38e51f57..b12e5a6b5d 100644 --- a/docker/tb/Makefile +++ b/docker/tb/Makefile @@ -9,4 +9,4 @@ build: push: build docker push ${PROJECT}/${APP}:${VERSION} - docker push ${PROJECT}/${APP}:latest \ No newline at end of file + docker push ${PROJECT}/${APP}:latest diff --git a/docker/zookeeper/Makefile b/docker/zookeeper/Makefile index 911c4f3320..9ac2703a3c 100644 --- a/docker/zookeeper/Makefile +++ b/docker/zookeeper/Makefile @@ -7,4 +7,4 @@ build: push: build docker push ${PROJECT}/${APP}:${VERSION} - docker push ${PROJECT}/${APP}:latest \ No newline at end of file + docker push ${PROJECT}/${APP}:latest diff --git a/pom.xml b/pom.xml index dec1e441ef..5f87e1a1a1 100755 --- a/pom.xml +++ b/pom.xml @@ -484,6 +484,10 @@ dom4j dom4j + + antlr + antlr +