introduce working dir

This commit is contained in:
jem 2019-11-08 20:10:51 +01:00
parent cb7ebcb773
commit b995887764
4 changed files with 96 additions and 48 deletions

View file

@ -4,8 +4,11 @@ terraform, dda-pallet, aws & hetzner-cloud.
""" """
from .meissa_build import meissa_init_project, stage, module, hetzner_api_key, tf_import_name, tf_import_resource, project_dict from .meissa_build import meissa_init_project, stage, module, hetzner_api_key, \
from .dda_pallet import dda_write_target, dda_write_domain, dda_uberjar project_root_path, build_commons_path, build_target_path, initialize_target, \
from .terraform import tf_copy_common, tf_plan, tf_import, tf_apply, tf_output, tf_destroy, tf_read_output_json tf_import_name, tf_import_resource
from .dda_pallet import dda_write_target, dda_write_domain, dda_install, dda_configure
from .terraform import tf_copy_common, tf_plan, tf_import, tf_apply, tf_output, tf_destroy, \
tf_read_output_json
__version__ = "${version}" __version__ = "${version}"

View file

@ -1,28 +1,44 @@
from .python_util import * from .python_util import *
import string from .meissa_build import build_target_path, project_root_path
from string import Template
TARGET = 'target.edn' TARGET = 'target.edn'
TEMPLATE_TARGET_CONTENT = string.Template(""" TEMPLATE_TARGET_CONTENT = Template("""
{:existing [{:node-name "k8s" {:existing [{:node-name "k8s"
:node-ip "$ipv4"}] :node-ip "$ipv4"}]
:provisioning-user {:login "root"}} :provisioning-user {:login "root"}}
""") """)
def dda_write_target(ipv4): def dda_write_target(project, ipv4):
with open(TARGET, "w") as output_file: with open(build_target_path(project) + TARGET, "w") as output_file:
output_file.write(TEMPLATE_TARGET_CONTENT.substitute({'ipv4' : ipv4})) output_file.write(TEMPLATE_TARGET_CONTENT.substitute({'ipv4' : ipv4}))
def dda_write_domain(domain_path, substitues): def dda_write_domain(project, domain_file_name, substitues):
with open(domain_path, "r") as input_file: with open(build_target_path(project) + domain_file_name, "r") as input_file:
domain_input = input_file.read() domain_input = input_file.read()
domain_template = string.Template(domain_input) domain_template = Template(domain_input)
with open('out_' + domain_path, "w") as output_file: with open(build_target_path(project) + 'out_' + domain_file_name, "w") as output_file:
output_file.write(domain_template.substitute(substitues)) output_file.write(domain_template.substitute(substitues))
def dda_uberjar(tenant, application, spec): def dda_install(project, tenant, application, domain_file_name):
cmd = ['java', '-jar', '../../../target/meissa-tenant-server.jar', '--targets', TARGET, return dda_uberjar(project, tenant, application, domain_file_name)
'--tenant', tenant, '--application', application, spec]
def dda_configure(project, tenant, application, domain_file_name):
return dda_uberjar(project, tenant, application, domain_file_name, True)
def dda_uberjar(project, tenant, application, domain_file_name, configure_switch=None):
if configure_switch:
cmd = ['java', '-jar', project_root_path(project) + 'target/meissa-tenant-server.jar', \
'--targets', build_target_path(project) + TARGET, \
'--tenant', tenant, '--application', application, \
'--configure', \
build_target_path(project) + 'out_' + domain_file_name]
else:
cmd = ['java', '-jar', project_root_path(project) + 'target/meissa-tenant-server.jar', \
'--targets', build_target_path(project) + TARGET, \
'--tenant', tenant, '--application', application, \
build_target_path(project) + 'out_' + domain_file_name]
prn_cmd=list(cmd) prn_cmd=list(cmd)
print(" ".join(prn_cmd)) print(" ".join(prn_cmd))
output = execute(cmd) output = execute(cmd)

View file

