Skip to content

Commit

Permalink
feat: change to swarm running
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Dec 22, 2024
1 parent de52b7a commit 30e558f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 15 deletions.
21 changes: 12 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ jobs:
mkdir ~/.ssh | true
echo "${{ secrets.SSH_KEY_BASE64 }}" | base64 --decode > ~/.ssh/id_rsa
echo "Host darkbot" > ~/.ssh/config
echo " HostName 95.217.237.232" >> ~/.ssh/config
echo " HostName 37.27.207.42" >> ~/.ssh/config
echo " User root" >> ~/.ssh/config
echo " IdentityFile ~/.ssh/id_rsa" >> ~/.ssh/config
echo " IdentitiesOnly yes" >> ~/.ssh/config
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*
ssh-keyscan -H 95.217.237.232 >> ~/.ssh/known_hosts | true
ssh-keyscan -H 37.27.207.42 >> ~/.ssh/known_hosts | true
ssh-keyscan -H darkbot >> ~/.ssh/known_hosts | true
- name: Build image
Expand All @@ -63,11 +63,14 @@ jobs:
run: |
docker push darkwind8/darkbot:${{ steps.data.outputs.BUILD_VERSION }}
docker tag darkwind8/darkbot:${{ steps.data.outputs.BUILD_VERSION }} darkwind8/darkbot:staging
docker push darkwind8/darkbot:staging
- name: Install arm64 redock at server (not amd64 in my case)
run: ssh root@darkbot 'curl -L $(curl -Ls -o /dev/null -w %{url_effective} https://github.com/darklab8/redock/releases/latest | sed "s/releases\/tag/releases\/download/")/redock-linux-arm64 -o /usr/local/bin/redock && chmod 777 /usr/local/bin/redock'

- name: Run redock
run: ssh root@darkbot 'redock --ctr=darkbot-staging --strict_pull=true --image_name=darkwind8/darkbot:${{ steps.data.outputs.BUILD_VERSION }}'
docker push darkwind8/darkbot:staging
- name: Switch service image to new one
run: |
docker pull darkwind8/darkbot:staging && docker service update --image darkwind8/darkbot:staging darkbot-staging
# could be nice having next step to fix changed terraform state ^_^
# this stuff is for Docker mode only
# - name: Install arm64 redock at server (not amd64 in my case)
# run: ssh root@darkbot 'curl -L $(curl -Ls -o /dev/null -w %{url_effective} https://github.com/darklab8/redock/releases/latest | sed "s/releases\/tag/releases\/download/")/redock-linux-arm64 -o /usr/local/bin/redock && chmod 777 /usr/local/bin/redock'
# - name: Run redock # for docker mod only needed
# run: ssh root@darkbot 'redock --ctr=darkbot-staging --strict_pull=true --image_name=darkwind8/darkbot:${{ steps.data.outputs.BUILD_VERSION }}'
36 changes: 35 additions & 1 deletion tf/modules/darkbot/docker.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "docker_image" "darkbot" {
count = var.mode == "docker" ? 1 : 0
count = var.mode == "docker" || var.mode == "swarm" ? 1 : 0
name = local.image_name
keep_locally = true
}
Expand Down Expand Up @@ -28,3 +28,37 @@ resource "docker_container" "darkbot" {
]
}
}

resource "docker_service" "darkbot" {
count = var.mode == "swarm" ? 1 : 0

name = "darkbot-${var.environment}"

task_spec {
container_spec {
image = docker_image.darkbot[0].name
env = local.envs

mounts {
target = "/code/data"
source = "/var/lib/darklab/darkbot-${var.environment}"
type = "bind"
read_only = false

bind_options {
propagation = "rprivate"
}
}
}
restart_policy {
condition = "any"
delay = "20s"
}
resources {
limits {
memory_bytes = 1000 * 1000 * 1000 # 1 gb
}
}
}
}

2 changes: 1 addition & 1 deletion tf/modules/darkbot/input.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ variable "mode" {
type = string

validation {
condition = contains(["kubernetes", "docker"], var.mode)
condition = contains(["kubernetes", "docker", "swarm"], var.mode)
error_message = "Invalid mode. should be docker or kubernetes"
}
}
Expand Down
4 changes: 2 additions & 2 deletions tf/production/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module "ssh_key" {

module "server" {
source = "../../../infra/tf/modules/hetzner_server/data"
name = "node-darkbot"
name = "node-darklab"
}

data "external" "secrets_darkbot" {
Expand All @@ -26,7 +26,7 @@ module "darkbot" {
consoler_prefix = "."
secrets = local.secrets
tag_version = "production"
mode = "docker"
mode = "swarm"
environment = "production"
debug = false
}
5 changes: 3 additions & 2 deletions tf/staging/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module "server" {
source = "../../../infra/tf/modules/hetzner_server/data"
name = "node-darkbot"
name = "node-darklab"
}

data "external" "secrets_darkbot" {
Expand All @@ -16,6 +16,7 @@ provider "docker" {
ssh_opts = ["-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", "-i", "~/.ssh/id_rsa.darklab"]
}

# IF USING mode="docker"
# limitation of `redock` usage with local terraform state. Repair container if necessary.
# Have remote accessable terraform state if u wish it being resolvable from CI automatically / or utilize docker swarm hmm
# cd tf/staging
Expand All @@ -30,6 +31,6 @@ module "darkbot" {
secrets = local.secrets
tag_version = "staging"
debug = false
mode = "docker"
mode = "swarm"
environment = "staging"
}

0 comments on commit 30e558f

Please sign in to comment.