Parcourir la source

Added README and help

Lukas Cerny il y a 6 ans
Parent
commit
d75aec1828

+ 1 - 1
Dockerfile

@@ -6,7 +6,7 @@ COPY . /app
 WORKDIR /app
 
 RUN mvn clean
-RUN mvn package -P $MAVEN_PROFILE -DskipTests=true
+RUN mvn package -P $MAVEN_PROFILE
 
 ENTRYPOINT if [ "$DEBUG" = "true" ] ; then \
         java -cp "bin/*" -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:5005 cz.senslog.connector.app.Main $APP_PARAMS \

+ 108 - 0
README.md

@@ -0,0 +1,108 @@
+## Docker
+
+The docker container is based on the image **zenika/alpine-maven:3-jdk8**. Uses **Java JDK 8** and **Maven 3**.
+Configuration is composed of **docker-compose.yaml** and **Dockerfile**.
+
+#### Dockerfile
+Fundamental configuration which is used as a base for all services. Contains several variables:
+
+<br />**Accessible in compile time:**
+- MAVEN_PROFILE: defines maven profile
+
+<br />**Accessible in runtime:**
+- APP_PARAMS: defines parameters for an application
+- DEBUG: defines if debugging will be enabled
+
+#### Docker-compose
+Configuration of services. This file contains configuration of each service where are defined build and runtime variables.
+
+Name of service is **fcs2** which is used for *build* and *start* the service. Build arguments contain definition of maven profile.
+Arguments variables defines in *build* section are accessible only during compile time. When the images is created, 
+the variables can not be changed. Environment variables are located in *environment* section and can be changed 
+after the build process. These variables are used to set configuration for the built. 
+Debugging can be enabled to set **DEBUG** variable to **true** and publish the port **50005**. Otherwise the debugging 
+is disable.
+ 
+##### docker-compose.yaml
+```yaml
+services:
+  fcs2:
+    container_name: fieldclimateSenslog2
+    build:
+      dockerfile: Dockerfile
+      context: .
+      args:
+         MAVEN_PROFILE: FieldClimateSenslog2
+    ports:
+      - "5005:5005"
+    restart: always
+    environment:
+        APP_PARAMS: -cf config/test.yaml
+        DEBUG: "true"
+
+```
+
+## Build & run
+To build all or a specific one service.
+```sh
+$ docker-compose build
+$ docker-compose build <service_name>  // e.g. fcs2
+```
+To start all or a specific one service.
+```sh
+$ docker-compose up
+$ docker-compose up <service>          // e.g. fcs2
+```
+
+
+## Java & Maven
+The application supports **Java 8+** and uses **Maven** as a build automation tool. Configuration for Maven contains
+profiles which defines modules that will be compiled. 
+
+It is recommended to clean entire project before compilation.
+```sh
+$ mvn clean
+```
+
+To build all modules.
+```sh
+$ mvn package
+```
+
+To build only the modules which contains modules for the Azure LoraWan and the Senslog V1.
+```sh
+$ mvn package -P LoraWanSenslog1
+```
+
+
+All modules (JARs) are compiled to the **bin** folder. To be able to start the application, it is important to add
+all JARs to classpath and to define main class (java <jvm-params> <main_class> <app_params>).
+```sh
+$ java -cp "bin/*" cz.senslog.connector.app.Main <app_params>
+```
+
+#### Parameters
+The application allows these parameters:
+```sh
+Usage: General Senslog Connector [options]
+    Options:
+      -cf, -config-file
+        Configuration file in .yaml format.
+      -h, -help
+ 
+```
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 5 - 1
connector-app/src/main/java/cz/senslog/connector/app/Application.java

