Last active
December 27, 2015 11:19
-
-
Save nacx/7317938 to your computer and use it in GitHub Desktop.
Install a Chef Server using ChefSolo and jclouds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.abiquo.jcloudstest; | |
| import static com.google.common.collect.Iterables.getOnlyElement; | |
| import static org.jclouds.compute.options.RunScriptOptions.Builder.overrideAuthenticateSudo; | |
| import static org.jclouds.compute.options.TemplateOptions.Builder.inboundPorts; | |
| import org.jclouds.ContextBuilder; | |
| import org.jclouds.View; | |
| import org.jclouds.aws.ec2.AWSEC2Api; | |
| import org.jclouds.aws.ec2.domain.AWSRunningInstance; | |
| import org.jclouds.compute.ComputeService; | |
| import org.jclouds.compute.ComputeServiceContext; | |
| import org.jclouds.compute.domain.NodeMetadata; | |
| import org.jclouds.compute.domain.Template; | |
| import org.jclouds.ec2.compute.domain.EC2HardwareBuilder; | |
| import org.jclouds.logging.slf4j.config.SLF4JLoggingModule; | |
| import org.jclouds.scriptbuilder.ExitInsteadOfReturn; | |
| import org.jclouds.scriptbuilder.domain.Statement; | |
| import org.jclouds.scriptbuilder.domain.StatementList; | |
| import org.jclouds.scriptbuilder.domain.chef.RunList; | |
| import org.jclouds.scriptbuilder.statements.chef.ChefSolo; | |
| import org.jclouds.scriptbuilder.statements.chef.InstallChefUsingOmnibus; | |
| import org.jclouds.scriptbuilder.statements.git.CloneGitRepo; | |
| import org.jclouds.scriptbuilder.statements.git.InstallGit; | |
| import org.jclouds.scriptbuilder.statements.login.AdminAccess; | |
| import org.jclouds.sshj.config.SshjSshClientModule; | |
| import com.google.common.collect.ImmutableSet; | |
| public class DeployChef | |
| { | |
| public static void main(final String[] args) throws Exception | |
| { | |
| // CONFIGURATION | |
| String chefVersion = "11.8.0-1"; // Chef Server version to install | |
| String imageId = "us-east-1/ami-a73264ce"; // Ubuntu 12.04.3 LTS (64-bit) | |
| int[] openPorts = new int[] {22, 80, 443}; // Ports to open in the node | |
| String identity = "<change me>"; | |
| String credential = "<change me>"; | |
| // END CONFIGURATION | |
| ComputeServiceContext context = | |
| ContextBuilder.newBuilder("aws-ec2").credentials(identity, credential) | |
| .modules(ImmutableSet.of(new SLF4JLoggingModule(), new SshjSshClientModule())) | |
| .buildView(ComputeServiceContext.class); | |
| ComputeService compute = context.getComputeService(); | |
| try | |
| { | |
| System.out.println("Looking for the template..."); | |
| Template template = compute.templateBuilder() // | |
| .fromHardware(EC2HardwareBuilder.t1_micro().build()) // Deploy the smallest template | |
| .imageId(imageId) // | |
| .options(inboundPorts(openPorts)) // | |
| .build(); | |
| System.out.println("Provisioning node..."); | |
| NodeMetadata node = | |
| getOnlyElement(compute.createNodesInGroup("chef-server", 1, template)); | |
| String publicDns = findPublicDns(node, context); | |
| System.out.println("Bootstrapping..."); | |
| Statement bootstrap = installAndConfigureChef(chefVersion, publicDns); | |
| compute.runScriptOnNode(node.getId(), bootstrap, overrideAuthenticateSudo(true)); | |
| System.out.println("Done! Access your node at: " + publicDns); | |
| } | |
| finally | |
| { | |
| context.close(); | |
| } | |
| } | |
| private static Statement installAndConfigureChef(final String chefServerVersion, | |
| final String nodeFqdn) | |
| { | |
| Statement cloneCookbooks = CloneGitRepo.builder() // | |
| .repository("https://github.com/opscode-cookbooks/chef-server.git") // | |
| .directory("/var/chef/cookbooks/chef-server") // | |
| .build(); | |
| String attributes = | |
| String.format("{\"chef-server\":{\"chef-version\":\"%s\",\"api_fqdn\":\"%s\"}}", | |
| chefServerVersion, nodeFqdn); | |
| Statement chefSolo = ChefSolo.builder() // | |
| .cookbookPath("/var/chef/cookbooks") // | |
| .runlist(RunList.builder().recipe("chef-server").build()) // | |
| .jsonAttributes(attributes) // | |
| .build(); | |
| return new StatementList(AdminAccess.standard(), | |
| new ExitInsteadOfReturn(new InstallGit()), | |
| cloneCookbooks, | |
| new InstallChefUsingOmnibus(), | |
| chefSolo); | |
| } | |
| private static String findPublicDns(final NodeMetadata node, final View view) | |
| { | |
| AWSEC2Api aws = view.unwrapApi(AWSEC2Api.class); | |
| AWSRunningInstance instance = | |
| getOnlyElement(getOnlyElement(aws.getInstanceApi().get() | |
| .describeInstancesInRegion(null, node.getProviderId()))); | |
| return instance.getDnsName(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| <modelVersion>4.0.0</modelVersion> | |
| <groupId>com.abiquo</groupId> | |
| <artifactId>deploy-chef</artifactId> | |
| <version>0.1-SNAPSHOT</version> | |
| <properties> | |
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
| <jclouds.version>1.7.0-SNAPSHOT</jclouds.version> | |
| </properties> | |
| <dependencies> | |
| <dependency> | |
| <groupId>org.apache.jclouds.provider</groupId> | |
| <artifactId>aws-ec2</artifactId> | |
| <version>${jclouds.version}</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.jclouds.driver</groupId> | |
| <artifactId>jclouds-slf4j</artifactId> | |
| <version>${jclouds.version}</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.jclouds.driver</groupId> | |
| <artifactId>jclouds-sshj</artifactId> | |
| <version>${jclouds.version}</version> | |
| </dependency> | |
| </dependencies> | |
| </project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment