Skip to content

Commit

Permalink
Merge pull request #27 from o0th/midnacoin
Browse files Browse the repository at this point in the history
kubernetes deployment
  • Loading branch information
o0th authored Jul 14, 2021
2 parents 0243d9e + 3915df1 commit 83ad612
Show file tree
Hide file tree
Showing 16 changed files with 254 additions and 116 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
npm-debug.log

.env

infra
40 changes: 40 additions & 0 deletions .github/workflows/midnabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: midnabot

on:
release:
types:
- created

jobs:
docker:
name: Docker
runs-on: ubuntu-latest
steps:

- name: Set up source
uses: actions/checkout@v2

- name: Set up version
id: version
uses: martinbeentjes/npm-get-version-action@master

- name: Set up QEMU
uses: docker/setup-qemu-action@v1

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Docker login
run: |
echo "${{ secrets.DOCKER_PASSWORD }}" | docker login \
--username ${{ secrets.DOCKER_USERNAME }} \
--password-stdin ${{ secrets.DOCKER_REPOSITORY }}
- name: Docker build and push
run: |
docker buildx build \
--platform linux/arm64 \
--tag ${{ secrets.DOCKER_REPOSITORY }}/midnabot:${{ steps.version.outputs.current-version }} \
--build-arg SERVICE_PORT=${{ secrets.SERVICE_PORT }} \
--push .
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
node_modules

*terraform*
*.tfvars
.terraform

.env
init.sh

infra/secrets/*
!infra/secrets/*.age
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM node:16-alpine

ARG SERVICE_PORT
ENV SERVICE_PORT=${SERVICE_PORT}

EXPOSE ${SERVICE_PORT}

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install
COPY . .

CMD ["node", "index.js"]
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { spam } = require('./modules/spam')
const { about } = require('./modules/about')
const { crypto } = require('./modules/crypto')

const bot = new Telegraf(process.env.TELEGRAM)
const bot = new Telegraf(process.env.TELEGRAM_TOKEN)

bot.use(logs)
bot.use(spam)
Expand All @@ -23,8 +23,8 @@ const development = () => {
/** Start bot in production mode (webhook) */
const production = () => {
process.stdout.write('Bot starting in production mode...\n')
const domain = process.env.PUBLIC_URL
const port = process.env.PORT
const domain = process.env.SERVICE_URL
const port = Number(process.env.SERVICE_PORT)
bot.launch({ webhook: { domain, port } })
}

Expand Down
155 changes: 155 additions & 0 deletions infra/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
provider "kubernetes" {
config_path = "~/.kube/config"
}

resource "kubernetes_namespace" "this" {
metadata {
name = "midnabot"
}
}

resource "kubernetes_secret" "this" {
metadata {
name = "midnabot"
namespace = kubernetes_namespace.this.metadata[0].name
}

type = "kubernetes.io/dockerconfigjson"

data = {
".dockerconfigjson" = file("${path.module}/secrets/docker-registry.json")
}
}

resource "kubernetes_deployment" "this" {
metadata {
name = "midnabot"
namespace = kubernetes_namespace.this.metadata[0].name
}

spec {
replicas = 1

selector {
match_labels = {
"app" = "midnabot"
}
}

template {
metadata {
labels = {
"app" = "midnabot"
}
}

spec {
container {
name = "midnabot"
image = var.image

image_pull_policy = "Always"

env {
name = "NODE_ENV"
value = var.node_env
}

env {
name = "SERVICE_PORT"
value = var.service_port
}

env {
name = "SERVICE_URL"
value = var.service_url
}

env {
name = "TELEGRAM_TOKEN"
value = var.telegram_token
}

port {
name = "https"
container_port = var.service_port
}
}

image_pull_secrets {
name = kubernetes_secret.this.metadata[0].name
}
}
}
}
}

resource "kubernetes_service" "this" {
metadata {
name = "midnabot"
namespace = kubernetes_namespace.this.metadata[0].name

labels = {
"app" = "midnabot"
}
}

spec {
selector = {
"app" = "midnabot"
}

port {
name = "https"
port = var.service_port
}
}
}

resource "kubernetes_secret" "cert" {
metadata {
name = "cert"
namespace = kubernetes_namespace.this.metadata[0].name
}

type = "kubernetes.io/tls"

data = {
"tls.crt" = file("${path.module}/secrets/tls.crt")
"tls.key" = file("${path.module}/secrets/tls.key")
}
}

resource "kubernetes_ingress" "this" {
wait_for_load_balancer = true

metadata {
name = "midnabot"
namespace = kubernetes_namespace.this.metadata[0].name
annotations = {
"kubernetes.io/ingress.class" = "public"
"nginx.ingress.kubernetes.io/proxy-body-size" = "1024m"
}
}

spec {
tls {
hosts = ["midnabot.o0th.io"]
secret_name = kubernetes_secret.cert.metadata[0].name
}

rule {
host = "midnabot.o0th.io"
http {
path {
path = "/"
backend {
service_name = "midnabot"
service_port = var.service_port
}
}
}
}
}
}

Binary file added infra/secrets/docker-registry.json.age
Binary file not shown.
Binary file added infra/secrets/tls.crt.age
Binary file not shown.
Binary file added infra/secrets/tls.key.age
Binary file not shown.
20 changes: 20 additions & 0 deletions infra/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
variable "service_url" {
type = string
}

variable "service_port" {
type = number
}

variable "node_env" {
type = string
}

variable "telegram_token" {
type = string
}

variable "image" {
type = string
}

25 changes: 0 additions & 25 deletions infrastructures/.terraform.lock.hcl

This file was deleted.

54 changes: 0 additions & 54 deletions infrastructures/main.tf

This file was deleted.

14 changes: 0 additions & 14 deletions infrastructures/providers.tf

This file was deleted.

9 changes: 0 additions & 9 deletions infrastructures/variables.tf

This file was deleted.

Loading

0 comments on commit 83ad612

Please sign in to comment.