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?, #write_extra_config_dirs, #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
28
# File 'lib/shards/db.rb', line 19

def initialize stage:, dns: nil
  set_config
  @stage=stage
  @dns=dns
  @databases_on_servers=[]
  @server_databases={}
  @time=Time.now.strftime("%Y_%m_%d__%H_%M_%S")

 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



152
153
154
# File 'lib/shards/db.rb', line 152

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

#base_dirObject



30
31
32
# File 'lib/shards/db.rb', line 30

def base_dir
  "/tmp/shards_tool/#{@time}"
end

#base_dir_create_commandObject



83
84
85
# File 'lib/shards/db.rb', line 83

def base_dir_create_command
  "mkdir -p #{base_dir}"
end

#base_dir_create_messageObject



75
76
77
# File 'lib/shards/db.rb', line 75

def base_dir_create_message
  "Creating #{base_dir} directory for db files"
end

#base_dir_delete_commandObject



87
88
89
# File 'lib/shards/db.rb', line 87

def base_dir_delete_command
  "rm -rf #{base_dir}"
end

#base_dir_delete_messageObject



79
80
81
# File 'lib/shards/db.rb', line 79

def base_dir_delete_message
  "Deleting #{base_dir} directory for db files"
end

#blank_db_connectionObject



166
167
168
# File 'lib/shards/db.rb', line 166

def blank_db_connection
  shard.list[stage.blank_db_shard_key]
end

#create(dryrun: true) ⇒ Object



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

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

#create_base_dirObject



91
92
93
# File 'lib/shards/db.rb', line 91

def create_base_dir
  system base_dir_create_command
end

#create_remote_base_dir!Object



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

def create_remote_base_dir!
  ssh_conn do |ssh|
     run_ssh ssh, base_dir_create_command, base_dir_create_message
  end
end

#create_scriptObject



42
43
44
45
46
47
48
49
# File 'lib/shards/db.rb', line 42

def create_script
  create_base_dir
  init_script
  creating_database
  dumping_database
  inserting_data
  write_file
end

#database_serverObject



130
131
132
133
134
# File 'lib/shards/db.rb', line 130

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

#delete_base_dirObject



95
96
97
# File 'lib/shards/db.rb', line 95

def delete_base_dir
  system base_dir_delete_command
end

#dropObject



156
157
158
159
160
161
162
163
164
# File 'lib/shards/db.rb', line 156

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)


146
147
148
149
150
# File 'lib/shards/db.rb', line 146

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

#parse_databases_on_servers(ssh_output) ⇒ Object



142
143
144
# File 'lib/shards/db.rb', line 142

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



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

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

#servers_to_dropObject



136
137
138
139
# File 'lib/shards/db.rb', line 136

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/shards/db.rb', line 112

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



99
100
101
102
103
104
105
# File 'lib/shards/db.rb', line 99

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

#upload!Object



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

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

#write_fileObject



34
35
36
37
38
39
40
# File 'lib/shards/db.rb', line 34

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