Przygotowanie środowiska deweloperskiego

vagrant Przygotowanie własnego obrazu Ubuntu 24.10 dla Vagranta

W tym wpisie pokazuję, jak przygotować własny obraz Ubuntu 24.10 dla Vagranta, opublikować go w rejestrze Vagrant Cloud (lub przez alternatywny hosting) oraz jak uruchomić środowisko deweloperskie przy pomocy vagrant up.

Cały proces ilustruje poniższy diagram:

---
config:
  theme: neo
  layout: dagre
  look: neo
---
flowchart LR
 subgraph s1["Przygotowanie obrazu"]
        n5["Przygotowanie maszyny<br>wirtualnej na virtualbox"]
        n6@{ label: "<span style=\"padding-left:\">Utworzenie box<br></span><span style=\"padding-left:\">(vagrant package)</span>" }
        n7@{ label: "Wysłanie do<br style=\"--tw-scale-x:\">Vagrant box Registry" }
  end
 subgraph s2["Użycie obrazu (vagrant up)"]
        n8["vagrant up"]
        n9["run bootstrap.bash"]
        n11["bash scripts"]
  end
    n5 --> n6
    n6 --> n7
    s1 --> s2
    n8 --> n9
    s2 --> n10("Ready to use")
    n9 --> n11
    n5@{ shape: rect}
    n6@{ shape: rect}
    n7@{ shape: rect}
    n9@{ shape: lean-r}
    n11@{ shape: procs}
    click n5 "#przygotowanie-maszyny-wirtualnej-na-virtualboxie"
    click n6 "#utworzenie-box"
    click n7 "#wys%c5%82anie-do-vagrant-box-registry"
    click n8 "#użycie-obrazu-vagrant-up"
    click n9 "#ręczne-uruchomienie-bootstrapbash"

Przygotowanie maszyny wirtualnej na virtualboxie

Przygotowałem maszynę wirtualną na virtualbox ubuntu 24-10

z użytkownikiem vagrant hasłem vagrant

echo "vagrant ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/vagrant
sudo apt-get update
sudo apt-get dist-upgrade -y
sudo apt install -y openssh-server curl wget sudo
sudo apt install -y build-essential dkms linux-headers-$(uname -r)
mkdir -p /home/vagrant/.ssh
curl -fsSL https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub -o /home/vagrant/.ssh/authorized_keys
chown -R vagrant:vagrant /home/vagrant/.ssh
chmod 700 /home/vagrant/.ssh
chmod 600 /home/vagrant/.ssh/authorized_keys
sudo apt-get autoremove -y
sudo apt-get clean
sudo rm -rf /var/lib/apt/lists/*
sudo timedatectl set-timezone UTC
sudo locale-gen en_US.UTF-8
sudo swapoff -a
sudo sed -i '/ swap / s/^/#/' /etc/fstab

Utworzenie box

vagrant package --base ubuntu-24-10 --output ubuntu-24.10.box
m@nbo-001-mr  ~  vagrant package --base ubuntu-24-10 --output ubuntu-24.10.box
==> ubuntu-24-10: Exporting VM...
==> ubuntu-24-10: Compressing package to: /home/maciej-rachuna/ubuntu-24.10.box
m@nbo-001-mr ~ ls -l |grep ubuntu-24
-rw-rw-r--  1 m m 1871476745 lip 23 08:49 ubuntu-24.10.box

Wysłanie do Vagrant box Registry

U mnie z jakiegoś powodu opcja nie działa, więc wrzuciłem obraz przez UI

vagrant cloud auth login

Użycie obrazu (vagrant up)

Pobranie repozytorium z vagrantem

git clone https://gitlab.com/pl.rachuna-net/tools/vagrant.git

Uruchomienie maszyny developerskiej

chmod +x start_vm.bash
./start_vm.bash
m@nbo-001-mr /repo/pl.rachuna-net/tools/vagrant (main) vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'rachuna-net.pl/ubuntu-24-10' version '1.0.0' is up to date...
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
==> default: Running provisioner: shell...
    default: Running: /tmp/vagrant-shell20250723-96391-b06anr.bash
    default: ===> 🚀 Run bootstrap script
    default: ===> 📁 Copy userfiles to /root
    default: ===> 🚀 Running script: /vagrant/provisioning/upgrade-ubuntu.bash
    default: ===> Upgrade system packages and distribution
    default: ===> 🚀 Running script: /vagrant/provisioning/install-packages.bash
    default: ===> Install packages
    default: ===> 🚀 Running script: /vagrant/provisioning/bootstrap-certs.bash
    default: ===> Pobieranie CA certyfikatu dla rachuna-net.pl
    default: ===> 🚀 Running script: /vagrant/provisioning/install-hashicorp.bash
    default: ===> 🚀 Running script: /vagrant/provisioning/install-zsh.bash
    default: ===> 🚀 Running script: /vagrant/provisioning/install-hugo.bash

Dostęp do maszyny wirtualnej

  • SSH jest dostępne na porcie 2222 na hoście, przekierowanym do portu 22 w VM.
  • Włączone jest przekazywanie agenta SSH oraz X11.
  • Połączenie można nawiązać poleceniem:
    ssh -p 2222 vagrant@localhost
    

Edytuj plik ~/.ssh/config, dodając wpis:

Host dev-station
   HostName 127.0.0.1
   Port 2222
   User vagrant
   IdentityFile /repo/pl.rachuna-net/tools/vagrant/.vagrant/machines/default/virtualbox/private_key
   ForwardX11 yes

Dzięki temu możesz łatwo łączyć się z maszyną wirtualną za pomocą polecenia:

ssh dev-station

Ręczne uruchomienie bootstrap.bash

vagrant@dev-station:/vagrant$ cd /vagrant/
vagrant@dev-station:/vagrant$ chmod +x bootstrap.bash 
vagrant@dev-station:/vagrant$ ./bootstrap.bash 
===> 🚀 Run bootstrap script
===> 📁 Copy userfiles to /home/vagrant
===> 🚀 Running script: /vagrant/provisioning/upgrade-ubuntu.bash
===> Upgrade system packages and distribution
===> 🚀 Running script: /vagrant/provisioning/install-packages.bash
===> Install packages
===> 🚀 Running script: /vagrant/provisioning/bootstrap-certs.bash
===> Pobieranie CA certyfikatu dla rachuna-net.pl
===> 🚀 Running script: /vagrant/provisioning/install-hashicorp.bash
===> 🚀 Running script: /vagrant/provisioning/install-zsh.bash
===> 🚀 Running script: /vagrant/provisioning/install-hugo.bash