Linecook - A Chef Alternative

84
Linecook (A Chef Alternative) Simon Chiang Tuesday, April 26, 2011

description

Slides from my talk at DeRailed in Denver on April 26, 2011

Transcript of Linecook - A Chef Alternative

Page 1: Linecook - A Chef Alternative

Linecook(A Chef Alternative)

Simon Chiang

Tuesday, April 26, 2011

Page 2: Linecook - A Chef Alternative

Chef

Tuesday, April 26, 2011

Page 3: Linecook - A Chef Alternative

ChefRuby

Tuesday, April 26, 2011

Page 4: Linecook - A Chef Alternative

ChefRuby

Opscode

Tuesday, April 26, 2011

Page 5: Linecook - A Chef Alternative

ChefRuby

Opscode“Systems Integration

Framework”

Tuesday, April 26, 2011

Page 6: Linecook - A Chef Alternative

ChefRuby

Opscode“Systems Integration

Framework”

Open Source

Tuesday, April 26, 2011

Page 7: Linecook - A Chef Alternative

ChefRuby

Opscode“Systems Integration

Framework”

Automated

Open Source

Tuesday, April 26, 2011

Page 8: Linecook - A Chef Alternative

ChefRuby

Opscode“Systems Integration

Framework”

AutomatedScalable

Open Source

Tuesday, April 26, 2011

Page 9: Linecook - A Chef Alternative

ChefRuby

Opscode“Systems Integration

Framework”

AutomatedScalable

Open SourceServer Provisioning

Tuesday, April 26, 2011

Page 10: Linecook - A Chef Alternative

ChefRuby

Opscode“Systems Integration

Framework”

AutomatedScalable

Open Source

Community

Server Provisioning

Tuesday, April 26, 2011

Page 11: Linecook - A Chef Alternative

ChefRuby

Opscode“Systems Integration

Framework”

AutomatedScalable

Hotness

Open Source

Community

Server Provisioning

Tuesday, April 26, 2011

Page 12: Linecook - A Chef Alternative

An alternative toshell scripts

Tuesday, April 26, 2011

Not dismissing Chef when I say that. The advantages over manually writing setup/maintenance script are not to be underestimated. But for the most part things you do with Chef are things you would otherwise do with shell scripts, and there are problems.

Page 13: Linecook - A Chef Alternative

Never a quickQuickstart

“Before installing Chef, you should take a moment to understand the various "flavors" of chef: client-server, chef-solo, and the Opscode Platform. Deciding which one is right for you will impact your installation process. You may also want to take a quick look at Chef's architecture to get an idea of what you're installing before you proceed.”

http://wiki.opscode.com/display/chef/Installation

Tuesday, April 26, 2011

Page 14: Linecook - A Chef Alternative

Not a script,but sort of...

[lib/chef/provider/package/zypper.rb] @ 0.10.0.rc.0

def install_package(name, version) if version run_command( :command => "zypper -n --no-gpg-checks install -l #{name}=#{version}" )

Tuesday, April 26, 2011

Page 15: Linecook - A Chef Alternative

Not a script,but sort of...

[lib/chef/provider/package/zypper.rb] @ 0.10.0.rc.0

def install_package(name, version) if version run_command( :command => "zypper -n --no-gpg-checks install -l #{name}=#{version}" )

