Class: Docman::Application
Instance Attribute Summary collapse
Attributes inherited from Command
#type
Class Method Summary
collapse
Instance Method Summary
collapse
-
#build(deploy_target_name, state, options = false) ⇒ Object
-
#deploy(deploy_target_name, name, type, version, options = false) ⇒ Object
-
#drush(env, site, command) ⇒ Object
-
#environment(name) ⇒ Object
-
#execute(action, state, name = nil, tag = nil) ⇒ Object
-
#force? ⇒ Boolean
-
#info(command, file, options = false) ⇒ Object
-
#info_recursive(info, command) ⇒ Object
-
#init(name, repo, options) ⇒ Object
-
#initialize ⇒ Application
constructor
A new instance of Application.
-
#template(name, options = false) ⇒ Object
-
#with_rescue(write_to_file = true) ⇒ Object
-
#write_environment(env, name) ⇒ Object
-
#write_state(state) ⇒ Object
Methods included from Context
#describe
Methods included from Logging
#log, logger, #logger, #prefix, #properties_info, #with_logging
Methods inherited from Command
#add_action, #add_actions, create, #describe, #perform, #prefix, register_command, #replace_placeholder, #run_actions, #run_with_hooks
Constructor Details
Returns a new instance of Application.
45
46
47
48
49
50
51
|
# File 'lib/application.rb', line 45
def initialize
@workspace_dir = Dir.pwd
@config = Docman::Config.new(File.join(Pathname(__FILE__).dirname.parent, 'config', 'config.yaml'))
@force = false
@commit_count = 0
end
|
Instance Attribute Details
#commit_count ⇒ Object
Returns the value of attribute commit_count.
40
41
42
|
# File 'lib/application.rb', line 40
def commit_count
@commit_count
end
|
#config ⇒ Object
Returns the value of attribute config.
39
40
41
|
# File 'lib/application.rb', line 39
def config
@config
end
|
#deploy_target ⇒ Object
Returns the value of attribute deploy_target.
40
41
42
|
# File 'lib/application.rb', line 40
def deploy_target
@deploy_target
end
|
#docroot_config ⇒ Object
Returns the value of attribute docroot_config.
39
40
41
|
# File 'lib/application.rb', line 39
def docroot_config
@docroot_config
end
|
#force ⇒ Object
Returns the value of attribute force.
40
41
42
|
# File 'lib/application.rb', line 40
def force
@force
end
|
#options ⇒ Object
Returns the value of attribute options.
40
41
42
|
# File 'lib/application.rb', line 40
def options
@options
end
|
Class Method Details
.bin ⇒ Object
240
241
242
|
# File 'lib/application.rb', line 240
def self.bin
File.join root, 'bin'
end
|
.lib ⇒ Object
244
245
246
|
# File 'lib/application.rb', line 244
def self.lib
File.join root, 'lib'
end
|
.root ⇒ Object
232
233
234
|
# File 'lib/application.rb', line 232
def self.root
Pathname(__FILE__).dirname.parent
end
|
Instance Method Details
#build(deploy_target_name, state, options = false) ⇒ Object
78
79
80
81
82
83
84
85
86
|
# File 'lib/application.rb', line 78
def build(deploy_target_name, state, options = false)
with_rescue do
@options = options
@deploy_target = @config['deploy_targets'][deploy_target_name]
@deploy_target['name'] = deploy_target_name
@docroot_config = DocrootConfig.new(@workspace_dir, deploy_target)
execute('build', state, nil, options['tag'])
end
end
|
#deploy(deploy_target_name, name, type, version, options = false) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/application.rb', line 88
def deploy(deploy_target_name, name, type, version, options = false)
result = nil
with_rescue do
@options = options
@deploy_target = @config['deploy_targets'][deploy_target_name]
raise "Wrong deploy target: #{deploy_target_name}" if @deploy_target.nil?
@deploy_target['name'] = deploy_target_name
@docroot_config = DocrootConfig.new(@workspace_dir, deploy_target)
@docroot_config.states_dependin_on(name, version).keys.each do |state|
execute('deploy', state, name)
write_environment(@deploy_target['states'][state], name)
write_state state
result = state
end
end
result
end
|
#drush(env, site, command) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/application.rb', line 119
def drush(env, site, command)
with_rescue(false) do
cmd = "drush env: '#{env}', site: '#{site}', '#{command}'"
log cmd
path = Dir.pwd
branch = 'commands'
current_branch = GitUtil.branch
GitUtil.exec("fetch")
have_branch = Exec.do("git ls-remote --exit-code . origin/#{branch} &> /dev/null")
log have_branch
if have_branch
GitUtil.exec("checkout #{branch}")
GitUtil.exec("pull origin #{branch}")
else
GitUtil.exec("checkout --orphan #{branch}")
GitUtil.exec("rm --cached -r .", false)
GitUtil.exec("clean -f -d", false)
end
File.open(File.join(path, 'commands'), 'a') {|f| f.puts cmd}
GitUtil.exec("add commands")
GitUtil.exec("commit -m 'Added command'")
GitUtil.exec("push origin #{branch}")
GitUtil.exec("checkout #{current_branch}")
end
end
|
#environment(name) ⇒ Object
236
237
238
|
# File 'lib/application.rb', line 236
def environment(name)
@config['environments'][name]
end
|
#execute(action, state, name = nil, tag = nil) ⇒ Object
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/application.rb', line 217
def execute(action, state, name = nil, tag = nil)
params = Marshal.load(Marshal.dump(@deploy_target))
params['state'] = state
params['action'] = action
params['name'] = name
params['tag'] = tag ? tag : state + '-' + Time.now.strftime("%Y-%m-%d-%H-%M-%S")
params['environment'] = @config['environments'][@deploy_target['states'][state]]
params['environment_name'] = @deploy_target['states'][state]
Docman::Deployers::Deployer.create(params, nil, self).perform
end
|
#force? ⇒ Boolean
228
229
230
|
# File 'lib/application.rb', line 228
def force?
@force or @options[:force]
end
|
#info(command, file, options = false) ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/application.rb', line 145
def info(command, file, options = false)
result = {}
with_rescue(false) do
@docroot_config = DocrootConfig.new(@workspace_dir, deploy_target)
if (command == 'full')
result['states'] = Docman::Application.instance.config['deploy_targets']['git_target']['states']
result['environments'] = Docman::Application.instance.config['environments']
projects = {}
info = @docroot_config.structure
@docroot_config.chain(info).values.each do |item|
projects.merge! info_recursive(item, command)
end
result['projects'] = projects
else
info = @docroot_config.structure
@docroot_config.chain(info).values.each do |item|
result.merge! info_recursive(item, command)
end
end
end
File.open(file, 'w') {|f| f.write result.to_json}
result
end
|
#info_recursive(info, command) ⇒ Object
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/application.rb', line 170
def info_recursive(info, command)
result = {}
case command
when 'list'
result[info['name']] = info['repo'] if info.key?('repo')
when 'full'
info_clone = info.clone
info_clone.delete('docroot_config')
info_clone.delete('children')
info_clone.delete('parent')
info_clone.delete('root')
result[info['name']] = info_clone
end
info['children'].each do |child|
result.merge! info_recursive(child, command)
end
result
end
|
#init(name, repo, options) ⇒ Object
53
54
55
56
57
58
59
60
|
# File 'lib/application.rb', line 53
def init(name, repo, options)
branch = options['branch'] ? options['branch'] : 'master'
`mkdir #{name}`
Dir.chdir name
GitUtil.clone_repo(repo, 'config', 'branch', branch, true, 1)
end
|
#template(name, options = false) ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/application.rb', line 106
def template(name, options = false)
with_rescue(false) do
@options = options
@docroot_config = DocrootConfig.new(@workspace_dir, nil)
project = @docroot_config.project(name)
unless project['template'].nil?
Dir.chdir project['full_build_path']
Exec.do "#{Application::bin}/project-template.sh #{project['template']}"
log "Project had been initialized with template: #{project['template']}"
end
end
end
|
#with_rescue(write_to_file = true) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/application.rb', line 62
def with_rescue(write_to_file = true)
failed_filepath = File.join(@workspace_dir, 'failed')
if File.file?(failed_filepath)
log 'Last operation failed, forced rebuild mode'
FileUtils.rm_f failed_filepath
@force = true
end
yield
rescue Exception => e
log "Operation failed: #{e.message}", 'error'
if write_to_file
File.open(failed_filepath, 'w') {|f| f.write(e.message) }
end
raise e
end
|
#write_environment(env, name) ⇒ Object
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
# File 'lib/application.rb', line 194
def write_environment(env, name)
environment = environment(env)
properties = {}
properties['ENV'] = env
unless environment.nil?
unless environment['previous'].nil?
unless environment['previous'][name].nil?
properties['project_last_result'] = environment['previous'][name]['result'] unless environment['previous'][name]['result'].nil?
unless environment['previous'][name]['context'].nil?
properties['temp_path'] = environment['previous'][name]['context']['temp_path'] unless environment['previous'][name]['context']['temp_path'].nil?
end
end
end
end
properties['last_project'] = name
filepath = File.join(@workspace_dir, 'last_deploy.properties')
File.open(filepath, 'w') do |file|
properties.each {|key, value| file.puts "#{key}=#{value}\n" }
end
end
|
#write_state(state) ⇒ Object
189
190
191
192
|
# File 'lib/application.rb', line 189
def write_state state
filepath = File.join(@workspace_dir, 'state')
File.open(filepath, 'w') { |file| file.write(state) }
end
|