Project

General

Profile

Download (1.37 KB) Statistics
| Branch: | Tag: | Revision:

foreman_pipeline / app / lib / actions / foreman_pipeline / jenkins_instance / create_jenkins_instance_keys.rb @ be52ca64

1
require 'net/ssh'
2
require 'net/scp'
3
require 'uri'
4

    
5
module Actions
6
  module ForemanPipeline
7
    module JenkinsInstance
8
      class CreateJenkinsInstanceKeys < AbstractJenkinsInstanceAction     
9

    
10
        def run
11
          output[:pubkey] = fetch_pubkey(parse_jenkins_hostname)
12
        end
13

    
14
        def fetch_pubkey(host)
15
          buffer = StringIO.new    
16
          ip = Socket::getaddrinfo(host, 'www', nil, Socket::SOCK_STREAM)[0][3]
17

    
18
          Net::SSH.start(ip, 'root', :keys => [input.fetch(:cert_path)]) do |ssh|
19
            status = ssh_exec!(ssh, "ls #{key_location}")[2]           
20
            ssh.exec!("mkdir #{key_location}") if status > 0
21
            ssh.exec! "sed -i s:Defaults\\.*requiretty:#Defaults\\ requiretty:g /etc/sudoers"
22
            ssh.exec! "rm -f #{key_location}/#{parse_jenkins_hostname}.pub #{key_location}/#{parse_jenkins_hostname}"
23
            ssh.exec! "sudo -u jenkins ssh-keygen -f #{key_location}/#{parse_jenkins_hostname} -t rsa -N '' -q"
24
            ssh.scp.download! "#{key_location}/#{parse_jenkins_hostname}.pub", buffer
25
          end  
26

    
27
          buffer.string
28
        end
29

    
30
        def key_location
31
          "#{input.fetch(:jenkins_home)}/.ssh"
32
        end
33

    
34
        def finalize
35
          if output[:pubkey].match(/^ssh-rsa/)
36
            output[:status] = 0
37
          else
38
            output[:status] = 1
39
          end
40
        end
41

    
42
      end
43
    end
44
  end
45
end