Class: Docman::Application

Inherits:
Command
  • Object
show all
Includes:
Context, Singleton
Defined in:
lib/application.rb

Instance Attribute Summary collapse

Attributes inherited from Command

#type

Class Method Summary collapse

Instance Method Summary collapse

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

#initializeApplication

Returns a new instance of Application.



41
42
43
44
45
46
# File 'lib/application.rb', line 41

def initialize
  # TODO: Define workspace properly
  @workspace_dir = Dir.pwd
  @config = Docman::Config.new(File.join(Pathname(__FILE__).dirname.parent, 'config', 'config.yaml'))
  @force = false
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



35
36
37
# File 'lib/application.rb', line 35

def config
  @config
end

#deploy_targetObject

Returns the value of attribute deploy_target.



36
37
38
# File 'lib/application.rb', line 36

def deploy_target
  @deploy_target
end

#docroot_configObject (readonly)

Returns the value of attribute docroot_config.



35
36
37
# File 'lib/application.rb', line 35

def docroot_config
  @docroot_config
end

#forceObject

Returns the value of attribute force.



36
37
38
# File 'lib/application.rb', line 36

def force
  @force
end

#optionsObject

Returns the value of attribute options.



36
37
38
# File 'lib/application.rb', line 36

def options
  @options
end

Class Method Details

.binObject



165
166
167
# File 'lib/application.rb', line 165

def self.bin
  File.join root, 'bin'
end

.libObject



169
170
171
# File 'lib/application.rb', line 169

def self.lib
  File.join root, 'lib'
end

.rootObject



157
158
159
# File 'lib/application.rb', line 157

def self.root
  Pathname(__FILE__).dirname.parent
end

Instance Method Details

#build(deploy_target_name, state, options = false) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/application.rb', line 73

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/application.rb', line 83

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

#environment(name) ⇒ Object



161
162
163
# File 'lib/application.rb', line 161

def environment(name)
  @config['environments'][name]
end

#execute(action, state, name = nil, tag = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/application.rb', line 142

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

Returns:

  • (Boolean)


153
154
155
# File 'lib/application.rb', line 153

def force?
  @force or @options[:force]
end

#init(name, repo, options) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/application.rb', line 48

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)
  #Dir.chdir File.join(name, 'config')
  #`git checkout #{branch} & git branch -u origin #{branch}`
end

#template(name, options = false) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/application.rb', line 101

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/application.rb', line 57

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/application.rb', line 119

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



114
115
116
117
# File 'lib/application.rb', line 114

def write_state state
  filepath = File.join(@workspace_dir, 'state')
  File.open(filepath, 'w') { |file| file.write(state) }
end