Fixed options :(

Tuesday, April 26, 2011

Page 16: Linecook - A Chef Alternative

Not a script,but sort of...

[lib/chef/provider/package/zypper.rb] @ 0.10.0.rc.0

def install_package(name, version) if version run_command( :command => "zypper -n --no-gpg-checks install -l #{name}=#{version}" )

https://github.com/opscode/chef/pull/27

Error for version=""

Fixed options :(

Tuesday, April 26, 2011

Page 17: Linecook - A Chef Alternative

Many moving parts

http://wiki.opscode.com/display/chef/Architecture

Tuesday, April 26, 2011

Page 18: Linecook - A Chef Alternative

Many moving parts

http://wiki.opscode.com/display/chef/Vagrant

http://wiki.opscode.com/display/chef/Architecture

Tuesday, April 26, 2011

Page 19: Linecook - A Chef Alternative

Many moving parts

http://wiki.opscode.com/display/chef/Vagrant

http://wiki.opscode.com/display/chef/Architecture

http://community.opscode.com/cookbooks

Tuesday, April 26, 2011

Page 20: Linecook - A Chef Alternative

Alternative?

Tuesday, April 26, 2011

Page 21: Linecook - A Chef Alternative

Shell Scripts!

Tuesday, April 26, 2011

Page 22: Linecook - A Chef Alternative

LinecookA Shell Script Generator

Tuesday, April 26, 2011

Page 23: Linecook - A Chef Alternative

Established Tools(bash, ssh, gems, VirtualBox)

Tuesday, April 26, 2011

Just use the existing systems. Some ‘advanced’ techniques but all established, known systemsPrior experience applies any learning will feed back into existing workflow

Page 24: Linecook - A Chef Alternative

Start with a ScriptEnd with a Script

Tuesday, April 26, 2011

Able to bring an existing script to Linecook, then rework to make it more powerful or maintainable. You will work with scripts directly. Again all prior experience applies.

Page 25: Linecook - A Chef Alternative

Pretty quickQuickstart

Make a server

Make a script

Run script on server

Tuesday, April 26, 2011

Page 26: Linecook - A Chef Alternative

Pretty quickQuickstart

Make a server

Make a script

Run script on server

1

Tuesday, April 26, 2011

Page 27: Linecook - A Chef Alternative

Pretty quickQuickstart

Make a server

Make a script

Run script on server

1

2

Tuesday, April 26, 2011

Page 28: Linecook - A Chef Alternative

Pretty quickQuickstart

Make a server

Make a script

Run script on server

1

2

3

Tuesday, April 26, 2011

Page 29: Linecook - A Chef Alternative

An ERB Trick...

Tuesday, April 26, 2011

Page 30: Linecook - A Chef Alternative

How to Make a Server

VirtualBox

Install OS

SSH Key Exchange localhost

Tuesday, April 26, 2011

Page 31: Linecook - A Chef Alternative

How to Make a Server

VirtualBox

Install OS

SSH Key Exchange

VirtualBox

localhost

Tuesday, April 26, 2011

Page 32: Linecook - A Chef Alternative

How to Make a Server

VirtualBox

Install OS

SSH Key Exchange

VirtualBox

abox-ubuntu

localhost

Tuesday, April 26, 2011

Page 33: Linecook - A Chef Alternative

How to Make a Server

VirtualBox

Install OS

SSH Key Exchange

VirtualBox

abox-ubuntu

localhost

Tuesday, April 26, 2011

Page 34: Linecook - A Chef Alternative

abox-ubuntu

localhost

VirtualBox

Port Forwarding

Access VM by ssh to localhost

ssh -p 2220 linecook@localhost

VBoxManage modifyvm abox --natpf1 'abox-ssh,tcp,,

2220,,22'

Tuesday, April 26, 2011

Page 35: Linecook - A Chef Alternative

abox-ubuntu

localhost

bbox-ubuntu

Lather, rinse

Repeat as needed

ssh -p 2220 linecook@localhost

ssh -p 2221 linecook@localhost

...VBoxManage modifyvm bbox

--natpf1 'bbox-ssh,tcp,,2221,,22'

Tuesday, April 26, 2011

Page 36: Linecook - A Chef Alternative

Advantages

No special prerequisites on servers

Fast Access (fast enough for TDD)

Cmdline control

Multiple VMs

Snapshots

Tuesday, April 26, 2011

Page 37: Linecook - A Chef Alternative

How to Run Scripts

cat > script.sh <<"DOC"echo "# $(whoami)@$(hostname): hello world!"DOCchmod +x script.sh

scp -P 2220 script.sh linecook@localhost:/tmp/script.shssh -p 2220 linecook@localhost -- /tmp/script.sh# linecook@abox-ubuntu: hello world!

Tuesday, April 26, 2011

Page 38: Linecook - A Chef Alternative

Redundany

cat > script.sh <<"DOC"echo "# $(whoami)@$(hostname): hello world!"DOCchmod +x script.sh

scp -P 2220 script.sh linecook@localhost:/tmp/script.shssh -p 2220 linecook@localhost -- /tmp/script.sh# linecook@abox-ubuntu: hello world!

Tuesday, April 26, 2011

Page 39: Linecook - A Chef Alternative

Use SSH Configmkdir configcat > config/ssh <<"DOC" Host aboxPort 2220User linecookHostname localhostDOC

scp -F config/ssh script.sh abox:/tmp/script.shssh -F config/ssh abox -- /tmp/script.sh# linecook@abox-ubuntu: hello world!

Tuesday, April 26, 2011

Page 40: Linecook - A Chef Alternative

Multiple Hostscat > config/ssh <<"DOC"Host aboxPort 2220

Host bboxPort 2221

Host *User linecookHostname localhostDOC

scp -F config/ssh script.sh bbox:/tmp/script.shssh -F config/ssh bbox -- /tmp/script.sh# linecook@bbox-ubuntu: hello world!

Tuesday, April 26, 2011

Page 41: Linecook - A Chef Alternative

Redundancycat > config/ssh <<"DOC"Host aboxPort 2220

Host bboxPort 2221

Host *User linecookHostname localhostDOC

scp -F config/ssh script.sh bbox:/tmp/script.shssh -F config/ssh bbox -- /tmp/script.sh# linecook@bbox-ubuntu: hello world!

Tuesday, April 26, 2011

Page 42: Linecook - A Chef Alternative

Redundancycat > config/ssh <<"DOC"Host aboxPort 2220

Host bboxPort 2221

Host *User linecookHostname localhostDOC

scp -F config/ssh script.sh bbox:/tmp/script.shssh -F config/ssh bbox -- /tmp/script.sh# linecook@bbox-ubuntu: hello world!

Tuesday, April 26, 2011

Page 43: Linecook - A Chef Alternative

Make a “Package”Run with Linecook

[config/ssh] Host aboxPort 2220 Host bboxPort 2221

Host *User linecookHostname localhost

[packages/abox/script.sh]echo "# $(whoami)@$(hostname): hello world!"

[packages/bbox/script.sh]echo "# $(whoami)@$(hostname): Hullo Wurld!"

linecook run --script script.sh --remote-dir /tmp abox bbox# linecook@abox-ubuntu: hello world!# linecook@bbox-ubuntu: Hullo Wurld!

Tuesday, April 26, 2011

Page 44: Linecook - A Chef Alternative

Leverge Defaults

[config/ssh] Host aboxPort 2220 Host bboxPort 2221

Host *User linecookHostname localhost

[packages/abox/run]echo "# $(whoami)@$(hostname): hello world!"

[packages/bbox/run]echo "# $(whoami)@$(hostname): Hullo Wurld!"

linecook run# linecook@abox-ubuntu: hello world!# linecook@bbox-ubuntu: Hullo Wurld!

script: runremote dir: ~/linecookhosts: *

Tuesday, April 26, 2011

Page 45: Linecook - A Chef Alternative

Easy Way To Test![packages/abox/run]echo "hello world" > /tmp/message.txt

[packages/abox/test]if [ $(cat /tmp/message.txt) == "hello world" ]then echo "# success"else echo "# fail"fi

linecook run --script test# faillinecook run linecook run --script test# success

Tuesday, April 26, 2011

Page 46: Linecook - A Chef Alternative

Cmdline Dev Cycle

linecook startlinecook runlinecook run --script testlinecook ssh aboxlinecook snapshot modifiedlinecook stop

linecook start --snapshot modified...

Tuesday, April 26, 2011

Page 47: Linecook - A Chef Alternative

Advantages

Standard use of SSH

One standard config file

Ordinary inputs (directories, scripts)

Multiple VMs

Flexible!

Tuesday, April 26, 2011

Page 48: Linecook - A Chef Alternative

How to Make a Script

Tuesday, April 26, 2011

Page 49: Linecook - A Chef Alternative

How to Make a Script(boring alert)

Tuesday, April 26, 2011

Page 50: Linecook - A Chef Alternative

Start with a Script

[packages/abox/run]echo "# I will not manually configure my server"

linecook run# I will not manually configure my server

Tuesday, April 26, 2011

Page 51: Linecook - A Chef Alternative

Convert to Recipe[packages/abox.yml]linecook: package: recipes: run: abox

[recipes/abox.rb]target <<"SCRIPT"echo "# I will not manually configure my server"SCRIPT

A package file

Tuesday, April 26, 2011

Page 52: Linecook - A Chef Alternative

Convert to Recipe[packages/abox.yml]linecook: package: recipes: run: abox

[recipes/abox.rb]target <<"SCRIPT"echo "# I will not manually configure my server"SCRIPT

A tempfile(packages/abox/run)

Tuesday, April 26, 2011

Page 53: Linecook - A Chef Alternative

Simplify

[packages/abox.yml]{}

If default, nomanifest needed

[recipes/abox.rb]target <<"SCRIPT"echo "# I will not manually configure my server"SCRIPT

Tuesday, April 26, 2011

Page 54: Linecook - A Chef Alternative

Build and Run

[packages/abox.yml]{}

[recipes/abox.rb]target <<"SCRIPT"echo "# I will not manually configure my server"SCRIPT

linecook buildlinecook run# I will not manually configure my server

Tuesday, April 26, 2011

Page 55: Linecook - A Chef Alternative

Ok... that was boring.

Tuesday, April 26, 2011

Seems pointless, but here is where it gets cool.

Page 56: Linecook - A Chef Alternative

Use Recipe asContext for ERB

Tuesday, April 26, 2011

Page 57: Linecook - A Chef Alternative

ERB Compiles to Ruby

require 'erb'

compiler = ERB::Compiler.new("<>") compiler.put_cmd = "target<<"compiler.insert_cmd = "target<<"compiler.compile "got <%= obj %>"

# => "target<<\"got \"; target<<(( obj ).to_s)"

Tuesday, April 26, 2011

Page 58: Linecook - A Chef Alternative

InstanceEval for Contextclass Recipe attr_accessor :target def initialize @target = "" end def obj "milk" endend

code = "target<<\"got \"; target<<(( obj ).to_s)"

recipe = Recipe.newrecipe.instance_eval(code) recipe.target

# => "got milk"

Tuesday, April 26, 2011

Page 59: Linecook - A Chef Alternative

Make a Modulemodule Helper def get(obj) target<<"got "; target<<(( obj ).to_s) endend

recipe = Recipe.newrecipe.extend Helperrecipe.instance_eval %q{ get "milk" target << ", " get "cookies"}recipe.target

# => "got milk, got cookies"

Tuesday, April 26, 2011

Page 60: Linecook - A Chef Alternative

Make a Modulemodule Helper def get(obj) target<<"got "; target<<(( obj ).to_s) endend

recipe = Recipe.newrecipe.extend Helperrecipe.instance_eval %q{ get "milk" target << ", " get "cookies"}recipe.target

# => "got milk, got cookies"

This is a recipe!

Tuesday, April 26, 2011

Page 61: Linecook - A Chef Alternative

Helpers[helpers/example/echo.erb]Write an echo statement(str)--echo "<%= str %>"

[recipes/abox.rb]helpers "example"echo "# I will not manually configure my server"

Tuesday, April 26, 2011

Page 62: Linecook - A Chef Alternative

Helpers[helpers/example/echo.erb]Write an echo statement(str)--echo "<%= str %>"

[recipes/abox.rb]helpers "example"echo "# I will not manually configure my server"

[lib/example.rb]module Example # Write an echo ... def echo(str) target<< "echo ";... endend

Tuesday, April 26, 2011

Page 63: Linecook - A Chef Alternative

Helpers[helpers/example/echo.erb]Write an echo statement(str)--echo "<%= str %>"

[recipes/abox.rb]helpers "example"echo "# I will not manually configure my server"

[lib/example.rb]module Example # Write an echo ... def echo(str) target<< "echo ";... endend

Tuesday, April 26, 2011

Page 64: Linecook - A Chef Alternative

Helpers[helpers/example/echo.erb]Write an echo statement(str)--echo "<%= str %>"

[recipes/abox.rb]helpers "example"echo "# I will not manually configure my server"

[lib/example.rb]module Example # Write an echo ... def echo(str) target<< "echo ";... endend

Tuesday, April 26, 2011

Page 65: Linecook - A Chef Alternative

Helpers[helpers/example/echo.erb]Write an echo statement(str)--echo "<%= str %>"

[recipes/abox.rb]helpers "example"echo "# I will not manually configure my server"

[lib/example.rb]module Example # Write an echo ... def echo(str) target<< "echo ";... endend

Tuesday, April 26, 2011

Page 66: Linecook - A Chef Alternative

Helpers[helpers/example/echo.erb]Write an echo statement(str)--echo "<%= str %>"

[recipes/abox.rb]helpers "example"echo "# I will not manually configure my server"

[lib/example.rb]module Example # Write an echo ... def echo(str) target<< "echo ";... endend

Tuesday, April 26, 2011

Page 67: Linecook - A Chef Alternative

Helpers[helpers/example/echo.erb]Write an echo statement(str)--echo "<%= str %>"

[recipes/abox.rb]helpers "example"echo "# I will not manually configure my server"

[lib/example.rb]module Example # Write an echo ... def echo(str) target<< "echo ";... endend

Tuesday, April 26, 2011

Page 68: Linecook - A Chef Alternative

Helpers[helpers/example/echo.erb]Write an echo statement(str)--echo "<%= str %>"

[recipes/abox.rb]helpers "example"echo "# I will not manually configure my server"

require "example"extend Example

Tuesday, April 26, 2011

Page 69: Linecook - A Chef Alternative

Helpers[helpers/example/echo.erb]Write an echo statement(str)--echo "<%= str %>"

[recipes/abox.rb]helpers "example"echo "# I will not manually configure my server"

linecook buildlinecook run# I will not manually configure my server

Tuesday, April 26, 2011

Page 70: Linecook - A Chef Alternative

Capture (no write)[helpers/example/color.erb]Add color to a string(color, str) codes = Hash[*%W{red 0;31 white 1;37 blue 0;34}]--\033[<%= codes[color.to_s] %>m<%= str %>\033[0m

[recipes/abox.rb]helpers "example"msg = "# I will not manually configure my server"echo _color("blue", msg)

Tuesday, April 26, 2011

Page 71: Linecook - A Chef Alternative

Capture (no write)[helpers/example/color.erb]Add color to a string(color, str) codes = Hash[*%W{red 0;31 white 1;37 blue 0;34}]--\033[<%= codes[color.to_s] %>m<%= str %>\033[0m

[recipes/abox.rb]helpers "example"msg = "# I will not manually configure my server"echo _color("blue", msg)

Prefix with underscoreString output used as input

Tuesday, April 26, 2011

Page 72: Linecook - A Chef Alternative

Capture (no write)[helpers/example/color.erb]Add color to a string(color, str) codes = Hash[*%W{red 0;31 white 1;37 blue 0;34}]--\033[<%= codes[color.to_s] %>m<%= str %>\033[0m

[recipes/abox.rb]helpers "example"msg = "# I will not manually configure my server"echo _color("blue", msg)

linecook buildlinecook run# I will not manually configure my server

Tuesday, April 26, 2011

Page 73: Linecook - A Chef Alternative

Recipes are Ruby

[recipes/abox.rb]helpers "example"

msg = "# I will not manually configure my server"3.times do echo _color("blue", msg)end

linecook buildlinecook run# I will not manually configure my server# I will not manually configure my server# I will not manually configure my server

Tuesday, April 26, 2011

Page 74: Linecook - A Chef Alternative

Advantages

Easily extensible DSL

Produces ordinary modules

Test as any other Ruby

Distribute as Gems (versions, bundler)

Reprocessing of output

Also, kind of cool...

Tuesday, April 26, 2011

Page 75: Linecook - A Chef Alternative

Linebook(Standard Library for Linecook)

Tuesday, April 26, 2011

Page 76: Linecook - A Chef Alternative

Logic + Pipelines[recipes/demo.rb]helpers 'linebook/shell'

unless_ _file?('/tmp/message') do cat.to('/tmp/message').heredoc do writeln 'hello world!' endend

cat('/tmp/message')

Tuesday, April 26, 2011

Page 77: Linecook - A Chef Alternative

Logic + Pipelines[packages/demo/run]

if ! [ -f "/tmp/message" ]then cat > /tmp/message << HEREDOC_0hello world!HEREDOC_0fi

cat "/tmp/message"

Tuesday, April 26, 2011

Page 78: Linecook - A Chef Alternative

Under Construction

Exit status checks, ‘stack trace’

Helpers for login/su

User and File Management

Server-side testing (assert_script)

Installs, config, deployments, etc.

Tuesday, April 26, 2011

Page 79: Linecook - A Chef Alternative

No need to wait!

Tuesday, April 26, 2011

Page 80: Linecook - A Chef Alternative

Linecook

Recipes Packages

Server

AttributesHelpers

FilesTemplates

Tuesday, April 26, 2011

Page 81: Linecook - A Chef Alternative

Learn More

GitHub:

pinnacol/linecook.git

pinnacol/linebook.git

Me:

github.com/thinkerbot

@thinkerbot

Tuesday, April 26, 2011

Page 82: Linecook - A Chef Alternative

Thanks Pinnacol!

Tuesday, April 26, 2011

Page 83: Linecook - A Chef Alternative

Thanks Derailed!

Tuesday, April 26, 2011

Page 84: Linecook - A Chef Alternative

Questions?

Tuesday, April 26, 2011