Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fletcher Nichol committed Aug 14, 2011
0 parents commit 8247781
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 0 deletions.
149 changes: 149 additions & 0 deletions README.md
@@ -0,0 +1,149 @@
# Description

LWRP to manage [homesick][homesick] castles.

# Requirements

## Chef

Tested on 0.10.2 and 0.10.4 but newer and older version should work just fine.
File an [issue][issues] if this isn't the case.

## Platform

The following platforms have been tested with this cookbook, meaning that the
recipes run on these platforms without error:

* ubuntu
* debian
* mac_os_x
* suse
* openbsd

Please [report][issues] any additional platforms so they can be added.

## Cookbooks

There are **no** external cookbook dependencies. If you are using [RVM][rvm],
then you should consider using the [rvm cookbook][rvm_cb].

# Usage

Simply include `recipe[homesick]` in your run_list and the `homesick_repo`
resource will be available.

To use `recipe[homesick::data_bag]`, include it in your run_list and have a
data bag called `"users"` with an item for your user like the following:

{
"id" : "wigglebottom",
"homesick_castles" : [
{ "name" : "dotvim",
"source" : "git://github.com/fnichol/dotvim.git"
},
{ "name" : "dotfiles",
"source" : "git://github.com/fnichol/dotfiles.git",
"action" : "update"
}
]
}

The data bag recipe will iterate through a list of usernames defined in
`node['users']` and attempt to pull in the homesick information from the data
bag item. In other words, having:

node['users'] = ['hsolo']

will set up the `hsolo` user's castles and not use the `wigglebottom` user.

# Recipes

## default

This recipe is a no-op and does nothing.

Use this recipe when you only want access to the `homesick_castle` LWRP.

## data_bag

Fetches an list of homesick castles with data drawn from a data bag. The
default data bag is `"users"` and the list of user accounts to iterate through
is set on `node['users']`.

Use this recipe when you want data bag driven data in your workflow.

# Attributes

## `data_bag`

The data bag name containing a group of user account information. This is used
by the `data_bag` recipe to use as a database of user accounts. The default is
`"users"`.

# Resources and Providers

## homesick_castle

### Actions

Action |Description |Default
----------|------------------------------|-------
install |Clones the castle and symlinks it. |Yes
update |Pulls updates for the castle and re-symlinks it. |

### Attributes

Attribute |Description |Default value
------------|------------|-------------
name |**Name attribute:** The name of the homesick castle. |`nil`
source |The clone URL (http, https, git, etc.) of the castle. |`nil`

### Examples

#### Install a Castle

homesick_castle 'dotvim' do
source 'git://github.com/fnichol/dotvim.git'
end

**Note:** the install action is the default.

#### Pull Updates for a Castle

homesick_castle 'dotfiles' do
source 'git://github.com/fnichol/dotfiles.git'
action :update
end

# Development

* Source hosted at [GitHub][repo]
* Report issues/Questions/Feature requests on [GitHub Issues][issues]

Pull requests are very welcome! Make sure your patches are well tested.
Ideally create a topic branch for every seperate change you make.

# License and Author

Author:: Fletcher Nichol (<fnichol@nichol.ca>)

Copyright 2011, Fletcher Nichol

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

[homesick]: https://github.com/technicalpickles/homesick
[rvm]: http://rvm.beginrescueend.com
[rvm_cb]: https://github.com/fnichol/chef-rvm

[repo]: https://github.com/fnichol/chef-homesick
[issues]: https://github.com/fnichol/chef-homesick/issues
22 changes: 22 additions & 0 deletions attributes/default.rb
@@ -0,0 +1,22 @@
#
# Cookbook Name:: homesick
# Attributes:: default
#
# Author:: Fletcher Nichol <fnichol@nichol.ca>
#
# Copyright 2011, Fletcher Nichol
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

default['homesick']['data_bag'] = "users"
15 changes: 15 additions & 0 deletions metadata.rb
@@ -0,0 +1,15 @@
maintainer "Fletcher Nichol"
maintainer_email "fnichol@nichol.ca"
license "Apache 2.0"
description "Installs/Configures homesick"
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version "0.0.1"

supports "ubuntu"
supports "debian"
supports "mac_os_x"
supports "openbsd"
supports "suse"

recipe "homesick", "Processes a list of homesick castles (which is empty by default)."
recipe "homesick::data_bag", "Fetches a list of homesick castles from a data bag item and appends it to the `node['homesick']['castles']` attribute for processing."
82 changes: 82 additions & 0 deletions providers/castle.rb
@@ -0,0 +1,82 @@
#
# Cookbook Name:: homesick
# Provider:: castle
#
# Copyright 2011, Fletcher Nichol
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'pathname'

def load_current_resource
castle = castles.select{ |c| c[:name] == new_resource.castle }.first
if castle
@current_resource = Chef::Resource::HomesickCastle.new(castle[:name])
@current_resource.source = castle[:source]
else
@current_resource = nil
end
end

action :install do
if !@current_resource # castle is not installed
clone
update
end
end

action :update do
clone if !@current_resource # castle is not installed
update
end

private

def castles
Pathname.glob("#{repos_dir}/**/*/.git").map do |c|
castle = c.dirname
source = ""
Dir.chdir castle do
source = `git config remote.origin.url`.chomp
end

{ :name => castle.relative_path_from(repos_dir).to_s,
:source => source
}
end
end

def repos_dir
@repos_dir ||= home_dir.join('.homesick', 'repos').expand_path
end

def home_dir
@home_dir ||= Pathname.new(Etc.getpwnam(new_resource.user).dir).expand_path
end

def clone
execute "homesick clone #{new_resource.source} --force" do
user new_resource.user
end
end

def update
execute "homesick pull #{new_resource.castle} --force" do
user new_resource.user
end

execute "homesick symlink #{new_resource.castle} --force" do
user new_resource.user
end
end
40 changes: 40 additions & 0 deletions recipes/data_bag.rb
@@ -0,0 +1,40 @@
#
# Cookbook Name:: homesick
# Recipe:: data_bag
#
# Copyright 2011, Fletcher Nichol
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

bag = node['homesick']['data_bag']
users = begin
data_bag(bag)
rescue => ex
Chef::Log.info("Data bag #{bag.join('/')} not found (#{ex}), so skipping")
[]
end

include_recipe 'homesick'

Array(node['users']).each do |i|
u = data_bag_item(bag, i)

Array(u['homesick_castles']).each do |castle|
homesick_castle castle['name'] do
user u['id']
source castle['source'] if castle['source']
action castle['action'].to_sym if castle['action']
end
end
end
27 changes: 27 additions & 0 deletions recipes/default.rb
@@ -0,0 +1,27 @@
#
# Cookbook Name:: homesick
# Recipe:: default
#
# Copyright 2011, Fletcher Nichol
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# install homesick gem during chef compile phase
gem_package 'homesick' do
action :nothing
end.run_action(:install)

require 'rubygems'
Gem.clear_paths
require 'homesick'
29 changes: 29 additions & 0 deletions resources/castle.rb
@@ -0,0 +1,29 @@
#
# Cookbook Name:: homesick
# Resource:: castle
#
# Copyright 2011, Fletcher Nichol
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

actions :install, :update

attribute :castle, :kind_of => String, :name_attribute => true
attribute :source, :kind_of => String, :required => true
attribute :user, :kind_of => String, :required => true

def initialize(*args)
super
@action = :install
end

0 comments on commit 8247781

Please sign in to comment.