Browse Source

more minor tweaks

David Yurkiewicz 8 months ago
parent
commit
9b8b1aaa18
1 changed files with 68 additions and 35 deletions
  1. 68 35
      miniConfigApply.py

+ 68 - 35
miniConfigApply.py

@@ -1,18 +1,25 @@
1 1
 #!/usr/bin/env python3
2
-# miniConfig
3
-# A very simple config management system
2
+"""
3
+miniConfig
4
+A very simple config management system
5
+"""
4 6
 
5 7
 import os
6 8
 import sys
7 9
 import json
8 10
 import hashlib
9
-from pprint import pprint
10 11
 from pssh.clients.native import ParallelSSHClient
11 12
 from gevent import joinall
12 13
 from pssh.utils import enable_host_logger
13 14
 enable_host_logger()
14 15
 
15 16
 class MiniConfig():
17
+    """
18
+    miniConfig is a python3 based minimal config management
19
+    that focuses mainly on add/removing files and packages.
20
+    miniConfig runs ssh in parallel to allow the management
21
+    of multiple nodes.
22
+    """
16 23
     hosts = []
17 24
     pkgs = []
18 25
     files = {}
@@ -33,8 +40,11 @@ class MiniConfig():
33 40
         self.file_path = self.role_path + '/' + defaultFiledir
34 41
 
35 42
     def config_parser(self):
36
-        with open(self.config_path) as f:
37
-            data = json.load(f)
43
+        """
44
+        Parses config.json and creates dict of config vars
45
+        """
46
+        with open(self.config_path) as config_file:
47
+            data = json.load(config_file)
38 48
             self.hosts.append(data['hosts'])
39 49
             self.pkgs.append(data['packages'][0])
40 50
             self.files = data['files']
@@ -42,7 +52,9 @@ class MiniConfig():
42 52
         return data
43 53
 
44 54
     def apply_config(self):
45
-        # Parse config.json
55
+        """
56
+        Apply provided config.json
57
+        """
46 58
         config = self.config_parser()
47 59
         # start ssh session
48 60
         self.client = ParallelSSHClient(self.hosts[0], user=self.ssh_user, password=self.ssh_pass)
@@ -65,7 +77,10 @@ class MiniConfig():
65 77
         if len(self.cmds) != 0:
66 78
             self.run_cmd(self.cmds)
67 79
 
68
-    def run_cmd(self, cmds=[]):
80
+    def run_cmd(self, cmds):
81
+        """
82
+        Run commands on remote machine(s)
83
+        """
69 84
         for cmd in cmds[0]:
70 85
             run_command = self.client.run_command(cmd, sudo=True)
71 86
             self.client.join(run_command, consume_output=True)
@@ -76,47 +91,59 @@ class MiniConfig():
76 91
                     print('[%s] Ran [ %s ]' % (host, cmd))
77 92
 
78 93
 
79
-    def add_package(self, pkgs=[]):
80
-        for pk in pkgs:
81
-            verify = self.client.run_command('apt list --installed | grep '+ pk + '/', sudo=True)
94
+    def add_package(self, pkgs):
95
+        """
96
+        Add packages provided on config.json
97
+        """
98
+        for pk_ in pkgs:
99
+            verify = self.client.run_command('apt list --installed | grep '+ pk_ + '/', sudo=True)
82 100
             self.client.join(verify, consume_output=False)
83 101
             for host, host_output in verify.items():
84 102
                 if host_output.exit_code != 0:
85
-                    print('[%s] %s not installed' % (host, pk))
103
+                    print('[%s] %s not installed' % (host, pk_))
86 104
                     install = ParallelSSHClient([host], user=self.ssh_user, password=self.ssh_pass)
87
-                    output = install.run_command('apt install -y ' + pk, sudo=True)
105
+                    output = install.run_command('apt install -y ' + pk_, sudo=True)
88 106
                     self.client.join(output, consume_output=True)
89
-                    for host, host_output in output.items():
90
-                        print('[%s] Installing %s' % (host, pk))
91
-                        if host_output.exit_code != 0:
92
-                            print('[%s] Error installing %s' % (host, pk))
107
+                    for host_, host_output_ in output.items():
108
+                        print('[%s] Installing %s' % (host_, pk_))
109
+                        if host_output_.exit_code != 0:
110
+                            print('[%s] Error installing %s' % (host_, pk_))
93 111
                         else:
