Top Level Namespace
Defined Under Namespace
Modules: Synchronize
Classes: Utils
Constant Summary
collapse
- RAILS_ROOT =
Dir.pwd
Instance Method Summary
collapse
Instance Method Details
#check_aws_env ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/synchronize/tasks.rb', line 87
def check_aws_env
unless ENV['AWS_ACCESS_KEY_ID']
raise " - Environment variable AWS_ACCESS_KEY_ID not set."
end
unless ENV['AWS_SECRET_ACCESS_KEY']
raise " - Environment variable AWS_SECRET_ACCESS_KEY not set."
end
unless ENV['AWS_BUCKET']
raise " - Enviroment variable AWS_BUCKET not set."
end
puts " - AWS env variables present."
end
|
#check_aws_knife ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/synchronize/tasks.rb', line 107
def check_aws_knife
aws_data = get_databag('aws','buckets')
unless aws_data['s3_backup_bucket']
raise " - Could not get s3_backup_bucket parameter from chef databag aws"
end
aws_data = get_databag('aws','keys')
unless aws_data['aws_access_key_id']
raise " - Could not get aws_access_key_id parameter from chef databag aws"
end
unless aws_data['aws_secret_access_key']
raise " - Could not get aws_secret_access_key parameter from chef databag aws"
end
end
|
#check_aws_option ⇒ Object
103
104
105
|
# File 'lib/synchronize/tasks.rb', line 103
def check_aws_option
@aws_option = true if (@options.aws_accesskey && @options.aws_accessid && @options.aws_bucket)
end
|
#check_database_yml ⇒ Object
134
135
136
137
138
139
|
# File 'lib/synchronize/tasks.rb', line 134
def check_database_yml
unless File.exists?("#{RAILS_ROOT}/config/database.yml")
raise "database.yml not found, are you running synchronize on your RAILS_ROOT directory?"
end
puts " - Found database.yml config file"
end
|
#check_duplicity ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/synchronize/tasks.rb', line 55
def check_duplicity
unless `duplicity -V`
raise " - duplicity not installed or not in PATH. You can install it manually or run synchronize dependencies to install through brew"
else
puts " - Found duplicity command."
end
end
|
#check_environment ⇒ Object
“Tests the existence of the environment variables required for backups tasks”
#check_knife_env ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/synchronize/tasks.rb', line 63
def check_knife_env
if ! ENV['KNIFE_CONFIG']
@knife_option = false
puts " - KNIFE_CONFIG variable or --knife parameter has not been set, falling back to AWS command line options or environment variables."
elsif ENV['KNIFE_CONFIG'] && ! File.exists?(ENV['KNIFE_CONFIG'])
raise " - KNIFE_CONFIG env variable exists but is not pointing to a proper knife config file, please point it to your knife config file."
else
puts " - knife properly configured."
@knife_option = true
end
end
|
#check_knife_option ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/synchronize/tasks.rb', line 75
def check_knife_option
@knife_option = true
if ! @options.knife
@knife_option = false
elsif @options.knife && ! File.exists?(@options.knife)
raise " - --knife is not pointing to a proper knife config file, please point it to your knife config file."
else
puts " - knife properly configured."
@knife_option = true
end
end
|
#check_mysql_option ⇒ Object
141
142
143
|
# File 'lib/synchronize/tasks.rb', line 141
def check_mysql_option
@mysql_option = true if @options.database
end
|
#check_rails_env ⇒ Object
127
128
129
130
131
132
|
# File 'lib/synchronize/tasks.rb', line 127
def check_rails_env
unless ENV['RAILS_ENV']
raise " - Neither environment variable RAILS_ENV nor --rails_env parameter has been set."
end
puts " - RAILS_ENV variable present."
end
|
#check_rails_option ⇒ Object
123
124
125
|
# File 'lib/synchronize/tasks.rb', line 123
def check_rails_option
@rails_option = true if @options.rails_env
end
|
#duplicity_restore ⇒ Object
3
4
5
6
7
|
# File 'lib/synchronize/assets.rb', line 3
def duplicity_restore
puts " - Restoring assets from s3"
system("duplicity -verror --no-encryption --force restore s3+http://#{@bucket}/projects/#{@app_name}/files/#{@app_name} #{RAILS_ROOT}/public/system/")
puts $? != 0 ? "Assets restore unsuccessful." : "Assets restore successful."
end
|
#get_app_name ⇒ Object
2
3
4
|
# File 'lib/synchronize/tasks.rb', line 2
def get_app_name
@app_name = @rails_env == "development" ? RAILS_ROOT.split('/').last : RAILS_ROOT.split('/')[2]
end
|
#get_aws_credentials ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/synchronize/tasks.rb', line 19
def get_aws_credentials
if @knife_option
puts " - Obtaining AWS credentials from knife."
aws_data = get_databag('aws','keys')
@aws_access_key_id = aws_data['aws_access_key_id']
@aws_secret_access_key = aws_data['aws_secret_access_key']
aws_data = get_databag('aws','buckets')
@bucket = aws_data['s3_backup_bucket']
ENV['AWS_ACCESS_KEY_ID'] = @aws_access_key_id
ENV['AWS_SECRET_ACCESS_KEY'] = @aws_secret_access_key
elsif @aws_option
@bucket = @options.aws_bucket
@aws_access_key_id = @options.aws_accessid
@aws_secret_access_key = @options.aws_accesskey
ENV['AWS_ACCESS_KEY_ID'] = @aws_access_key_id
ENV['AWS_SECRET_ACCESS_KEY'] = @aws_secret_access_key
else
@bucket = ENV['AWS_BUCKET']
@aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
@aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
end
end
|
#get_databag(databag, key) ⇒ Object
14
15
16
17
|
# File 'lib/synchronize/tasks.rb', line 14
def get_databag(databag, key)
aws_yml = `unset BUNDLE_GEMFILE; cd /; knife data bag show #{databag} #{key} -F yml -c #{@knife_config}`
YAML.load(aws_yml)
end
|
#get_knife_config ⇒ Object
10
11
12
|
# File 'lib/synchronize/tasks.rb', line 10
def get_knife_config
@knife_config = @options.knife ? @options.knife : ENV['KNIFE_CONFIG']
end
|
#get_latest_s3_dump ⇒ Object
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/synchronize/db.rb', line 3
def get_latest_s3_dump
puts " - Downloading #{@app_name} S3 dump."
connection = Fog::Storage.new(
:provider => 'AWS',
:aws_secret_access_key => @aws_secret_access_key,
:aws_access_key_id => @aws_access_key_id
)
directory = connection.directories.get(@bucket)
s3_file_url = "projects/#{@app_name}/dbdumps/#{@production_db}/latest.gz"
s3_file = directory.files.get(s3_file_url)
File.open("#{RAILS_ROOT}/#{@production_db}.gz", "w+") do |local_file|
local_file.write(s3_file.body)
end
end
|
#get_mysql_credentials ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/synchronize/db.rb', line 20
def get_mysql_credentials
if @mysql_option
@db_user = @options.mysql_user || 'root'
@db_pass = @options.mysql_pass
@db_host = @options.mysql_host || '127.0.0.1'
@db_name = @options.database
else
credentials = Utils.load_yaml_file("#{RAILS_ROOT}/config/database.yml")
@db_user = credentials[@rails_env]['username'] || 'root'
@db_pass = credentials[@rails_env]['password']
@db_host = credentials[@rails_env]['host'] || '127.0.0.1'
@db_name = credentials[@rails_env]['database']
end
end
|
#get_production_db_name ⇒ Object
57
58
59
|
# File 'lib/synchronize/db.rb', line 57
def get_production_db_name
@production_db = "jorgesancha_#{@app_name}_production"
end
|
#get_rails_env ⇒ Object
6
7
8
|
# File 'lib/synchronize/tasks.rb', line 6
def get_rails_env
@rails_env = @options.rails_env ? @options.rails_env : ENV['RAILS_ENV']
end
|
#install_dependencies ⇒ Object
Installs the command line tools required for synchronize.
46
47
48
49
50
51
52
53
|
# File 'lib/synchronize/tasks.rb', line 46
def install_dependencies
puts " - Installing dependencies"
`brew install python`
`/usr/local/share/python/easy_install pip`
`/usr/local/share/python/pip install boto`
`brew install duplicity`
puts " - Synchronize dependencies installed"
end
|
#load_database ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/synchronize/db.rb', line 42
def load_database
puts " - Loading #{@app_name} dump into the database."
command_pre = "mysql -u #{@db_user} -h #{@db_host} "
command_in = @db_pass ? "-p#{@db_pass} " : String.new
command_post = "-e \"DROP DATABASE IF EXISTS #{@db_name}; CREATE DATABASE #{@db_name}\""
system(command_pre + command_in + command_post)
command_post = "#{@db_name} < #{RAILS_ROOT}/#{@production_db}"
system(command_pre + command_in + command_post)
puts $? != 0 ? " - There was a problem loading database #{@app_name}." : " - Database #{@app_name} sucessfully loaded."
end
|
#rm_dump ⇒ Object
61
62
63
|
# File 'lib/synchronize/db.rb', line 61
def rm_dump
`rm -f #{RAILS_ROOT}/#{@production_db}`
end
|
#unzip_database ⇒ Object
35
36
37
38
39
40
|
# File 'lib/synchronize/db.rb', line 35
def unzip_database
puts
puts " - Unzipping #{@app_name} dump."
`gunzip -f #{RAILS_ROOT}/#{@production_db}.gz`
puts " - #{@app_name} dump sucessfully unzipped."
end
|