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.
@@ -0,0 +1,57 @@
1
+ require 'shards/meta/list'
2
+
3
+ module Shards
4
+
5
+ module Meta
6
+
7
+ attr_reader :config
8
+
9
+ def get_config class_name
10
+ yaml_file[class_name]
11
+ end
12
+
13
+ def run var
14
+ eval( config[var.to_s] )
15
+ end
16
+
17
+ def yaml_file
18
+ ENV['FORMAT_METHODS_FILE'].nil? ? default_yaml : default_yaml.merge(custom_yaml)
19
+ end
20
+
21
+ def default_yaml
22
+ YAML.load_file File.join(Shards::LIB_PATH,'settings','format_methods.yaml')
23
+ end
24
+
25
+ def custom_yaml
26
+ YAML.load_file ENV['FORMAT_METHODS_FILE']
27
+ end
28
+
29
+ def add text
30
+ @output << text
31
+ end
32
+
33
+ def output
34
+ @output.join("\n")
35
+ end
36
+
37
+ def print_output
38
+ puts output unless test_env?
39
+ reset_output
40
+ end
41
+
42
+ def reset_output
43
+ @output=[' ']
44
+ end
45
+
46
+ def test_env?
47
+ ENV['TEST'] || false
48
+ end
49
+
50
+ def method_list class_name
51
+ get_config(class_name).keys
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+
@@ -0,0 +1,45 @@
1
+ require "git"
2
+
3
+ module Shards
4
+
5
+ class Repo
6
+
7
+ @@times = 0
8
+ attr_accessor :git, :client, :root
9
+
10
+ def initialize
11
+ @root=ENV['ENGINEERING_ROOT_PATH']
12
+ @git=Git.open root
13
+ clean if @@times<1
14
+ end
15
+
16
+ def live_branch
17
+ ENV['LIVE_BRANCH'] || 'v5'
18
+ end
19
+
20
+ def clean
21
+ @@times+=1
22
+ git.reset_hard
23
+ git.clean(force: true, d: true)
24
+ git.branch(live_branch).checkout
25
+ git.pull('origin',live_branch)
26
+ end
27
+
28
+ def autocommit
29
+ git.add all: true
30
+ git.commit message
31
+ git.push 'origin', live_branch
32
+ end
33
+
34
+ def message
35
+ "[AUTOCOMMIT] Added shard for #{client}"
36
+ end
37
+
38
+ def diff
39
+ git.add all: true
40
+ git.diff
41
+ end
42
+
43
+ end
44
+ end
45
+
@@ -0,0 +1,64 @@
1
+ require 'shards/base_yaml_object'
2
+
3
+ module Shards
4
+
5
+ class Shard < BaseYamlObject
6
+
7
+ method_list('Shards::Shard').each do |m|
8
+ define_method(m.to_sym) { run __method__ }
9
+ end
10
+
11
+ attr_accessor :domain, :db, :dns
12
+
13
+ def shard_list_key
14
+ ENV['SHARD_LIST_KEY_ROOT_PATH'].split.map do |k|
15
+ "['#{k}']"
16
+ end.join
17
+ end
18
+
19
+ def rest
20
+ @rest||=clean_basename
21
+ end
22
+
23
+ def clean_basename
24
+ shard_rest=list.clone
25
+ shard_rest.delete(basename)
26
+ shard_rest
27
+ end
28
+
29
+ def add_domain domain
30
+ @domain=domain
31
+ @dns=Shards::Dns.new stage: stage, domain: domain
32
+ @db=Shards::Db.new stage: stage, dns: dns
33
+ end
34
+
35
+ def connection
36
+ { name => db.connection }
37
+ end
38
+
39
+ def add_connection
40
+ rest.merge! connection
41
+ @rest=@rest.sort.to_h
42
+ end
43
+
44
+ def merged_connection
45
+ base.merge rest
46
+ end
47
+
48
+ def update_yaml
49
+ eval "@yaml#{shard_list_key.to_s}=merged_connection"
50
+ end
51
+
52
+ def write_domain
53
+ add_connection
54
+ update_yaml
55
+ write_yaml
56
+ end
57
+
58
+ def exist?
59
+ list.keys.include? name
60
+ end
61
+
62
+ end
63
+ end
64
+
@@ -0,0 +1,31 @@
1
+ require 'shards/base_yaml_object'
2
+
3
+ module Shards
4
+
5
+ class Site < BaseYamlObject
6
+
7
+ attr_accessor :client
8
+
9
+ method_list('Shards::Site').each do |m|
10
+ define_method(m.to_sym) { run __method__ }
11
+ end
12
+
13
+ def add_site dryrun: false
14
+
15
+ add_data
16
+
17
+ if dryrun
18
+ add_site_message
19
+ else
20
+ write_yaml
21
+ end
22
+
23
+ end
24
+
25
+ def exist?
26
+ !yaml.select { |x| x['domain']==domain }.empty?
27
+ end
28
+
29
+ end
30
+ end
31
+
@@ -0,0 +1,64 @@
1
+ require 'shards/shard'
2
+ require 'shards/site'
3
+ require 'shards/db'
4
+ require 'shards/dns'
5
+ require 'shards/base'
6
+
7
+ module Shards
8
+
9
+ class Stage < Shards::Base
10
+
11
+ include Shards::Meta
12
+
13
+ method_list('Shards::Stage').each do |m|
14
+ define_method(m.to_sym) { run __method__ }
15
+ end
16
+
17
+ attr_accessor :name, :location_name, :shard
18
+
19
+ def initialize name, data, location_name
20
+
21
+ set_config
22
+ @name=name
23
+ @location_name=location_name
24
+ @variables = data
25
+ @output=[]
26
+
27
+ end
28
+
29
+ def shard
30
+ @shard||=Shards::Shard.new self
31
+ @shard
32
+ end
33
+
34
+ def add_domain domain, dryrun: false
35
+
36
+ shard.add_domain domain
37
+
38
+ if dryrun
39
+ add_domain_message
40
+ else
41
+ shard.write_domain
42
+ end
43
+
44
+ end
45
+
46
+ def get var_name
47
+ @variables.has_key?(var_name) ? @variables[var_name] : send(var_name)
48
+ end
49
+
50
+ def config_name
51
+ config_replace[name] || name
52
+ end
53
+
54
+ def config_replace
55
+ ENV['CONFIG_REPLACE'] ? eval(ENV['CONFIG_REPLACE']) : {}
56
+ end
57
+
58
+ def vars
59
+ @variables.keys
60
+ end
61
+
62
+ end
63
+ end
64
+
@@ -1,3 +1,3 @@
1
1
  module Shards
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.2"
3
3
  end
