Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
Store git info with deployment metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ncdc authored and danmcp committed Oct 7, 2013
1 parent 48271e5 commit 1c9b1da
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
Expand Up @@ -78,6 +78,13 @@ def post_configure(cart_name, template_git_url=nil)

write_deployment_metadata(deployment_datetime, 'state', 'DEPLOYED')

application_repository = ApplicationRepository.new(self)
git_sha1 = application_repository.get_sha1('master')
if git_sha1 != ''
write_deployment_metadata(deployment_datetime, 'git_sha1', git_sha1)
write_deployment_metadata(deployment_datetime, 'git_ref', 'master')
end

deployments_dir = PathUtils.join(@container_dir, 'app-deployments')
set_rw_permission_R(deployments_dir)
reset_permission_R(deployments_dir)
Expand Down Expand Up @@ -320,7 +327,12 @@ def post_receive(options={})
end

repo_dir = PathUtils.join(@container_dir, 'app-deployments', options[:deployment_datetime], 'repo')
ApplicationRepository.new(self).archive(repo_dir, options[:ref] || 'master')
application_repository = ApplicationRepository.new(self)
git_ref = options[:ref] || 'master'
application_repository.archive(repo_dir, git_ref)
git_sha1 = application_repository.get_sha1(git_ref)
write_deployment_metadata(options[:deployment_datetime], 'git_sha1', git_sha1)
write_deployment_metadata(options[:deployment_datetime], 'git_ref', git_ref)

build(options)

Expand Down
19 changes: 19 additions & 0 deletions node/lib/openshift-origin-node/model/application_repository.rb
Expand Up @@ -202,6 +202,20 @@ def archive(destination, ref)
@container.run_in_container_context("/bin/rm -rf #{cache} &")
end

def get_sha1(ref)
@deployment_ref = ref

out, _, rc = @container.run_in_container_context(ERB.new(GIT_GET_SHA1).result(binding),
chdir: @path)

if 0 == rc
out.chomp
else
# if the repo is empty (no commits) or the ref is invalid, the rc will be nonzero
''
end
end

def destroy
FileUtils.rm_r(@path) if File.exist? @path
end
Expand Down Expand Up @@ -319,6 +333,11 @@ def ssh_like?(url)
auto = 100
}

GIT_GET_SHA1 = %Q{
set -xe;
git rev-parse --short <%= @deployment_ref %>
}

PRE_RECEIVE = %q{
gear prereceive
}
Expand Down
23 changes: 23 additions & 0 deletions node/test/unit/build_lifecycle_test.rb
Expand Up @@ -81,6 +81,10 @@ def test_post_receive_default_builder_nonscaled
@container.expects(:create_deployment_dir).returns(deployment_datetime)

repository.expects(:archive).with(PathUtils.join(@container.container_dir, 'app-deployments', deployment_datetime, 'repo'), 'master')
git_sha1 = 'abcd1234'
repository.expects(:get_sha1).with('master').returns(git_sha1)
@container.expects(:write_deployment_metadata).with(deployment_datetime, 'git_sha1', git_sha1)
@container.expects(:write_deployment_metadata).with(deployment_datetime, 'git_ref', 'master')

@container.expects(:build).with(out: $stdout, err: $stderr, deployment_datetime: deployment_datetime)
@container.expects(:prepare).with(out: $stdout, err: $stderr, deployment_datetime: deployment_datetime)
Expand Down Expand Up @@ -115,6 +119,10 @@ def test_post_receive_default_builder_scaled
@container.expects(:create_deployment_dir).returns(deployment_datetime)

repository.expects(:archive).with(PathUtils.join(@container.container_dir, 'app-deployments', deployment_datetime, 'repo'), 'master')
git_sha1 = 'abcd1234'
repository.expects(:get_sha1).with('master').returns(git_sha1)
@container.expects(:write_deployment_metadata).with(deployment_datetime, 'git_sha1', git_sha1)
@container.expects(:write_deployment_metadata).with(deployment_datetime, 'git_ref', 'master')

@container.expects(:build).with(out: $stdout, err: $stderr, deployment_datetime: deployment_datetime)
@container.expects(:prepare).with(out: $stdout, err: $stderr, deployment_datetime: deployment_datetime)
Expand Down Expand Up @@ -150,6 +158,10 @@ def test_post_receive_default_hot_deploy
@container.expects(:create_deployment_dir).never

repository.expects(:archive).with(PathUtils.join(@container.container_dir, 'app-deployments', deployment_datetime, 'repo'), 'master')
git_sha1 = 'abcd1234'
repository.expects(:get_sha1).with('master').returns(git_sha1)
@container.expects(:write_deployment_metadata).with(deployment_datetime, 'git_sha1', git_sha1)
@container.expects(:write_deployment_metadata).with(deployment_datetime, 'git_ref', 'master')

@container.expects(:build).with(out: $stdout, err: $stderr, deployment_datetime: deployment_datetime, hot_deploy: true)
@container.expects(:prepare).with(out: $stdout, err: $stderr, deployment_datetime: deployment_datetime, hot_deploy: true)
Expand Down Expand Up @@ -324,6 +336,12 @@ def test_post_configure_defaults
@container.expects(:prepare).with(deployment_datetime: latest_deployment_datetime)
@container.expects(:update_repo_symlink).with(latest_deployment_datetime)
@container.expects(:write_deployment_metadata).with(latest_deployment_datetime, 'state', 'DEPLOYED')
git_sha1 = 'abcd1234'
repository = mock()
::OpenShift::Runtime::ApplicationRepository.expects(:new).with(@container).returns(repository)
repository.expects(:get_sha1).with('master').returns(git_sha1)
@container.expects(:write_deployment_metadata).with(latest_deployment_datetime, 'git_sha1', git_sha1)
@container.expects(:write_deployment_metadata).with(latest_deployment_datetime, 'git_ref', 'master')
@container.expects(:set_rw_permission_R).with(File.join(@container.container_dir, 'app-deployments'))
@container.expects(:reset_permission_R).with(File.join(@container.container_dir, 'app-deployments'))
@cartridge_model.expects(:post_configure).with(cart_name)
Expand All @@ -346,6 +364,11 @@ def test_post_configure_empty_clone_spec_prevents_build
@container.expects(:prepare).with(deployment_datetime: latest_deployment_datetime)
@container.expects(:update_repo_symlink).with(latest_deployment_datetime)
@container.expects(:write_deployment_metadata).with(latest_deployment_datetime, 'state', 'DEPLOYED')
repository = mock()
::OpenShift::Runtime::ApplicationRepository.expects(:new).with(@container).returns(repository)
repository.expects(:get_sha1).with('master').returns('')
@container.expects(:write_deployment_metadata).with(latest_deployment_datetime, 'git_sha1', '').never
@container.expects(:write_deployment_metadata).with(latest_deployment_datetime, 'git_ref', 'master').never
@container.expects(:set_rw_permission_R).with(File.join(@container.container_dir, 'app-deployments'))
@container.expects(:reset_permission_R).with(File.join(@container.container_dir, 'app-deployments'))
@cartridge_model.expects(:post_configure).with(cart_name)
Expand Down

0 comments on commit 1c9b1da

Please sign in to comment.