Class: Yadecli::Service::HostService

Inherits:
Object
  • Object
show all
Defined in:
lib/yadecli/service/host_service.rb

Overview

host service

Instance Method Summary collapse

Constructor Details

#initializeHostService

Returns a new instance of HostService.



14
15
16
17
18
# File 'lib/yadecli/service/host_service.rb', line 14

def initialize
  @host_client = Yadecli::Client::HostClient.new
  @domain_client = Yadecli::Client::DomainClient.new
  @role_client = Yadecli::Client::RoleClient.new
end

Instance Method Details

#bootstrap(host_fqdn, options) ⇒ Object

bootstrap a host



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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/yadecli/service/host_service.rb', line 43

def bootstrap(host_fqdn, options)
  CliUtil.print_header'Yade bootstrap Host',
                      ["Going to bootstrap Yade host #{host_fqdn}", '']

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

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

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

  username = 'administrator'
  port = 22

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

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

    output = ssh.exec!("git clone -b test --depth 1 https://gitlab.com/yadedev/yade-puppet-bootstrap.git #{local_repo} || (cd #{local_repo} ; git pull)")
    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 = {
          'puppetlabs/ntp': '7.1.1',
          'saz/timezone': '4.1.1',
          'cjtoolseram/puppetconf': '0.2.7',
          'puppetlabs/puppet_authorization': '0.4.0',
          'puppetlabs/puppetdb': '7.0.1',
          'puppetlabs-puppetserver_gem': '1.0.0',
          'puppet-r10k': '6.6.1',
          'abrader-gms': '1.0.3',
          'theforeman-puppet': '9.1.0',
          #'theforeman-foreman': '9.2.0'
      }

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

      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
end

#listObject

list the available hosts



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/yadecli/service/host_service.rb', line 21

def list
  CliUtil.print_header'Yade List Hosts', ['This are the available hosts', '']

  hosts = @host_client.list

  table = TTY::Table.new header: ['Name', 'Domain', 'Role', 'Ip', 'OsType', 'Environment']

  hosts.each do |h|
    domain_id = h.domainId
    domain = @domain_client.get(domain_id)

    role_id = h.roleId
    role = @role_client.get(role_id)

    table << [h.name, domain.name, role.name, h.ip, h.osType, h.environmentType]
  end

  puts table.render(:ascii, width: 100, resize: true, padding: [0, 1])
  puts ''
end

#provision(host_fqdn, options) ⇒ Object

provision a host



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/yadecli/service/host_service.rb', line 155

def provision(host_fqdn, options)
  CliUtil.print_header'Yade provision Host',
                      ["Going to provision Yade host #{host_fqdn}", '']

  host = @host_client.host_by_fqdn(host_fqdn)

  username = 'administrator'
  port = 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
end