Class: Travis::CLI::Sshkey

Inherits:
RepoCommand show all
Defined in:
lib/travis/cli/sshkey.rb

Constant Summary

Constants inherited from RepoCommand

RepoCommand::GIT_REGEX, RepoCommand::TRAVIS

Constants inherited from Command

Command::DAY, Command::HOUR, Command::MINUTE, Command::WEEK

Constants included from Tools::Assets

Tools::Assets::BASE

Instance Attribute Summary

Attributes inherited from RepoCommand

#slug

Attributes inherited from ApiCommand

#enterprise_name, #session

Attributes inherited from Command

#arguments, #config, #debug, #force_interactive, #formatter, #input, #output

Instance Method Summary collapse

Methods inherited from RepoCommand

#repository, #setup

Methods inherited from ApiCommand

#authenticate, #detected_endpoint?, #endpoint_config, #enterprise?, #initialize, #org?, #pro?, #setup, #sync

Methods included from Travis::Client::Methods

#access_token, #access_token=, #account, #accounts, #api_endpoint, #api_endpoint=, #artifact, #broadcasts, #build, #cancel, #explicit_api_endpoint?, #github_auth, #hooks, #job, #lint, #listen, #repo, #repos, #restart, #user

Methods inherited from Command

abstract, abstract?, #check_completion, #check_ruby, #check_version, command_name, #command_name, #debug?, description, #execute, #help, #info, #initialize, #last_check, #on_signal, #parse, #say, #setup, skip, subcommands, #terminal, #time, #usage, #usage_for, #write_to

Methods included from Tools::Assets

#asset, #asset_path

Methods included from Parser

#new, #on, #on_initialize

Constructor Details

This class inherits a constructor from Travis::CLI::ApiCommand

Instance Method Details

#check_access(gh) ⇒ Object



72
73
74
75
76
# File 'lib/travis/cli/sshkey.rb', line 72

def check_access(gh)
  gh["repos/#{slug}"]
rescue GH::Error
  error "GitHub account has no read access to #{color slug, :bold}"
end

#delete_keyObject



41
42
43
44
45
46
47
# File 'lib/travis/cli/sshkey.rb', line 41

def delete_key
  return if interactive? and not danger_zone? "Remove SSH key for #{color slug, :info}?"
  say "removing ssh key for #{color slug, :info}"
  ssh_key.delete
rescue Travis::Client::NotFound
  warn "no key found to remove"
end

#display_keyObject



27
28
29
30
31
32
33
# File 'lib/travis/cli/sshkey.rb', line 27

def display_key
  say "Current SSH key: #{color(ssh_key.description, :info)}"
  say "Finger print:    #{color(ssh_key.fingerprint, :info)}"
rescue Travis::Client::NotFound
  say "No custom SSH key installed."
  exit 1 if check?
end

#generate_keyObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/travis/cli/sshkey.rb', line 49

def generate_key
  github.with_basic_auth do |gh|
    check_access(gh)
    empty_line

    say "Generating RSA key."
    private_key        = Tools::SSLKey.generate_rsa
    self.description ||= "key for fetching dependencies for #{slug}"

    say "Uploading public key to GitHub."
    gh.post("/user/keys", :title => "#{description} (Travis CI)", :key => Tools::SSLKey.rsa_ssh(private_key.public_key))

    say "Uploading private key to Travis CI."
    ssh_key.update(:value => private_key.to_s, :description => description)

    empty_line
    if agree("Store private key? ") { |q| q.default = "no" }
      path = ask("Path: ") { |q| q.default = "id_travis_rsa" }
      File.write(path, private_key.to_s)
    end
  end
end

#githubObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/travis/cli/sshkey.rb', line 78

def github
  @github ||= begin
    load_gh
    Tools::Github.new(session.config['github']) do |g|
      g.note          = "token for fetching dependencies for #{slug} (Travis CI)"
      g.explode       = explode?
      g.     = proc { ask("Username: ") }
      g.ask_password  = proc { |user| ask("Password for #{user}: ") { |q| q.echo = "*" } }
      g.ask_otp       = proc { |user| ask("Two-factor authentication code for #{user}: ") }
      g.  = proc {  }
      g.debug         = proc { |log| debug(log) }
      g.after_tokens  = proc { g.explode = true and error("no suitable github token found") }
    end
  end
end

#login_headerObject



94
95
96
97
98
99
# File 'lib/travis/cli/sshkey.rb', line 94

def 
  say "We need the #{color("GitHub login", :important)} for the account you want to add the key to."
  say "This information will #{color("not be sent to Travis CI", :important)}, only to #{color(github_endpoint.host, :info)}."
  say "The password will not be displayed."
  empty_line
end

#runObject



18
19
20
21
22
23
24
25
# File 'lib/travis/cli/sshkey.rb', line 18

def run
  error "SSH keys are not available on #{color(session.config['host'], :bold)}" if org?
  delete_key                            if delete?
  update_key File.read(upload), upload  if upload?
  update_key $stdin.read, 'stdin'       if stdin?
  generate_key                          if generate?
  display_key
end

#update_key(value, file) ⇒ Object



35
36
37
38
39
# File 'lib/travis/cli/sshkey.rb', line 35

def update_key(value, file)
  self.description ||= ask("Key description: ") { |q| q.default = file } if interactive?
  say "updating ssh key for #{color slug, :info} with key from #{color file, :info}"
  ssh_key.update(:value => value, :description => description || file)
end