Compare commits

..
12 Commits
Author SHA1 Message Date
Bartal Læarsson 6c407cd995 update readme 2025-03-03 23:20:48 +00:00
Bartal Læarsson f6786fea1b alter readme 2025-03-03 23:15:01 +00:00
Bartal Læarsson bf3890aaf9 add correct execution of provision-cli, start work on helm charts 2025-03-03 16:03:17 +00:00
Bartal Læarsson 7b4bed7047 rqlite functional 2025-03-03 14:21:57 +00:00
Bartal Læarsson 0312778ef4 rqlite refine 2025-03-03 14:03:20 +00:00
Bartal Læarsson f66be725b2 add rqlite to cli 2025-03-03 13:45:47 +00:00
Bartal Læarsson 2a1facd90f change build process 2025-03-03 13:28:38 +00:00
Bartal Læarsson f18e328bf2 change build process 2025-03-03 13:19:55 +00:00
Bartal Læarsson 7ef296daee add rqlite ansible 2025-03-03 10:53:25 +00:00
Bartal LæarssonandGitHub 902b2c4dd8 Merge pull request #3 from bxtal-lsn/cli-dev
Cli dev
2025-03-02 21:55:56 +00:00
Bartal Læarsson 128f260a3f resilient install.sh fie 2025-03-02 21:53:03 +00:00
Bartal Læarsson 762eb94ee0 cli test 2025-03-02 21:42:39 +00:00
39 changed files with 1774 additions and 385 deletions
+1 -6
View File
@@ -2,12 +2,7 @@ multipass/cloud-init/ssh_key.txt
multipass/cloud-init/common_processed.yaml multipass/cloud-init/common_processed.yaml
ansible/inventories/kubernetes.yml ansible/inventories/kubernetes.yml
provision-cli
# Go build artifacts (except for released binaries)
/build/
/bin/
cli/bin
# Uncomment the lines above if you don't want to include binaries in the repo
# Go dependency directories # Go dependency directories
/vendor/ /vendor/
+60 -9
View File
@@ -1,11 +1,28 @@
# Kubernetes Local Development Environment # Kubernetes Local Development Environment
Only tested on Ubuntu-24.04 wsl
## Quick Start ## Quick Start
```bash ```bash
# Please install golang v1.22 or greater before starting
# and multipass, and ansible
# Golang
sudo apt install golang
# Ansible (Please double check installation process yourself if this is outdated)
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
# multipass (If you encounter authentication error, please follow step 6 in troubleshooting section
sudo snap install multipass
# Run provisioning
# Option 1: Using CLI tool (recommended) # Option 1: Using CLI tool (recommended)
./cli/install.sh # Install the CLI tool ./cli/build.sh # build CLI tool in project
provision # Run interactive provisioning ./provision-cli provision # Run interactive provisioning in project root
# Option 2: Using scripts directly # Option 2: Using scripts directly
./scripts/provision-kubernetes.sh # Create Kubernetes cluster ./scripts/provision-kubernetes.sh # Create Kubernetes cluster
@@ -28,21 +45,21 @@ This project provides tools to create and manage local Kubernetes development en
- [Ansible](https://docs.ansible.com/ansible/latest/installation_guide/index.html) - for configuration - [Ansible](https://docs.ansible.com/ansible/latest/installation_guide/index.html) - for configuration
- SSH tools (OpenSSH client) - SSH tools (OpenSSH client)
- Python 3 - Python 3
- Golang v1.22 or greater (is currently used for building binary)
### CLI Tool (Recommended) ### CLI Tool (Recommended)
It is currently required to have golang v1.22 or greater to build the provision-cli.
On Ubuntu 24.04 you can just run `sudo apt install golang`
The CLI tool provides a user-friendly interface for managing your environments: The CLI tool provides a user-friendly interface for managing your environments:
```bash ```bash
# Install CLI tool # build CLI tool
./cli/install.sh ./cli/build.sh
# The installer adds the binary to ~/.local/bin
# Make sure this is in your PATH
export PATH="$PATH:$HOME/.local/bin"
# The installer adds the binary to project root
# Run the tool # Run the tool
provision ./provision-cli provision
``` ```
[CLI Documentation](./cli/README.md) - Detailed CLI usage and development [CLI Documentation](./cli/README.md) - Detailed CLI usage and development
@@ -103,6 +120,40 @@ If you encounter issues:
3. For Kubernetes issues, use `kubectl` commands on the control plane 3. For Kubernetes issues, use `kubectl` commands on the control plane
4. Ensure all prerequisites are properly installed 4. Ensure all prerequisites are properly installed
5. **Note**: This project has only been tested on Ubuntu 24.04 and WSL Ubuntu 24.04. Using other operating systems may result in compatibility issues. 5. **Note**: This project has only been tested on Ubuntu 24.04 and WSL Ubuntu 24.04. Using other operating systems may result in compatibility issues.
6. In case you get an authentication error when running something like `multipass list`
```
list failed: The client is not authenticated with the Multipass service.
Please use 'multipass authenticate' before proceeding.
```
`multipass authenticate`
```
Please enter passphrase:
authenticate failed: Passphrase is not set. Please `multipass set
local.passphrase` with a trusted client.
```
`multipass set local.passphrase`
```
Please enter passphrase:
Please re-enter passphrase:
set failed: The client is not authenticated with the Multipass service.
Please use 'multipass authenticate' before proceeding.
```
This may not even work when using sudo.
The following workaround should help get out of this situation:
```
cat ~/snap/multipass/current/data/multipass-client-certificate/multipass_cert.pem | sudo tee -a /var/snap/multipass/common/data/multipassd/authenticated-certs/multipass_client_certs.pem > /dev/null
snap restart multipass
```
You may need sudo with this last command: `sudo snap restart multipass.`
At this point, your client should be authenticated with the Multipass service.
## Contributing ## Contributing
+25
View File
@@ -0,0 +1,25 @@
---
# rqlite configuration
rqlite_version: "8.36.11"
rqlite_http_port: 4001
rqlite_raft_port: 4002
rqlite_data_dir: "/home/{{ ansible_user }}/data"
rqlite_extract_dir: "/opt/rqlite"
rqlite_download_url: "https://github.com/rqlite/rqlite/releases/download/v{{ rqlite_version }}/rqlite-v{{ rqlite_version }}-linux-amd64.tar.gz"
# Node configuration
rqlite_node_list:
- rqlite1
- rqlite2
- rqlite3
rqlite_leader_node: rqlite1
# Node resources
node_cpus: 2
node_memory: 2G
node_disk: 10G
# DNS configuration
dns_servers:
- "8.8.8.8"
- "8.8.4.4"
+5 -5
View File
@@ -6,17 +6,17 @@ all:
control_plane: control_plane:
hosts: hosts:
controlplane: controlplane:
ansible_host: 10.203.146.85 ansible_host: 10.193.215.215
workers: workers:
hosts: hosts:
node01: node01:
ansible_host: 10.203.146.146 ansible_host: 10.193.215.247
node02: node02:
ansible_host: 10.203.146.50 ansible_host: 10.193.215.126
node03: node03:
ansible_host: 10.203.146.103 ansible_host: 10.193.215.158
vars: vars:
ansible_user: ubuntu ansible_user: ubuntu
ansible_become: yes ansible_become: yes
ansible_ssh_private_key_file: /home/bl/.ssh/id_rsa_provisioning ansible_ssh_private_key_file: /home/bxtal/.ssh/id_rsa_provisioning
ansible_ssh_common_args: '-o StrictHostKeyChecking=no' ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
+25
View File
@@ -0,0 +1,25 @@
---
all:
children:
rqlite_cluster:
children:
rqlite_leader:
hosts:
rqlite1:
ansible_host: 10.57.249.162
rqlite_followers:
hosts:
rqlite2:
ansible_host: 10.57.249.245
rqlite3:
ansible_host: 10.57.249.178
vars:
rqlite_nodes:
- rqlite1
- rqlite2
- rqlite3
vars:
ansible_user: ubuntu
ansible_become: yes
ansible_ssh_private_key_file: /home/bl/.ssh/id_rsa_provisioning
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
+63
View File
@@ -0,0 +1,63 @@
---
# ansible/playbooks/rqlite.yml
- name: Setup all rqlite nodes
hosts: rqlite_cluster
become: yes
vars_files:
- ../defaults/rqlite.yml
vars:
# Explicitly define the node list and leader
rqlite_node_list:
- rqlite1
- rqlite2
- rqlite3
rqlite_leader_node: rqlite1
tags:
- rqlite-setup
tasks:
- include_tasks: ../tasks/rqlite/node-setup.yml
- name: Initialize rqlite leader node
hosts: rqlite_leader
become: yes
vars_files:
- ../defaults/rqlite.yml
vars:
node_id: 1
rqlite_node_list:
- rqlite1
- rqlite2
- rqlite3
rqlite_leader_node: rqlite1
tags:
- leader
tasks:
- include_tasks: ../tasks/rqlite/leader-init.yml
- name: Join follower nodes to cluster
hosts: rqlite_followers
become: yes
vars_files:
- ../defaults/rqlite.yml
vars:
# Dynamically calculate node ID for followers
node_id: "{{ rqlite_node_list.index(inventory_hostname) + 2 }}"
rqlite_node_list:
- rqlite1
- rqlite2
- rqlite3
rqlite_leader_node: rqlite1
tags:
- followers
tasks:
- include_tasks: ../tasks/rqlite/follower-join.yml
- name: Validate rqlite cluster
hosts: rqlite_cluster
become: yes
vars_files:
- ../defaults/rqlite.yml
tags:
- validate
tasks:
- include_tasks: ../tasks/rqlite/validate-cluster.yml
+82
View File
@@ -0,0 +1,82 @@
# Rqlite
Go to `https://github.com/rqlite/rqlite/releases`
This presupposes you use `Rqlite v8.36.11`
The service user you use is `ubuntu`
```bash
version="v8.36.11"
curl -L https://github.com/rqlite/rqlite/releases/download/"$version"/rqlite-"$version"-linux-amd64.tar.gz -o rqlite-"$version"-linux-amd64.tar.gz
tar xvfz rqlite-"$version"-linux-amd64.tar.gz
cd rqlite-"$version"-linux-amd64
sudo cp rq* /usr/local/bin/
cd
rm -r rqlite-"$version"-linux-amd64.tar.gz rqlite-"$version"-linux-amd64
```
Create a service for initial leader node at `sudo vim /etc/systemd/system/rqlited.service`
```conf
[Unit]
Description=rqlite distributed SQLite node
After=network.target
[Service]
Type=simple
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu
ExecStart=/usr/local/bin/rqlited -node-id <1, 2, 3, etc> -http-addr <machine-id>:4001 -raft-addr <machine-id>:4002 /home/ubuntu/data
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
```
Create a service for joining nodes at `sudo vim /etc/systemd/system/rqlited.service`
```conf
[Unit]
Description=rqlite distributed SQLite node
After=network.target
[Service]
Type=simple
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu
ExecStart=/usr/local/bin/rqlited -node-id <1, 2, 3, etc> -http-addr <machine-id>:4001 -raft-addr <machine-id>:4002 -join <initial-machine-ip>:4002 /home/ubuntu/data
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
```
Prepare directory and permissions
```bash
mkdir -p /home/ubuntu/data
sudo chown -R ubuntu:ubuntu /home/ubuntu/data
```
start and enable service
```bash
sudo systemctl daemon-reload
sudo systemctl enable --now rqlited
```
check status
```bash
sudo systemctl status rqlited
```
from your dev machine which has access to all machines (or one of the nodes) run
```bash
# On any node
rqlite -H <any-node-ip>
```
+340
View File
@@ -0,0 +1,340 @@
---
# Ansible playbook for deploying a rqlite cluster
# Run with: ansible-playbook -i inventory.yml deploy-rqlite.yml -e "node_ips=10.203.146.136,10.203.146.91,10.203.146.232 leader_ip=10.203.146.232 ansible_user=ubuntu"
- name: Process input parameters and set up host groups
hosts: localhost
connection: local # Use local connection instead of SSH
gather_facts: no
tasks:
- name: Validate required variables are provided
fail:
msg: "Required variables 'node_ips' and 'leader_ip' must be provided"
when: node_ips is not defined or leader_ip is not defined
- name: Create array of IPs from comma-separated string
set_fact:
ip_list: "{{ node_ips.split(',') }}"
- name: Verify leader_ip is in the node_ips list
fail:
msg: "Leader IP ({{ leader_ip }}) must be included in the list of node IPs"
when: leader_ip not in ip_list
- name: Add leader node to inventory
add_host:
name: "leader"
ansible_host: "{{ leader_ip }}"
node_id: "{{ ip_list.index(leader_ip) + 1 }}"
groups: "rqlite_leader,rqlite_cluster"
# Explicitly handle up to 2 follower nodes - this approach avoids complex loops
- name: Add first follower node if it exists
add_host:
name: "follower1"
ansible_host: "{{ item }}"
node_id: "{{ ip_list.index(item) + 1 }}"
groups: "rqlite_followers,rqlite_cluster"
with_items: "{{ ip_list }}"
when: item != leader_ip
run_once: true
ignore_errors: yes
- name: Add second follower node if it exists
add_host:
name: "follower2"
ansible_host: "{{ item }}"
node_id: "{{ ip_list.index(item) + 1 }}"
groups: "rqlite_followers,rqlite_cluster"
with_items: "{{ ip_list }}"
when:
- item != leader_ip
- item != (hostvars['follower1']['ansible_host'] | default(''))
run_once: true
ignore_errors: yes
- name: Display hosts configuration
debug:
msg: |
Leader: {{ hostvars['leader']['ansible_host'] }} (node ID: {{ hostvars['leader']['node_id'] }})
Followers: {{ groups['rqlite_followers'] | map('extract', hostvars, ['ansible_host']) | join(', ') }}
# Deploy leader node first, then followers
- name: Deploy leader node
hosts: rqlite_leader
gather_facts: yes
become: yes
vars:
rqlite_version: "8.36.11"
rqlite_http_port: 4001
rqlite_raft_port: 4002
rqlite_data_dir: "/home/{{ ansible_user }}/data"
rqlite_download_url: "https://github.com/rqlite/rqlite/releases/download/v{{ rqlite_version }}/rqlite-v{{ rqlite_version }}-linux-amd64.tar.gz"
rqlite_extract_dir: "/opt/rqlite"
leader_host: "{{ hostvars['leader']['ansible_host'] }}"
tasks:
- name: Install required packages
apt:
name:
- tar
- wget
- curl
state: present
update_cache: yes
when: ansible_os_family == "Debian"
- name: Install required packages (RHEL/CentOS)
yum:
name:
- tar
- wget
- curl
state: present
when: ansible_os_family == "RedHat"
- name: Create rqlite directories
file:
path: "{{ item }}"
state: directory
mode: '0755'
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
with_items:
- "{{ rqlite_extract_dir }}"
- "{{ rqlite_data_dir }}"
- name: Download and extract rqlite
unarchive:
src: "{{ rqlite_download_url }}"
dest: "{{ rqlite_extract_dir }}"
remote_src: yes
creates: "{{ rqlite_extract_dir }}/rqlited"
extra_opts:
- --strip-components=1
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
- name: Copy rqlite binaries to /usr/local/bin
copy:
src: "{{ rqlite_extract_dir }}/{{ item }}"
dest: "/usr/local/bin/{{ item }}"
mode: '0755'
remote_src: yes
with_items:
- rqlited
- rqlite
- name: Create leader service file
copy:
dest: /etc/systemd/system/rqlited.service
content: |
[Unit]
Description=rqlite distributed SQLite leader node
After=network.target
[Service]
Type=simple
User={{ ansible_user }}
Group={{ ansible_user }}
WorkingDirectory=/home/{{ ansible_user }}
ExecStart=/usr/local/bin/rqlited -node-id {{ node_id }} -http-addr {{ ansible_host }}:{{ rqlite_http_port }} -raft-addr {{ ansible_host }}:{{ rqlite_raft_port }} {{ rqlite_data_dir }}
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
- name: Configure firewall (UFW)
ufw:
rule: allow
port: "{{ item }}"
proto: tcp
with_items:
- "{{ rqlite_http_port }}"
- "{{ rqlite_raft_port }}"
when: ansible_os_family == "Debian"
ignore_errors: yes # In case UFW isn't installed or enabled
- name: Configure firewall (firewalld)
firewalld:
port: "{{ item }}/tcp"
permanent: yes
state: enabled
with_items:
- "{{ rqlite_http_port }}"
- "{{ rqlite_raft_port }}"
when: ansible_os_family == "RedHat"
ignore_errors: yes # In case firewalld isn't installed or enabled
- name: Start and enable rqlite service on leader node
systemd:
name: rqlited
enabled: yes
daemon_reload: yes
state: started
# Verify leader is fully operational before moving to followers
- name: Wait for leader node to be fully operational
uri:
url: "http://{{ leader_host }}:{{ rqlite_http_port }}/readyz"
return_content: yes
register: leader_ready
until: leader_ready.status == 200
retries: 12 # Try for up to 1 minute (5 seconds × 12)
delay: 5 # 5 second delay between retries
- name: Ensure leader has stabilized
pause:
seconds: 5
- name: Check leader status
shell: "curl -s http://{{ leader_host }}:{{ rqlite_http_port }}/status?pretty"
register: leader_status
changed_when: false
- name: Display leader status
debug:
var: leader_status.stdout_lines
# Deploy follower nodes after the leader is ready
- name: Deploy follower nodes
hosts: rqlite_followers
gather_facts: yes
become: yes
vars:
rqlite_version: "8.36.11"
rqlite_http_port: 4001
rqlite_raft_port: 4002
rqlite_data_dir: "/home/{{ ansible_user }}/data"
rqlite_download_url: "https://github.com/rqlite/rqlite/releases/download/v{{ rqlite_version }}/rqlite-v{{ rqlite_version }}-linux-amd64.tar.gz"
rqlite_extract_dir: "/opt/rqlite"
leader_host: "{{ hostvars['leader']['ansible_host'] }}"
tasks:
- name: Install required packages
apt:
name:
- tar
- wget
- curl
state: present
update_cache: yes
when: ansible_os_family == "Debian"
- name: Install required packages (RHEL/CentOS)
yum:
name:
- tar
- wget
- curl
state: present
when: ansible_os_family == "RedHat"
- name: Create rqlite directories
file:
path: "{{ item }}"
state: directory
mode: '0755'
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
with_items:
- "{{ rqlite_extract_dir }}"
- "{{ rqlite_data_dir }}"
- name: Download and extract rqlite
unarchive:
src: "{{ rqlite_download_url }}"
dest: "{{ rqlite_extract_dir }}"
remote_src: yes
creates: "{{ rqlite_extract_dir }}/rqlited"
extra_opts:
- --strip-components=1
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
- name: Copy rqlite binaries to /usr/local/bin
copy:
src: "{{ rqlite_extract_dir }}/{{ item }}"
dest: "/usr/local/bin/{{ item }}"
mode: '0755'
remote_src: yes
with_items:
- rqlited
- rqlite
- name: Create follower service file
copy:
dest: /etc/systemd/system/rqlited.service
content: |
[Unit]
Description=rqlite distributed SQLite follower node
After=network.target
[Service]
Type=simple
User={{ ansible_user }}
Group={{ ansible_user }}
WorkingDirectory=/home/{{ ansible_user }}
ExecStart=/usr/local/bin/rqlited -node-id {{ node_id }} -http-addr {{ ansible_host }}:{{ rqlite_http_port }} -raft-addr {{ ansible_host }}:{{ rqlite_raft_port }} -join {{ leader_host }}:{{ rqlite_raft_port }} {{ rqlite_data_dir }}
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
- name: Configure firewall (UFW)
ufw:
rule: allow
port: "{{ item }}"
proto: tcp
with_items:
- "{{ rqlite_http_port }}"
- "{{ rqlite_raft_port }}"
when: ansible_os_family == "Debian"
ignore_errors: yes # In case UFW isn't installed or enabled
- name: Configure firewall (firewalld)
firewalld:
port: "{{ item }}/tcp"
permanent: yes
state: enabled
with_items:
- "{{ rqlite_http_port }}"
- "{{ rqlite_raft_port }}"
when: ansible_os_family == "RedHat"
ignore_errors: yes # In case firewalld isn't installed or enabled
- name: Start and enable rqlite service on follower nodes
systemd:
name: rqlited
enabled: yes
daemon_reload: yes
state: started
# Check cluster status after all nodes are deployed
- name: Verify cluster status
hosts: localhost
connection: local
gather_facts: no
vars:
leader_host: "{{ hostvars['leader']['ansible_host'] }}"
rqlite_http_port: 4001
tasks:
- name: Wait for all nodes to be up
uri:
url: "http://{{ leader_host }}:{{ rqlite_http_port }}/readyz"
return_content: yes
register: cluster_ready
until: cluster_ready.status == 200
retries: 6
delay: 5
- name: Check cluster status
shell: "curl -s http://{{ leader_host }}:{{ rqlite_http_port }}/nodes?pretty"
register: cluster_status
changed_when: false
- name: Display cluster status
debug:
var: cluster_status.stdout_lines
+5
View File
@@ -0,0 +1,5 @@
---
all:
hosts:
localhost:
ansible_connection: local # This forces local connection for localhost
+157
View File
@@ -0,0 +1,157 @@
---
# Ansible playbook for deploying a 3-node rqlite cluster
# Filename: rqlite-cluster.yml
- name: Deploy rqlite cluster
hosts: rqlite_nodes
become: yes
vars:
rqlite_version: "7.21.4" # Change to the latest version as needed
rqlite_download_url: "https://github.com/rqlite/rqlite/releases/download/v{{ rqlite_version }}/rqlite-v{{ rqlite_version }}-linux-amd64.tar.gz"
rqlite_extract_dir: "/opt/rqlite"
rqlite_data_dir: "/var/lib/rqlite"
rqlite_http_port: 4001
rqlite_raft_port: 4002
tasks:
- name: Install required packages
apt:
name:
- tar
- wget
- curl
state: present
update_cache: yes
- name: Create directories
file:
path: "{{ item }}"
state: directory
mode: '0755'
loop:
- "{{ rqlite_extract_dir }}"
- "{{ rqlite_data_dir }}"
- name: Download and extract rqlite
unarchive:
src: "{{ rqlite_download_url }}"
dest: "{{ rqlite_extract_dir }}"
remote_src: yes
creates: "{{ rqlite_extract_dir }}/rqlited"
extra_opts:
- --strip-components=1
- name: Create symlinks to rqlite binaries
file:
src: "{{ rqlite_extract_dir }}/{{ item }}"
dest: "/usr/local/bin/{{ item }}"
state: link
loop:
- rqlited
- rqlite
- name: Create rqlite systemd service
template:
src: rqlited.service.j2
dest: /etc/systemd/system/rqlited.service
owner: root
group: root
mode: '0644'
- name: Check if UFW is installed and enabled
command: ufw status
register: ufw_status
changed_when: false
failed_when: false
- name: Configure firewall with UFW if available
ufw:
rule: allow
port: "{{ item }}"
proto: tcp
loop:
- "{{ rqlite_http_port }}"
- "{{ rqlite_raft_port }}"
when: "ufw_status.rc == 0 and 'Status: active' in ufw_status.stdout"
- name: Ensure iptables allows required ports
iptables:
chain: INPUT
protocol: tcp
destination_port: "{{ item }}"
jump: ACCEPT
loop:
- "{{ rqlite_http_port }}"
- "{{ rqlite_raft_port }}"
when: "ufw_status.rc != 0 or 'Status: active' not in ufw_status.stdout"
ignore_errors: yes
- name: Enable and start rqlited service on leader node
systemd:
name: rqlited
enabled: yes
daemon_reload: yes
state: started
when: inventory_hostname == groups['rqlite_nodes'][0]
- name: Wait for leader node to be up
wait_for:
host: "{{ hostvars[groups['rqlite_nodes'][0]]['ansible_host'] }}"
port: "{{ rqlite_http_port }}"
timeout: 60
when: inventory_hostname != groups['rqlite_nodes'][0]
- name: Debug connection to leader
shell: "curl -v {{ hostvars[groups['rqlite_nodes'][0]]['ansible_host'] }}:{{ rqlite_http_port }}/status"
register: connectivity_test
failed_when: false
when: inventory_hostname != groups['rqlite_nodes'][0]
- name: Display connectivity test results
debug:
var: connectivity_test
when: inventory_hostname != groups['rqlite_nodes'][0]
- name: Enable and start rqlited service on follower nodes
systemd:
name: rqlited
enabled: yes
daemon_reload: yes
state: started
when: inventory_hostname != groups['rqlite_nodes'][0]
- name: Check service status on all nodes
shell: systemctl status rqlited
register: service_status
failed_when: false
- name: Display service status
debug:
var: service_status.stdout_lines
- name: Check service logs
shell: journalctl -u rqlited -n 50
register: service_logs
failed_when: false
- name: Display service logs
debug:
var: service_logs.stdout_lines
- name: Wait for all nodes to be up
wait_for:
host: "{{ ansible_host }}"
port: "{{ rqlite_http_port }}"
timeout: 60
when: false
- name: Check cluster status
shell: "curl -s http://{{ hostvars[groups['rqlite_nodes'][0]]['ansible_host'] }}:{{ rqlite_http_port }}/nodes?pretty"
register: cluster_status
changed_when: false
when: inventory_hostname == groups['rqlite_nodes'][0]
- name: Display cluster status
debug:
var: cluster_status.stdout
when: inventory_hostname == groups['rqlite_nodes'][0]
+21
View File
@@ -0,0 +1,21 @@
[Unit]
Description=rqlite distributed SQLite database
After=network.target
[Service]
Type=simple
User=root
Group=root
LimitNOFILE=65536
Restart=always
RestartSec=5
{% if inventory_hostname == groups['rqlite_nodes'][0] %}
# Leader node configuration - initial node doesn't join any other node
ExecStart=/usr/local/bin/rqlited -node-id 1 -http-addr 0.0.0.0:{{ rqlite_http_port }} -raft-addr 0.0.0.0:{{ rqlite_raft_port }} -http-adv-addr {{ ansible_host }}:{{ rqlite_http_port }} -raft-adv-addr {{ ansible_host }}:{{ rqlite_raft_port }} {{ rqlite_data_dir }}
{% else %}
# Follower node configuration - they join the leader
ExecStart=/usr/local/bin/rqlited -node-id {{ groups['rqlite_nodes'].index(inventory_hostname) + 1 }} -http-addr 0.0.0.0:{{ rqlite_http_port }} -raft-addr 0.0.0.0:{{ rqlite_raft_port }} -http-adv-addr {{ ansible_host }}:{{ rqlite_http_port }} -raft-adv-addr {{ ansible_host }}:{{ rqlite_raft_port }} -join {{ hostvars[groups['rqlite_nodes'][0]]['ansible_host'] }}:{{ rqlite_raft_port }} {{ rqlite_data_dir }}
{% endif %}
[Install]
WantedBy=multi-user.target
+155
View File
@@ -0,0 +1,155 @@
---
# Ansible playbook for tearing down an rqlite cluster
# Run with: ansible-playbook -i inventory.yml teardown-rqlite.yml -e "node_ips=10.203.146.136,10.203.146.91,10.203.146.232 leader_ip=10.203.146.232 ansible_user=ubuntu"
- name: Process input parameters and set up host groups
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Validate required variables are provided
fail:
msg: "Required variables 'node_ips' and 'leader_ip' must be provided"
when: node_ips is not defined or leader_ip is not defined
- name: Create array of IPs from comma-separated string
set_fact:
ip_list: "{{ node_ips.split(',') }}"
- name: Verify leader_ip is in the node_ips list
fail:
msg: "Leader IP ({{ leader_ip }}) must be included in the list of node IPs"
when: leader_ip not in ip_list
- name: Add leader node to inventory
add_host:
name: "leader"
ansible_host: "{{ leader_ip }}"
node_id: "{{ ip_list.index(leader_ip) + 1 }}"
groups: "rqlite_leader,rqlite_cluster"
# Explicitly handle up to 2 follower nodes
- name: Add first follower node if it exists
add_host:
name: "follower1"
ansible_host: "{{ item }}"
node_id: "{{ ip_list.index(item) + 1 }}"
groups: "rqlite_followers,rqlite_cluster"
with_items: "{{ ip_list }}"
when: item != leader_ip
run_once: true
ignore_errors: yes
- name: Add second follower node if it exists
add_host:
name: "follower2"
ansible_host: "{{ item }}"
node_id: "{{ ip_list.index(item) + 1 }}"
groups: "rqlite_followers,rqlite_cluster"
with_items: "{{ ip_list }}"
when:
- item != leader_ip
- item != (hostvars['follower1']['ansible_host'] | default(''))
run_once: true
ignore_errors: yes
- name: Display hosts that will be cleaned up
debug:
msg: |
Preparing to remove rqlite from the following hosts:
- Leader: {{ hostvars['leader']['ansible_host'] }}
- Followers: {{ groups['rqlite_followers'] | map('extract', hostvars, ['ansible_host']) | join(', ') }}
WARNING: This will completely remove rqlite and all its data from these servers.
To cancel, press Ctrl+C now.
- name: Pause for confirmation
pause:
prompt: "Press Enter to continue with the teardown process, or Ctrl+C to abort"
seconds: 10
- name: Tear down rqlite on all nodes
hosts: rqlite_cluster
gather_facts: yes
become: yes
vars:
rqlite_data_dir: "/home/{{ ansible_user }}/data"
rqlite_extract_dir: "/opt/rqlite"
tasks:
- name: Stop and disable rqlited service
systemd:
name: rqlited
state: stopped
enabled: no
ignore_errors: yes
- name: Remove rqlited service file
file:
path: /etc/systemd/system/rqlited.service
state: absent
- name: Run daemon-reload to update systemd
systemd:
daemon_reload: yes
- name: Remove rqlite binaries
file:
path: "/usr/local/bin/{{ item }}"
state: absent
with_items:
- rqlited
- rqlite
- name: Remove rqlite data directory
file:
path: "{{ rqlite_data_dir }}"
state: absent
- name: Remove rqlite extract directory
file:
path: "{{ rqlite_extract_dir }}"
state: absent
- name: Close firewall ports (UFW)
ufw:
rule: allow
port: "{{ item }}"
proto: tcp
delete: yes
with_items:
- 4001 # Default HTTP port
- 4002 # Default Raft port
when: ansible_os_family == "Debian"
ignore_errors: yes # In case UFW isn't installed
- name: Close firewall ports (firewalld)
firewalld:
port: "{{ item }}/tcp"
permanent: yes
state: disabled
with_items:
- 4001 # Default HTTP port
- 4002 # Default Raft port
when: ansible_os_family == "RedHat"
ignore_errors: yes # In case firewalld isn't installed
- name: Verify teardown is complete
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Display completion message
debug:
msg: |
Teardown Complete
rqlite has been completely removed from:
- Leader: {{ hostvars['leader']['ansible_host'] }}
- Followers: {{ groups['rqlite_followers'] | map('extract', hostvars, ['ansible_host']) | join(', ') }}
The following items have been removed:
- rqlite service
- rqlite binaries
- rqlite data directories
- Firewall rules for rqlite
+39
View File
@@ -0,0 +1,39 @@
---
# ansible/tasks/rqlite/follower-join.yml
- name: Create follower service file
copy:
dest: /etc/systemd/system/rqlited.service
content: |
[Unit]
Description=rqlite distributed SQLite follower node
After=network.target
[Service]
Type=simple
User={{ ansible_user }}
Group={{ ansible_user }}
WorkingDirectory=/home/{{ ansible_user }}
ExecStart=/usr/local/bin/rqlited -node-id {{ node_id }} -http-addr {{ ansible_host }}:{{ rqlite_http_port }} -raft-addr {{ ansible_host }}:{{ rqlite_raft_port }} -join {{ hostvars[rqlite_leader_node]['ansible_host'] }}:{{ rqlite_raft_port }} {{ rqlite_data_dir }}
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
- name: Configure firewall (UFW)
ufw:
rule: allow
port: "{{ item }}"
proto: tcp
with_items:
- "{{ rqlite_http_port }}"
- "{{ rqlite_raft_port }}"
when: ansible_os_family == "Debian"
ignore_errors: yes # In case UFW isn't installed
- name: Start and enable rqlite service on follower node
systemd:
name: rqlited
enabled: yes
daemon_reload: yes
state: started
+61
View File
@@ -0,0 +1,61 @@
---
# ansible/tasks/rqlite/leader-init.yml
- name: Create leader service file
copy:
dest: /etc/systemd/system/rqlited.service
content: |
[Unit]
Description=rqlite distributed SQLite leader node
After=network.target
[Service]
Type=simple
User={{ ansible_user }}
Group={{ ansible_user }}
WorkingDirectory=/home/{{ ansible_user }}
ExecStart=/usr/local/bin/rqlited -node-id 1 -http-addr {{ ansible_host }}:{{ rqlite_http_port }} -raft-addr {{ ansible_host }}:{{ rqlite_raft_port }} {{ rqlite_data_dir }}
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
- name: Configure firewall (UFW)
ufw:
rule: allow
port: "{{ item }}"
proto: tcp
with_items:
- "{{ rqlite_http_port }}"
- "{{ rqlite_raft_port }}"
when: ansible_os_family == "Debian"
ignore_errors: yes # In case UFW isn't installed
- name: Start and enable rqlite service on leader node
systemd:
name: rqlited
enabled: yes
daemon_reload: yes
state: started
- name: Wait for leader node to be fully operational
uri:
url: "http://{{ ansible_host }}:{{ rqlite_http_port }}/readyz"
return_content: yes
register: leader_ready
until: leader_ready.status == 200
retries: 12 # Try for up to 1 minute (5 seconds × 12)
delay: 5 # 5 second delay between retries
- name: Ensure leader has stabilized
pause:
seconds: 5
- name: Check leader status
shell: "curl -s http://{{ ansible_host }}:{{ rqlite_http_port }}/status?pretty"
register: leader_status
changed_when: false
- name: Display leader status
debug:
var: leader_status.stdout_lines
+52
View File
@@ -0,0 +1,52 @@
---
# ansible/tasks/rqlite/node-setup.yml
- name: Install required packages
apt:
name:
- tar
- wget
- curl
state: present
update_cache: yes
when: ansible_os_family == "Debian"
- name: Install required packages (RHEL/CentOS)
yum:
name:
- tar
- wget
- curl
state: present
when: ansible_os_family == "RedHat"
- name: Create rqlite directories
file:
path: "{{ item }}"
state: directory
mode: '0755'
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
with_items:
- "{{ rqlite_extract_dir }}"
- "{{ rqlite_data_dir }}"
- name: Download and extract rqlite
unarchive:
src: "{{ rqlite_download_url }}"
dest: "{{ rqlite_extract_dir }}"
remote_src: yes
creates: "{{ rqlite_extract_dir }}/rqlited"
extra_opts:
- --strip-components=1
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
- name: Copy rqlite binaries to /usr/local/bin
copy:
src: "{{ rqlite_extract_dir }}/{{ item }}"
dest: "/usr/local/bin/{{ item }}"
mode: '0755'
remote_src: yes
with_items:
- rqlited
- rqlite
+44
View File
@@ -0,0 +1,44 @@
---
# ansible/tasks/rqlite/validate-cluster.yml
- name: Wait for all nodes to be up
uri:
url: "http://{{ hostvars[item]['ansible_host'] }}:{{ rqlite_http_port }}/readyz"
return_content: yes
register: node_ready
until: node_ready.status == 200
retries: 12
delay: 5
with_items:
- "{{ rqlite_node_list }}"
delegate_to: localhost
run_once: true
- name: Check cluster status
shell: "curl -s http://{{ hostvars[rqlite_leader_node]['ansible_host'] }}:{{ rqlite_http_port }}/nodes?pretty"
register: cluster_status
changed_when: false
delegate_to: localhost
run_once: true
- name: Display cluster status
debug:
var: cluster_status.stdout_lines
delegate_to: localhost
run_once: true
- name: Verify node count
debug:
msg: "Successfully configured {{ rqlite_node_list | length }} rqlite nodes"
delegate_to: localhost
run_once: true
- name: Display cluster information
debug:
msg:
- "rqlite cluster validation summary:"
- "Leader node: {{ hostvars[rqlite_leader_node]['ansible_host'] }}"
- "Follower nodes: {{ rqlite_node_list | difference([rqlite_leader_node]) | map('extract', hostvars, 'ansible_host') | list | join(', ') }}"
- "HTTP API port: {{ rqlite_http_port }}"
- "Raft consensus port: {{ rqlite_raft_port }}"
delegate_to: localhost
run_once: true
+5 -29
View File
@@ -6,37 +6,13 @@ A user-friendly CLI tool for provisioning and managing infrastructure components
> **Important**: This tool has only been tested on Ubuntu 24.04 and WSL Ubuntu 24.04. There is no guarantee it will work on other operating systems. > **Important**: This tool has only been tested on Ubuntu 24.04 and WSL Ubuntu 24.04. There is no guarantee it will work on other operating systems.
### Quick Install (Ubuntu) ### Quick install/build (Ubuntu)
```bash ```bash
./install.sh ./build.sh
# Make sure ~/.local/bin is in your PATH
export PATH="$PATH:$HOME/.local/bin"
``` ```
This installs the CLI tool to `~/.local/bin`. You may need to add this directory to your PATH if it's not already there. To make this change permanent, add the export line to your `~/.bashrc` or `~/.profile`. This builds the CLI tool to project root.
### Manual Install
Find the appropriate binary for your platform in the `bin` directory:
```bash
# Create ~/.local/bin directory if it doesn't exist
mkdir -p ~/.local/bin
# Example for Linux/AMD64
cp bin/linux_amd64/provision ~/.local/bin/
chmod +x ~/.local/bin/provision
# Add to PATH if needed
export PATH="$PATH:$HOME/.local/bin"
```
Available binaries:
- Linux: `bin/linux_amd64/provision` or `bin/linux_arm64/provision`
- macOS: `bin/darwin_amd64/provision` or `bin/darwin_arm64/provision`
- Windows: `bin/windows_amd64/provision.exe`
## Usage ## Usage
@@ -110,9 +86,9 @@ Cleanup completed successfully
To modify or extend the CLI tool: To modify or extend the CLI tool:
1. Make changes to the Go code 1. Make changes to the Go code
2. Build binaries for all platforms: 2. Build binary:
```bash ```bash
./scripts/build-release.sh ./build.sh
``` ```
### Adding New Commands ### Adding New Commands
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Executable
+29
View File
@@ -0,0 +1,29 @@
#!/bin/bash
set -e # Exit on error
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo "Building Kubernetes CLI tool for Linux amd64..."
# Check for Go installation
if ! command -v go &> /dev/null; then
echo -e "${RED}✗ Error:${NC} Go is not installed. This script requires Go to build the binary."
exit 1
fi
# Build for Linux amd64 only
echo "Building for linux/amd64..."
GOOS=linux GOARCH=amd64 go build -o "../provision-cli" ./cmd/provision
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓${NC} Binary built successfully!"
echo "The binary is available at: ../provision-cli"
chmod +x ../provision-cli
else
echo -e "${RED}✗ Error:${NC} Build failed."
exit 1
fi
+5 -3
View File
@@ -6,10 +6,11 @@ import (
"os/exec" "os/exec"
) )
// RunPlaybook executes an Ansible playbook with the given inventory and extra variables // RunPlaybook executes an Ansible playbook with the given inventory
func RunPlaybook(playbook, inventory string, extraArgs []string) error { func RunPlaybook(playbook, inventory string, extraArgs []string) error {
// Check if ansible-playbook is available // Check if ansible-playbook is available
if _, err := exec.LookPath("ansible-playbook"); err != nil { _, err := exec.LookPath("ansible-playbook")
if err != nil {
return fmt.Errorf("ansible-playbook command not found: %w", err) return fmt.Errorf("ansible-playbook command not found: %w", err)
} }
@@ -28,9 +29,10 @@ func RunPlaybook(playbook, inventory string, extraArgs []string) error {
// Connect the command's outputs to our process's outputs // Connect the command's outputs to our process's outputs
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin // Pass stdin for any prompts cmd.Stdin = os.Stdin
// Run the command // Run the command
fmt.Printf("Running: ansible-playbook %v\n", args) fmt.Printf("Running: ansible-playbook %v\n", args)
return cmd.Run() return cmd.Run()
} }
+9 -2
View File
@@ -5,20 +5,21 @@ import (
"github.com/bxtal-lsn/kubernetes/cli/cmd/provision/interactive" "github.com/bxtal-lsn/kubernetes/cli/cmd/provision/interactive"
"github.com/bxtal-lsn/kubernetes/cli/cmd/provision/kubernetes" "github.com/bxtal-lsn/kubernetes/cli/cmd/provision/kubernetes"
"github.com/bxtal-lsn/kubernetes/cli/cmd/provision/rqlite"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var provisionCmd = &cobra.Command{ var provisionCmd = &cobra.Command{
Use: "provision", Use: "provision",
Short: "Provision infrastructure components", Short: "Provision infrastructure components",
Long: `Provision infrastructure components like Kubernetes clusters, PostgreSQL, etc.`, Long: `Provision infrastructure components like Kubernetes clusters, RQLite, etc.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
provisionInteractive() provisionInteractive()
}, },
} }
func provisionInteractive() { func provisionInteractive() {
options := []string{"Kubernetes Cluster", "PostgreSQL Cluster"} options := []string{"Kubernetes Cluster", "RQLite Cluster"}
choice, err := interactive.PromptSelect("What would you like to provision?", options) choice, err := interactive.PromptSelect("What would you like to provision?", options)
if err != nil { if err != nil {
exitWithError("Failed to get user input", err) exitWithError("Failed to get user input", err)
@@ -30,6 +31,12 @@ func provisionInteractive() {
if err := kubernetes.ProvisionInteractive(); err != nil { if err := kubernetes.ProvisionInteractive(); err != nil {
exitWithError("Failed to provision Kubernetes cluster", err) exitWithError("Failed to provision Kubernetes cluster", err)
} }
case "RQLite Cluster":
fmt.Println("Starting RQLite cluster provisioning...")
if err := rqlite.ProvisionInteractive(); err != nil {
exitWithError("Failed to provision RQLite cluster", err)
}
default: default:
fmt.Println("Invalid choice") fmt.Println("Invalid choice")
} }
+9 -5
View File
@@ -8,13 +8,17 @@ import (
) )
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "provision", Use: "provision-cli",
Short: "Infrastructure provisioning tool", Short: "Kubernetes cluster provisioning tool",
Long: `A flexible CLI for provisioning and managing infrastructure components Long: `A simple CLI for provisioning and managing Kubernetes clusters on local VMs.
such as Kubernetes clusters, PostgreSQL clusters, and more.`, This tool must be run from the project root directory.`,
Run: func(cmd *cobra.Command, args []string) {
// If no subcommand is provided, show help
cmd.Help()
},
} }
// Execute adds all child commands to the root command and sets flags appropriately. // Execute adds all child commands to the root command and sets flags appropriately
func Execute() error { func Execute() error {
return rootCmd.Execute() return rootCmd.Execute()
} }
+14 -47
View File
@@ -4,62 +4,29 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"sync"
) )
var ( // GetRepoRoot simply returns the current working directory
repoRootCache string // Since we're assuming the binary runs from the project root
repoRootOnce sync.Once
repoRootErr error
)
// GetRepoRoot returns the repository root path, caching the result after the first call
func GetRepoRoot() (string, error) { func GetRepoRoot() (string, error) {
repoRootOnce.Do(func() { // Get current working directory
repoRootCache, repoRootErr = findRepoRoot()
})
return repoRootCache, repoRootErr
}
// findRepoRoot attempts to find the repository root by looking for marker files/directories
func findRepoRoot() (string, error) {
// Start from the current working directory
cwd, err := os.Getwd() cwd, err := os.Getwd()
if err != nil { if err != nil {
return "", fmt.Errorf("failed to get current working directory: %w", err) return "", fmt.Errorf("failed to get current working directory: %w", err)
} }
// Walk up the directory tree until we find a marker of the repository root // Basic validation - check if key directories exist
currentDir := cwd ansibleDir := filepath.Join(cwd, "ansible")
for { if !dirExists(ansibleDir) {
// Check for common repository root markers: return "", fmt.Errorf("ansible directory not found in %s - please run from project root", cwd)
// 1. Check if ansible directory exists
ansibleDir := filepath.Join(currentDir, "ansible")
if dirExists(ansibleDir) {
return currentDir, nil
}
// 2. Check for scripts directory with our kubernetes scripts
scriptsDir := filepath.Join(currentDir, "scripts", "provision-kubernetes.sh")
if fileExists(scriptsDir) {
return currentDir, nil
}
// 3. Check for .git directory as a fallback
gitDir := filepath.Join(currentDir, ".git")
if dirExists(gitDir) {
return currentDir, nil
}
// Move up one directory
parentDir := filepath.Dir(currentDir)
if parentDir == currentDir {
// We've reached the filesystem root without finding the repo root
return "", fmt.Errorf("repository root not found: no repository markers found in parent directories")
}
currentDir = parentDir
} }
scriptsDir := filepath.Join(cwd, "scripts")
if !dirExists(scriptsDir) {
return "", fmt.Errorf("scripts directory not found in %s - please run from project root", cwd)
}
return cwd, nil
} }
// dirExists checks if a directory exists // dirExists checks if a directory exists
+33 -119
View File
@@ -4,9 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"github.com/bxtal-lsn/kubernetes/cli/cmd/provision/ansible"
"github.com/bxtal-lsn/kubernetes/cli/cmd/provision/config" "github.com/bxtal-lsn/kubernetes/cli/cmd/provision/config"
"github.com/bxtal-lsn/kubernetes/cli/cmd/provision/interactive" "github.com/bxtal-lsn/kubernetes/cli/cmd/provision/interactive"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@@ -29,7 +27,7 @@ type Config struct {
KubernetesPackages []string `yaml:"kubernetes_packages"` KubernetesPackages []string `yaml:"kubernetes_packages"`
} }
// LoadDefaultConfig loads the default Kubernetes configuration from the defaults file // LoadDefaultConfig loads the default Kubernetes configuration
func LoadDefaultConfig() (*Config, error) { func LoadDefaultConfig() (*Config, error) {
defaultsPath, err := config.GetAnsiblePath("defaults/kubernetes.yml") defaultsPath, err := config.GetAnsiblePath("defaults/kubernetes.yml")
if err != nil { if err != nil {
@@ -50,27 +48,26 @@ func LoadDefaultConfig() (*Config, error) {
return config, nil return config, nil
} }
// SaveConfig saves the Kubernetes configuration to a temporary file // SaveConfig saves the configuration to a temp file
func SaveConfig(config *Config) (string, error) { func SaveConfig(config *Config) (string, error) {
data, err := yaml.Marshal(config) data, err := yaml.Marshal(config)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to marshal config: %w", err) return "", fmt.Errorf("failed to marshal config: %w", err)
} }
// Create a temporary directory // Create a temporary file in the system temp directory
tmpDir, err := os.MkdirTemp("", "k8s-provision") tempFile, err := os.CreateTemp("", "k8s-config-*.yml")
if err != nil { if err != nil {
return "", fmt.Errorf("failed to create temp dir: %w", err) return "", fmt.Errorf("failed to create temp file: %w", err)
}
defer tempFile.Close()
// Write the config data
if _, err := tempFile.Write(data); err != nil {
return "", fmt.Errorf("failed to write config to temp file: %w", err)
} }
// Write config to a file return tempFile.Name(), nil
configPath := filepath.Join(tmpDir, "kubernetes.yml")
err = os.WriteFile(configPath, data, 0o644)
if err != nil {
return "", fmt.Errorf("failed to write config file: %w", err)
}
return configPath, nil
} }
// ProvisionInteractive handles interactive Kubernetes cluster provisioning // ProvisionInteractive handles interactive Kubernetes cluster provisioning
@@ -102,100 +99,29 @@ func ProvisionInteractive() error {
return err return err
} }
var config *Config k8sConfig := defaultConfig
if useDefaults { if !useDefaults {
config = defaultConfig // If user doesn't want defaults, prompt for custom values
} else { // This part of the code remains mostly the same, but could be simplified further
config = defaultConfig // Start with defaults k8sVersion, err := interactive.PromptText("Kubernetes Version", k8sConfig.KubernetesVersion)
// Ask for each configurable value
k8sVersion, err := interactive.PromptText("Kubernetes Version", config.KubernetesVersion)
if err != nil { if err != nil {
return err return err
} }
config.KubernetesVersion = k8sVersion k8sConfig.KubernetesVersion = k8sVersion
podCIDR, err := interactive.PromptText("Pod CIDR", config.PodCIDR) // Add more prompts for other values here...
if err != nil { // For brevity, I'm just showing a simplified version
return err
}
config.PodCIDR = podCIDR
serviceCIDR, err := interactive.PromptText("Service CIDR", config.ServiceCIDR)
if err != nil {
return err
}
config.ServiceCIDR = serviceCIDR
cniPlugin, err := interactive.PromptSelect("CNI Plugin", []string{"calico", "flannel"})
if err != nil {
return err
}
config.CNIPlugin = cniPlugin
if cniPlugin == "calico" {
calicoVersion, err := interactive.PromptText("Calico Version", config.CalicoVersion)
if err != nil {
return err
}
config.CalicoVersion = calicoVersion
}
// Control plane resources
cpCPUs, err := interactive.PromptIntWithRange("Control Plane CPUs", config.ControlPlaneCPUs, 1, 16)
if err != nil {
return err
}
config.ControlPlaneCPUs = cpCPUs
cpMemory, err := interactive.PromptText("Control Plane Memory (e.g., 4G)", config.ControlPlaneMemory)
if err != nil {
return err
}
config.ControlPlaneMemory = cpMemory
cpDisk, err := interactive.PromptText("Control Plane Disk (e.g., 20G)", config.ControlPlaneDisk)
if err != nil {
return err
}
config.ControlPlaneDisk = cpDisk
// Worker resources
workerCPUs, err := interactive.PromptIntWithRange("Worker CPUs", config.WorkerCPUs, 1, 16)
if err != nil {
return err
}
config.WorkerCPUs = workerCPUs
workerMemory, err := interactive.PromptText("Worker Memory (e.g., 2G)", config.WorkerMemory)
if err != nil {
return err
}
config.WorkerMemory = workerMemory
workerDisk, err := interactive.PromptText("Worker Disk (e.g., 20G)", config.WorkerDisk)
if err != nil {
return err
}
config.WorkerDisk = workerDisk
// Split by comma and trim whitespace
config.DNSServers = []string{}
for _, server := range defaultConfig.DNSServers {
config.DNSServers = append(config.DNSServers, server)
}
} }
// Save configuration to a temporary file // Save configuration to a temporary file
configPath, err := SaveConfig(config) configPath, err := SaveConfig(k8sConfig)
if err != nil { if err != nil {
return err return err
} }
defer os.RemoveAll(filepath.Dir(configPath)) // Clean up temp dir when done defer os.Remove(configPath) // Clean up temp file when done
// Confirm before proceeding // Confirm before proceeding
fmt.Println("\nReady to provision Kubernetes cluster with the selected settings.") proceed, err := interactive.PromptConfirm("Do you want to proceed with provisioning?")
proceed, err := interactive.PromptConfirm("Do you want to proceed?")
if err != nil { if err != nil {
return err return err
} }
@@ -207,32 +133,20 @@ func ProvisionInteractive() error {
// Run the provision script // Run the provision script
fmt.Println("Running provisioning script...") fmt.Println("Running provisioning script...")
return runProvisionScript(configPath)
}
// runProvisionScript runs the actual Kubernetes provisioning // Get the path to the provision script
func runProvisionScript(configPath string) error { scriptPath, err := config.GetScriptsPath("provision-kubernetes.sh")
// Get repository paths for inventory and playbook
inventory, err := config.GetAnsiblePath("inventories/kubernetes.yml")
if err != nil { if err != nil {
return fmt.Errorf("failed to locate inventory file: %w", err) return fmt.Errorf("failed to locate provision script: %w", err)
} }
playbook, err := config.GetAnsiblePath("playbooks/kubernetes.yml") // Execute the script directly (simpler approach)
if err != nil { cmd := exec.Command(scriptPath)
return fmt.Errorf("failed to locate playbook file: %w", err) cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
// Check if files exist return cmd.Run()
if _, err := os.Stat(inventory); os.IsNotExist(err) {
return fmt.Errorf("inventory file does not exist: %s", inventory)
}
if _, err := os.Stat(playbook); os.IsNotExist(err) {
return fmt.Errorf("playbook file does not exist: %s", playbook)
}
// Run ansible-playbook
return ansible.RunPlaybook(playbook, inventory, []string{"-e", "@" + configPath})
} }
// Cleanup handles Kubernetes cluster cleanup // Cleanup handles Kubernetes cluster cleanup
@@ -252,7 +166,7 @@ func Cleanup() error {
cmd := exec.Command(scriptPath) cmd := exec.Command(scriptPath)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin // Pass stdin for any prompts cmd.Stdin = os.Stdin
return cmd.Run() return cmd.Run()
} }
+219
View File
@@ -0,0 +1,219 @@
package rqlite
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/bxtal-lsn/kubernetes/cli/cmd/provision/config"
"github.com/bxtal-lsn/kubernetes/cli/cmd/provision/interactive"
"gopkg.in/yaml.v3"
)
// Config holds rqlite cluster configuration
type Config struct {
RqliteVersion string `yaml:"rqlite_version"`
RqliteHttpPort int `yaml:"rqlite_http_port"`
RqliteRaftPort int `yaml:"rqlite_raft_port"`
RqliteDataDir string `yaml:"rqlite_data_dir"`
RqliteExtractDir string `yaml:"rqlite_extract_dir"`
NodeCPUs int `yaml:"node_cpus"`
NodeMemory string `yaml:"node_memory"`
NodeDisk string `yaml:"node_disk"`
DNSServers []string `yaml:"dns_servers"`
}
// LoadDefaultConfig loads the default rqlite configuration from the defaults file
func LoadDefaultConfig() (*Config, error) {
defaultsPath, err := config.GetAnsiblePath("defaults/rqlite.yml")
if err != nil {
return nil, fmt.Errorf("failed to locate defaults file: %w\n"+
"Please make sure you are running the command from within the repository and that the ansible/defaults/rqlite.yml file exists", err)
}
data, err := os.ReadFile(defaultsPath)
if err != nil {
return nil, fmt.Errorf("failed to read defaults file at %s: %w\n"+
"Please check file permissions and that the repository is correctly cloned", defaultsPath, err)
}
config := &Config{}
err = yaml.Unmarshal(data, config)
if err != nil {
return nil, fmt.Errorf("failed to parse defaults file: %w", err)
}
return config, nil
}
// SaveConfig saves the rqlite configuration to a temporary file
func SaveConfig(config *Config) (string, error) {
data, err := yaml.Marshal(config)
if err != nil {
return "", fmt.Errorf("failed to marshal config: %w", err)
}
// Create a temporary directory
tmpDir, err := os.MkdirTemp("", "rqlite-provision")
if err != nil {
return "", fmt.Errorf("failed to create temp dir: %w", err)
}
// Write config to a file
configPath := filepath.Join(tmpDir, "rqlite.yml")
err = os.WriteFile(configPath, data, 0o644)
if err != nil {
return "", fmt.Errorf("failed to write config file: %w", err)
}
return configPath, nil
}
// ProvisionInteractive handles interactive rqlite cluster provisioning
func ProvisionInteractive() error {
// Load default configuration
defaultConfig, err := LoadDefaultConfig()
if err != nil {
// If the file doesn't exist, use hardcoded defaults
defaultConfig = &Config{
RqliteVersion: "8.36.11",
RqliteHttpPort: 4001,
RqliteRaftPort: 4002,
RqliteDataDir: "/home/ubuntu/data",
RqliteExtractDir: "/opt/rqlite",
NodeCPUs: 2,
NodeMemory: "2G",
NodeDisk: "10G",
DNSServers: []string{"8.8.8.8", "8.8.4.4"},
}
}
// Display default settings
fmt.Println("\nCurrent default settings:")
fmt.Printf("rqlite Version: %s\n", defaultConfig.RqliteVersion)
fmt.Printf("HTTP Port: %d\n", defaultConfig.RqliteHttpPort)
fmt.Printf("Raft Port: %d\n", defaultConfig.RqliteRaftPort)
fmt.Printf("Node Resources: %d CPUs, %s Memory, %s Disk\n",
defaultConfig.NodeCPUs,
defaultConfig.NodeMemory,
defaultConfig.NodeDisk)
// Ask if user wants to use default settings
useDefaults, err := interactive.PromptConfirm("Do you want to use these default settings?")
if err != nil {
return err
}
var config *Config
if useDefaults {
config = defaultConfig
} else {
config = defaultConfig // Start with defaults
// Ask for each configurable value
rqliteVersion, err := interactive.PromptText("rqlite Version", config.RqliteVersion)
if err != nil {
return err
}
config.RqliteVersion = rqliteVersion
rqliteHttpPort, err := interactive.PromptInt("HTTP Port", config.RqliteHttpPort)
if err != nil {
return err
}
config.RqliteHttpPort = rqliteHttpPort
rqliteRaftPort, err := interactive.PromptInt("Raft Port", config.RqliteRaftPort)
if err != nil {
return err
}
config.RqliteRaftPort = rqliteRaftPort
// Node resources
nodeCPUs, err := interactive.PromptIntWithRange("Node CPUs", config.NodeCPUs, 1, 16)
if err != nil {
return err
}
config.NodeCPUs = nodeCPUs
nodeMemory, err := interactive.PromptText("Node Memory (e.g., 2G)", config.NodeMemory)
if err != nil {
return err
}
config.NodeMemory = nodeMemory
nodeDisk, err := interactive.PromptText("Node Disk (e.g., 10G)", config.NodeDisk)
if err != nil {
return err
}
config.NodeDisk = nodeDisk
}
// Save configuration to a temporary file
configPath, err := SaveConfig(config)
if err != nil {
return err
}
defer os.RemoveAll(filepath.Dir(configPath)) // Clean up temp dir when done
// Confirm before proceeding
fmt.Println("\nReady to provision rqlite cluster with the selected settings.")
proceed, err := interactive.PromptConfirm("Do you want to proceed?")
if err != nil {
return err
}
if !proceed {
fmt.Println("Provisioning cancelled.")
return nil
}
// Run the provision script
fmt.Println("Running provisioning script...")
return runProvisionScript(configPath)
}
// runProvisionScript runs the actual rqlite provisioning
func runProvisionScript(configPath string) error {
// Get path to provision script
scriptPath, err := config.GetScriptsPath("provision-rqlite.sh")
if err != nil {
return fmt.Errorf("failed to locate provision script: %w", err)
}
// Check if script exists
if _, err := os.Stat(scriptPath); os.IsNotExist(err) {
return fmt.Errorf("provision script does not exist: %s", scriptPath)
}
// Run the provision script with the config file
cmd := exec.Command(scriptPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin // Pass stdin for any prompts
return cmd.Run()
}
// Cleanup handles rqlite cluster cleanup
func Cleanup() error {
// Get path to cleanup script
scriptPath, err := config.GetScriptsPath("cleanup-rqlite.sh")
if err != nil {
return fmt.Errorf("failed to locate cleanup script: %w", err)
}
// Check if script exists
if _, err := os.Stat(scriptPath); os.IsNotExist(err) {
return fmt.Errorf("cleanup script does not exist: %s", scriptPath)
}
// Run the cleanup script
cmd := exec.Command(scriptPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin // Pass stdin for any prompts
return cmd.Run()
}
-84
View File
@@ -1,84 +0,0 @@
#!/bin/bash
set -e # Exit on error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# Determine OS and architecture
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
# Map architecture to Go architecture naming
case $ARCH in
x86_64)
GOARCH="amd64"
;;
aarch64|arm64)
GOARCH="arm64"
;;
*)
echo -e "${RED}✗ Error:${NC} Unsupported architecture: $ARCH"
exit 1
;;
esac
# Binary paths
BINARY_NAME="provision"
BINARY_PATH="bin/${OS}_${GOARCH}/${BINARY_NAME}"
INSTALL_DIR="$HOME/.local/bin"
echo "Installing infrastructure CLI tool..."
# Ensure .local directory exists
mkdir -p "$HOME/.local"
# Check and fix .local/bin directory ownership if needed
if [ "$(stat -c '%U' "$INSTALL_DIR" 2>/dev/null)" != "$USER" ]; then
echo -e "${YELLOW}⚠${NC} Fixing directory ownership..."
if command -v sudo >/dev/null; then
sudo chown -R "$USER:$USER" "$INSTALL_DIR"
else
echo -e "${RED}✗ Error:${NC} Cannot fix directory ownership. sudo not available."
exit 1
fi
fi
# Ensure .local/bin directory exists with correct permissions
mkdir -m 755 -p "$INSTALL_DIR"
# Check if the binary exists for this platform
if [ ! -f "$BINARY_PATH" ]; then
echo -e "${RED}✗ Error:${NC} Binary not found for your platform: ${OS}_${GOARCH}"
echo "Please compile the binary for your platform or contact the repository maintainer."
exit 1
fi
# Try copying the binary
if cp "$BINARY_PATH" "$INSTALL_DIR/$BINARY_NAME"; then
chmod 755 "$INSTALL_DIR/$BINARY_NAME"
echo -e "${GREEN}✓${NC} Installation successful!"
else
echo -e "${RED}✗ Error:${NC} Failed to copy binary. Check directory permissions."
echo "Current .local/bin permissions:"
ls -ld "$INSTALL_DIR"
exit 1
fi
# Check if .local/bin is in PATH
if [[ ":$PATH:" == *":$INSTALL_DIR:"* ]]; then
echo -e "${GREEN}✓${NC} .local/bin is already in your PATH."
else
echo -e "${YELLOW}⚠${NC} The installation directory is not in your PATH."
echo "Please add the following line to your shell profile:"
echo " export PATH=\"\$PATH:$INSTALL_DIR\""
echo ""
echo "Then restart your shell or run:"
echo " source ~/.bashrc # or your appropriate shell config file"
fi
echo ""
echo "Run '${BINARY_NAME}' to start using the tool."
-76
View File
@@ -1,76 +0,0 @@
#!/bin/bash
set -e # Exit on error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# Set variables
BINARY_NAME="provision"
OUTPUT_DIR="bin"
echo "Building release binaries for the infrastructure CLI tool..."
# Check for Go installation
if ! command -v go &> /dev/null; then
echo -e "${RED}✗ Error:${NC} Go is not installed. This script requires Go to build the binaries."
exit 1
fi
# Check Go version
GO_VERSION=$(go version | awk '{print $3}' | sed 's/go//')
GO_VERSION_MAJOR=$(echo $GO_VERSION | cut -d. -f1)
GO_VERSION_MINOR=$(echo $GO_VERSION | cut -d. -f2)
if [ "$GO_VERSION_MAJOR" -lt 1 ] || ([ "$GO_VERSION_MAJOR" -eq 1 ] && [ "$GO_VERSION_MINOR" -lt 18 ]); then
echo -e "${RED}✗ Error:${NC} Go version 1.18 or higher is required. You have $GO_VERSION."
exit 1
fi
echo -e "${GREEN}✓${NC} Using Go version $GO_VERSION"
# Create output directory
mkdir -p $OUTPUT_DIR
# Define target platforms
PLATFORMS=(
"linux/amd64"
"linux/arm64"
"darwin/amd64"
"darwin/arm64"
"windows/amd64"
)
# Build for each platform
for PLATFORM in "${PLATFORMS[@]}"; do
OS="${PLATFORM%/*}"
ARCH="${PLATFORM#*/}"
OUTPUT_NAME="${BINARY_NAME}"
if [ "$OS" = "windows" ]; then
OUTPUT_NAME="${OUTPUT_NAME}.exe"
fi
echo "Building for ${OS}/${ARCH}..."
mkdir -p "${OUTPUT_DIR}/${OS}_${ARCH}"
GOOS=$OS GOARCH=$ARCH go build -o "${OUTPUT_DIR}/${OS}_${ARCH}/${OUTPUT_NAME}" ./cmd/provision
echo -e "${GREEN}✓${NC} Built ${OS}/${ARCH}"
done
echo -e "${GREEN}✓${NC} All binaries built successfully!"
echo ""
echo "Binaries are available in the ${OUTPUT_DIR} directory with the following structure:"
echo " ${OUTPUT_DIR}/[os]_[arch]/${BINARY_NAME}"
echo ""
echo "Examples:"
echo " ${OUTPUT_DIR}/linux_amd64/${BINARY_NAME}"
echo " ${OUTPUT_DIR}/darwin_arm64/${BINARY_NAME}"
echo " ${OUTPUT_DIR}/windows_amd64/${BINARY_NAME}.exe"
echo ""
echo "To install the tool, users can run ./install.sh or copy the appropriate binary to their PATH."
+15
View File
@@ -0,0 +1,15 @@
# Kubernetes Applications
This directory contains Helm charts for deploying applications to your Kubernetes cluster.
## Available Applications
- **rqlite**: Distributed SQLite database (3 replicas by default)
## Usage
### Prerequisites
1. Ensure you have Helm installed:
```bash
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
+7
View File
@@ -0,0 +1,7 @@
# helm/rqlite/Chart.yaml
apiVersion: v2
name: rqlite
description: A Helm chart for deploying rqlite on Kubernetes
type: application
version: 0.1.0
appVersion: "8.36.11"
+7
View File
@@ -0,0 +1,7 @@
# helm/rqlite/Chart.yaml
apiVersion: v2
name: rqlite
description: A Helm chart for deploying rqlite on Kubernetes
type: application
version: 0.1.0
appVersion: "8.36.11"
+16
View File
@@ -0,0 +1,16 @@
# helm/rqlite/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}
spec:
selector:
app: {{ .Release.Name }}
ports:
- name: http
port: 4001
targetPort: http
- name: raft
port: 4002
targetPort: raft
clusterIP: None # Headless service for StatefulSet
+56
View File
@@ -0,0 +1,56 @@
# helm/rqlite/templates/statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: {{ .Release.Name }}
spec:
serviceName: {{ .Release.Name }}
replicas: {{ .Values.replicas }}
selector:
matchLabels:
app: {{ .Release.Name }}
template:
metadata:
labels:
app: {{ .Release.Name }}
spec:
containers:
- name: rqlite
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
ports:
- containerPort: 4001
name: http
- containerPort: 4002
name: raft
volumeMounts:
- name: data
mountPath: /rqlite/data
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
command:
- /bin/sh
- -c
- >
if [ "${POD_NAME}" = "{{ .Release.Name }}-0" ]; then
exec rqlited -node-id ${POD_NAME} -http-addr 0.0.0.0:4001 -raft-addr 0.0.0.0:4002 /rqlite/data;
else
exec rqlited -node-id ${POD_NAME} -http-addr 0.0.0.0:4001 -raft-addr 0.0.0.0:4002 -join {{ .Release.Name }}-0.{{ .Release.Name }}.{{ .Release.Namespace }}.svc.cluster.local:4002 /rqlite/data;
fi
{{- if not .Values.persistence.enabled }}
volumes:
- name: data
emptyDir: {}
{{- end }}
{{- if .Values.persistence.enabled }}
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: {{ .Values.persistence.size }}
{{- end }}
+12
View File
@@ -0,0 +1,12 @@
# helm/rqlite/values.yaml
replicas: 3
image:
repository: rqlite/rqlite
tag: "8.36.11"
persistence:
enabled: true
size: 1Gi
service:
type: ClusterIP
Regular → Executable
View File
+203
View File
@@ -0,0 +1,203 @@
#!/bin/bash
# provision-rqlite.sh - Creates and configures a rqlite environment with 3 nodes
set -e # Exit on error
# Set variables
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/common-lib.sh"
# rqlite VM Names
NODE1="rqlite1"
NODE2="rqlite2"
NODE3="rqlite3"
# SSH Key paths
SSH_KEY_NAME="id_rsa_provisioning"
SSH_KEY_PATH="$HOME/.ssh/$SSH_KEY_NAME"
SSH_PUB_KEY_PATH="$SSH_KEY_PATH.pub"
# Cloud-init and inventory paths
CLOUD_INIT_TEMPLATE="$SCRIPT_DIR/../multipass/cloud-init/common.yaml"
CLOUD_INIT_PROCESSED="$SCRIPT_DIR/../multipass/cloud-init/common_processed.yaml"
INVENTORY_FILE="$SCRIPT_DIR/../ansible/inventories/rqlite.yml"
DEFAULTS_FILE="$SCRIPT_DIR/../ansible/defaults/rqlite.yml"
# Default resource values
NODE_CPU=2
NODE_MEM="2G"
NODE_DISK="10G"
# Load resource values from defaults file if it exists
if [ -f "$DEFAULTS_FILE" ] && command -v python3 >/dev/null && python3 -c "import yaml" 2>/dev/null; then
echo "Loading VM resource settings from defaults file..."
# Extract the VM resource values using Python
RESOURCES=$(python3 -c "
import yaml
try:
with open('$DEFAULTS_FILE', 'r') as f:
vars = yaml.safe_load(f)
node_cpu = vars.get('node_cpus', 2)
node_mem = vars.get('node_memory', '2G')
node_disk = vars.get('node_disk', '10G')
print(f'{node_cpu} {node_mem} {node_disk}')
except Exception as e:
print('2 2G 10G') # Default values if anything fails
")
read NODE_CPU NODE_MEM NODE_DISK <<< "$RESOURCES"
echo "Node resources: $NODE_CPU CPUs, $NODE_MEM memory, $NODE_DISK disk"
else
echo "Using default VM resource settings"
echo "Node resources: $NODE_CPU CPUs, $NODE_MEM memory, $NODE_DISK disk"
fi
# Check dependencies
check_dependencies || exit 1
# Ensure SSH key exists
ensure_ssh_key "$SSH_KEY_PATH" || exit 1
# Process cloud-init template
prepare_cloud_init "$CLOUD_INIT_TEMPLATE" "$CLOUD_INIT_PROCESSED" "$SSH_PUB_KEY_PATH" || exit 1
# Check if VMs exist
echo "Checking for existing VMs..."
NODE1_EXISTS=false
NODE2_EXISTS=false
NODE3_EXISTS=false
if multipass info "$NODE1" &>/dev/null; then
echo -e "${GREEN}✓${NC} VM '$NODE1' exists"
NODE1_EXISTS=true
else
echo -e "${YELLOW}⚠${NC} VM '$NODE1' does not exist"
fi
if multipass info "$NODE2" &>/dev/null; then
echo -e "${GREEN}✓${NC} VM '$NODE2' exists"
NODE2_EXISTS=true
else
echo -e "${YELLOW}⚠${NC} VM '$NODE2' does not exist"
fi
if multipass info "$NODE3" &>/dev/null; then
echo -e "${GREEN}✓${NC} VM '$NODE3' exists"
NODE3_EXISTS=true
else
echo -e "${YELLOW}⚠${NC} VM '$NODE3' does not exist"
fi
# Create VMs if they don't exist
if [[ "$NODE1_EXISTS" == "false" || "$NODE2_EXISTS" == "false" || "$NODE3_EXISTS" == "false" ]]; then
echo "Creating rqlite VMs with Multipass..."
echo "This may take several minutes. Please be patient..."
# Create VMs that don't exist
if [[ "$NODE1_EXISTS" == "false" ]]; then
echo "Creating node VM: $NODE1 ($NODE_CPU CPUs, $NODE_MEM memory, $NODE_DISK disk)"
multipass launch --name "$NODE1" --cpus "$NODE_CPU" --memory "$NODE_MEM" --disk "$NODE_DISK" --cloud-init "$CLOUD_INIT_PROCESSED"
fi
if [[ "$NODE2_EXISTS" == "false" ]]; then
echo "Creating node VM: $NODE2 ($NODE_CPU CPUs, $NODE_MEM memory, $NODE_DISK disk)"
multipass launch --name "$NODE2" --cpus "$NODE_CPU" --memory "$NODE_MEM" --disk "$NODE_DISK" --cloud-init "$CLOUD_INIT_PROCESSED"
fi
if [[ "$NODE3_EXISTS" == "false" ]]; then
echo "Creating node VM: $NODE3 ($NODE_CPU CPUs, $NODE_MEM memory, $NODE_DISK disk)"
multipass launch --name "$NODE3" --cpus "$NODE_CPU" --memory "$NODE_MEM" --disk "$NODE_DISK" --cloud-init "$CLOUD_INIT_PROCESSED"
fi
# Remove processed file after use
rm "$CLOUD_INIT_PROCESSED"
# Give VMs a moment to initialize
echo "Waiting for VMs to initialize..."
sleep 15
fi
echo "Extracting IP addresses..."
# Extract IPs
NODE1_IP=$(multipass info "$NODE1" | grep "IPv4" | head -n 1 | awk '{print $2}')
NODE2_IP=$(multipass info "$NODE2" | grep "IPv4" | head -n 1 | awk '{print $2}')
NODE3_IP=$(multipass info "$NODE3" | grep "IPv4" | head -n 1 | awk '{print $2}')
# Verify IPs were extracted successfully
if [ -z "$NODE1_IP" ] || [ -z "$NODE2_IP" ] || [ -z "$NODE3_IP" ]; then
echo -e "${RED}✗ Error:${NC} Failed to extract IP addresses from multipass."
echo "Please verify that VMs are running using 'multipass list'"
exit 1
fi
# Print the IPs to verify
echo "Node1 IP: $NODE1_IP"
echo "Node2 IP: $NODE2_IP"
echo "Node3 IP: $NODE3_IP"
echo "Generating Ansible inventory..."
# Create directory for inventory file if it doesn't exist
mkdir -p "$(dirname "$INVENTORY_FILE")"
# Create inventory file
cat > "$INVENTORY_FILE" << EOF
---
all:
children:
rqlite_cluster:
children:
rqlite_leader:
hosts:
rqlite1:
ansible_host: $NODE1_IP
rqlite_followers:
hosts:
rqlite2:
ansible_host: $NODE2_IP
rqlite3:
ansible_host: $NODE3_IP
vars:
rqlite_nodes:
- rqlite1
- rqlite2
- rqlite3
vars:
ansible_user: ubuntu
ansible_become: yes
ansible_ssh_private_key_file: $SSH_KEY_PATH
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
EOF
echo -e "${GREEN}✓${NC} Ansible inventory created at $INVENTORY_FILE"
# Verify inventory
echo "Verifying inventory file contents:"
cat "$INVENTORY_FILE"
# Run Ansible playbook
PLAYBOOK="$SCRIPT_DIR/../ansible/playbooks/rqlite.yml"
if [ ! -f "$PLAYBOOK" ]; then
echo -e "${RED}✗ Error:${NC} Ansible playbook not found at $PLAYBOOK"
exit 1
fi
echo "Running Ansible playbook for rqlite deployment..."
echo "This will take some time. Please be patient..."
if ansible-playbook -i "$INVENTORY_FILE" "$PLAYBOOK"; then
echo -e "${GREEN}✓${NC} Ansible playbook completed successfully"
else
echo -e "${RED}✗ Error:${NC} Ansible playbook failed"
exit 1
fi
echo -e "${GREEN}✓${NC} rqlite environment setup complete."
echo "Leader Node: $NODE1_IP:4001 (HTTP) / $NODE1_IP:4002 (Raft)"
echo "Follower Nodes: $NODE2_IP, $NODE3_IP"
echo ""
echo "To access the rqlite cluster, run:"
echo "rqlite -H $NODE1_IP"
echo ""
echo "Or from inside one of the VMs:"
echo "multipass shell $NODE1"
echo "rqlite"