def install_def_json(host, *, connection=None, call_collect=False): print("Transferring def.json to hub: '{}'".format(host)) data = json.dumps(def_json(call_collect), indent=2) path = os.path.join(cf_remote_dir("json"), "def.json") save_file(path, data) scp(path, host, connection=connection) ssh_sudo(connection, "cp def.json /var/cfengine/masterfiles/def.json")
def agent_run(host, *, connection=None): print("Triggering an agent run on: '{}'".format(host)) command = "/var/cfengine/bin/cf-agent -Kf update.cf" output = ssh_sudo(connection, command) log.debug(output) command = "/var/cfengine/bin/cf-agent -K" output = ssh_sudo(connection, command) log.debug(output)
def install_package(host, pkg, data, *, connection=None): print("Installing: '{}' on '{}'".format(pkg, host)) if ".deb" in pkg: output = ssh_sudo(connection, "dpkg -i {}".format(pkg)) else: output = ssh_sudo(connection, "rpm -i {}".format(pkg)) if output is None: sys.exit("Installation failed on '{}'".format(host))
def install_package(host, pkg, data, *, connection=None): print("Installing: '{}' on '{}'".format(pkg, host)) if ".deb" in pkg: output = ssh_sudo(connection, "dpkg -i {}".format(pkg), True) elif ".msi" in pkg: # Windows is crazy, be careful if you decide to change this; # This needs to work in both powershell and cmd, and in # Windows 2012 Server, 2016, and so on... # sleep is powershell specific, # timeout doesn't work over ssh. output = ssh_cmd(connection, powershell(r'.\{} ; sleep 10'.format(pkg)), True) else: output = ssh_sudo(connection, "rpm -i {}".format(pkg), True) if output is None: sys.exit("Installation failed on '{}'".format(host))
def bootstrap_host(host, policy_server, *, connection=None): print("Bootstrapping: '{}' -> '{}'".format(host, policy_server)) command = "/var/cfengine/bin/cf-agent --bootstrap {}".format(policy_server) output = ssh_sudo(connection, command) if output is None: sys.exit("Bootstrap failed on '{}'".format(host)) if output and "completed successfully" in output: print("Bootstrap successful: '{}' -> '{}'".format(host, policy_server)) else: user_error("Something went wrong while bootstrapping")
def deploy_masterfiles(host, tarball, *, connection=None): data = get_info(host, connection=connection) print_info(data) if not data["agent_version"]: log.error( f"Cannot deploy masterfiles on {host} - CFEngine not installed") return 1 scp(tarball, host, connection=connection) ssh_cmd(connection, f"tar -xzf masterfiles.tgz") commands = [ "systemctl stop cfengine3", "rm -rf /var/cfengine/masterfiles", "mv masterfiles /var/cfengine/masterfiles", "systemctl start cfengine3", "cf-agent -Kf update.cf", "cf-agent -K", ] combined = " && ".join(commands) print(f"Running: '{combined}'") ssh_sudo(connection, combined) return 0
def bootstrap_host(host_data, policy_server, *, connection=None): host = host_data["ssh_host"] agent = host_data["agent"] print("Bootstrapping: '{}' -> '{}'".format(host, policy_server)) command = "{} --bootstrap {}".format(agent, policy_server) if host_data["os"] == "windows": output = ssh_cmd(connection, command) else: output = ssh_sudo(connection, command) if output is None: sys.exit("Bootstrap failed on '{}'".format(host)) if output and "completed successfully" in output: print("Bootstrap successful: '{}' -> '{}'".format(host, policy_server)) else: user_error("Something went wrong while bootstrapping")
def bootstrap_host(host_data, policy_server, *, connection=None, trust_server=True): host = host_data["ssh_host"] agent = host_data["agent"] print("Bootstrapping: '{}' -> '{}'".format(host, policy_server)) command = "{} --bootstrap {}".format(agent, policy_server) if not trust_server: command += " --trust-server=no" if host_data["os"] == "windows": output = ssh_cmd(connection, powershell(command)) else: output = ssh_sudo(connection, command) if output is None: sys.exit("Bootstrap failed on '{}'".format(host)) if output and "completed successfully" in output: print("Bootstrap successful: '{}' -> '{}'".format(host, policy_server)) return True else: log.error("Something went wrong while bootstrapping") return False
def run_command(host, command, *, users=None, connection=None, sudo=False): if sudo: return ssh_sudo(connection, command, errors=True) return ssh_cmd(connection, command, errors=True)