docman 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4bab1348bae18d45a114e6b4d6b09062941dea2b
4
- data.tar.gz: aea9b94d3dcc51c3c8434742656f8d088a31e5fa
3
+ metadata.gz: c523b74f00ef5c2e00b332f79f8e886bb8432991
4
+ data.tar.gz: 7690899373bc70b61271f3d376b829a547c33ec6
5
5
  SHA512:
6
- metadata.gz: f92686f1ef0f5da95066dfbf18ef3b6d784d00df281347126855cbca9ee9a919596ce1d0d6b67634d5f11e91d4ff06b817b7b5d95d82eb9eae96f9d6d467a309
7
- data.tar.gz: fe889237d4ca329320f242e9e86655f9e589b7186deb41ac39f2a091defc898298b2c374ea14a91dd7183c549d70db3b16db47d0571082412bda84f88ec22d56
6
+ metadata.gz: a87b66ae034745a1f126e727932fca79442367110d6afed95646dc925bdd3035699fcbc70922e2d0740a138dadba8d73ad2c6502a87da53858bd78fe1bb8a022
7
+ data.tar.gz: 6df3d08107aa0e8b1d624bdaa7c8c384abd3d409151c9a32070adf63b150f8177abeeee385aaa503e62dc029fa404618d3f62ea2a97177b2a98005e9da740e43
data/docman.gemspec CHANGED
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_dependency 'hash-deep-merge'
27
27
  spec.add_dependency 'net-sftp'
28
28
  spec.add_dependency 'hooks'
29
+ spec.add_dependency 'diffy'
29
30
  end
data/lib/application.rb CHANGED
@@ -65,7 +65,7 @@ module Docman
65
65
  end
66
66
 
67
67
  def execute(action, state, name = nil)
68
- params = @deploy_target
68
+ params = @deploy_target.clone
69
69
  params['state'] = state
70
70
  params['action'] = action
71
71
  params['name'] = name
@@ -22,7 +22,7 @@ module Docman
22
22
 
23
23
  def config
24
24
  super
25
- add_action('before_execute', {'type' => :clean_changed})
25
+ add_action('before_execute', {'type' => :clean_changed}, @context)
26
26
  end
27
27
 
28
28
  def validate_command
@@ -31,7 +31,6 @@ module Docman
31
31
  end
32
32
 
33
33
  before_execute do
34
- #TODO: rebuld if config changed.
35
34
  if @context.need_rebuild?
36
35
  log("Need rebuild")
37
36
  else
@@ -6,12 +6,14 @@ module Docman
6
6
 
7
7
  def execute
8
8
  execute_result = GitUtil.get(@context['repo'], @context['full_build_path'], @context.version_type, @context.version)
9
+ # No commit hash for 'root' as it will be changed later
9
10
  @context['type'] == 'root' ? @context['build_path'] : execute_result
10
11
  end
11
12
 
12
13
  def changed?
13
14
  stored_version = @context.stored_version['result']
14
- repo_version = GitUtil.get(@context['repo'], @context['full_build_path'], @context.version_type, @context.version)
15
+ # No commit hash for 'root' as it will be changed later
16
+ repo_version = @context['type'] == 'root' ? @context['build_path'] : GitUtil.get(@context['repo'], @context['full_build_path'], @context.version_type, @context.version)
15
17
  stored_version != repo_version
16
18
  end
17
19
 
@@ -4,15 +4,13 @@ module Docman
4
4
 
5
5
  register_builder :git_strip_builder
6
6
 
7
-
8
7
  def execute
9
8
  FileUtils.rm_r(@context['full_build_path']) if File.directory? @context['full_build_path']
10
- FileUtils.rm_r @context['temp_path'] if @context.need_rebuild? and File.directory? @context['temp_path']
9
+ # FileUtils.rm_r @context['temp_path'] if @context.need_rebuild? and File.directory? @context['temp_path']
11
10
  result = GitUtil.get(@context['repo'], @context['temp_path'], @context.version_type, @context.version)
12
11
  FileUtils.mkdir_p(@context['full_build_path'])
13
12
  FileUtils.cp_r(Dir["#{@context['temp_path']}/."], @context['full_build_path'])
