smart_proxy_salt 2.1.8 → 3.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ {% if 'cmd' in data and data['cmd'] == '_return' and 'fun' in data and (
2
+ data['fun'] == 'state.highstate' or (data['fun'] == 'state.template_str' and
3
+ 'fun_args' in data and
4
+ data['fun_args'][0].startswith('state.highstate:')
5
+ )) %}
6
+ foreman_report_upload:
7
+ runner.foreman_report_upload.now:
8
+ - args:
9
+ - highstate: '{{data|json|base64_encode}}'
10
+ {% endif %}
@@ -1,26 +1,33 @@
1
1
  #!/usr/bin/env python
2
2
  # Uploads reports from the Salt job cache to Foreman
3
3
 
4
+ from __future__ import print_function
5
+
4
6
  LAST_UPLOADED = '/etc/salt/last_uploaded'
5
7
  FOREMAN_CONFIG = '/etc/salt/foreman.yaml'
6
8
  LOCK_FILE = '/var/lock/salt-report-upload.lock'
7
9
 
8
- import httplib
10
+ try:
11
+ from http.client import HTTPConnection, HTTPSConnection
12
+ except ImportError:
13
+ from httplib import HTTPSConnection, HTTPSConnection
9
14
  import ssl
10
15
  import json
11
16
  import yaml
17
+ import io
12
18
  import os
13
19
  import sys
14
20
  import base64
15
-
16
21
  import traceback
17
-
18
22
  import salt.config
19
23
  import salt.runner
20
24
 
25
+ if sys.version_info.major == 3:
26
+ unicode = str
27
+
21
28
 
22
29
  def salt_config():
23
- with open(FOREMAN_CONFIG, 'r') as f:
30
+ with io.open(FOREMAN_CONFIG, 'r') as f:
24
31
  config = yaml.load(f.read())
25
32
  return config
26
33
 
@@ -30,9 +37,14 @@ def get_job(job_id):
30
37
 
31
38
  # If any minion's results are strings, they're exceptions
32
39
  # and should be wrapped in a list like other errors
33
- for minion, value in result.iteritems():
40
+ for minion, value in result.items():
34
41
  if type(value) == str:
35
42
  result[minion] = [value]
43
+ else:
44
+ for key, entry in value.items():
45
+ if key.startswith('module_') and entry['__id__'] == 'state.highstate':
46
+ result[minion] = entry['changes']['ret']
47
+ break
36
48
 
