Class: DTK::NodeAgent::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/dtk-node-agent/installer.rb

Constant Summary collapse

CONFIG =

read configuration

eval(File.open(File.expand_path('../config/install.config', File.dirname(__FILE__))) {|f| f.read })

Class Method Summary collapse

Class Method Details

.base_dirObject



177
178
179
# File 'lib/dtk-node-agent/installer.rb', line 177

def self.base_dir
  File.expand_path('../..', File.dirname(__FILE__))
end

.install_additionsObject



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/dtk-node-agent/installer.rb', line 163

def self.install_additions
  # create puppet group
  shell "groupadd puppet" unless `grep puppet /etc/group`.include? "puppet"
  # create necessary dirs
  [   '/var/log/puppet/',
    '/var/lib/puppet/lib/puppet/indirector',
    '/etc/puppet/modules'
    ].map! { |p| FileUtils.mkdir_p(p) unless File.directory?(p) }
  # copy puppet libs
  FileUtils.cp_r(Dir.glob("#{base_dir}/puppet_additions/puppet_lib_base/puppet/indirector/*"), "/var/lib/puppet/lib/puppet/indirector/")
  # copy r8 puppet module
  FileUtils.cp_r(Dir.glob("#{base_dir}/puppet_additions/modules/r8"), "/etc/puppet/modules")
end

.install_arbiterObject



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/dtk-node-agent/installer.rb', line 191

def self.install_arbiter
  shell "git clone -b stable https://github.com/dtk/dtk-arbiter /usr/share/dtk/dtk-arbiter"
  Dir.chdir "/usr/share/dtk/dtk-arbiter"
  shell "bundle install --without development"
  puts "Installing dtk-arbiter init script"
  FileUtils.ln_sf("/usr/share/dtk/dtk-arbiter/etc/#{@osfamily}.dtk-arbiter.init", "/etc/init.d/dtk-arbiter")
  set_init("dtk-arbiter")
  puts "Installing dtk-arbiter monit config."
  monit_cfg_path = (@osfamily == 'debian') ? "/etc/monit/conf.d" : "/etc/monit.d"
  set_init("monit")
  FileUtils.ln_sf("/usr/share/dtk/dtk-arbiter/etc/dtk-arbiter.monit", "#{monit_cfg_path}/dtk-arbiter")            
end

.parse(argv) ⇒ Object



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
# File 'lib/dtk-node-agent/installer.rb', line 118

def self.parse(argv)
  options = {}
  parser = OptionParser.new do |opts|
    opts.banner = "    usage:\n    \n    dtk-node-agent [-p|--puppet-version] [-v|--version]\n    BANNER\n    opts.on(\"-d\",\n      \"--debug\",\n      \"enable debug mode\")  { |v| options[:debug] = true }      \n    opts.on_tail(\"-v\",\n      \"--version\",\n      \"Print the version and exit.\") do\n      puts ::DtkNodeAgent::VERSION\n      exit(0)\n    end\n    opts.on_tail(\"-h\",\n      \"--help\",\n      \"Print this help message.\") do\n      puts parser\n      exit(0)\n    end\n  end\n\n  parser.parse!(argv)\n\n  options\n  \nrescue OptionParser::InvalidOption => e\n  $stderr.puts e.message\n  exit(12)\nend\n"

.run(argv) ⇒ Object



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
# File 'lib/dtk-node-agent/installer.rb', line 33

def self.run(argv)
        require 'optparse'
        require 'fileutils'
        require 'dtk-node-agent/version'

        @@options = parse(argv)

        unless Process.uid == 0