14
13
  FileUtils.rm_r(File.join(@context['full_build_path'], '.git')) if File.directory?(File.join(@context['full_build_path'], '.git'))
15
- # GitUtil.repo_changed?(@context['full_build_path']) ? result : false
16
14
  result
17
15
  end
18
16
 
data/lib/docman/cli.rb CHANGED
@@ -1,20 +1,28 @@
1
1
  require 'thor'
2
2
  require 'application'
3
+ require 'docman/git_util'
3
4
 
4
5
  module Docman
5
6
  class CLI < Thor
6
7
 
7
8
  # TODO: add proper descriptions.
8
9
 
9
- desc 'init NAME', 'init to NAME'
10
+ desc 'init "dirname" "repo"', 'Initialize docroot in "dirname" from config repo "repo"'
10
11
  method_option :force, :aliases => '-f', :desc => 'Force init'
12
+ method_option :skip, :aliases => '-s', :desc => 'Skip if docroot initialized already'
11
13
  def init(name, repo)
12
- if File.directory? "#{name}"
14
+ if File.directory? name
13
15
  say("Directory #{name} already exists")
14
16
  if options[:force]
15
17
  FileUtils.rm_r(name)
16
- elsif
17
- choice = ask('Are you sure you want do delete existing docroot? Type "yes" if you agree.')
18
+ elsif options[:skip]
19
+ if File.directory? File.join(name, 'config') and GitUtil.repo? File.join(name, 'config')
20
+ return
21
+ else
22
+ FileUtils.rm_r(name)
23
+ end
24
+ else
25
+ choice = ask('Are you sure you want do delete existing docroot? Type "yes" if you agree.')
18
26
  if choice == 'yes'
19
27
  FileUtils.rm_r(name)
20
28
  elsif
@@ -11,8 +11,14 @@ module Docman
11
11
  def execute
12
12
  if File.directory? @context['full_build_path']
13
13
  if @context.need_rebuild?
14
+ return false if @context['type'] == 'dir'
14
15
  return false if @context['type'] == 'root' and @context['build_type'] == :dir_builder and not GitUtil.repo?(@context['full_build_path'])
15
16
  return false if @context['type'] == 'root' and @context['build_type'] == :git_direct_builder and GitUtil.repo?(@context['full_build_path'])
17
+ if @context['type'] == 'repo'
18
+ if @context['build_type'] == :dir_direct_builder
19
+ return false if GitUtil.repo?(@context['full_build_path'])
20
+ end
21
+ end
16
22
  log("Remove #{@context['full_build_path']}")
17
23
  FileUtils.rm_rf @context['full_build_path']
18
24
  end
@@ -43,23 +43,29 @@ module Docman
43
43
  end
44
44
 
45
45
  def config
46
- add_actions self
47
- add_actions @context unless @context.nil?
46
+ add_actions(self, @context)
47
+ add_actions(@context, @context) if @context
48
48
  end
49
49
 
50
- def add_actions(obj)
50
+ def add_actions(obj, context = nil)
51
51
  if obj.has_key? 'hooks' and obj['hooks'].has_key? @type
52
- obj['hooks'][@type].each_pair do |name, hook|
52
+ obj['hooks'][@type].each_pair do |name, hooks|
53
+ hooks = hooks.clone
54
+ unless context.nil?
55
+ hooks.each do |hook|
56
+ hook['context'] = context
57
+ end
58
+ end
53
59
  if @hooks[name].nil?
54
- @hooks[name] = hook
60
+ @hooks[name] = hooks
55
61
  else
56
- @hooks[name].concat(hook)
62
+ @hooks[name].concat(hooks)
57
63
  end
58
64
  end
59
65
  end
60
66
  end
61
67
 
62
- def add_action(name, hook)
68
+ def add_action(name, hook, context = nil)
63
69
  if @hooks.has_key? name
64
70
  @hooks[name] << {'type' => hook}
65
71
  else
@@ -70,45 +76,41 @@ module Docman
70
76
  def run_actions(name)
71
77
  if @hooks.has_key? name
72
78
  @hooks[name].each do |hook|
