Class: Chef::Provider::Subversion

Inherits:
Chef::Provider show all
Includes:
Mixin::Command
Defined in:
lib/chef/provider/subversion.rb

Constant Summary collapse

SVN_INFO_PATTERN =
/^([\w\s]+): (.+)$/

Instance Attribute Summary

Attributes inherited from Chef::Provider

#action, #current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_with_systems_locale

Methods included from Mixin::Command::Windows

#popen4

Methods included from Mixin::Command::Unix

#popen4

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #cleanup_after_converge, #converge, #cookbook_name, #events, #initialize, #node, #process_resource_requirements, #requirements, #resource_collection, #run_action, #whyrun_mode?

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::EnforceOwnershipAndPermissions

#access_controls, #enforce_ownership_and_permissions

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #platform_family?, #search, #value_for_platform, #value_for_platform_family

Constructor Details

This class inherits a constructor from Chef::Provider

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::Mixin::RecipeDefinitionDSLCore

Instance Method Details

#action_checkoutObject



61
62
63
64
65
66
67
68
69
# File 'lib/chef/provider/subversion.rb', line 61

def action_checkout
  if target_dir_non_existent_or_empty?
    converge_by("perform checkout of #{@new_resource.repository} into #{@new_resource.destination}") do
      run_command(run_options(:command => checkout_command))
    end
  else
    Chef::Log.debug "#{@new_resource} checkout destination #{@new_resource.destination} already exists or is a non-empty directory - nothing to do"
  end
end

#action_exportObject



71
72
73
74
75
76
77
# File 'lib/chef/provider/subversion.rb', line 71

def action_export
  if target_dir_non_existent_or_empty?
    action_force_export
  else
    Chef::Log.debug "#{@new_resource} export destination #{@new_resource.destination} already exists or is a non-empty directory - nothing to do"
  end
end

#action_force_exportObject



79
80
81
82
83
# File 'lib/chef/provider/subversion.rb', line 79

def action_force_export
  converge_by("export #{@new_resource.repository} into #{@new_resource.destination}") do
    run_command(run_options(:command => export_command))
  end
end

#action_syncObject



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

def action_sync
  assert_target_directory_valid!
  if ::File.exist?(::File.join(@new_resource.destination, ".svn"))
    current_rev = find_current_revision
    Chef::Log.debug "#{@new_resource} current revision: #{current_rev} target revision: #{revision_int}"
    unless current_revision_matches_target_revision?
      converge_by("sync #{@new_resource.destination} from #{@new_resource.repository}") do
        run_command(run_options(:command => sync_command))
        Chef::Log.info "#{@new_resource} updated to revision: #{revision_int}"
      end 
    end
  else
    action_checkout
  end
end

#checkout_commandObject



107
108
109
110
111
112
# File 'lib/chef/provider/subversion.rb', line 107

def checkout_command
  c = scm :checkout, @new_resource.svn_arguments, verbose, authentication,
      "-r#{revision_int}", @new_resource.repository, @new_resource.destination
  Chef::Log.info "#{@new_resource} checked out #{@new_resource.repository} at revision #{@new_resource.revision} to #{@new_resource.destination}"
				c
end

#current_revision_matches_target_revision?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/chef/provider/subversion.rb', line 152

def current_revision_matches_target_revision?
  (!@current_resource.revision.nil?) && (revision_int.strip.to_i == @current_resource.revision.strip.to_i)
end

#define_resource_requirementsObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chef/provider/subversion.rb', line 49

def define_resource_requirements
  requirements.assert(:all_actions)  do |a|
    # Make sure the parent dir exists, or else fail.
    # for why run, print a message explaining the potential error.
    parent_directory = ::File.dirname(@new_resource.destination)
    a.assertion { ::File.directory?(parent_directory) }
    a.failure_message(Chef::Exceptions::MissingParentDirectory, 
      "Cannot clone #{@new_resource} to #{@new_resource.destination}, the enclosing directory #{parent_directory} does not exist")
    a.whyrun("Directory #{parent_directory} does not exist, assuming it would have been created")
  end
end

#export_commandObject



114
115
116
117
118
119
120
121
# File 'lib/chef/provider/subversion.rb', line 114

def export_command
  args = ["--force"]
  args << @new_resource.svn_arguments << verbose << authentication <<
      "-r#{revision_int}" << @new_resource.repository << @new_resource.destination
  c = scm :export, *args
  Chef::Log.info "#{@new_resource} exported #{@new_resource.repository} at revision #{@new_resource.revision} to #{@new_resource.destination}"
				c
end

#find_current_revisionObject



141
142
143
144
145
146
147
148
149
150
# File 'lib/chef/provider/subversion.rb', line 141

def find_current_revision
  return nil unless ::File.exist?(::File.join(@new_resource.destination, ".svn"))
  command = scm(:info)
  status, svn_info, error_message = output_of_command(command, run_options(:cwd => cwd))

  unless [0,1].include?(status.exitstatus)
    handle_command_failures(status, "STDOUT: #{svn_info}\nSTDERR: #{error_message}")
  end
  extract_revision_info(svn_info)
end

#load_current_resourceObject



39
40
41
42
43
44
45
46
47
# File 'lib/chef/provider/subversion.rb', line 39

def load_current_resource
  @current_resource = Chef::Resource::Subversion.new(@new_resource.name)

  unless [:export, :force_export].include?(Array(@new_resource.action).first)
    if current_revision = find_current_revision
      @current_resource.revision current_revision
    end
  end
end

#revision_intObject Also known as: revision_slug

If the specified revision isn’t an integer (“HEAD” for example), look up the revision id by asking the server If the specified revision is an integer, trust it.



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/chef/provider/subversion.rb', line 126

def revision_int
  @revision_int ||= begin
    if @new_resource.revision =~ /^\d+$/
      @new_resource.revision
    else
      command = scm(:info, @new_resource.repository, @new_resource.svn_info_args, authentication, "-r#{@new_resource.revision}")
      status, svn_info, error_message = output_of_command(command, run_options)
      handle_command_failures(status, "STDOUT: #{svn_info}\nSTDERR: #{error_message}")
      extract_revision_info(svn_info)
    end
  end
end

#run_options(run_opts = {}) ⇒ Object



156
157
158
159
160
# File 'lib/chef/provider/subversion.rb', line 156

def run_options(run_opts={})
  run_opts[:user] = @new_resource.user if @new_resource.user
  run_opts[:group] = @new_resource.group if @new_resource.group
  run_opts
end

#sync_commandObject



101
102
103
104
105
# File 'lib/chef/provider/subversion.rb', line 101

def sync_command
  c = scm :update, @new_resource.svn_arguments, verbose, authentication, "-r#{revision_int}", @new_resource.destination
  Chef::Log.debug "#{@new_resource} updated working copy #{@new_resource.destination} to revision #{@new_resource.revision}"
				c
end

#whyrun_supported?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/chef/provider/subversion.rb', line 35

def whyrun_supported?
  true
end