Class: Chef::Provider::Git

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

Instance Attribute Summary

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #node

Instance Method Summary collapse

Methods included from Mixin::Command

handle_command_failures, not_if, only_if, output_of_command, popen4, run_command, run_command_with_systems_locale

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #initialize

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

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

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



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

def action_checkout
  if !::File.exist?(@new_resource.destination) || Dir.entries(@new_resource.destination) == ['.','..']
    clone
    checkout
    enable_submodules
    @new_resource.updated = true
  else
    Chef::Log.info "Taking no action, checkout destination #{@new_resource.destination} already exists or is a non-empty directory"
  end
end

#action_exportObject



49
50
51
52
53
# File 'lib/chef/provider/git.rb', line 49

def action_export
  action_checkout
  FileUtils.rm_rf(::File.join(@new_resource.destination,".git"))
  @new_resource.updated = true
end

#action_syncObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/chef/provider/git.rb', line 55

def action_sync
  if !::File.exist?(@new_resource.destination) || Dir.entries(@new_resource.destination) == ['.','..']
    action_checkout
  else
    sync
    enable_submodules
  end

  @new_resource.updated = true
end

#checkoutObject



91
92
93
94
95
96
# File 'lib/chef/provider/git.rb', line 91

def checkout
  sha_ref = revision_sha
  Chef::Log.info "Checking out branch: #{@new_resource.revision} reference: #{sha_ref}"
  # checkout into a local branch rather than a detached HEAD
  run_command(run_options(:command => "#{git} checkout -b deploy #{sha_ref}", :cwd => @new_resource.destination))
end

#cloneObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chef/provider/git.rb', line 78

def clone
  remote = @new_resource.remote

  args = []
  args << "-o #{remote}" unless remote == 'origin'
  args << "--depth #{@new_resource.depth}" if @new_resource.depth
  
  Chef::Log.info "Cloning repo #{@new_resource.repository} to #{@new_resource.destination}"
  
  clone_cmd = "#{git} clone #{args.join(' ')} #{@new_resource.repository} #{@new_resource.destination}"
  run_command(run_options(:command => clone_cmd))
end

#enable_submodulesObject



98
99
100
101
102
103
104
# File 'lib/chef/provider/git.rb', line 98

def enable_submodules
  if @new_resource.enable_submodules
    Chef::Log.info "Enabling git submodules"
    command = "#{git} submodule init && #{git} submodule update"
    run_command(run_options(:command => command, :cwd => @new_resource.destination))
  end
end

#find_current_revisionObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/chef/provider/git.rb', line 66

def find_current_revision
  if ::File.exist?(::File.join(cwd, ".git"))
    status, result, error_message = output_of_command("git rev-parse HEAD", run_options(:cwd=>cwd))
    
    # 128 is returned when we're not in a git repo. this is fine
    unless [0,128].include?(status.exitstatus)
      handle_command_failures(status, "STDOUT: #{result}\nSTDERR: #{error_message}")
    end
  end
  sha_hash?(result) ? result : nil
end

#load_current_resourceObject



31
32
33
34
35
36
# File 'lib/chef/provider/git.rb', line 31

def load_current_resource
  @current_resource = Chef::Resource::Git.new(@new_resource.name)
  if current_revision = find_current_revision
    @current_resource.revision current_revision
  end
end

#remote_resolve_referenceObject



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/chef/provider/git.rb', line 144

def remote_resolve_reference
  command = scm('ls-remote', @new_resource.repository, @new_resource.revision)
  Chef::Log.debug("Executing #{command}")
  begin
    status, result, error_message = output_of_command(command, run_options)
    handle_command_failures(status, "STDOUT: #{result}\nSTDERR: #{error_message}")
  rescue RuntimeError => e
    raise RuntimeError, e.message + "\n" + "Could not access the remote Git repository. "+
          "If this is a private repository, please verify that the deploy key for your application " +
          "has been added to your remote Git account."
  end
  result
end

#revision_shaObject Also known as: revision_slug



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/chef/provider/git.rb', line 129

def revision_sha
  @revision_sha ||= begin
    assert_revision_not_remote
    
    if sha_hash?(@new_resource.revision)
      @revision_sha = @new_resource.revision 
    else
      resolved_reference = remote_resolve_reference
      @revision_sha = extract_revision(resolved_reference)
    end
  end
end

#syncObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/chef/provider/git.rb', line 106

def sync
  revision = revision_sha
  sync_command = []

  # Use git-config to setup a remote tracking branches. Could use
  # git-remote but it complains when a remote of the same name already
  # exists, git-config will just silenty overwrite the setting every
  # time. This could cause wierd-ness in the remote cache if the url
  # changes between calls, but as long as the repositories are all
  # based from each other it should still work fine.
  if @new_resource.remote != 'origin'
    Chef::Log.info  "Configuring remote tracking branches for repository #{@new_resource.repository} "+
                    "at remote #{@new_resource.remote}"
    sync_command << "#{git} config remote.#{@new_resource.remote}.url #{@new_resource.repository}"
    sync_command << "#{git} config remote.#{@new_resource.remote}.fetch +refs/heads/*:refs/remotes/#{@new_resource.remote}/*"
  end

  # since we're in a local branch already, just reset to specified revision rather than merge
  sync_command << "#{git} fetch #{@new_resource.remote} --tags && #{git} reset --hard #{revision}"
  Chef::Log.info "Fetching updates from #{new_resource.remote} and resetting to revison #{revision}"
  run_command(run_options(:command => sync_command.join(" && "), :cwd => @new_resource.destination))
end