2
0
mirror of https://github.com/xcat2/confluent.git synced 2025-02-10 15:55:27 +00:00

Finish syncflieclient

This commit is contained in:
Jarrod Johnson 2021-03-29 13:50:07 -04:00
parent ca0c592044
commit 54c5bf128e

View File

@ -1,5 +1,9 @@
#!/usr/bin/python
import importlib
import tempfile
import json
import os
import shutil
from importlib.machinery import SourceFileLoader
try:
apiclient = SourceFileLoader('apiclient', '/opt/confluent/bin/apiclient').load_module()
@ -11,7 +15,7 @@ def partitionhostsline(line):
comment = ''
try:
cmdidx = line.index('#')
comment = line[cmdiddx:]
comment = line[cmdidx:]
line = line[:cmdidx].strip()
except ValueError:
pass
@ -125,12 +129,12 @@ class CredMerger:
except ValueError:
continue
if targfile:
if uid < minid or uid > maxid:
if uid <= minid or uid >= maxid:
self.targdata.append(line)
else:
self.discardnames[name] = 1
else:
if uid > minid and uid < maxid:
if uid >= minid and uid <= maxid:
self.sourcedata.append(line)
def read_shadow(self, source):
@ -178,7 +182,38 @@ class CredMerger:
continue
shadout.write(name + ':!::\n')
def synchronize():
tmpdir = tempfile.mkdtemp()
try:
ac = apiclient.HTTPSClient()
data = json.dumps({'merge': tmpdir})
status, rsp = ac.grab_url_with_status('/confluent-api/self/remotesyncfiles', data)
if status == 202:
while status != 204:
status, rsp = ac.grab_url_with_status('/confluent-api/self/remotesyncfiles')
if not isinstance(rsp, str):
rsp = rsp.decode('utf8')
pendpasswd = os.path.join(tmpdir, 'etc/passwd')
if os.path.exists(pendpasswd):
cm = CredMerger()
cm.read_passwd(pendpasswd, targfile=False)
cm.read_passwd('/etc/passwd', targfile=True)
cm.write_out('/etc/passwd')
pendgroup = os.path.join(tmpdir, 'etc/group')
if os.path.exists(pendgroup):
cm = CredMerger()
cm.read_group(pendgroup, targfile=False)
cm.read_group('/etc/group', targfile=True)
cm.write_out('/etc/group')
pendhosts = os.path.join(tmpdir, 'etc/hosts')
if os.path.exists(pendhosts):
cm = HostMerger()
cm.read_source(pendhosts)
cm.read_target('/etc/hosts')
cm.write_out('/etc/hosts')
finally:
shutil.rmtree(tmpdir)
if __name__ == '__main__':
synchronize()