Permalink
Browse files

WIP

  • Loading branch information...
1 parent 7a2b72b commit 6216746740d8a2ee67bc2ac2d01b552912b0699e Michael Schubert & Trace Wax committed Apr 9, 2012
Showing with 39 additions and 0 deletions.
  1. +39 −0 recipes/github_ssh_keys.rb
View
@@ -12,6 +12,25 @@
mode "0644"
end
+if !File.exist?("#{WS_HOME}/.ssh/known_hosts") || !File.read("#{WS_HOME}/.ssh/known_hosts").include?("github.com")
+ if node["github_username"].nil? || node["github_password"].nil?
+ require "tempfile"
+ credentials_script = Tempfile.new("github_credentials")
+ credentials_script << <<-RUBY
+ value = gets
+ puts value
+ RUBY
+ credentials_script.rewind
+ print "Github Username: "
+ node["github_username"] = `ruby #{credentials_script.path}`.strip
+ print "Github Password: "
+ node["github_password"] = `ruby #{credentials_script.path}`.strip
+ credentials_script.close
+ end
+end
+
+include_recipe "pivotal_workstation::rename_machine" unless node["github_project"]
+
execute "add github to knownhosts" do
user WS_USER
cwd "#{WS_HOME}/.ssh"
@@ -43,3 +62,23 @@
not_if "grep 'id_github_current' #{WS_HOME}/.ssh/config"
user WS_USER
end
+
+# add ssh keys to github account if possible
+if node["github_username"].to_s != "" && node["github_password"].to_s != ""
+ github_name="#{WS_USER}@#{node[:fqdn]}"
+ curl_options = %[--retry 3 --retry-delay 5 --retry-max-time 30 --connect-timeout 5 --max-time 30 -u "#{node["github_username"]}:#{node["github_password"]}"]
+ execute "upload ssh key to github if it does not already exist there" do
+ not_if <<-SH
+ curl #{curl_options} https://api.github.com/user/keys | grep "`cat #{WS_HOME}/.ssh/id_github_#{node["github_project"] || node[:fqdn]}.pub` | cut -f 2 -d ' '`"
+ SH
+ command <<-SH
+ curl -X POST --verbose #{curl_options} \
+ --data "{ \\"key\\": \\"`cat #{WS_HOME}/.ssh/id_github_#{node["github_project"] || node[:fqdn]}.pub`\\", \\"title\\": \\"#{github_name}\\" }" \
+ https://api.github.com/user/keys
+ SH
+ end
+ user WS_USER
+else
+ log "No GITHUB_USERNAME or GITHUB_PASSWORD provided, skipping steps to upload ssh keys to github.com"
+end
+

0 comments on commit 6216746

Please sign in to comment.