Module: Vagabond::Helpers::Base

Defined in:
lib/vagabond/helpers/base.rb

Constant Summary collapse

SSH_KEY_BASE =
'/opt/hw-lxc-config/id_rsa'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/vagabond/helpers/base.rb', line 122

def included(klass)
  klass.class_eval do
    class << self
      attr_accessor :ui
    end
    attr_accessor :vagabondfile, :internal_config, :name, :ui, :options, :leftover_args
  end
end

Instance Method Details

#base_setup(*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vagabond/helpers/base.rb', line 13

def base_setup(*args)
  @options = Mash.new(@options.dup)
  @vagabondfile = Vagabondfile.new(options[:vagabond_file], :allow_missing)
  Lxc.use_sudo = sudo
  options[:sudo] = sudo
  setup_ui(*args)
  config_args = args.detect{|i| i.is_a?(Hash) && i[:config]} || {}
  @internal_config = InternalConfiguration.new(@vagabondfile, ui, options, config_args[:config] || {})
  configure(:allow_missing) unless args.include?(:no_configure)
  validate_if_required unless args.include?(:no_validate)
  Chef::Log.init('/dev/null') unless options[:debug]
  Settings[:ssh_key] = setup_key!
end

#configure(*args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vagabond/helpers/base.rb', line 43

def configure(*args)
  @config ||= Mash.new
  @config.merge!(vagabondfile.for_node(name, *args))
  @lxc = Lxc.new(internal_config[mappings_key][name] || '____nonreal____')
  if(options[:local_server] && vagabondfile.local_chef_server? && lxc_installed?)
    proto = vagabondfile[:local_chef_server][:zero] ? 'http' : 'https'
    srv_name = internal_config[:mappings][:server] || '____nonreal____'
    srv = Lxc.new(srv_name)
    if(srv.running?)
      knife_config :server_url => "#{proto}://#{srv.container_ip(10, true)}"
    else
      unless(action.to_s == 'status' || name.to_s =='server')
        ui.warn 'Local chef server is not currently running!'
      end
    end
  end
end

#debug(s) ⇒ Object



90
91
92
# File 'lib/vagabond/helpers/base.rb', line 90

def debug(s)
  ui.info "#{ui.color('DEBUG:', :red, :bold)} #{s}" if options[:debug] && ui
end

#executeObject



108
109
110
111
112
113
114
115
# File 'lib/vagabond/helpers/base.rb', line 108

def execute
  if(public_methods.include?(@action.to_sym))
    send(@action)
  else
    ui.error "Invalid action received: #{@action}"
    raise VagabondError::InvalidAction.new(@action)
  end
end

#lxc_installed?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/vagabond/helpers/base.rb', line 117

def lxc_installed?
  system('which lxc-info > /dev/null')
end

#setup_key!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vagabond/helpers/base.rb', line 27

def setup_key!
  path = "/tmp/.#{ENV['USER']}_id_rsa"
  unless(File.exists?(path))
    [
      "cp #{SSH_KEY_BASE} #{path}",
      "chown #{ENV['USER']} #{path}",
      "chmod 600 #{path}"
    ].each do |com|
      cmd = build_command(com, :sudo => true)
      cmd.run_command
      cmd.error!
    end
  end
  path
end

#setup_ui(*args) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/vagabond/helpers/base.rb', line 94

def setup_ui(*args)
  unless(@ui)
    unless(args.first.is_a?(Chef::Knife::UI))
      Chef::Config[:color] = options[:color].nil? ? true : options[:color]
      @ui = Chef::Knife::UI.new(STDOUT, STDERR, STDIN, {})
    else
      @ui = args.first
    end
    options[:debug] = STDOUT if options[:debug]
    self.class.ui = @ui unless args.include?(:no_class_set)
  end
  @ui
end

#sudoObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vagabond/helpers/base.rb', line 69

def sudo
  sudo_val = vagabondfile[:sudo]
  if(sudo_val.nil? || sudo_val.to_s == 'smart')
    if(ENV['rvm_bin_path'] && RbConfig::CONFIG['bindir'].include?(File.dirname(ENV['rvm_bin_path'])))
      sudo_val = 'rvmsudo'
    elsif(Etc.getpwuid.uid == 0)
      sudo_val = false
    else
      sudo_val = true
    end
  end
  case sudo_val
  when FalseClass
    ''
  when String
    "#{sudo_val} "
  else
    'sudo '
  end
end

#validate_if_requiredObject



61
62
63
64
65
66
67
# File 'lib/vagabond/helpers/base.rb', line 61

def validate_if_required
  if(respond_to?(check = "#{action}_validate?".to_sym))
    validate! if send(check)
  else
    validate!
  end
end