-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmakesecrets
executable file
·77 lines (65 loc) · 2.41 KB
/
makesecrets
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#! /usr/bin/python
#
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# usage:
# makesecrets [VALUES_YML]
#
# Copies example-secrets.env to container/secrets/secrets.env.
#
# Optionally, VALUES_YML can be a YAML file containing values for some or all of
# the variables in example-secrets.env; for these variables, the generated file
# will contain the value from VALUES_YML, rather than the original value from
# example-secrets.env. Any variables not mentioned in VALUES_YML will be copied
# unmodified. Any variables in VALUES_YML which do not occur in
# example-secrets.env will be ignored; they will not appear in the generated
# file.
import yaml
import re
import sys
import os
values_yml = None
if len(sys.argv) > 2:
print("usage: makesecrets [VALUES_YML]")
sys.exit(-1)
if len(sys.argv) > 1:
values_yml = sys.argv[1]
if values_yml != None:
if not os.path.exists(values_yml):
print("%s not found" % values_yml)
sys.exit(-1)
if os.path.exists("container/secrets/secrets.env"):
print("Cowardly refusing to overwrite existing file container/secrets/secrets.env.")
print("If you really want to overwrite that file, delete it first.")
sys.exit(-1)
if not os.path.exists("example-secrets.env"):
print("File not found: example-secrets.env")
sys.exit(-1)
data = {}
if values_yml != None:
with open(values_yml, "r") as f:
data = yaml.load(f, Loader=yaml.FullLoader)
if values_yml != None:
print("Subsituting values from %s" % values_yml)
with open("container/secrets/secrets.env", "w") as fout:
with open("example-secrets.env", "r") as fin:
for line in fin.readlines():
line = line.rstrip()
m = re.match(r'^export ([^=]+)=(.*)$', line)
if m and m.group(1) in data:
key = m.group(1)
fout.write("export %s=\"%s\"\n" % (key, re.sub(r'"', '\\"', data[key])))
else:
fout.write("%s\n" % line)
print("Wrote ./container/secrets/secrets.env")