Class: OpzWorks::Commands::JSON

Inherits:
Object
  • Object
show all
Defined in:
lib/opzworks/commands/json.rb

Class Method Summary collapse

Class Method Details



14
15
16
# File 'lib/opzworks/commands/json.rb', line 14

def self.banner
  'Update stack json'
end

.runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/opzworks/commands/json.rb', line 18

def self.run
  options = Trollop.options do
    banner "      \#{JSON.banner}\n\n        opzworks json stack1 stack2 ...\n\n      The stack name can be passed as any unique regex. If there is\n      more than one match, it will simply be skipped.\n\n      Options:\n    EOS\n    opt :quiet, 'Update the stack json without confirmation', short: 'q', default: false\n    opt :context, 'Change the number lines of diff context to show', short: 'c', default: 5\n  end\n  ARGV.empty? ? Trollop.die('no stacks specified') : false\n\n  config   = OpzWorks.config\n  client   = Aws::OpsWorks::Client.new(region: config.aws_region, profile: config.aws_profile)\n  response = client.describe_stacks\n\n  # loops over inputs\n  ARGV.each do |opt|\n    populate_stack(opt, response)\n    next if @populate_stack_failure == true\n\n    manage_berks_repos\n    next if @berks_repo_failure == true\n\n    json = File.read(\"\#{@target_path}/stack.json\")\n    diff = Diffy::Diff.new(@stack_json + \"\\n\", json, context: options[:context])\n    diff_str = diff.to_s(:color).chomp\n\n    hash = {}\n    hash[:stack_id] = @stack_id\n    hash[:custom_json] = json\n\n    if diff_str.empty?\n      puts 'There are no differences between the existing stack json and the json you\\'re asking to push.'.foreground(:yellow)\n    else\n      if options[:quiet]\n        puts 'Quiet mode detected. Pushing the following updated json:'.foreground(:yellow)\n        puts diff_str\n\n        client.update_stack(hash)\n        puts 'Done!'.color(:green)\n      else\n        puts \"The following is a partial diff of the existing stack json and the json you're asking to push:\".foreground(:yellow)\n        puts diff_str\n        STDOUT.print \"\\nType \".foreground(:yellow) + 'yes '.foreground(:blue) + 'to continue, any other key will abort: '.foreground(:yellow)\n        input = STDIN.gets.chomp\n        if input =~ /^y/i\n          client.update_stack(hash)\n          puts 'Done!'.color(:green)\n        else\n          puts 'Update skipped.'.foreground(:red)\n        end\n      end\n    end\n  end\nend\n".unindent