Skip to content

Instantly share code, notes, and snippets.

@AlBaker
Created December 22, 2013 17:57
Show Gist options
  • Save AlBaker/8086125 to your computer and use it in GitHub Desktop.
Save AlBaker/8086125 to your computer and use it in GitHub Desktop.
Pallet Java Example Project
(ns pallet-java-example.main
(:use [clojure.tools.cli :only (cli)]
[clojure.pprint])
(:gen-class :main true)
(:require
[pallet.api :refer [group-spec server-spec node-spec plan-fn converge lift]]
[pallet.compute :refer [nodes images]]
[clojure.pprint :refer [pprint]]
[pallet.compute.vmfest :refer [add-image]]
[pallet.node :refer [group-name primary-ip]]
[pallet.phase :as pphase]
[pallet.actions :as pact]
[clojure.java.io :as io]
[pallet-java-example.groups.pallet-java-example :as pvm]
[clojure.java.shell :as csh]))
;; Simple example showing creatin of a compute service provider, via vmfest
;; and the converge of it
;; Note you'll have to shut down the VM manually in this example
(defn -main [& args]
(do
(pvm/init-vmfest)
(when-not (contains? (images (:vmfest @pvm/provider)) :ubuntu-13.04-64bit)
(println "Adding default ubuntu VM")
(add-image (:vmfest @pvm/provider) "https://s3.amazonaws.com/vmfest-images/ubuntu-13.04-64bit.vdi.gz"))
(println "Spinning up example-group virtual machine")
(converge {pvm/example-group 1} :compute (:vmfest @pvm/provider))
(println "Completed VM creation")
(println (nodes (:vmfest @pvm/provider)))
(System/exit 0)))
(ns pallet-java-example.groups.pallet-java-example
"Node defintions for pallet-java-example"
(:require
[pallet.compute :refer [instantiate-provider]]
[pallet.api :refer [group-spec server-spec node-spec plan-fn converge lift]]
[pallet.compute :refer [images nodes]]
[pallet.configure :refer [compute-service]]
[pallet.crate.java :as java]
[pallet.crate.automated-admin-user :refer [automated-admin-user]]
[pallet.actions :as pact]
[pallet.compute.vmfest :as vmf]))
;; A refer to hold the provider
;; There are several ways to configure providers, such as a local .pallet/config.clj file
;; Here is an example where we can be very explicit about what we want, and even develop
;; a solution that changes the cloud provider to suit our needs - e.g. some code goes to EC2,
;; sometimes we use VMFest (Oracle VirtualBox) for development, etc
(def provider (ref {}))
;; the pallet compute provider, such as EC2, VMFest, Docker, etc can be manipulated
;; programmatically, as done here, or configured centrally such as via a local ~/.pallet/config.clj file
;; there is also a lein plugin for working with them as lein tasks
(defn init-vmfest []
"Initializes the vmfest cloud provider"
(dosync
(ref-set provider
(hash-map :vmfest
(instantiate-provider "vmfest" :vbox-comm :ws)))))
;; Note image specification, such as RAM amount can be specified here
(def default-node-spec
(node-spec
:image {:image-id :ubuntu-13.04-64bit}
:hardware {:min-cores 1}))
;; here is where the automated-admin-user, a representation of your current
;; user ID executing the program, and SSH key used to bootstrap the target VM
;; this enables seamless ssh to the VM
(def
^{:doc "Defines the type of node will run on"}
base-server
(server-spec
:phases
{:bootstrap (plan-fn (automated-admin-user))}))
;; typically a server spec is where all of the application specific configuration
;; will take place, such as moving files, unzipping them, etc
(def
^{:doc "Define a server spec"}
example-server
(server-spec
:phases
{:configure (plan-fn
(pact/file "/tmp/phase-configure-basenode" :owner "vmfest", :group "vmfest", :mode "0644")
;; add more functions here, usually using things out of the pallet.actions namespace
)}))
;; The group is what is used by converge or lift to spin up VMs and move configuration on to them
;; Each of servers will be executed in the order of phases (bootstrap, configure, install)
;; So per the above, base-server has a bootstrap with automated-admin-user which will be done first
;; next, the java server-spec will execute during the install phase
;; lastly, the example-server configure phase will create the remote file, finishing up the converge
(def
^{:doc "Defines a example group spec that can be passed to converge or lift."}
example-group
(group-spec
"example-group"
:extends [base-server example-server (java/server-spec {:vendor :oracle :version "7"})]
:node-spec default-node-spec))
(defproject pallet-java-example "0.1.0-SNAPSHOT"
:description "Example Pallet project for show casing how to get a VM with Java up and running"
:dependencies [[org.clojure/clojure "1.5.1"]
[com.palletops/pallet "0.8.0-RC.3"]
[com.palletops/pallet-vmfest "0.3.0-beta.2"]
[com.palletops/java-crate "0.8.0-beta.5"]
[org.clojars.tbatchelli/vboxjws "4.2.4"]
[org.slf4j/slf4j-api "1.6.1"]
[ch.qos.logback/logback-core "1.0.0"]
[ch.qos.logback/logback-classic "1.0.0"]]
:profiles {:dev
{:dependencies
[[com.palletops/pallet "0.8.0-RC.3"
:classifier "tests"]]
:plugins
[[com.palletops/pallet-lein "0.8.0-alpha.1"]]}
:leiningen/reply
{:dependencies [[org.slf4j/jcl-over-slf4j "1.7.2"]]
:exclusions [commons-logging]}}
:local-repo-classpath true
:main pallet-java-example.main
:repositories
{"sonatype" "https://oss.sonatype.org/content/repositories/releases/"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment