prepend Oyster::Provision:: if not supplied by config
[p5sagit/Oyster.git] / lib / Dist / Zilla / App / Command / provision.pm
index 1b355a6..9ba0bc2 100644 (file)
@@ -8,11 +8,13 @@ BEGIN {
 use Dist::Zilla::App -command;
 use Moose;
 use Config::Any;
+use Hash::Merge 'merge';
+use Oyster::Provision;
 
 sub abstract { 'provision a new Oyster VM' }
 
 sub opt_spec {
-  [ 'name|s'     => 'the name of the VM to create' ],
+  [ 'name=s'     => 'the name of the VM to create' ],
 }
 
 sub execute {
@@ -24,21 +26,26 @@ sub execute {
     or die "No name provided!";
   my @config_files = ( './oyster.conf' ); # TODO make configurable
 
-  my $cfg = Config::Any->load_files({ files => \@config_files });
+  my $cfg = Config::Any->load_files({ files => \@config_files, use_ext => 0 });
   ($cfg) = values %{ $cfg->[0] }; # FIX with ::JFDI or similar
 
   my $Provision = $cfg->{Provision} or die "No <Provision> section";
-  my $target = $Provision->{$name}  or die "No section for <Provision> <$name>";
 
-  my $type = $target->{type} || 'EC2';
+  my @hashes = grep $_, $Provision->{Default}, $Provision->{$name}
+      or die "No section for <Provision> <$name>, and no <default>";
+    
+  my %hash = @hashes > 1 ? %{ merge( @hashes ) } : $hashes[0];
+
+  my $type = delete $hash{type} || 'Oyster::Provision::Rackspace';
+  $hash{provision_backend} = $type =~/^Oyster::Provision::/ ? $type : "Oyster::Provision::$type";
+  $hash{pub_ssh} ||= "$ENV{HOME}/.ssh/id_rsa.pub";
+  $hash{size}    ||= 1;  # id 1 - ram 256 MiB - disk 10 GiB
+  $hash{image}   ||= 69; # id 69 - Ubuntu 10.10 (meerkat)
 
-  use Oyster::Provision;
   my $server = Oyster::Provision->new(
         name => $name,
-        size => '256',
-        image => 'Meerkat',
-        pub_ssh => "$ENV{HOME}/.ssh/id_rsa.pub",
-        provision_backend => $type,
+        config => \%hash,
+        %hash,
   );
   $server->create;
   print "Instance $name created! ($server)\n";