jflow 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 49c73bd7ddc1961e2cee02340e5e278ca38728b6
4
- data.tar.gz: d24a8c760b838ead40d337d6ba3942ae08ed1e7c
3
+ metadata.gz: 00ce3f02a925a9d23d449d6e58d5ee6e234b5053
4
+ data.tar.gz: 64979f0b07f76690b8f7f6d0705936d3c4679a6f
5
5
  SHA512:
6
- metadata.gz: 4f0511d6ff5150fdc07d1a7d6bcb57a50def52f2b0f8cd52bd8cc08bf0cfc79f1779e139edef360004cd63a5b75260ef19cc28d89940ae75666123e304dfcaee
7
- data.tar.gz: af71fc5d74cdcd66f762ea16ff011c40ba773721b2e136765086998d1de3e00a15c4c495f1a6696d675d312acf43b41e3d200c5ca5b02fb6e5649d33db59fe42
6
+ metadata.gz: 15148f66af1e2011e5f1a3bf68df900548d399fba332001abff1b308c4da304820c6f3bd201fe086dd2963438c163bb9122f5516eb8617fdc3e371b69f3dbbfb
7
+ data.tar.gz: 4b254b3f1313f2e99230c265f6d184f428062111ca536b36b87beb393190fab7ec0b6da7ba21fe5d47dccc67eb0725c8608258bf848e1ac03fe96126164329c0
@@ -13,6 +13,7 @@ require "jflow/activity/task.rb"
13
13
  require "jflow/activity/map.rb"
14
14
  require "jflow/activity/worker.rb"
15
15
  require "jflow/worker_thread.rb"
16
+ require "jflow/termination_protector.rb"
16
17
  require "jflow/stats.rb"
17
18
  require "jflow/cli.rb"
18
19
 
@@ -25,7 +25,7 @@ module JFlow
25
25
  number_of_workers.times do
26
26
  worker_threads << worker_thread
27
27
  end
28
- worker_threads << stats_thread if enable_stats
28
+ worker_threads << maintenance_thread if enable_stats || is_ec2_instance?
29
29
  worker_threads.each(&:join)
30
30
  end
31
31
 
@@ -63,18 +63,34 @@ module JFlow
63
63
  JFlow.load_activities
64
64
  end
65
65
 
66
- def stats_thread
66
+ def maintenance_thread
67
67
  JFlow::WorkerThread.new do
68
68
  Thread.current.set_state(:polling)
69
69
  stats = JFlow::Stats.new(@domain, @tasklist)
70
+ protector = JFlow::TerminationProtector.new
70
71
  loop do
71
72
  break if Thread.current.marked_for_shutdown?
72
- stats.tick
73
+ JFlow.configuration.logger.debug "Should protect?: #{should_protect?}"
74
+ protector.set_protection(should_protect?) if is_ec2_instance?
75
+ stats.tick if enable_stats
73
76
  sleep 30
74
77
  end
75
78
  end
76
79
  end
77
80
 
81
+ # returns true if any thread if working on someting
82
+ def should_protect?
83
+ worker_threads.each do |thread|
84
+ return true if thread.currently_working?
85
+ end
86
+ return false
87
+ end
88
+
89
+ # This should exist on all EC2 instances
90
+ def is_ec2_instance?
91
+ @ec2_instance ||= File.exist?('/sys/hypervisor/uuid')
92
+ end
93
+
78
94
  def log(str)
79
95
  JFlow.configuration.logger.info str
80
96
  end
@@ -95,4 +111,4 @@ module JFlow
95
111
  end
96
112
 
97
113
  end
98
- end
114
+ end
@@ -0,0 +1,49 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module JFlow
5
+ class TerminationProtector
6
+
7
+ def region
8
+ instance_data['region']
9
+ end
10
+
11
+ def instance_id
12
+ instance_data['instanceId']
13
+ end
14
+
15
+ # Returns a hash of instance data, including region, instance id + more
16
+ def instance_data
17
+ @instance_data ||= JSON.parse(Net::HTTP.get(URI.parse('http://169.254.169.254/latest/dynamic/instance-identity/document')))
18
+ end
19
+
20
+ def get_asg_name
21
+ ec2_client = Aws::EC2::Client.new(region: region)
22
+ instance_tags = ec2_client.describe_tags(filters: [
23
+ {
24
+ name: "resource-id",
25
+ values: [instance_id]
26
+ }
27
+ ])[0]
28
+ asg_name = instance_tags.select{|tag| tag.key == "aws:autoscaling:groupName"}.first.value
29
+ JFlow.configuration.logger.debug "Discovered autoscaling group name #{asg_name}"
30
+
31
+ asg_name
32
+ end
33
+
34
+ def set_protection(protect_status)
35
+ JFlow.configuration.logger.debug "Setting termination protection status to #{protect_status} for instance #{@instance_id} in region #{@region}"
36
+ begin
37
+ asg_client = Aws::AutoScaling::Client.new(region: region)
38
+ asg_client.set_instance_protection({
39
+ instance_ids: [instance_id],
40
+ auto_scaling_group_name: get_asg_name,
41
+ protected_from_scale_in: protect_status
42
+ })
43
+ rescue => e
44
+ JFlow.configuration.logger.debug "Something went wrong setting termination proection: #{e.inspect}"
45
+ end
46
+ end
47
+
48
+ end
49
+ end
@@ -1,3 +1,3 @@
1
1
  module JFlow
2
- VERSION = "0.4.2"
2
+ VERSION = "0.4.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jflow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christophe Verbinnen
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-05-02 00:00:00.000000000 Z
12
+ date: 2016-05-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk
@@ -140,6 +140,7 @@ files:
140
140
  - lib/jflow/configuration.rb
141
141
  - lib/jflow/domain.rb
142
142
  - lib/jflow/stats.rb
143
+ - lib/jflow/termination_protector.rb
143
144
  - lib/jflow/version.rb
144
145
  - lib/jflow/worker_thread.rb
145
146
  homepage: https://github.com/djpate/jflow