Class: Configspec::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/configspec/setup.rb

Class Method Summary collapse

Class Method Details

.ask_os_typeObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/configspec/setup.rb', line 45

def self.ask_os_type
  prompt = "Select OS type:\n\n  1) UN*X\n  2) Windows\n\nSelect number: \n"

  print prompt.chop
  num = $stdin.gets.to_i - 1
  puts

  @os_type = [ 'UN*X', 'Windows' ][num] || 'UN*X'
end

.ask_unix_backendObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/configspec/setup.rb', line 62

def self.ask_unix_backend
  prompt = "Select a backend type:\n\n  1) SSH\n  2) Exec (local)\n  3) Dockerfile\n\nSelect number: \n"
  print prompt.chop
  num = $stdin.gets.to_i - 1
  puts

  @backend_type = [ 'Ssh', 'Exec', 'Dockerfile' ][num] || 'Exec'
end

.ask_windows_backendObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/configspec/setup.rb', line 79

def self.ask_windows_backend
  prompt = "Select a backend type:\n\n  1) WinRM\n  2) Cmd (local)\n\nSelect number: \n"
  print prompt.chop
  num = $stdin.gets.to_i - 1
  puts

  @backend_type = [ 'WinRM', 'Cmd' ][num] || 'Exec'
end

.auto_vagrant_configurationObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/configspec/setup.rb', line 174

def self.auto_vagrant_configuration
  if find_vagrantfile
    vagrant_list = `vagrant status`
    list_of_vms = []
    if vagrant_list != ''
      vagrant_list.each_line do |line|
        if match = /([a-z_-]+[\s]+)(created|not created|poweroff|running|saved)[\s](\(virtualbox\)|\(vmware\))/.match(line)
          list_of_vms << match[1].strip!
        end
      end
      if list_of_vms.length == 1
        @hostname = list_of_vms[0]
      else
        list_of_vms.each_with_index { |vm, index | puts "#{index}) #{vm}\n" }
        print "Choose a VM from the Vagrantfile: "
        chosen_vm = $stdin.gets.chomp
        @hostname = list_of_vms[chosen_vm.to_i]
      end
    else
      $stderr.puts "Vagrant status error - Check your Vagrantfile or .vagrant"
      exit 1
    end
  else
    $stderr.puts "Vagrantfile not found in directory!"
    exit 1
  end
end

.find_vagrantfileObject



166
167
168
169
170
171
172
# File 'lib/configspec/setup.rb', line 166

def self.find_vagrantfile
  Pathname.new(Dir.pwd).ascend do |dir|
    path = File.expand_path("Vagrantfile", dir)
    return path if File.exists?(path)
  end
  nil
end

.runObject



6
7
8
9
10
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/configspec/setup.rb', line 6

def self.run

  # ask_os_type
  @os_type = 'UN*X'

  if @os_type == 'UN*X'
    ask_unix_backend
  else
    ask_windows_backend
  end

  if @backend_type == 'Ssh'
    print "Vagrant instance y/n: "
    @vagrant = $stdin.gets.chomp
    if @vagrant =~ (/(true|t|yes|y|1)$/i)
      @vagrant = true
      print "Auto-configure Vagrant from Vagrantfile? y/n: "
      auto_config = $stdin.gets.chomp
      if auto_config =~ (/(true|t|yes|y|1)$/i)
        auto_vagrant_configuration
      else
        print("Input vagrant instance name: ")
        @hostname = $stdin.gets.chomp
      end
    else
      @vagrant = false
      print("Input target host name: ")
      @hostname = $stdin.gets.chomp
    end
  elsif @backend_type == 'Exec'
    @hostname = 'localhost'
  end

  [ 'spec', "spec/#{@hostname}" ].each { |dir| safe_mkdir(dir) }
  safe_create_spec
  safe_create_spec_helper
  safe_create_rakefile
end

.safe_create_rakefileObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/configspec/setup.rb', line 144

def self.safe_create_rakefile
  content = "require 'rake'\nrequire 'rspec/core/rake_task'\n\nRSpec::Core::RakeTask.new(:spec) do |t|\n  t.pattern = 'spec/*/*_spec.rb'\nend\n"
  if File.exists? 'Rakefile'
    old_content = File.read('Rakefile')
    if old_content != content
      $stderr.puts "!! Rakefile already exists and differs from template"
    end
  else
    File.open('Rakefile', 'w') do |f|
      f.puts content
    end
    puts ' + Rakefile'
  end
end

.safe_create_specObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/configspec/setup.rb', line 95

