Add fudo ssh key script
This commit is contained in:
parent
81fa83834b
commit
7486922c1d
|
@ -0,0 +1,48 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
require 'uri'
|
||||||
|
require 'net/http'
|
||||||
|
require 'net/https'
|
||||||
|
require 'json'
|
||||||
|
require 'socket'
|
||||||
|
|
||||||
|
if ! ENV['FUDO_GIT_TOKEN']
|
||||||
|
puts "FUDO_GIT_TOKEN must be set first"
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
token = ENV['FUDO_GIT_TOKEN']
|
||||||
|
|
||||||
|
if ARGV.length != 1
|
||||||
|
puts "usage: #{$0} <filename>"
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
filename = ARGV[0]
|
||||||
|
|
||||||
|
if not File::exist?(filename)
|
||||||
|
puts "file does not exist: #{filename}"
|
||||||
|
exit 2
|
||||||
|
end
|
||||||
|
|
||||||
|
target_uri = URI.parse("https://git.fudo.org/api/v1/admin/users/fudo/keys")
|
||||||
|
|
||||||
|
key = File::open(filename).read.strip
|
||||||
|
|
||||||
|
hostname = Socket::gethostname
|
||||||
|
|
||||||
|
@payload = {
|
||||||
|
key: key,
|
||||||
|
read_only: true,
|
||||||
|
title: "#{hostname} fudo key"
|
||||||
|
}
|
||||||
|
|
||||||
|
https = Net::HTTP.new(target_uri.host, target_uri.port)
|
||||||
|
https.use_ssl = true
|
||||||
|
req = Net::HTTP::Post.new(target_uri.path, initheader = {
|
||||||
|
'Content-Type' => 'application/json',
|
||||||
|
'Authorization' => "token #{token}"
|
||||||
|
})
|
||||||
|
req.body = @payload.to_json
|
||||||
|
res = https.request(req)
|
||||||
|
puts "response #{res.code} #{res.message}: #{res.body}"
|
Loading…
Reference in New Issue