package com.nitorcreations.cloudyplugin;


import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.enterprise.config.EnterpriseConfigurationModule;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.scriptbuilder.statements.login.AdminAccess;
import org.jclouds.sshj.config.SshjSshClientModule;

import com.google.common.collect.ImmutableSet;
import com.google.common.io.Files;
import com.google.inject.Module;

import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.getOnlyElement;
import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_AMI_QUERY;
import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_CC_AMI_QUERY;
import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_SCRIPT_COMPLETE;
import static org.jclouds.compute.options.TemplateOptions.Builder.overrideLoginUser;
import static org.jclouds.compute.options.TemplateOptions.Builder.runScript;

import java.io.File;
import java.util.Properties;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

/**
 *
 */
@Mojo( name = "init" )
public class InitMojo extends AbstractMojo
{
	@Parameter( defaultValue = "${project}", required = true )
	private MavenProject project;

	@Parameter( defaultValue = "none", property = "ciHost", required = true )
	private String ciHost;

	@Parameter ( property = "identity", required = true )
	private String identity;

	public void execute() throws MojoExecutionException	{
		if ("none".equals(ciHost)) {
			try (Scanner s = new Scanner(System.in)){
				String provider = "aws-ec2";
				String groupName = project.getGroupId().replaceAll("[^a-zA-Z\\-]", "-") + "-" + project.getArtifactId().replaceAll("[^a-zA-Z\\-]", "-");

				getLog().info("Please give aws credential");
				String credential = s.next();
				
				//LoginCredentials login =  getLoginForCommandExecution();

				ComputeService compute = initComputeService(provider, identity, credential);

				System.out.printf(">> adding node to group %s%n", groupName);

				TemplateBuilder templateBuilder = compute.templateBuilder();
				//templateBuilder = templateBuilder.imageId("us-east-1/ami-d5ddd9bc");

	            Statement bootInstructions = AdminAccess.standard();
	            templateBuilder.options(runScript(bootInstructions));

				NodeMetadata node = getOnlyElement(compute.createNodesInGroup(groupName, 1, templateBuilder.build()));
				getLog().info(String.format("<< node %s: %s%n", node.getId(),
						concat(node.getPrivateAddresses(), node.getPublicAddresses())));
				getLog().info(String.format("Private key: %s\n", node.getCredentials().getOptionalPrivateKey().or("")));
				getLog().info(String.format("Identity: %s\n", node.getCredentials().identity));

				getLog().info(String.format("Credential: %s\n", node.getCredentials().credential));
			} catch (Exception e){
				throw new MojoExecutionException("" ,e);
			}
		}
		project.getFile();
	}

	private static ComputeService initComputeService(String provider, String identity, String credential) {
		// example of specific properties, in this case optimizing image list to
		// only amazon supplied
		Properties properties = new Properties();
		properties.setProperty(PROPERTY_EC2_AMI_QUERY, "owner-id=137112412989;state=available;image-type=machine");
		properties.setProperty(PROPERTY_EC2_CC_AMI_QUERY, "");
		long scriptTimeout = TimeUnit.MILLISECONDS.convert(20, TimeUnit.MINUTES);
		properties.setProperty(TIMEOUT_SCRIPT_COMPLETE, scriptTimeout + "");

		// example of injecting a ssh implementation
		Iterable<Module> modules = ImmutableSet.<Module> of(
				new SshjSshClientModule(),
				new SLF4JLoggingModule(),
				new EnterpriseConfigurationModule());

		ContextBuilder builder = ContextBuilder.newBuilder(provider)
				.credentials(identity, credential)
				.modules(modules)
				.overrides(properties);

		System.out.printf(">> initializing %s%n", builder.getApiMetadata());

		return builder.buildView(ComputeServiceContext.class).getComputeService();
	}

	private static LoginCredentials getLoginForCommandExecution() {
		try {
			String user = System.getProperty("user.name");
			String privateKey = Files.toString(
					new File(System.getProperty("user.home") + "/.ssh/id_rsa"), UTF_8);
			return LoginCredentials.builder().
					user(user).privateKey(privateKey).build();
		} catch (Exception e) {
			System.err.println("error reading ssh key " + e.getMessage());
			System.exit(1);
			return null;
		}
	}

	}

@@ -559,16 +559,75 @@
 ntials;%0A
+import org.jclouds.ec2.compute.options.EC2TemplateOptions;%0A
 import o
@@ -678,32 +678,32 @@
 gurationModule;%0A
-
 import org.jclou
@@ -1468,16 +1468,108 @@
 MPLETE;%0A
+import static org.jclouds.compute.options.TemplateOptions.Builder.overrideLoginCredentials;%0A
 import s
@@ -1721,16 +1721,80 @@
 nScript;
+%0Aimport static org.jclouds.scriptbuilder.domain.Statements.exec;
 %0A%0Aimport
@@ -3002,30 +3002,20 @@
 d9bc%22);%0A
-%0A%09            
+%09%09%09%09
 Statemen
@@ -3100,35 +3100,159 @@
 ons(
-runScript(bootInstructions)
+EC2TemplateOptions.Builder.inboundPorts(22, 80, 8080).runScript(bootInstructions));%0A%09            LoginCredentials login =  getLoginForCommandExecution(
 );%0A%0A
@@ -3692,93 +3692,176 @@
 ));%0A
-%0A
 %09%09%09%09
-getLog().info(String.format(%22Credential: %25s%5Cn%22, node.getCredentials().credential
+compute.runScriptOnNode(node.getId(), exec(%22yum install -y java-1.7.0-openjdk-devel%22), %0A%09%09%09%09%09%09overrideLoginCredentials(login).runAsRoot(true).wrapInInitScript(false
 ));%0A

Add security group for http ports and install for openjdk - needs unencrypted private ssh key in ~/.ssh/id_rsa. Next step ssh-agent