Created
December 10, 2015 01:44
-
-
Save stevecrozz/2289215921e767eaba95 to your computer and use it in GitHub Desktop.
Capistrano Install deploy keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ask :bitbucket_username, nil | |
| ask :bitbucket_password, nil | |
| set :bitbucket_deploy_key_path, '/home/ubuntu/.ssh/id_rsa.pub' | |
| set :bitbucket_deploy_key_label, 'capistrano-bitbucket' | |
| desc 'Generate SSH key pair' | |
| task :generate_ssh_keys do | |
| on roles(:all) do | |
| execute '[ -f ~/.ssh/id_rsa ] || ssh-keygen -b 2048 -t rsa -f ~/.ssh/id_rsa -q -N ""' | |
| end | |
| end | |
| before :starting, :generate_ssh_keys | |
| desc 'Install deploy keys' | |
| task :install_deploy_keys do | |
| on roles(:all) do | |
| unless test("[ -f ~/.bitbucket-deploy-key-label ]") | |
| user, pass = fetch(:bitbucket_username), fetch(:bitbucket_password) | |
| label = fetch(:bitbucket_deploy_key_label) | |
| Net::HTTP.start('bitbucket.org', use_ssl: true) do |http| | |
| index = '/api/1.0/repositories/brandedcrate/rxaid/deploy-keys/' | |
| request = Net::HTTP::Get.new index | |
| request.basic_auth user, pass | |
| response = JSON.parse(http.request(request).body) | |
| if response.none? { |k| k['label'] == label } | |
| request = Net::HTTP::Post.new(index, 'Content-Type' => 'application/json') | |
| request.basic_auth user, pass | |
| request.body = JSON.dump( | |
| label: label, | |
| key: capture("cat #{fetch(:bitbucket_deploy_key_path)}")) | |
| response = http.request request | |
| execute("echo #{label} > ~/.bitbucket-deploy-key-label") | |
| end | |
| end | |
| end | |
| end | |
| end | |
| before :starting, :install_deploy_keys |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment