Class: Slugforge::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/slugforge/models/host.rb

Direct Known Subclasses

FogHost, HostnameHost, IpAddressHost

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, server = nil) ⇒ Host

Returns a new instance of Host.



8
9
10
11
12
13
14
15
16
# File 'lib/slugforge/models/host.rb', line 8

def initialize(pattern, server=nil)
  @pattern = pattern
  @server  = server
  @deploy_results  = []
  @timeline = []
  @start_time = Time.now
  @actions = []
  self
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



6
7
8
# File 'lib/slugforge/models/host.rb', line 6

def pattern
  @pattern
end

#serverObject (readonly)

Returns the value of attribute server.



6
7
8
# File 'lib/slugforge/models/host.rb', line 6

def server
  @server
end

#slug_nameObject (readonly)

Returns the value of attribute slug_name.



6
7
8
# File 'lib/slugforge/models/host.rb', line 6

def slug_name
  @slug_name
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/slugforge/models/host.rb', line 6

def status
  @status
end

Instance Method Details

#add_action(action) ⇒ Object



76
77
78
# File 'lib/slugforge/models/host.rb', line 76

def add_action(action)
  @actions << action
end

#complete?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/slugforge/models/host.rb', line 64

def complete?
  success? || failed?
end

#deploy(slug_name, logger, opts) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/slugforge/models/host.rb', line 109

def deploy(slug_name, logger, opts)
  begin
    record_event :started
    if opts[:pretend]
      logger.log "not actually #{effective_action} slug (#{name})", {:color => :yellow, :status => :pretend}
    else
      logger.say_status :deploy, "#{effective_action} to host #{name} as #{username(opts)}", :green
      Net::SSH.start(ssh_host, username(opts), ssh_opts(opts)) do |ssh|
        host_slug = detect_slug(ssh, slug_name, logger) unless opts[:force]
        host_slug ||= copy_slug(ssh, slug_name, logger, opts)
        explode_slug(ssh, host_slug, logger, opts) if stage?
        install_slug(ssh, host_slug, logger, opts) if install?
      end
    end
    record_event :deployed
  rescue => e
    record_event :failed
    message = "#{e.class.to_s}: #{e.to_s}"
    logger.log "#{message} (#{ip}: #{name})", {:color => :red, :status => :fail}
    @deploy_results << {:output => message}
  end
  logger.say_status :deploy, "#{effective_action} complete for host: #{name}", success? ? :green : :red
  @deploy_results
end

#effective_actionObject



96
97
98
99
# File 'lib/slugforge/models/host.rb', line 96

def effective_action
  return "installing" if @actions.include?(:install)
  "staging"
end

#elapsed_timeObject



51
52
53
# File 'lib/slugforge/models/host.rb', line 51

def elapsed_time
  Time.at(Time.now - @start_time).strftime('%M:%S')
end

#failed?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/slugforge/models/host.rb', line 72

def failed?
  @status == :failed?
end

#has_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/slugforge/models/host.rb', line 80

def has_action?(action)
  @actions.include?(action)
end

#idObject



30
31
32
# File 'lib/slugforge/models/host.rb', line 30

def id
  nil
end

#install?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/slugforge/models/host.rb', line 92

def install?
  @actions.include?(:install)
end

#ipObject



22
23
24
# File 'lib/slugforge/models/host.rb', line 22

def ip
  @pattern
end

#is_autoscaled?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/slugforge/models/host.rb', line 34

def is_autoscaled?
  false
end

#nameObject



18
19
20
# File 'lib/slugforge/models/host.rb', line 18

def name
  "name:#{@pattern}"
end

#outputObject



105
106
107
# File 'lib/slugforge/models/host.rb', line 105

def output
  @deploy_results.map { |result| result[:output] unless result[:exit_code] == 0 }.compact
end

#record_event(status) ⇒ Object



55
56
57
58
# File 'lib/slugforge/models/host.rb', line 55

def record_event(status)
  @status = status
  @timeline << {:status => status, :elapsed_time => elapsed_time}
end

#remove_action(action) ⇒ Object



84
85
86
# File 'lib/slugforge/models/host.rb', line 84

def remove_action(action)
  @actions.delete(action)
end

#ssh_hostObject



26
27
28
# File 'lib/slugforge/models/host.rb', line 26

def ssh_host
  @pattern
end

#stage?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/slugforge/models/host.rb', line 88

def stage?
  @actions.include?(:stage)
end

#success?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/slugforge/models/host.rb', line 68

def success?
  [:deployed, :terminated].include?(@status) && output.empty?
end

#terminated?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/slugforge/models/host.rb', line 101

def terminated?
  @status == :terminated?
end

#timelineObject



60
61
62
# File 'lib/slugforge/models/host.rb', line 60

def timeline
  @timeline.map { |event| "#{event[:status]} @ #{event[:elapsed_time]}" }.join ', '
end

#to_statusObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/slugforge/models/host.rb', line 38

def to_status
  {
    :name       => name,
    :ip         => ip,
    :pattern    => @pattern,
    :slug_name  => @slug_name,
    :status     => @status.to_s,
    :output     => @deploy_results,
    :start_time => @start_time,
    :timeline   => timeline,
  }
end