shards 0.1.0 → 1.0.2

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/lib/shards.rb CHANGED
@@ -1,5 +1,14 @@
1
- require "shards/version"
2
-
3
1
  module Shards
4
- # Your code goes here...
2
+
3
+ LIB_PATH = File.dirname(__FILE__)
4
+
5
+ def self.root_path
6
+
7
+ self::LIB_PATH[0..-4]
8
+
9
+ end
10
+
5
11
  end
12
+
13
+ require "shards/version"
14
+ require "shards/client"
@@ -0,0 +1,25 @@
1
+ require "shards/meta/meta"
2
+
3
+ module Shards
4
+
5
+ class Base
6
+
7
+ include Shards::Meta
8
+
9
+ class << self
10
+
11
+ def method_list class_name
12
+
13
+ l=Shards::Meta::List.new
14
+ l.names class_name
15
+ end
16
+
17
+ end
18
+
19
+ def set_config
20
+ @config=get_config self.class.name
21
+ end
22
+
23
+ end
24
+ end
25
+
@@ -0,0 +1,34 @@
1
+ require "shards/base"
2
+
3
+ module Shards
4
+
5
+ class BaseYamlObject < Shards::Base
6
+
7
+ attr_reader :stage, :yaml
8
+
9
+ def initialize stage
10
+ set_config
11
+ @stage=stage
12
+ @yaml= YAML.load_file file
13
+ @output=[]
14
+ end
15
+
16
+ def dir
17
+ stage.get 'config_dir'
18
+ end
19
+
20
+ def file
21
+ file_path ENV['BASE_CONFIG_FILE']
22
+ end
23
+
24
+ def file_path file_name
25
+ File.join(ENV['ENGINEERING_ROOT_PATH'],ENV['CONFIG_FILES_ROOT_PATH'], dir ,file_name)
26
+ end
27
+
28
+ def write_yaml
29
+ File.write file, YAML.dump(yaml)
30
+ end
31
+
32
+ end
33
+ end
34
+
@@ -0,0 +1,118 @@
1
+ require 'shards/workflow/workflow'
2
+ require 'thor'
3
+ require 'terminal-table'
4
+
5
+ module Shards
6
+
7
+ class Client < Thor
8
+
9
+ desc 'create LOCATION STAGE CLIENT DOMAIN', 'Fast creation interactive shards creation tool'
10
+
11
+ def create location, stage, client, domain
12
+
13
+ fast_terminal=Shards::Workflow::FastTerminal.new config
14
+ fast_terminal.params= { client: client, domain: domain, location: location, stage: stage }
15
+ fast_terminal.start
16
+
17
+ end
18
+
19
+ desc 'terminal', 'Run interactive shards creation tool in the terminal'
20
+
21
+ def terminal
22
+ t=Shards::Workflow::Terminal.new config
23
+ t.start
24
+ end
25
+
26
+ desc 'locations', 'Locations list'
27
+
28
+ def locations
29
+
30
+ rows=config.locations.each_pair.map do |k, v|
31
+ [k , v.stages.keys.join(' ') ]
32
+ end
33
+
34
+ columns %w(Locations Stages), rows
35
+
36
+ end
37
+
38
+
39
+ desc "list LOCATION STAGE", "Shard list by LOCATION and STAGE"
40
+
41
+ def list(location, stage)
42
+
43
+ s = get_stage location, stage
44
+
45
+ rows=s.shard.list.each_pair.map do |k,v|
46
+ [ k, v['database'], v['host'] ]
47
+ end
48
+
49
+ columns %w(Shard Database Host), rows
50
+
51
+ end
52
+
53
+ desc "databases LOCATION STAGE", "List databases in server by LOCATION and STAGE"
54
+
55
+ def databases(location, stage)
56
+
57
+ s = get_stage location, stage
58
+ s.shard.add_domain 'fakedomain'
59
+ db=s.shard.db
60
+ db.show
61
+
62
+ rows=db.databases_on_server.map do |v|
63
+ [ v ]
64
+ end
65
+
66
+ columns %w(Database), rows
67
+
68
+ end
69
+
70
+
71
+ desc "dns LOCATION STAGE DOMAIN", "Check if dns record is available for a domain"
72
+
73
+ def dns(location, stage, domain)
74
+
75
+ s = get_stage location, stage
76
+ s.shard.add_domain domain
77
+ dns=s.shard.dns
78
+
79
+ puts "Host: #{dns.host} , exists: #{dns.exist?}"
80
+
81
+ end
82
+
83
+
84
+ desc "sites LOCATION STAGE", "Site list by LOCATION and STAGE"
85
+
86
+ def sites(location, stage)
87
+
88
+ s = get_stage location, stage
89
+
90
+ site=Shards::Site.new s
91
+
92
+ rows=site.yaml.map do |x|
93
+ [ x['name'], x['subdomain'], x['shard'] ]
94
+ end
95
+
96
+ columns %w(Name Subdomain Shard), rows
97
+
98
+ end
99
+
100
+ private
101
+
102
+ def config
103
+ Shards::Config.new
104
+ end
105
+
106
+ def columns headings, rows
107
+ table = Terminal::Table.new headings: headings, rows: rows
108
+ puts table
109
+ end
110
+
111
+ def get_stage location, stage
112
+ l= config.locations[location]
113
+ l.stages[stage]
114
+ end
115
+
116
+ end
117
+
118
+ end
@@ -0,0 +1,54 @@
1
+ require 'dotenv'
2
+ require 'yaml'
3
+ require 'shards/location'
4
+ require 'shards/repo'
5
+
6
+ module Shards
7
+
8
+ class Config
9
+
10
+ ENV_VARS = %w(ENGINEERING_ROOT_PATH CONFIG_FILE PROXY_SUBDOMAIN DOMAIN CONFIG_FILES_ROOT_PATH SHARDS_FILE_NAME SITES_FILE_NAME CONFIG_DIR_SUFFIX DNS_ZONE_CODE)
11
+
12
+ attr_accessor :locations, :settings_file, :repo
13
+
14
+ def initialize settings_file='settings.env', origin_file=nil
15
+
16
+ @settings_file=settings_file
17
+
18
+ Dotenv.load(settings_file)
19
+
20
+ ENV_VARS.each { |variable| validate variable }
21
+
22
+ @repo=Shards::Repo.new
23
+
24
+ @locations={}
25
+
26
+ data=YAML.load_file(origin_file || File.join(repo.root,config_file) )
27
+
28
+ add_locations data
29
+
30
+ end
31
+
32
+ def add_locations data
33
+ data['Locations'].each_pair do |name,location_data|
34
+ @locations[name]=Location.new( name, location_data)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def validate variable
41
+ raise LoadError.new "#{variable} #{message}" unless ENV.has_key?(variable)
42
+ end
43
+
44
+ def message
45
+ "not found in #{settings_file}"
46
+ end
47
+
48
+ def config_file
49
+ ENV['CONFIG_FILE']
50
+ end
51
+
52
+ end
53
+ end
54
+
data/lib/shards/db.rb ADDED
@@ -0,0 +1,92 @@
1
+ require 'shards/base'
2
+ require 'net/ssh'
3
+ require 'net/scp'
4
+
5
+ module Shards
6
+
7
+ class Db < Shards::Base
8
+
9
+ include Shards::Meta
10
+
11
+ method_list('Shards::Db').each do |m|
12
+ define_method(m.to_sym) { run __method__ }
13
+ end
14
+
15
+ attr_reader :stage, :dns, :databases_on_server
16
+
17
+ def initialize stage:, dns:
18
+ set_config
19
+ @stage=stage
20
+ @dns=dns
21
+ reset_output
22
+ end
23
+
24
+ def write_file
25
+ add " "
26
+ open(file, 'w+') { |fsh|
27
+ fsh.puts output
28
+ }
29
+ reset_output
30
+ end
31
+
32
+ def create_script
33
+ init_script
34
+ creating_database
35
+ dumping_database
36
+ inserting_data
37
+ write_file
38
+ end
39
+
40
+ def create dryrun: true
41
+ if dryrun
42
+ dryrun_message
43
+ else
44
+ setting_up_database_message
45
+ print_output
46
+ create_script
47
+ upload!
48
+ ssh!
49
+ end
50
+ print_output
51
+ end
52
+
53
+ def upload!
54
+ Net::SCP.upload! proxy, proxy_user, file, file
55
+ end
56
+
57
+ def ssh!
58
+ ssh_conn do |ssh|
59
+ run_ssh ssh, chmod_file_command, chmod_file_message
60
+ run_ssh ssh, db_creation_command, db_creation_message
61
+ end
62
+ end
63
+
64
+ def run_ssh ssh, command, message
65
+ puts message
66
+ ssh.exec! command
67
+ end
68
+
69
+ def show
70
+ ssh_conn do |ssh|
71
+ parse_databases_on_server( ssh.exec! show_command )
72
+ end
73
+ end
74
+
75
+ def ssh_conn
76
+ Net::SSH.start(proxy, proxy_user) do |ssh|
77
+ yield ssh
78
+ end
79
+ end
80
+
81
+ def parse_databases_on_server ssh_output
82
+ @databases_on_server=ssh_output.split("\n").reject { |x| x.include?"Warning" }
83
+ end
84
+
85
+ def exist?
86
+ show
87
+ databases_on_server.include? name
88
+ end
89
+
90
+ end
91
+ end
92
+
data/lib/shards/dns.rb ADDED
@@ -0,0 +1,56 @@
1
+ require "aws-sdk-route53"
2
+ require 'shards/base'
3
+
4
+ module Shards
5
+
6
+ class Dns < Shards::Base
7
+
8
+ method_list('Shards::Dns').each do |m|
9
+ define_method(m.to_sym) { run __method__ }
10
+ end
11
+
12
+ attr_reader :stage, :domain
13
+ attr_accessor :aws_route53_resp, :r53
14
+
15
+ def initialize stage:, domain:
16
+ set_config
17
+ @stage=stage
18
+ @domain=domain
19
+ reset_output
20
+ @r53 = Aws::Route53::Client.new()
21
+ end
22
+
23
+ def set dryrun: true
24
+ if dryrun
25
+ dryrun_message
26
+ else
27
+ creating_route53_message
28
+ print_output
29
+ set_aws_route53_record
30
+ end
31
+ print_output
32
+ end
33
+
34
+ def set_aws_route53_record
35
+ @aws_route53_resp = r53.change_resource_record_sets r53upsert
36
+ end
37
+
38
+ def exist?
39
+ !record_list.empty?
40
+ end
41
+
42
+ def record_list
43
+ record_sets.select { |x| x.name.include? host }
44
+ end
45
+
46
+ def record_sets
47
+ r53.list_resource_record_sets({
48
+ hosted_zone_id: zone,
49
+ start_record_name: host,
50
+ max_items: 20
51
+ }).resource_record_sets
52
+ end
53
+
54
+ end
55
+ end
56
+
@@ -0,0 +1,29 @@
1
+ require 'shards/stage'
2
+
3
+ module Shards
4
+
5
+ class Location
6
+
7
+ attr_accessor :name, :stages
8
+
9
+ def initialize location_name, data
10
+
11
+ @name=location_name
12
+
13
+ @stages = {}
14
+
15
+ add_stages data
16
+
17
+ end
18
+
19
+ private
20
+
21
+ def add_stages data
22
+ data['stages'].each_pair do |stage_name, stage_data|
23
+ @stages[stage_name]= Stage.new( stage_name, stage_data, name )
24
+ end
25
+ end
26
+
27
+ end
28
+ end
29
+
@@ -0,0 +1,15 @@
1
+ module Shards
2
+ module Meta
3
+
4
+ class List
5
+
6
+ include Shards::Meta
7
+
8
+ def names class_name
9
+ config= get_config class_name
10
+ config.keys
11
+ end
12
+ end
13
+
14
+ end
15
+ end