capistrano-template 0.0.5 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b7c26cd31e68ace9c983f2690872cf25d451575
4
- data.tar.gz: ad40226e2f26582edd73d56ea42006076d70c292
3
+ metadata.gz: cf1b76bed38d227a7b50b774e670a58bbc31baf5
4
+ data.tar.gz: 347549beb8f5546df3002e1c5000e6b0c3d5ca81
5
5
  SHA512:
6
- metadata.gz: bb0f80f4116772a04a4a711e3cb1e65ba14d8029ab651b349036253726f203791a0720f12ce1839ca5d2eb779067b2e85dbd28547c4ceb352f2c6b9bfe3c086c
7
- data.tar.gz: f26c7fb86f05eaccb669324573288f504c8f496bc6d7c0271334f13f1c6dfbba565a9a3724da4117994042d53593029907e932d563ead7a0167b4d32436fdd4d
6
+ metadata.gz: 9957f08403791cb36451ed1a12fbd036f7a8bb35aaf052330699bc9cb8ccfaff801f5965705daf222eeb35e1f8d69b56ff12ce524819357d5a4aeb0e620fe717
7
+ data.tar.gz: 17ee68b87d83147ee93f0552d1a14f52da956b20a425a810aa019c7cdadd25a4055cacda8d5537f90040cd4a3411d28e270361713121386015c4e60959fe620a
data/.gitignore CHANGED
@@ -13,6 +13,8 @@ lib/bundler/man
13
13
  pkg
14
14
  rdoc
15
15
  spec/reports
16
+ spec/dummy_app/.vagrant
17
+ spec/dummy_app/log
16
18
  test/tmp
17
19
  test/version_tmp
18
20
  tmp
@@ -16,3 +16,10 @@ MethodLength:
16
16
 
17
17
  RegexpLiteral:
18
18
  Enabled: false
19
+
20
+ AllCops:
21
+ Exclude:
22
+ - 'spec/dummy_app/**/*'
23
+ - 'vendor/**/*'
24
+ - 'tmp/**/*'
25
+ TargetRubyVersion: 2.0
@@ -1,4 +1,4 @@
1
- ruby=ruby-2.3.0
1
+ ruby=ruby-2.3.1
2
2
  ruby-gemset=capistrano-template
3
3
  #ruby-gem-install=bundler rake
4
4
  #ruby-bundle-install=true
@@ -1,13 +1,22 @@
1
- 0.0.6 (Next)
1
+ 0.0.7 (Next)
2
2
  =============
3
3
 
4
4
 
