forked from Angatar/s3cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
57 lines (42 loc) · 1.68 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
aws_access_key = os.environ.get('ACCESS_KEY')
aws_secret_key = os.environ.get('SECRET_KEY')
aws_host = os.environ.get('HOST', default="s3.amazonaws.com")
aws_host_bucket = os.environ.get('HOST_BUCKET', default="%(bucket)s.s3.amazonaws.com")
cron_schedule_pull = os.environ.get('CRON_SCHEDULE_PULL', default="*/2 * * * *")
cron_schedule_push = os.environ.get('CRON_SCHEDULE_PUSH', default="*/5 * * * *")
s3_path = os.environ.get('S3_PATH')
local_path = os.environ.get('LOCAL_PATH')
# create dictionary of environment variables
cfg_dict = {}
cfg_dict['access_key'] = aws_access_key
cfg_dict['secret_key'] = aws_secret_key
cfg_dict['host_base'] = aws_host
cfg_dict['host_bucket'] = aws_host_bucket
env_dict = {}
env_dict['CRON_SCHEDULE_PULL'] = cron_schedule_pull
env_dict['CRON_SCHEDULE_PUSH'] = cron_schedule_push
env_dict['S3_PATH'] = s3_path
env_dict['LOCAL_PATH'] = local_path
# function to add environment variables to file
def add_env_variables(file, env_dict):
for key in env_dict:
file = file.replace(key, env_dict[key])
return file
# read file for crontabs
with open('/etc/crontabs/root', 'r') as in_file:
text = in_file.read()
# write file for crontabs
with open('/etc/crontabs/root', 'w') as out_file:
out_file.write(add_env_variables(text, env_dict))
# function to add config variables to file
def add_cfg_variables(file, cfg_dict):
for key in cfg_dict:
file = file.replace(f"{key} =", f"{key} = {cfg_dict[key]}")
return file
# read file for crontabs
with open('/root/.s3cfg', 'r') as in_file:
text = in_file.read()
# write file for crontabs
with open('/root/.s3cfg', 'w') as out_file:
out_file.write(add_cfg_variables(text, cfg_dict))