Skip to content

Commit

Permalink
Merge branch 'master' into CFE-3684
Browse files Browse the repository at this point in the history
  • Loading branch information
olehermanse authored Dec 19, 2024
2 parents 62eb52a + 26e2f8d commit 7a35d48
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
18 changes: 18 additions & 0 deletions cf_remote/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,3 +898,21 @@ def deploy(hubs, masterfiles):
os.system("rm -rf %s" % tarball)
os.system("tar -czf %s -C %s masterfiles" % (tarball, above))
return deploy_tarball(hubs, tarball)

def agent(hosts, bootstrap=None) :

command = "cf-agent"
if bootstrap :
command += " --bootstrap {}".format(bootstrap)

for host in hosts:
data = get_info(host)

if not data["agent_version"] :
user_error("CFEngine not installed on {}".format(host))

output = run_command(host, command, sudo=True)
if output :
print(output)

return 0
10 changes: 10 additions & 0 deletions cf_remote/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ def _get_arg_parser():
type=str,
nargs="?",
)
sp = subp.add_parser("agent", help="Run cf-agent")
sp.add_argument(
"--hosts", "-H", help="Which hosts to run cf-agent from", type=str, required=True
)
sp.add_argument("--bootstrap", "-B", help="Which hub to bootstrap to", type=str)

return ap

Expand All @@ -287,6 +292,9 @@ def run_command_with_args(command, args):
else:
trust_keys = None

if not args.bootstrap :
log.warning("You did not specify --bootstrap in the install command, so CFEngine has been installed, but not started.\nTo fix this, run:\ncf-remote agent --hosts HOSTS --bootstrap BOOTSTRAP")

return commands.install(
args.hub,
args.clients,
Expand Down Expand Up @@ -373,6 +381,8 @@ def run_command_with_args(command, args):
return commands.destroy(group_name)
elif command == "deploy":
return commands.deploy(args.hub, args.masterfiles)
elif command == "agent":
return commands.agent(args.hosts, args.bootstrap)
else:
user_error("Unknown command: '{}'".format(command))

Expand Down
9 changes: 9 additions & 0 deletions cf_remote/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import os
import shutil
import sys
Expand Down Expand Up @@ -204,6 +205,7 @@ def print_progress_dot(*args):
print(".", end="")
sys.stdout.flush() # STDOUT is line-buffered


def copy_file(input_path, output_path) :
filename = os.path.basename(input_path)
output_dir = os.path.dirname(output_path)
Expand All @@ -213,3 +215,10 @@ def copy_file(input_path, output_path) :

shutil.copyfile(input_path, tmp_output_path)
os.rename(tmp_output_path, output_path)


def is_different_checksum(checksum, content) :
assert type(content) == bytes

digest = hashlib.sha256(content).digest().hex()
return checksum != digest
19 changes: 11 additions & 8 deletions cf_remote/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import urllib.request
import json
from collections import OrderedDict
from cf_remote.utils import user_error, write_json, mkdir, parse_json
from cf_remote.utils import is_different_checksum, user_error, write_json, mkdir, parse_json
from cf_remote import log
from cf_remote.paths import cf_remote_dir, cf_remote_packages_dir

Expand Down Expand Up @@ -39,22 +39,25 @@ def download_package(url, path=None, checksum=None):

# Use "ab" to prevent truncation of the file in case it is already being
# downloaded by a different thread.
with open(path, "ab") as f:
with open(path, "ab+") as f:
# Get an exclusive lock. If the file size is != 0 then it's already
# downloaded, otherwise we download.
fcntl.flock(f.fileno(), fcntl.LOCK_EX)
st = os.stat(path)
if st.st_size != 0:
log.debug("Package '{}' already downloaded".format(path))
print("Package '{}' already downloaded".format(path))

f.seek(0)
content = f.read()
if checksum and is_different_checksum(checksum, content):
user_error("Downloaded file '{}' does not match expected checksum '{}'. Please delete the file.".format(filename, checksum))

else:
print("Downloading package: '{}'".format(path))

answer = urllib.request.urlopen(url).read()

if checksum:
digest = hashlib.sha256(answer).digest().hex()
if checksum != digest:
user_error("Downloaded file '{}' does not match expected checksum '{}'".format(filename, checksum))
if checksum and is_different_checksum(checksum, answer):
user_error("Downloaded file '{}' does not match expected checksum '{}'".format(filename, checksum))

f.write(answer)
f.flush()
Expand Down

0 comments on commit 7a35d48

Please sign in to comment.