Docker image bundle vagrant-libvirt with vagrant (#1569)

Move the vagrant-libvirt plugin into being combined directly with
vagrant which both prevents the plugin from being reinstalled by any
subsequent call to `vagrant plugin install <plugin>` and avoids the need
to create and manage symlinks to a /vagrant.d directory for boxes.
This commit is contained in:
Darragh Bailey 2022-09-01 20:14:24 +01:00 committed by GitHub
parent c4efd1de49
commit d282a9dbd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 44 deletions

View File

@ -1,3 +1,7 @@
Dockerfile
Gemfile.Lock
pkg
# Vim swap files
**/.*.sw[po]

View File

@ -1,5 +1,5 @@
# syntax = docker/dockerfile:1.0-experimental
ARG VAGRANT_VERSION=2.2.19
# syntax = docker/dockerfile:1.3-labs
ARG VAGRANT_VERSION=2.3.0
FROM ubuntu:bionic as base
@ -19,20 +19,18 @@ RUN apt update \
&& rm -rf /var/lib/apt/lists \
;
RUN mkdir /vagrant
ENV VAGRANT_HOME /vagrant
ENV VAGRANT_HOME /.vagrant.d
ARG VAGRANT_VERSION
ENV VAGRANT_VERSION ${VAGRANT_VERSION}
RUN set -e \
&& curl https://releases.hashicorp.com/vagrant/${VAGRANT_VERSION}/vagrant_${VAGRANT_VERSION}_x86_64.deb -o vagrant.deb \
&& curl https://releases.hashicorp.com/vagrant/${VAGRANT_VERSION}/vagrant_${VAGRANT_VERSION}-1_amd64.deb -o vagrant.deb \
&& apt update \
&& apt install -y ./vagrant.deb \
&& rm -rf /var/lib/apt/lists/* \
&& rm -f vagrant.deb \
;
ENV VAGRANT_DEFAULT_PROVIDER=libvirt
FROM base as build
@ -58,17 +56,34 @@ WORKDIR /build
COPY . .
RUN rake build
RUN vagrant plugin install ./pkg/vagrant-libvirt*.gem
RUN for dir in boxes data tmp; \
do \
touch /vagrant/${dir}/.remove; \
done \
;
RUN find /opt/vagrant/embedded/ -type f | grep -v /opt/vagrant/embedded/plugins.json > /files-to-delete.txt
RUN /opt/vagrant/embedded/bin/gem install --install-dir /opt/vagrant/embedded/gems/${VAGRANT_VERSION} ./pkg/vagrant-libvirt*.gem
RUN cat <<EOF > /opt/vagrant/embedded/plugins.json
{
"version": "1",
"installed": {
"vagrant-libvirt": {
"ruby_version":"$(/opt/vagrant/embedded/bin/ruby -e 'puts RUBY_VERSION')",
"vagrant_version":"${VAGRANT_VERSION}",
"gem_version":"",
"require":"",
"sources":[]
}
}
}
EOF
FROM build as pruned
RUN cat /files-to-delete.txt | xargs rm -f
FROM base as slim
COPY --from=build /vagrant /vagrant
COPY --from=pruned /opt/vagrant/embedded/gems /opt/vagrant/embedded/gems
COPY --from=build /opt/vagrant/embedded/plugins.json /opt/vagrant/embedded/plugins.json
COPY entrypoint.sh /usr/local/bin/

View File

@ -110,21 +110,11 @@ vagrant(){
#### Using Podman
Preparing the podman run, only once:
```bash
mkdir -p ~/.vagrant.d/{boxes,data,tmp}
```
_N.B. This is needed until the entrypoint works for podman to only mount the `~/.vagrant.d` directory_
To run with Podman you need to include
```bash
--entrypoint /bin/bash \
--security-opt label=disable \
-v ~/.vagrant.d/boxes:/vagrant/boxes \
-v ~/.vagrant.d/data:/vagrant/data \
-v ~/.vagrant.d/tmp:/vagrant/tmp \
```
for example:
@ -134,9 +124,7 @@ vagrant(){
podman run -it --rm \
-e LIBVIRT_DEFAULT_URI \
-v /var/run/libvirt/:/var/run/libvirt/ \
-v ~/.vagrant.d/boxes:/vagrant/boxes \
-v ~/.vagrant.d/data:/vagrant/data \
-v ~/.vagrant.d/tmp:/vagrant/tmp \
-v ~/.vagrant.d:/.vagrant.d \
-v $(realpath "${PWD}"):${PWD} \
-w $(realpath "${PWD}") \
--network host \
@ -147,15 +135,14 @@ vagrant(){
}
```
Running Podman in rootless mode maps the root user inside the container to your host user so we need to bypass [entrypoint.sh](https://github.com/vagrant-libvirt/vagrant-libvirt/blob/main/entrypoint.sh) and mount persistent storage directly to `/vagrant`.
Running Podman in rootless mode maps the root user inside the container to your host user so we need to bypass [entrypoint.sh](https://github.com/vagrant-libvirt/vagrant-libvirt/blob/main/entrypoint.sh).
#### Extending the container image with additional vagrant plugins
By default the image published and used contains the entire tool chain required
to reinstall the vagrant-libvirt plugin and it's dependencies, as this is the
default behaviour of vagrant anytime a new plugin is installed. This means it
should be possible to use a simple `FROM` statement and ask vagrant to install
additional plugins.
to install the vagrant-libvirt plugin and it's dependencies. This allows any plugin
that requires native extensions to be installed and should be possible to use a
simple `FROM` statement and ask vagrant to install additional plugins.
```
FROM vagrantlibvirt/vagrant-libvirt:latest
@ -163,6 +150,11 @@ FROM vagrantlibvirt/vagrant-libvirt:latest
RUN vagrant plugin install <plugin>
```
Recently the image has now moved to bundling the plugin with the vagrant system plugins
it should no longer attempt to reinstall each time. Eventually this will become
the default so additional plugin installs will need to install any dependencies needed
by them.
### Ubuntu / Debian
{: .info }

View File

@ -104,19 +104,6 @@ then
${USERCMD} --shell /bin/bash -u ${USER_UID} -g ${USER_GID} -o -c "" -m ${USER} >/dev/null 2>&1 || exit 3
fi
# Perform switching in of boxes, data directory containing machine index
# and temporary directory from the user mounted environment
for dir in boxes data tmp
do
# if the directory hasn't been explicitly mounted over, remove it.
if [[ -e "/vagrant/${dir}/.remove" ]]
then
rm -rf /vagrant/${dir}
[[ ! -e ${vdir}/${dir} ]] && gosu ${USER} mkdir ${vdir}/${dir}
ln -s ${vdir}/${dir} /vagrant/${dir}
fi
done
# make sure the directories can be written to by vagrant otherwise will
# get a start up error
find "${VAGRANT_HOME}" -maxdepth 1 ! -exec chown -h ${USER}:${GROUP} {} \+