Skip to content

Commit

Permalink
Merge pull request #19 from coryan/nmaster
Browse files Browse the repository at this point in the history
Initial bigtable/api example with automated builds. (#2)
  • Loading branch information
SurferJeffAtGoogle committed Jul 26, 2017
2 parents b10b8e9 + 4120f5c commit 3ff856d
Show file tree
Hide file tree
Showing 17 changed files with 819 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "bigtable/api/googleapis"]
path = bigtable/api/googleapis
url = https://github.com/googleapis/googleapis.git
64 changes: 64 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright 2017, Google Inc.
#
# 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.

language: cpp

dist: trusty
sudo: required

matrix:
include:
- os: linux
compiler: clang
env: DOCKER_BUILD=yes DISTRO=ubuntu DISTRO_VERSION=17.04
- os: linux
compiler: clang
env: DOCKER_BUILD=yes DISTRO=fedora DISTRO_VERSION=24
- os: linux
compiler: gcc
env: DOCKER_BUILD=yes DISTRO=ubuntu DISTRO_VERSION=16.04
- os: linux
compiler: gcc
env: DOCKER_BUILD=yes DISTRO=fedora DISTRO_VERSION=25
- os: osx
compiler: clang
env: MACOSX_BUILD=yes

script:
- bigtable/api/ci/build-linux.sh
- bigtable/api/ci/build-macosx.sh

# Cache the (saved) docker images.
# With recent version of docker one can reuse a prior image as a
# source cache, that can speed up the builds, as the dependencies and
# images can be reused ...
cache:
directories:
- docker-images/ubuntu/16.04
- docker-images/ubuntu/17.04
- docker-images/fedora/24
- docker-images/fedora/25

install:
# Restore the docker image from the cached directory. That way we
# can reuse the steps in the docker image that install
# pre-requisites and build dependencies ...
- bigtable/api/ci/install-linux.sh
- bigtable/api/ci/install-macosx.sh

before_cache:
- bigtable/api/ci/cache-linux.sh

notifications:
email: false
11 changes: 11 additions & 0 deletions bigtable/api/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
# We'll use defaults from the Google Style Guide.
BasedOnStyle: Google

IncludeCategories:
- Regex: '^<google/'
Priority: 1000
- Regex: '^<grpc\+\+/'
Priority: 2000
- Regex: '^<grpc/'
Priority: 3000
8 changes: 8 additions & 0 deletions bigtable/api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
googleapis-gens
Makefile
cmake_install.cmake
CMakeFiles/
CMakeCache.txt
libgoogleapis.a
list_instances
build/
86 changes: 86 additions & 0 deletions bigtable/api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright 2017, Google Inc.
#
# 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.5)

# ... define the project name, version, and main programming language ...
set(PACKAGE_NAME "cpp-docs-samples-bigtable-api")
set(PACKAGE_VERSION "0.1")
set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
set(PACKAGE_TARNAME "${PACKAGE_NAME}-${PACKAGE_VERSION}")
set(PACKAGE_BUGREPORT "https://github.com/GoogleCloudPlatform/cpp-docs-samples/issues")
project(${PACKAGE_NAME} CXX C)

# ... configure the Compiler options, we will be using C++14 features,
# so enable that ...
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# ... the author is paranoid. Turn on all available warnings
# and turn warnings into errors to stop the build if any warning is
# emitted ...
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG(-Werror COMPILER_SUPPORTS_WERROR)
if(COMPILER_SUPPORTS_WERROR)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
endif()
CHECK_CXX_COMPILER_FLAG(-Wall COMPILER_SUPPORTS_WALL)
if(COMPILER_SUPPORTS_WALL)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
endif()
CHECK_CXX_COMPILER_FLAG(/WX COMPILER_SUPPORTS_WX)
if(COMPILER_SUPPORTS_WX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX")
endif()
CHECK_CXX_COMPILER_FLAG(/W4 COMPILER_SUPPORTS_SWALL)
if(COMPILER_SUPPORTS_SWALL)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Wall")
endif()

# ... include the functions to compile proto files ...
include(FindProtobuf)

# ... find the grpc and grpc++ libraries using pkg-config ...
include(FindPkgConfig)
pkg_check_modules(GRPCPP REQUIRED grpc++>=1.4.1)
pkg_check_modules(GRPC REQUIRED grpc>=4.0)
pkg_check_modules(PROTOBUF REQUIRED protobuf>=3.0)
link_directories(${GRPCPP_LIBRARY_DIRS} ${GRPC_LIBRARY_DIRS} ${PROTOBUF_LIBRARY_DIRS})
include_directories(${GRPCPP_INCLUDE_DIRS} ${GRPC_INCLUDE_DIRS} ${PROTOBUF_INCLUDE_DIRS})

# ... define where the googleapis protos can be found, and add that
# directory to the search path for header files ...
# Typically you can generate these using:
# $ make -C googleapis OUTPUT=$PWD/googleapis-gens
set(GOOGLEAPIS_PATH "${CMAKE_SOURCE_DIR}/googleapis")
set(GOOGLEAPIS_GENS_PATH "${GOOGLEAPIS_PATH}-gens")
include_directories("${GOOGLEAPIS_GENS_PATH}")

################################################################
# Create targets here ...

# ... discover all the generated Google API files and turn them into a
# static library ...
file(GLOB_RECURSE GOOGLEAPIS_SRCS ${GOOGLEAPIS_GENS_PATH}/*.pb.cc)
file(GLOB_RECURSE GOOGLEAPIS_HDRS ${GOOGLEAPIS_GENS_PATH}/*.pb.h)
add_library(googleapis ${GOOGLEAPIS_SRCS} ${GOOGLEAPIS_HDRS})

add_executable(list_instances list_instances.cc)
target_link_libraries(list_instances googleapis ${GRPCPP_LIBRARIES} ${GRPC_LIBRARIES} ${PROTOBUF_LIBRARIES})

add_executable(create_instance create_instance.cc)
target_link_libraries(create_instance googleapis ${GRPCPP_LIBRARIES} ${GRPC_LIBRARIES} ${PROTOBUF_LIBRARIES})

add_executable(delete_instance delete_instance.cc)
target_link_libraries(delete_instance googleapis ${GRPCPP_LIBRARIES} ${GRPC_LIBRARIES} ${PROTOBUF_LIBRARIES})
79 changes: 79 additions & 0 deletions bigtable/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Bigtable Samples.

These samples demonstrate how to call the [Google Cloud Bigtable API](https://cloud.google.com/bigtable/) using C++.

## Build and Run

1. **Create a project in the Google Cloud Console**.
If you haven't already created a project, create one now. Projects enable
you to manage all Google Cloud Platform resources for your app, including
deployment, access control, billing, and services.
1. Open the [Google Cloud Console](https://console.cloud.google.com/).
1. In the drop-down menu at the top, select Create a project.
1. Click Show advanced options. Under App Engine location, select a
United States location.
1. Give your project a name.
1. Make a note of the project ID, which might be different from the project
name. The project ID is used in commands and in configurations.

1. **Enable billing for your project**.
If you haven't already enabled billing for your project, [enable billing now](https://console.cloud.google.com/project/_/settings).
Enabling billing allows the application to consume billable resources such
as Cloud Bigtable API calls.
See [Google Cloud Console Help](https://support.google.com/cloud/answer/6288653) for more information about billing settings.

1. **Enable the Cloud Bigtable Admin APIs for your project**.
[Click here](https://console.cloud.google.com/flows/enableapi?apiid=bigtableadmin&showconfirmation=true) to visit Google Cloud Console and enable the Bigtable Admin API.

1. **Enable the Cloud Bigtable APIs for your project**.
[Click here](https://console.cloud.google.com/flows/enableapi?apiid=bigtable&showconfirmation=true) to visit Google Cloud Console and enable the Bigtable Admin API.

1. **Download service account credentials**.
These samples use service accounts for authentication.
1. Visit the [Google Cloud Console](http://cloud.google.com/console), and navigate to:
`API Manager > Credentials > Create credentials > Service account key`
1. Under **Service account**, select `New service account`.
1. Under **Service account name**, enter a service account name of your choosing. For example, `transcriber`.
1. Under **Role**, select `Project > Service Account Actor`.
1. Under **Key type**, leave `JSON` selected.
1. Click **Create** to create a new service account, and download the json credentials file.
1. Set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to your downloaded service account credentials:
```
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials-key.json
```
See the [Cloud Platform Auth Guide](https://cloud.google.com/docs/authentication#developer_workflow) for more information.

1. **Install gRPC.**
1. Visit [the gRPC github repo](https://github.com/grpc/grpc) and follow the instructions to install gRPC.
1. Then, follow the instructions in the **Pre-requisites** section to install **protoc**.

1. **Download or close this repo** with
```console
git clone https://github.com/GoogleCloudPlatform/cpp-docs-samples
cd cpp-docs-samples
git submodule update --init
```

1. **Generate googleapis gRPC source code.**
```console
cd bigtable/api
env -u LANGUAGE make -C googleapis OUTPUT=$PWD/googleapis-gens
```

1. **Compile the examples**
```console
cmake .
make -j 2
```

1. **Run the examples**
```console
# for example: list_instance my-project
./list_instances <project_id>

# for example: create_instance my-project bt-test-instance cluster00 us-east1-c
./create_instance <project_id> <instance_id> <cluster_id> <zone>

./list_instances <project_id>
./delete_instance <project_id> <instance_id>
```
33 changes: 33 additions & 0 deletions bigtable/api/ci/build-linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/sh

set -e

if [ "x${DOCKER_BUILD}" != "xyes" ]; then
echo "Not a Docker-based build, exit successfully."
exit 0
fi

IMAGE="cached-${DISTRO?}-${DISTRO_VERSION?}"
latest_id=$(sudo docker inspect -f '{{ .Id }}' ${IMAGE?}:latest >/dev/null || echo "")

echo IMAGE = $IMAGE
echo IMAGE LATEST ID = $latest_id

# TODO() - on cron buiids, we would want to disable the cache
# altogether, to make sure we can still build against recent versions
# of grpc, protobug, the compilers, etc.
cacheargs=""
if [ "x${latest_id}" != "x" ]; then
cacheargs="--cache-from ${IMAGE?}:latest"
fi

echo cache args = $cacheargs

sudo docker build -t ${IMAGE?}:tip ${cacheargs?} \
--build-arg DISTRO_VERSION=${DISTRO_VERSION?} \
--build-arg CXX=${CXX?} \
--build-arg CC=${CC?} \
--build-arg TRAVIS_JOB_NUMBER=${TRAVIS_JOB_NUMBER} \
-f bigtable/api/docker/Dockerfile.${DISTRO?} bigtable/

exit 0
24 changes: 24 additions & 0 deletions bigtable/api/ci/build-macosx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

set -e

if [ "x${MACOSX_BUILD}" != "xyes" ]; then
echo "Not a Mac OS X build, exit successfully"
exit 0
fi

cd bigtable/api
make -C googleapis OUTPUT=$PWD/googleapis-gens

# On my local workstation I prefer to keep all the build artifacts in
# a sub-directory, not so important for Travis builds, but that makes
# this script easier to test.
test -d build || mkdir build

cd build
cmake ..

make -j 2

exit 0

19 changes: 19 additions & 0 deletions bigtable/api/ci/cache-linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

set -e

if [ "x${DOCKER_BUILD}" != "xyes" ]; then
echo "Not a Docker-based build, exit successfully."
exit 0
fi

IMAGE=cached-${DISTRO?}-${DISTRO_VERSION?}
TARBALL=docker-images/${DISTRO?}/${DISTRO_VERSION?}/saved.tar.gz

# The build creates a new "*:tip" image, we want to save it as
# "*:latest" so it can be used as a cache source in the next build.
sudo docker image tag ${IMAGE?}:tip ${IMAGE?}:latest

sudo docker save ${IMAGE?}:latest | gzip - > ${TARBALL?}

exit 0
23 changes: 23 additions & 0 deletions bigtable/api/ci/install-linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh

set -e

if [ "x${DOCKER_BUILD}" != "xyes" ]; then
echo "Not a Docker-based build, exit successfully."
exit 0
fi

sudo apt-get update
sudo apt-get install -y docker-ce
sudo docker --version

TARBALL=docker-images/${DISTRO?}/${DISTRO_VERSION?}/saved.tar.gz
if [ -f ${TARBALL?} ]; then
gunzip <${TARBALL?} | sudo docker load || echo "Could not load saved image, continue without cache"
fi

echo "DEBUG output for install script"
ls -l docker-images/${DISTRO}/${DISTRO_VERSION} || /bin/true
sudo docker image ls

exit 0
19 changes: 19 additions & 0 deletions bigtable/api/ci/install-macosx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

set -e

if [ "x${MACOSX_BUILD}" != "xyes" ]; then
echo "Not a Mac OS X build, exit successfully."
exit 0
fi

brew install grpc protobuf

echo "DEBUG output for install script"
brew info grpc | grep '^/usr/local/Cellar' | awk '{print $1}'
brew info protobuf | grep '^/usr/local/Cellar' | awk '{print $1}'

pkg-config --cflags protobuf
pkg-config --cflags grpc

exit 0
Loading

0 comments on commit 3ff856d

Please sign in to comment.