| 1 | package Chef; | ||
| 2 | |||
| 3 | use Chef::Recipe; | ||
| 4 | use Chef::Resource; | ||
| 5 | use Data::Dumper; | ||
| 6 | use JSON::Any qw(XS JSON DWIW); | ||
| 7 | |||
| 8 | require Exporter; | ||
| 9 | @ISA = qw(Exporter); | ||
| 10 | @EXPORT = qw(resource node); | ||
| 11 | |||
| 12 | use warnings; | ||
| 13 | use strict; | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Chef - Write Chef recipes in Perl instead of Ruby. | ||
| 18 | |||
| 19 | Learn more about Chef - L<http://wiki.opscode.com/display/chef> | ||
| 20 | |||
| 21 | =head1 VERSION | ||
| 22 | |||
| 23 | Version 0.01 | ||
| 24 | |||
| 25 | =head1 WARNING | ||
| 26 | |||
| 27 | This is a proof of concept - it shows the path for future integration, but all the steps are not complete. What remains: | ||
| 28 | |||
| 29 | * You cannot write Attribute or Definition files in Perl. | ||
| 30 | * At the moment, all your Perl recipes must live in the same cookbook. | ||
| 31 | * There is very little error handling. (ah, who am I kidding - there is none) | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | our $VERSION = '0.01'; | ||
| 36 | our $recipe = Chef::Recipe->new; | ||
| 37 | our $node_data; | ||
| 38 | |||
| 39 | =head1 SYNOPSIS | ||
| 40 | |||
| 41 | Example: | ||
| 42 | |||
| 43 | use Chef; | ||
| 44 | |||
| 45 | resource file => '/tmp/foo', sub { | ||
| 46 | my $r = shift; | ||
| 47 | $r->owner('adam'); | ||
| 48 | $r->action('create'); | ||
| 49 | }; | ||
| 50 | |||
| 51 | resource file => '/tmp/' . node->{attributes}->{hostname} . "_created_with_perl", sub { | ||
| 52 | my $r = shift; | ||
| 53 | $r->action('create'); | ||
| 54 | }; | ||
| 55 | |||
| 56 | Would create a file called /tmp/foo, and one called /tmp/HOSTNAME_created_with_perl. (Where HOSTNAME is, well, your hostname). | ||
| 57 | |||
| 58 | To use this module, you will need to install Chef, place the included cookbook in your cookbook repository, and place your perl based recipes in files/default/perl_recipes. | ||
| 59 | |||
| 60 | =head1 EXPORT | ||
| 61 | |||
| 62 | We export two functions in to your namespace, resource and node. | ||
| 63 | |||
| 64 | =head1 FUNCTIONS | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub INIT { | ||
| 69 | load_node(); | ||
| 70 | } | ||
| 71 | |||
| 72 | =head2 node | ||
| 73 | |||
| 74 | Returns the Chef::Node object. This allows you to see what recipes are applied to this node via: | ||
| 75 | |||
| 76 | node->{recipes} # Returns an array of recipe names | ||
| 77 | |||
| 78 | Also allows you to access all the nodes attributes via: | ||
| 79 | |||
| 80 | node->{attributes} # Returns all the nodes attributes | ||
| 81 | |||
| 82 | Any changes you make to the node object do not currently persist back in to Chef. (ie: you cannot use them in subsequent recipes.) This is likely to change once integration is complete. | ||
| 83 | |||
| 84 | =cut | ||
| 85 | sub node { | ||
| 86 | return $node_data; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub load_node { | ||
| 90 | my $data; | ||
| 91 | while ( my $line = <STDIN> ) { | ||
| 92 | $data = $data . $line; | ||
| 93 | } | ||
| 94 | $node_data = JSON::Any->jsonToObj($data); | ||
| 95 | 1; | ||
| 96 | } | ||
| 97 | |||
| 98 | =head2 resource | ||
| 99 | |||
| 100 | Create a new Chef Resource. Valid resources are listed at: | ||
| 101 | |||
| 102 | L<http://wiki.opscode.com/display/chef/Resources> | ||
| 103 | |||
| 104 | An example of translating from the ruby version to perl: | ||
| 105 | |||
| 106 | # The ruby version | ||
| 107 | package "sudo" do | ||
| 108 | action :install | ||
| 109 | end | ||
| 110 | |||
| 111 | # Make sure sudo is always at the latest version | ||
| 112 | resource package => "sudo", sub { | ||
| 113 | my $r = shift; | ||
| 114 | $r->action("upgrade"); | ||
| 115 | } | ||
| 116 | |||
| 117 | Essentially, you create new resources by calling this method with the resource type (package, remote_file, etc.), resource name ("sudo", "/tmp/foo"), and a subroutine which recives a Chef::Resource object. You can then set attributes of the resource via that object. (Hence, my $r = shift). | ||
| 118 | |||
| 119 | =cut | ||
| 120 | sub resource { | ||
| 121 | my $resource_type = shift; | ||
| 122 | my $resource_name = shift; | ||
| 123 | my $resource_sub = shift; | ||
| 124 | |||
| 125 | my $resource = Chef::Resource->new( | ||
| 126 | { | ||
| 127 | resource_type => $resource_type, | ||
| 128 | name => $resource_name, | ||
| 129 | resource_sub => $resource_sub | ||
| 130 | } | ||
| 131 | ); | ||
| 132 | $resource->evaluate(); | ||
| 133 | $recipe->add_resource($resource); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub send_to_chef { | ||
| 137 | print JSON::Any->objToJson( | ||
| 138 | { | ||
| 139 | node => $node_data, | ||
| 140 | resource_collection => $recipe->prepare_json | ||
| 141 | } | ||
| 142 | ); | ||
| 143 | } | ||
| 144 | |||
| 145 | sub END { | ||
| 146 | send_to_chef; | ||
| 147 | } | ||
| 148 | |||
| 149 | =head1 AUTHOR | ||
| 150 | |||
| 151 | Adam Jacob, C<< <adam at opscode.com> >> | ||
| 152 | |||
| 153 | =head1 SOURCE | ||
| 154 | |||
| 155 | You can find the source on GitHub at L<http://github.com/adamhjk/chef-perl> | ||
| 156 | |||
| 157 | =head1 BUGS | ||
| 158 | |||
| 159 | Please report bugs to L<http://tickets.opscode.com>. | ||
| 160 | |||
| 161 | =head1 SUPPORT | ||
| 162 | |||
| 163 | You can find documentation for this module with the perldoc command. | ||
| 164 | |||
| 165 | perldoc Chef | ||
| 166 | |||
| 167 | You can also look for information at: | ||
| 168 | |||
| 169 | =over 4 | ||
| 170 | |||
| 171 | =item * Opscodes Ticket Tracking System: | ||
| 172 | |||
| 173 | L<http://tickets.opscode.com> | ||
| 174 | |||
| 175 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 176 | |||
| 177 | L<http://annocpan.org/dist/Chef> | ||
| 178 | |||
| 179 | =item * CPAN Ratings | ||
| 180 | |||
| 181 | L<http://cpanratings.perl.org/d/Chef> | ||
| 182 | |||
| 183 | =item * Search CPAN | ||
| 184 | |||
| 185 | L<http://search.cpan.org/dist/Chef/> | ||
| 186 | |||
| 187 | =back | ||
| 188 | |||
| 189 | |||
| 190 | =head1 ACKNOWLEDGEMENTS | ||
| 191 | |||
| 192 | |||
| 193 | =head1 COPYRIGHT & LICENSE | ||
| 194 | |||
| 195 | Copyright 2009 Opscode, Inc., all rights reserved. | ||
| 196 | |||
| 197 | This program is free software; you can redistribute it and/or modify it | ||
| 198 | under the same terms as Perl itself. | ||
| 199 | |||
| 200 | |||
| 201 | =cut | ||
| 202 | |||
| 203 | 1; # End of Chef |