Skip to content

cuba-labs/gaef-sample

Repository files navigation

Sample CUBA Project to deploy to Google App Engine

Here are steps to deploy and run the CUBA application in Google App Engine Flexible.

Google Cloud SDK Installation

Install the Cloud SDK. See the documentation.

When the installation is finished, create a new project and name it cuba-sample. If you skip this step, then you’ll need to create the project in the web console.

Install the app-engine-java component: gcloud components install app-engine-java.

Create Google App Engine Project

If you skipped project creation, use the GCP Console to do it. Open the console and click on the sample project name on the top of the screen. Create new project.

Create Application, Step 1

Give project the name cuba-sample. Create an application using Java language

Create Application, Step 2

Specify a region. The default on is US-Central zone. You can choose another if you'd like to.

Create Application, Step 3

Please note project ID – in this case it is cuba-sample-278518

Create Google Cloud Database

Choose SQL menu and click on it.

Create Database, Step 1

Create cloud SQL instance

Create Database, Step 2

Select PostgreSQL

Create Database, Step 3

Assign the instance name. In this case, it is cuba-sample-db. Specify the password for the postgres user (postgres in this case) and select the region. Here we choose the same region as for the application: US-Central

Create Database, Step 4

Create Database, Step 5

Dive into the DB instance settings

Create Database, Step 6

and create a new database. For this application, its name is gaef.

Create Database, Step 7

Create Database, Step 8

As stated in this documentation section you’ll need to enable the Cloud SQL Admin API. Follow the “Enable API” link from this section and select a cuba-sample project in the appeared dialog. Click “Continue”.

Create Database, Step 9

Specify Database for CUBA Application

Open the CUBA-> Main Data Store Settings in the CUBA Studio and change the database type to PostgreSQL, datasource should be defined in application.

Connect App to DB, Step 1

Open the CUBA -> Deployment -> Edit UberJar Settings menu and specify Uber JAR deployment options as shown on the picture:

  • Check the ‘Build Uber JAR’ checkbox
  • Generate the Logback configuration file

Connect App to DB, Step 2

We will use runtime profiles to change database connection properties for Google Application Engine deploy. Create file gae-app.properties in CORE module.

Connect App to DB, Step 3

Add GAE-specific PostgreSQL connection. The database URL should conform the format described in the manual, i.e.:

jdbc:postgresql://google/${database}?useSSL=false& cloudSqlInstance=${INSTANCE_CONNECTION_NAME}& socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=${user}& password=${password}

Instance connection name can be copied from the database properties in the web console.

Connect App to DB, Step 4

For this application, the profile-specific properties will look like this:

cuba.dataSource.username=postgres
cuba.dataSource.password=postgres
cuba.dataSource.dbName=gaef
cuba.dataSource.host=google
cuba.dataSource.connectionParams=?useSSL=false&cloudSqlInstance=cuba-sample-278518:us-central1:cuba-sample-db&socketFactory=com.google.cloud.sql.postgres.SocketFactory

To connect deployed application to the cloud database, you need to enable VPC connector according to documentation.

Connect App to DB, Step 5

Prepare the Application for Deploy to Google App Engine

Create the app.yaml file in the appengine directory in the root of the project. Please note that we use project ID and conector name that we defined earlier. Entrypoint is important, in this line we specify the following:

  • -Dapp.home=/tmp/app_home - specifies temporary folder where CUBA stores its settings
  • -Dspring.profiles.active=gae - enables settings values specified in gae-app.properties file. Double check that spring profile name and file name suffix are the same.
runtime: java11
manual_scaling:
  instances: 1
entrypoint: "java -Dapp.home=/tmp/app_home -Dspring.profiles.active=gae -jar app.jar"
vpc_access_connector:
  name: "projects/cuba-sample-278518/locations/us-central1/connectors/gaef-connector"

You can find more about app.yaml syntax in the documentation.

Update Application Build Script

The next step - configure deployment using the Gradle plugin.

Add a dependency - appengine-gradle-plugin in the build.gradle:

dependencies {
    classpath "com.haulmont.gradle:cuba-plugin:$cubaVersion"
    classpath 'com.google.cloud.tools:appengine-gradle-plugin:2.2.0'
}

In the end of the build.gradle file add the required gradle tasks:

apply plugin: 'com.google.cloud.tools.appengine'

appengine {
    stage {
        artifact = "$buildDir/distributions/uberJar/app.jar"
        appEngineDirectory = 'appengine'    // a directory with app.yaml
        stagingDirectory = "$buildDir/staged-app"
    }

    deploy {
        projectId = 'cuba-sample-278518'     // specify a project id if the current project is not the default one
        stopPreviousVersion = true           // default - stop the current version
        promote = true                       // default - & make this the current version
        version = 'GCLOUD_CONFIG'
    }
}

appengineStage.dependsOn(buildUberJar)

// a dummy task. It is required for appengineStage task of the google plugin
task assemble {
    doLast {}
}

In the dependencies section of the coreModule add a dependency to the postgres-socket-factory:

dependencies {
    compile(globalModule)
    compileOnly(servletApi)
    jdbc(postgres)
    jdbc('com.google.cloud.sql:postgres-socket-factory:1.0.16') {
        exclude group: 'com.google.guava', module: 'guava'
    }
    testRuntime(postgres)
}

Please note the exclusion of the guava library in the dependencies. If you don’t do it, you can get a problem because of CUBA’s and SocketFactory libraries clash.

java.lang.NoSuchMethodError: 'java.util.stream.Collector com.google.common.collect.ImmutableList.toImmutableList()'

Change the PostgreSQL JDBC driver version:

def postgres = 'org.postgresql:postgresql:42.2.12'

Then run the appengineDeploy gradle task and the project should be deployed:

./gradlew appengineDeploy

After the deployment is completed, open the application URL in the browser and add the /app to the end of the URL similar to this: https://cuba-sample-278518.uc.r.appspot.com/app

An important note here. You will not be able to save files using the standard FileStorage. If your application uses it, then you 'll have to do something with files persistence. There is a sample implementation of such storage can be foung on GitHub.

About

Google App Engine Flexible Deployment

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages