Skip to content
This repository has been archived by the owner on Sep 23, 2021. It is now read-only.

Commit

Permalink
Deployment (#2)
Browse files Browse the repository at this point in the history
* Create deployment

* Add infra as a code

* Update readme file

* Remove example files
  • Loading branch information
tzimisce012 committed May 15, 2019
1 parent af48c20 commit b59c2a0
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 2 deletions.
17 changes: 17 additions & 0 deletions Dockerfile
@@ -0,0 +1,17 @@
FROM hseeberger/scala-sbt as builder
ARG PROJECT
WORKDIR /build
COPY project project
COPY build.sbt .
RUN sbt update
COPY . .
RUN sbt $PROJECT/universal:packageBin

FROM openjdk:8u181-jre-slim
ARG VERSION
ARG PROJECT
ENV version=$VERSION
COPY --from=builder /build/$PROJECT/target/universal/. .
RUN unzip -o ./mu-smart-home-$VERSION.zip
RUN chmod +x mu-smart-home-$VERSION/bin/mu-smart-home
ENTRYPOINT mu-smart-home-$version/bin/mu-smart-home
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -4,4 +4,6 @@

##Instructions

You need to export the environment variable `GOOGLE_APPLICATION_CREDENTIALS`. It must point to the JSON file that contains your service account key
You need to export the environment variable `GOOGLE_APPLICATION_CREDENTIALS`. It must point to the JSON file that contains your service account key.

Then, you can deploy a kubernetes cluster with `mu-smart-home` server running on it with `gcloud deployment-manager deployments create mu-example --config cloudbuild.yaml`. Also, you will deploy a bigquery table and a pubsub topic. You can modify the configuration in the `cloudbuild.yaml` configuration file
1 change: 1 addition & 0 deletions build.sbt
Expand Up @@ -12,6 +12,7 @@ lazy val shared = project

lazy val server = project
.in(file("server"))
.enablePlugins(UniversalPlugin, JavaAppPackaging)
.settings(moduleName := "mu-smart-home-server")
.settings(serverSettings)
.dependsOn(protocol)
Expand Down
38 changes: 38 additions & 0 deletions cloudbuild.yaml
@@ -0,0 +1,38 @@
imports:
- path: deployment/k8s.jinja
name: k8s.jinja
- path: deployment/docker.jinja
name: docker.jinja
- path: deployment/bigquery.jinja
name: bigquery.jinja
- path: deployment/pubsub.jinja
name: pubsub.jinja
- path: deployment/server.jinja
name: server.jinja

resources:
- name: pubsub
type: pubsub.jinja
- name: mu-table
type: bigquery.jinja
- name: mu-build
type: docker.jinja
properties:
project: server
version: 0.1.0
branch: master
- name: mu-cluster
type: k8s.jinja
properties:
description: "Mu Cluster"
zones:
- europe-west1-b
- europe-west1-c
initialNodeCount: 1
- name: mu-smart-home-server
type: server.jinja
properties:
cluster-type: $(ref.mu-cluster.clusterType)
cluster-type-apps: $(ref.mu-cluster.clusterType-apps)
cluster-type-v1beta1-extensions: $(ref.mu-cluster.clusterType-v1beta1-extensions)
version: 0.1.0
23 changes: 23 additions & 0 deletions deployment/bigquery.jinja
@@ -0,0 +1,23 @@
{% set DATASET = (env["deployment"] + "-dataset")|replace("-","_") %}
{% set TABLE = (env["deployment"] + "-table")|replace("-","_") %}

resources:
- name: {{ DATASET }}
type: gcp-types/bigquery-v2:datasets
properties:
datasetReference:
datasetId: {{ DATASET }}
- name: {{ TABLE }}
type: gcp-types/bigquery-v2:tables
properties:
datasetId: $(ref.{{ DATASET }}.datasetReference.datasetId)
tableReference:
tableId: {{ TABLE }}
schema:
fields:
- name: timestamp
type: TIMESTAMP
- name: lat
type: FLOAT
- name: long
type: FLOAT
33 changes: 33 additions & 0 deletions deployment/docker.jinja
@@ -0,0 +1,33 @@
{% set IMAGE = "eu.gcr.io/" + env["project"] + "/mu-smart-home:" + properties['version'] %}

resources:
- name: build-mu
action: gcp-types/cloudbuild-v1:cloudbuild.projects.builds.create
metadata:
runtimePolicy:
- UPDATE_ON_CHANGE
properties:
steps:
- name: gcr.io/cloud-builders/git
args:
- clone
- https://github.com/47deg/mu-smart-home.git
- name: gcr.io/cloud-builders/git
dir: mu-smart-home/
args:
- checkout
- {{ properties['branch'] }}
- name: gcr.io/cloud-builders/docker
dir: mu-smart-home/
args:
- build
- --build-arg
- PROJECT={{ properties['project'] }}
- --build-arg
- VERSION={{ properties['version'] }}
- -t
- {{ IMAGE }}
- .
images:
- {{ IMAGE }}

66 changes: 66 additions & 0 deletions deployment/k8s.jinja
@@ -0,0 +1,66 @@
{% set K8S_ENDPOINTS = {
'': 'api/v1',
'-apps': 'apis/apps/v1',
'-rbac': 'apis/rbac.authorization.k8s.io/v1',
'-v1beta1-extensions': 'apis/extensions/v1beta1'
} %}

resources:
- name: {{ env['name'] }}
type: container.v1.cluster
properties:
zone: {{ properties['zones'][0] }}
cluster:
description: {{ properties['description'] }}
initialClusterVersion: '1.12.7-gke.10'
locations: {{ properties['zones'] }}
nodePools:
- name: {{ env['name'] }}-pool
initialNodeCount: {{ properties['initialNodeCount'] | default(1) }}
config:
machineType: {{ properties['machineType'] | default('n1-standard-1') }}
oauthScopes:
- https://www.googleapis.com/auth/compute
- https://www.googleapis.com/auth/devstorage.read_only
- https://www.googleapis.com/auth/logging.write
- https://www.googleapis.com/auth/monitoring
management:
autoUpgrade: true
autoRepair: true

{% for typeSuffix, endpoint in K8S_ENDPOINTS.iteritems() %}
- name: {{ env['name'] }}-type{{ typeSuffix }}
type: deploymentmanager.v2beta.typeProvider
properties:
options:
validationOptions:
# Kubernetes API accepts ints, in fields they annotate with string.
# This validation will show as warning rather than failure for
# Deployment Manager.
# https://github.com/kubernetes/kubernetes/issues/2971
schemaValidation: IGNORE_WITH_WARNINGS
# According to kubernetes spec, the path parameter 'name'
# should be the value inside the metadata field
# https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md
# This mapping specifies that
inputMappings:
- fieldName: name
location: PATH
methodMatch: ^(GET|DELETE|PUT)$
value: $.ifNull($.resource.properties.metadata.name, $.resource.name)
- fieldName: metadata.name
location: BODY
methodMatch: ^(PUT|POST)$
value: $.ifNull($.resource.properties.metadata.name, $.resource.name)
- fieldName: Authorization
location: HEADER
value: >
$.concat("Bearer ", $.googleOauth2AccessToken())
descriptorUrl: https://$(ref.{{ env['name'] }}.endpoint)/swaggerapi/{{ endpoint }}
{% endfor %}

outputs:
{% for typeSuffix, endpoint in K8S_ENDPOINTS.iteritems() %}
- name: clusterType{{ typeSuffix }}
value: {{ env['name'] }}-type{{ typeSuffix }}
{% endfor %}
15 changes: 15 additions & 0 deletions deployment/pubsub.jinja
@@ -0,0 +1,15 @@
{% set topic = env['deployment'] + '-' + env['name'] + '-topic' %}
{% set subscription = env['deployment'] + '-' + env['name'] + '-subscription' %}

resources:
- name: my-topic
#type: pubsub.v1.topic
type: gcp-types/pubsub-v1:projects.topics
properties:
topic: {{ topic }}
- name: my-subscription
#type: pubsub.v1.subscription
type: gcp-types/pubsub-v1:projects.subscriptions
properties:
subscription: {{ subscription }}
topic: $(ref.my-topic.name)
62 changes: 62 additions & 0 deletions deployment/server.jinja
@@ -0,0 +1,62 @@
{% set NAME_PREFIX = env['deployment'] %}
{% set CLUSTER_TYPE = env['project'] + '/' + properties['cluster-type'] %}
{% set CLUSTER_TYPE_APPS = env['project'] + '/' + properties['cluster-type-apps'] %}
{% set CLUSTER_TYPE_BETA = env['project'] + '/' + properties['cluster-type-v1beta1-extensions'] %}
{% set DEPLOYMENT_COLLECTION = '/apis/apps/v1/namespaces/{namespace}/deployments' %}
{% set SERVICE_COLLECTION = '/api/v1/namespaces/{namespace}/services' %}
{% set IMAGE = "eu.gcr.io/" + env["project"] + "/mu-smart-home:" + properties['version'] %}

resources:
- name: {{ NAME_PREFIX }}
type: {{ CLUSTER_TYPE_APPS }}:{{ DEPLOYMENT_COLLECTION }}
metadata:
dependsOn:
- {{ properties['cluster-type-apps'] }}
properties:
apiVersion: apps/v1
kind: Deployment
namespace: {{ properties['namespace'] | default('default') }}
metadata:
labels:
app: {{ env['name'] }}
deployment: {{ env['deployment'] }}
spec:
replicas: {{ properties['replicas'] | default(2) }}
selector:
matchLabels:
app: {{ env['name'] }}
deployment: {{ env['deployment'] }}
template:
metadata:
labels:
app: {{ env['name'] }}
deployment: {{ env['deployment'] }}
spec:
containers:
- image: {{ IMAGE }}
name: mu-smart-home-server
ports:
- containerPort: 9111

- name: {{ NAME_PREFIX }}-svc
type: {{ CLUSTER_TYPE }}:{{ SERVICE_COLLECTION }}
metadata:
dependsOn:
- {{ properties['cluster-type'] }}
properties:
apiVersion: v1
kind: Service
namespace: {{ properties['namespace'] | default('default') }}
metadata:
name: {{ NAME_PREFIX }}-svc
labels:
app: {{ env['name'] }}
deployment: {{ env['deployment'] }}
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 9111
selector:
app: {{ env['name'] }}
deployment: {{ env['deployment'] }}
1 change: 1 addition & 0 deletions project/plugins.sbt
@@ -1,2 +1,3 @@
addSbtPlugin("io.higherkindness" % "sbt-mu-idlgen" % "0.18.0")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.20")
Expand Up @@ -15,4 +15,4 @@ case class PubSubConfig(topic: String,
delayThreshold: Int
)

case class Row(timestamp: Instant, lat: Double, lon: Double)
case class Row(timestamp: Instant, lat: Double, long: Double)

0 comments on commit b59c2a0

Please sign in to comment.