Compare commits
35 Commits
feature/me
...
main
Author | SHA1 | Date | |
---|---|---|---|
2c6c74fe73 | |||
0f995dadbd | |||
862278c419 | |||
4dbb2a1573 | |||
f3bd1f0ee8 | |||
18bde2e1b9 | |||
722793830b | |||
b56c0edd1f | |||
6c0d57b60e | |||
d0ad44a8e8 | |||
754368ffce | |||
3796dcce50 | |||
eb0db5f633 | |||
383f2c76fb | |||
a866be86f1 | |||
1ecfc17231 | |||
a76d9813bd | |||
faf93e7eb5 | |||
f1494b1817 | |||
44ad0306a9 | |||
467993dee3 | |||
e6b3321c2a | |||
140067aa15 | |||
8d4f4c9ab6 | |||
4d8a40fe8e | |||
e48e6f0c3d | |||
1f7b711048 | |||
e014270903 | |||
5c9f96be8e | |||
9eaca5ae11 | |||
1cb55b1fb3 | |||
ecc2e332da | |||
ede5600da5 | |||
b4ee531a81 | |||
1663a469c2 |
56
.drone.yml
56
.drone.yml
@ -1,30 +1,36 @@
|
|||||||
|
type: docker
|
||||||
kind: pipeline
|
kind: pipeline
|
||||||
name: Serverctl
|
name: Serverctl
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: terraform plan
|
- name: test
|
||||||
image: alpine
|
image: harbor.front.kjuulh.io/docker-proxy/library/bash:latest
|
||||||
environment:
|
|
||||||
HCLOUD_TOKEN:
|
|
||||||
from_secret: serverctl_hcloud_token
|
|
||||||
ACCESS_KEY:
|
|
||||||
from_secret: serverctl_access_key
|
|
||||||
SECRET_KEY:
|
|
||||||
from_secret: serverctl_secret_key
|
|
||||||
SSH_ZIP_KEY:
|
|
||||||
from_secret: serverctl_ssh_zip_key
|
|
||||||
HCLOUD_SSH_KEY_ID:
|
|
||||||
from_secret: serverctl_hcloud_ssh_key_id
|
|
||||||
commands:
|
commands:
|
||||||
- apk --update add curl zip ansible python3
|
- echo 'Run tests'
|
||||||
- cd infrastructure && ./unzip-ssh-keys.sh "$SSH_ZIP_KEY" && cd ..
|
#
|
||||||
- curl --silent --output terraform.zip "https://releases.hashicorp.com/terraform/1.1.6/terraform_1.1.6_linux_amd64.zip"
|
# - name: terraform plan
|
||||||
- unzip terraform.zip ; rm -f terraform.zip; chmod +x terraform
|
# image: alpine
|
||||||
- mkdir -p ${HOME}/bin ; export PATH=${PATH}:${HOME}/bin; mv terraform ${HOME}/bin/
|
# environment:
|
||||||
- terraform -v
|
# HCLOUD_TOKEN:
|
||||||
- cd infrastructure/create-resources
|
# from_secret: serverctl_hcloud_token
|
||||||
- terraform init -backend-config="access_key=$ACCESS_KEY" -backend-config="secret_key=$SECRET_KEY"
|
# ACCESS_KEY:
|
||||||
- terraform validate
|
# from_secret: serverctl_access_key
|
||||||
- terraform apply -auto-approve -var "hcloud_token=$HCLOUD_TOKEN" -var "pvt_key=../ssh_keys/id_ed25519" -var "pub_key=../ssh_keys/id_ed25519.pub" -var "hcloud_serverctl_ssh_key_id=$HCLOUD_SSH_KEY_ID"
|
# SECRET_KEY:
|
||||||
- cd ansible
|
# from_secret: serverctl_secret_key
|
||||||
- ANSIBLE_HOST_KEY_CHECKING=False /usr/bin/ansible-playbook -u root --key-file '../../ssh_keys/id_ed25519' -e 'pub_key=../../ssh_keys/id_ed25519.pub' site.yml
|
# SSH_ZIP_KEY:
|
||||||
|
# from_secret: serverctl_ssh_zip_key
|
||||||
|
# HCLOUD_SSH_KEY_ID:
|
||||||
|
# from_secret: serverctl_hcloud_ssh_key_id
|
||||||
|
# commands:
|
||||||
|
# - apk --update add curl zip ansible python3
|
||||||
|
# - cd infrastructure && ./unzip-ssh-keys.sh "$SSH_ZIP_KEY" && cd ..
|
||||||
|
# - curl --silent --output terraform.zip "https://releases.hashicorp.com/terraform/1.1.6/terraform_1.1.6_linux_amd64.zip"
|
||||||
|
# - unzip terraform.zip ; rm -f terraform.zip; chmod +x terraform
|
||||||
|
# - mkdir -p ${HOME}/bin ; export PATH=${PATH}:${HOME}/bin; mv terraform ${HOME}/bin/
|
||||||
|
# - terraform -v
|
||||||
|
# - cd infrastructure/create-resources
|
||||||
|
# - terraform init -backend-config="access_key=$ACCESS_KEY" -backend-config="secret_key=$SECRET_KEY"
|
||||||
|
# - terraform validate
|
||||||
|
# - terraform apply -auto-approve -var "hcloud_token=$HCLOUD_TOKEN" -var "pvt_key=../ssh_keys/id_ed25519" -var "pub_key=../ssh_keys/id_ed25519.pub" -var "hcloud_serverctl_ssh_key_id=$HCLOUD_SSH_KEY_ID"
|
||||||
|
# - cd ansible
|
||||||
|
# - ANSIBLE_HOST_KEY_CHECKING=False /usr/bin/ansible-playbook -u root --key-file '../../ssh_keys/id_ed25519' -e 'pub_key=../../ssh_keys/id_ed25519.pub' site.yml
|
||||||
|
@ -52,29 +52,10 @@ services:
|
|||||||
logging: *loki-logging
|
logging: *loki-logging
|
||||||
depends_on:
|
depends_on:
|
||||||
- db_migrator
|
- db_migrator
|
||||||
- rabbitmq
|
|
||||||
|
|
||||||
# Messaging
|
|
||||||
rabbitmq:
|
|
||||||
image: docker.io/bitnami/rabbitmq:latest
|
|
||||||
ports:
|
|
||||||
- '4369:4369'
|
|
||||||
- '5551:5551'
|
|
||||||
- '5552:5552'
|
|
||||||
- '5672:5672'
|
|
||||||
- '25672:25672'
|
|
||||||
- '15672:15672'
|
|
||||||
networks:
|
|
||||||
- back-tier
|
|
||||||
environment:
|
|
||||||
- RABBITMQ_USERNAME=serverctl
|
|
||||||
- RABBITMQ_PASSWORD=serverctlsecret
|
|
||||||
volumes:
|
|
||||||
- 'rabbitmq_data:/bitnami/rabbitmq/mnesia'
|
|
||||||
|
|
||||||
# Logging
|
# Logging
|
||||||
loki:
|
loki:
|
||||||
image: grafana/loki:2.4.2
|
image: grafana/loki:2.7.0
|
||||||
ports:
|
ports:
|
||||||
- 3100
|
- 3100
|
||||||
networks:
|
networks:
|
||||||
@ -85,7 +66,7 @@ services:
|
|||||||
logging: *loki-logging
|
logging: *loki-logging
|
||||||
|
|
||||||
promtail:
|
promtail:
|
||||||
image: grafana/promtail:2.4.2
|
image: grafana/promtail:2.7.0
|
||||||
volumes:
|
volumes:
|
||||||
- ./services/logs/promtail/config.yaml:/mnt/config/promtail-config.yaml
|
- ./services/logs/promtail/config.yaml:/mnt/config/promtail-config.yaml
|
||||||
- /var/lib/docker/containers:/host/containers
|
- /var/lib/docker/containers:/host/containers
|
||||||
@ -133,4 +114,3 @@ volumes:
|
|||||||
db_data: {}
|
db_data: {}
|
||||||
prometheus_data: {}
|
prometheus_data: {}
|
||||||
grafana_data: {}
|
grafana_data: {}
|
||||||
rabbitmq_data: {}
|
|
||||||
|
@ -2,6 +2,14 @@
|
|||||||
k3s_version: v1.22.3+k3s1
|
k3s_version: v1.22.3+k3s1
|
||||||
ansible_user: root
|
ansible_user: root
|
||||||
systemd_dir: /etc/systemd/system
|
systemd_dir: /etc/systemd/system
|
||||||
master_ip: "{{ hostvars[groups['serverctl_master_hosts'][0]]['ansible_host'] | default(groups['serverctl_master_hosts'][0]) }}"
|
systemd_network_dir: /etc/systemd/network
|
||||||
extra_server_args: ""
|
master_ip: "{{ hostvars[groups['serverctl_master_hosts'][0]]['wireguard_ip'] | default(groups['serverctl_master_hosts'][0]) }}"
|
||||||
extra_agent_args: ""
|
extra_server_args: "--flannel-iface=serverctl-wg0"
|
||||||
|
extra_agent_args: "--flannel-iface=serverctl-wg0"
|
||||||
|
|
||||||
|
ansible_become_method: su
|
||||||
|
|
||||||
|
ufw_enabled: true
|
||||||
|
|
||||||
|
wireguard_mask_bits: 24
|
||||||
|
wireguard_port: 51871
|
32
infrastructure/create-resources/ansible/inventory/hosts.cfg
Executable file
32
infrastructure/create-resources/ansible/inventory/hosts.cfg
Executable file
@ -0,0 +1,32 @@
|
|||||||
|
[serverctl_master_hosts]
|
||||||
|
95.217.155.228 ansible_host=95.217.155.228 wireguard_ip=10.1.1.1
|
||||||
|
|
||||||
|
[serverctl_node_hosts]
|
||||||
|
65.21.50.146 ansible_host=65.21.50.146 wireguard_ip=10.1.1.10
|
||||||
|
95.216.162.16 ansible_host=95.216.162.16 wireguard_ip=10.1.1.11
|
||||||
|
|
||||||
|
[serverctl_home_servers]
|
||||||
|
192.168.1.150 ansible_host=192.168.1.150 wireguard_ip=10.1.1.8
|
||||||
|
#192.168.1.233 ansible_host=192.168.1.233 wireguard_ip=10.1.1.9
|
||||||
|
|
||||||
|
[serverctl_cluster:children]
|
||||||
|
serverctl_master_hosts
|
||||||
|
serverctl_node_hosts
|
||||||
|
|
||||||
|
[serverctl_super_cluster:children]
|
||||||
|
serverctl_cluster
|
||||||
|
serverctl_home_servers
|
||||||
|
|
||||||
|
[serverctl_home_servers:vars]
|
||||||
|
client_server=True
|
||||||
|
|
||||||
|
[serverctl_super_cluster:vars]
|
||||||
|
pipelining=true
|
||||||
|
ansible_ssh_user=root
|
||||||
|
ansible_ssh_port=22
|
||||||
|
|
||||||
|
[serverctl_cluster:vars]
|
||||||
|
client_server=False
|
||||||
|
pipelining=true
|
||||||
|
ansible_ssh_user=root
|
||||||
|
ansible_ssh_port=22
|
8
infrastructure/create-resources/ansible/kubeconfig.yml
Normal file
8
infrastructure/create-resources/ansible/kubeconfig.yml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
- hosts: serverctl_master_hosts[0]
|
||||||
|
become: yes
|
||||||
|
tasks:
|
||||||
|
- name: Fetch kubeconfig
|
||||||
|
ansible.builtin.fetch:
|
||||||
|
src: ~/.kube/config
|
||||||
|
dest: temp/.kube/config
|
||||||
|
|
7
infrastructure/create-resources/ansible/ping.yml
Normal file
7
infrastructure/create-resources/ansible/ping.yml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
- hosts: serverctl_super_cluster
|
||||||
|
gather_facts: yes
|
||||||
|
tasks:
|
||||||
|
- name: ping
|
||||||
|
command: "ping -c3 {{ hostvars[item].wireguard_ip}}"
|
||||||
|
with_items: "{{groups['all']}}"
|
@ -0,0 +1,67 @@
|
|||||||
|
---
|
||||||
|
- name: update packages
|
||||||
|
apt:
|
||||||
|
update_cache: yes
|
||||||
|
cache_valid_time: 3600
|
||||||
|
become: yes
|
||||||
|
|
||||||
|
- name: install ufw
|
||||||
|
apt:
|
||||||
|
name: ufw
|
||||||
|
state: present
|
||||||
|
become: yes
|
||||||
|
when: ufw_enabled
|
||||||
|
|
||||||
|
- name: Allow SSH in UFW
|
||||||
|
ufw:
|
||||||
|
rule: allow
|
||||||
|
port: "{{ ansible_ssh_port }}"
|
||||||
|
proto: tcp
|
||||||
|
become: yes
|
||||||
|
when: ufw_enabled
|
||||||
|
|
||||||
|
- name: Allow wireguard port in UFW
|
||||||
|
ufw:
|
||||||
|
rule: allow
|
||||||
|
port: "{{ wireguard_port }}"
|
||||||
|
proto: udp
|
||||||
|
become: yes
|
||||||
|
when: ufw_enabled
|
||||||
|
|
||||||
|
- name: Set ufw logging
|
||||||
|
ufw:
|
||||||
|
logging: "on"
|
||||||
|
become: yes
|
||||||
|
when: ufw_enabled
|
||||||
|
|
||||||
|
- name: inter-node Wireguard UFW connectivity
|
||||||
|
ufw:
|
||||||
|
rule: allow
|
||||||
|
src: "{{ hostvars[item].wireguard_ip }}"
|
||||||
|
with_items: "{{ groups['all'] }}"
|
||||||
|
become: yes
|
||||||
|
when: ufw_enabled and item != inventory_hostname
|
||||||
|
|
||||||
|
- name: Reject everything and enable UFW
|
||||||
|
ufw:
|
||||||
|
state: enabled
|
||||||
|
policy: reject
|
||||||
|
log: yes
|
||||||
|
become: yes
|
||||||
|
when: ufw_enabled
|
||||||
|
|
||||||
|
- name: Allow 6443 in UFW /tcp
|
||||||
|
ufw:
|
||||||
|
rule: allow
|
||||||
|
port: "6443"
|
||||||
|
proto: tcp
|
||||||
|
become: yes
|
||||||
|
when: ufw_enabled
|
||||||
|
|
||||||
|
- name: Allow 6443 in UFW udp
|
||||||
|
ufw:
|
||||||
|
rule: allow
|
||||||
|
port: "6443"
|
||||||
|
proto: udp
|
||||||
|
become: yes
|
||||||
|
when: ufw_enabled
|
@ -1,4 +1,5 @@
|
|||||||
---
|
---
|
||||||
|
|
||||||
- name: Copy K3s service file
|
- name: Copy K3s service file
|
||||||
register: k3s_service
|
register: k3s_service
|
||||||
template:
|
template:
|
||||||
|
@ -7,7 +7,7 @@ After=network-online.target
|
|||||||
Type=notify
|
Type=notify
|
||||||
ExecStartPre=-/sbin/modprobe br_netfilter
|
ExecStartPre=-/sbin/modprobe br_netfilter
|
||||||
ExecStartPre=-/sbin/modprobe overlay
|
ExecStartPre=-/sbin/modprobe overlay
|
||||||
ExecStart=/usr/local/bin/k3s server --data-dir {{ k3s_server_location }} {{ extra_server_args | default("") }}
|
ExecStart=/usr/local/bin/k3s server --data-dir {{ k3s_server_location }} {{ extra_server_args | default("") }} --advertise-address {{master_ip}}
|
||||||
KillMode=process
|
KillMode=process
|
||||||
Delegate=yes
|
Delegate=yes
|
||||||
# Having non-zero Limit*s causes performance problems due to accounting overhead
|
# Having non-zero Limit*s causes performance problems due to accounting overhead
|
||||||
|
@ -7,7 +7,7 @@ After=network-online.target
|
|||||||
Type=notify
|
Type=notify
|
||||||
ExecStartPre=-/sbin/modprobe br_netfilter
|
ExecStartPre=-/sbin/modprobe br_netfilter
|
||||||
ExecStartPre=-/sbin/modprobe overlay
|
ExecStartPre=-/sbin/modprobe overlay
|
||||||
ExecStart=/usr/local/bin/k3s agent --server https://{{ master_ip }}:6443 --token {{ hostvars[groups['serverctl_master_hosts'][0]]['token'] }} {{ extra_agent_args | default("") }}
|
ExecStart=/usr/local/bin/k3s agent --server https://{{ master_ip }}:6443 --token {{ hostvars[groups['serverctl_master_hosts'][0]]['token'] }} {{ extra_agent_args | default("") }} --node-ip {{inventory_hostname}}
|
||||||
KillMode=process
|
KillMode=process
|
||||||
Delegate=yes
|
Delegate=yes
|
||||||
# Having non-zero Limit*s causes performance problems due to accounting overhead
|
# Having non-zero Limit*s causes performance problems due to accounting overhead
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
- name: systemd network restart
|
||||||
|
service:
|
||||||
|
name: systemd-networkd
|
||||||
|
state: restarted
|
||||||
|
enabled: yes
|
||||||
|
become: yes
|
@ -0,0 +1,89 @@
|
|||||||
|
---
|
||||||
|
- name: install wireguard
|
||||||
|
apt:
|
||||||
|
name: wireguard
|
||||||
|
state: present
|
||||||
|
become: yes
|
||||||
|
when: ansible_distribution == 'Debian' or ansible_distribution == "Ubuntu"
|
||||||
|
|
||||||
|
- name: install wireguard
|
||||||
|
pacman:
|
||||||
|
name: wireguard-tools
|
||||||
|
state: present
|
||||||
|
become: yes
|
||||||
|
when: ansible_distribution == "Archlinux"
|
||||||
|
|
||||||
|
- name: generate wireguard keypair
|
||||||
|
shell: wg genkey | tee /etc/wireguard/serverctl-privatekey | wg pubkey | tee /etc/wireguard/serverctl-publickey
|
||||||
|
args:
|
||||||
|
creates: /etc/wireguard/serverctl-privatekey
|
||||||
|
become: yes
|
||||||
|
|
||||||
|
- name: register private key
|
||||||
|
shell: cat /etc/wireguard/serverctl-privatekey
|
||||||
|
register: wireguard_private_key
|
||||||
|
changed_when: false
|
||||||
|
become: yes
|
||||||
|
|
||||||
|
- name: register public key
|
||||||
|
shell: cat /etc/wireguard/serverctl-publickey
|
||||||
|
register: wireguard_public_key
|
||||||
|
changed_when: false
|
||||||
|
become: yes
|
||||||
|
|
||||||
|
- name: generate preshared keypair
|
||||||
|
shell: "wg genpsk > /etc/wireguard/serverctl-psk-{{item}}"
|
||||||
|
args:
|
||||||
|
creates: "/etc/wireguard/serverctl-psk-{{item}}"
|
||||||
|
when: inventory_hostname < item
|
||||||
|
with_items: "{{groups['serverctl_super_cluster']}}"
|
||||||
|
become: yes
|
||||||
|
|
||||||
|
- name: register preshared key
|
||||||
|
shell: "cat /etc/wireguard/serverctl-psk-{{item}}"
|
||||||
|
register: wireguard_preshared_key
|
||||||
|
changed_when: false
|
||||||
|
when: inventory_hostname < item
|
||||||
|
with_items: "{{groups['serverctl_super_cluster']}}"
|
||||||
|
become: yes
|
||||||
|
|
||||||
|
- name: message preshared keys
|
||||||
|
set_fact: "wireguard_preshared_keys={{wireguard_preshared_keys|default({}) | combine({item.item: item.stdout})}}"
|
||||||
|
when: item.skipped is not defined
|
||||||
|
with_items: "{{wireguard_preshared_key.results}}"
|
||||||
|
become: yes
|
||||||
|
|
||||||
|
#- name: print hostvars
|
||||||
|
# ansible.builtin.debug:
|
||||||
|
# msg: "{{hostvars[item]}}"
|
||||||
|
# with_items: "{{groups['serverctl_super_cluster']}}"
|
||||||
|
|
||||||
|
- name: Setup wg0 device
|
||||||
|
template:
|
||||||
|
src: 'systemd.netdev'
|
||||||
|
dest: '{{systemd_network_dir}}/99-serverctl-wg0.netdev'
|
||||||
|
owner: root
|
||||||
|
group: systemd-network
|
||||||
|
mode: 0640
|
||||||
|
become: yes
|
||||||
|
notify: systemd network restart
|
||||||
|
|
||||||
|
- name: Setup wg0 network
|
||||||
|
template:
|
||||||
|
src: 'systemd.network'
|
||||||
|
dest: "{{systemd_network_dir}}/99-serverctl-wg0.network"
|
||||||
|
owner: root
|
||||||
|
group: systemd-network
|
||||||
|
mode: 0640
|
||||||
|
become: yes
|
||||||
|
notify: systemd network restart
|
||||||
|
|
||||||
|
#- name: Start and enalbe wireguard on book
|
||||||
|
# systemd:
|
||||||
|
# name: wg-quick@wgserverctl0
|
||||||
|
# enabled: yes
|
||||||
|
# state: started
|
||||||
|
|
||||||
|
#- debug: msg="{{item.1}} - {{ (wireguard_base_ipv4|ipaddr(item.0 + 1)) }}"
|
||||||
|
# with_indexed_items: "{{groups.serverctl_mesh_nodes}}"
|
||||||
|
|
@ -0,0 +1,22 @@
|
|||||||
|
[NetDev]
|
||||||
|
Name=serverctl-wg0
|
||||||
|
Kind=wireguard
|
||||||
|
Description=WireGuard tunnel serverctl-wg0
|
||||||
|
|
||||||
|
[WireGuard]
|
||||||
|
ListenPort={{ wireguard_port }}
|
||||||
|
PrivateKey={{ wireguard_private_key.stdout }}
|
||||||
|
|
||||||
|
{% for peer in groups['serverctl_super_cluster'] %}
|
||||||
|
{% if peer != inventory_hostname %}
|
||||||
|
|
||||||
|
[WireGuardPeer]
|
||||||
|
PublicKey={{ hostvars[peer].wireguard_public_key.stdout }}
|
||||||
|
PresharedKey={{ wireguard_preshared_keys[peer] if inventory_hostname < peer else hostvars[peer].wireguard_preshared_keys[inventory_hostname] }}
|
||||||
|
AllowedIPs={{ hostvars[peer].wireguard_ip }}/32
|
||||||
|
{% if not hostvars[peer].client_server %}
|
||||||
|
Endpoint={{ hostvars[peer].ansible_host }}:{{ wireguard_port }}
|
||||||
|
PersistentKeepalive=25
|
||||||
|
{% endif %}
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
@ -0,0 +1,5 @@
|
|||||||
|
[Match]
|
||||||
|
Name=serverctl-wg0
|
||||||
|
|
||||||
|
[Network]
|
||||||
|
Address={{ wireguard_ip }}/{{ wireguard_mask_bits }}
|
@ -5,12 +5,21 @@
|
|||||||
roles:
|
roles:
|
||||||
- role: prereq
|
- role: prereq
|
||||||
- role: download
|
- role: download
|
||||||
|
- role: firewall
|
||||||
|
|
||||||
|
- hosts: serverctl_super_cluster
|
||||||
|
gather_facts: yes
|
||||||
|
become: yes
|
||||||
|
roles:
|
||||||
|
- role: wireguard/mesh
|
||||||
|
|
||||||
- hosts: serverctl_master_hosts
|
- hosts: serverctl_master_hosts
|
||||||
become: yes
|
become: yes
|
||||||
roles:
|
roles:
|
||||||
- role: "./k3s/master"
|
- role: "./k3s/master"
|
||||||
#- hosts: serverctl_node_hosts
|
|
||||||
# become: yes
|
- hosts: serverctl_node_hosts
|
||||||
# roles:
|
become: yes
|
||||||
# - role: "./k3s/node"
|
roles:
|
||||||
#
|
- role: "./k3s/node"
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
variable "serverctl_master_count" {
|
variable "serverctl_master_count" {
|
||||||
default = 0
|
default = 0
|
||||||
}
|
}
|
||||||
@ -7,7 +6,6 @@ variable "serverctl_node_count" {
|
|||||||
default = 0
|
default = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
resource "hcloud_placement_group" "serverctl_master" {
|
resource "hcloud_placement_group" "serverctl_master" {
|
||||||
name = "serverctl_master_group"
|
name = "serverctl_master_group"
|
||||||
type = "spread"
|
type = "spread"
|
||||||
@ -73,7 +71,7 @@ resource "hcloud_server" "serverctl_node" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resource "local_file" "hosts_cfg" {
|
resource "local_file" "hosts_cfg" {
|
||||||
content = templatefile("${path.module}/templates/hosts.tpl",
|
content = templatefile("${path.module}/templates/hosts.tftpl",
|
||||||
{
|
{
|
||||||
serverctl_masters = hcloud_server.serverctl_master.*.ipv4_address
|
serverctl_masters = hcloud_server.serverctl_master.*.ipv4_address
|
||||||
serverctl_nodes = hcloud_server.serverctl_node.*.ipv4_address
|
serverctl_nodes = hcloud_server.serverctl_node.*.ipv4_address
|
||||||
|
@ -2,7 +2,7 @@ terraform {
|
|||||||
required_providers {
|
required_providers {
|
||||||
hcloud = {
|
hcloud = {
|
||||||
source = "hetznercloud/hcloud"
|
source = "hetznercloud/hcloud"
|
||||||
version = "1.32.2"
|
version = "1.48.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
35
infrastructure/create-resources/templates/hosts.tftpl
Normal file
35
infrastructure/create-resources/templates/hosts.tftpl
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
[serverctl_master_hosts]
|
||||||
|
%{ for ip in serverctl_masters ~}
|
||||||
|
${ip} ansible_host=${ip} wireguard_ip=${cidrhost("10.1.1.0/24", index(serverctl_masters, ip) + 1)}
|
||||||
|
%{ endfor ~}
|
||||||
|
|
||||||
|
[serverctl_node_hosts]
|
||||||
|
%{ for ip in serverctl_nodes ~}
|
||||||
|
${ip} ansible_host=${ip} wireguard_ip=${cidrhost("10.1.1.0/24", index(serverctl_nodes, ip) + 10)}
|
||||||
|
%{ endfor ~}
|
||||||
|
|
||||||
|
[serverctl_home_servers]
|
||||||
|
192.168.1.150 ansible_host=192.168.1.150 wireguard_ip=10.1.1.8
|
||||||
|
#192.168.1.233 ansible_host=192.168.1.233 wireguard_ip=10.1.1.9
|
||||||
|
|
||||||
|
[serverctl_cluster:children]
|
||||||
|
serverctl_master_hosts
|
||||||
|
serverctl_node_hosts
|
||||||
|
|
||||||
|
[serverctl_super_cluster:children]
|
||||||
|
serverctl_cluster
|
||||||
|
serverctl_home_servers
|
||||||
|
|
||||||
|
[serverctl_home_servers:vars]
|
||||||
|
client_server=True
|
||||||
|
|
||||||
|
[serverctl_super_cluster:vars]
|
||||||
|
pipelining=true
|
||||||
|
ansible_ssh_user=root
|
||||||
|
ansible_ssh_port=22
|
||||||
|
|
||||||
|
[serverctl_cluster:vars]
|
||||||
|
client_server=False
|
||||||
|
pipelining=true
|
||||||
|
ansible_ssh_user=root
|
||||||
|
ansible_ssh_port=22
|
@ -1,13 +0,0 @@
|
|||||||
[serverctl_master_hosts]
|
|
||||||
%{ for ip in serverctl_masters ~}
|
|
||||||
${ip}
|
|
||||||
%{ endfor ~}
|
|
||||||
|
|
||||||
[serverctl_node_hosts]
|
|
||||||
%{ for ip in serverctl_nodes ~}
|
|
||||||
${ip}
|
|
||||||
%{ endfor ~}
|
|
||||||
|
|
||||||
[serverctl_cluster:children]
|
|
||||||
serverctl_master_hosts
|
|
||||||
serverctl_node_hosts
|
|
3
renovate.json
Normal file
3
renovate.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://docs.renovatebot.com/renovate-schema.json"
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
FROM golang:1.17-bullseye
|
FROM golang:1.23-bullseye
|
||||||
|
|
||||||
RUN go install github.com/jackc/tern@latest
|
RUN go install github.com/jackc/tern@latest
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
FROM golang:1.17-bullseye
|
FROM golang:1.23-bullseye
|
||||||
|
|
||||||
RUN go install github.com/cosmtrek/air@latest
|
RUN go install github.com/cosmtrek/air@latest
|
||||||
# Development don't need this
|
# Development don't need this
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"serverctl/pkg/api"
|
"serverctl/pkg/api"
|
||||||
"serverctl/pkg/infrastructure"
|
"serverctl/pkg/infrastructure"
|
||||||
"serverctl/pkg/infrastructure/dependencies"
|
"serverctl/pkg/infrastructure/dependencies"
|
||||||
"serverctl/pkg/infrastructure/queue"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Run main app, will bootstrap dependencies and run all external ports (http servers, queues, etc).
|
// Run main app, will bootstrap dependencies and run all external ports (http servers, queues, etc).
|
||||||
@ -12,12 +11,6 @@ func Run() {
|
|||||||
d := dependencies.New()
|
d := dependencies.New()
|
||||||
d.Logger.Info("Starting serverctl")
|
d.Logger.Info("Starting serverctl")
|
||||||
|
|
||||||
queue.NewRabbitMQ(d.Logger, &queue.RabbitMqConfig{
|
|
||||||
Username: "serverctl",
|
|
||||||
Password: "serverctlsecret",
|
|
||||||
Host: "rabbitmq",
|
|
||||||
Port: 5672,
|
|
||||||
})
|
|
||||||
// if development add seed data
|
// if development add seed data
|
||||||
infrastructure.AddSeedData(d.Database, d.Logger)
|
infrastructure.AddSeedData(d.Database, d.Logger)
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
module serverctl
|
module serverctl
|
||||||
|
|
||||||
go 1.17
|
go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Microsoft/go-winio v0.4.17 // indirect
|
github.com/Microsoft/go-winio v0.4.17 // indirect
|
||||||
@ -18,6 +18,7 @@ require (
|
|||||||
github.com/docker/go-units v0.4.0 // indirect
|
github.com/docker/go-units v0.4.0 // indirect
|
||||||
github.com/dustin/go-humanize v1.0.0 // indirect
|
github.com/dustin/go-humanize v1.0.0 // indirect
|
||||||
github.com/eko/gocache v1.2.0 // indirect
|
github.com/eko/gocache v1.2.0 // indirect
|
||||||
|
github.com/georgysavva/scany v0.3.0 // indirect
|
||||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
github.com/gin-gonic/gin v1.7.7 // indirect
|
github.com/gin-gonic/gin v1.7.7 // indirect
|
||||||
github.com/go-co-op/gocron v1.12.0 // indirect
|
github.com/go-co-op/gocron v1.12.0 // indirect
|
||||||
@ -36,6 +37,7 @@ require (
|
|||||||
github.com/jackc/pgproto3/v2 v2.2.0 // indirect
|
github.com/jackc/pgproto3/v2 v2.2.0 // indirect
|
||||||
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
|
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
|
||||||
github.com/jackc/pgtype v1.10.0 // indirect
|
github.com/jackc/pgtype v1.10.0 // indirect
|
||||||
|
github.com/jackc/pgx v3.6.2+incompatible // indirect
|
||||||
github.com/jackc/pgx/v4 v4.15.0 // indirect
|
github.com/jackc/pgx/v4 v4.15.0 // indirect
|
||||||
github.com/jackc/puddle v1.2.1 // indirect
|
github.com/jackc/puddle v1.2.1 // indirect
|
||||||
github.com/json-iterator/go v1.1.10 // indirect
|
github.com/json-iterator/go v1.1.10 // indirect
|
||||||
@ -52,7 +54,6 @@ require (
|
|||||||
github.com/prometheus/client_model v0.2.0 // indirect
|
github.com/prometheus/client_model v0.2.0 // indirect
|
||||||
github.com/prometheus/common v0.18.0 // indirect
|
github.com/prometheus/common v0.18.0 // indirect
|
||||||
github.com/prometheus/procfs v0.6.0 // indirect
|
github.com/prometheus/procfs v0.6.0 // indirect
|
||||||
github.com/rabbitmq/amqp091-go v1.3.0 // indirect
|
|
||||||
github.com/robfig/cron/v3 v3.0.1 // indirect
|
github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||||
github.com/sirupsen/logrus v1.8.1 // indirect
|
github.com/sirupsen/logrus v1.8.1 // indirect
|
||||||
github.com/spf13/cast v1.3.1 // indirect
|
github.com/spf13/cast v1.3.1 // indirect
|
||||||
|
@ -793,8 +793,6 @@ github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
|
|||||||
github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=
|
github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=
|
||||||
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
|
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
|
||||||
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
|
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
|
||||||
github.com/rabbitmq/amqp091-go v1.3.0 h1:A/QuHiNw7LMCJsxx9iZn5lrIz6OrhIn7Dfk5/1YatWM=
|
|
||||||
github.com/rabbitmq/amqp091-go v1.3.0/go.mod h1:ogQDLSOACsLPsIq0NpbtiifNZi2YOz0VTJ0kHRghqbM=
|
|
||||||
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
|
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
|
||||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||||
@ -848,8 +846,6 @@ github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/y
|
|||||||
github.com/stefanberger/go-pkcs11uri v0.0.0-20201008174630-78d3cae3a980/go.mod h1:AO3tvPzVZ/ayst6UlUKUv6rcPQInYe3IknH3jYhAKu8=
|
github.com/stefanberger/go-pkcs11uri v0.0.0-20201008174630-78d3cae3a980/go.mod h1:AO3tvPzVZ/ayst6UlUKUv6rcPQInYe3IknH3jYhAKu8=
|
||||||
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
|
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
|
||||||
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
|
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
|
||||||
github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo=
|
|
||||||
github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
|
|
||||||
github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI=
|
github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI=
|
||||||
github.com/stretchr/objx v0.0.0-20180129172003-8a3f7159479f/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.0.0-20180129172003-8a3f7159479f/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
package queue
|
|
||||||
|
|
||||||
type Queue interface {
|
|
||||||
}
|
|
@ -1,217 +0,0 @@
|
|||||||
package queue
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
import amqp "github.com/rabbitmq/amqp091-go"
|
|
||||||
|
|
||||||
type RabbitMQ struct {
|
|
||||||
logger *zap.Logger
|
|
||||||
config *RabbitMqConfig
|
|
||||||
conn *amqp.Connection
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ Queue = RabbitMQ{}
|
|
||||||
|
|
||||||
type RabbitMqConfig struct {
|
|
||||||
Username string
|
|
||||||
Password string
|
|
||||||
Host string
|
|
||||||
Port int
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRabbitMQ(logger *zap.Logger, config *RabbitMqConfig) Queue {
|
|
||||||
|
|
||||||
conn, err := amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s:%d/", config.Username, config.Password, config.Host, config.Port))
|
|
||||||
if err != nil {
|
|
||||||
logger.Panic("Could not dial rabbitmq", zap.String("username", config.Username), zap.String("Host", config.Host), zap.Int("port", config.Port), zap.String("error", err.Error()))
|
|
||||||
}
|
|
||||||
|
|
||||||
//sendBasic(logger, config, conn)
|
|
||||||
//receiveBasic(logger, config, conn)
|
|
||||||
sendPublishingBasic(logger, config, conn)
|
|
||||||
for i := 0; i < 200; i++ {
|
|
||||||
time.Sleep(time.Millisecond * 200)
|
|
||||||
receivePublishingBasic(logger, config, conn)
|
|
||||||
}
|
|
||||||
//sendMany(logger, config, conn)
|
|
||||||
|
|
||||||
return &RabbitMQ{
|
|
||||||
logger: logger,
|
|
||||||
config: config,
|
|
||||||
conn: conn,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func sendBasic(logger *zap.Logger, config *RabbitMqConfig, conn *amqp.Connection) {
|
|
||||||
ch, err := conn.Channel()
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Could not open channel", zap.String("username", config.Username), zap.String("Host", config.Host), zap.Int("port", config.Port))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
q, err := ch.QueueDeclare("test", false, false, false, false, nil)
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Could not open queue", zap.String("queueName", "test"))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
body := "Hello world!"
|
|
||||||
err = ch.Publish("", q.Name, false, false, amqp.Publishing{ContentType: "text/plain", Body: []byte(body)})
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Could not send message", zap.String("queueName", "test"))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
logger.Info("Sent message", zap.String("body", body))
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer ch.Close()
|
|
||||||
counter := 0
|
|
||||||
for {
|
|
||||||
body := fmt.Sprintf("message nr: %d", counter)
|
|
||||||
err = ch.Publish("", q.Name, false, false, amqp.Publishing{ContentType: "text/plain", Body: []byte(body)})
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Could not send message", zap.String("queueName", "test"))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
logger.Info("Sent message", zap.String("body", body))
|
|
||||||
counter += 1
|
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
func receiveBasic(logger *zap.Logger, config *RabbitMqConfig, conn *amqp.Connection) {
|
|
||||||
ch, err := conn.Channel()
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Could not open channel", zap.String("username", config.Username), zap.String("Host", config.Host), zap.Int("port", config.Port))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
q, err := ch.QueueDeclare("test", false, false, false, false, nil)
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Could not open queue", zap.String("queueName", "test"))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
msgs, err := ch.Consume(q.Name,
|
|
||||||
"",
|
|
||||||
true,
|
|
||||||
false, false, false, nil)
|
|
||||||
if err != nil {
|
|
||||||
logger.Panic("failed to register consumer", zap.String("queueName", q.Name), zap.Error(err))
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer ch.Close()
|
|
||||||
|
|
||||||
for d := range msgs {
|
|
||||||
logger.Info("Received msg", zap.String("body", string(d.Body)))
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
func sendMany(logger *zap.Logger, config *RabbitMqConfig, conn *amqp.Connection) {
|
|
||||||
ch, err := conn.Channel()
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Could not open channel", zap.String("username", config.Username), zap.String("Host", config.Host), zap.Int("port", config.Port))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
q, err := ch.QueueDeclare("test", false, false, false, false, nil)
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Could not open queue", zap.String("queueName", "test"))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
body := "Hello world!"
|
|
||||||
err = ch.Publish("", q.Name, false, false, amqp.Publishing{ContentType: "text/plain", Body: []byte(body)})
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Could not send message", zap.String("queueName", "test"))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
logger.Info("Sent message", zap.String("body", body))
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer ch.Close()
|
|
||||||
counter := 0
|
|
||||||
for {
|
|
||||||
body := fmt.Sprintf("message nr: %d", counter)
|
|
||||||
err = ch.Publish("", q.Name, false, false, amqp.Publishing{ContentType: "text/plain", Body: []byte(body)})
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Could not send message", zap.String("queueName", "test"))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
logger.Info("Sent message", zap.String("body", body))
|
|
||||||
counter += 1
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
func sendPublishingBasic(logger *zap.Logger, config *RabbitMqConfig, conn *amqp.Connection) {
|
|
||||||
ch, err := conn.Channel()
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Could not open channel", zap.String("username", config.Username), zap.String("Host", config.Host), zap.Int("port", config.Port))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
q, err := ch.QueueDeclare("test", true, false, false, false, nil)
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Could not open queue", zap.String("queueName", "test"))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer ch.Close()
|
|
||||||
counter := 0
|
|
||||||
for {
|
|
||||||
body := fmt.Sprintf("message nr: %d", counter)
|
|
||||||
err = ch.Publish("", q.Name, false, false, amqp.Publishing{
|
|
||||||
DeliveryMode: amqp.Persistent,
|
|
||||||
ContentType: "text/plain", Body: []byte(body)})
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Could not send message", zap.String("queueName", "test"))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
logger.Info("Sent message", zap.String("body", body))
|
|
||||||
counter += 1
|
|
||||||
|
|
||||||
time.Sleep(time.Millisecond * 200)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
func receivePublishingBasic(logger *zap.Logger, config *RabbitMqConfig, conn *amqp.Connection) {
|
|
||||||
ch, err := conn.Channel()
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Could not open channel", zap.String("username", config.Username), zap.String("Host", config.Host), zap.Int("port", config.Port))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ch.Qos(1, 0, false)
|
|
||||||
|
|
||||||
q, err := ch.QueueDeclare("test", true, false, false, false, nil)
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal("Could not open queue", zap.String("queueName", "test"))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
msgs, err := ch.Consume(q.Name,
|
|
||||||
"",
|
|
||||||
false,
|
|
||||||
false, false, false, nil)
|
|
||||||
if err != nil {
|
|
||||||
logger.Panic("failed to register consumer", zap.String("queueName", q.Name), zap.Error(err))
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer ch.Close()
|
|
||||||
|
|
||||||
for d := range msgs {
|
|
||||||
logger.Info("Received msg", zap.String("body", string(d.Body)), zap.Int("secondsToSleep", len(d.Body)))
|
|
||||||
t := time.Duration(len(d.Body))
|
|
||||||
time.Sleep(t * time.Second)
|
|
||||||
logger.Info("Received msg: Done")
|
|
||||||
d.Ack(false)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user