Class: Stackadmin

Inherits:
Object
  • Object
show all
Defined in:
lib/stackadmin/base.rb,
lib/stackadmin/yaml.rb,
lib/stackadmin/audit.rb,
lib/stackadmin/patch.rb,
lib/stackadmin/version.rb,
lib/stackadmin/exceptions.rb

Defined Under Namespace

Classes: Exception, InvalidCommand, InvalidFlags, InvalidPatchStatus, YAMLTargetNotFound

Constant Summary collapse

VERSION =
'0.1.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, port = 22, debug = false, yml_file = nil) ⇒ Stackadmin

Returns a new instance of Stackadmin.



9
10
11
12
13
# File 'lib/stackadmin/base.rb', line 9

def initialize(target, port = 22, debug = false, yml_file = nil)
  @port = port
  @debug = debug
  yml_file ? parse_yaml(target, yml_file) : audit(target)
end

Instance Attribute Details

#freshObject (readonly)

Returns the value of attribute fresh.



7
8
9
# File 'lib/stackadmin/base.rb', line 7

def fresh
  @fresh
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/stackadmin/base.rb', line 7

def id
  @id
end

#licenseObject (readonly)

Returns the value of attribute license.



7
8
9
# File 'lib/stackadmin/base.rb', line 7

def license
  @license
end

#nodesObject (readonly)

Returns the value of attribute nodes.



7
8
9
# File 'lib/stackadmin/base.rb', line 7

def nodes
  @nodes
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/stackadmin/base.rb', line 7

def version
  @version
end

Class Method Details

.find_instances(ssh_config = '~/.ssh/config', filter = []) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/stackadmin/base.rb', line 20

