Module: Skywatch

Includes:
FileUtils
Defined in:
lib/skywatch/tool.rb,
lib/skywatch/version.rb

Defined Under Namespace

Classes: Alert, Check, Script

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#alert(name) ⇒ Object



72
73
74
75
# File 'lib/skywatch/tool.rb', line 72

def alert(name)
  fail unless skywatch?
  Alert[Dir["./alerts/#{name}"].first]
end

#alertsObject



62
63
64
65
# File 'lib/skywatch/tool.rb', line 62

def alerts
  fail unless skywatch?
  Dir["./alerts/*"].collect{|c| Alert[c] }
end

#builtin_pathObject



49
50
51
# File 'lib/skywatch/tool.rb', line 49

def builtin_path
  File.dirname(__FILE__)+"/builtin"
end

#check(name) ⇒ Object



67
68
69
70
# File 'lib/skywatch/tool.rb', line 67

def check(name)
  fail unless skywatch?
  Check[Dir["./checks/*.#{name}"].first]
end

#checksObject



57
58
59
60
# File 'lib/skywatch/tool.rb', line 57

def checks
  fail unless skywatch?
  Dir["./checks/*"].collect{|c| Check[c] }
end

#commitObject



142
143
144
145
146
147
148
149
150
# File 'lib/skywatch/tool.rb', line 142

def commit
  fail unless skywatch?
  cd '.skywatch' do
    system 'git add .'
    if not `git status`.include? "nothing to commit"
      system 'git commit -m "skywatch commit"'
    end
  end
end

#create_alert(name, interpreter) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/skywatch/tool.rb', line 77

def create_alert(name, interpreter)
  fail unless skywatch?
  fail if File.exist? "./alerts/#{name}"
  File.open("./alerts/#{name}", 'w') do |f|
    f.write "#!/usr/bin/env #{interpreter}\n"
    if interpreter == 'bash'
      f.write "set -e\n"
    end
    f.write "# Write your alert here"
  end
  alert(name)
end

#create_check(name, interval, interpreter) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/skywatch/tool.rb', line 90

def create_check(name, interval, interpreter)
  fail unless skywatch?
  fail if File.exist? "./checks/#{interval}.#{name}"
  File.open("./checks/#{interval}.#{name}", 'w') do |f|
    f.write "#!/usr/bin/env #{interpreter}\n"
    if interpreter == 'bash'
      f.write "set -e\n"
    end
    f.write "# Write your check here"
  end
  check(name)
end

#deployObject



152
153
154
155
156
157
# File 'lib/skywatch/tool.rb', line 152

def deploy
  stage
  cd '.skywatch' do
    puts `git push heroku master`
  end
end

#destroyObject



159
160
161
162
163
# File 'lib/skywatch/tool.rb', line 159

def destroy
  fail unless skywatch?
  puts `heroku apps:destroy -a #{name} --confirm #{name}`
  rm_rf '.skywatch'
end

#init(name = "") ⇒ Object



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
# File 'lib/skywatch/tool.rb', line 6

def init(name="")
  fail if skywatch?
  mkdir '.skywatch'
  begin
    cd '.skywatch' do
      system "git init"
      puts `heroku create #{name}`
      fail if not $?.exitstatus.zero?
      puts `heroku addons:add sendgrid:starter`
      system "cp #{watcher_path}/* ."
      system "bundle install > /dev/null 2>&1"
    end
    mkdir_p 'alerts'
    if Dir['alerts/*'].empty?
      system "cp #{builtin_path}/alerts/* alerts"
    end
    mkdir_p 'checks'
    if Dir['checks/*'].empty?
      system "cp #{builtin_path}/checks/* checks"
    end
    system "echo '.skywatch' >> .gitignore"
    deploy
    cd '.skywatch' do
      system "heroku ps:scale watcher=1"
    end
  rescue
    rm_rf '.skywatch'
  end
end

#logged_in?Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/skywatch/tool.rb', line 36

def logged_in?
  `heroku auth:token < /dev/null`
  $?.exitstatus.zero?
end

#loginObject



41
42
43
# File 'lib/skywatch/tool.rb', line 41

def 
  exec 'heroku auth:login'
end

#monitorObject



122
123
124
125
126
127
# File 'lib/skywatch/tool.rb', line 122

def monitor
  fail unless skywatch?
  cd ".skywatch" do
    exec "heroku logs -t -n 10"
  end
end

#nameObject



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/skywatch/tool.rb', line 103

def name
  fail unless skywatch?
  cd ".skywatch" do
    if not File.exist? 'name'
      name = `eval $(heroku apps:info -s | grep "^name=") && echo "$name"`.strip
      File.open('name', 'w') {|f| f.write(name) }
    end
    @@name ||= File.read('name')
  end
  @@name
end

#resetObject



115
116
117
118
119
120
# File 'lib/skywatch/tool.rb', line 115

def reset
  fail unless skywatch?
  cd ".skywatch" do
    puts `heroku restart`
  end
end

#skywatch?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/skywatch/tool.rb', line 53

def skywatch?
  not Dir["./.skywatch"].empty?
end

#stageObject



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/skywatch/tool.rb', line 129

def stage
  fail unless skywatch?
  cd '.skywatch' do
    system 'git rm -f alerts/* > /dev/null 2>&1'
    system 'git rm -f checks/* > /dev/null 2>&1'
  end
  mkdir_p '.skywatch/alerts'
  mkdir_p '.skywatch/checks'
  system 'cp alerts/* .skywatch/alerts'
  system 'cp checks/* .skywatch/checks'
  commit
end

#watcher_pathObject



45
46
47
# File 'lib/skywatch/tool.rb', line 45

def watcher_path
  File.dirname(__FILE__)+"/watcher"
end