Class: VagrantPlugins::GithubKeyManager::Action::AddKey
- Inherits:
-
Object
- Object
- VagrantPlugins::GithubKeyManager::Action::AddKey
- Defined in:
- lib/vagrant-github_key_manager/action/add_key.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, env) ⇒ AddKey
constructor
A new instance of AddKey.
Constructor Details
#initialize(app, env) ⇒ AddKey
Returns a new instance of AddKey.
11 12 13 14 15 |
# File 'lib/vagrant-github_key_manager/action/add_key.rb', line 11 def initialize(app, env) @app = app @ui = env[:ui] @machine = env[:machine] end |
Instance Method Details
#call(env) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/vagrant-github_key_manager/action/add_key.rb', line 17 def call(env) config = env[:global_config].github_key_manager github_api_url = config.github_api_url ssh_key_title = config.ssh_key_title # if we have a successful response, skip generating key unless @machine.communicate.test('test -f ~/.ssh/github_key_id') @ui.info("We will now generate an ssh key and add it to your github account.") unless @machine.communicate.test('test -f ~/.ssh/github_rsa') @machine.communicate.execute('ssh-keygen -N "" -f ~/.ssh/github_rsa') @ui.info('Generate ssh key for github') end @ui.info("Register ssh key to your github account.") github_username = @ui.ask("What is your github account name? ") github_password = @ui.ask("What is your github account password? (not stored) ", echo: false) execute_script = <<EOF require "rubygems" require "json" require "net/https" require "uri" ssh_pub_key = File.read File.expand_path("~/.ssh/github_rsa.pub") uri = URI.parse "#{github_api_url}" https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = uri.scheme == "https" https.start do |https| req = Net::HTTP::Post.new(uri.path) req.basic_auth "#{github_username}", "#{github_password}" github_ssh_key_settings = { :title => "#{ssh_key_title}", :key => ssh_pub_key } req.body = JSON.generate github_ssh_key_settings response = https.request(req) res_data = JSON.parse response.body github_ssh_key_id = res_data["id"] File.open(File.expand_path("~/.ssh/github_key_id"), "w") do |file| file.puts github_ssh_key_id end end `ssh-add -D` `ssh-add ~/.ssh/github_rsa` EOF @machine.communicate.execute("ruby -e '#{execute_script}'") #@machine.communicate.execute('eval $(ssh-agent)') #@machine.communicate.execute('ssh-add ~/.ssh/github_rsa') @ui.info("register ssh key to github") end @app.call(env) end |