Newer
Older
Kevin Di Lallo
committed
#!/usr/bin/python
import sys
import yaml
import subprocess
def usage():
  print('''
    NAME
        configure-secrets - Create or delete platform deployment secrets from FILE
    SYNOPSIS
        configure-secrets <ACTION> FILE
    ACTION
        -s, set     Set secrets
        -d, del     Delete secrets
    FILE
        File containing secrets to be provisioned/removed
  ''')
# Parse secrets file
def parse(fname):
Kevin Di Lallo
committed
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
78
79
80
81
82
83
84
85
86
87
88
  with open(fname, 'r') as stream:
    secrets = {}
    try:
      secrets = yaml.safe_load(stream)
    except yaml.YAMLError as exc:
      print(exc)
      print('ERROR: failed to parse yaml file')
      exit(1)
    return secrets
# Add provided secrets 
def add(secrets):
  print('\n>>> Setting secrets')
  if not bool(secrets):
    print('no secrets to add')
    return
  for secret, fields in secrets.items():
    if not bool(fields):
      print('skipping secret with no fields: ' + secret)
      continue
    entries = ''
    for field, value in fields.items():
      entries += ' --from-literal=' + field + '=' + value
    subprocess.call('kubectl create secret generic ' + secret + entries, shell=True)
# Remove provided secrets
def remove(secrets):
  print('\n>>> Removing secrets')
  if not bool(secrets):
    print('no secrets to remove')
    return
  for secret, fields in secrets.items():
    subprocess.call('kubectl delete secret ' + secret, shell=True)
# Parse arguments
argCount = len(sys.argv)
if argCount != 3:
  print('ERROR: invalid number of args')
  usage()
  sys.exit(1)
action = sys.argv[1]
fname = sys.argv[2]
# Run command
if (action == '-s' or action == 'set'):
  secrets = parse(fname)
  remove(secrets)
  add(secrets)
elif (action == '-d' or action == 'del'):
  secrets = parse(fname)
  remove(secrets)
else:
  print('ERROR: invalid action')
  usage()
  sys.exit(1)
print('')