15
16
17
18
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'app/business/aws_deploy/tasks/generate_production_cron_check_updates.rb', line 15
def generate_production_cron_check_updates
content =
"#!/usr/bin/env ruby \n" +
"# \n" +
"require \"fileutils\" \n" +
"require \"aws-sdk\" \n" +
" \n" +
"# ------------------------------------------------------------------------------------- \n" +
"# CONFIGURABLE \n" +
"# ------------------------------------------------------------------------------------- \n" +
" \n" +
" AWS.config(:access_key_id => \"#{@aws_access_key_id}\", :secret_access_key => \"#{@aws_secret_access_key}\") \n" +
" \n" +
"data_dir = File.expand_path File.dirname(__FILE__) \n" +
"file_pattern = \"#{@file_pattern}\" \n" +
"bucket = \"#{@aws_deploy_bucket}\" \n" +
" \n" +
"# \n" +
"# ------------------------------------------------------------------------------------- \n" +
" \n" +
"s3_filename = \"nofile\" \n" +
" \n" +
"puts \"------------------------------------------------------------------------------ \" \n" +
" \n" +
"if \"\#{ARGV[0]}\".empty? \n" +
" puts \"ERR - Process stoped. Environment not defined.\" \n" +
" \n" +
"else \n" +
" if !%w(#{envs_code()}).include?(\"\#{ARGV[0]}\") \n" +
" puts \"ERR - Invalid Environment: (\#{ARGV[0]})\" \n" +
" else \n" +
" \n" +
" s3_filename = \"\#{file_pattern}-\#{ARGV[0]}.zip\" \n" +
" s3_filename_md5 = \"\#{file_pattern}-\#{ARGV[0]}.md5\" \n" +
" \n" +
" \n" +
" # # puts \"------------------------------------------------------------------------------ \" \n" +
" puts \"1. Getting MD5 ... \#{bucket}/\#{s3_filename_md5}\" \n" +
" # # puts \"------------------------------------------------------------------------------ \" \n" +
" \n" +
" s3 = AWS::S3.new \n" +
" bucket = s3.buckets[\"\#{bucket}\"] \n" +
" \n" +
" obj = bucket.objects[s3_filename_md5] \n" +
" \n" +
" if obj.exists? \n" +
" File.delete(\"\#{data_dir}/\#{s3_filename_md5}\") if File.exist?(\"\#{data_dir}/\#{s3_filename_md5}\") \n" +
" \n" +
" File.open(\"\#{data_dir}/\#{s3_filename_md5}\", 'wb') do |file| \n" +
" obj.read do |chunk| \n" +
" file.write(chunk) \n" +
" end \n" +
" end \n" +
" \n" +
" \n" +
" # # puts \"------------------------------------------------------------------------------ \" \n" +
" puts \"2. Checking MD5 ... \" \n" +
" \n" +
" # md5 checking... \n" +
" md5_file_data = File.read(\"\#{data_dir}/\#{s3_filename_md5}\") \n" +
" \n" +
" if File.exist?(\"\#{data_dir}/\#{s3_filename_md5}.local\") \n" +
" md5_local = File.read(\"\#{data_dir}/\#{s3_filename_md5}.local\") \n" +
" else \n" +
" md5_local = \"new__\#{md5_file_data}\" \n" +
" end \n" +
" \n" +
" puts \" remote MD5: \#{md5_file_data}\" \n" +
" puts \" local MD5: \#{md5_local}\" \n" +
" \n" +
" \n" +
" # process the deployment... \n" +
" if md5_local != md5_file_data \n" +
" # # puts \"------------------------------------------------------------------------------ \" \n" +
" puts \"3. Downloading package \" \n" +
" \n" +
" obj = bucket.objects[s3_filename] \n" +
" \n" +
" if obj.exists? \n" +
" \n" +
" File.open(\"\#{data_dir}/\#{s3_filename}\", 'w+b') do |file| \n" +
" obj.read do |chunk| \n" +
" file.write(chunk) \n" +
" end \n" +
" end \n" +
" \n" +
" # # puts \"------------------------------------------------------------------------------ \" \n" +
" puts \"4. Writing controls ... .log and .md5.local files \" \n" +
" \n" +
" \n" +
" system(\"echo \\\"\#{Time.now.strftime(\"%y%m%d_%H%M%S\")}\\\" >> \#{data_dir}/\#{file_pattern}-\#{ARGV[0]}_YES.log\") \n" +
" # updating local MD5 \n" +
" File.open(\"\#{data_dir}/\#{s3_filename_md5}.local\", 'w+') { |file| file.write(\"\#{md5_file_data}\") } \n " +
" \n" +
" puts \"5. Deploying....\" \n" +
" system(\"\#{data_dir}/deploy.sh \#{ARGV[0]}\") \n " +
" \n" +
" else \n" +
" system(\"echo \\\"\#{Time.now.strftime(\"%y%m%d_%H%M%S\")}\\\" >> \#{data_dir}/\#{file_pattern}-\#{ARGV[0]}_NO.log\") \n" +
" end \n" +
" else \n" +
" system(\"echo \\\"\#{Time.now.strftime(\"%y%m%d_%H%M%S\")}\\\" >> \#{data_dir}/\#{file_pattern}-\#{ARGV[0]}_NO.log\") \n" +
" end \n" +
" else \n" +
" system(\"echo \\\"\#{Time.now.strftime(\"%y%m%d_%H%M%S\")}\\\" >> \#{data_dir}/\#{file_pattern}-\#{ARGV[0]}_NO.log\") \n" +
" end \n" +
" \n" +
" end \n" +
" \n" +
"end \n" +
"puts \" ------------------------------------------------------------------------------ \" \n"
File.delete("#{Rails.root}/script/production/cron_check_updates.rb") if File.exists?("#{Rails.root}/script/production/cron_check_updates.rb")
File.open("#{Rails.root}/script/production/cron_check_updates.rb", "w") do |f|
f.write("#{content}")
end
"#{Rails.root}/script/production/cron_check_updates.rb"
end
|