smart_proxy_salt 0.0.2 → 1.0.0

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: acacbc60a4623c98351222437e7dc95180ffe154
4
- data.tar.gz: 1eadaeb629d7defaeb6112606bfdd25566f47866
3
+ metadata.gz: 716d33f19133ef06e16cf558c8a43f44d3bbd4db
4
+ data.tar.gz: 27104bd5181a5dd086c0ac5f76932e5a6ca59a99
5
5
  SHA512:
6
- metadata.gz: fb6e7a820f206431e75d88010840e8d6f9a2ff4068680bcab3dd9ec148931f8b355da8fc66f0f55e5e8996bf20f80e55763890343fc2126c191160f4b253773f
7
- data.tar.gz: 6dd63d90454b6becf490e615ca7dcd9bd58fc30a430af9651d9f02569e8103f43242f6636521fef4a61ca8f55faababeb80d03747398444c39099ed712baeffb
6
+ metadata.gz: 4137417072ef95e3cf394ab307c04c221e02a973b169ceeecdcaeef8980a3e610bf19825b361275ad8426023554333cc05075da5de91df36782f0a9e5c4e6d39
7
+ data.tar.gz: 47a173fe70cb02e92a169a1b3fe7cff2160a3987586db6d9e4fe9a5a8fc20d525733de325a7f29aa9fba7d95b1308315ba0cf388b6a1769cb9192108da18dfe7
@@ -0,0 +1,5 @@
1
+ SHELL=/bin/sh
2
+ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
3
+
4
+ # Check every 10 minutes for new Salt reports to upload to Foreman
5
+ */10 * * * * root /usr/sbin/upload-salt-reports >>/var/log/foreman-proxy/salt-cron.log 2>&1
@@ -1,5 +1,5 @@
1
1
  module Proxy
2
2
  module Salt
3
- VERSION = '0.0.2'
3
+ VERSION = '1.0.0'
4
4
  end
5
5
  end
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env python
2
+ # Uploads reports from the Salt job cache to Foreman
3
+
4
+ LAST_UPLOADED = '/etc/salt/last_uploaded'
5
+ FOREMAN_CONFIG = '/etc/salt/foreman.yaml'
6
+ LOCK_FILE = '/var/lock/salt-report-upload.lock'
7
+
8
+ import urllib
9
+ import httplib
10
+ import json
11
+ import yaml
12
+ import os
13
+ import sys
14
+
15
+ import salt.config
16
+ import salt.runner
17
+
18
+
19
+ def salt_config():
20
+ with open(FOREMAN_CONFIG, 'r') as f:
21
+ config = yaml.load(f.read())
22
+ return config
23
+
24
+
25
+ def get_job(job_id):
26
+ return {'job':
27
+ {
28
+ 'result': run('jobs.lookup_jid', argument=[job_id]),
29
+ 'function': 'state.highstate',
30
+ 'job_id': job_id
31
+ }
32
+ }
33
+
34
+
35
+ def read_last_uploaded():
36
+ if not os.path.isfile(LAST_UPLOADED):
37
+ return 0
38
+ else:
39
+ with open(LAST_UPLOADED, 'r') as f:
40
+ result = f.read().strip()
41
+ if len(result) == 20:
42
+ try:
43
+ return int(result)
44
+ except ValueError:
45
+ return 0
46
+ else:
47
+ return 0
48
+
49
+
50
+ def write_last_uploaded(last_uploaded):
51
+ with open(LAST_UPLOADED, 'w+') as f:
52
+ f.write(last_uploaded)
53
+
54
+
55
+ def run(function, argument={}):
56
+ __opts__ = salt.config.master_config(
57
+ os.environ.get('SALT_MASTER_CONFIG', '/etc/salt/master'))
58
+
59
+ runner = salt.runner.Runner(__opts__)
60
+ stdout_bak = sys.stdout
61
+ with open(os.devnull, 'wb') as f:
62
+ sys.stdout = f
63
+ ret = runner.cmd(function, argument)
64
+ sys.stdout = stdout_bak
65
+ return ret
66
+
67
+
68
+ def jobs_to_upload():
69
+ jobs = run('jobs.list_jobs')
70
+ last_uploaded = read_last_uploaded()
71
+
72
+ job_ids = [id for (id, value) in jobs.iteritems()
73
+ if value['Function'] == 'state.highstate' and
74
+ int(id) > last_uploaded]
75
+
76
+ jobs = dict([(job, get_job(job)) for job in job_ids])
77
+ ids = sorted(jobs.keys())
78
+
79
+ return (ids, jobs)
80
+
81
+
82
+ def upload(jobs):
83
+ config = salt_config()
84
+ headers = {'Accept': 'application/json',
85
+ 'Content-Type': 'application/json'}
86
+
87
+ if config[':proto'] == 'https':
88
+ connection = httplib.HTTPSConnection(config[':host'],
89
+ port=config[':port'], key_file=config[':ssl_key'],
90
+ cert_file=config[':ssl_cert'])
91
+ else:
92
+ connection = httplib.HTTPConnection(config[':host'],
93
+ port=config[':port'])
94
+
95
+ for id in jobs[0]:
96
+ job = jobs[1][id]
97
+
98
+ if job['job']['result'] == {}:
99
+ continue
100
+
101
+ connection.request('POST', '/salt/api/v2/jobs/upload',
102
+ json.dumps(job), headers)
103
+ response = connection.getresponse()
104
+
105
+ if response.status == 200:
106
+ write_last_uploaded(id)
107
+ print "Success %s: %s" % (id, response.read())
108
+ else:
109
+ print "Unable to upload job - aborting report upload"
110
+ print response.read()
111
+
112
+
113
+ def get_lock():
114
+ if os.path.isfile(LOCK_FILE):
115
+ raise Exception("Unable to obtain lock.")
116
+ else:
117
+ open(LOCK_FILE, 'w+').close()
118
+
119
+
120
+ def release_lock():
121
+ if os.path.isfile(LOCK_FILE):
122
+ os.remove(LOCK_FILE)
123
+
124
+ if __name__ == '__main__':
125
+ try:
126
+ get_lock()
127
+ upload(jobs_to_upload())
128
+ release_lock()
129
+ except:
130
+ release_lock()
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_proxy_salt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Moll
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-31 00:00:00.000000000 Z
12
+ date: 2014-11-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: SaltStack Plug-In for Foreman's Smart Proxy
15
15
  email: foreman-dev@googlegroups.com