@@ -48,7 +48,11 @@ class Application implements Runnable {
      */
     static Runnable init(String... args) throws IOException {
         AppConfig appConfig = AppConfig.load();
-        Parameters parameters = Parameters.parse(args);
+        Parameters parameters = Parameters.parse(appConfig, args);
+
+        if (parameters.isHelp()) {
+            return parameters::printHelp;
+        }
         return new Application(appConfig, parameters);
     }
 

+ 21 - 2
connector-app/src/main/java/cz/senslog/connector/app/config/Parameters.java

@@ -26,17 +26,25 @@ public final class Parameters {
 
     private static Logger logger = LogManager.getLogger(Parameters.class);
 
+    private JCommander jCommander;
+
     /**
      * Static method to parse input parameters.
+     * @param appConfig - main configuration of the application.
      * @param args - array of parameters in format e.g. ["-cf", "fileName"].
      * @return instance of {@code Parameters}.
      * @throws IOException throws if is chosen "-cf" or "-config-file" parameter and the file does not exist.
      */
-    public static Parameters parse(String... args) throws IOException {
+    public static Parameters parse(AppConfig appConfig, String... args) throws IOException {
         logger.debug("Parsing input parameters {}", Arrays.toString(args));
 
         Parameters parameters = new Parameters();
-        JCommander.newBuilder().addObject(parameters).build().parse(args);
+        JCommander jCommander = JCommander.newBuilder()
+                .programName(appConfig.getName())
+                .addObject(parameters).build();
+        parameters.jCommander = jCommander;
+
+        jCommander.parse(args);
 
         String configFileName = parameters.getConfigFileName();
         logger.debug("Checking existence of configuration file {}", configFileName);
@@ -48,6 +56,9 @@ public final class Parameters {
         return parameters;
     }
 
+    @Parameter(names = {"-h", "-help"}, help = true)
+    private boolean help = false;
+
     @Parameter(names = {"-cf", "-config-file"}, description = "Configuration file in .yaml format.")
     private String configFileName;
 
@@ -58,4 +69,12 @@ public final class Parameters {
     public String getConfigFileName() {
         return configFileName;
     }
+
+    public boolean isHelp() {
+        return help;
+    }
+
+    public void printHelp() {
+        jCommander.usage();
+    }
 }

+ 13 - 3
connector-app/src/test/java/cz/senslog/connector/app/config/ParametersTest.java

@@ -8,6 +8,8 @@ import java.io.FileNotFoundException;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 class ParametersTest {
 
@@ -17,7 +19,10 @@ class ParametersTest {
         File yamlConfigFile = File.createTempFile("config_test_", ".yaml");
         String [] params = new String[] {"-cf", yamlConfigFile.getAbsolutePath()};
 
-        Parameters parameters = Parameters.parse(params);
+        AppConfig appConfig = mock(AppConfig.class);
+        when(appConfig.getName()).thenReturn("Test application.");
+
+        Parameters parameters = Parameters.parse(appConfig, params);
 
         assertEquals(yamlConfigFile.getAbsolutePath(), parameters.getConfigFileName());
     }
@@ -29,7 +34,10 @@ class ParametersTest {
         // 'cf' was changed to wrong parameter 'cp'
         String [] params = new String[] {"-cp", yamlConfigFile.getAbsolutePath()};
 
-        assertThrows(ParameterException.class, () -> Parameters.parse(params));
+        AppConfig appConfig = mock(AppConfig.class);
+        when(appConfig.getName()).thenReturn("Test application.");
+
+        assertThrows(ParameterException.class, () -> Parameters.parse(appConfig, params));
     }
 
     @Test
@@ -37,7 +45,9 @@ class ParametersTest {
 
         // Config file 'config.yaml' does not exist
         String [] params = new String[] {"-cf", "config.yaml"};
+        AppConfig appConfig = mock(AppConfig.class);
+        when(appConfig.getName()).thenReturn("Test application.");
 
-        assertThrows(FileNotFoundException.class, () -> Parameters.parse(params));
+        assertThrows(FileNotFoundException.class, () -> Parameters.parse(appConfig, params));
     }
 }