annotate kcl/versions.py @ 0:0f44ee073173 default tip

fake salt, initial commit
author Jeff Hammel <k0scist@gmail.com>
date Mon, 06 Feb 2017 01:10:22 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 compare package versions across nodes:
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 $ sudo rpm -q --qf "%{VERSION}" python-requests
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 2.6.0
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 """
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 # imports
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 import argparse
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 import json
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 import kcl
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 import sys
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 def main(args=sys.argv[1:]):
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 """CLI"""
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 # parse command line
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 parser = argparse.ArgumentParser(description=__doc__)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 parser.add_argument('salt_master')
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 parser.add_argument('package')
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 parser.add_argument('--group', dest='group',
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 action='store_true', default=False,
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 help="group hosts by versions")
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 options = parser.parse_args(args)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 # instantiate API
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 salt = kcl.KCl(options.salt_master)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 # get versions
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 command = 'rpm -q --qf "%{VERSION}" ' + options.package
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 code, versions = salt.run(command)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 # output
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 if options.group:
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 # group host by package versions
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 group = {}
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 for host, version in versions.items():
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 group.setdefault(version, []).append(host)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 print (json.dumps(group, indent=2, sort_keys=True))
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 else:
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 print (json.dumps(versions, indent=2, sort_keys=True))
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 sys.exit(code)
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 if __name__ == '__main__':
0f44ee073173 fake salt, initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 main()