37
49
  return {'job':
38
50
  {
@@ -47,7 +59,7 @@ def read_last_uploaded():
47
59
  if not os.path.isfile(LAST_UPLOADED):
48
60
  return 0
49
61
  else:
50
- with open(LAST_UPLOADED, 'r') as f:
62
+ with io.open(LAST_UPLOADED, 'r') as f:
51
63
  result = f.read().strip()
52
64
  if len(result) == 20:
53
65
  try:
@@ -59,8 +71,8 @@ def read_last_uploaded():
59
71
 
60
72
 
61
73
  def write_last_uploaded(last_uploaded):
62
- with open(LAST_UPLOADED, 'w+') as f:
63
- f.write(last_uploaded)
74
+ with io.open(LAST_UPLOADED, 'w+') as f:
75
+ f.write(unicode(last_uploaded))
64
76
 
65
77
 
66
78
  def run(*args, **kwargs):
@@ -68,7 +80,7 @@ def run(*args, **kwargs):
68
80
  os.environ.get('SALT_MASTER_CONFIG', '/etc/salt/master'))
69
81
 
70
82
  runner = salt.runner.Runner(__opts__)
71
- with open(os.devnull, 'wb') as f:
83
+ with io.open(os.devnull, 'w') as f:
72
84
  stdout_bak, sys.stdout = sys.stdout, f
73
85
  try:
74
86
  ret = runner.cmd(*args, **kwargs)
@@ -79,12 +91,11 @@ def run(*args, **kwargs):
79
91
 
80
92
  def jobs_to_upload():
81
93
  jobs = run('jobs.list_jobs', kwarg={
82
- "search_function": "state.highstate",
94
+ "search_function": ["state.highstate","state.template_str"],
83
95
  })
84
96
  last_uploaded = read_last_uploaded()
85
97
 
86
- job_ids = [jid for (jid, value) in jobs.iteritems()
87
- if int(jid) > last_uploaded]
98
+ job_ids = [jid for jid in jobs.keys() if int(jid) > last_uploaded]
88
99
 
89
100
  for job_id in sorted(job_ids):
90
101
  yield job_id, get_job(job_id)
@@ -99,12 +110,12 @@ def upload(jobs):
99
110
  ctx = ssl.create_default_context()
100
111
  ctx.load_cert_chain(certfile=config[':ssl_cert'], keyfile=config[':ssl_key'])
101
112
  if config[':ssl_ca']:
102
- ctx.load_verify_locations(cafile=config[':ssl_ca'])
103
- connection = httplib.HTTPSConnection(config[':host'],
104
- port=config[':port'], context=ctx)
113
+ ctx.load_verify_locations(cafile=config[':ssl_ca'])
114
+ connection = HTTPSConnection(config[':host'],
115
+ port=config[':port'], context=ctx)
105
116
  else:
106
- connection = httplib.HTTPConnection(config[':host'],
107
- port=config[':port'])
117
+ connection = HTTPConnection(config[':host'],
118
+ port=config[':port'])
108
119
  if ':username' in config and ':password' in config:
109
120
  token = base64.b64encode('{}:{}'.format(config[':username'],
110
121
  config[':password']))
@@ -121,23 +132,24 @@ def upload(jobs):
121
132
 
122
133
  if response.status == 200:
123
134
  write_last_uploaded(job_id)
124
- print "Success %s: %s" % (job_id, response.read())
135
+ print("Success %s: %s" % (job_id, response.read()))
125
136
  else:
126
- print "Unable to upload job - aborting report upload"
127
- print response.read()
137
+ print("Unable to upload job - aborting report upload")
138
+ print(response.read())
128
139
 
129
140
 
130
141
  def get_lock():
131
142
  if os.path.isfile(LOCK_FILE):
132
143
  raise Exception("Unable to obtain lock.")
133
144
  else:
134
- open(LOCK_FILE, 'w+').close()
145
+ io.open(LOCK_FILE, 'w+').close()
135
146
 
136
147
 
137
148
  def release_lock():
138
149
  if os.path.isfile(LOCK_FILE):
139
150
  os.remove(LOCK_FILE)
140
151
 
152
+
141
153
  if __name__ == '__main__':
142
154
  try:
143
155
  get_lock()
@@ -0,0 +1,3 @@
1
+ salt:
2
+ log_file: /var/log/foreman-proxy/salt.log
3
+ log_level_logfile: debug
@@ -9,3 +9,4 @@
9
9
  :api_auth: pam
10
10
  :api_username: saltuser
11
11
  :api_password: password
12
+ # :saltfile: '/etc/foreman-proxy/settings.d/salt.saltfile'
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: 2.1.8
4
+ version: 3.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Moll
@@ -9,8 +9,92 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-03-29 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2020-06-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: test-unit
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '2'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '2'
28
+ - !ruby/object:Gem::Dependency
29
+ name: mocha
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1'
42
+ - !ruby/object:Gem::Dependency
43
+ name: webmock
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '10'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rubocop
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '='
75
+ - !ruby/object:Gem::Version
76
+ version: 0.32.1
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '='
82
+ - !ruby/object:Gem::Version
83
+ version: 0.32.1
84
+ - !ruby/object:Gem::Dependency
85
+ name: rack-test
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
14
98
  description: SaltStack Plug-In for Foreman's Smart Proxy
15
99
  email: foreman-dev@googlegroups.com
16
100
  executables:
@@ -34,11 +118,16 @@ files:
34
118
  - lib/smart_proxy_salt/salt_api.rb
35
119
  - lib/smart_proxy_salt/salt_http_config.ru
36
120
  - lib/smart_proxy_salt/version.rb
121
+ - salt/report_upload/README.md
122
+ - salt/report_upload/master.snippet
123
+ - salt/report_upload/srv/salt/_runners/foreman_report_upload.py
124
+ - salt/report_upload/srv/salt/foreman_report_upload.sls
37
125
  - sbin/upload-salt-reports
126
+ - settings.d/salt.saltfile.example
38
127
  - settings.d/salt.yml.example
39
128
  homepage: https://github.com/theforeman/smart_proxy_salt
40
129
  licenses:
41
- - GPLv3
130
+ - GPL-3.0
42
131
  metadata: {}
43
132
  post_install_message:
44
133
  rdoc_options: []
@@ -56,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
145
  version: '0'
57
146
  requirements: []
58
147
  rubyforge_project:
59
- rubygems_version: 2.5.2
148
+ rubygems_version: 2.7.6
60
149
  signing_key:
61
150
  specification_version: 4
62
151
  summary: SaltStack Plug-In for Foreman's Smart Proxy