Class: Heroku::Command::Rds

Inherits:
BaseWithApp
  • Object
show all
Defined in:
lib/heroku/commands/rds.rb

Overview

manage Amazon RDS instances

Defined Under Namespace

Classes: RdsProxy

Instance Method Summary collapse

Instance Method Details

#accessObject

rds:access

displays current ingress access settings



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/heroku/commands/rds.rb', line 90

def access
  data = Array.new
  rds.security_groups.all.each do |data, group|
    group.ec2_security_groups.each do |group_access|
      data << [group.id, group_access['EC2SecurityGroupName'] + ' @ ' + group_access['EC2SecurityGroupOwnerId'], group_access['Status']]
    end
    group.ip_ranges.each do |ip_range|
      data << [group.id, ip_range['CIDRIP'], ip_range['Status']]
    end
    data
  end
  begin
    require 'hirb'
    puts Hirb::Helpers::AutoTable.render(data, :headers => ['Security Group', 'IP Range/Security Group', 'Status'])
  rescue LoadError
    data.unshift ['SECURITY GROUP', 'IP RANGE / SECURITY GROUP', 'STATUS']
    lengths = (0..2).map { |i| data.map { |d| d[i].length }.max }
    puts data.map { |d| '%-*s  %-*s  %-*s' % [lengths[0], d[0], lengths[1], d[1], lengths[2], d[2]] }.join("\n")
  end
end

#dumpObject

rds:dump [FILE]

Download a database dump, bzipped and saved locally

-f, –force # allow overwriting existing file

if no FILE is specified, appname-date.sql.bz2 is used by default. if name of FILE does not end in .sql.bz2, it will be added automatically.



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
# File 'lib/heroku/commands/rds.rb', line 29

def dump
  check_dependencies('mysqldump', 'bzip2', '/bin/sh')
  options = {}
  while arg = args.shift
    case arg
    when '-f', '--force'
      options[:force] = true
    when /^[^-]/
      raise CommandFailed, "too many arguments passed" if options[:filename]
      options[:filename] = arg
    else
      raise CommandFailed, "unsupported option: #{arg}"
    end
  end

  options[:filename] ||= "#{app}-#{Time.now.strftime('%Y-%m-%d')}.sql.bz2"
  options[:filename] += '.sql.bz2' unless options[:filename] =~ /\.sql(\.bz2)?$/
  options[:filename] += '.bz2' unless options[:filename] =~ /\.bz2$/

  if File.exists?(options[:filename]) && !options[:force]
    raise CommandFailed, "file already exists. use --force to override."
  end

  exec('/bin/sh', '-c',
       "mysqldump --compress --single-transaction #{args_to_s(mysql_args(database_uri))}" +
       pv_pipe +
       %{| bzip2 > '#{options[:filename]}'})
end

#importObject

rds:import FILE

uploads a local database dump into the remote databse

supports gzipped, bzipped, and uncompressed sql dumps



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/heroku/commands/rds.rb', line 155

def import
  check_dependencies('mysql', '/bin/sh')
  source = args.shift or raise CommandFailed, "You must specify a file to import from"
  File.readable?(source) or raise CommandFailed, "#{source.inspect} is not readable"
  command = case source
            when /\.bz2$/
              check_dependencies('bzcat')
              'bzcat'
            when /\.gz$/
              check_dependencies('gunzip')
              'gunzip -c'
            else
              check_dependencies('cat')
              'cat'
            end

  display "This will replace the #{app} database with #{source}!"
  exit unless dangerous_prompt

  exec('/bin/sh', '-c',
       "#{command} #{args_to_s(source)}" +
       pv_pipe +
       %{| mysql --compress } + args_to_s(mysql_args(database_uri)))
end

#indexObject

rds

Opens a MySQL console connected to the current database. Ingress access is required to run this command (use rds:ingress to grant access).



15
16
17
18
# File 'lib/heroku/commands/rds.rb', line 15

def index
  check_dependencies('mysql')
  exec *(['mysql', '--compress'] + mysql_args(database_uri))
end

#ingressObject

rds:ingress [IP] [SECURITY GROUP]

Authorize ingress access to a particular IP

  • if IP is not specified, your current IP will be used

  • if SECURITY GROUP is not specified, ‘default’ will be used

  • IP can also be a CIDR range



66
67
68
69
70
# File 'lib/heroku/commands/rds.rb', line 66

def ingress
  ip, security_group = parse_security_group_and_ip_from_args
  rds.authorize_db_security_group_ingress(security_group, 'CIDRIP' => ip)
  self.access
end

#pullObject

rds:pull [RAILS_ENV or DATABASE_URL]

downloads the remote database into a local database

If a RAILS_ENV or DATABASE_URL is not specified, the current development environment is used (as read from config/database.yml). This command will confirm before executing the transfer.



119
120
121
122
123
124
125
126
127
128
# File 'lib/heroku/commands/rds.rb', line 119

def pull
  check_dependencies('mysqldump', 'mysql', '/bin/sh')
  target = parse_db_location(args.shift || 'development')

  display "This will erase all data in the #{target['database'].inspect} database" +
    (target['host'].empty? ? '' : " on #{target['host']}") + "!"
  exit unless dangerous_prompt

  copy_db_to_db(database_uri, target)
end

#pushObject

rds:push [RAILS_ENV or DATABASE_URL]

uploads the local database into the remote database

If a RAILS_ENV or DATABASE_URL is not specified, the current development environment is used (as read from config/database.yml). This command will confirm before executing the transfer.



138
139
140
141
142
143
144
145
146
147
# File 'lib/heroku/commands/rds.rb', line 138

def push
  check_dependencies('mysqldump', 'mysql', '/bin/sh')
  source = parse_db_location(args.shift || 'development')

  display "This will replace the #{app} database with the #{source['database'].inspect} database" +
    (source['host'].empty? ? '' : " on #{source['host']}") + "!"
  exit unless dangerous_prompt

  copy_db_to_db(source, database_uri)
end

#revokeObject

rds:revoke [IP] [SECURITY GROUP]

Revokes previously-granted ingress access from a particular IP

  • if IP is not specified, your current IP will be used

  • if SECURITY GROUP is not specified, ‘default’ will be used

  • IP can also be a CIDR range



80
81
82
83
84
# File 'lib/heroku/commands/rds.rb', line 80

def revoke
  ip, security_group = parse_security_group_and_ip_from_args
  rds.revoke_db_security_group_ingress(security_group, 'CIDRIP' => ip)
  self.access
end