@@ -20,18 +20,20 @@ extra_rdoc_files:
20
20
  - README.md
21
21
  - LICENSE
22
22
  files:
23
- - LICENSE
24
- - README.md
25
23
  - bin/foreman-node
26
- - bundler.d/salt.rb
24
+ - cron/smart_proxy_salt
27
25
  - etc/foreman.yaml.example
28
- - lib/smart_proxy_salt.rb
26
+ - lib/smart_proxy_salt/salt_main.rb
27
+ - lib/smart_proxy_salt/salt_http_config.ru
29
28
  - lib/smart_proxy_salt/salt.rb
30
29
  - lib/smart_proxy_salt/salt_api.rb
31
- - lib/smart_proxy_salt/salt_http_config.ru
32
- - lib/smart_proxy_salt/salt_main.rb
33
30
  - lib/smart_proxy_salt/version.rb
31
+ - lib/smart_proxy_salt.rb
32
+ - sbin/upload-salt-reports
34
33
  - settings.d/salt.yml.example
34
+ - bundler.d/salt.rb
35
+ - README.md
36
+ - LICENSE
35
37
  homepage: https://github.com/theforeman/smart_proxy_salt
36
38
  licenses:
37
39
  - GPLv3
@@ -42,17 +44,17 @@ require_paths:
42
44
  - lib
43
45
  required_ruby_version: !ruby/object:Gem::Requirement
44
46
  requirements:
45
- - - ">="
47
+ - - '>='
46
48
  - !ruby/object:Gem::Version
47
49
  version: '0'
48
50
  required_rubygems_version: !ruby/object:Gem::Requirement
49
51
  requirements:
50
- - - ">="
52
+ - - '>='
51
53
  - !ruby/object:Gem::Version
52
54
  version: '0'
53
55
  requirements: []
54
56
  rubyforge_project:
55
- rubygems_version: 2.2.2
57
+ rubygems_version: 2.1.11
56
58
  signing_key:
57
59
  specification_version: 4
58
60
  summary: SaltStack Plug-In for Foreman's Smart Proxy