Class: Wire::SpecCommand

Inherits:
BaseCommand show all
Defined in:
lib/wire/commands/spec_command.rb

Overview

SpecCommand generates a serverspec output for given model which tests all model elements. optionally runs serverspec

Instance Attribute Summary collapse

Attributes inherited from BaseCommand

#params

Instance Method Summary collapse

Methods inherited from BaseCommand

#check_user, #default_handle_resource, #dump_state, #ensure_hostip_netmask, #objects_in_zone, #outputs, #run, #state

Constructor Details

#initializeSpecCommand

initializes empty spec



23
24
25
# File 'lib/wire/commands/spec_command.rb', line 23

def initialize
  @spec_code = []
end

Instance Attribute Details

#projectObject

project to operate on



14
15
16
# File 'lib/wire/commands/spec_command.rb', line 14

def project
  @project
end

#spec_codeObject

spec_code will contain all serverspec code blocks, ready to be written to file



20
21
22
# File 'lib/wire/commands/spec_command.rb', line 20

def spec_code
  @spec_code
end

#target_dirObject

target_dir to read model from (and put specs into)



16
17
18
# File 'lib/wire/commands/spec_command.rb', line 16

def target_dir
  @target_dir
end

Instance Method Details

#run_on_appgroup_in_zone(zone_name, appgroup_name, appgroup_data) ⇒ Object

given an appgroup object, this generates spec for it. params: zone_name name of zone (needed for erb context) appgroup_name name of appgroup (needed for erb context) appgroup_data appgroup details rubocop:disable Lint/UnusedMethodArgument Lint/UselessAssignment :reek:UnusedParameters



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/wire/commands/spec_command.rb', line 157

def run_on_appgroup_in_zone(zone_name, appgroup_name, appgroup_data)
  $log.debug("Creating specs for appgroup #{appgroup_name}")

  # check controller
  controller_data = appgroup_data[:controller]
  if controller_data[:type] == 'fig'
    # get fig file name
    figfile_part = controller_data[:file] || "#{zone_name}/fig.yaml"
    figfile = File.join(File.expand_path(@target_dir), figfile_part)

    template = SpecTemplatesContainers.build_template__fig_file_is_valid
    erb = ERB.new(template, nil, '%')
    @spec_code << erb.result(binding)

    template = SpecTemplatesContainers.build_template__fig_containers_are_up
    erb = ERB.new(template, nil, '%')
    @spec_code << erb.result(binding)
  end

  $log.debug("Done for appgroup #{appgroup_name}")
end

#run_on_network_in_zone(zone_name, bridge_name, network_data) ⇒ Object

given a network object, this generates spec for it. params: zone_name name of zone (needed for erb context) bridge_name name of network/bridge (needed for erb context) network_data network details rubocop:disable Lint/UnusedMethodArgument rubocop:disable Lint/UselessAssignment :reek:UnusedParameters



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/wire/commands/spec_command.rb', line 102

def run_on_network_in_zone(zone_name, bridge_name, network_data)
  $log.debug("Creating specs for network #{bridge_name}")

  template = SpecTemplatesNetwork.build_template__bridge_exists
  erb = ERB.new(template, nil, '%')
  @spec_code << erb.result(binding)

  vlan = network_data[:vlan]
  if vlan
    vlanid = vlan[:id]
    on_trunk = vlan[:on_trunk]
    template = SpecTemplatesNetwork.build_template__bridge_vlan_id_and_trunk
    erb = ERB.new(template, nil, '%')
    @spec_code << erb.result(binding)
  end

  # render template for hostip (if any)
  ip = network_data[:hostip]
  if ip
    template = SpecTemplatesNetwork.build_template__ip_is_up
    erb = ERB.new(template, nil, '%')
    @spec_code << erb.result(binding)
  end

  # render template for network/port attachments (if any)
  attach_intf = network_data[:attach]
  if attach_intf
    template = SpecTemplatesNetwork.build_template__port_exists
    erb = ERB.new(template, nil, '%')
    @spec_code << erb.result(binding)
  end

  # render dhcp spec (if any)
  dhcp_data = network_data[:dhcp]
  if dhcp_data
    ip_start = dhcp_data[:start]
    ip_end = dhcp_data[:end]
    hostip = ip
    template = SpecTemplatesNetwork.build_template__dhcp_is_valid
    erb = ERB.new(template, nil, '%')
    @spec_code << erb.result(binding)

  end

  $log.debug("Done for network #{bridge_name}")
end

#run_on_projectObject

process specification for whole project model



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/wire/commands/spec_command.rb', line 28

def run_on_project
  @target_dir = @params[:target_dir]
  zones = @project.get_element('zones')

  # iterates all zones, descend into zone
  run_on_project_zones(zones)

  # use the specwrite class to write a complete
  # serverspec example in a subdirectory(serverspec)
  target_specdir = File.join(@target_dir, 'serverspec')

  begin
    spec_writer = SpecWriter.new(target_specdir, @spec_code)
    spec_writer.write

    outputs 'SPEC', "Serverspecs written to #{target_specdir}. Run:"
    outputs 'SPEC', "( cd #{target_specdir}; sudo rake spec )"
    outputs 'SPEC', 'To run automatically, use --run'
  rescue => exception
    $log.error "Error writing serverspec files, #{exception}"
    STDERR.puts exception.inspect
  end

  run_serverspec(target_specdir) if @params[:auto_run]
end

#run_on_project_zones(zones) ⇒ Object

run verification on zones



67
68
69
70
71
72
73
# File 'lib/wire/commands/spec_command.rb', line 67

def run_on_project_zones(zones)
  zones.select do |zone_name, _|
    $log.debug("Creating specs for zone #{zone_name} ...")
    run_on_zone(zone_name)
    $log.debug("Done for zone #{zone_name} ...")
  end
end

#run_on_zone(zone_name) ⇒ Object

run spec steps in given zone_name



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/wire/commands/spec_command.rb', line 76

def run_on_zone(zone_name)
  networks = @project.get_element('networks')

  # select networks in current zone only
  networks_in_zone = networks.select do|_, network_data|
    network_data[:zone] == zone_name
  end
  networks_in_zone.each do |network_name, network_data|
    run_on_network_in_zone zone_name, network_name, network_data
  end

  # select application groups in current zone
  objects_in_zone('appgroups', zone_name).each do |appgroup_name, appgroup_data|
    run_on_appgroup_in_zone zone_name, appgroup_name, appgroup_data
  end
end

#run_serverspec(target_specdir) ⇒ Object

executes serverspec in its target directory TODO: stream into stdout instead of Kernel.“ params: target_dir model and output dir



58
59
60
61
62
63
64
# File 'lib/wire/commands/spec_command.rb', line 58

def run_serverspec(target_specdir)
  $log.debug 'Running serverspec'
  cmd = "cd #{target_specdir} && sudo rake spec"
  $log.debug "cmd=#{cmd}"
  result = `#{cmd}`
  puts result
end