Commit 29ac438c6c4d3390fc209d2c369d2ba082d16735

morph exchange deployment
  
11class ReportsController < ApplicationController
22
33 def index
4 @reports = {:simple => "Previsto/Planejado com filtro por mês",
5 :summary => "Resumo do Mês atual, previsto, planejado, folgas, erros de cálculo",
6 :detailed => "Detalhes de cada previsão por Mês, realizações do previsto, erros de calculo, possíveis pontos de melhora",
7 :accounting => "Relatório contábil"}
48 end
59
610 def simple
  
11<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
22<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3<head>
4 <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
5 <title>Controle Financeiro e Planejamento</title>
6 <%= stylesheet_link_tag 'layout' %>
7 <%= stylesheet_link_tag 'themes/window/window' %>
8 <%= stylesheet_link_tag 'themes/window/leopard' %>
9 <%= javascript_include_tag :defaults, 'lowpro', 'prototype-ui', 'window', 'validators-en' %>
10 <%= calendar_date_select_includes :silver %>
11</head>
12<body>
13 <div id="menu">
14 <ul>
15 <li><%= link_to 'Painel', :controller => 'home' %></li>
16 <li><%= link_to 'Movimentações', movements_path %></li>
17 <li><%= link_to 'Créditos Agendados', scheduled_profits_path%></li>
18 <li><%= link_to 'Débitos Agendados', scheduled_debts_path %></li>
19 <li>Relatórios</li>
20 <li>Livro Caixa</li>
21 </ul>&nbsp;
22 </div>
23 <div style="clear:both;"></div>
24 <h1>Orçamento e Planejamento Financeiro</h1>
25<%= yield %>
26</body>
3 <head>
4 <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
5 <title>Controle Financeiro e Planejamento</title>
6 <%= stylesheet_link_tag 'layout' %>
7 <%= stylesheet_link_tag 'themes/window/window' %>
8 <%= stylesheet_link_tag 'themes/window/leopard' %>
9 <%= javascript_include_tag :defaults, 'lowpro', 'prototype-ui', 'window', 'validators-en' %>
10 <%= calendar_date_select_includes :silver %>
11 </head>
12 <body>
13 <div id="menu">
14 <ul>
15 <li><%= link_to 'Painel', :controller => 'home' %></li>
16 <li><%= link_to 'Movimentações', movements_path %></li>
17 <li><%= link_to 'Créditos Agendados', scheduled_profits_path%></li>
18 <li><%= link_to 'Débitos Agendados', scheduled_debts_path %></li>
19 <li><%= link_to 'Relatórios', :controller => 'reports' %></li>
20 <li><%= link_to 'Livro Caixa', :controller => 'reports', :action => 'accounting' %></li>
21 </ul>&nbsp;
22 </div>
23 <div style="clear:both;"></div>
24 <!--h1>Orçamento e Planejamento Financeiro</h1-->
25 <%= yield %>
26 </body>
2727</html>
  
1<h1>Reports#detailed</h1>
2<p>Find me in app/views/reports/detailed.html.erb</p>
1<ul>
2 <% @reports.each do |k,v| %>
3 <li><%= link_to v, :action => k %></li>
4 <% end %>
5</ul>
  
1<h1>Reports#index</h1>
2<p>Find me in app/views/reports/index.html.erb</p>
1<% window 'reports', 'Relatórios disponíveis' do %>
2 <ul>
3 <% @reports.each do |k,v| %>
4 <li><%= link_to v, :action => k %></li>
5 <% end %>
6 </ul>
7<% end %>
  
2828 m.resource :realization, map_res_options('confirmacao')
2929 end
3030
31
32 map.connect 'relatorios', :controller => 'reports'
33 map.connect 'relatorios/simples', :controller => 'reports', :action => 'simple'
34 map.connect 'relatorios/detalhes', :controller => 'reports', :action => 'detailed'
35 map.connect 'relatorios/resumo', :controller => 'reports', :action => 'summary'
36 map.connect 'relatorios/contabil', :controller => 'reports', :action => 'accounting'
37
3138 # The priority is based upon order of creation: first created -> highest priority.
3239
3340 # Sample of regular route:
  