def self.find_instances(ssh_config = '~/.ssh/config', filter = [])
  instances = IO.read(File.expand_path(ssh_config))
                .scan(/^\s*Host (.*stackato.*)/)
                .flatten
  filter.each { |f| instances.delete_if { |host| host =~ /#{f}/ } }
  instances
end

Instance Method Details

#install!(patches = [], node = nil, local = false, restart = true, force_update = false, manifest_file = nil) ⇒ Object

Returns hash of hashes: { node1: { installed: [patch1], not_installed: [patch2] }, etc } TODO: DRY this with ::mark! & ::reinstall! & ::revert!



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/stackadmin/patch.rb', line 11

def install!(patches = [], node = nil, local = false, restart = true, force_update = false, manifest_file = nil)
  status = Hash.new
  output = patch('install', patches, node: node, local: local, no_restart: !restart, force_update: force_update, manifest: manifest_file)

  output.each do |p|
    p = p[:stdout]
    log "Job output:\n#{p}"
    p.gsub!(/\e\[\d+m/, '')    # Strip ASCII color

    p.scan(/^Failed installing update (.+?) on (.+?)\.$/).each do |fp|
      # fp = Array of failed patches
      # e.g. [["patch1-name", "node1-ip"], ["patch2-name", "node2-ip"]]
      status[fp[1]] = {
        installed: [],
        not_installed: []
      } unless status.has_key?(fp[1])

      status[fp[1]][:not_installed] << fp[0]
    end

    p.scan(/^Successfully installed update (.+?) on (.+?)\.$/).each do |sp|
      # sp = Array of successful patches. See fp example above
      status[sp[1]] = {
        installed: [],
        not_installed: []
      } unless status.has_key?(sp[1])

      status[sp[1]][:installed] << sp[0]
    end
  end

  status
end

#manifest(uri = nil) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/stackadmin/base.rb', line 28

def manifest(uri = nil)
  unless @manifest
    uri ||= "https://get.stackato.com/kato-patch/#{@version}/manifest.json"
    log "Retrieving latest manifest from: #{uri}"
  end

  @manifest ||= JSON.load(open(uri)) rescue nil
end

#mark!(patches = [], mark_installed = false, node = nil, local = false) ⇒ Object

Returns hash: { node1: [patch1, patch2], etc } TODO: DRY this with ::install! & ::reinstall! & ::revert!



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/stackadmin/patch.rb', line 60

def mark!(patches = [], mark_installed = false, node = nil, local = false)
  status = Hash.new
  output = v3_action_check('mark') do
             patch('mark', patches, node: node, local: local, mark_installed: mark_installed)
           end

  output.each do |p|
    p = p[:stdout]
    log "Job output:\n#{p}"
    p.gsub!(/\e\[\d+m/, '')   # Strip ASCII color

    p.scan(/^Successfully marked patch (.+?) as #{'not ' unless mark_installed}installed on (.+?)\.$/).each do |sp|
      # sp = Array of successfully marked patches
      # e.g. [["patch1-name", "node1-ip"], ["patch2-name", "node2-ip"]]
      status[sp[1]] = Array.new unless status.has_key?(sp[1])
      status[sp[1]] << sp[0]
    end
  end

  status
end

#refresh(target = nil) ⇒ Object



15
16
17
18
# File 'lib/stackadmin/base.rb', line 15

def refresh(target = nil)
  @id ||= target
  audit
end

#reinstall!(patches = [], node = nil, local = false, restart = true, force_update = false, manifest_file = nil) ⇒ Object

Returns hash of hashes: { node1: { installed: [patch1], not_installed: [patch2] }, etc } TODO: DRY this with ::install! & ::mark! & ::revert!



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
114
115
116
117
118
119
120
# File 'lib/stackadmin/patch.rb', line 84

def reinstall!(patches = [], node = nil, local = false, restart = true, force_update = false, manifest_file = nil)
  if patches.empty?
    raise InvalidCommand, "kato patch reinstall requires a specific patchname!"
  else
    status = Hash.new
    output = patch('reinstall', patches, node: node, local: local, no_restart: !restart, force_update: force_update, manifest: manifest_file)

    output.each do |p|
      p = p[:stdout]
      log "Job output:\n#{p}"
      p.gsub!(/\e\[\d+m/, '')    # Strip ASCII color

      p.scan(/^Failed installing update (.+?) on (.+?)\.$/).each do |fp|
        # fp = Array of failed patches
        # e.g. [["patch1-name", "node1-ip"], ["patch2-name", "node2-ip"]]
        status[fp[1]] = {
          installed: [],
          not_installed: []
        } unless status.has_key?(fp[1])

        status[fp[1]][:not_installed] << fp[0]
      end

      p.scan(/^Successfully installed update (.+?) on (.+?)\.$/).each do |sp|
        # sp = Array of successful patches. See fp example above
        status[sp[1]] = {
          installed: [],
          not_installed: []
        } unless status.has_key?(sp[1])

        status[sp[1]][:installed] << sp[0]
      end
    end

    status
  end
end

#reset!(node = nil, local = false) ⇒ Object



45
46
47
48
# File 'lib/stackadmin/patch.rb', line 45

def reset!(node = nil, local = false)
  output = patch('reset', [], node: node, local: local)
  output[0][:stdout] =~ /^Updates state reset.$/ ? true : false
end

#revert!(patches = [], node = nil, local = false, restart = true, force_update = false, manifest_file = nil) ⇒ Object

Returns hash of hashes: { node1: { reverted: [patch1], not_reverted: [patch2], unable_to_revert: [patch3] }, etc } TODO: DRY this with ::install! & ::mark!



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
151
152
153
154
155
156
157
158
159
160
# File 'lib/stackadmin/patch.rb', line 124

def revert!(patches = [], node = nil, local = false, restart = true, force_update = false, manifest_file = nil)
  status = Hash.new
  output = v3_action_check('revert') do
             patch('revert', patches, node: node, local: local, no_restart: !restart, force_update: force_update, manifest: manifest_file)
           end

  output.each do |p|
    p = p[:stdout]
    log "Job output:\n#{p}"
    p.gsub!(/\e\[\d+m/, '')    # Strip ASCII color

    p.scan(/^Failed reverting update (.+?) on (.+?)\.$/).each do |fp|
      # fp = Array of failed patches
      # e.g. [["patch1-name", "node1-ip"], ["patch2-name", "node2-ip"]]
      status[fp[1]] = {
        reverted: [],
        not_reverted: [],
        unable_to_revert: []
      } unless status.has_key?(fp[1])

      status[fp[1]][:not_installed] << fp[0]
    end

    p.scan(/^Successfully reverted update (.+?) on (.+?)\.$/).each do |sp|
      # sp = Array of successful patches. See fp example above
      status[sp[1]] = {
        reverted: [],
        not_reverted: [],
        unable_to_revert: []
      } unless status.has_key?(sp[1])

      status[sp[1]][:installed] << sp[0]
    end
  end

  status
end

#to_yamlObject



7
8
9
10
11
12
# File 'lib/stackadmin/yaml.rb', line 7

def to_yaml
  { 'id'      => @id,
    'version' => @version,
    'license' => @license,
    'nodes'   => @nodes }.to_yaml
end

#update!(node = nil, local = false, manifest_file = nil) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/stackadmin/patch.rb', line 50

def update!(node = nil, local = false, manifest_file = nil)
  output = v3_action_check('update') do
             patch('update', [], node: node, local: local, manifest: manifest_file)
           end

  output[0][:stdout] =~ /^done!$/ ? true : false
end