Class: DeployAgent::ConfigurationGenerator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#claim_codeObject

Returns the value of attribute claim_code.



8
9
10
# File 'lib/deploy_agent/configuration_generator.rb', line 8

def claim_code
  @claim_code
end

Instance Method Details

#configureObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/deploy_agent/configuration_generator.rb', line 10

def configure
  FileUtils.mkdir_p(CONFIG_PATH)
  if File.file?(CERTIFICATE_PATH) || File.file?(ACCESS_PATH)
    puts "***************************** WARNING *****************************"
    puts "The Deploy agent has already been configured. Are you sure you wish"
    puts "to remove the existing configuration and generate a new one?"
    puts

    response = ask("Remove existing configuration? [no]: ")
    Process.exit(1) unless response == 'yes'
    puts
  end

  generate_certificate
  generate_access_list

  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 " >> #{claim_code} <<"
  puts
  puts "You can start the agent using the following command:"
  puts
  puts " # deploy-agent start"
  puts
end

#generate_access_listObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/deploy_agent/configuration_generator.rb', line 76

def generate_access_list
  puts "By default this utility only allows connections from DeployHQ to localhost."
  puts "To to deploy to other hosts or networks enter their addresses below:\n"

  user_hosts = []
  loop do
    host = ask("IP Address [leave blank to finish]: ")
    if host.empty?
      break
    else
      user_hosts << host
    end
  end

  begin
    access_list = File.open(ACCESS_PATH, 'w')

    # Add header and localhost entries
    access_list.write("# 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")

    # Add user entries (if any)
    access_list.write("\n# User defined destinations\n")
    user_hosts.each do |host|
      access_list.write("#{host}\n")
    end
  ensure
    access_list.close if access_list
  end
end

#generate_certificateObject



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
# File 'lib/deploy_agent/configuration_generator.rb', line 38

def generate_certificate
  puts 'This tool will assist you in generating a certificate for your Deploy agent.'
  puts 'Please enter a name for this agent.'

  name = ask("Agent Name: ")
  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'
        self.claim_code = response_hash['data']['claim_code']

        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
      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