1require 'fileutils'
2require 'cgi'
3require 'net/https'
4require 'yaml'
5require 'highline/import'
6load 'deploy'
7
8# The svn repository is used to export the code into the temporary directory before
9# uploading code into the Morph control panel. Currently only svn is supported,
10# but you could change it to fit your need by changing the get_code task
11set :repository, 'http://git.gitorious.org/planejamento/mainline.git' # Set here your repository! Example: 'https://dev.morphexchange.com/repos/ruby/trunk/control_panel/control_panel'
12set :repo_line_number, __LINE__ - 1 # Needed to report missing repository later on
13
14# The version name to set in the control panel. Defautls to date and time, but can be altered by passing it
15# on the command line as -s version_name='The Version Name'
16set :version_name, Time.now.utc.strftime('%Y-%m-%d %H:%M:%S')
17
18# If you want to use a different scm or use a different export method, you can chane it here
19# Please note that the export to directory is removed before the checkout starts. If
20# You want it to work differently, change the code in the get_code task
21set :deploy_via, :export
22set :scm, :git
23
24# MORPH SETTINGS, please do not change
25set :morph_host, "panel.morphexchange.com"
26set :morph_port, 443
27set :morph_tmp_dir, 'morph_tmp'
28set :mex_key, "95aec1655ff89061ed41fffb129110e4bf6b0d20"
29set :rmdir_cmd, PLATFORM.include?('mswin') ? 'rmdir /s /q' : 'rm -rf'
30set :mv_cmd, PLATFORM.include?('mswin') ? 'ren' : 'mv'
31set :morph_tmp_dir, 'morph_tmp'
32set :release_path, morph_tmp_dir # needed in order to generate the correct scm command
33set :get_code_using, :get_code
34
35
36namespace :morph do
37
38 # This is the entry point task. It gets the new code, then upload it into S3
39 # to a special folder used later for deployment. Finally it mark the version
40 # Uploaded to be deployed
41 task :deploy do
42 transaction do
43 update_code
44
45 conn_start do |http|
46 r = Net::HTTP::Post.new('/api/deploy/deploy')
47 add_mex_fields(r)
48 r.add_field('Content-length', '0')
49 response = http.request(r)
50 puts response.body
51 raise "Error - Could not deploy the application!" if !response.is_a?(Net::HTTPOK)
52 end
53 say("Done.")
54 end
55 end
56
57 # Specialized command to deploy from a packaged gem
58 task :deploy_from_gem do
59 set :get_code_using, :get_code_from_gem
60 deploy
61 end
62
63 # This task calls the get_code helper, then upload the code into S3
64 task :update_code do
65 transaction do
66 abort('You need a MeX key!') if mex_key.nil?
67
68 self.send get_code_using
69 say("*** Getting info for Morph authentication ***")
70 s3_data = ""
71
72 #Authenticate
73 is_auth = false
74 loop do
75 set(:morph_user, ask("Morph user: ")) if !exists?(:morph_user) || morph_user.nil?
76 set(:morph_pass, ask("Password: ") { |q| q.echo = false }) if !exists?(:morph_pass) || morph_pass.nil?
77
78 #make a request to MorphCP to get S3 auth
79 conn_start do |http|
80 r = Net::HTTP::Get.new('/api/s3/connection_data')
81 add_mex_fields(r)
82 response = http.request(r)
83 if !response.is_a?(Net::HTTPOK)
84 say("Authentication failed! Please try again.")
85 set :morph_user, nil
86 set :morph_pass, nil
87 else
88 s3_data = YAML.load(response.body)
89 is_auth = true
90 end
91 end
92 break if is_auth
93 end
94
95 abort('****** ERROR: Authentication with MorpExchange failed!') if !is_auth
96 #connect to s3
97 say("Uploading the code...")
98 conn_start(s3_data[:host]) do |http|
99 request = Net::HTTP::Put.new(s3_data[:path])
100 s3_data[:header].each {|k,v| request.add_field(k,v)}
101 #upload .tar file
102 raise "Error - Could not find archive to upload." unless File.exist?('code_update.tar.gz')
103 request.content_length = File.stat('code_update.tar.gz').size
104 File.open('code_update.tar.gz', 'rb') do |up_file|
105 request.body_stream = up_file
106 response = http.request(request)
107 raise "Could not upload the code!" if !response.is_a?(Net::HTTPOK) && !response.is_a?(Net::HTTPSuccess)
108 end
109 end
110 say("Creating new appspace version...")
111
112 #create a version in CP
113 conn_start do |http|
114 r = Net::HTTP::Post.new('/versions/create2')
115 add_mex_fields(r)
116 r.add_field('morph-version-name', version_name)
117 r.add_field('morph-version-s3-object', s3_data[:obj_name])
118 r.add_field('Content-length', '0')
119 response = http.request(r)
120 raise "Error - Could not create a new version!" if !response.is_a?(Net::HTTPOK)
121 end
122 say("Done.")
123
124 end
125 end
126
127 # A task that create a temp dir, export the code ouf ot svn and tar
128 # the code preparing it for upload.
129 # If you are not using svn, or using a different source control tool
130 # you can customize this file to work with it. The requirement is that
131 # It will export the whole structure into the temp directory as set in
132 # morph_tmp_dir.
133 #
134 # You can choose to release a different version than head by setting the
135 # Environment variable 'REL_VER' to the version to use.
136 task :get_code do
137 on_rollback do
138 system("#{rmdir_cmd} #{morph_tmp_dir} code_update.tar.gz")
139 end
140
141 # Make sure we have a repo to work from!
142 abort("***ERROR: Must specify the repository to check out from! See line #{repo_line_number} in #{__FILE__}.") if !repository
143
144 transaction do
145 # Clean up previous deploys
146 system("#{rmdir_cmd} #{morph_tmp_dir} code_update.tar.gz")
147
148 #get latest code from from the repository
149 say("Downloading the code from the repository...")
150 system(strategy.send(:command))
151 raise('Export from repository failed! Please check the repository setting at the start of the file') if $?.to_i != 0
152
153 # Verify that we have the expected rails structure
154 ['/app', '/public', '/config/environment.rb', '/lib'].each do |e|
155 raise "\n***ERROR: Rails directories are missing. Please make sure your set :repository is correct!\n" if !File.exist?("#{morph_tmp_dir}#{e}")
156 end
157
158 #create archive
159 system("tar -C #{morph_tmp_dir} -czf code_update.tar.gz --exclude='./.*' .")
160 raise('Failed to tar the file for upload.') if $?.to_i != 0
161
162 # Verify that we have the expected rails structure in the archive
163 flist = `tar -tzf code_update.tar.gz`
164 all_in = flist.include?('lib/') && flist.include?('app/') && flist.include?('config/environment.rb')
165 raise "\n***ERROR: code archive is missing the rails directories. Please check your checkout and tar\n" if !all_in
166
167 system("#{rmdir_cmd} \"#{morph_tmp_dir}\"")
168 end
169 end
170
171 # Get the code from a packaged gem. Name comes from a setting passed to the command
172 # using the -s form
173 task :get_code_from_gem do
174 # Make sure we have the gem defined and that we have the gem file
175 if !exists?(:gem_file) || gem_file.nil?
176 abort("***ERROR: The gem file must be provided on the command line using -s.\n For example: cap -f morph_deploy.rb -s gem_file=/home/morph/my_app.gem morph:deploy_from_gem")
177 end
178
179 abort("***ERROR: gem file not found! Please check the file location and try again") if !File.exists?(gem_file)
180
181 # Remove older file
182 system("#{rmdir_cmd} code_update.tar.gz")
183
184 # Extract the data.tar.gz code from the gem
185 system("tar xf #{gem_file} data.tar.gz")
186 abort("***ERROR: Couldn't fine the data.tar.gz file in the gem file provided!") if !File.exists?('data.tar.gz')
187
188 # rename it to upload_code.tar.gz
189 system("#{mv_cmd} data.tar.gz code_update.tar.gz")
190 end
191
192 # Helper to add the mex auth fields to the requests
193 def add_mex_fields(req)
194 req.add_field('morph-user', morph_user)
195 req.add_field('morph-pass', morph_pass)
196 req.add_field('morph-app', mex_key)
197 end
198
199 # Helper to create a connection with all the needed setting
200 # And yeals to the block
201 def conn_start host = morph_host, port = morph_port
202 con = Net::HTTP.new(host, port)
203 con.use_ssl = true
204 # If you have cert files to use, change this to OpenSSL::SSL::VERIFY_PEER
205 # And set the ca_cert of the connection
206 con.verify_mode = OpenSSL::SSL::VERIFY_NONE
207 con.start
208 if block_given?
209 begin
210 return yield(con)
211 ensure
212 con.finish
213 end
214 end
215 con
216 end
217
218end