2011-09-07 23:01:25 +00:00
|
|
|
#!/usr/bin/env python3
|
2011-09-07 22:41:37 +00:00
|
|
|
#
|
|
|
|
# 2010-2011 Nico Schottelius (nico-cdist at schottelius.org)
|
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cdist is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2011-09-07 23:01:25 +00:00
|
|
|
import sys # argv
|
2011-09-07 23:21:15 +00:00
|
|
|
import subprocess # execute stuff
|
2011-09-08 00:12:48 +00:00
|
|
|
import os
|
2011-09-08 01:56:59 +00:00
|
|
|
import tempfile
|
|
|
|
import shutil
|
2011-09-07 22:41:37 +00:00
|
|
|
|
2011-09-08 01:56:59 +00:00
|
|
|
|
|
|
|
class Cdist:
|
|
|
|
"""Cdist main class to hold arbitrary data"""
|
|
|
|
version="2.0.0"
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.tempdir = tempfile.mkdtemp()
|
|
|
|
print(self.tempdir)
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
print("Zerstoeren")
|
|
|
|
print(self.tempdir)
|
|
|
|
shutil.rmtree(self.tempdir)
|
2011-09-07 22:41:37 +00:00
|
|
|
|
2011-09-07 23:21:15 +00:00
|
|
|
def logger(type, *args):
|
2011-09-07 23:01:25 +00:00
|
|
|
"""Ignore type for now, support later"""
|
|
|
|
print(*args)
|
2011-09-07 22:41:37 +00:00
|
|
|
|
2011-09-07 23:21:15 +00:00
|
|
|
def exit_error(*args):
|
2011-09-08 01:28:06 +00:00
|
|
|
logger("error", *args)
|
2011-09-07 23:21:15 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def run_or_fail(*args):
|
2011-09-08 01:23:18 +00:00
|
|
|
# newargs = ["echo"]
|
2011-09-08 00:46:09 +00:00
|
|
|
newargs = []
|
2011-09-07 23:36:22 +00:00
|
|
|
newargs.extend(*args)
|
2011-09-08 01:23:18 +00:00
|
|
|
print(newargs)
|
2011-09-07 23:36:22 +00:00
|
|
|
|
2011-09-07 23:21:15 +00:00
|
|
|
try:
|
2011-09-07 23:36:22 +00:00
|
|
|
subprocess.check_call(newargs)
|
2011-09-08 01:28:06 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
exit_error("Command failed:", " ".join(newargs))
|
2011-09-07 23:36:22 +00:00
|
|
|
|
|
|
|
def remote_run_or_fail(hostname, *args):
|
2011-09-08 01:23:18 +00:00
|
|
|
"""Run something on the remote side and fail is something breaks"""
|
2011-09-08 00:57:47 +00:00
|
|
|
newargs = ["ssh", "root@" + hostname]
|
2011-09-07 23:36:22 +00:00
|
|
|
newargs.extend(*args)
|
|
|
|
run_or_fail(newargs)
|
2011-09-07 23:21:15 +00:00
|
|
|
|
2011-09-08 00:51:45 +00:00
|
|
|
def remove_remote_dir(hostname, destination):
|
|
|
|
remote_run_or_fail(hostname, ["rm", "-rf", destination])
|
|
|
|
|
2011-09-08 00:46:09 +00:00
|
|
|
def transfer_dir(hostname, source, destination):
|
2011-09-08 00:51:45 +00:00
|
|
|
remove_remote_dir(hostname, destination)
|
2011-09-08 00:57:47 +00:00
|
|
|
run_or_fail(["scp", "-r", source, "root@" + hostname + ":" + destination])
|
2011-09-07 23:21:15 +00:00
|
|
|
|
2011-09-08 00:12:48 +00:00
|
|
|
def base_directory():
|
|
|
|
"""Returns the directory in which all cdist stuff is based in"""
|
2011-09-08 00:39:24 +00:00
|
|
|
print("Going to", __file__, os.path.join(os.path.dirname(__file__), os.pardir))
|
2011-09-08 00:12:48 +00:00
|
|
|
os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
|
|
|
|
return os.getcwd()
|
|
|
|
|
2011-09-08 00:56:02 +00:00
|
|
|
def remote_base_directory():
|
|
|
|
return "/var/lib/cdist"
|
|
|
|
|
2011-09-08 00:16:13 +00:00
|
|
|
def conf_directory():
|
|
|
|
"""Returns path to main configuration directory"""
|
|
|
|
return os.path.join(base_directory(), "conf")
|
|
|
|
|
2011-09-08 00:56:02 +00:00
|
|
|
def remote_conf_directory():
|
|
|
|
"""Returns path to remote main configuration directory"""
|
|
|
|
return os.path.join(remote_base_directory(), "conf")
|
|
|
|
|
2011-09-08 01:31:58 +00:00
|
|
|
def out_dir():
|
2011-09-08 01:56:59 +00:00
|
|
|
# FIXME: stopped - probably need static temp know!
|
2011-09-08 01:31:58 +00:00
|
|
|
"""Local directory containing output"""
|
|
|
|
return os.path.join(base_directory(), "conf")
|
|
|
|
|
2011-09-08 00:12:48 +00:00
|
|
|
def global_explorer_directory():
|
|
|
|
"""Returns path to directory containing the global explorers"""
|
2011-09-08 00:16:13 +00:00
|
|
|
return os.path.join(conf_directory(), "explorer")
|
2011-09-08 00:12:48 +00:00
|
|
|
|
2011-09-08 00:46:09 +00:00
|
|
|
def remote_global_explorer_directory():
|
2011-09-08 00:56:02 +00:00
|
|
|
"""Returns path to the remote directory containing the global explorers"""
|
|
|
|
return os.path.join(remote_conf_directory(), "explorer")
|
2011-09-08 00:46:09 +00:00
|
|
|
|
2011-09-08 01:23:18 +00:00
|
|
|
def remote_global_explorer_path(explorer):
|
|
|
|
"""Returns path to the remote explorer"""
|
|
|
|
return os.path.join(remote_global_explorer_directory(), explorer)
|
|
|
|
|
2011-09-08 00:12:48 +00:00
|
|
|
def list_global_explorers():
|
|
|
|
"""Return list of available explorers"""
|
2011-09-08 00:39:24 +00:00
|
|
|
return os.listdir(global_explorer_directory())
|
2011-09-08 00:12:48 +00:00
|
|
|
|
2011-09-08 00:46:09 +00:00
|
|
|
def transfer_global_explorers(hostname):
|
|
|
|
transfer_dir(hostname, global_explorer_directory(), remote_global_explorer_directory())
|
|
|
|
|
2011-09-08 00:39:24 +00:00
|
|
|
def global_explore(hostname):
|
|
|
|
"""Run global explorers"""
|
2011-09-08 01:23:18 +00:00
|
|
|
explorers = list_global_explorers()
|
|
|
|
if(len(explorers) == 0):
|
2011-09-08 00:39:24 +00:00
|
|
|
exit_error("No explorers found in", global_explorer_directory())
|
2011-09-07 23:21:15 +00:00
|
|
|
|
2011-09-08 00:57:47 +00:00
|
|
|
transfer_global_explorers(hostname)
|
2011-09-08 01:23:18 +00:00
|
|
|
for explorer in explorers:
|
|
|
|
remote_run_or_fail(hostname, [remote_global_explorer_path(explorer)])
|
2011-09-08 01:24:27 +00:00
|
|
|
|
2011-09-08 00:57:47 +00:00
|
|
|
|
2011-09-07 23:36:22 +00:00
|
|
|
def init_deploy(hostname):
|
2011-09-07 23:21:15 +00:00
|
|
|
logger("info", "Creating clean directory structure")
|
|
|
|
|
|
|
|
# Ensure there is no old stuff, neither local nor remote
|
2011-09-08 00:52:38 +00:00
|
|
|
# run_or_fail(["rm -rf", "$__cdist_local_base_dir"])
|
|
|
|
#
|
|
|
|
# remote_run_or_fail(hostname, ["rm -rf", "${__cdist_remote_base_dir}"])
|
|
|
|
#
|
|
|
|
# # Create base directories
|
|
|
|
# run_or_fail(["mkdir -p", "$__cdist_local_base_dir"])
|
|
|
|
# remote_run_or_fail(hostname,["mkdir -p", "${__cdist_remote_base_dir}"])
|
|
|
|
#
|
|
|
|
# # Link configuraion source directory - consistent with remote
|
|
|
|
# run_or_fail(["ln -sf", "$__cdist_conf_dir", "$__cdist_local_base_dir/$__cdist_name_conf_dir"])
|
2011-09-07 22:41:37 +00:00
|
|
|
|
2011-09-08 00:39:24 +00:00
|
|
|
def cdist_deploy_to(hostname):
|
|
|
|
"""Mimic the old deploy to: Deploy to one host"""
|
|
|
|
logger("info", "Deploying to host", hostname)
|
|
|
|
init_deploy(hostname)
|
|
|
|
global_explore(hostname)
|
|
|
|
|
2011-09-07 22:41:37 +00:00
|
|
|
|
2011-09-07 23:01:25 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
hostname=sys.argv[1]
|
2011-09-08 01:56:59 +00:00
|
|
|
# logger("info", "cdist", cdist_version, ": Configuring host", hostname)
|
2011-09-07 23:01:25 +00:00
|
|
|
cdist_deploy_to(hostname)
|
2011-09-08 00:39:24 +00:00
|
|
|
print(list_global_explorers())
|
2011-09-07 22:41:37 +00:00
|
|
|
|
2011-09-08 01:56:59 +00:00
|
|
|
c = Cdist()
|
|
|
|
|