port-authority-prz 0.5.5 → 0.5.6

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: c715abe065e3422335e537f140e91efb992de022
4
- data.tar.gz: 6d1e0a1a556fc910b78bac4dc3ba940494a66ad0
3
+ metadata.gz: bd836ca32f8398911e4e6ddc0985225c1df41e9d
4
+ data.tar.gz: fa429af2b0beab602e57706a19c271e35635e23a
5
5
  SHA512:
6
- metadata.gz: e47512c1a1be378e350fa376e20b9087c877cfc95fb0c5d0f7ba272512ef273ccbaca2446d690a44739483e6d30c965483088b46f60b8afc643ca52d1fb576ba
7
- data.tar.gz: a14b67d140beeb4a147af9ada12b2cb6907c43bfb81e9adf429853f9d1ce3c70d7542d3f29c199ceff848ef0783fcfa29a1b15071ee9c7c15abb56d5abee1fd0
6
+ metadata.gz: bf77d0d4a46bfa21effdb9d6033c344f8514cd663d1db45dd47c6445606f02f1fd696cac7da93833ec1d36cf4cfbab042e6ad0a01f9565d980366a5f86c981f8
7
+ data.tar.gz: 0aa354ed18c0e4027fe0583be7ec6c99d8869e09b6ac8e8aa9d598c4d6e5597d655903a55db99a32494a2f942c01d8ba48562cb34906d4bc8a68a9306295e68c
data/bin/pa-cron-agent ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'port-authority/agents/cronsvc'
3
+ PortAuthority::Agents::CronSvc.new
@@ -0,0 +1,93 @@
1
+ # rubocop:disable MethodLength, CyclomaticComplexity, Metrics/BlockNesting, Metrics/LineLength, Metrics/AbcSize, Metrics/PerceivedComplexity
2
+ require 'socket'
3
+ require 'port-authority/agent'
4
+ require 'port-authority/mechanism/cron'
5
+
6
+ module PortAuthority
7
+ module Agents
8
+ class CronSvc < PortAuthority::Agent
9
+ include PortAuthority::Mechanism
10
+
11
+ def run
12
+ setup(daemonize: Config.daemonize, nice: -10, root: true)
13
+ Signal.trap('HUP') { Config.load! && Cron.init! }
14
+ Signal.trap('USR1') { Logger.debug! }
15
+ Signal.trap('USR2') { @cron_update_hook = true }
16
+ @status_swarm = false
17
+ @etcd = PortAuthority::Etcd.cluster_connect Config.etcd
18
+
19
+ thr_create(:swarm, Config.cron[:swarm_interval] || Config.cron[:interval]) do
20
+ begin
21
+ Logger.debug 'Checking Swarm state'
22
+ status = @etcd.am_i_swarm_leader?
23
+ thr_safe { @status_swarm = status }
24
+ Logger.debug "I am Swarm #{status ? 'leader' : 'follower' }"
25
+ rescue StandardError => e
26
+ Logger.error [ e.class, e.message ].join(': ')
27
+ e.backtrace.each {|line| Logger.debug " #{line}"}
28
+ thr_safe { @status_swarm = false }
29
+ sleep(Config.cron[:swarm_interval] || Config.cron[:interval])
30
+ retry unless exit?
31
+ end
32
+ end
33
+
34
+ thr_start
35
+
36
+ Cron.init!
37
+
38
+ Logger.debug 'Waiting for threads to gather something...'
39
+ sleep Config.cron[:interval]
40
+ first_cycle = true
41
+ status_time = Time.now.to_i - 60
42
+
43
+ until exit?
44
+ status_swarm = false if first_cycle
45
+ if @cron_update_hook
46
+ Logger.notice 'Cron update triggerred'
47
+ Cron.update!
48
+ @cron_update_hook = false
49
+ Logger.notice 'Cron update finished'
50
+ end
51
+ sleep Config.cron[:interval]
52
+ thr_safe(:swarm) { status_swarm = @status_swarm }
53
+ # main logic
54
+ if status_swarm
55
+ # handle FloatingIP on leader
56
+ Logger.debug 'I am the LEADER'
57
+ # handle LoadBalancer on leader
58
+ if Cron.up?
59
+ Logger.debug 'Cron is up, that is OK'
60
+ else
61
+ Logger.notice 'Cron is down, starting'
62
+ Cron.start!
63
+ end
64
+ else
65
+ # handle LoadBalancer on follower
66
+ if Cron.up?
67
+ Logger.notice 'Cron is up, stopping'
68
+ Cron.stop!
69
+ else
70
+ Logger.debug 'Cron is down, that is OK'
71
+ end
72
+ end # logic end
73
+ end
74
+
75
+ thr_wait
76
+
77
+ # stop LB on shutdown
78
+ if Cron.up?
79
+ Logger.notice 'Stopping Cron'
80
+ Cron.stop!
81
+ end
82
+
83
+ Logger.notice 'Exiting...'
84
+ exit 0
85
+ end
86
+
87
+ def my_ip
88
+ @my_ip ||= Socket.ip_address_list.detect(&:ipv4_private?).ip_address
89
+ end
90
+
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,86 @@
1
+ require 'docker-api'
2
+
3
+ module PortAuthority
4
+ module Mechanism
5
+ module Cron
6
+
7
+ extend self
8
+
9
+ attr_reader :_container, :_container_def, :_image
10
+
11
+ def init!
12
+ Docker.url = Config.cron[:docker_endpoint]
13
+ Docker.options = { connect_timeout: Config.cron[:docker_timeout] || 10 }
14
+ self.container || ( self.pull! && self.create! )
15
+ end
16
+
17
+ def container
18
+ @_container ||= Docker::Container.get(Config.cron[:name]) rescue nil
19
+ end
20
+
21
+ def image
22
+ @_image ||= Docker::Image.create('fromImage' => Config.cron[:image])
23
+ end
24
+
25
+ def pull!
26
+ @_image = Docker::Image.create('fromImage' => Config.cron[:image])
27
+ end
28
+
29
+ def create!
30
+ @_container_def = {
31
+ 'Image' => self.image.json['Id'],
32
+ 'name' => Config.cron[:name],
33
+ 'Hostname' => Config.cron[:name],
34
+ 'Env' => [ "ETCDCTL_ENDPOINT=#{Config.etcd[:endpoints].join(',')}","#{Config.cron[:env]}" ],
35
+ 'RestartPolicy' => { 'Name' => 'never' },
36
+ 'HostConfig' => {
37
+ 'Binds' => [ "#{Config.cron[:folder]}" ],
38
+ 'NetworkMode' => Config.cron[:network]
39
+ }
40
+ }
41
+ if Config.cron[:log_dest] != ''
42
+ @_container_def['HostConfig']['LogConfig'] = {
43
+ 'Type' => 'gelf',
44
+ 'Config' => {
45
+ 'gelf-address' => Config.cron[:log_dest],
46
+ 'tag' => Socket.gethostbyname(Socket.gethostname).first + '/{{.Name}}/{{.ID}}'
47
+ }
48
+ }
49
+ end
50
+ @_container = Docker::Container.create(@_container_def)
51
+ end
52
+
53
+
54
+ def update!
55
+ begin
56
+ ( self.stop! && start = true ) if self.up?
57
+ self.remove!
58
+ self.pull!
59
+ self.create!
60
+ self.start! if start == true
61
+ rescue StandardError => e
62
+ Logger.error "UNCAUGHT EXCEPTION IN THREAD #{Thread.current[:name]}"
63
+ Logger.error [' ', e.class, e.message].join(' ')
64
+ Logger.error ' ' + e.backtrace.to_s
65
+ end
66
+ end
67
+
68
+ def remove!
69
+ @_container.delete
70
+ end
71
+
72
+ def up?
73
+ @_container.json['State']['Running']
74
+ end
75
+
76
+ def start!
77
+ @_container.start
78
+ end
79
+
80
+ def stop!
81
+ @_container.stop
82
+ end
83
+
84
+ end
85
+ end
86
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: port-authority-prz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.5.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Radek 'blufor' Slavicinsky
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-06-02 00:00:00.000000000 Z
13
+ date: 2016-06-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: etcd
@@ -96,18 +96,22 @@ description: Highly opinionated PaaS based on Docker Swarm and ETCD
96
96
  email: devops@prozeta.eu
97
97
  executables:
98
98
  - pa-service-list
99
+ - pa-cron-agent
99
100
  - pa-lbaas-agent
100
101
  extensions: []
101
102
  extra_rdoc_files: []
102
103
  files:
104
+ - bin/pa-cron-agent
103
105
  - bin/pa-lbaas-agent
104
106
  - bin/pa-service-list
105
107
  - lib/port-authority.rb
106
108
  - lib/port-authority/agent.rb
109
+ - lib/port-authority/agents/cronsvc.rb
107
110
  - lib/port-authority/agents/lbaas.rb
108
111
  - lib/port-authority/config.rb
109
112
  - lib/port-authority/etcd.rb
110
113
  - lib/port-authority/logger.rb
114
+ - lib/port-authority/mechanism/cron.rb
111
115
  - lib/port-authority/mechanism/floating_ip.rb
112
116
  - lib/port-authority/mechanism/load_balancer.rb
113
117
  - lib/port-authority/tool.rb