fix bugs
[p5sagit/Oyster.git] / lib / Dist / Zilla / App / Command / provision.pm
1 use strict;
2 use warnings;
3 package Dist::Zilla::App::Command::provision;
4 BEGIN {
5   $Dist::Zilla::App::Command::provision::VERSION = '0.1';
6 }
7 # ABSTRACT: release your dist to the CPAN
8 use Dist::Zilla::App -command;
9 use Moose;
10 use Config::Any;
11 use Hash::Merge 'merge';
12 use Oyster::Provision;
13
14 sub abstract { 'provision a new Oyster VM' }
15
16 sub opt_spec {
17   [ 'name=s'     => 'the name of the VM to create' ],
18 }
19
20 sub execute {
21   my ($self, $opt, $arg) = @_;
22
23   my $zilla = $self->zilla;
24
25   my $name = $opt->name
26     or die "No name provided!";
27   my @config_files = ( './oyster.conf' ); # TODO make configurable
28
29   my $cfg = Config::Any->load_files({ files => \@config_files, use_ext => 0 });
30   ($cfg) = values %{ $cfg->[0] }; # FIX with ::JFDI or similar
31
32   my $Provision = $cfg->{Provision} or die "No <Provision> section";
33
34   my @hashes = grep $_, $Provision->{Default}, $Provision->{$name}
35       or die "No section for <Provision> <$name>, and no <default>";
36
37   my %hash = @hashes > 1 ? %{ merge( @hashes ) } : %{ $hashes[0] };
38
39   my $type = delete $hash{type} || 'Oyster::Provision::Rackspace';
40   $hash{provision_backend} = $type =~/^Oyster::Provision::/ ? $type : "Oyster::Provision::$type";
41   $hash{pub_ssh} ||= "$ENV{HOME}/.ssh/id_rsa.pub";
42   $hash{size}    ||= 1;  # id 1 - ram 256 MiB - disk 10 GiB
43   $hash{image}   ||= 69; # id 69 - Ubuntu 10.10 (meerkat)
44
45   my $server = Oyster::Provision->new(
46         name => $name,
47         config => \%hash,
48         %hash,
49   );
50   $server->create;
51   print "Instance $name created! ($server)\n";
52 }
53
54 1;
55
56 __END__
57 =pod
58
59 =head1 NAME
60
61 Dist::Zilla::App::Command::provision - provision a new Oyster VM
62
63 =head1 VERSION
64
65 version 0.1
66
67 =head1 SYNOPSIS
68
69     TODO
70
71 =head1 AUTHOR
72
73 CONTRIBUTORS
74
75 =head1 COPYRIGHT AND LICENSE
76
77 This software is copyright (c) 2010 by CONTRIBUTORS
78
79 This is free software; you can redistribute it and/or modify it under
80 the same terms as the Perl 5 programming language system itself.
81
82 =cut
83