Class: KnifeGithubTokenCreate::GithubTokenCreate
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- KnifeGithubTokenCreate::GithubTokenCreate
- Defined in:
- lib/chef/knife/github_token_create.rb
Instance Method Summary collapse
-
#create_github_token(params) ⇒ Object
Create the OAuth authentication token for the knife-github application.
- #delete_github_token(params) ⇒ Object
- #run ⇒ Object
-
#update_knife_config(token) ⇒ Object
Updates the knife configuration with the token information inside ~/.chef/knife.rb.
-
#validate_github_token(username = nil) ⇒ Object
Validate the OAuth authentication token for the knife-github application.
Instance Method Details
#create_github_token(params) ⇒ Object
Create the OAuth authentication token for the knife-github application.
154 155 156 157 158 159 160 |
# File 'lib/chef/knife/github_token_create.rb', line 154 def create_github_token(params) Chef::Log.debug("Creating new application token for user: #{username}.") params[:url] = @github_url + "/api/" + @github_api_version + "/authorizations" params[:body] = '{"note":"knife-github","scopes":["delete_repo", "user", "public_repo", "repo", "gist"]"}' params[:action] = "POST" connection.request(params) end |
#delete_github_token(params) ⇒ Object
162 163 164 165 166 167 |
# File 'lib/chef/knife/github_token_create.rb', line 162 def delete_github_token(params) Chef::Log.debug("Deleting token id: #{params[':id']}") params[:url] = @github_url + "/api/" + @github_api_version + "/authorizations/#{params[:id]}" params[:action] = "DELETE" connection.request(params) end |
#run ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/chef/knife/github_token_create.rb', line 62 def run extend Chef::Mixin::ShellOut # validate base options from base module. # Display information if debug mode is on. display_debug_info # Get the name_args from the command line username = name_args.first # Get token information token = get_github_token() unless config[:force] # Create github token if needed if token.nil? token = validate_github_token(username) update_knife_config(token) end puts "Finished updating your token. Using key:#{token}" end |
#update_knife_config(token) ⇒ Object
Updates the knife configuration with the token information inside ~/.chef/knife.rb
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/chef/knife/github_token_create.rb', line 89 def update_knife_config(token) contents = '' update = false config = File.join(ENV["HOME"], ".chef/knife.rb") File.foreach(config) do |line| if line =~ /^\s*knife\[:github_token\].*/ && !token.nil? Chef::Log.debug("Replacing current token with: #{token}") contents = contents << "knife[:github_token] = \"#{token}\"\n" update = true else contents = contents << line end end unless update Chef::Log.debug("Updating configuration with token: #{token}") contents = contents << "knife[:github_token] = \"#{token}\"\n" end File.open(config, 'w') {|f| f.write(contents) } return true end |
#validate_github_token(username = nil) ⇒ Object
Validate the OAuth authentication token for the knife-github application.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/chef/knife/github_token_create.rb', line 113 def validate_github_token(username=nil) params = {} username = ENV["USER"] if username.nil? params[:url] = @github_url + "/api/" + @github_api_version + "/authorizations" Chef::Log.debug("Validating token information for user: #{username}.") params[:username] = username params[:password] = HighLine.new.ask("Please enter github password for #{username} :") { |q| q.echo = "x" } params[:action] = "GET" token_key = nil result = connection.request(params) result.each do |token| if token['app'] && token['app']['name'] == "knife-github (API)" if token['scopes'].include?("delete_repo") Chef::Log.debug("Found and using token: #{token_key}") token_key = token['token'] else Chef::Log.debug("Found token: #{token_key} but wrong scope, deleting token.") params[:id] = token['id'] delete_github_token(params) end end end if token_key.nil? result = create_github_token(params) token_key = result['token'] end return token_key end |