Class: Skewer::Bootstrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/bootstrapper.rb

Overview

puts all of puppet’s dependencies on

Constant Summary collapse

MAX_CACHE =
3600

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, options) ⇒ Bootstrapper

Returns a new instance of Bootstrapper.



11
12
13
14
15
16
# File 'lib/bootstrapper.rb', line 11

def initialize(node,options)
  @node = node
  @options = options
  @util = Util.new
  @mock = false
end

Instance Attribute Details

#mock=(value) ⇒ Object (writeonly)

Sets the attribute mock

Parameters:

  • value

    the value to set the attribute mock to.



9
10
11
# File 'lib/bootstrapper.rb', line 9

def mock=(value)
  @mock = value
end

Instance Method Details

#add_key_to_agent(executor = Kernel, homedir = ENV['HOME']) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/bootstrapper.rb', line 40

def add_key_to_agent(executor = Kernel, homedir = ENV['HOME'])
  config = SkewerConfig.instance
  key_name = config.get('key_name')
  key_path = File.join(homedir, '.ssh', "#{key_name}.pem")
  Skewer.logger.debug "****Looking for #{key_path}"
  if File.exists?(key_path)
    executor.system("ssh-add #{key_path}")
  end
end

#add_ssh_hostkeyObject



18
19
20
21
# File 'lib/bootstrapper.rb', line 18

def add_ssh_hostkey
  location = @util.get_location(@node)
  system "ssh -o 'StrictHostKeyChecking no' -o 'PasswordAuthentication no' no_such_user@#{location} >/dev/null 2>&1"
end

#destroy_lock_fileObject



80
81
82
# File 'lib/bootstrapper.rb', line 80

def destroy_lock_file()
  FileUtils.rm_f(self.lock_file)
end

#execute(file_name) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/bootstrapper.rb', line 23

def execute(file_name)
  file = File.join(File.dirname(__FILE__), '..', 'assets', file_name)
  raise "#{file} does not exist" unless File.exists? file
  @node.scp file, '/var/tmp/.'
  result = @node.ssh "sudo bash /var/tmp/#{file_name}"
  puts result.inspect
  result
end

#goObject



105
106
107
108
109
110
# File 'lib/bootstrapper.rb', line 105

def go
  i_should_run = should_i_run?
  prepare_node() if i_should_run
  sync_source
  install_gems if i_should_run
end

#install_gemsObject



32
33
34
35
36
37
38
# File 'lib/bootstrapper.rb', line 32

def install_gems
  Skewer.logger.debug "Installing Gems"
  assets = File.join(File.dirname(__FILE__), '..', 'assets')
  @node.scp File.join(File.expand_path(assets), 'Gemfile'), 'infrastructure'
  command = ". /etc/profile.d/rubygems.sh && cd infrastructure && bundle install"
  result = @node.ssh(command)
end

#lock_fileObject



69
70
71
# File 'lib/bootstrapper.rb', line 69

def lock_file
  File.join('/tmp', 'skewer-' + Util.new.get_location(@node))
end

#lock_file_expired?(lock_file) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
# File 'lib/bootstrapper.rb', line 73

def lock_file_expired?(lock_file)
  now = Time.now
  lock_file_time = File.stat(lock_file).mtime
  age = now - lock_file_time
  age > MAX_CACHE
end

#prepare_nodeObject



99
100
101
102
103
# File 'lib/bootstrapper.rb', line 99

def prepare_node
  add_ssh_hostkey
  execute('rubygems.sh')
  add_key_to_agent
end

#should_i_run?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bootstrapper.rb', line 84

def should_i_run?
  lock_file = lock_file()
  if File.exists?(lock_file)
    if lock_file_expired?(lock_file)
      destroy_lock_file
      return true
    else
      return false
    end
  else
    FileUtils.touch(lock_file)
    true
  end
end

#sync_sourceObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bootstrapper.rb', line 50

def sync_source()
  require 'source'
  require 'puppet_node'
  config = SkewerConfig.instance
  source_dir = config.get(:puppet_repo)
  Skewer.logger.debug "Using Puppet Code from #{source_dir}"
  role = @options[:role]
  if role
    PuppetNode.new({:default => role.to_sym}).render
  end
  # TODO: if there's no role, it should look it up from an external source
  if @mock
    Skewer.logger.debug "Mock: would normally rsync now"
    else
    Source.new(source_dir).rsync(@node)
  end

end