5
+ 0.0.6 (28/09/2016)
6
+ =============
7
+
8
+ #### Features
9
+
10
+ * [#6](https://github.com/faber-lotto/capistrano-template/6): Add dry run support. [@claudusd](https://github.com/claudusd).
11
+ * [#9](https://github.com/faber-lotto/capistrano-template/9): Add support for setting file owner. [@slowjack2k](https://github.com/slowjack2k).
12
+ * [#10](https://github.com/faber-lotto/capistrano-template/10): Add support for within block. [@slowjack2k](https://github.com/slowjack2k).
13
+
5
14
  0.0.5 (27/07/2016)
6
15
  ==================
7
16
 
8
17
  #### Features
9
18
 
10
- * [#5](https://github.com/faber-lotto/capistrano-template/5): Enable the usage of locals.
19
+ * [#5](https://github.com/faber-lotto/capistrano-template/5): Enable the usage of locals. [@slowjack2k](https://github.com/slowjack2k).
11
20
 
12
21
  #### Fixes
13
22
 
data/README.md CHANGED
@@ -38,19 +38,22 @@ In your Capfile:
38
38
 
39
39
  desc 'Upload a rendered erb-template'
40
40
  task :setup do
41
- on roles :all
41
+ on roles :all do
42
42
  # searchs for template assets.host.site.erb in :templating_paths
43
43
  # renders the template and upload it to "#{release_path}/assets.host.site" on all hosts
44
44
  # when the new rendered content is changed or the remote file does not exists
45
45
  template 'assets.host.site', locals: { 'local1' => 'value local 1'}
46
46
  end
47
47
 
48
- on roles :all
48
+ on roles :all do
49
49
  # searchs for template other.template.name.erb in :templating_paths
50
50
  # renders the template and upload it to "~/execute_some_thing.sh" on all hosts
51
51
  # when the new rendered content is changed or the remote file does not exists
52
52
  # after this the mode is changed to 0750
53
- template 'other.template.name', '~/execute_some_thing.sh', 0750, locals: { 'local1' => 'value local 1'}
53
+ # owner is changed to "deployer:www-run"
54
+ # keep in mind chown and chgrp needs sudo privileges
55
+
56
+ template 'other.template.name', '~/execute_some_thing.sh', 0750, 'deployer', 'www-run' ,locals: { 'local1' => 'value local 1'}
54
57
  end
55
58
 
56
59
  end
@@ -119,6 +122,7 @@ This settings can be changed in your Capfile, deploy.rb or stage file.
119
122
  |`templating_digster` | <code> -&gt;(data){ OpenSSL::Digest::MD5.hexdigest(data)} </code> | Checksum algorythmous for rendered template to check for remote diffs |
120
123
  |`templating_digest_cmd`| <code>%Q{test "Z$(openssl md5 %&lt;path&gt;s &#124; sed 's/^.*= *//')" = "Z%&lt;digest&gt;s" }</code> | Remote command to validate a digest. Format placeholders path is replaces by full `path` to the remote file and `digest` with the digest calculated in capistrano. |
121
124
  |`templating_mode_test_cmd` | <code>%Q{ &#91; "Z$(printf "%%.4o" 0$(stat -c "%%a" %&lt;path&gt;s 2&gt;/dev/null &#124;&#124; stat -f "%%A" %&lt;path&gt;s))" != "Z%&lt;mode&gt;s" &#93; }</code> | Test command to check the remote file permissions. |
125
+ |`templating_user_test_cmd` | <code>%Q{ &#91; "Z$(stat -c "%%U" %&lt;path&gt;s 2&gt;/dev/null)" != "Z%&lt;user&gt;s" &#93; }</code> | Test command to check the remote file permissions. |
122
126
  | `templating_paths` | <code>&#91;"config/deploy/templates/#{fetch(:stage)}/%&lt;host&gt;s",</code> <br> <code> "config/deploy/templates/#{fetch(:stage)}",</code> <br> <code> "config/deploy/templates/shared/%&lt;host&gt;s",</code> <br> <code> "config/deploy/templates/shared"&#93;</code>| Folder to look for a template to render. `<host>` is replaced by the actual host. |
123
127
 
124
128
 
@@ -2,22 +2,27 @@ module Capistrano
2
2
  module Template
3
3
  module Helpers
4
4
  module DSL
5
- def template(from, to = nil, mode = 0640, locals: {})
5
+ # rubocop: disable Metrics/AbcSize
6
+ def template(from, to = nil, mode = 0640, user = nil, group = nil, locals: {})
6
7
  fail ::ArgumentError, "template #{from} not found Paths: #{template_paths_lookup.paths_for_file(from).join(':')}" unless template_exists?(from)
7
8
 
8
- to ||= "#{release_path}/#{File.basename(from, '.erb')}"
9
- to = remote_path_for(to, true)
9
+ return if dry_run?
10
10
 
11
11
  template = _template_factory.call(template_file(from), self, fetch(:templating_digster), locals)
12
12
 
13
- _uploader_factory.call(to, self,
13
+ _uploader_factory.call(get_to(to, from), self,
14
14
  digest: template.digest,
15
15
  digest_cmd: fetch(:templating_digest_cmd),
16
16
  mode_test_cmd: fetch(:templating_mode_test_cmd),
17
+ user_test_cmd: fetch(:templating_user_test_cmd),
18
+ group_test_cmd: fetch(:templating_group_test_cmd),
17
19
  mode: mode,
20
+ user: user,
21
+ group: group,
18
22
  io: template.as_io
19
23
  ).call
20
24
  end
25
+ # rubocop: enable Metrics/AbcSize
21
26
 
22
27
  def template_exists?(template)
23
28
  template_paths_lookup.template_exists?(template)
@@ -47,7 +52,10 @@ module Capistrano
47
52
  path = File.dirname(path)
48
53
  end
49
54
 
50
- remote_path = capture("/bin/bash -c '(cd #{path} && pwd -P) || readlink -sf #{path}'").chomp
55
+ cd_cmd = "cd #{path}"
56
+ cd_cmd = "cd #{pwd_path}; #{cd_cmd}" if pwd_path
57
+
58
+ remote_path = capture("/bin/bash -c '(#{cd_cmd} && pwd -P) || readlink -sf #{path}'").chomp
51
59
 
52
60
  includes_filename ? File.join(remote_path, filename) : remote_path
53
61
  end
@@ -63,6 +71,19 @@ module Capistrano
63
71
  super
64
72
  end
65
73
  end
74
+
75
+ def dry_run?
76
+ if ::Capistrano::Configuration.respond_to?(:dry_run?)
77
+ ::Capistrano::Configuration.dry_run?
78
+ else
79
+ ::Capistrano::Configuration.env.send(:config)[:sshkit_backend] == SSHKit::Backend::Printer
80
+ end
81
+ end
82
+
83
+ def get_to(to, from)
84
+ to ||= "#{release_path}/#{File.basename(from, '.erb')}"
85
+ remote_path_for(to, true)
86
+ end
66
87
  end
67
88
  end
68
89
  end
@@ -4,18 +4,27 @@ module Capistrano
4
4
  require 'capistrano/template/helpers/renderer'
5
5
  require 'capistrano/template/helpers/template_digester'
6
6
 
7
+ # rubocop: disable Metrics/ClassLength
7
8
  class Uploader
8
9
  attr_accessor :io,
9
10
  :digest,
10
11
  :full_to_path,
11
12
  :digest_cmd,
12
13
  :mode,
14
+ :user,
15
+ :group,
16
+ :user_test_cmd,
17
+ :group_test_cmd,
13
18
  :remote_handler,
14
19
  :mode_test_cmd
15
20
 
16
21
  def initialize(full_to_path, remote_handler,
17
22
  mode: 0640,
18
23
  mode_test_cmd: nil,
24
+ user: nil,
25
+ user_test_cmd: nil,
26
+ group: nil,
27
+ group_test_cmd: nil,
19
28
  digest: nil,
20
29
  digest_cmd: nil,
21
30
  io: nil
@@ -27,6 +36,10 @@ module Capistrano
27
36
  self.digest_cmd = digest_cmd
28
37
  self.mode = mode
29
38
  self.mode_test_cmd = mode_test_cmd
39
+ self.user = user
40
+ self.user_test_cmd = user_test_cmd
41
+ self.group = group
42
+ self.group_test_cmd = group_test_cmd
30
43
 
31
44
  self.io = io
32
45
  self.digest = digest
@@ -35,11 +48,17 @@ module Capistrano
35
48
  def call
36
49
  upload_as_file
37
50
  set_mode
51
+ set_user
52
+ set_group
38
53
  end
39
54
 
40
55
  def upload_as_file
41
56
  if file_changed?
42
57
  remote_handler.info "copying to: #{full_to_path}"
58
+
59
+ # just in case owner changed
60
+ remote_handler.execute 'rm', '-f', full_to_path
61
+
43
62
  remote_handler.upload! io, full_to_path
44
63
  else
45
64
  remote_handler.info "File #{full_to_path} on host #{host} not changed"
@@ -59,6 +78,26 @@ module Capistrano
59
78
  end
60
79
  end
61
80
 
81
+ def set_user
82
+ if user_changed?
83
+ remote_handler.info "user changed for file #{full_to_path} on #{host} set new user"
84
+
85
+ remote_handler.execute 'sudo', 'chown', user, full_to_path
86
+ else
87
+ remote_handler.info "user not changed for file #{full_to_path} on #{host}"
88
+ end
89
+ end
90
+
91
+ def set_group
92
+ if group_changed?
93
+ remote_handler.info "group changed for file #{full_to_path} on #{host} set new group"
94
+
95
+ remote_handler.execute 'sudo', 'chgrp', group, full_to_path
96
+ else
97
+ remote_handler.info "group not changed for file #{full_to_path} on #{host}"
98
+ end
99
+ end
100
+
62
101
  def file_changed?
63
102
  !__check__(digest_cmd)
64
103
  end
@@ -67,6 +106,14 @@ module Capistrano
67
106
  __check__(mode_test_cmd)
68
107
  end
69
108
 
109
+ def user_changed?
110
+ user && __check__(user_test_cmd)
111
+ end
112
+
113
+ def group_changed?
114
+ group && __check__(group_test_cmd)
115
+ end
116
+
70
117
  protected
71
118
 
72
119
  def __check__(*args)
@@ -88,7 +135,22 @@ module Capistrano
88
135
  mode: octal_mode_str
89
136
  }
90
137
  end
138
+
139
+ def user_test_cmd
140
+ @user_test_cmd % {
141
+ path: full_to_path,
142
+ user: user
143
+ }
144
+ end
145
+
146
+ def group_test_cmd
147
+ @group_test_cmd % {
148
+ path: full_to_path,
149
+ group: group
150
+ }
151
+ end
91
152
  end
153
+ # rubocop: enable Metrics/ModuleLength
92
154
  end
93
155
  end
94
156
  end
@@ -4,6 +4,8 @@ namespace :load do
4
4
  set :templating_digster, ->{ ->(data){ OpenSSL::Digest::MD5.hexdigest(data)} }
5
5
  set :templating_digest_cmd, %Q{test "Z$(openssl md5 %<path>s| sed 's/^.*= *//')" = "Z%<digest>s" } # alternative %Q{echo "%<digest>s %<path>s" | md5sum -c --status } should return true when the file content is the same
6
6
  set :templating_mode_test_cmd, %Q{ [ "Z$(printf "%%.4o" 0$(stat -c "%%a" %<path>s 2>/dev/null || stat -f "%%A" %<path>s))" != "Z%<mode>s" ] } # mac uses different mode formatter
7
+ set :templating_user_test_cmd, %Q{ [ "Z$(stat -c "%%U" %<path>s 2>/dev/null)" != "Z%<user>s" ] } # should return true when user is different
8
+ set :templating_group_test_cmd, %Q{ [ "Z$(stat -c "%%G" %<path>s 2>/dev/null)" != "Z%<group>s" ] } # should return true when group is different
7
9
  set :templating_paths , ->{ ["config/deploy/templates/#{fetch(:stage)}/%<host>s",
8
10
  "config/deploy/templates/#{fetch(:stage)}",
9
11
  "config/deploy/templates/shared/%<host>s",
@@ -1,5 +1,5 @@
1
1
  module Capistrano
2
2
  module Template
3
- VERSION = '0.0.5'
3
+ VERSION = '0.0.7'
4
4
  end
5
5
  end
@@ -0,0 +1,31 @@
1
+ # Load DSL and set up stages
2
+ require 'capistrano/setup'
3
+
4
+ # Include default deployment tasks
5
+ require 'capistrano/deploy'
6
+
7
+ Bundler.require(:development)
8
+
9
+ # Include tasks from other gems included in your Gemfile
10
+ #
11
+ # For documentation on these, see for example:
12
+ #
13
+ # https://github.com/capistrano/rvm
14
+ # https://github.com/capistrano/rbenv
15
+ # https://github.com/capistrano/chruby
16
+ # https://github.com/capistrano/bundler
17
+ # https://github.com/capistrano/rails
18
+ # https://github.com/capistrano/passenger
19
+ #
20
+ # require 'capistrano/rvm'
21
+ # require 'capistrano/rbenv'
22
+ # require 'capistrano/chruby'
23
+ # require 'capistrano/bundler'
24
+ # require 'capistrano/rails/assets'
25
+ # require 'capistrano/rails/migrations'
26
+ # require 'capistrano/passenger'
27
+
28
+ require 'capistrano/capistrano_plugin_template'
29
+
30
+ # Load custom tasks from `lib/capistrano/tasks` if you have any defined
31
+ Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
@@ -0,0 +1,26 @@
1
+ VAGRANTFILE_API_VERSION = '2'.freeze
2
+
3
+ $provision_script = <<SCRIPT
4
+ echo I am provisioning...
5
+ useradd deploy -s /bin/bash -m
6
+ chpasswd << 'END'
7
+ deploy:12345678
8
+ END
9
+ apt-get update -y
10
+ apt-get install -y git-core
11
+ mkdir -p /var/www
12
+ chown -R vagrant /var/www
13
+ date > /etc/vagrant_provisioned_at
14
+ SCRIPT
15
+
16
+ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
17
+ config.vm.box = 'ubuntu/precise64'
18
+ config.vm.box_url = 'https://files.vagrantup.com/precise64.box'
19
+
20
+ config.vm.define 'cap-template-1' do |web|
21
+ web.vm.network 'forwarded_port', guest: 22, host: 3001
22
+
23
+ config.vm.provision 'shell',
24
+ inline: $provision_script
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ # config valid only for current version of Capistrano
2
+ lock '3.6.1'
3
+
4
+ set :application, 'my_app_name'
5
+
6
+ set :repo_url, 'https://github.com/faber-lotto/capistrano-template.git'
7
+ # Default branch is :master
8
+ # ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
9
+
10
+ # Default deploy_to directory is /var/www/my_app_name
11
+ # set :deploy_to, '/var/www/my_app_name'
12
+
13
+ # Default value for :scm is :git
14
+ # set :scm, :git
15
+
16
+ # Default value for :format is :airbrussh.
17
+ set :format, :pretty
18
+
19
+ # You can configure the Airbrussh format using :format_options.
20
+ # These are the defaults.
21
+ # set :format_options, command_output: true, log_file: 'log/capistrano.log', color: :auto, truncate: :auto
22
+
23
+ # Default value for :pty is false
24
+ # set :pty, true
25
+
26
+ # Default value for :linked_files is []
27
+ # append :linked_files, 'config/database.yml', 'config/secrets.yml'
28
+
29
+ # Default value for linked_dirs is []
30
+ # append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'public/system'
31
+
32
+ # Default value for default_env is {}
33
+ # set :default_env, { path: "/opt/ruby/bin:$PATH" }
34
+
35
+ # Default value for keep_releases is 5
36
+ # set :keep_releases, 5
@@ -0,0 +1,53 @@
1
+ # server-based syntax
2
+ # ======================
3
+ # Defines a single server with a list of roles and multiple properties.
4
+ # You can define all roles on a single server, or split them:
5
+
6
+ server 'localhost', user: 'vagrant', roles: %w(app db web)
7
+
8
+ # role-based syntax
9
+ # ==================
10
+
11
+ # Defines a role with one or multiple servers. The primary server in each
12
+ # group is considered to be the first unless any hosts have the primary
13
+ # property set. Specify the username and a domain or IP for the server.
14
+ # Don't use `:all`, it's a meta role.
15
+
16
+ # role :app, %w{deploy@example.com}, my_property: :my_value
17
+ # role :web, %w{user1@primary.com user2@additional.com}, other_property: :other_value
18
+ # role :db, %w{deploy@example.com}
19
+
20
+ # Configuration
21
+ # =============
22
+ # You can set any configuration variable like in config/deploy.rb
23
+ # These variables are then only loaded and set in this stage.
24
+ # For available Capistrano configuration variables see the documentation page.
25
+ # http://capistranorb.com/documentation/getting-started/configuration/
26
+ # Feel free to add new variables to customise your setup.
27
+
28
+ # Custom SSH Options
29
+ # ==================
30
+ # You may pass any option but keep in mind that net/ssh understands a
31
+ # limited set of options, consult the Net::SSH documentation.
32
+ # http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start
33
+ #
34
+ # Global options
35
+ # --------------
36
+ set :ssh_options, port: '3001',
37
+ password: 'vagrant',
38
+ forward_agent: false,
39
+ auth_methods: %w(password)
40
+
41
+ #
42
+ # The server-based syntax can be used to override options:
43
+ # ------------------------------------
44
+ # server 'example.com',
45
+ # user: 'user_name',
46
+ # roles: %w{web app},
47
+ # ssh_options: {
48
+ # user: 'user_name', # overrides user setting above
49
+ # keys: %w(/home/user_name/.ssh/id_rsa),
50
+ # forward_agent: false,
51
+ # auth_methods: %w(publickey password)
52
+ # # password: 'please use keys'
53
+ # }
@@ -0,0 +1,31 @@
1
+ desc 'Upload a rendered erb-template'
2
+ task :setup do
3
+ on roles :all do
4
+ # searchs for template other.template.name.erb in :templating_paths
5
+ # renders the template and upload it to "~/execute_some_thing.sh" on all hosts
6
+ # when the new rendered content is changed or the remote file does not exists
7
+ # after this the mode is changed to 0750
8
+ template 'other.template.name',
9
+ './execute_some_thing.sh',
10
+ 0o750,
11
+ 'deploy',
12
+ 'deploy',
13
+ locals: { 'local1' => 'value local 1' }
14
+
15
+ puts "PATH: #{pwd_path}"
16
+
17
+ within '/var/www' do
18
+
19
+ puts "PATH: #{pwd_path}"
20
+
21
+
22
+ template 'other.template.name',
23
+ './execute_some_thing.sh',
24
+ 0o750,
25
+ 'deploy',
26
+ 'deploy',
27
+ locals: { 'local1' => 'value local 1' }
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ #!/bin/bash
2
+
3
+ SCRIPT_PATH="`dirname \"$0\"`"
4
+
5
+ SCRIPT_PATH="`( cd \"$SCRIPT_PATH\" && pwd )`"
6
+ if [ -z "$SCRIPT_PATH" ] ; then
7
+ exit 1
8
+ fi
9
+
10
+ (
11
+ cd $SCRIPT_PATH
12
+
13
+ vagrant up
14
+ #vagrant ssh -- -t 'sudo rm ~/execute_some_thing.sh'
15
+ rm -f $SCRIPT_PATH/log/*
16
+ cap development check
17
+ cap development setup
18
+ vagrant ssh -- -t 'sudo ls -l ~/execute_some_thing.sh; sudo cat ~/execute_some_thing.sh'
19
+ vagrant ssh -- -t 'sudo ls -l /var/www/execute_some_thing.sh; sudo cat /var/www/execute_some_thing.sh'
20
+ vagrant halt
21
+ )
@@ -13,7 +13,9 @@ module Capistrano
13
13
  self.data = {
14
14
  templating_digster: ->(data) { Digest::MD5.hexdigest(data) },
15
15
  templating_digest_cmd: %Q(echo "%<digest>s %<path>s" | md5sum -c --status ),
16
- templating_mode_test_cmd: %Q{ [ "Z$(printf "%%.4o" 0$(stat -c "%%a" %<path>s 2>/dev/null || stat -f "%%A" %<path>s))" != "Z%<mode>s" ] }
16
+ templating_mode_test_cmd: %Q{ [ "Z$(printf "%%.4o" 0$(stat -c "%%a" %<path>s 2>/dev/null || stat -f "%%A" %<path>s))" != "Z%<mode>s" ] },
17
+ templating_user_test_cmd: %Q{ [ "Z$(stat -c "%%U" %<path>s 2>/dev/null)" != "Z%<user>s" ] },
18
+ templating_group_test_cmd: %Q{ [ "Z$(stat -c "%%G" %<path>s 2>/dev/null)" != "Z%<group>s" ] }
17
19
  }
18
20
  end
19
21
 
@@ -58,6 +60,13 @@ module Capistrano
58
60
 
59
61
  def error(*)
60
62
  end
63
+
64
+ def pwd_path
65
+ end
66
+
67
+ def dry_run?
68
+ false
69
+ end
61
70
  end
62
71
  end
63
72
  end
@@ -91,7 +91,6 @@ module Capistrano
91
91
 
92
92
  expect(mode).to eq(0640)
93
93
  end
94
-
95
94
  end
96
95
 
97
96
  end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ module Capistrano
4
+ module Template
5
+ module Helpers
6
+ module Unit # protect from other dummy classes
7
+ module DSLSpec
8
+ class DummyDryRun
9
+ include DSL
10
+ def template_exists?
11
+ true
12
+ end
13
+
14
+ def dry_run?
15
+ true
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ describe DSL do
22
+ subject do
23
+ Unit::DSLSpec::DummyDryRun.new
24
+ end
25
+
26
+ describe '#template dry run' do
27
+ it 'do nothing' do
28
+ expect(subject).not_to receive(:_template_factory)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -24,6 +24,10 @@ module Capistrano
24
24
  '/var/www/app/releases/20140510'
25
25
  end
26
26
 
27
+ def pwd_path
28
+ nil
29
+ end
30
+
27
31
  def fetch(*args)
28
32
  data.fetch(*args)
29
33
  end
@@ -2,6 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  module Capistrano
4
4
  module Template
5
+ # rubocop: disable Metrics/ModuleLength
5
6
  module Helpers
6
7
  describe Uploader do
7
8
 
@@ -11,6 +12,9 @@ module Capistrano
11
12
  upload_handler,
12
13
  mode: 0640,
13
14
  mode_test_cmd: mode_test_cmd,
15
+ user: 'deploy',
16
+ group: 'www-run',
17
+ user_test_cmd: user_test_cmd,
14
18
  digest: digest,
15
19
  digest_cmd: digest_cmd,
16
20
  io: as_io
@@ -34,13 +38,28 @@ module Capistrano
34
38
  let(:digest) { Digest::MD5.hexdigest(rendered_template_content) }
35
39
  let(:digest_cmd) { %Q(echo "%<digest>s %<path>s" | md5sum -c --status) }
36
40
  let(:mode_test_cmd) { %Q{ [ "Z$(printf "%%.4o" 0$(stat -c "%%a" %<path>s 2>/dev/null || stat -f "%%A" %<path>s))" != "Z%<mode>s" ] } }
41
+ let(:user_test_cmd) { %Q{ [ "Z$(stat -c "%%U" %<path>s 2>/dev/null)" != "Z%<user>s" ] } }
37
42
 
38
43
  describe '#upload_as_file' do
39
44
 
40
45
  it 'uploads changed files' do
41
46
  allow(subject).to receive(:file_changed?).and_return true
42
- expect(upload_handler).to receive(:upload!).with(as_io, remote_filename_expented)
47
+ allow(upload_handler).to receive(:execute).and_return true
48
+ allow(upload_handler).to receive(:upload!).and_return true
49
+
50
+ subject.upload_as_file
51
+
52
+ expect(upload_handler).to have_received(:upload!).with(as_io, remote_filename_expented)
53
+ end
54
+
55
+ it 'deletes a file before upload' do
56
+ allow(subject).to receive(:file_changed?).and_return true
57
+ allow(upload_handler).to receive(:execute).and_return true
58
+ allow(upload_handler).to receive(:upload!).and_return true
59
+
43
60
  subject.upload_as_file
61
+
62
+ expect(upload_handler).to have_received(:execute).with('rm', '-f', remote_filename_expented)
44
63
  end
45
64
 
46
65
  it 'does not upload unchanged files' do
@@ -67,6 +86,38 @@ module Capistrano
67
86
  end
68
87
  end
69
88
 
89
+ describe '#set_user' do
90
+ it 'sets the user for the remote file' do
91
+ allow(subject).to receive(:user_changed?).and_return true
92
+
93
+ expect(upload_handler).to receive(:execute).with('sudo', 'chown', 'deploy', remote_filename_expented)
94
+
95
+ subject.set_user
96
+ end
97
+
98
+ it 'sets not the user for the remote file' do
99
+ allow(subject).to receive(:user_changed?).and_return false
100
+ expect(upload_handler).not_to receive(:execute)
101
+ subject.set_user
102
+ end
103
+ end
104
+
105
+ describe '#set_group' do
106
+ it 'sets the group for the remote file' do
107
+ allow(subject).to receive(:group_changed?).and_return true
108
+
109
+ expect(upload_handler).to receive(:execute).with('sudo', 'chgrp', 'www-run', remote_filename_expented)
110
+
111
+ subject.set_group
112
+ end
113
+
114
+ it 'sets not the group for the remote file' do
115
+ allow(subject).to receive(:group_changed?).and_return false
116
+ expect(upload_handler).not_to receive(:execute)
117
+ subject.set_group
118
+ end
119
+ end
120
+
70
121
  describe '#file_changed?' do
71
122
  it 'uses the "digest_cmd" to check file changes' do
72
123
  expect(subject).to receive(:__check__).with(%Q(echo "#{digest} /var/www/shared/config/database.yml" | md5sum -c --status))
@@ -82,22 +133,39 @@ module Capistrano
82
133
  end
83
134
 
84
135
  it 'replaces "<path>"' do
136
+ allow(subject).to receive(:__check__)
85
137
  subject.digest_cmd = '%<path>s'
86
- expect(subject).to receive(:__check__).with(remote_filename_expented)
87
138
 
88
139
  subject.file_changed?
140
+
141
+ expect(subject).to have_received(:__check__).with(remote_filename_expented)
89
142
  end
90
143
  end
91
144
 
92
145
  describe '#permission_changed?' do
93
146
  it 'checks the actual file permissions' do
94
- expect(subject).to receive(:__check__).with(mode_test_cmd % { mode: '0640', path: remote_filename_expented })
95
-
147
+ allow(subject).to receive(:__check__)
96
148
  subject.permission_changed?
149
+
150
+ expect(subject).to have_received(:__check__).with(mode_test_cmd % { mode: '0640', path: remote_filename_expented })
151
+ end
152
+ end
153
+
154
+ describe '#user_changed?' do
155
+ it 'returns "false" when no user is given' do
156
+ subject.user = nil
157
+ expect(subject.user_changed?).to be_falsy
158
+ end
159
+
160
+ it 'checks the actual user' do
161
+ allow(subject).to receive(:__check__)
162
+ subject.user_changed?
163
+ expect(subject).to have_received(:__check__).with(user_test_cmd % { user: 'deploy', path: remote_filename_expented })
97
164
  end
98
165
  end
99
166
 
100
167
  end
168
+ # rubocop: enable Metrics/ModuleLength
101
169
  end
102
170
  end
103
171
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-template
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dieter Späth
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-27 00:00:00.000000000 Z
11
+ date: 2016-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capistrano
@@ -139,10 +139,19 @@ files:
139
139
  - lib/capistrano/template/helpers/uploader.rb
140
140
  - lib/capistrano/template/tasks/template_defaults.rake
141
141
  - lib/capistrano/template/version.rb
142
+ - spec/dummy_app/Capfile
143
+ - spec/dummy_app/Vagrantfile
144
+ - spec/dummy_app/config/deploy.rb
145
+ - spec/dummy_app/config/deploy/development.rb
146
+ - spec/dummy_app/config/deploy/templates/development/localhost/other.template.name.erb
147
+ - spec/dummy_app/lib/capistrano/tasks/test_template.rake
148
+ - spec/dummy_app/log/.gitkeep
149
+ - spec/dummy_app/test.sh
142
150
  - spec/integration/capistrano/template/helpers/dsl_spec.rb
143
151
  - spec/integration/capistrano/template/helpers/paths_lookup_spec.rb
144
152
  - spec/integration/capistrano/template/helpers/uploader_spec.rb
145
153
  - spec/spec_helper.rb
154
+ - spec/unit/capistrano/template/helpers/dsl_dry_run_spec.rb
146
155
  - spec/unit/capistrano/template/helpers/dsl_spec.rb
147
156
  - spec/unit/capistrano/template/helpers/paths_lookup_spec.rb
148
157
  - spec/unit/capistrano/template/helpers/renderer_spec.rb
@@ -174,10 +183,19 @@ signing_key:
174
183
  specification_version: 4
175
184
  summary: Erb-Template rendering and upload for capistrano 3
176
185
  test_files:
186
+ - spec/dummy_app/Capfile
187
+ - spec/dummy_app/Vagrantfile
188
+ - spec/dummy_app/config/deploy.rb
189
+ - spec/dummy_app/config/deploy/development.rb
190
+ - spec/dummy_app/config/deploy/templates/development/localhost/other.template.name.erb
191
+ - spec/dummy_app/lib/capistrano/tasks/test_template.rake
192
+ - spec/dummy_app/log/.gitkeep
193
+ - spec/dummy_app/test.sh
177
194
  - spec/integration/capistrano/template/helpers/dsl_spec.rb
178
195
  - spec/integration/capistrano/template/helpers/paths_lookup_spec.rb
179
196
  - spec/integration/capistrano/template/helpers/uploader_spec.rb
180
197
  - spec/spec_helper.rb
198
+ - spec/unit/capistrano/template/helpers/dsl_dry_run_spec.rb
181
199
  - spec/unit/capistrano/template/helpers/dsl_spec.rb
182
200
  - spec/unit/capistrano/template/helpers/paths_lookup_spec.rb
183
201
  - spec/unit/capistrano/template/helpers/renderer_spec.rb