| 1 | package CloudDeploy::CommandLine::Diff { | ||
| 2 | use Cfn; | ||
| 3 | use Cfn::Diff; | ||
| 4 | use Paws; | ||
| 5 | use File::Slurp; | ||
| 6 | use MooseX::App; | ||
| 7 | use CloudDeploy::Utils; | ||
| 8 | use CloudDeploy::DeploymentCollection; | ||
| 9 | use JSON; | ||
| 10 | use String::Diff; | ||
| 11 | use Term::ANSIColor qw/:constants/; | ||
| 12 | use Scalar::Util; | ||
| 13 | |||
| 14 | parameter left => ( | ||
| 15 | is => 'rw', | ||
| 16 | isa => 'Str', | ||
| 17 | documentation => q[cfn:<REGION>:<NAME>|deploy:<NAME>|file:<JSON_FILE>], | ||
| 18 | required => 1, | ||
| 19 | ); | ||
| 20 | |||
| 21 | parameter right => ( | ||
| 22 | is => 'rw', | ||
| 23 | isa => 'Str', | ||
| 24 | documentation => q[cfn:<REGION>:<NAME>|deploy:<NAME>|file:<JSON_FILE>], | ||
| 25 | required => 1, | ||
| 26 | ); | ||
| 27 | |||
| 28 | option pretty => ( | ||
| 29 | is => 'ro', | ||
| 30 | isa => 'Bool', | ||
| 31 | documentation => 'Pretty print the changes in JSON', | ||
| 32 | default => 0 | ||
| 33 | ); | ||
| 34 | |||
| 35 | has _left_cfn => ( | ||
| 36 | is => 'ro', | ||
| 37 | isa => 'Cfn', | ||
| 38 | lazy => 1, | ||
| 39 | default => sub { shift->_get_cfn('left') } | ||
| 40 | ); | ||
| 41 | |||
| 42 | has _right_cfn => ( | ||
| 43 | is => 'ro', | ||
| 44 | isa => 'Cfn', | ||
| 45 | lazy => 1, | ||
| 46 | default => sub { shift->_get_cfn('right') } | ||
| 47 | ); | ||
| 48 | |||
| 49 | |||
| 50 | sub _get_cfn { | ||
| 51 | my ($self, $side) = @_; | ||
| 52 | if (my ($region, $stack_name) = ($self->$side =~ m/^cfn\:(.*?)\:(.*)$/)) { | ||
| 53 | return Cfn->from_json(Paws->service('CloudFormation', region => $region)->GetTemplate(StackName => $stack_name)->TemplateBody); | ||
| 54 | } elsif (my ($file) = ($self->$side =~ m/^file\:(.*)$/)) { | ||
| 55 | # read_file needs to be in scalar context to return all lines as one string | ||
| 56 | return Cfn->from_json(scalar(read_file($file))); | ||
| 57 | } elsif (my ($deploy_name) = ($self->$side =~ m/^deploy\:(.*)$/)) { | ||
| 58 | my $deployments = CloudDeploy::DeploymentCollection->new(account => $ENV{'CPSD_AWS_ACCOUNT'}); | ||
| 59 | my $deploy = $deployments->get_deployment($deploy_name); | ||
| 60 | |||
| 61 | my $module = load_class($deploy->type); | ||
| 62 | |||
| 63 | my @attached = | ||
| 64 | map { $_->name } | ||
| 65 | grep { $_->does('Attached') } | ||
| 66 | $module->{params_class}->meta->get_all_attributes; | ||
| 67 | |||
| 68 | my $params = $deploy->params; | ||
| 69 | delete $params->{ $_ } for (@attached); | ||
| 70 | |||
| 71 | my $merged_params = $module->{params_class}->new_with_options(%{ $params }, argv => $self->extra_argv); | ||
| 72 | my $obj = $module->{class}->new(params => $merged_params); | ||
| 73 | return Cfn->from_hashref($obj->as_hashref); | ||
| 74 | } else { | ||
| 75 | die "Unknown format for side $side"; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | has differences => (is => 'ro', isa => 'Cfn::Diff', lazy => 1, default => sub { | ||
| 80 | my $self = shift; | ||
| 81 | return Cfn::Diff->new(left => $self->_left_cfn, right => $self->_right_cfn); | ||
| 82 | }); | ||
| 83 | |||
| 84 | sub _is_dynamic { | ||
| 85 | my ($self, $element) = @_; | ||
| 86 | return (blessed($element) and $element->isa('CCfnX::DynamicValue')); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub run { | ||
| 90 | my $self = shift; | ||
| 91 | |||
| 92 | printf "Comparing %s to %s\n", $self->left, $self->right; | ||
| 93 | $self->differences->diff; | ||
| 94 | |||
| 95 | if (@{ $self->differences->changes } == 0) { | ||
| 96 | print "No changes detected\n"; | ||
| 97 | return; | ||
| 98 | } | ||
| 99 | |||
| 100 | foreach my $change (@{ $self->differences->changes }) { | ||
| 101 | my $compare_from = $change->from; | ||
| 102 | my $compare_to = $change->to; | ||
| 103 | |||
| 104 | # We'll always compare the textual form that cloudformation would recieve, since the diff will return | ||
| 105 | # changes in properties that have DynamicValues in them, that can render the result equal or different | ||
| 106 | |||
| 107 | $compare_from = $self->_print_element($change->from, $self->_left_cfn); | ||
| 108 | $compare_to = $self->_print_element($change->to, $self->_right_cfn); | ||
| 109 | |||
| 110 | printf "%s %s\n", $change->path, $change->change; | ||
| 111 | if (defined $compare_from and defined $compare_to and $compare_from ne $compare_to) { | ||
| 112 | my $diff = String::Diff::diff($compare_from, $compare_to, | ||
| 113 | remove_open => RED . DARK . BOLD, | ||
| 114 | remove_close => CLEAR, | ||
| 115 | append_open => GREEN . DARK . BOLD, | ||
| 116 | append_close => CLEAR, | ||
| 117 | ); | ||
| 118 | printf "\tfrom: %s\n", $diff->[0]; | ||
| 119 | printf "\t to: %s\n", $diff->[1]; | ||
| 120 | } elsif (not defined $compare_from) { | ||
| 121 | printf "\tfrom:\n"; | ||
| 122 | printf "\t to: %s%s%s\n", GREEN . DARK . BOLD, $compare_to, CLEAR; | ||
| 123 | } elsif (not defined $compare_to) { | ||
| 124 | printf "\tfrom: %s%s%s\n", RED . DARK . BOLD, $compare_from, CLEAR; | ||
| 125 | printf "\t to:\n"; | ||
| 126 | } | ||
| 127 | printf "----------------------\n"; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _print_element { | ||
| 132 | my ($self, $element, $c) = @_; | ||
| 133 | |||
| 134 | if (blessed($element)) { | ||
| 135 | if ($element->isa('Cfn::Value::Primitive')){ | ||
| 136 | return $element->Value; | ||
| 137 | } elsif ($element->isa('CCfnX::DynamicValue')) { | ||
| 138 | # A dynamic value has to be converted to a normal value, and then be "printed" | ||
| 139 | return $self->_print_element($element->to_value($c), $c); | ||
| 140 | } else { | ||
| 141 | if ($self->pretty) { | ||
| 142 | return JSON->new->canonical->pretty->encode($element->as_hashref($c)); | ||
| 143 | } else { | ||
| 144 | return JSON->new->canonical->encode($element->as_hashref($c)); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } else { | ||
| 148 | if (ref($element) eq 'HASH') { | ||
| 149 | my %jsondocs; | ||
| 150 | |||
| 151 | foreach my $key (keys(%{$element})) { | ||
| 152 | my $value = $self->_print_element($element->{$key}); | ||
| 153 | |||
| 154 | if ($value =~ /^\{/) { | ||
| 155 | $jsondocs{$key} = JSON->new->decode($value); | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | $jsondocs{$key} = $value; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | if ($self->pretty) { | ||
| 163 | return JSON->new->canonical->pretty->encode(\%jsondocs); | ||
| 164 | } else { | ||
| 165 | return JSON->new->canonical->encode(\%jsondocs); | ||
| 166 | } | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | return $element; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | } | ||
| 175 | |||
| 176 | 1; |