Class: Pero::Puppet

Inherits:
Object
  • Object
show all
Extended by:
SshExecutable
Defined in:
lib/pero/puppet.rb,
lib/pero/puppet/base.rb,
lib/pero/puppet/redhat.rb

Defined Under Namespace

Classes: Base, Redhat

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SshExecutable

ssh_exec!

Constructor Details

#initialize(host, options, mutex) ⇒ Puppet

Returns a new instance of Puppet.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pero/puppet.rb', line 27

def initialize(host, options, mutex)
  @options = options.dup
  @mutex = mutex

  @options[:host] = host
  so = ssh_options

  so.delete(:strict_host_key_checking) unless Net::SSH::VALID_OPTIONS.include?(:strict_host_key_checking)

  @specinfra = Specinfra::Backend::Ssh.new(
    request_pty: true,
    host: so[:host_name],
    ssh_options: so,
    disable_sudo: false
  )
end

Instance Attribute Details

#specinfraObject (readonly)

Returns the value of attribute specinfra.



25
26
27
# File 'lib/pero/puppet.rb', line 25

def specinfra
  @specinfra
end

Instance Method Details

#applyObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/pero/puppet.rb', line 121

def apply
  serve_master do |container|
    port = container.info['Ports'].first['PublicPort']
    https = Net::HTTP.new('localhost', port)
    https.use_ssl = true
    https.verify_mode = OpenSSL::SSL::VERIFY_NONE
    https.start do
      https.delete('/puppet-admin-api/v1/environment-cache')
    end

    begin
      tmpdir = container.info['id'][0..5]
      in_ssh_forwarding(port) do |host, ssh|
        Pero.log.info "#{host}:puppet cmd[#{puppet_cmd}]"
        cmd = "mkdir -p /tmp/puppet/#{tmpdir} && unshare -m -- /bin/bash -c 'export PATH=$PATH:/opt/puppetlabs/bin/ && \
                       mkdir -p `puppet config print ssldir` && mount --bind /tmp/puppet/#{tmpdir} `puppet config print ssldir` && \
                       #{puppet_cmd}'"
        Pero.log.debug "run cmd:#{cmd}"
        ssh_exec(ssh, host, cmd)

        if @options['one-shot']
          cmd = "/bin/rm -rf /tmp/puppet/#{tmpdir}"
          ssh_exec(ssh, host, cmd)
        end

        ssh.loop { true } if ENV['PERO_DEBUG']
      end
    rescue StandardError => e
      Pero.log.error "puppet apply error:#{e.inspect}"
    end
  end

  Pero::History::Attribute.new(specinfra, @options).save
end

#dockerObject



110
111
112
# File 'lib/pero/puppet.rb', line 110

def docker
  Pero::Docker.new(@options['server-version'], @options['image-name'], @options['environment'], @options['volumes'])
end

#in_ssh_forwarding(port) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/pero/puppet.rb', line 192

def in_ssh_forwarding(port)
  options = specinfra.get_config(:ssh_options)

  options.delete(:strict_host_key_checking) unless Net::SSH::VALID_OPTIONS.include?(:strict_host_key_checking)

  Pero.log.info "start forwarding #{specinfra.get_config(:host)}:8140 => localhost:#{port}"
  Net::SSH.start(
    specinfra.get_config(:host),
    options[:user],
    options
  ) do |ssh|
    ssh.forward.remote(port, 'localhost', 8140)
    yield specinfra.get_config(:host), ssh
  end
end

#installObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pero/puppet.rb', line 81

def install
  osi = specinfra.os_info
  os = case osi[:family]
       when 'redhat'
         Redhat.new(specinfra, osi)
       else
         raise 'sorry unsupport os, please pull request!!!'
       end
  os.install(@options['agent-version']) if @options['agent-version']
  Pero::History::Attribute.new(specinfra, @options).save
end

#parse_puppet_option(options) ⇒ Object



182
183
184
185
186
187
188
189
190
# File 'lib/pero/puppet.rb', line 182

def parse_puppet_option(options)
  ret = ''
  %w[noop verbose test].each do |n|
    ret << " --#{n}" if options[n]
  end
  ret << " --tags #{options['tags'].join(',')}" if options['tags']
  ret << " --environment #{options['environment']}" if options['environment']
  ret
end

#puppet_cmdObject



174
175
176
177
178
179
180
# File 'lib/pero/puppet.rb', line 174

def puppet_cmd
  if Gem::Version.new('5.0.0') > Gem::Version.new(@options['agent-version'])
    "puppet agent --no-daemonize --onetime #{parse_puppet_option(@options)} --ca_port 8140 --ca_server localhost --masterport 8140 --server localhost"
  else
    "/opt/puppetlabs/bin/puppet agent --no-daemonize --onetime #{parse_puppet_option(@options)} --ca_server localhost --masterport 8140 --server localhost"
  end
end

#run_containerObject



114
115
116
117
118
119
# File 'lib/pero/puppet.rb', line 114

def run_container
  @mutex.lock
  docker.alerady_run? || docker.run
ensure
  @mutex.unlock
end

#serve_masterObject



100
101
102
103
104
105
106
107
108
# File 'lib/pero/puppet.rb', line 100

def serve_master
  container = run_container
  begin
    yield container
  rescue StandardError => e
    Pero.log.error e.inspect
    raise e
  end
end

#ssh_exec(ssh, host, cmd) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/pero/puppet.rb', line 156

def ssh_exec(ssh, host, cmd)
  ssh.open_channel do |ch|
    ch.request_pty
    ch.on_data do |_ch, data|
      Pero.log.info "#{host}:#{data.chomp}"
    end

    ch.on_extended_data do |_c, _type, data|
      Pero.log.error "#{host}:#{data.chomp}"
    end

    ch.exec specinfra.build_command(cmd) do |_ch, success|
      raise "could not execute #{cmd}" unless success
    end
  end
  ssh.loop
end

#ssh_optionsObject

refs: github.com/itamae-kitchen/itamae



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
# File 'lib/pero/puppet.rb', line 45

def ssh_options
  opts = {}
  opts[:host_name] = @options[:host]

  # from ssh-config
  ssh_config_files = @options['ssh_config'] ? [@options['ssh_config']] : Net::SSH::Config.default_files
  opts.merge!(Net::SSH::Config.for(@options['host'], ssh_config_files))
  opts[:user] = @options['user'] || opts[:user] || Etc.getlogin
  opts[:password] = @options['password'] if @options['password']
  opts[:keys] = [@options['key']] if @options['key']
  opts[:port] = @options['port'] if @options['port']
  opts[:timeout] = @options['timeout'] if @options['timeout']

  if @options['vagrant']
    config = Tempfile.new('', Dir.tmpdir)
    hostname = opts[:host_name] || 'default'
    vagrant_cmd = "vagrant ssh-config #{hostname} > #{config.path}"
    if defined?(Bundler)
      Bundler.with_clean_env do
        `#{vagrant_cmd}`
      end
    else
      `#{vagrant_cmd}`
    end
    opts.merge!(Net::SSH::Config.for(hostname, [config.path]))
  end

  if @options['ask_password']
    print 'password: '
    password = STDIN.noecho(&:gets).strip
    print "\n"
    opts.merge!(password: password)
  end
  opts
end

#stop_masterObject



93
94
95
96
97
98
# File 'lib/pero/puppet.rb', line 93

def stop_master
  @mutex.lock
  run_container.kill if docker.alerady_run?
ensure
  @mutex.unlock
end