Class: Pero::Puppet
- Inherits:
-
Object
- Object
- Pero::Puppet
- Extended by:
- SshExecutable
- Defined in:
- lib/pero/puppet.rb,
lib/pero/puppet/base.rb,
lib/pero/puppet/redhat.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#specinfra ⇒ Object
readonly
Returns the value of attribute specinfra.
Instance Method Summary collapse
- #apply ⇒ Object
- #docker ⇒ Object
- #in_ssh_forwarding(port) ⇒ Object
-
#initialize(host, options, mutex) ⇒ Puppet
constructor
A new instance of Puppet.
- #install ⇒ Object
- #parse_puppet_option(options) ⇒ Object
- #puppet_cmd ⇒ Object
- #run_container ⇒ Object
- #serve_master ⇒ Object
- #ssh_exec(ssh, host, cmd) ⇒ Object
-
#ssh_options ⇒ Object
refs: github.com/itamae-kitchen/itamae.
- #stop_master ⇒ Object
Methods included from SshExecutable
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, , mutex) @options = .dup @mutex = mutex @options[:host] = host so = 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
#specinfra ⇒ Object (readonly)
Returns the value of attribute specinfra.
25 26 27 |
# File 'lib/pero/puppet.rb', line 25 def specinfra @specinfra end |
Instance Method Details
#apply ⇒ Object
118 119 120 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 |
# File 'lib/pero/puppet.rb', line 118 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 |
#docker ⇒ Object
107 108 109 |
# File 'lib/pero/puppet.rb', line 107 def docker Pero::Docker.new(@options['server-version'], @options['image-name'], @options['environment'], @options['volumes']) end |
#in_ssh_forwarding(port) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/pero/puppet.rb', line 189 def in_ssh_forwarding(port) = specinfra.get_config(:ssh_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), [:user], ) do |ssh| ssh.forward.remote(port, 'localhost', 8140) yield specinfra.get_config(:host), ssh end end |
#install ⇒ Object
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
179 180 181 182 183 184 185 186 187 |
# File 'lib/pero/puppet.rb', line 179 def parse_puppet_option() ret = '' %w[noop verbose test].each do |n| ret << " --#{n}" if [n] end ret << " --tags #{['tags'].join(',')}" if ['tags'] ret << " --environment #{['environment']}" if ['environment'] ret end |
#puppet_cmd ⇒ Object
171 172 173 174 175 176 177 |
# File 'lib/pero/puppet.rb', line 171 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_container ⇒ Object
111 112 113 114 115 116 |
# File 'lib/pero/puppet.rb', line 111 def run_container @mutex.lock docker.alerady_run? || docker.run ensure @mutex.unlock end |
#serve_master ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/pero/puppet.rb', line 97 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
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/pero/puppet.rb', line 153 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_options ⇒ Object
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 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_master ⇒ Object
93 94 95 |
# File 'lib/pero/puppet.rb', line 93 def stop_master run_container.kill if docker.alerady_run? end |