Skip to content

Commit

Permalink
Development & software operations: kite service, Dockerfile, charts, …
Browse files Browse the repository at this point in the history
…pipeline, Helm & other stuff.
  • Loading branch information
Yaroslav Savchuk authored and yivo committed Jan 10, 2018
1 parent 20f5623 commit c1305cf
Show file tree
Hide file tree
Showing 21 changed files with 775 additions and 6 deletions.
48 changes: 48 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
FROM ruby:2.5.0

# By default image is built using RAILS_ENV=production.
# You may want to customize it:
#
# --build-arg RAILS_ENV=development
#
# See https://docs.docker.com/engine/reference/commandline/build/#set-build-time-variables-build-arg
#
ARG RAILS_ENV=production
ENV RAILS_ENV ${RAILS_ENV}

ENV APP_HOME=/home/app

RUN groupadd -r app --gid=1000
RUN useradd -r -m -g app -d /home/app --uid=1000 app

RUN apt-get update \
&& apt-get install -y \
default-libmysqlclient-dev\
imagemagick

WORKDIR $APP_HOME

COPY Gemfile Gemfile.lock $APP_HOME/

# Install dependencies
RUN mkdir -p /opt/vendor/bundle && chown -R app:app /opt/vendor
RUN su app -s /bin/bash -c "bundle install --path /opt/vendor/bundle"

# Copy the main application.
COPY . $APP_HOME

RUN chown -R app:app /home/app && \
mv config/database.yml.example config/database.yml
USER app

RUN bundle exec rake tmp:create assets:precompile

# Expose port 8080 to the Docker host, so we can access it
# from the outside.
EXPOSE 8080
ENTRYPOINT ["bundle", "exec"]

# The main command to run when the container starts. Also
# tell the Rails dev server to bind to all interfaces by
# default.
CMD ["rails", "s", "-p", "8080", "-b", "0.0.0.0"]
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gem 'mysql2', '~> 0.4'
gem 'puma', '~> 3.7'
gem 'sassc-rails', '~> 1.3'
gem 'uglifier', '~> 4'
gem 'mini_racer', '~> 0.1', require: false

group :development, :test do
gem 'listen', '~> 3.1'
Expand Down
6 changes: 5 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ GEM
i18n (0.9.1)
concurrent-ruby (~> 1.0)
io-like (0.3.0)
libv8 (6.3.292.48.1)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -83,6 +84,8 @@ GEM
method_source (0.9.0)
mini_mime (1.0.0)
mini_portile2 (2.3.0)
mini_racer (0.1.15)
libv8 (~> 6.3)
minitest (5.11.1)
mysql2 (0.4.10)
nio4r (2.2.0)
Expand Down Expand Up @@ -192,6 +195,7 @@ DEPENDENCIES
chromedriver-helper (~> 1.1)
factory_bot (~> 4.8)
listen (~> 3.1)
mini_racer (~> 0.1)
mysql2 (~> 0.4)
pry-byebug (~> 3.5)
puma (~> 3.7)
Expand All @@ -202,4 +206,4 @@ DEPENDENCIES
uglifier (~> 4)

BUNDLED WITH
1.16.1
1.16.0
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
VERSION ?= $(shell cat VERSION)
ENV ?= staging
SERVICE := barong
IMAGE := gcr.io/helios-stage/barong:$(VERSION)
CURRENT_CONTEXT := $(shell kubectl config current-context)

.PHONY: default build push run ci deploy

default: build run

build:
@echo '> Using ENV: $(ENV) and context: $(CURRENT_CONTEXT)'
@echo '> Building "$(SERVICE)" docker image...'
@docker build -t $(IMAGE) .

push: build
gcloud docker -- push $(IMAGE)

run:
@echo '> Starting "$(SERVICE)" container...'
@docker run -d $(IMAGE)

ci:
@fly -t ci set-pipeline -p $(SERVICE) -c config/pipelines/review.yml -n
@fly -t ci unpause-pipeline -p $(SERVICE)