def self.safe_create_spec
  content = "require 'spec_helper'\n\ndescribe package('httpd') do\n  it { should be_installed }\nend\n"

  if File.exists? "spec/#{@hostname}/001_httpd_spec.rb"
    old_content = File.read("spec/#{@hostname}/001_httpd_spec.rb")
    if old_content != content
      $stderr.puts "!! spec/#{@hostname}/httpd_spec.rb already exists and differs from template"
    end
  else
    File.open("spec/#{@hostname}/001_httpd_spec.rb", 'w') do |f|
      f.puts content
    end
    puts " + spec/#{@hostname}/001_httpd_spec.rb"
  end
end

.safe_create_spec_helperObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/configspec/setup.rb', line 128

def self.safe_create_spec_helper
  requirements = []
  content = ERB.new(spec_helper_template, nil, '-').result(binding)
  if File.exists? 'spec/spec_helper.rb'
    old_content = File.read('spec/spec_helper.rb')
    if old_content != content
      $stderr.puts "!! spec/spec_helper.rb already exists and differs from template"
    end
  else
    File.open('spec/spec_helper.rb', 'w') do |f|
      f.puts content
    end
    puts ' + spec/spec_helper.rb'
  end
end

.safe_mkdir(dir) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/configspec/setup.rb', line 117

def self.safe_mkdir(dir)
  if File.exists? dir
    unless File.directory? dir
      $stderr.puts "!! #{dir} already exists and is not a directory"
    end
  else
    FileUtils.mkdir dir
    puts " + #{dir}/"
  end
end

.spec_helper_templateObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/configspec/setup.rb', line 202

def self.spec_helper_template
  template = "require 'configspec'\n<% if @os_type == 'UN*X' and @backend_type != 'Dockerfile' -%>\nrequire 'pathname'\n<% end -%>\n<% if @backend_type == 'Ssh' -%>\nrequire 'net/ssh'\n<% end -%>\n<% if @backend_type == 'WinRM' -%>\nrequire 'winrm'\n<% end -%>\n\n<% if @backend_type == 'Dockerfile' -%>\ninclude Configspec::Helper::<%= @backend_type %>\n<% else -%>\ninclude SpecInfra::Helper::<%= @backend_type %>\n<% end -%>\n<% if @os_type == 'UN*X' && @backend_type != 'Dockerfile' -%>\ninclude SpecInfra::Helper::DetectOS\n<% else  -%>\ninclude SpecInfra::Helper::RedHat\n<% end -%>\n\n<% if @os_type == 'UN*X' && @backend_type != 'Dockerfile' -%>\nRSpec.configure do |c|\n  if ENV['ASK_SUDO_PASSWORD']\nrequire 'highline/import'\nc.sudo_password = ask(\"Enter sudo password: \") { |q| q.echo = false }\n  else\nc.sudo_password = ENV['SUDO_PASSWORD']\n  end\n  <%- if @backend_type == 'Ssh' -%>\n  c.before :all do\nblock = self.class.metadata[:example_group_block]\nif RUBY_VERSION.start_with?('1.8')\n  file = block.to_s.match(/.*@(.*):[0-9]+>/)[1]\nelse\n  file = block.source_location.first\nend\nhost  = File.basename(Pathname.new(file).dirname)\nif c.host != host\n  c.ssh.close if c.ssh\n  c.host  = host\n  options = Net::SSH::Config.for(c.host)\n  user    = options[:user] || Etc.getlogin\n<%- if @vagrant -%>\n  vagrant_up = `vagrant up \#{@hostname}`\n  config = `vagrant ssh-config \#{@hostname}`\n  if config != ''\n    config.each_line do |line|\n      if match = /HostName (.*)/.match(line)\n        host = match[1]\n      elsif  match = /User (.*)/.match(line)\n        user = match[1]\n      elsif match = /IdentityFile (.*)/.match(line)\n        options[:keys] =  [match[1].gsub(/\\\"/,'')]\n      elsif match = /Port (.*)/.match(line)\n        options[:port] = match[1]\n      end\n    end\n  end\n<%- end -%>\n  c.ssh   = Net::SSH.start(host, user, options)\nend\n  end\n  <%- end -%>\nend\n<% end -%>\n<% if @backend_type == 'WinRM'-%>\nRSpec.configure do |c|\n  user = <username>\n  pass = <password>\n  endpoint = \"http://<hostname>:5985/wsman\"\n\n  c.winrm = ::WinRM::WinRMWebService.new(endpoint, :ssl, :user => user, :pass => pass, :basic_auth_only => true)\n  c.winrm.set_timeout 300 # 5 minutes max timeout for any operation\nend\n<% end -%>\n"
  template
end