Class: Yadecli::Command::Host::HostProvisionCommand

Inherits:
Mutations::Command
  • Object
show all
Defined in:
lib/yadecli/command/host/host_provision_command.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/yadecli/command/host/host_provision_command.rb', line 22

def execute
  host_client = Yade::Domain::Rest::Client::HostClient.new

  host = host_client.host_by_fqdn(self.host_fqdn)

  username = host.username
  port = host.sshPort || 22

  Net::SSH.start(host.hostName, username, port: port) do |ssh|
    # clone or update
    cmd_line = "sudo puppet agent -t --environment=#{host.environmentType.downcase}"

    channel = ssh.open_channel do |ch|
      ch.exec cmd_line do |ch, success|
        raise "could not execute command" unless success

        # "on_data" is called when the process writes something to stdout
        ch.on_data do |c, data|
          $stdout.print data if options[:verbose]
        end

        # "on_extended_data" is called when the process writes something to stderr
        ch.on_extended_data do |c, type, data|
          $stderr.print data if options[:verbose]
        end

        ch.on_close { puts "done!" }
      end
    end

    channel.wait
  end
  "Successfully provisioned host #{self.host_fqdn}"
end