Friday, August 3, 2012

Converting Pupppet User.pp to Chef User.json

For the last couple weeks my team and I have been looking at Chef.
We currently have medium size puppet deployment but wants to see if the grass is greener on the other side with chef. Chef seems to be allot more scripting where puppet is more of template based DSL.

Regardless One of the first hurdles we faced was migrating users from one to another
Here is a little perl to help anybody get past this hurdle.

*** Update *****

Ran into a little issue converting this with the GID so I took it out the useradd adds this for you so no need to do it.

./convert-to-chef.pl user.pp

It outputs it as user.json


###### Convert-to-chef.pl ################
#!/usr/bin/perl
use Switch;
$userfile = $ARGV[0];
open(USERFILE, "$userfile") or die $! ;
@lines = ;
foreach (@lines)
{
 $line = $_ ;
  switch ( $line )
  {

    case (/account/) {
                                   @account_line = split(/\{/,$line);
                              $account = $account_line[1];
                              $account =~ s/\,//g ;
                              $account =~ s/\://g ;
                              $account =~ s/\"//g ;
                              $account =~ s/\”//g ;
                              $account =~ s/\ //g ;
                              chomp($account);   
                         }

    case (/uid/) {
                                   @uid_line = split(/\>/, $line);
                              $uid = $uid_line[1];
                              $uid =~ s/\,//g ;
                              $uid =~ s/\ //g ;
                              chomp($uid);
                     }
    case (/gid/) {
                              @gid_line = split(/\>/, $line);
                             $gid = $gid_line[1] ;
                              $gid =~ s/\,//g ;
                              $gid =~ s/\ //g ;
                              chomp($gid);
                       }
    case (/comment/) {
                              @comment_array = split(/\>/, $line);
                              $comment = $comment_array[1];
                              $comment =~ s/\,//g ;
                              chomp($comment);
                            }
  }
}
close(USERFILE);

open(CHEFFILE, ">$account.json") or die $! ;

print CHEFFILE "
\{
  \"id\"\: \"$account\"\,
  \"uid\"\: \"$uid\"\,
  \"home_dir\"\: \"\/home\/$account\"\,
  \"group\"\: \"wheel\"\,
  \"shell\"\: \"\/bin\/bash\"\
\}
";
close(CHEFFILE);