Class: Seiton::Init

Inherits:
Object
  • Object
show all
Defined in:
lib/seiton/init.rb

Class Method Summary collapse

Class Method Details

.create_directoryObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/seiton/init.rb', line 9

def self.create_directory
  puts 'Create spec directory...'
  FileUtils.mkdir_p('spec') unless FileTest.exist?('spec')
  spec_helper = "require 'awspec'\nAwsecrets.load(secrets_path: File.expand_path('./secrets.yml', File.dirname(__FILE__)))\n  SPEC_HELPER\n\n  File.open('spec/spec_helper.rb', 'w') do |f|\n    f.puts(spec_helper)\n  end\nend\n"

.welcomeObject



3
4
5
6
7
# File 'lib/seiton/init.rb', line 3

def self.welcome
  puts 'Welcome to seiton (整頓) !!'
  create_directory
  write_rakefile
end

.write_rakefileObject



22
23
24
25
26
27
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/seiton/init.rb', line 22

def self.write_rakefile
  puts 'Write Rakefile...'
  rakefile = "require 'rspec/core/rake_task'\nrequire 'aws-sdk'\nrequire 'highline'\nrequire 'logger'\n\nnamespace :check do\n  targets1 = []\n  targets2 = []\n  Dir.glob(['./spec/*']).each do |file|\ntarget = File.basename(file)\nif target.include?('_spec.rb') then\n  targets1 << File.basename(target, '_spec.rb')\nelsif target.include?('_list.txt') then\n  targets2 << File.basename(target, '_list.txt')\nend\n  end\n\n  targets1.each do |target|\ndesc \"Test for \\\#{target} deleted\"\n\nRSpec::Core::RakeTask.new(target.to_sym) do |t|\n  # t.rspec_opts = [\"--format documentation\", \"--format html\", \"--out ./result_html/\\\#{target}_result.html\"]\n  t.rspec_opts = [\"--format documentation\"]\n  t.pattern = \"spec/\\\#{target}_spec.rb\"\n  t.verbose = true\nend if target != 'ec2_snapshot'\n  end\n\n  targets2.each do |target|\ndesc \"Test for \\\#{target} deleted\"\ntask target.to_sym do\n  log.info('Check that ' + target + ' has been deleted.')\n  resouces = []\n  File.open('./spec/' + target + '_list.txt', \"r\") do |f|\n    f.each_line do |line|\n      resouces << line.chomp\n    end\n  end\n  eval \"check_\\\#{target}(resouces)\"\nend\n  end\nend\n\ndef ec2_client\n  @ec2_client ||= Aws::EC2::Client.new\nend\n\ndef rds_client\n  @rds_client ||= Aws::RDS::Client.new\nend\n\ndef hl\n  @hl ||= HighLine.new\nend\n\ndef log\n  @log ||= Logger.new(STDOUT)\nend\n\ndef ignore_resource?(resource)\n  if resource.include?('ignore|')\nr = resource.split('|').last\nlog.info('[ ' + hl.color(\"!\", :yellow) + ' ] ' + r + ' is not for deletion.')\nnot_deleted = '[ ' + hl.color(\"\\\\u2714\".encode('utf-8'), :green) + ' ] ' + r + ' exists.'\ndeleted = '[ ' + hl.color(\"\\\\u2715\".encode('utf-8'), :red) + ' ] ' + r + ' has been removed.'\n  else\nr = resource\ndeleted = '[ ' + hl.color(\"\\\\u2714\".encode('utf-8'), :green) + ' ] ' + r + ' has been removed..'\nnot_deleted = '[ ' + hl.color(\"\\\\u2715\".encode('utf-8'), :red) + ' ] ' + r + ' exists.'\n  end\n  return r, deleted, not_deleted\nend\n\ndef check_ec2_snapshots(resouces)\n  resouces.each do |resouce|\nr, deleted, not_deleted = ignore_resource?(resouce)\nbegin\n  ec2_client.describe_snapshots({ snapshot_ids: [ r ] })\n  log.info(not_deleted)\nrescue\n  log.info(deleted)\nend\n  end\nend\n\ndef check_ec2_eips(resouces)\n  resouces.each do |resouce|\nr, deleted, not_deleted = ignore_resource?(resouce)\nres = ec2_client.describe_addresses({ filters: [{ name: 'public-ip', values: [r] }] })\nif res.addresses.empty?\n  log.info(deleted)\nelse\n  log.info(not_deleted)\nend\n  end\nend\n\ndef check_db_snapshots(resouces)\n  resouces.each do |resouce|\nr, deleted, not_deleted = ignore_resource?(resouce)\nres = rds_client.describe_db_cluster_snapshots(db_cluster_snapshot_identifier: r)\nif res.db_cluster_snapshots.empty?\n  log.info(deleted)\nelse\n  log.info(not_deleted)\nend\n  end\nend\n  RAKE_FILE\n\n  File.open('Rakefile', 'w') do |f| \n    f.puts(rakefile)\n  end unless FileTest.exist?('Rakefile')\nend\n"