Class: Shards::Shard

Inherits:
BaseYamlObject show all
Defined in:
lib/shards/shard.rb

Instance Attribute Summary collapse

Attributes inherited from BaseYamlObject

#internal_host, #stage, #yaml

Attributes inherited from Base

#subdomain_param

Attributes included from Meta

#config

Instance Method Summary collapse

Methods inherited from BaseYamlObject

#command_to_read_remote, #command_to_remote, #copy_file_to_extra_dirs, #dir, #file, #file_path, #fingerprint_command, #fingerprints, #initialize, #local_fingerprint, #proxy_read_remote_file, #remote_file, #remote_fingerprint, #same_fingerprint?, #ssh!, #target_config_dir

Methods inherited from Base

#default_region, method_list, #set_config

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

Constructor Details

This class inherits a constructor from Shards::BaseYamlObject

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



11
12
13
# File 'lib/shards/shard.rb', line 11

def database
  @database
end

#dbObject

Returns the value of attribute db.



11
12
13
# File 'lib/shards/shard.rb', line 11

def db
  @db
end

#dnsObject

Returns the value of attribute dns.



11
12
13
# File 'lib/shards/shard.rb', line 11

def dns
  @dns
end

#domainObject

Returns the value of attribute domain.



11
12
13
# File 'lib/shards/shard.rb', line 11

def domain
  @domain
end

#name_paramObject



64
65
66
# File 'lib/shards/shard.rb', line 64

def name_param
  @name_param || name
end

#server=(value) ⇒ Object (writeonly)

Sets the attribute server

Parameters:

  • value

    the value to set the attribute server to.



12
13
14
# File 'lib/shards/shard.rb', line 12

def server=(value)
  @server = value
end

Instance Method Details

#add_connectionObject



40
41
42
43
# File 'lib/shards/shard.rb', line 40

def add_connection
  rest.merge! connection
  @rest=@rest.sort.to_h
end

#add_domain(domain) ⇒ Object



30
31
32
33
34
# File 'lib/shards/shard.rb', line 30

def add_domain domain
  @domain=domain
  @dns=Shards::Dns.new stage: stage, domain: domain
  @db=Shards::Db.new stage: stage, dns: dns
end

#by_databaseObject



85
86
87
88
# File 'lib/shards/shard.rb', line 85

def by_database
  raise "database is nil" if database.nil?
  list.select { |k,v| v['database']==database['database'] }
end

#by_server(server) ⇒ Object



113
114
115
# File 'lib/shards/shard.rb', line 113

def by_server server
  list.values.select { |v| v['host']==server }.first
end

#clean_basenameObject



24
25
26
27
28
# File 'lib/shards/shard.rb', line 24

def clean_basename
  shard_rest=list.clone
  shard_rest.delete(basename)
  shard_rest
end

#connectionObject



36
37
38
# File 'lib/shards/shard.rb', line 36

def connection
  { name => db.connection }
end

#count_by_databaseObject



90
91
92
# File 'lib/shards/shard.rb', line 90

def count_by_database
  by_database.keys.count
end

#databases_by_host(server) ⇒ Object



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

def databases_by_host server
  list.values.select { |x| x['host']==server }.map { |x| x['database'] }.uniq
end

#deleteObject



72
73
74
75
76
77
# File 'lib/shards/shard.rb', line 72

def delete
  raise "#{name_param} doesn't exist" unless exist?
  @database=list[name_param]
  delete_in_yaml
  write_yaml
end

#delete_in_yamlObject



79
80
81
82
83
# File 'lib/shards/shard.rb', line 79

def delete_in_yaml
  list_to_delete=list.clone
  list_to_delete.delete name_param
  eval "@yaml#{shard_list_key.to_s}=list_to_delete"
end

#exist?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/shards/shard.rb', line 68

def exist?
  list.keys.include? name_param
end

#merged_connectionObject



45
46
47
# File 'lib/shards/shard.rb', line 45

def merged_connection
  base.merge rest
end

#password_by_server(server) ⇒ Object



121
122
123
# File 'lib/shards/shard.rb', line 121

def password_by_server server
  by_server(server)['password']
end

#restObject



20
21
22
# File 'lib/shards/shard.rb', line 20

def rest
  @rest||=clean_basename
end

#server_databasesObject



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

def server_databases
  servers.map do |s|
    [s,databases_by_host(s).count ]
  end.to_h
end

#server_with_min_databasesObject



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

def server_with_min_databases
  min=server_databases.values.min
  server_databases.invert[min]
end

#serversObject



94
95
96
# File 'lib/shards/shard.rb', line 94

def servers
  list.values.map { |x| x['host'] }.uniq
end

#shard_list_keyObject



14
15
16
17
18
# File 'lib/shards/shard.rb', line 14

def shard_list_key
  ENV['SHARD_LIST_KEY_ROOT_PATH'].split.map do |k|
    "['#{k}']"
  end.join
end

#update_yamlObject



49
50
51
# File 'lib/shards/shard.rb', line 49

def update_yaml
  eval "@yaml#{shard_list_key.to_s}=merged_connection"
end

#username_by_server(server) ⇒ Object



117
118
119
# File 'lib/shards/shard.rb', line 117

def username_by_server server
  by_server(server)['username']
end

#write_domainObject



53
54
55
56
57
# File 'lib/shards/shard.rb', line 53

def write_domain
  add_connection
  update_yaml
  write_yaml
end

#write_yamlObject



59
60
61
62
# File 'lib/shards/shard.rb', line 59

def write_yaml
  super
  write_extra_config_dirs stage
end