Skip to content

Commit

Permalink
Initial import to GH
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Mar 5, 2013
0 parents commit a991fb3
Show file tree
Hide file tree
Showing 28 changed files with 1,052 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
*.gem
*.rbc
.bundle
.config
.vagrant
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
Vagrantfile
10 changes: 10 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
source 'https://rubygems.org'

gemspec

group :development do
# We depend on Vagrant for development, but we don't add it as a
# gem dependency because we expect to be installed within the
# Vagrant environment itself using `vagrant plugin`.
gem "vagrant", :git => "git://github.com/mitchellh/vagrant.git"
end
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2013 Mitchell Hashimoto

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
146 changes: 146 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Vagrant RackSpace Cloud Provider

This is a [Vagrant](http://www.vagrantup.com) 1.1+ plugin that adds a
[RackSpace Cloud](http://www.rackspace.com/cloud) provider to Vagrant,
allowing Vagrant to control and provision machines within RackSpace
cloud.

## Features

* Boot Rackspace Cloud instances.
* SSH into the instances.
* Provision the instances with any built-in Vagrant provisioner.
* Minimal synced folder support via `rsync`.

## Usage

Install using standard Vagrant 1.1+ plugin installation methods. After
installing, `vagrant up` and specify the `rackspace` provider. An example is
shown below.

```
$ vagrant plugin install vagrant-rackspace
...
$ vagrant up --provider=rackspace
...
```

Of course prior to doing this, you'll need to obtain an Rackspace-compatible
box file for Vagrant.

## Quick Start

After installing the plugin (instructions above), the quickest way to get
started is to actually use a dummy Rackspace box and specify all the details
manually within a `config.vm.provider` block. So first, add the dummy
box using any name you want:

```
$ vagrant box add dummy https://github.com/mitchellh/vagrant-rackspace/raw/master/dummy.box
...
```

And then make a Vagrantfile that looks like the following, filling in
your information where necessary.

```
Vagrant.configure("2") do |config|
config.vm.box = "dummy"
config.vm.provider :rackspace do |rs|
rs.username = "YOUR USERNAME"
rs.api_key = "YOUR API KEY"
rs.flavor = /512MB/
rs.image = /Ubuntu/
end
end
```

And then run `vagrant up --provider=rackspace`.

This will start an Ubuntu 12.04 instance in the DFW datacenter region within
your account. And assuming your SSH information was filled in properly
within your Vagrantfile, SSH and provisioning will work as well.

Note that normally a lot of this boilerplate is encoded within the box
file, but the box file used for the quick start, the "dummy" box, has
no preconfigured defaults.

## Box Format

Every provider in Vagrant must introduce a custom box format. This
provider introduces `rackspace` boxes. You can view an example box in
the [example_box/ directory](https://github.com/mitchellh/vagrant-rackspace/tree/master/example_box).
That directory also contains instructions on how to build a box.

The box format is basically just the required `metadata.json` file
along with a `Vagrantfile` that does default settings for the
provider-specific configuration for this provider.

## Configuration

This provider exposes quite a few provider-specific configuration options:

* `api_key` - The API key for accessing Rackspace.
* `flavor` - The server flavor to boot. This can be a string matching
the exact ID or name of the server, or this can be a regular expression
to partially match some server flavor.
* `image` - The server image to boot. This can be a string matching the
exact ID or name of the image, or this can be a regular expression to
partially match some image.
* `endpoint` - The endpoint to hit. By default this is DFW.
* `username` - The username with which to access Rackspace.

These can be set like typical provider-specific configuration:

```ruby
Vagrant.configure("2") do |config|
# ... other stuff

config.vm.provider :rackspace do |rs|
rs.username = "mitchellh"
rs.api_key = "foobarbaz"
end
end
```

## Networks

Networking features in the form of `config.vm.network` are not
supported with `vagrant-rackspace`, currently. If any of these are
specified, Vagrant will emit a warning, but will otherwise boot
the Rackspace server.

## Synced Folders

There is minimal support for synced folders. Upon `vagrant up`,
`vagrant reload`, and `vagrant provision`, the Rackspace provider will use
`rsync` (if available) to uni-directionally sync the folder to
the remote machine over SSH.

This is good enough for all built-in Vagrant provisioners (shell,
chef, and puppet) to work!

## Development

To work on the `vagrant-rackspace` plugin, clone this repository out, and use
[Bundler](http://gembundler.com) to get the dependencies:

```
$ bundle
```

Once you have the dependencies, verify the unit tests pass with `rake`:

```
$ bundle exec rake
```

If those pass, you're ready to start developing the plugin. You can test
the plugin without installing it into your Vagrant environment by just
creating a `Vagrantfile` in the top level of this directory (it is gitignored)
that uses it, and uses bundler to execute Vagrant:

```
$ bundle exec vagrant up --provider=rackspace
```
21 changes: 21 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'rubygems'
require 'bundler/setup'
require 'rspec/core/rake_task'

# Immediately sync all stdout so that tools like buildbot can
# immediately load in the output.
$stdout.sync = true
$stderr.sync = true

# Change to the directory of this file.
Dir.chdir(File.expand_path("../", __FILE__))

# This installs the tasks that help with gem creation and
# publishing.
Bundler::GemHelper.install_tasks

# Install the `spec` task so that we can run tests.
RSpec::Core::RakeTask.new

# Default task is to run the unit tests
task :default => "spec"
Binary file added dummy.box
Binary file not shown.
13 changes: 13 additions & 0 deletions example_box/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Vagrant RackSpace Cloud Example Box

Vagrant providers each require a custom provider-specific box format.
This folder shows the example contents of a box for the `rackspace` provider.
To turn this into a box:

```
$ tar cvzf rackspace.box ./metadata.json ./Vagrantfile
```

This box works by using Vagrant's built-in Vagrantfile merging to setup
defaults for RackSpace. These defaults can easily be overwritten by higher-level
Vagrantfiles (such as project root Vagrantfiles).
3 changes: 3 additions & 0 deletions example_box/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"provider": "rackspace"
}
53 changes: 53 additions & 0 deletions lib/vagrant-rackspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require "pathname"

require "vagrant-rackspace/plugin"

module VagrantPlugins
module Rackspace
lib_path = Pathname.new(File.expand_path("../vagrant-rackspace", __FILE__))
autoload :Errors, lib_path.join("errors")

# This initializes the i18n load path so that the plugin-specific
# translations work.
def self.init_i18n
I18n.load_path << File.expand_path("locales/en.yml", source_root)
I18n.reload!
end

# This initializes the logging so that our logs are outputted at
# the same level as Vagrant core logs.
def self.init_logging
# Initialize logging
level = nil
begin
level = Log4r.const_get(ENV["VAGRANT_LOG"].upcase)
rescue NameError
# This means that the logging constant wasn't found,
# which is fine. We just keep `level` as `nil`. But
# we tell the user.
level = nil
end

# Some constants, such as "true" resolve to booleans, so the
# above error checking doesn't catch it. This will check to make
# sure that the log level is an integer, as Log4r requires.
level = nil if !level.is_a?(Integer)

# Set the logging level on all "vagrant" namespaced
# logs as long as we have a valid level.
if level
logger = Log4r::Logger.new("vagrant_rackspace")
logger.outputters = Log4r::Outputter.stderr
logger.level = level
logger = nil
end
end

# This returns the path to the source of this plugin.
#
# @return [Pathname]
def self.source_root
@source_root ||= Pathname.new(File.expand_path("../../", __FILE__))
end
end
end
95 changes: 95 additions & 0 deletions lib/vagrant-rackspace/action.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require "pathname"

require "vagrant/action/builder"

module VagrantPlugins
module Rackspace
module Action
# Include the built-in modules so we can use them as top-level things.
include Vagrant::Action::Builtin

# This action is called to destroy the remote machine.
def self.action_destroy
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use Call, IsCreated do |env, b2|
if !env[:result]
b2.use MessageNotCreated
next
end

b2.use ConnectRackspace
b2.use DeleteServer
end
end
end

# This action is called to read the SSH info of the machine. The
# resulting state is expected to be put into the `:machine_ssh_info`
# key.
def self.action_read_ssh_info
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use ConnectRackspace
b.use ReadSSHInfo
end
end

# This action is called to read the state of the machine. The
# resulting state is expected to be put into the `:machine_state_id`
# key.
def self.action_read_state
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use ConnectRackspace
b.use ReadState
end
end

def self.action_ssh
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use Call, IsCreated do |env, b2|
if !env[:result]
b2.use MessageNotCreated
next
end

b2.use SSHExec
end
end
end

def self.action_up
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use Call, IsCreated do |env, b2|
if env[:result]
b2.use MessageAlreadyCreated
next
end

b2.use ConnectRackspace
b2.use Provision
b2.use SyncFolders
b2.use WarnNetworks
b2.use CreateServer
end
end
end

# The autoload farm
action_root = Pathname.new(File.expand_path("../action", __FILE__))
autoload :ConnectRackspace, action_root.join("connect_rackspace")
autoload :CreateServer, action_root.join("create_server")
autoload :DeleteServer, action_root.join("delete_server")
autoload :IsCreated, action_root.join("is_created")
autoload :MessageAlreadyCreated, action_root.join("message_already_created")
autoload :MessageNotCreated, action_root.join("message_not_created")
autoload :ReadSSHInfo, action_root.join("read_ssh_info")
autoload :ReadState, action_root.join("read_state")
autoload :SyncFolders, action_root.join("sync_folders")
autoload :WarnNetworks, action_root.join("warn_networks")
end
end
end
Loading

0 comments on commit a991fb3

Please sign in to comment.