forked from ungleich-public/cdist
wrap into a class
Signed-off-by: Nico Schottelius <nico@kr.ethz.ch>
This commit is contained in:
parent
572401e4f8
commit
e8a6d89e3c
1 changed files with 98 additions and 89 deletions
111
bin/cdist
111
bin/cdist
|
@ -30,25 +30,36 @@ class Cdist:
|
||||||
"""Cdist main class to hold arbitrary data"""
|
"""Cdist main class to hold arbitrary data"""
|
||||||
version="2.0.0"
|
version="2.0.0"
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, hostname):
|
||||||
self.tempdir = tempfile.mkdtemp()
|
self.tempdir = tempfile.mkdtemp()
|
||||||
|
self.hostname = hostname
|
||||||
|
print(self.hostname)
|
||||||
print(self.tempdir)
|
print(self.tempdir)
|
||||||
|
|
||||||
def __del__(self):
|
def cleanup(self):
|
||||||
print("Zerstoeren")
|
# Do not use in __del__:
|
||||||
|
# http://docs.python.org/reference/datamodel.html#customization
|
||||||
|
# "other globals referenced by the __del__() method may already have been deleted
|
||||||
|
# or in the process of being torn down (e.g. the import machinery shutting down)"
|
||||||
|
#
|
||||||
print(self.tempdir)
|
print(self.tempdir)
|
||||||
shutil.rmtree(self.tempdir)
|
shutil.rmtree(self.tempdir)
|
||||||
|
|
||||||
def logger(type, *args):
|
def out_dir(self):
|
||||||
|
# FIXME: stopped - probably need static temp know!
|
||||||
|
"""Local directory containing output"""
|
||||||
|
return os.path.join(base_directory(), "conf")
|
||||||
|
|
||||||
|
def logger(self,type, *args):
|
||||||
"""Ignore type for now, support later"""
|
"""Ignore type for now, support later"""
|
||||||
print(*args)
|
print(*args)
|
||||||
|
|
||||||
def exit_error(*args):
|
def exit_error(self,*args):
|
||||||
logger("error", *args)
|
self.logger("error", *args)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def run_or_fail(*args):
|
def run_or_fail(self,*args):
|
||||||
# newargs = ["echo"]
|
# newargs = ["echo"]
|
||||||
newargs = []
|
newargs = []
|
||||||
newargs.extend(*args)
|
newargs.extend(*args)
|
||||||
|
@ -57,75 +68,71 @@ def run_or_fail(*args):
|
||||||
try:
|
try:
|
||||||
subprocess.check_call(newargs)
|
subprocess.check_call(newargs)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
exit_error("Command failed:", " ".join(newargs))
|
self.exit_error("Command failed:", " ".join(newargs))
|
||||||
|
|
||||||
def remote_run_or_fail(hostname, *args):
|
def remote_run_or_fail(self, *args):
|
||||||
"""Run something on the remote side and fail is something breaks"""
|
"""Run something on the remote side and fail is something breaks"""
|
||||||
newargs = ["ssh", "root@" + hostname]
|
newargs = ["ssh", "root@" + self.hostname]
|
||||||
newargs.extend(*args)
|
newargs.extend(*args)
|
||||||
run_or_fail(newargs)
|
print(newargs, "bbbbb", self.hostname)
|
||||||
|
self.run_or_fail(newargs)
|
||||||
|
|
||||||
def remove_remote_dir(hostname, destination):
|
def remove_remote_dir(self, destination):
|
||||||
remote_run_or_fail(hostname, ["rm", "-rf", destination])
|
self.remote_run_or_fail(["rm", "-rf", destination])
|
||||||
|
|
||||||
def transfer_dir(hostname, source, destination):
|
def transfer_dir(self, source, destination):
|
||||||
remove_remote_dir(hostname, destination)
|
self.remove_remote_dir(destination)
|
||||||
run_or_fail(["scp", "-r", source, "root@" + hostname + ":" + destination])
|
self.run_or_fail(["scp", "-qr", source, "root@" + self.hostname + ":" + destination])
|
||||||
|
|
||||||
def base_directory():
|
def base_directory(self):
|
||||||
"""Returns the directory in which all cdist stuff is based in"""
|
"""Returns the directory in which all cdist stuff is based in"""
|
||||||
print("Going to", __file__, os.path.join(os.path.dirname(__file__), os.pardir))
|
print("Going to", __file__, os.path.join(os.path.dirname(__file__), os.pardir))
|
||||||
os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
|
os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
|
||||||
return os.getcwd()
|
return os.getcwd()
|
||||||
|
|
||||||
def remote_base_directory():
|
def remote_base_directory(self):
|
||||||
return "/var/lib/cdist"
|
return "/var/lib/cdist"
|
||||||
|
|
||||||
def conf_directory():
|
def conf_directory(self):
|
||||||
"""Returns path to main configuration directory"""
|
"""Returns path to main configuration directory"""
|
||||||
return os.path.join(base_directory(), "conf")
|
return os.path.join(self.base_directory(), "conf")
|
||||||
|
|
||||||
def remote_conf_directory():
|
def remote_conf_directory(self):
|
||||||
"""Returns path to remote main configuration directory"""
|
"""Returns path to remote main configuration directory"""
|
||||||
return os.path.join(remote_base_directory(), "conf")
|
return os.path.join(self.remote_base_directory(), "conf")
|
||||||
|
|
||||||
def out_dir():
|
def global_explorer_directory(self):
|
||||||
# FIXME: stopped - probably need static temp know!
|
|
||||||
"""Local directory containing output"""
|
|
||||||
return os.path.join(base_directory(), "conf")
|
|
||||||
|
|
||||||
def global_explorer_directory():
|
|
||||||
"""Returns path to directory containing the global explorers"""
|
"""Returns path to directory containing the global explorers"""
|
||||||
return os.path.join(conf_directory(), "explorer")
|
return os.path.join(self.conf_directory(), "explorer")
|
||||||
|
|
||||||
def remote_global_explorer_directory():
|
def remote_global_explorer_directory(self):
|
||||||
"""Returns path to the remote directory containing the global explorers"""
|
"""Returns path to the remote directory containing the global explorers"""
|
||||||
return os.path.join(remote_conf_directory(), "explorer")
|
return os.path.join(self.remote_conf_directory(), "explorer")
|
||||||
|
|
||||||
def remote_global_explorer_path(explorer):
|
def remote_global_explorer_path(self,explorer):
|
||||||
"""Returns path to the remote explorer"""
|
"""Returns path to the remote explorer"""
|
||||||
return os.path.join(remote_global_explorer_directory(), explorer)
|
return os.path.join(self.remote_global_explorer_directory(), explorer)
|
||||||
|
|
||||||
def list_global_explorers():
|
def list_global_explorers(self):
|
||||||
"""Return list of available explorers"""
|
"""Return list of available explorers"""
|
||||||
return os.listdir(global_explorer_directory())
|
return os.listdir(self.global_explorer_directory())
|
||||||
|
|
||||||
def transfer_global_explorers(hostname):
|
def transfer_global_explorers(self):
|
||||||
transfer_dir(hostname, global_explorer_directory(), remote_global_explorer_directory())
|
self.transfer_dir(self.global_explorer_directory(), self.remote_global_explorer_directory())
|
||||||
|
|
||||||
def global_explore(hostname):
|
def global_explore(self):
|
||||||
"""Run global explorers"""
|
"""Run global explorers"""
|
||||||
explorers = list_global_explorers()
|
explorers = self.list_global_explorers()
|
||||||
if(len(explorers) == 0):
|
if(len(explorers) == 0):
|
||||||
exit_error("No explorers found in", global_explorer_directory())
|
self.exit_error("No explorers found in", self.global_explorer_directory())
|
||||||
|
|
||||||
transfer_global_explorers(hostname)
|
self.transfer_global_explorers()
|
||||||
for explorer in explorers:
|
for explorer in explorers:
|
||||||
remote_run_or_fail(hostname, [remote_global_explorer_path(explorer)])
|
self.remote_run_or_fail([self.remote_global_explorer_path(explorer)])
|
||||||
|
|
||||||
|
|
||||||
def init_deploy(hostname):
|
def init_deploy(self):
|
||||||
logger("info", "Creating clean directory structure")
|
self.logger("info", "Creating clean directory structure")
|
||||||
|
|
||||||
# Ensure there is no old stuff, neither local nor remote
|
# Ensure there is no old stuff, neither local nor remote
|
||||||
# run_or_fail(["rm -rf", "$__cdist_local_base_dir"])
|
# run_or_fail(["rm -rf", "$__cdist_local_base_dir"])
|
||||||
|
@ -139,18 +146,20 @@ def init_deploy(hostname):
|
||||||
# # Link configuraion source directory - consistent with remote
|
# # Link configuraion source directory - consistent with remote
|
||||||
# run_or_fail(["ln -sf", "$__cdist_conf_dir", "$__cdist_local_base_dir/$__cdist_name_conf_dir"])
|
# run_or_fail(["ln -sf", "$__cdist_conf_dir", "$__cdist_local_base_dir/$__cdist_name_conf_dir"])
|
||||||
|
|
||||||
def cdist_deploy_to(hostname):
|
def deploy_to(self):
|
||||||
"""Mimic the old deploy to: Deploy to one host"""
|
"""Mimic the old deploy to: Deploy to one host"""
|
||||||
logger("info", "Deploying to host", hostname)
|
self.logger("info", "Deploying to host", self.hostname)
|
||||||
init_deploy(hostname)
|
self.init_deploy()
|
||||||
global_explore(hostname)
|
self.global_explore()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
hostname=sys.argv[1]
|
hostname=sys.argv[1]
|
||||||
# logger("info", "cdist", cdist_version, ": Configuring host", hostname)
|
# logger("info", "cdist", cdist_version, ": Configuring host", hostname)
|
||||||
cdist_deploy_to(hostname)
|
|
||||||
print(list_global_explorers())
|
|
||||||
|
|
||||||
c = Cdist()
|
for host in sys.argv[1:]:
|
||||||
|
c = Cdist(host)
|
||||||
|
c.deploy_to()
|
||||||
|
print(c.list_global_explorers())
|
||||||
|
c.cleanup()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue