Class: Yadecli::Command::Host::HostBootstrapCommand

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

Instance Method Summary collapse

Instance Method Details

#executeObject

The execute method is called only if the inputs validate. It does your business action.



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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/yadecli/command/host/host_bootstrap_command.rb', line 22

def execute
  host_client = Yade::Domain::Rest::Client::HostClient.new
  domain_client = Yade::Domain::Rest::Client::DomainClient.new
  role_client = Yade::Domain::Rest::Client::RoleClient.new

  master_host = host_client.master
  host = host_client.host_by_fqdn(self.host_fqdn)

  domain_id = host.domainId
  domain = domain_client.get(domain_id)

  role_id = host.roleId
  role = role_client.get(role_id)

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

  Net::SSH.start(host.hostName, username, port: port) do |ssh|
    # checkout_dir = Yade::Common::Config::AppConfig[:yade_home]
    checkout_dir = "/home/#{username}/.yade"
    local_repo = "#{checkout_dir}/puppet-bootstrap"

    # clone or update
    output = ssh.exec!("mkdir -p #{checkout_dir}")
    puts output if options[:verbose]

    clone_cmd = "git clone -b #{Yade::Common::Config::DomainConfig[:yade_bootstrap_repo_branch]} --depth 1 #{Yade::Common::Config::DomainConfig[:yade_bootstrap_repo_url]} #{local_repo} || (cd #{local_repo} ; git pull)"
    puts clone_cmd
    output = ssh.exec!(clone_cmd)
    puts output if options[:verbose]

    # setup
    if options[:setup]
      # output = ssh.exec!("mkdir -p #{local_repo}")
      # puts output if options[:verbose]

      output = ssh.exec!("sudo #{local_repo}/scripts/ubuntu/setup-managed-host.sh #{host.hostName} #{domain.name} #{host.ip}")
      puts output if options[:verbose]

      output = ssh.exec!("sudo #{local_repo}/scripts/ubuntu/local-bootstrap.sh \"mc nano\"")
      puts output if options[:verbose]

      output = ssh.exec!("sudo #{local_repo}/scripts/ubuntu/puppet-install.sh")
      puts output if options[:verbose]

      # noinspection RubyLiteralArrayInspection
      modules = Yade::Common::Config::DomainConfig[:yade_bootstrap_puppet_modules]

      FileUtils.rm_rf 'modules'
      FileUtils.mkdir_p 'modules'

      output = ssh.exec!("echo $HTTP_PROXY")
      puts output if options[:verbose]

      modules.each do |mod, version|
        cmd = "puppet module install --modulepath #{local_repo}/modules --version #{version} #{mod}"

        output = ssh.exec!(cmd)
        puts output if options[:verbose]
      end
    end

    # provision
    cmd_a = [
        'FACTER_vagrant=1',
        "FACTER_host_ip=#{host.ip}",
        "FACTER_host_name=#{host.hostName}",
        "FACTER_host_domain=#{domain.name}",
        "FACTER_master_ip=#{master_host.ip}",
        "FACTER_master_host_name=#{master_host.hostName}",
        "FACTER_role=#{role.name}",
        "FACTER_datacenter=#{host.datacenter}",
        "FACTER_zone=#{host.zone}",
        "FACTER_is_master=#{host.isMaster}",
        "FACTER_username=#{username}",
        'sudo --preserve-env puppet apply',
        "--hiera_config=#{local_repo}/config/hiera.yml",
        "--modulepath=#{local_repo}/modules:#{local_repo}/dist",
        "#{local_repo}/environments/production/manifests/default.pp"
    ]

    cmd_line = cmd_a.join(' ')

    puts cmd_line if options[:verbose]

    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 bootstrapped host #{self.host_fqdn}"
end