wrap into a class

Signed-off-by: Nico Schottelius <nico@kr.ethz.ch>
This commit is contained in:
Nico Schottelius 2011-09-08 04:22:21 +02:00
parent 572401e4f8
commit e8a6d89e3c

187
bin/cdist
View file

@ -30,127 +30,136 @@ 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):
"""Ignore type for now, support later""" # FIXME: stopped - probably need static temp know!
print(*args) """Local directory containing output"""
return os.path.join(base_directory(), "conf")
def exit_error(*args): def logger(self,type, *args):
logger("error", *args) """Ignore type for now, support later"""
sys.exit(1) print(*args)
def run_or_fail(*args): def exit_error(self,*args):
# newargs = ["echo"] self.logger("error", *args)
newargs = [] sys.exit(1)
newargs.extend(*args)
print(newargs)
try: def run_or_fail(self,*args):
subprocess.check_call(newargs) # newargs = ["echo"]
except subprocess.CalledProcessError: newargs = []
exit_error("Command failed:", " ".join(newargs)) newargs.extend(*args)
print(newargs)
def remote_run_or_fail(hostname, *args): try:
"""Run something on the remote side and fail is something breaks""" subprocess.check_call(newargs)
newargs = ["ssh", "root@" + hostname] except subprocess.CalledProcessError:
newargs.extend(*args) self.exit_error("Command failed:", " ".join(newargs))
run_or_fail(newargs)
def remove_remote_dir(hostname, destination): def remote_run_or_fail(self, *args):
remote_run_or_fail(hostname, ["rm", "-rf", destination]) """Run something on the remote side and fail is something breaks"""
newargs = ["ssh", "root@" + self.hostname]
newargs.extend(*args)
print(newargs, "bbbbb", self.hostname)
self.run_or_fail(newargs)
def transfer_dir(hostname, source, destination): def remove_remote_dir(self, destination):
remove_remote_dir(hostname, destination) self.remote_run_or_fail(["rm", "-rf", destination])
run_or_fail(["scp", "-r", source, "root@" + hostname + ":" + destination])
def base_directory(): def transfer_dir(self, source, destination):
"""Returns the directory in which all cdist stuff is based in""" self.remove_remote_dir(destination)
print("Going to", __file__, os.path.join(os.path.dirname(__file__), os.pardir)) self.run_or_fail(["scp", "-qr", source, "root@" + self.hostname + ":" + destination])
os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
return os.getcwd()
def remote_base_directory(): def base_directory(self):
return "/var/lib/cdist" """Returns the directory in which all cdist stuff is based in"""
print("Going to", __file__, os.path.join(os.path.dirname(__file__), os.pardir))
os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
return os.getcwd()
def conf_directory(): def remote_base_directory(self):
"""Returns path to main configuration directory""" return "/var/lib/cdist"
return os.path.join(base_directory(), "conf")
def remote_conf_directory(): def conf_directory(self):
"""Returns path to remote main configuration directory""" """Returns path to main configuration directory"""
return os.path.join(remote_base_directory(), "conf") return os.path.join(self.base_directory(), "conf")
def out_dir(): def remote_conf_directory(self):
# FIXME: stopped - probably need static temp know! """Returns path to remote main configuration directory"""
"""Local directory containing output""" return os.path.join(self.remote_base_directory(), "conf")
return os.path.join(base_directory(), "conf")
def global_explorer_directory(): def global_explorer_directory(self):
"""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"])
# #
# remote_run_or_fail(hostname, ["rm -rf", "${__cdist_remote_base_dir}"]) # remote_run_or_fail(hostname, ["rm -rf", "${__cdist_remote_base_dir}"])
# #
# # Create base directories # # Create base directories
# run_or_fail(["mkdir -p", "$__cdist_local_base_dir"]) # run_or_fail(["mkdir -p", "$__cdist_local_base_dir"])
# remote_run_or_fail(hostname,["mkdir -p", "${__cdist_remote_base_dir}"]) # remote_run_or_fail(hostname,["mkdir -p", "${__cdist_remote_base_dir}"])
# #
# # 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()