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.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/pero/puppet.rb', line 26 def initialize(host, , mutex) @options = .dup @mutex = mutex @options[:host] = host so = if !Net::SSH::VALID_OPTIONS.include?(:strict_host_key_checking) so.delete(:strict_host_key_checking) end @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
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 |
# File 'lib/pero/puppet.rb', line 121 def apply serve_master do |container| port = container.info["Ports"].first["PublicPort"] 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 => e Pero.log.error "puppet apply error:#{e.inspect}" end end Pero::History::Attribute.new(specinfra, @options).save end |
#docker ⇒ Object
108 109 110 |
# File 'lib/pero/puppet.rb', line 108 def docker Pero::Docker.new(@options["server-version"], @options["image-name"], @options["environment"], @options["volumes"]) end |
#in_ssh_forwarding(port) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/pero/puppet.rb', line 185 def in_ssh_forwarding(port) = specinfra.get_config(:ssh_options) if !Net::SSH::VALID_OPTIONS.include?(:strict_host_key_checking) .delete(:strict_host_key_checking) end 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
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/pero/puppet.rb', line 82 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
175 176 177 178 179 180 181 182 183 |
# File 'lib/pero/puppet.rb', line 175 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
167 168 169 170 171 172 173 |
# File 'lib/pero/puppet.rb', line 167 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
112 113 114 115 116 117 118 119 |
# File 'lib/pero/puppet.rb', line 112 def run_container begin @mutex.lock docker.alerady_run? || docker.run ensure @mutex.unlock end end |
#serve_master ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/pero/puppet.rb', line 98 def serve_master container = run_container begin yield container rescue => e Pero.log.error e.inspect raise e end end |
#ssh_exec(ssh, host, cmd) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/pero/puppet.rb', line 149 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
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 |
# File 'lib/pero/puppet.rb', line 46 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
94 95 96 |
# File 'lib/pero/puppet.rb', line 94 def stop_master run_container.kill if docker.alerady_run? end |