Skip to content

Commit

Permalink
okdinst: Rework on top of openshift/installer#861
Browse files Browse the repository at this point in the history
  • Loading branch information
cgwalters committed Dec 13, 2018
1 parent bdab69f commit f7b9302
Showing 1 changed file with 67 additions and 19 deletions.
86 changes: 67 additions & 19 deletions bin/okdinst
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,87 @@ import os,sys,json,yaml,shutil,argparse,subprocess,re,collections
import tempfile,hashlib,gzip

CONFIGDIR = os.path.expanduser('~/.config/openshift-install')
BASECONFIG = os.path.join(CONFIGDIR, "config.yaml")
CLUSTERSDIR = os.path.join(CONFIGDIR, "clusters")

def get_installer_config(basedir):
with open(os.path.join(basedir, 'config.json')) as f:
return json.load(f)

def installer_exec(args, *instargs):
os.makedirs(CLUSTERSDIR)
with open(os.path.join(CONFIGDIR, 'config.yaml')) as f:
installer_env = yaml.load(f)
installer_env['CLUSTER_NAME'] = args.name
installer_env['PULL_SECRET_PATH'] = os.path.join(CONFIGDIR, 'pull-secret.json')
installer_env['SSH_PUB_KEY_PATH'] = os.path.join(CONFIGDIR, 'ssh-pubkey')
env = dict(os.environ)
# https://github.com/openshift/installer/pull/785
env['TF_VAR_tectonic_libvirt_memory'] = '8192'
for (k,v) in installer_env.items():
env['OPENSHIFT_INSTALL_' + k] = v
def mkdir_p(path):
if not os.path.isdir(path):
os.makedirs(path)

def rm_rf(path):
if os.path.exists(path):
shutil.rmtree(path)

def ensure_empty_dir(path):
rm_rf(path)
mkdir_p(path)

def run_installer(*args):
local_binpath = os.path.join(os.getcwd(), "bin/openshift-install")
if os.path.exists(local_binpath):
binpath = local_binpath
else:
binpath = instargs[0]
instargs = [binpath] + list(instargs)
subprocess.check_call(instargs, env=env)
binpath = 'openshift-install'
env = dict(os.environ)
# https://github.com/openshift/installer/pull/785
env['TF_VAR_tectonic_libvirt_memory'] = '8192'
subprocess.check_call([binpath] + list(args), env=env)

def ensure_base_config():
if os.path.isfile(BASECONFIG):
return
print("Generating base configuration")
tmpdir = os.path.join(CONFIGDIR, "tmp")
ensure_empty_dir(tmpdir)
run_installer('create', 'install-config', f'--dir={tmpdir}')
os.rename(os.path.join(tmpdir, "install-config.yml"), BASECONFIG)
print(f"Generated base config: {BASECONFIG}")

def get_cluster_state(clusterdir):
state_path = clusterdir + '/okdinst-state.json'
if not os.path.exists(state_path):
return None
with open(state_path) as f:
return json.load(f)['state']

def write_cluster_state(clusterdir, state):
state_path = clusterdir + '/okdinst-state.json'
with open(state_path + '.tmp', 'w') as f:
json.dump({ "state": state }, f)
os.rename(state_path + '.tmp', state_path)

def cmd_create(args):
clusterdir = os.path.join(CLUSTERSDIR, args.name)
installer_exec(args, "create", "cluster", "--dir", clusterdir)
ensure_base_config()
name = args.name
clusterdir = os.path.join(CLUSTERSDIR, name)
mkdir_p(clusterdir)
state = get_cluster_state(clusterdir)
if state == 'in-destruction':
sys.exit(f"Cluster {name} was queued for destruction; rerun `destroy` first")
target_config = os.path.join(clusterdir, 'install-config.yml')
if not os.path.exists(target_config):
shutil.copy(BASECONFIG, target_config)
write_cluster_state(clusterdir, 'creating')
else:
print(f"Resuming installation of cluster {name}")
write_cluster_state(clusterdir, 'resuming')
run_installer("create", "cluster", "--dir", clusterdir)
write_cluster_state(clusterdir, 'created')

def cmd_destroy(args):
clusterdir = os.path.join(CLUSTERSDIR, args.name)
installer_exec(args, "destroy", "cluster", "--dir", clusterdir)
name = args.name
clusterdir = os.path.join(CLUSTERSDIR, name)
state = get_cluster_state()
if state is None:
sys.exit(f"No such cluster {name}")
else:
print(f"Destroying cluster '{name}' in state {state}")
write_cluster_state('in-destruction')
run_installer("destroy", "cluster", "--dir", clusterdir)
shutil.rmtree(clusterdir)

# Parse args and dispatch
Expand Down

0 comments on commit f7b9302

Please sign in to comment.