Class: S3MasterCli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Shell
Defined in:
lib/s3_master/cli.rb

Instance Method Summary collapse

Instance Method Details

#apply(cli_bucket = nil, cli_policy_type = nil, cli_policy_id = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/s3_master/cli.rb', line 51

def apply(cli_bucket=nil, cli_policy_type=nil, cli_policy_id=nil)
  config = S3Master::Config.new(options[:"config-file"])
  config.each do |bucket, policy_type, policy_id|
    next if !cli_bucket.nil? && cli_bucket != bucket ||
            !cli_policy_type.nil? && cli_policy_type != policy_type ||
            !cli_policy_id.nil? && cli_policy_id != policy_id

    policy_diff = diff(bucket, policy_type, policy_id)

    next if policy_diff.identical? || ! (options[:force] || yes?("Proceed? (y/N)"))

    local_policy = S3Master::LocalPolicy.new(config, bucket, policy_type, options.merge(id: policy_id).symbolize_keys)
    remote_policy = S3Master::RemotePolicy.new(bucket, policy_type, {id: policy_id, region: config.region(bucket)})
    remote_policy.write(local_policy)
  end
end

#diff(bucket, policy_type, policy_id = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/s3_master/cli.rb', line 25

def diff(bucket, policy_type, policy_id=nil)
  config = S3Master::Config.new(options[:"config-file"])

  remote_policy = S3Master::RemotePolicy.new(bucket, policy_type, {id: policy_id, region: config.region(bucket)})
  local_policy = S3Master::LocalPolicy.new(config, bucket, policy_type, options.merge(id: policy_id).symbolize_keys)

  if options[:debug]
    bkt = Aws::S3::Bucket.new(bucket)
    puts "%s: %s" % [bkt.name, bkt.url]
    puts "=== Remote Policy:\n%s" % [JSON.neat_generate(remote_policy.body, sort: true)]
    puts "=== Local Policy:\n%s" % [JSON.neat_generate(local_policy.body, sort: true)]
  end

  policy_diff = S3Master::PolicyDiffer.new(remote_policy.body, local_policy.body)
  prefix = "#{bucket}/#{policy_type}"
  prefix += "/#{policy_id}" if policy_id

  if policy_diff.identical?
    puts "#{prefix}: Local and remote policies match."
  else
    puts "#{prefix} diff:\n%s" % [policy_diff.to_s]
  end
  policy_diff
end

#fetch(buckets = nil, policy_types = S3Master::RemotePolicy::POLICY_TYPES, policy_id = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/s3_master/cli.rb', line 69

def fetch(buckets=nil, policy_types=S3Master::RemotePolicy::POLICY_TYPES, policy_id=nil)
  config = S3Master::Config.new(options[:"config-file"])
  buckets ||= config[:buckets].keys

  Array(buckets).each do |bucket|
    Array(policy_types).each do |policy_type|
      next if ! S3Master::RemotePolicy.known_policy_type?(policy_type)
      local_policy = S3Master::LocalPolicy.new(config, bucket, policy_type, options.merge(skip_load: true, id: policy_id).symbolize_keys)
      remote_policy = S3Master::RemotePolicy.new(bucket, policy_type, {id: policy_id, region: config.region(bucket)})

      if !local_policy.basename.nil?
        local_policy.write(remote_policy)
      else
        puts "%s policy:\n%s" % [policy_type, remote_policy.pretty_body]
      end
    end
  end
end

#status(user_bucket = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/s3_master/cli.rb', line 89

def status(user_bucket=nil)
  config = S3Master::Config.new(options[:"config-file"])

  any_differences = false
  config.each do |bucket, policy_type, policy_id|
    next if !user_bucket.nil? && user_bucket != bucket
    local_policy = S3Master::LocalPolicy.new(config, bucket, policy_type, options.merge(id: policy_id).symbolize_keys)
    remote_policy = S3Master::RemotePolicy.new(bucket, policy_type, {id: policy_id, region: config.region(bucket)})

    policy_diff = S3Master::PolicyDiffer.new(remote_policy.body, local_policy.body)
    if !policy_diff.identical?
      any_differences = true

      if policy_id.nil?
        puts "* %s: %s" % [bucket, policy_type]
      else
        puts "* %s: %s %s" % [bucket, policy_type, policy_id]
      end
    end
  end

  puts "No differences detected." if !any_differences
end