73
- Docman::Command.create(hook, @context, self).perform
79
+ context = hook.has_key?('context') ? hook['context'] : @context
80
+ Docman::Command.create(hook, context, self).perform
74
81
  end
75
82
  end
76
83
  end
77
84
 
78
85
  def run_with_hooks(method)
79
86
  with_logging(method) do
87
+ run_actions("before_#{method}")
80
88
  run_hook "before_#{method}".to_sym
81
89
  result = self.send(method)
82
90
  @execute_result = result if method == 'execute'
83
91
  run_hook "after_#{method}".to_sym
92
+ run_actions("after_#{method}")
84
93
  end
85
94
  end
86
95
 
87
- before_execute do
88
- run_actions('before_execute')
89
- end
90
-
91
96
  # @abstract
92
97
  def execute
93
98
  raise NoMethodError.new("Please define #execute for #{self.class.name}", '')
94
99
  end
95
100
 
96
- after_execute do
97
- run_actions('after_execute')
98
- end
99
-
100
101
  def perform
101
102
  config if self.respond_to? :config
102
103
  validate_command if self.respond_to? :validate_command
103
- @execute_result = run_with_hooks('execute')
104
+ run_with_hooks('execute')
105
+ @execute_result
104
106
  rescue CommandValidationError => e
105
- logger.error "Command validation error: #{e.message}"
107
+ log "Command validation error: #{e.message}", 'error'
106
108
  return false
107
109
  rescue NoChangesError => e
108
- logger.info "No changes: #{e.message}"
110
+ log "No changes: #{e.message}", 'error'
109
111
  return false
110
112
  rescue StandardError => e
111
- logger.error e.message
113
+ log e.message, 'error'
112
114
  raise
113
115
  ensure
114
116
  @execute_result
@@ -130,7 +132,7 @@ module Docman
130
132
  value.gsub! '$DOCROOT$', @context['docroot_config'].docroot_dir
131
133
  value.gsub! '$PROJECT$', @context['full_build_path']
132
134
  value.gsub! '$INFO$', @context['full_path']
133
- value.gsub! '$ENVIRONMENT$', @context['docroot_config'].deploy_target['environment']
135
+ value.gsub! '$ENVIRONMENT$', @context.environment
134
136
  end
135
137
 
136
138
  end
@@ -30,7 +30,7 @@ module Docman
30
30
  params.each do |param|
31
31
  case param
32
32
  when 'environment'
33
- result << @context['docroot_config'].deploy_target['environment']
33
+ result << @context.environment
34
34
  end
35
35
  end
36
36
  result.join(' ')
@@ -10,6 +10,7 @@ module Docman
10
10
  Net::SFTP.start(self['ssh_host'], self['ssh_user']) do |sftp|
11
11
  n = 0
12
12
  begin
13
+ sleep 15
13
14
  n+=1
14
15
  log "Checking if files deployed, retry ##{n}, filename: #{filename}"
15
16
  sftp.stat!(filename) do |response|
@@ -18,7 +19,6 @@ module Docman
18
19
  end
19
20
  end
20
21
  data = YAML.load sftp.download!(filename)
21
- sleep 30
22
22
  end until data['random'] == self['version']
23
23
  end
24
24
  true
@@ -2,17 +2,19 @@ require 'docman/commands/target_checker'
2
2
  require 'docman/commands/ssh_target_checker'
3
3
  require 'docman/exceptions/no_changes_error'
4
4
  require 'securerandom'
5
+ require 'diffy'
5
6
 
6
7
  module Docman
7
8
  module Deployers
8
9
  class Deployer < Docman::Command
9
10
 
10
- attr_accessor :before, :after
11
-
12
- define_hooks :before_push, :after_push
11
+ define_hooks :before_push, :after_push, :before_build, :after_build, :before_deploy, :after_deploy
13
12
 
14
13
  @@deployers = {}
15
14
 
15
+ #todo: prod ssh settings deploy target
16
+ #todo: docroot config in separate repos for projects
17
+
16
18
  def self.create(params, context = nil, caller = nil)
17
19
  c = @@deployers[params['handler']]
18
20
  if c
