Skip to content

Commit

Permalink
fix ssh key copying + fix key removing too early + better comments
Browse files Browse the repository at this point in the history
  • Loading branch information
frdescam committed Oct 20, 2022
1 parent bda4938 commit e1ce1c3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
19 changes: 11 additions & 8 deletions p1/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,29 @@ Vagrant.configure("2") do |config|
v.memory = 512
v.cpus = 1
end
# Setup shared folder between host and VMs
config.vm.synced_folder "./shared", "/vagrant"

# This is triggered before both VMs are started which is useless
# Copy ssh key before starting
config.trigger.before :up do |trigger|
trigger.run = { inline: <<-SHELL
if [ ! -f "~/.ssh/id_rsa" ]
then
ssh-keyken -f ~/.ssh/id_rsa -N ""
cp ~/.ssh/id_rsa.pub ./shared
bash -c "
if [ ! -f ~/.ssh/id_rsa ]
then
ssh-keyken -f ~/.ssh/id_rsa -N ''
fi
cp ~/.ssh/id_rsa.pub ./shared
"
SHELL
}
end

# Create the ltouretS VM
config.vm.define "ltouretS" do |master|
master.vm.hostname = "ltouretS"
# You may need to add a /etc/vbox/networks.conf file containing
# '* 0.0.0.0/0 ::/0' on your host system for this line to work properly
# idk why, will check later
master.vm.network :private_network, ip: "192.168.42.110"
# Run at VM creation the K3s installation script in controller mode
master.vm.provision "shell", privileged: true, path: "install_k3s.sh", args: ["controller"]
Expand All @@ -40,7 +46,4 @@ Vagrant.configure("2") do |config|
# Run at VM creation the K3s installation script in agent mode
worker.vm.provision "shell", privileged: true, path: "install_k3s.sh", args: ["agent", "192.168.42.110"]
end
config.trigger.after :up do |trigger|
trigger.run = {inline: "rm ./shared/id_rsa.pub"}
end
end
12 changes: 11 additions & 1 deletion p1/install_k3s.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@ if [ "$1" = "controller" ]
then
echo "[provisioned script] Starting installation in controller mode ..."
curl -sfL https://get.k3s.io | sh -
sleep 30
echo "[provisioned script] Waiting for k3s to start ..."
while [ ! -f /var/lib/rancher/k3s/server/node-token ]
do
sleep 1
done
echo "[provisioned script] Copying node-token to shared folder ..."
cp /var/lib/rancher/k3s/server/node-token /vagrant/
echo "[provisioned script] Authorizating ssh key ..."
cat /vagrant/id_rsa.pub >> /home/vagrant/.ssh/authorized_keys
else
echo "[provisioned script] Starting installation in agent mode ..."
curl -sfL https://get.k3s.io | \
K3S_TOKEN=$(cat /vagrant/node-token) K3S_URL=https://$2:6443 sh -
echo "[provisioned script] Authorizating ssh key ..."
cat /vagrant/id_rsa.pub >> /home/vagrant/.ssh/authorized_keys

echo "[provisioned script] Removing files from shared folder ..."
rm /vagrant/id_rsa.pub /vagrant/node-token
fi

0 comments on commit e1ce1c3

Please sign in to comment.