All Downloads are FREE. Search and download functionalities are using the official Maven repository.

au.net.causal.maven.plugins.boxdb.db.VagrantDatabase Maven / Gradle / Ivy

There is a newer version: 3.3
Show newest version
package au.net.causal.maven.plugins.boxdb.db;

import au.net.causal.maven.plugins.boxdb.vagrant.BoxDefinition;
import au.net.causal.maven.plugins.boxdb.vagrant.Vagrant;
import au.net.causal.maven.plugins.boxdb.vagrant.VagrantException;
import org.apache.maven.shared.utils.io.FileUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.TimeoutException;

public abstract class VagrantDatabase implements BoxDatabase
{
    private final BoxConfiguration boxConfiguration;
    private final ProjectConfiguration projectConfiguration;
    private final BoxContext context;

    private final Vagrant vagrant;
    private final Path vagrantDirectory;
    private final Path vagrantMainFile;

    public VagrantDatabase(BoxConfiguration boxConfiguration, ProjectConfiguration projectConfiguration, BoxContext context,
                           Vagrant vagrant, Path vagrantDirectory, Path vagrantMainFile)
    {
        Objects.requireNonNull(boxConfiguration, "boxConfiguration == null");
        Objects.requireNonNull(projectConfiguration, "projectConfiguration == null");
        Objects.requireNonNull(context, "context == null");
        Objects.requireNonNull(vagrant, "vagrant == null");
        Objects.requireNonNull(vagrantDirectory, "vagrantDirectory == null");
        Objects.requireNonNull(vagrantMainFile, "vagrantMainFile == null");

        this.boxConfiguration = boxConfiguration;
        this.projectConfiguration = projectConfiguration;
        this.context = context;
        this.vagrant = vagrant;
        this.vagrantDirectory = vagrantDirectory;
        this.vagrantMainFile = vagrantMainFile;
    }

    protected Vagrant getVagrant()
    {
        return vagrant;
    }

    protected BoxConfiguration getBoxConfiguration()
    {
        return boxConfiguration;
    }

    protected ProjectConfiguration getProjectConfiguration()
    {
        return projectConfiguration;
    }

    @Override
    public void createAndStart() throws BoxDatabaseException
    {
        start();
    }

    protected Vagrant.BoxStatus getVagrantStatus()
    throws BoxDatabaseException
    {
        getContext().getLog().debug("Getting status for " + vagrantDirectory);
        Vagrant.StatusOptions options = new Vagrant.StatusOptions(vagrantDirectory, containerName());
        configureInstanceOptions(options);
        try
        {
            return vagrant.status(options);
        }
        catch (VagrantException e)
        {
            throw new BoxDatabaseException("Failed to get Vagrant status: " + e.getMessage(), e);
        }
    }

    @Override
    public boolean exists() throws BoxDatabaseException
    {
        return getVagrantStatus() != Vagrant.BoxStatus.NOT_CREATED;
    }

    @Override
    public boolean isRunning() throws BoxDatabaseException
    {
        return getVagrantStatus() == Vagrant.BoxStatus.RUNNING;
    }

    @Override
    public String getName()
    {
        return containerName();
    }

    @Override
    public void start() throws BoxDatabaseException
    {
        Vagrant.UpOptions options = new Vagrant.UpOptions(vagrantDirectory, containerName());
        configureInstanceOptions(options);
        try
        {
            vagrant.up(options);
        }
        catch (VagrantException e)
        {
            throw new BoxDatabaseException("Failed to start Vagrant: " + e.getMessage(), e);
        }
    }

    @Override
    public void stop() throws BoxDatabaseException
    {
        Vagrant.HaltOptions options = new Vagrant.HaltOptions(vagrantDirectory, containerName());
        configureInstanceOptions(options);
        try
        {
            vagrant.halt(options);
        }
        catch (VagrantException e)
        {
            throw new BoxDatabaseException("Failed to stop Vagrant: " + e.getMessage(), e);
        }
    }

    protected void configureInstanceOptions(Vagrant.InstanceOptions options)
    {
        configureBaseOptions(options);
        options.setBaseDirectory(getVagrantDirectory());
        options.setBoxName(containerName());
    }

    protected void configureBaseOptions(Vagrant.BaseOptions options)
    {
        options.env("CONTAINERNAME", containerName());
    }

    @Override
    public void delete() throws BoxDatabaseException
    {
        Vagrant.DestroyOptions options = new Vagrant.DestroyOptions(vagrantDirectory, containerName());
        configureInstanceOptions(options);
        try
        {
            vagrant.destroy(options);
        }
        catch (VagrantException e)
        {
            throw new BoxDatabaseException("Failed to perform Vagrant destroy: " + e.getMessage(), e);
        }

        //Also delete the Vagrant directory
        if (Files.exists(vagrantDirectory))
        {
            getContext().getLog().info("Deleting Vagrant directory " + vagrantDirectory.toString());
            try
            {
                FileUtils.deleteDirectory(vagrantDirectory.toFile());
            }
            catch (IOException e)
            {
                throw new BoxDatabaseException("Failed to delete container directory " + vagrantDirectory.toString() + ": " + e, e);
            }
        }
    }

    @Override
    public void deleteImage() throws BoxDatabaseException
    {
        Vagrant.BoxRemoveOptions options = new Vagrant.BoxRemoveOptions(vagrantBoxDefinition());
        configureBaseOptions(options);
        options.setForce(true);
        try
        {
            vagrant.boxRemove(options);
        }
        catch (VagrantException e)
        {
            throw new BoxDatabaseException("Failed to remove Vagrant box: " + e.getMessage(), e);
        }
    }

    @Override
    public void waitUntilStarted(Duration maxTimeToWait)
    throws TimeoutException, BoxDatabaseException
    {
        DatabaseUtils.waitUntilTcpPortResponding(maxTimeToWait, this, getContext());
    }

    /**
     * @return the name of the container.  Defaults to the name given in the configuration.
     */
    protected String containerName()
    {
        return boxConfiguration.getContainerName();
    }

    protected Path getVagrantDirectory()
    {
        return vagrantDirectory;
    }

    protected abstract BoxDefinition vagrantBoxDefinition();

    protected BoxContext getContext()
    {
        return context;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy