Class: DeployAgent::CertificateManager

Inherits:
Object
  • Object
show all
Defined in:
lib/deploy_agent/certificate_manager.rb

Instance Method Summary collapse

Instance Method Details

#certificate_uriObject



10
11
12
# File 'lib/deploy_agent/certificate_manager.rb', line 10

def certificate_uri
  URI(ENV['DEPLOY_AGENT_CERTIFICATE_URL'] || 'https://api.deployhq.com/api/v1/agents/create')
end

#generate_certificateObject



14
15
16
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
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/deploy_agent/certificate_manager.rb', line 14

def generate_certificate
  FileUtils.mkdir_p(CONFIG_PATH)
  unless File.file?(ACCESS_PATH)
    File.write(ACCESS_PATH, "# This file contains a list of host and network addresses the Deploy agent\n # will allow connections to. Add IPs or networks (CIDR format) as needed.\n\n# Allow deployments to localhost\n127.0.0.1\n::1\n")
  end

  puts 'This tool will assist you in generating a certificate for your Deploy agent.'
  puts
  if File.file?(CERTIFICATE_PATH)
    puts "***************************** WARNING *****************************"
    puts "The Deploy agent has already been configured. Are you sure you wish"
    puts "to remove the existing certificate and generate a new one?"
    puts
    Readline.completion_proc = Proc.new {}
    begin
      response = Readline.readline("Remove existing certificate? [no]: ", true)
    rescue Interrupt => e
      puts
      Process.exit(1)
    end
    unless response == 'yes'
      Process.exit(1)
    end
    puts
  end
  puts 'Please enter a name for this agent.'
  Readline.completion_proc = Proc.new {}
  begin
    name = Readline.readline("Agent Name: ", true)
  rescue Interrupt => e
    puts
    Process.exit(1)
  end
  if name.length < 2
    puts "Name must be at least 2 characters."
    Process.exit(1)
  else
    uri = certificate_uri
    Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
      request = Net::HTTP::Post.new(uri)
      request.body = {:name => name}.to_json
      request['Content-Type'] = 'application/json'
      response = http.request request
      response_hash = JSON.parse(response.body)
      if response_hash['status'] == 'success'
        File.write(CERTIFICATE_PATH, response_hash['data']['certificate'])
        File.write(KEY_PATH,         response_hash['data']['private_key'])
        puts
        puts "Certificate has been successfully generated and installed."
        puts
        puts "You can now associate this Agent with your Deploy account."
        puts "Browse to Settings -> Agents in your account and enter the code below:"
        puts
        puts " >> #{response_hash['data']['claim_code']} <<"
        puts
        puts "You can start the agent using the following command:"
        puts
        puts " # deploy-agent start"
        puts
      else
        puts
        puts "An error occurred obtaining a certificate."
        puts "Please contact support, quoting the debug information below:"
        puts
        puts response.inspect
        puts response.body
        puts
        Process.exit(1)
      end
    end
  end
end