Class: Shards::Db

Inherits:
Base
  • Object
show all
Includes:
Meta
Defined in:
lib/shards/db.rb

Instance Attribute Summary collapse

Attributes included from Meta

#config

Attributes inherited from Base

#subdomain_param

Instance Method Summary collapse

Methods included from Meta

#add, #custom_yaml, #default_yaml, #get_config, #method_list, #output, #print_output, #proxy, #proxy_user, #reset_output, #run, #ssh_conn, #test_env?, #yaml_file

Methods inherited from Base

#default_region, method_list, #set_config

Constructor Details

#initialize(stage:, dns: nil) ⇒ Db

Returns a new instance of Db.



19
20
21
22
23
24
25
26
27
# File 'lib/shards/db.rb', line 19

def initialize stage:, dns: nil
  set_config
  @stage=stage
  @dns=dns
  @databases_on_servers=[]
  @server_databases={}

 reset_output
end

Instance Attribute Details

#blank_dbObject

Returns the value of attribute blank_db.



15
16
17
# File 'lib/shards/db.rb', line 15

def blank_db
  @blank_db
end

#connObject



114
115
116
# File 'lib/shards/db.rb', line 114

def conn
  @conn || default_connection
end

#database_to_dropObject

Returns the value of attribute database_to_drop.



15
16
17
# File 'lib/shards/db.rb', line 15

def database_to_drop
  @database_to_drop
end

#databases_on_serversObject (readonly)

Returns the value of attribute databases_on_servers.



16
17
18
# File 'lib/shards/db.rb', line 16

def databases_on_servers
  @databases_on_servers
end

#dnsObject (readonly)

Returns the value of attribute dns.



16
17
18
# File 'lib/shards/db.rb', line 16

def dns
  @dns
end

#server=(value) ⇒ Object (writeonly)

Sets the attribute server

Parameters:

  • value

    the value to set the attribute server to.



17
18
19
# File 'lib/shards/db.rb', line 17

def server=(value)
  @server = value
end

#server_databasesObject (readonly)

Returns the value of attribute server_databases.



16
17
18
# File 'lib/shards/db.rb', line 16

def server_databases
  @server_databases
end

#stageObject (readonly)

Returns the value of attribute stage.



16
17
18
# File 'lib/shards/db.rb', line 16

def stage
  @stage
end

Instance Method Details

#blank_db_connectionObject



128
129
130
# File 'lib/shards/db.rb', line 128

def blank_db_connection
  shard.list[blank_db || stage.get('blank_db')]
end

#create(dryrun: true) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/shards/db.rb', line 45

def create dryrun: true
  if dryrun
    dryrun_message
  else
    setting_up_database_message
    print_output
    create_script
    upload!
    ssh!
  end
  print_output
end

#create_scriptObject



37
38
39
40
41
42
43
# File 'lib/shards/db.rb', line 37

def create_script
  init_script
  creating_database
  dumping_database
  inserting_data
  write_file
end

#database_serverObject



92
93
94
95
96
# File 'lib/shards/db.rb', line 92

def database_server
  server_databases.each_pair.map do |server,db|
    db.product [server]
  end.flatten(1).sort
end

#dropObject



118
119
120
121
122
123
124
125
126
# File 'lib/shards/db.rb', line 118

def drop

  raise "database_to_drop is nil" if database_to_drop.nil?

  ssh_conn do |ssh|
    add(ssh.exec! drop_command)
  end

end

#exist?(searched = nil) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
# File 'lib/shards/db.rb', line 108

def exist? searched=nil
  searched||=name
  show
  databases_on_servers.include? searched
end

#parse_databases_on_servers(ssh_output) ⇒ Object



104
105
106
# File 'lib/shards/db.rb', line 104

def parse_databases_on_servers ssh_output
  @server_databases[server]=ssh_output.split("\n").reject { |x| x.include?"Warning" }
end

#run_ssh(ssh, command, message) ⇒ Object



69
70
71
72
# File 'lib/shards/db.rb', line 69

def run_ssh ssh, command, message
  puts message
  ssh.exec! command
end

#servers_to_dropObject



98
99
100
101
# File 'lib/shards/db.rb', line 98

def servers_to_drop
  raise "database_to_drop is nil" if database_to_drop.nil?
  server_databases.select { |k,v| v.include?database_to_drop }.keys
end

#showObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/shards/db.rb', line 74

def show
  @databases_on_servers=[]
  @server_databases={}

  ssh_conn do |ssh|
    shard.servers.each do |s|
      @server=s
      parse_databases_on_servers( ssh.exec! show_command )
    end
  end
  @server_databases.values.each do |databases|
    @databases_on_servers+=databases
  end

  @databases_on_servers=databases_on_servers.uniq

end

#ssh!Object



62
63
64
65
66
67
# File 'lib/shards/db.rb', line 62

def ssh!
  ssh_conn do |ssh|
    run_ssh ssh, chmod_file_command, chmod_file_message
    run_ssh ssh, db_creation_command, db_creation_message
  end
end

#upload!Object



58
59
60
# File 'lib/shards/db.rb', line 58

def upload!
  Net::SCP.upload! proxy, proxy_user, file, file
end

#write_fileObject



29
30
31
32
33
34
35
# File 'lib/shards/db.rb', line 29

def write_file
  add " "
  open(file, 'w+') { |fsh|
    fsh.puts output
  }
  reset_output
end