@@ -0,0 +1,177 @@
1
+ require 'shards/config'
2
+
3
+ module Shards
4
+
5
+ module Workflow
6
+
7
+ class Base
8
+
9
+ attr_accessor :yaml, :config, :flow, :pointer, :step, :answer, :pointer_history
10
+
11
+ def initialize config
12
+ @config=config
13
+ config.repo.clean
14
+ @yaml= YAML.load_file file
15
+ @pointer_history = []
16
+ end
17
+
18
+ def file
19
+ ENV['WORKFLOW_YAML_FILE'] || default_file
20
+ end
21
+
22
+ def default_file
23
+ File.join Shards.root_path, %w(lib settings workflow.yaml)
24
+ end
25
+
26
+ def start wf=nil
27
+ wf||=self.class.name.split('::').last.downcase
28
+ @flow=yaml[wf]
29
+ @pointer=flow.keys.first
30
+ run_step
31
+ end
32
+
33
+ def run_step
34
+
35
+ @step=flow[pointer]
36
+ @pointer_history << pointer
37
+ @answer=nil
38
+
39
+ begin
40
+ send pointer
41
+ @pointer=step['correct']
42
+ rescue => e
43
+ puts e.message
44
+ @pointer=step['wrong']
45
+ end
46
+
47
+ @step=flow[pointer]
48
+
49
+ run_step if @step['correct']!='finish'
50
+
51
+ end
52
+
53
+ def ask_question
54
+ puts step['question']
55
+ @answer=$stdin.gets.chomp
56
+ end
57
+
58
+ def format_options title, array
59
+ puts title
60
+ puts
61
+ array.each_index.map do |k|
62
+ puts "\t#{k}) #{array[k]}"
63
+ end
64
+ end
65
+
66
+ def ask_for_exit
67
+ exit
68
+ end
69
+
70
+ def choose_one title, array
71
+ format_options(title, array)
72
+ ask_question
73
+ validate_in_index answer, array
74
+ end
75
+
76
+ def validate_min_size text, min
77
+ condition= text.size < min
78
+ wrong_text= "'%s' is shorter than '%d' characters" % [text, min]
79
+ validation condition, wrong_text
80
+ end
81
+
82
+ def validate_regex text, regex
83
+ condition= text !~ /#{regex}/
84
+ wrong_text = "'%s' has a wrong format" % text
85
+ validation condition, wrong_text
86
+ end
87
+
88
+ def validate_in_index text, array
89
+ condition= array.at(text.to_i).nil?
90
+ wrong_text = text
91
+ validation condition, wrong_text
92
+ end
93
+
94
+ def validation condition, wrong_text
95
+ raise_wrong_text wrong_text if condition
96
+ end
97
+
98
+ def raise_wrong_text wrong_text
99
+ raise @step['wrong_message'] % wrong_text
100
+ end
101
+
102
+ def previous_pointer
103
+ @pointer_history.at(-2)
104
+ end
105
+
106
+ def ask_for_repeat
107
+ ask_question
108
+ if answer.strip.downcase!='r'
109
+ config.repo.clean
110
+ raise step['wrong_message']
111
+ end
112
+ @pointer=previous_pointer
113
+ run_step
114
+
115
+ end
116
+
117
+ def check_shard_presence_in_shards
118
+ stage.add_domain domain, dryrun: true
119
+ raise_wrong_text stage.shard.name if stage.shard.exist?
120
+ end
121
+
122
+ def check_database_presence_in_server
123
+ raise_wrong_text [stage.shard.db.name,stage.shard.db.server] if stage.shard.db.exist?
124
+ end
125
+
126
+ def check_domain_dns_presence
127
+ raise_wrong_text stage.shard.dns.host if stage.shard.dns.exist?
128
+ end
129
+
130
+ def check_domain_presence_in_sites
131
+ @site = Shards::Site.new stage
132
+ site.client = client
133
+ raise_wrong_text site.domain if site.exist?
134
+ end
135
+
136
+ def write_yaml_files
137
+ stage.add_domain domain
138
+ site.add_site
139
+ puts config.repo.diff
140
+ ask_question
141
+ raise step['wrong_message'] if answer.strip.downcase!='y'
142
+ end
143
+
144
+ def commit_changes
145
+ puts config.repo.autocommit
146
+ end
147
+
148
+ def create_database
149
+ stage.shard.db.create
150
+ message_params= [stage.shard.db.name, stage.shard.db.server]
151
+ raise_wrong_text message_params unless stage.shard.db.exist?
152
+ puts step['message'] % message_params
153
+ end
154
+
155
+ def clean_repo_and_exit
156
+ puts config.repo.clean
157
+ end
158
+
159
+ def dns_upsert
160
+ stage.shard.dns.r53upsert
161
+ raise_wrong_text host unless stage.shard.dns.exist?
162
+ puts step['message'] % host
163
+ end
164
+
165
+ def host
166
+ stage.shard.dns.host
167
+ end
168
+
169
+ def ask_for_exit_and_restart
170
+ ask_question
171
+ raise step['wrong_message'] if answer.strip.downcase!='r'
172
+ end
173
+
174
+ end
175
+
176
+ end
177
+ end