@@ -36,64 +38,92 @@ module Docman
36
38
  def config
37
39
  unless self['name'].nil?
38
40
  @docroot_config.chain(@docroot_config.info_by(self['name'])).values.each do |info|
39
- add_actions(info)
41
+ add_actions(info, info)
40
42
  end
41
43
  end
42
- end
43
44
 
44
- before_execute do
45
45
  stored_config_hash = read_version_file_param('config_hash')
46
46
  @config_hash = Docman::Application.instance.config.config_hash
47
+ @config_yaml = Docman::Application.instance.config.to_yaml
48
+
49
+ #TODO: need to refactor
47
50
  stored_docroot_config_hash = read_version_file_param('docroot_config_hash')
48
51
  @docroot_config_hash = @docroot_config.config_hash
49
- # config = Docman::Application.instance.config
50
- # log(config.to_yaml)
51
- if stored_config_hash != @config_hash or stored_docroot_config_hash != @docroot_config_hash
52
- logger.info 'Forced rebuild as configuration was changed'
52
+ @docroot_config_yaml = @docroot_config.raw_infos.to_yaml
53
+ if stored_config_hash != @config_hash
54
+ log 'Forced rebuild as configuration was changed', 'info'
55
+ filename = File.join(@docroot_config.root['full_build_path'], 'config.yaml')
56
+ log Diffy::Diff.new(read_file(filename), @config_yaml) if File.file? filename
57
+ Docman::Application.instance.force = true
58
+ end
59
+ if stored_docroot_config_hash != @docroot_config_hash
60
+ log 'Forced rebuild as configuration was changed', 'info'
61
+ filename = File.join(@docroot_config.root['full_build_path'], 'docroot_config.yaml')
62
+ log Diffy::Diff.new(read_file(filename), @docroot_config_yaml) if File.file? filename
53
63
  Docman::Application.instance.force = true
54
64
  end
55
65
  end
56
66
 
57
67
  def execute
58
- logger.info "Deploy started"
59
- if self['name'].nil?
60
- build_recursive
61
- else
62
- build_dir_chain(@docroot_config.info_by(self['name']))
63
- end
64
-
68
+ run_with_hooks('build')
65
69
  if @changed
66
- filename = 'version.yaml'
67
- path = File.join(@docroot_config.root['full_build_path'], filename)
68
- version = SecureRandom.hex
69
- write_version_file version, path
70
- run_with_hooks('push')
71
- raise 'Files are not deployed' unless files_deployed? version, filename
70
+ run_with_hooks('deploy')
72
71
  else
73
- logger.info 'No changes in docroot'
72
+ log 'No changes in docroot', 'info'
74
73
  end
75
- logger.debug 'Deploy results:'
76
- logger.debug @build_results.to_yaml
77
- logger.info 'Deploy finished'
74
+ log "Deploy results:\n" + @build_results.to_yaml
75
+ end
76
+
77
+ def deploy
78
+ filename = 'version.yaml'
79
+ path = File.join(@docroot_config.root['full_build_path'], filename)
80
+ version = SecureRandom.hex
81
+ write_version_file version, path
82
+ write_config_file @config_yaml, File.join(@docroot_config.root['full_build_path'], 'config.yaml')
83
+ write_config_file @docroot_config_yaml, File.join(@docroot_config.root['full_build_path'], 'docroot_config.yaml')
84
+ run_with_hooks('push')
85
+ raise 'Files are not deployed' unless files_deployed? version, filename
86
+ end
87
+
88
+ def files_deployed?(version, filename)
89
+ return true unless self.has_key? 'target_checker'
90
+ params = self['target_checker']
91
+ params['version'] = version
92
+ params['filename'] = filename
93
+ Docman::TargetChecker.create(params, self).perform
94
+ end
95
+
96
+ def read_file(path)
97
+ YAML::load_file(path)
78
98
  end
79
99
 
80
100
  def read_version_file_param(param)
81
101
  path = File.join(@docroot_config.root['full_build_path'], 'version.yaml')
82
102
  return false unless File.file?(path)
83
- content = YAML::load_file(path)
103
+ content = read_file(path)
84
104
  content[param] if content.has_key? param
85
105
  end
