Class: Awful::Ssm

Inherits:
Cli show all
Defined in:
lib/awful/ssm.rb

Constant Summary collapse

COLORS =
{
  Success:   :green,
  TimedOut:  :red,
  Cancelled: :red,
  Failed:    :red
}

Instance Method Summary collapse

Methods inherited from Cli

#initialize, #ll, #version

Constructor Details

This class inherits a constructor from Awful::Cli

Instance Method Details

#commandsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/awful/ssm.rb', line 85

def commands
  ssm.list_commands.commands.output do |cmds|
    if options[:long]
      print_table cmds.map { |c|
        [
          c.command_id,
          c.instance_ids.join(','),
          c.document_name,
          color(c.status),
          c.requested_date_time,
          c.comment
        ]
      }
    else
      puts cmds.map(&:command_id)
    end
  end
end

#delete(name) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/awful/ssm.rb', line 65

def delete(name)
  if yes?("Really delete parameter #{name}?", :yellow)
    ssm.delete_parameter(name: name)
  end
rescue Aws::SSM::Errors::ParameterNotFound => e
  error(e.message)
end

#documents(name = '.') ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/awful/ssm.rb', line 116

def documents(name = '.')
  filter = [{key: 'PlatformTypes', value: options[:platform_types].capitalize}]
  ssm.list_documents(document_filter_list: filter).document_identifiers.select do |doc|
    doc.name.match(/#{name}/i)
  end.output do |docs|
    if options[:long]
      print_table docs.map { |d| [d.name, d.platform_types.join(',')] }
    else
      puts docs.map(&:name)
    end
  end
end

#dump(id) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/awful/ssm.rb', line 105

def dump(id)
  ssm.list_command_invocations(command_id: id, details: true).command_invocations.output do |cmds|
    cmds.each do |cmd|
      puts YAML.dump(stringify_keys(cmd.to_hash))
    end
  end
end

#get(name) ⇒ Object



29
30
31
32
33
# File 'lib/awful/ssm.rb', line 29

def get(name)
  puts ssm.get_parameter(name: name, with_decryption: options[:decrypt]).parameter.value
rescue Aws::SSM::Errors::ParameterNotFound => e
  error(e.message)
end

#history(name) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/awful/ssm.rb', line 75

def history(name)
  ssm.get_parameter_history(name: name, with_decryption: options[:decrypt]).each do |p|
    print_table p.parameters.map { |h|
      [ h.version, h.last_modified_date, h.value ]
    }
  end
end

#ls(prefix = '/') ⇒ Object



19
20
21
22
23
24
25
# File 'lib/awful/ssm.rb', line 19

def ls(prefix = '/')
  filters = [ { key: :Name, option: :BeginsWith, values: [ prefix.sub(/^(\w)/, '/\1') ] } ]
  ssm.describe_parameters(parameter_filters: filters).each do |response|
    response.parameters.each { |p| puts p.name }
    sleep 0.1               # this api will throttle easily
  end
end

#path(path) ⇒ Object



39
40
41
42
43
44
# File 'lib/awful/ssm.rb', line 39

def path(path)
  cmd = options[:show] ? ->(p) { puts "#{p.name} #{p.value}" } : ->(p) { puts p.name }
  ssm.get_parameters_by_path(path: path, with_decryption: options[:decrypt], recursive: options[:recursive]).each do |response|
    response.parameters.each(&cmd.method(:call))
  end
end

#put(name, value) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/awful/ssm.rb', line 51

def put(name, value)
  ssm.put_parameter(
    name:        name,
    value:       value,
    description: options[:description],
    type:        options[:type],
    key_id:      options[:key_id],
    overwrite:   options[:overwrite],
  )
rescue Aws::SSM::Errors::ParameterAlreadyExists => e
  error(e.message)
end

#shell_script(*instance_ids) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/awful/ssm.rb', line 135

def shell_script(*instance_ids)
  ssm.send_command(
    instance_ids: instance_ids,
    comment: options[:comment],
    timeout_seconds: options[:timeout],
    output_s3_bucket_name: options[:bucket],
    output_s3_key_prefix: options[:prefix],
    document_name: 'AWS-RunShellScript',
    parameters: {
      commands: options[:commands]
    }
  ).output do |response|
    puts response.command.command_id
  end
end