-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impl(batch/cpp_application): add a cloud batch example for a cpp appl…
…ication
- Loading branch information
1 parent
94ecd2d
commit 92cc70f
Showing
11 changed files
with
460 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ cmake-build-debug/ | |
.build/ | ||
.vscode/ | ||
cpp-samples-checkers/ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# ~~~ | ||
# Copyright 2024 Google LLC | ||
# | ||
# 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. | ||
# ~~~ | ||
|
||
cmake_minimum_required(VERSION 3.20) | ||
|
||
# Define the project name and where to report bugs. | ||
set(PACKAGE_BUGREPORT | ||
"https://github.com/GoogleCloudPlatform/cpp-samples/issues") | ||
project(cpp-samples-batch CXX) | ||
|
||
find_package(google_cloud_cpp_batch REQUIRED) | ||
|
||
add_executable(main main.cc) | ||
target_link_libraries("main" PRIVATE google-cloud-cpp::batch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# Using Cloud Batch | ||
|
||
This example shows how to run a C++ application on Cloud Batch job using C++. | ||
|
||
If you are not familiar with the Batch API, we recommend you first read the | ||
[API overview] before starting this guide. | ||
|
||
## The example | ||
|
||
The following steps are included: | ||
|
||
1. Create a docker image | ||
1. Upload it to Artifact registry | ||
1. Create the job | ||
1. Poll until the job finishes | ||
|
||
## Pre-reqs | ||
|
||
1. Install the [gcloud CLI](https://cloud.google.com/sdk/docs/install). | ||
1. Install [docker](https://docs.docker.com/engine/install/). | ||
|
||
## 1. Create the docker image | ||
|
||
The instructions are [here](application/README.md). | ||
|
||
## 2. Upload it to Artifact registry | ||
|
||
1. \[If it does not already exist\] Create the artifact repository | ||
1. Build the image locally | ||
1. Tag and push the image to the artifact repository | ||
|
||
### 1. Create the artifact repository | ||
|
||
To run this example, replace the `[PROJECT ID]` placeholder with the id of your | ||
project: | ||
|
||
Authorize via gcloud cli | ||
|
||
```shell | ||
PROJECT_ID=[PROJECT_ID] | ||
LOCATION="us-central1" | ||
REPOSITORY="application-repo" | ||
|
||
gcloud auth login | ||
gcloud config set project ${PROJECT_ID} | ||
# Create the repository | ||
gcloud artifacts repositories create ${REPOSITORY} \ | ||
--repository-format=docker \ | ||
--location=${LOCATION} \ | ||
--description="Store the example C++ application" \ | ||
--immutable-tags \ | ||
--async | ||
``` | ||
|
||
<details> | ||
<summary>To verify repo was created</summary> | ||
``` | ||
gcloud artifacts repositories list | ||
``` | ||
|
||
You should see something like | ||
|
||
``` | ||
application-repo DOCKER STANDARD_REPOSITORY Store the example C++ application us-central1 Google-managed key 2024-05-13T20:07:11 2024-05-13T20:07:11 0 | ||
``` | ||
|
||
</details> | ||
|
||
### 2. Build the image locally | ||
|
||
``` | ||
cd batch/cpp_application/application | ||
docker build --tag=application-image:latest . | ||
``` | ||
|
||
### 3. Tag and push the image to the artifact repository | ||
|
||
``` | ||
# Tag the image | ||
docker tag application-image:latest ${LOCATION}-docker.pkg.dev/${PROJECT_ID}/${REPOSITORY}/application-image:latest | ||
# Push the image | ||
docker push ${LOCATION}-docker.pkg.dev/${PROJECT_ID}/${REPOSITORY}/application-image:latest | ||
``` | ||
|
||
## 3. Create the job | ||
|
||
## Compiling the Example | ||
|
||
This project uses `vcpkg` to install its dependencies. Clone `vcpkg` in your | ||
`$HOME`: | ||
|
||
```shell | ||
git clone -C $HOME https://github.com/microsoft/vcpkg.git | ||
``` | ||
|
||
Install the typical development tools, on Ubuntu you would use: | ||
|
||
```shell | ||
apt update && apt install -y build-essential cmake git ninja-build pkg-config g++ curl tar zip unzip | ||
``` | ||
|
||
In this directory compile the dependencies and the code, this can take as long | ||
as an hour, depending on the performance of your workstation: | ||
|
||
```shell | ||
cd cpp-samples/batch/cpp_application | ||
cmake -S . -B .build -DCMAKE_BUILD_TYPE=Release \ | ||
-DCMAKE_TOOLCHAIN_FILE=$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake | ||
cmake --build .build | ||
``` | ||
|
||
## Run the sample | ||
|
||
Run the example, replace the `[PROJECT ID]` placeholder with the id of your | ||
project: | ||
|
||
```shell | ||
.build/main [PROJECT ID] us-central1 cpp-application-run application.json application-repo | ||
``` | ||
|
||
This submits the batch job and then polls until the job is complete. | ||
|
||
[api overview]: https://cloud.google.com/batch/docs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"taskGroups": [ | ||
{ | ||
"taskSpec": { | ||
"runnables": [ | ||
{ | ||
"container": { | ||
"imageUri": "", | ||
} | ||
} | ||
], | ||
"computeResource": { | ||
"cpuMilli": 2000, | ||
"memoryMib": 16 | ||
}, | ||
"maxRetryCount": 2, | ||
"maxRunDuration": "3600s" | ||
}, | ||
"taskCount": 1, | ||
"parallelism": 1 | ||
} | ||
], | ||
"allocationPolicy": { | ||
"instances": [ | ||
{ | ||
"policy": { "machineType": "e2-standard-4" } | ||
} | ||
] | ||
}, | ||
"labels": { | ||
"env": "testing" | ||
}, | ||
"logsPolicy": { | ||
"destination": "CLOUD_LOGGING" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# ~~~ | ||
# Copyright 2024 Google LLC | ||
# | ||
# 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. | ||
# ~~~ | ||
|
||
cmake_minimum_required(VERSION 3.20) | ||
|
||
# Define the project name and where to report bugs. | ||
set(PACKAGE_BUGREPORT | ||
"https://github.com/GoogleCloudPlatform/cpp-samples/issues") | ||
project(cpp-samples-batch CXX) | ||
|
||
add_executable(main src/main.cc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# 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. | ||
|
||
# We chose Alpine to build the image because it has good support for creating | ||
# statically-linked, small programs. | ||
FROM alpine:3.19 AS build | ||
|
||
# Install the typical development tools for C++, and | ||
# the base OS headers and libraries. | ||
RUN apk update && \ | ||
apk add \ | ||
build-base \ | ||
cmake \ | ||
curl \ | ||
git \ | ||
gcc \ | ||
g++ \ | ||
libc-dev \ | ||
linux-headers \ | ||
ninja \ | ||
pkgconfig \ | ||
tar \ | ||
unzip \ | ||
zip | ||
|
||
# Copy the source code to /src and compile it. | ||
COPY . /src | ||
WORKDIR /src | ||
|
||
# Run the CMake configuration step, setting the options to create | ||
# a statically linked C++ program | ||
RUN cmake -S /src -B /build -GNinja \ | ||
-DCMAKE_BUILD_TYPE=Release | ||
|
||
# Compile the binary and strip it to reduce its size. | ||
RUN cmake --build /build | ||
RUN strip /build/main | ||
|
||
# Make the program the entry point. | ||
ENTRYPOINT [ "/build/main" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# To build and run | ||
|
||
``` | ||
cmake -S . -B build | ||
cmake --build build | ||
build/main | ||
``` | ||
|
||
# To create docker image | ||
|
||
``` | ||
docker build --tag=application-image:latest . | ||
``` | ||
|
||
## To run and enter your image | ||
|
||
``` | ||
docker run -it --entrypoint bash application-image:latest | ||
``` | ||
|
||
To exit container, type `exit` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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. | ||
|
||
#include <iostream> | ||
|
||
int main(int argc, char* argv[]) { std::cout << "Running my application\n"; } |
Oops, something went wrong.