@ -1,35 +1,50 @@
import os import os
from .credential import gopass_credential_from_env_path from .credential import gopass_credential_from_env_path
from subprocess import run
def meissa_init_project(project): STAGE = 'stage'
project.set_property('stage', os.environ.get('STAGE', 'intergation')) HTTP_NET_API_KEY = 'http_net_api_key'
project.set_property('http_net_api_key', HETZNER_API_KEY = 'hetzner_api_key'
PROJECT_ROOT_PATH = 'project_root_path'
BUILD_COMMONS_PATH = 'build_commons_path'
def meissa_init_project(project, project_root_path, build_commons_path):
project.set_property(STAGE, os.environ.get('STAGE', 'intergation'))
project.set_property(PROJECT_ROOT_PATH, project_root_path)
project.set_property(BUILD_COMMONS_PATH, build_commons_path)
project.set_property(HTTP_NET_API_KEY, \
gopass_credential_from_env_path('HTTP_NET_API_KEY_PATH')) gopass_credential_from_env_path('HTTP_NET_API_KEY_PATH'))
project.set_property('hetzner_api_key', project.set_property(HETZNER_API_KEY, \
gopass_credential_from_env_path('HETZNER_API_KEY_PATH')) gopass_credential_from_env_path('HETZNER_API_KEY_PATH'))
return project return project
def hetzner_api_key(project): def hetzner_api_key(project):
return project.get_property('hetzner_api_key') return project.get_property(HETZNER_API_KEY)
def stage(project): def stage(project):
return project.get_property('stage') return project.get_property(STAGE)
def module(project): def module(project):
return project.get_property('name') return project.get_property('name')
def project_root_path(project):
return project.get_property(PROJECT_ROOT_PATH)
def build_commons_path(project):
return project.get_property(BUILD_COMMONS_PATH)
def build_target_path(project):
return project_root_path(project) + 'target/' + project.name + '/'
def tf_import_name(project): def tf_import_name(project):
return project.get_property('tf_import_name') return project.get_property('tf_import_name')
def tf_import_resource(project): def tf_import_resource(project):
return project.get_property('tf_import_resource') return project.get_property('tf_import_resource')
def project_dict(project): def initialize_target(project):
my_hetzner_api_key = hetzner_api_key(project) run('rm -rf ' + build_target_path(project), shell=True)
my_module = project.name run('mkdir -p ' + build_target_path(project), shell=True)
ret = {'stage' : stage(project)} run('cp *.tf ' + build_target_path(project), shell=True)
if my_hetzner_api_key: run('cp *.tfars ' + build_target_path(project), shell=True)
ret['hetzner_api_key'] = my_hetzner_api_key run('cp *.edn ' + build_target_path(project), shell=True)
if my_module:
ret['module'] = my_module
return ret

View file

@ -1,50 +1,64 @@
from os import path from os import path
from json import load from json import load
from subprocess import call from subprocess import run
from .meissa_build import stage, project_dict, tf_import_name, tf_import_resource from .meissa_build import stage, hetzner_api_key, tf_import_name, tf_import_resource, \
build_commons_path, build_target_path
from .python_util import execute from .python_util import execute
from python_terraform import * from python_terraform import *
OUTPUT_JSON = "output.json" OUTPUT_JSON = "output.json"
def tf_copy_common(base_path): def tf_copy_common(project):
call(['cp', '-f', base_path + '00_build_common/terraform/gitignore_on_target', '.gitignore']) run(['cp', '-f', build_commons_path(project) + 'terraform/aws_provider.tf', \
call(['cp', '-f', base_path + '00_build_common/terraform/aws_provider.tf', 'aws_provider.tf']) build_target_path(project) + 'aws_provider.tf'])
call(['cp', '-f', base_path + '00_build_common/terraform/variables.tf', 'variables.tf']) run(['cp', '-f', build_commons_path(project) + 'terraform/variables.tf', \
build_target_path(project) + 'variables.tf'])
def tf_plan(project): def tf_plan(project):
init(project) init(project)
tf = Terraform(working_dir='.') tf = Terraform(working_dir=build_target_path(project))
tf.plan(capture_output=False, var=project_dict(project)) tf.plan(capture_output=False, var=project_vars(project))
def tf_import(project): def tf_import(project):
init(project) init(project)
tf = Terraform(working_dir='.') tf = Terraform(working_dir=build_target_path(project))
tf.import_cmd(tf_import_name(project), tf_import_resource(project), \ tf.import_cmd(tf_import_name(project), tf_import_resource(project), \
capture_output=False, var=project_dict(project)) capture_output=False, var=project_vars(project))
def tf_apply(project, p_auto_approve=False): def tf_apply(project, p_auto_approve=False):
init(project) init(project)
tf = Terraform(working_dir='.') tf = Terraform(working_dir=build_target_path(project))
tf.apply(capture_output=False, auto_approve=p_auto_approve, var=project_dict(project)) tf.apply(capture_output=False, auto_approve=p_auto_approve, var=project_vars(project))
tf_output(project) tf_output(project)
def tf_output(project): def tf_output(project):
tf = Terraform(working_dir='.') init(project)
tf = Terraform(working_dir=build_target_path(project))
result = tf.output(json=IsFlagged) result = tf.output(json=IsFlagged)
with open(OUTPUT_JSON, "w") as output_file: with open(build_target_path(project) + OUTPUT_JSON, "w") as output_file:
output_file.write(json.dumps(result)) output_file.write(json.dumps(result))
def tf_destroy(project, p_auto_approve=False): def tf_destroy(project, p_auto_approve=False):
tf = Terraform(working_dir='.') init(project)
tf.destroy(capture_output=False, auto_approve=p_auto_approve, var=project_dict(project)) tf = Terraform(working_dir=build_target_path(project))
tf.destroy(capture_output=False, auto_approve=p_auto_approve, var=project_vars(project))
def tf_read_output_json(): def tf_read_output_json(project):
with open(OUTPUT_JSON, 'r') as f: with open(build_target_path(project) + OUTPUT_JSON, 'r') as f:
return load(f) return load(f)
def project_vars(project):
my_hetzner_api_key = hetzner_api_key(project)
my_module = project.name
ret = {'stage' : stage(project)}
if my_hetzner_api_key:
ret['hetzner_api_key'] = my_hetzner_api_key
if my_module:
ret['module'] = my_module
return ret
def init(project): def init(project):
tf = Terraform(working_dir='.') tf = Terraform(working_dir=build_target_path(project))
tf.init() tf.init()
try: try:
tf.workspace('select', stage(project)) tf.workspace('select', stage(project))