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.



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, options, mutex)
  @options = options.dup
  @mutex = mutex

  @options[:host] = host
  so = ssh_options

  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

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

#dockerObject



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)
  options = specinfra.get_config(:ssh_options)

  if !Net::SSH::VALID_OPTIONS.include?(:strict_host_key_checking)
    options.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),
    options[:user],
    options
  ) do |ssh|
    ssh.forward.remote(port, 'localhost', 8140)
    yield specinfra.get_config(:host), ssh
  end
end

#installObject



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



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_containerObject



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_masterObject



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_optionsObject

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



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

def stop_master
  run_container.kill if docker.alerady_run?
end