94
-                            print('[%s] %s installed' % (host, pk))
112
+                            print('[%s] %s installed' % (host_, pk_))
95 113
                 else:
96
-                    print('[%s] %s already installed. Nothing to do!' % (host, pk))
114
+                    print('[%s] %s already installed. Nothing to do!' % (host, pk_))
97 115
 
98
-    def rm_package(self, pkgs=[]):
99
-        for pk in pkgs:
116
+    def rm_package(self, pkgs):
117
+        """
118
+        Removes packes provided by config.json
119
+        """
120
+        for pk_ in pkgs:
100 121
             # Check if package is installed
101
-            verify = self.client.run_command('apt list --installed | grep '+ pk + '/', sudo=True)
122
+            verify = self.client.run_command('apt list --installed | grep '+ pk_ + '/', sudo=True)
102 123
             self.client.join(verify, consume_output=False)
103 124
             for host, host_output in verify.items():
104 125
                 if host_output.exit_code == 0:
105 126
                     # Try removing package since we found a ref of it with apt list
106
-                    print('[%s] %s installed on system' % (host, pk))
127
+                    print('[%s] %s installed on system' % (host, pk_))
107 128
                     remove = ParallelSSHClient([host], user=self.ssh_user, password=self.ssh_pass)
108
-                    output = remove.run_command('apt remove --purge -y ' + pk, sudo=True)
129
+                    output = remove.run_command('apt remove --purge -y ' + pk_, sudo=True)
109 130
                     self.client.join(output, consume_output=True)
110
-                    for host, host_output in output.items():
111
-                        print('[%s] Removing %s' % (host, pk))
112
-                        if host_output.exit_code != 0:
113
-                            print('[%s] Error removing %s' % (host, pk))
131
+                    for host_, host_output_ in output.items():
132
+                        print('[%s] Removing %s' % (host_, pk_))
133
+                        if host_output_.exit_code != 0:
134
+                            print('[%s] Error removing %s' % (host_, pk_))
114 135
                         else:
115
-                            print('[%s] %s removed' % (host, pk))
136
+                            print('[%s] %s removed' % (host_, pk_))
116 137
                 else:
117
-                    print('[%s] %s not installed. Nothing to do' % (host, pk))
138
+                    print('[%s] %s not installed. Nothing to do' % (host, pk_))
118 139
 
119
-    def add_file(self, file={}):
140
+    def add_file(self, file):
141
+        """
142
+        Adds files defined under config.json
143
+        Files are copied over from roles/$role_name/files dir
144
+        First we check if the file is in the remote system
145
+        A md5sum hash is compared to see if the file has change
146
+        """
120 147
         local_file_path = self.file_path + '/' + file['name']
121 148
         remote_file_path = file['path'] + '/' + file['name']
122 149
         # Get local md5hash to compare with remote file
@@ -182,6 +209,9 @@ class MiniConfig():
182 209
 
183 210
 
184 211
     def rm_file(self, file={}):
212
+        """
213
+        Removes files provided in config.json
214
+        """
185 215
         remote_file_path = file['path'] + '/' + file['name']
186 216
         # Stat the file to make sure is there before attempting to remove
187 217
         verify = self.client.run_command('/usr/bin/stat -t ' + remote_file_path, sudo=True)
@@ -199,8 +229,11 @@ class MiniConfig():
199 229
             else:
200 230
                 print('[%s] %s not on system. Nothing to do!' % (host, remote_file_path))
201 231
 
202
-if len(sys.argv) == 1:
203
-    print("Missing role name. Please provide role to apply")
204
-for i in range(1, len(sys.argv)):
205
-    t = MiniConfig(sys.argv[i])
206
-    t.apply_config()
232
+
233
+if __name__ == "__main__":
234
+
235
+    if len(sys.argv) == 1:
236
+        print("Missing role name. Please provide role to apply")
237
+    for i in range(1, len(sys.argv)):
238
+        t = MiniConfig(sys.argv[i])
239
+        t.apply_config()