puts "dtk-node-agent must be started with root/sudo privileges."
exit(1)
        end

        if @osfamily == 'debian'
    # set up apt and install packages
    shell "apt-get update --fix-missing"
    shell "apt-get install -y build-essential wget curl git"
    # install upgrades
    Array(CONFIG[:upgrades][:debian]).each do |package|
      shell "apt-get install -y #{package}"
    end
    shell "wget http://apt.puppetlabs.com/puppetlabs-release-#{@distcodename}.deb"
    puts "Installing Puppet Labs repository..."
    shell "dpkg -i puppetlabs-release-#{@distcodename}.deb"
    puts "Installing Puppet Labs repository..."
    shell "dpkg -i puppetlabs-release-#{@distcodename}.deb"
    # install logstash forwarder
    logstash_forwarder_url = CONFIG[:logstash_forwarder_deb64]
    logstash_forwarder_package = logstash_forwarder_url.rpartition('/').last
    shell "wget #{logstash_forwarder_url}"
    puts "Installing logstash-forwarder"
    shell "dpkg -i #{logstash_forwarder_package}"
    shell "apt-get update"
    shell "rm puppetlabs-release-#{@distcodename}.deb #{logstash_forwarder_package}"
    # pin down the puppetlabs apt repo
    FileUtils.cp("#{base_dir}/src/etc/apt/preferences.d/puppetlabs", "/etc/apt/preferences.d/puppetlabs")
  elsif @osfamily == 'redhat'
    shell "yum -y install yum-utils wget curl"
    case @osmajrelease
    when "5"
      shell "rpm -ivh #{CONFIG[:puppetlabs_el5_rpm_repo]}"
      @osarch == 'X86_64' ? (shell "rpm -ivh #{CONFIG[:rpm_forge_el5_X86_64_repo]}") : (shell "rpm -ivh #{CONFIG[:rpm_forge_el5_i686_repo]}")
    # 20xx is the major release naming pattern of Amazon Linux
    when "6", "n/a", "2015", "2014", "2016"
      shell "rpm -ivh #{CONFIG[:puppetlabs_el6_rpm_repo]}"
      @osarch == 'X86_64' ? (shell "rpm -ivh #{CONFIG[:rpm_forge_el6_X86_64_repo]}") : (shell "rpm -ivh #{CONFIG[:rpm_forge_el6_i686_repo]}")
      shell "yum-config-manager --disable rpmforge-release"
      shell "yum-config-manager --enable rpmforge-extras"
    when "7"
      shell "rpm -ivh #{CONFIG[:puppetlabs_el7_rpm_repo]}"
    else
      puts "#{@osname} #{@osmajrelease} is not supported. Exiting now..."
      exit(1)
    end
    shell "yum -y install git"
    # install upgrades
    Array(CONFIG[:upgrades][:redhat]).each do |package|
      shell "yum -y install #{package}"
      shell "yum -y update #{package}"
    end
    # install ec2-run-user-data init script
    # but only if the machine is running on AWS
    if `curl -m 5 -sI http://169.254.169.254/latest/meta-data/`.include? '200 OK'
      FileUtils.cp("#{base_dir}/src/etc/init.d/ec2-run-user-data", "/etc/init.d/ec2-run-user-data") unless File.exist?("/etc/init.d/ec2-run-user-data")
      set_init("ec2-run-user-data")
    end
    # install logstash-forwarder
    puts "Installing logstash-forwarder"
    shell "rpm -ivh #{CONFIG[:logstash_forwarder_rpm64]}"
    FileUtils.cp("#{base_dir}/src/etc/init.d/logstash-forwarder.rpm.init", "/etc/init.d/logstash-forwarder")
    set_init("logstash-forwarder")
  else
    puts "Unsuported OS for automatic agent installation. Exiting now..."
    exit(1)
  end

  puts "Installing additions Puppet..."
  install_additions

  puts "Installing DTK Arbiter"
  install_arbiter
end

.set_init(script) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/dtk-node-agent/installer.rb', line 181

def self.set_init(script)
  shell "chmod +x /etc/init.d/#{script}"
  if @osfamily == 'debian'
    shell "update-rc.d #{script} defaults"
  elsif @osfamily == 'redhat'
    shell "chkconfig --level 345 #{script} on"
    shell "systemctl daemon-reload" if @osmajrelease == '7'
  end
end

.shell(cmd) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/dtk-node-agent/installer.rb', line 152

def self.shell(cmd)
  puts "running: #{cmd}" if @@options[:debug]
  output = `#{cmd}`
  puts output if @@options[:debug]
  if $?.exitstatus != 0
    puts "Executing command \`#{cmd}\` failed"
    puts "Command output:"
    puts output
  end
end