Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kohkimakimoto committed Feb 27, 2014
0 parents commit 2dd41fa
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/vendor
/tmp
/composer.phar
/composer.lock
/.project
/.altax
/build
.DS_Store
Thumbs.db
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Altax chef plugin

Runs chef-solo via [altax](https://github.com/kohkimakimoto/altax) and Git.

> NOTE: This product is in development stag, So I sometimes break code.
## Requirement

Depends on my personal environment.

* CentOS6
* PHP5.4

## Installation

Edit your `.altax/composer.json` file like the following.

{
"require": {
"kohkimakimoto/altax-chef": "dev-master"
}
}

Run altax update.

$ altax update

Add the following code your `.altax/config.php` file.

Task::register("chef", 'Altax\Contrib\Chef\Command\ChefCommand')
->config(array(
"repo" => "git repository path to store chef repository"
))
;

## Usage

See

altax chef -h


24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "kohkimakimoto/altax-chef",
"description": "Runs chef-solo via altax.",
"keywords": ["altax", "plugin", "chef"],
"homepage": "https://github.com/kohkimakimoto/altax-chef",
"license": "Apache License 2.0",
"authors": [
{
"name": "Kohki Makimoto",
"email": "kohki.makimoto@gmail.com"
}
],
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"kohkimakimoto/altax": "3.*",
"phpunit/phpunit": "3.*"
},
"minimum-stability": "alpha",
"autoload": {
"psr-0": { "Altax\\Contrib\\Chef\\": "src/"}
}
}
8 changes: 8 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<testsuites>
<testsuite name="Unit tests">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
</phpunit>
127 changes: 127 additions & 0 deletions src/Altax/Contrib/Chef/Command/ChefCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
namespace Altax\Contrib\Chef\Command;

use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Finder\Finder;

class ChefCommand extends \Altax\Command\Command
{
public $chefRpm = "https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-11.8.2-1.el6.x86_64.rpm";

protected function configure()
{
$this
->setDescription("Runs chef-solo via altax.")
->addArgument(
'target',
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
'Target nodes or roles to run chef-solo.'
)
->addOption(
'berks',
null,
InputOption::VALUE_NONE,
'Runs berkself even if exists cookbooks.'
)
->addOption(
'no-solo',
null,
InputOption::VALUE_NONE,
'Do not run chef-solo.'
)
->addOption(
'prepare',
null,
InputOption::VALUE_NONE,
'Prepare chef-solo (installing chef and something).'
)
;
}

protected function fire($task)
{
$input = $task->getInput();
$output = $task->getOutput();

$config = $this->getTaskConfig();
if (!isset($config["repo"])) {
throw new \RuntimeException("You must set config key 'repo'.");
}

$repo = $config["repo"];
$dir = isset($config["dir"]) ? $config["dir"] : "/var/chef";
$berks = isset($config["berks"]) ? $config["berks"] : "/opt/chef/embedded/bin/berks";

$target = $input->getArgument("target");
$runBerks = $input->getOption("berks");
$noSolo = $input->getOption("no-solo");
$prepare = $input->getOption("prepare");

if ($prepare) {
// Prepare
$chefRpm = $this->chefRpm;

$task->exec(function($process) use ($chefRpm, $dir, $repo, $berks, $runBerks, $noSolo) {

// Install git
$process->run("yum install -y git", arary("user" => "root"));
// Install chef
$process->run("rpm -ivh $chefRpm", arary("user" => "root"));
// Install berkself gem
$process->run("/opt/chef/embedded/bin/gem install berkshelf --no-rdoc --no-ri", arary("user" => "root"));

}, $target);

} else {
// Run chef-solo

$task->exec(function($process) use ($dir, $repo, $berks, $runBerks, $noSolo) {

$node = $process->getNode();

// Get chef repository
$ret = null;
if ($process->run("test -d $dir")->isFailed()) {
$ret = $process->run("git clone $repo $dir");
} else {
$ret = $process->run("git pull", array("cwd" => $dir));
}

if ($ret->isFailed()) {
throw new \RuntimeException("Got a error.");
}

// Check existing the cookbooks directory
if ($process->run("test -d $dir/cookbooks")->isFailed()) {
$runBerks = true;
}

if ($runBerks) {
// Run berkself
// "unset" Prevent to load system wide ruby and gems.
$process->run(array(
"unset GEM_HOME && unset GEM_PATH",
"cd $dir",
"$berks --path cookbooks"
), array("user" => "root"));

}

if (!$noSolo) {
// Run chef-solo
$nodeName = $node->getName();
$process->run(array(
"unset GEM_HOME && unset GEM_PATH",
"cd $dir",
"chef-solo -c $dir/config/solo.rb -j $dir/nodes/${nodeName}.json"
), array("user" => "root"));
}

}, $target);

}


}
}

0 comments on commit 2dd41fa

Please sign in to comment.