86
106
 
87
107
  def write_version_file(version, path)
88
108
  to_write = Hash.new
89
109
  to_write['random'] = version
90
- # config = Docman::Application.instance.config.raw_config
91
- # log(config.to_yaml)
92
110
  to_write['config_hash'] = @config_hash
93
111
  to_write['docroot_config_hash'] = @docroot_config_hash
94
112
  File.open(path, 'w') {|f| f.write to_write.to_yaml}
95
113
  end
96
114
 
115
+ def write_config_file(config, path)
116
+ File.open(path, 'w') {|f| f.write config.to_yaml}
117
+ end
118
+
119
+ def build
120
+ if self['name'].nil?
121
+ build_recursive
122
+ else
123
+ build_dir_chain(@docroot_config.info_by(self['name']))
124
+ end
125
+ end
126
+
97
127
  def build_dir_chain(info)
98
128
  @docroot_config.chain(info).values.each do |item|
99
129
  item.state = self['state']
@@ -109,12 +139,10 @@ module Docman
109
139
  def build_dir(info)
110
140
  return if @builded.include? info['name']
111
141
  info.state = self['state']
112
-
113
142
  build_result = Docman::Builders::Builder.create(self['builders'][info['type']], info, self).perform
114
143
  logger.info '-------------------------------------------------------'
115
144
  @changed = true if build_result
116
145
  @build_results[info['name']] = build_result
117
-
118
146
  @builded << info['name']
119
147
  end
120
148
 
@@ -127,14 +155,6 @@ module Docman
127
155
  end
128
156
  end
129
157
 
130
- def files_deployed?(version, filename)
131
- return true unless self.has_key? 'target_checker'
132
- params = self['target_checker']
133
- params['version'] = version
134
- params['filename'] = filename
135
- Docman::TargetChecker.create(params, self).perform
136
- end
137
-
138
158
  def describe(type = 'short')
139
159
  properties_info(['handler'])
140
160
  end
@@ -5,7 +5,8 @@ module Docman
5
5
 
6
6
  class DocrootConfig
7
7
 
8
- attr_reader :structure, :deploy_target, :docroot_dir, :root
8
+ attr_reader :structure, :deploy_target, :docroot_dir, :root, :raw_infos
9
+
9
10
  def initialize(docroot_dir, deploy_target)
10
11
  @docroot_dir = docroot_dir
11
12
  @deploy_target = deploy_target
@@ -15,7 +16,7 @@ module Docman
15
16
  Docman::Application.instance.config.merge_config_from_file(File.join(@docroot_config_dir, 'config.yaml'))
16
17
  end
17
18
  @names = {}
18
- @raw_infos = []
19
+ @raw_infos = {}
19
20
  @structure = structure_build File.join(@docroot_config_dir, 'master')
20
21
  end
21
22
 
@@ -28,7 +29,7 @@ module Docman
28
29
 
29
30
  children = []
30
31
  info = YAML::load_file(File.join(path, 'info.yaml'))
31
- @raw_infos << YAML::load_file(File.join(path, 'info.yaml'))
32
+ @raw_infos[File.basename path] = YAML::load_file(File.join(path, 'info.yaml'))
32
33
  unless info['status'].nil?
33
34
  return if info['status'] == 'disabled'
34
35
  end
@@ -88,5 +89,9 @@ module Docman
88
89
  Digest::MD5.hexdigest(Marshal::dump(@raw_infos))
89
90
  end
90
91
 
92
+ def root_path
93
+ @root['fuil_build_path']
94
+ end
95
+
91
96
  end
92
97
  end
data/lib/docman/info.rb CHANGED
@@ -98,5 +98,9 @@ module Docman
98
98
  []
99
99
  end
100
100
 
101
+ def environment
102
+ self['docroot_config'].deploy_target['environments'][self['state']]
103
+ end
104
+
101
105
  end
102
106
  end
@@ -1,3 +1,3 @@
1
1
  module Docman
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Tolstikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-09 00:00:00.000000000 Z
11
+ date: 2014-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - '>='
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: diffy
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
125
139
  description:
126
140
  email:
127
141
  - atolstikov@adyax.com