Class: Caterer::Server

Inherits:
Object
  • Object
show all
Includes:
Util::Shell
Defined in:
lib/caterer/server.rb

Direct Known Subclasses

Command::Clean, Command::Lock

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Shell

#bash, #env_string, #escape, #su

Constructor Details

#initialize(env, opts = nil) ⇒ Server

Returns a new instance of Server.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/caterer/server.rb', line 11

def initialize(env, opts=nil)
  @env    = env || Environment.new
  @alias  = opts[:alias]
  @user   = opts[:user]
  @pass   = opts[:pass]
  @host   = opts[:host]
  @port   = opts[:port]
  @key    = opts[:key]
  @images = opts[:images] || []
  @data   = opts[:data] || {}

  @logger = Log4r::Logger.new("caterer::server")

  @logger.info("Server: #{opts.inspect}")
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



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

def env
  @env
end

Instance Method Details

#can_rsync?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/caterer/server.rb', line 100

def can_rsync?
  !Vli::Util::Platform.windows? and Communication::Rsync.available?
end

#clean(opts = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/caterer/server.rb', line 48

def clean(opts={})
  if @images.length > 0
    @images.each do |i|
      ui.info "*** Cleaning image: #{i} ***"
      run_action(:clean, opts.merge({:image => i, :ghost_mode => true}))
    end
  else
    ui.info "*** Cleaning ***"
    run_action(:clean, opts.merge({:ghost_mode => true}))
  end
end

#cleanup!Object



83
84
85
86
# File 'lib/caterer/server.rb', line 83

def cleanup!
  ui.info "Cleaning up server..."
  ssh.sudo "rm -rf #{config.dest_dir}"
end

#configObject



88
89
90
# File 'lib/caterer/server.rb', line 88

def config
  @config ||= Caterer.config
end

#dataObject



150
151
152
153
154
# File 'lib/caterer/server.rb', line 150

def data
  @data_hash ||= begin
    (@data.is_a? Hash) ? @data : {}
  end
end

#detect_platformObject Also known as: platform



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/caterer/server.rb', line 166

def detect_platform
  @platform ||= begin
    ui.info "Detecting platform..."
    out = ""
    res = ssh.sudo bash(File.read(platform_script)) do |_stream, data|
      out += data
    end
    if res == 0
      out.strip # success
    else
      ui.error "Unknown platform"
      false
    end
  end
end

#hostObject



134
135
136
# File 'lib/caterer/server.rb', line 134

def host
  @host
end

#keyObject



142
143
144
# File 'lib/caterer/server.rb', line 142

def key
  @key
end

#keysObject



146
147
148
# File 'lib/caterer/server.rb', line 146

def keys
  @keys ||= [].tap {|keys| keys << key if key }
end

#lock(opts = {}) ⇒ Object



38
39
40
41
# File 'lib/caterer/server.rb', line 38

def lock(opts={})
  ui.info "*** Locking ***"
  run_action(:lock, opts)
end

#lock!Object



60
61
62
63
# File 'lib/caterer/server.rb', line 60

def lock!
  ui.info "Locking..."
  ssh.sudo "touch #{lock_path}"
end

#locked?Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/caterer/server.rb', line 70

def locked?
  res = ssh.sudo "[ -f #{lock_path} ]"
  res == 0 ? true : false
end

#nameObject



122
123
124
# File 'lib/caterer/server.rb', line 122

def name
  @alias || host
end

#passwordObject



130
131
132
# File 'lib/caterer/server.rb', line 130

def password
  @pass
end

#platform_scriptObject



183
184
185
# File 'lib/caterer/server.rb', line 183

def platform_script
  File.expand_path("../../templates/server/platform.sh", __FILE__)
end

#portObject



138
139
140
# File 'lib/caterer/server.rb', line 138

def port
  @port || 22
end

#prepare!Object



75
76
77
78
79
80
81
# File 'lib/caterer/server.rb', line 75

def prepare!
  ui.info "Preparing server..."
  ssh.sudo "mkdir -p #{config.dest_dir}", :stream => true
  ssh.sudo "mkdir -p #{config.dest_dir}/lib", :stream => true
  ssh.sudo "mkdir -p #{config.dest_dir}/bin", :stream => true
  ssh.sudo "chown -R #{username} #{config.dest_dir}", :stream => true
end

#provision(opts = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/caterer/server.rb', line 27

def provision(opts={})
  if @images.length > 0
    @images.each do |i|
      ui.info "*** Provisioning image: #{i} ***"
      run_action(:provision, opts.merge({:image => i}))
    end
  else
    ui.error "*** No image to provision ***"
  end
end

#rsyncObject



96
97
98
# File 'lib/caterer/server.rb', line 96

def rsync
  @rsync ||= Communication::Rsync.new(self)
end

#run_action(name, options = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/caterer/server.rb', line 156

def run_action(name, options=nil)
  options = {
    :server => self,
    :ui => ui,
    :config => env.config
  }.merge(options || {})

  env.action_runner.run(name, options)
end

#sshObject



92
93
94
# File 'lib/caterer/server.rb', line 92

def ssh
  @ssh ||= Communication::SSH.new(self)
end

#ssh_optsObject



104
105
106
107
108
109
110
111
112
# File 'lib/caterer/server.rb', line 104

def ssh_opts
  {}.tap do |opts|
    opts[:paranoid]      = false
    opts[:port]          = port
    opts[:password]      = password if password
    opts[:keys]          = keys if keys.length > 0
    opts[:forward_agent] = true
  end
end

#uiObject



114
115
116
117
118
119
120
# File 'lib/caterer/server.rb', line 114

def ui
  @ui ||= begin
    ui = @env.ui.dup
    ui.resource = name
    ui
  end
end

#unlock(opts = {}) ⇒ Object



43
44
45
46
# File 'lib/caterer/server.rb', line 43

def unlock(opts={})
  ui.info "*** Unlocking ***"
  run_action(:unlock, opts)
end

#unlock!Object



65
66
67
68
# File 'lib/caterer/server.rb', line 65

def unlock!
  ui.info "Unlocking..."
  ssh.sudo "rm -f #{lock_path}"
end

#upload_directory(from, to) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/caterer/server.rb', line 187

def upload_directory(from, to)
  if File.exists? from
    if can_rsync?
      from += "/" if not from.match /\/$/
      res = rsync.sync(from, to)
      ui.error "rsync was unsuccessful" if res != 0
    else
      # yuck. Heaven help the poor soul who hath not rsync...
      ui.warn "Rsync unavailable, falling back to scp (slower)..."
      unique = ::Digest::MD5.hexdigest(from)
      ssh.sudo "rm -rf #{to}/*", :stream => true
      ssh.sudo "mkdir -p #{to}/#{unique}", :stream => true
      ssh.sudo "chown -R #{username} #{to}/#{unique}", :stream => true
      ssh.upload from, "#{to}/#{unique}"
      ssh.sudo "mv #{to}/#{unique}/**/* #{to}/", :stream => true
      ssh.sudo "rm -rf #{to}/#{unique}", :stream => true
    end
  end
end

#usernameObject



126
127
128
# File 'lib/caterer/server.rb', line 126

def username
  @user || Etc.getlogin
end