Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated vagrant-binding to work with Vagrant 1.2.7 #3

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -13,3 +13,8 @@ bin
.settings/org.eclipse.m2e.core.prefs

.vagrant


.idea

*.iml
20 changes: 16 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.guigarage</groupId>
<artifactId>vagrant-binding</artifactId>
<version>0.0.3-SNAPSHOT</version>
<version>1.3.3-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
@@ -15,10 +15,22 @@
<version>2.5.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.7</source>
<target>1.7</target>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
@@ -30,7 +42,7 @@
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby-complete</artifactId>
<version>1.7.3</version>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
45 changes: 25 additions & 20 deletions src/main/java/com/guigarage/vagrant/Vagrant.java
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import org.apache.commons.io.FileUtils;
import org.jruby.RubyObject;
import org.jruby.embed.LocalContextScope;
import org.jruby.embed.LocalVariableBehavior;
import org.jruby.embed.ScriptingContainer;

import com.guigarage.vagrant.configuration.VagrantConfiguration;
@@ -20,34 +21,38 @@
public class Vagrant {

private ScriptingContainer scriptingContainer;

public Vagrant() {
this(false);
}

public Vagrant(boolean debug) {
scriptingContainer = new ScriptingContainer(
LocalContextScope.SINGLETHREAD);
if(debug) {
debug();
}
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private void debug() {
Map currentEnv = scriptingContainer.getEnvironment();
Map newEnv = new HashMap<>(currentEnv);
newEnv.put("VAGRANT_LOG", "DEBUG");
scriptingContainer.setEnvironment(newEnv);

public void shutdown() {
if (scriptingContainer != null) {
scriptingContainer.terminate();
}
}

public enum LogLevel {
DEBUG,
INFO,
WARN,
ERROR,
}

public Vagrant(LogLevel logLevel) {
scriptingContainer = new ScriptingContainer(LocalContextScope.THREADSAFE, LocalVariableBehavior.PERSISTENT);

Map currentEnv = scriptingContainer.getEnvironment();
Map newEnv = new HashMap(currentEnv);
newEnv.put("VAGRANT_LOG", logLevel.toString());

scriptingContainer.setEnvironment(newEnv);
}


public VagrantEnvironment createEnvironment() {
RubyObject vagrantEnv = (RubyObject) scriptingContainer.runScriptlet("require 'rubygems'\n"
+ "require 'vagrant'\n"
+ "\n" + "return Vagrant::Environment.new");
return new VagrantEnvironment(vagrantEnv);
}

public VagrantEnvironment createEnvironment(File path) {
RubyObject vagrantEnv = (RubyObject) scriptingContainer.runScriptlet("require 'rubygems'\n"
+ "require 'vagrant'\n"
Original file line number Diff line number Diff line change
@@ -22,11 +22,11 @@ public class VagrantConfiguration {

public VagrantConfiguration(VagrantEnvironmentConfig environmentConfig, Iterable<VagrantFileTemplateConfiguration> fileTemplateConfigurations, Iterable<VagrantFolderTemplateConfiguration> folderTemplateConfigurations) {
this.environmentConfig = environmentConfig;
this.fileTemplateConfigurations = new ArrayList<>();
this.fileTemplateConfigurations = new ArrayList<VagrantFileTemplateConfiguration>();
for(VagrantFileTemplateConfiguration fileTemplateConfiguration : fileTemplateConfigurations) {
this.fileTemplateConfigurations.add(fileTemplateConfiguration);
}
this.folderTemplateConfigurations = new ArrayList<>();
this.folderTemplateConfigurations = new ArrayList<VagrantFolderTemplateConfiguration>();
for(VagrantFolderTemplateConfiguration folderTemplateConfiguration : folderTemplateConfigurations) {
this.folderTemplateConfigurations.add(folderTemplateConfiguration);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.guigarage.vagrant.configuration;

import com.guigarage.vagrant.util.RandomString;

import java.net.URL;
import java.util.UUID;
import java.util.Map;

/**
* Some utilities for the configuration of Vagrant environments. This class creates configurationfiles for Vagrant.
@@ -10,13 +12,14 @@
*/
public class VagrantConfigurationUtilities {

private VagrantConfigurationUtilities() {
private VagrantConfigurationUtilities() {
}

public static String createVagrantFileContent(
VagrantEnvironmentConfig config) {
StringBuilder builder = new StringBuilder();
builder.append("Vagrant::Config.run do |config|").append("\n");
builder.append("VAGRANTFILE_API_VERSION = \"2\"\n");
builder.append("Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|").append("\n");

//TODO: Wenn nicht MultiVM kann man hier auch ein einfaches Config-File erstellen und so primaryVm etc. nutzen...

@@ -31,7 +34,7 @@ private static String createVmInMultiEnvConfig(VagrantVmConfig vmConfig) {
StringBuilder builder = new StringBuilder();
String vmName = vmConfig.getName();
if (vmName == null) {
vmName = UUID.randomUUID().toString();
vmName = RandomString.randomString(10);
}
builder.append(
"config.vm.define :" + vmName + " do |" + vmName
@@ -41,16 +44,38 @@ private static String createVmInMultiEnvConfig(VagrantVmConfig vmConfig) {
.getPortForwardings()) {
builder.append(createPortForwardingConfig(vmName + "_config", portForwarding));
}

if (!vmConfig.getCustomizeVm().isEmpty()) {
builder.append("config.vm.provider :virtualbox do |vb|\n");
for (String[] customization : vmConfig.getCustomizeVm()) {
builder.append(createCustomizedVmConfig(customization));
}
builder.append("end\n");
}

for (VagrantSyncFolder syncFolder : vmConfig
.getSyncFolders()) {
builder.append(createSyncFolderConfig(vmName + "_config", syncFolder));
}

builder.append(createBoxNameConfig(vmName + "_config", vmConfig.getBoxName()));

if (vmConfig.getGuest() != null) {
builder.append(createVmGuestConfig(vmName + "_config", vmConfig.getGuest()));
}

URL boxUrl = vmConfig.getBoxUrl();
if (boxUrl != null) {
builder.append(createBoxUrlConfig(vmName + "_config", boxUrl));
}

String ip = vmConfig.getIp();
if (ip != null) {
builder.append(createHostOnlyIpConfig(vmName + "_config", ip));
if (vmConfig.isPrivateNetwork()) {
builder.append(createPrivateNetworkIpConfig(vmName + "_config", ip));
} else {
builder.append(createHostOnlyIpConfig(vmName + "_config", ip));
}
}

boolean guiMode = vmConfig.isGuiMode();
@@ -69,43 +94,83 @@ private static String createVmInMultiEnvConfig(VagrantVmConfig vmConfig) {
builder.append(createPuppetProvisionerConfig(
vmName + "_config", puppetProvisionerConfig));
}

String bashProvisionScript = vmConfig.getBashProvisionScript();
if (bashProvisionScript != null) {
builder.append(createBashProvisionScriptConfig(vmName + "_config", bashProvisionScript));
}

builder.append("end").append("\n");
return builder.toString();
}

private static String createPortForwardingConfig(String vmConfigName, VagrantPortForwarding portForwarding) {

private static String createCustomizedVmConfig(String[] entry) {
StringBuilder builder = new StringBuilder();
builder.append("\t").append("vb.customize [");

for (int i = 0; i < entry.length; i++) {
if (entry[i].equals(":id")) {
builder.append(entry[i]);
} else {
builder.append("\"").append(entry[i]).append("\"");
}
if (i < entry.length - 1) {
builder.append(",");
}
}

builder.append("]\n");
return builder.toString();
}

private static String createVmGuestConfig(String vmConfigName, String guest) {
StringBuilder builder = new StringBuilder();
builder.append(
vmConfigName + ".vm.guest = \"" + guest
+ "\"").append("\n");
return builder.toString();
}

private static String createSyncFolderConfig(String vmConfigName, VagrantSyncFolder syncFolder) {
StringBuilder builder = new StringBuilder();
builder.append(vmConfigName + ".vm.synced_folder ")
.append("'" + syncFolder.getSource()).append("', ")
.append("'" + syncFolder.getDestination()).append("', ")
.append("disabled: " + syncFolder.isDisabled())
.append("\n");
return builder.toString();

}

private static String createPortForwardingConfig(String vmConfigName, VagrantPortForwarding portForwarding) {
StringBuilder builder = new StringBuilder();
String portForwardingName = portForwarding.getName();
if (portForwardingName != null) {
builder.append(
vmConfigName + ".vm.forward_port \""
+ portForwardingName + "\", "
+ portForwarding.getGuestport() + ", "
+ portForwarding.getHostport())
.append("\n");
} else {
builder.append(
vmConfigName + ".vm.forward_port "
+ portForwarding.getGuestport() + ", "
+ portForwarding.getHostport())
.append("\n");
}
return builder.toString();
builder.append(
vmConfigName + ".vm.network :forwarded_port, guest: " + portForwarding.getGuestport()
+ ", host: " + portForwarding.getHostport())
.append("\n");
return builder.toString();
}

private static String createBoxNameConfig(String vmConfigName, String boxName) {
StringBuilder builder = new StringBuilder();
builder.append(
vmConfigName + ".vm.box = \"" + boxName
+ "\"").append("\n");
return builder.toString();
}

private static String createHostNameConfig(String vmConfigName, String hostName) {

private static String createBashProvisionScriptConfig(String vmConfigName, String bashProvisionScript) {
StringBuilder builder = new StringBuilder();
builder.append(vmConfigName + ".vm.provision :shell, :path => \"" + bashProvisionScript + "\"").append("\n");
return builder.toString();
}


private static String createHostNameConfig(String vmConfigName, String hostName) {
StringBuilder builder = new StringBuilder();
if (hostName != null) {
builder.append(
vmConfigName + ".vm.host_name = \"" + hostName + ".local\"")
vmConfigName + ".vm.hostname = \"" + hostName + "\"")
.append("\n");
}
return builder.toString();
@@ -130,6 +195,16 @@ private static String createHostOnlyIpConfig(String vmConfigName, String ip) {
}
return builder.toString();
}

private static String createPrivateNetworkIpConfig(String vmConfigName, String ip) {
StringBuilder builder = new StringBuilder();
if (ip != null) {
builder.append(
vmConfigName + ".vm.network :private_network, ip: \"" + ip + "\"")
.append("\n");
}
return builder.toString();
}

private static String createGuiModeConfig(String vmConfigName) {
StringBuilder builder = new StringBuilder();
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ public class VagrantEnvironmentConfig {
private List<VagrantVmConfig> vmConfigs;

public VagrantEnvironmentConfig(Iterable<VagrantVmConfig> vmConfigs) {
this.vmConfigs = new ArrayList<>();
this.vmConfigs = new ArrayList<VagrantVmConfig>();
if(vmConfigs != null) {
for(VagrantVmConfig vagrantVmConfig : vmConfigs) {
this.vmConfigs.add(vagrantVmConfig);
Original file line number Diff line number Diff line change
@@ -12,27 +12,10 @@ public class VagrantPortForwarding {

private int hostport;

private String name;

/**
* Constructor for the port forwarding
* @param name the name of the port forwarding. This is optional and used by Vagrant internally
* @param guestport the guestport
* @param hostport the hostport
*/
public VagrantPortForwarding(String name, int guestport, int hostport) {
this.guestport = guestport;
this.hostport = hostport;
this.name = name;
}

/**
* The name of the port forwarding. This is optional and used by Vagrant internally
* @return name of the port forwarding
*/
public String getName() {
return name;
}
public VagrantPortForwarding(int guestport, int hostport) {
this.guestport = guestport;
this.hostport = hostport;
}

/**
* Returns the guestport.
Loading