aws_helper 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -31,6 +31,10 @@ Prune so only keep 7 snapshots:
31
31
 
32
32
  aws_helper snap_prune /dev/sda1 --snapshots_to_keep=7
33
33
 
34
+ Email me a list of the latest 20 snapshots:
35
+
36
+ aws_helper snap_email me@company.com ebs.backups@company.com mysmtpemailserver.com
37
+
34
38
  ## Complex Usage
35
39
 
36
40
  If your server does not have a role then you need to code the AWS keys which is not best practice:
@@ -42,12 +46,19 @@ Snapshot EBS attached to device /dev/sdf volume vol-123456 access AWS through an
42
46
  export HTTP_PROXY=http://myproxy:port
43
47
  aws_helper snap /dev/sdf vol-123456 --description zzzzzzzzz
44
48
 
45
- Prune so only keep 7 snapshots:
49
+ Prune so only keep 20 snapshots:
50
+
51
+ export AWS_ACCESS_KEY_ID ='xxxxxxxxxxxx'
52
+ export AWS_SECRET_ACCESS_KEY ='yyyyyyyy'
53
+ export HTTP_PROXY=http://myproxy:port
54
+ aws_helper snap_prune /dev/sdf vol-123456 --snapshots_to_keep=20
55
+
56
+ Email me a list of the latest 30 snapshots with a subject title on email:
46
57
 
47
58
  export AWS_ACCESS_KEY_ID ='xxxxxxxxxxxx'
48
59
  export AWS_SECRET_ACCESS_KEY ='yyyyyyyy'
49
60
  export HTTP_PROXY=http://myproxy:port
50
- aws_helper snap_prune /dev/sdf vol-123456 --snapshots_to_keep=7
61
+ aws_helper snap_email me@company.com ebs.backups@company.com mysmtpemailserver.com 'My EBS Backups' --rows=30
51
62
 
52
63
  Other functions to follow
53
64
 
data/lib/awshelper/cli.rb CHANGED
@@ -2,6 +2,7 @@ require 'thor'
2
2
  require 'awshelper'
3
3
  require 'awshelper/ec2'
4
4
  require 'syslog'
5
+ require 'net/smtp'
5
6
 
6
7
  module Awshelper
7
8
  class CLI < Thor
@@ -80,7 +81,7 @@ option :description
80
81
  long_desc <<-LONGDESC
81
82
  'snap DEVICE [VOLUME_ID] --description xxxxxx'
82
83
  \x5 Take a snapshot of a EBS Disk by specifying device and/or volume_id.
83
- \x5 All commands rely on environment variables
84
+ \x5 All commands rely on environment variables or the server having an IAM role
84
85
  \x5 export AWS_ACCESS_KEY_ID ='xxxxxxxxxx'
85
86
  \x5 export AWS_SECRET_ACCESS_KEY ='yyyyyy'
86
87
  \x5 For example
@@ -102,7 +103,7 @@ option :snapshots_to_keep, :type => :numeric, :required => true
102
103
  long_desc <<-LONGDESC
103
104
  'snap_prune DEVICE [VOLUME_ID] --snapshots_to_keep=<numeric>'
104
105
  \x5 Prune the number of snapshots of a EBS Disk by specifying device and/or volume_id and the no to keep.
105
- \x5 All commands rely on environment variables
106
+ \x5 All commands rely on environment variables or the server having an IAM role
106
107
  \x5 export AWS_ACCESS_KEY_ID ='xxxxxxxxxxxx'
107
108
  \x5 export AWS_SECRET_ACCESS_KEY ='yyyyyyyy'
108
109
  \x5 For example
@@ -129,6 +130,42 @@ def snap_prune(device, volume_id=nil)
129
130
  end
130
131
  end
131
132
 
133
+ desc "snap_email TO FROM EMAIL_SERVER", "Email Snapshot List."
134
+ option :rows, :type => :numeric, :required => false
135
+
136
+ long_desc <<-LONGDESC
137
+ 'snap_email TO FROM EMAIL_SERVER ['EBS Backups'] --rows=<numeric>''
138
+ \x5 Emails the last 20 snapshots from specific email address via the email_server.
139
+ \x5 All commands rely on environment variables or the server having an IAM role
140
+ \x5 export AWS_ACCESS_KEY_ID ='xxxxxxxxxxxx'
141
+ \x5 export AWS_SECRET_ACCESS_KEY ='yyyyyyyy'
142
+ \x5 For example
143
+ \x5 aws_helper snap_email me@mycompany.com ebs.backups@mycompany.com emailserver.com 'My EBS Backups' --rows=20
144
+ \x5 will email the list of the latest 20 snapshots to email address me@mycompany.com via email server emailserver.com
145
+ LONGDESC
146
+
147
+ def snap_email(to, from, email_server, subject='EBS Backups')
148
+ rows = 20
149
+ rows = options[:rows] if options[:rows]
150
+ message = ""
151
+ log("Report on snapshots")
152
+ # ({ Name="start-time", Values="today in YYYY-MM-DD"})
153
+ i = rows
154
+ ec2.describe_snapshots.sort { |a,b| b[:aws_started_at] <=> a[:aws_started_at] }.each do |snapshot|
155
+ if i >0
156
+ message = message+"#{snapshot[:aws_id]} #{snapshot[:aws_volume_id]} #{snapshot[:aws_started_at]} #{snapshot[:aws_description]} #{snapshot[:aws_status]}\n"
157
+ i = i-1
158
+ end
159
+ end
160
+ opts = {}
161
+ opts[:server] = email_server
162
+ opts[:from] = from
163
+ opts[:from_alias] = 'EBS Backups'
164
+ opts[:subject] = subject
165
+ opts[:body] = message
166
+ send_email(to,opts)
167
+ end
168
+
132
169
  private
133
170
 
134
171
  def log(message,type="info")
@@ -294,6 +331,26 @@ def detach_volume(volume_id, timeout)
294
331
  end
295
332
  end
296
333
 
334
+ def send_email(to,opts={})
335
+ opts[:server] ||= 'localhost'
336
+ opts[:from] ||= 'email@example.com'
337
+ opts[:from_alias] ||= 'Example Emailer'
338
+ opts[:subject] ||= "You need to see this"
339
+ opts[:body] ||= "Important stuff!"
340
+
341
+ msg = <<END_OF_MESSAGE
342
+ From: #{opts[:from_alias]} <#{opts[:from]}>
343
+ To: <#{to}>
344
+ Subject: #{opts[:subject]}
345
+
346
+ #{opts[:body]}
347
+ END_OF_MESSAGE
348
+ puts "Sending to #{to} from #{opts[:from]} email server #{opts[:server]}"
349
+ Net::SMTP.start(opts[:server]) do |smtp|
350
+ smtp.send_message msg, opts[:from], to
351
+ end
352
+ end
353
+
297
354
 
298
355
  end
299
356
 
@@ -1,3 +1,3 @@
1
1
  module Awshelper
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-25 00:00:00.000000000 Z
12
+ date: 2015-01-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: right_aws