deploy:
@helm install ./config/charts/$(SERVICE) --set "image.tag=$(VERSION)"
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
4 changes: 4 additions & 0 deletions config/charts/barong/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
description: A Barong chart
name: barong
version: 0.1.0
19 changes: 19 additions & 0 deletions config/charts/barong/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }}
{{- range .Values.ingress.hosts }}
http://{{ . }}
{{- end }}
{{- else if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ template "fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
{{- else if contains "LoadBalancer" .Values.service.type }}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status of by running 'kubectl get svc -w {{ template "fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo http://$SERVICE_IP:{{ .Values.service.externalPort }}
{{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app={{ template "name" . }},release={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl port-forward $POD_NAME 8080:{{ .Values.service.internalPort }}
{{- end }}
82 changes: 82 additions & 0 deletions config/charts/barong/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{{/* vim: set filetype=mustache: */}}

{{/*
Expand the name of the chart.
*/}}
{{- define "name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Expands image name.
*/}}
{{- define "image" -}}
{{- printf "%s:%s" .Values.image.repository .Values.image.tag -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Environment for database migration job.
It is pre-install hook, so we don't have secrets created yet and we need to use plain password.
*/}}
{{- define "prepare-db-env" -}}
- name: RAILS_ENV
value: production
- name: DATABASE_HOST
value: {{ .Values.db.host }}
- name: DATABASE_USER
value: {{ default "root" .Values.db.user }}
- name: COOKIES_SECRET_KEY
value: ""
{{- if .Values.db.password }}
- name: DATABASE_PASSWORD
value: {{ .Values.db.password }}
{{- end }}
{{- end -}}
{{/*
Environment for barong container
*/}}
{{- define "env" -}}
{{- range $key, $value := .Values.barong.env }}
- name: {{ $key }}
value: {{ $value | quote }}
{{- end }}
- name: RAILS_ENV
value: production
- name: URL_HOST
value: {{ .Values.ingress.host }}
- name: URL_SCHEME
value: http{{ if .Values.ingress.tls }}s{{ end }}
- name: DATABASE_HOST
value: {{ .Values.db.host }}
- name: DATABASE_USER
value: {{ default "root" .Values.db.user }}
- name: SECRET_KEY_BASE
valueFrom:
secretKeyRef:
name: {{ template "fullname" . }}
key: cookiesSecretKey
{{- if .Values.db.password }}
- name: DATABASE_PASSWORD
valueFrom:
secretKeyRef:
name: {{ template "fullname" . }}
key: dbPassword
{{- end }}
{{- if .Values.db.name }}
- name: DATABASE_NAME
valueFrom:
secretKeyRef:
name: {{ template "fullname" . }}
key: dbName
{{- end }}
{{- end -}}
37 changes: 37 additions & 0 deletions config/charts/barong/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: {{ template "fullname" . }}
labels:
app: {{ template "name" . }}
chart: {{ .Chart.Name }}-{{ .Chart.Version }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
spec:
replicas: {{ .Values.replicaCount }}
template:
metadata:
labels:
app: {{ template "name" . }}
release: {{ .Release.Name }}
spec:
containers:
- name: {{ .Chart.Name }}
image: {{ template "image" . }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- containerPort: {{ .Values.service.internalPort }}
livenessProbe:
httpGet:
path: /
port: {{ .Values.service.internalPort }}
readinessProbe:
httpGet:
path: /
port: {{ .Values.service.internalPort }}
resources:
{{ toYaml .Values.resources | indent 12 }}
{{- if .Values.nodeSelector }}
nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
{{- end }}
36 changes: 36 additions & 0 deletions config/charts/barong/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{{- if .Values.ingress.enabled -}}
{{- $serviceName := include "fullname" . -}}
{{- $servicePort := .Values.service.externalPort -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: {{ template "fullname" . }}
labels:
app: {{ template "name" . }}
chart: {{ .Chart.Name }}-{{ .Chart.Version }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
annotations:
{{- range $key, $value := .Values.ingress.annotations }}
{{ $key }}: {{ $value | quote }}
{{- end }}
spec:
rules:
{{- range $host := .Values.ingress.hosts }}
- host: {{ $host }}
http:
paths:
- path: /
backend:
serviceName: {{ $serviceName }}
servicePort: {{ $servicePort }}
{{- end -}}
{{- if .Values.ingress.tls }}
tls:
{{- range $host := .Values.ingress.hosts }}
- secretName: {{ $host | replace "." "-" }}-barong-tls
hosts:
- {{ $host }}
{{- end -}}
{{- end -}}
{{- end -}}
19 changes: 19 additions & 0 deletions config/charts/barong/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: Service
metadata:
name: {{ template "fullname" . }}
labels:
app: {{ template "name" . }}
chart: {{ .Chart.Name }}-{{ .Chart.Version }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.externalPort }}
targetPort: {{ .Values.service.internalPort }}
protocol: TCP
name: {{ .Values.service.name }}
selector:
app: {{ template "name" . }}
release: {{ .Release.Name }}
29 changes: 29 additions & 0 deletions config/charts/barong/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Default values for barong.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
image:
repository: gcr.io/helios-stage/barong
tag: 0.1.0
pullPolicy: IfNotPresent
service:
name: barong
type: ClusterIP
externalPort: 8080
internalPort: 8080
ingress:
enabled: true
hosts:
- barong.local
annotations:
kubernetes.io/ingress.class: nginx
kubernetes.io/tls-acme: "true"
tls:
enabled: true

resources: {}

barong:
env: {}

db: {}
10 changes: 5 additions & 5 deletions config/database.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch('RAILS_MAX_THREADS', 5) %>
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
username: <%= ENV.fetch('DATABASE_USER', 'root') %>
password: <%= ENV.fetch('DATABASE_PASSWORD', 'root') %>

development:
<<: *default
database: barong_development
database: <%= ENV.fetch('DATABASE_NAME', 'barong_development') %>

test:
<<: *default
database: barong_test
database: <%= ENV.fetch('DATABASE_NAME', 'barong_test') %>

production:
<<: *default
database: barong_production
database: <%= ENV.fetch('DATABASE_NAME', 'barong_production') %>
Empty file added config/environments/.keep
Empty file.
Loading

0 comments on commit c1305cf

Please sign in to comment.