Skip to content

Commit

Permalink
working on deployment on Kubernetes
Browse files Browse the repository at this point in the history
  • Loading branch information
wxw-matt committed Feb 26, 2022
1 parent 8fc2072 commit 1cd247a
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/args_helper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class GlobalArgs(object):
class CustomArgs(object):
pass

_args = GlobalArgs()
_args = CustomArgs()

def get_global_args():
global _args
Expand Down
2 changes: 2 additions & 0 deletions lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ def write_rod(rails_base_tag, tag, release_tag, service,project_dir):
if 'image' not in config:
config['image'] = {}
config['service'] = {}
config['rails'] = {}
with open(fn, 'w+') as configfile:
# tag is used by docker, service name `web` is for docker-compose
config['service']['web'] = service
config['image']['tag'] = tag
config['image']['release_tag'] = release_tag
config['image']['base'] = rails_base_tag
config['rails']['env'] = 'development'
config.write(configfile)


Expand Down
6 changes: 6 additions & 0 deletions lib/rails_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def project_handler(args):

def create_files_for_the_project(rails_base_tag, database, project_dir):
app_name = args_helper.get_global_args().name
tag, release_tag = config.get_project_tags()
with open(f'{project_dir}/Dockerfile', 'w+') as f:
f.write(template.dockerfile_template(rails_base_tag))

Expand Down Expand Up @@ -105,6 +106,11 @@ def create_files_for_the_project(rails_base_tag, database, project_dir):
else:
f.write(template.dc_rails_sqlite3_template(None, **kwargs))

with open('k8s-deployment.yml','w+') as f:
name = os.path.basename(project_dir)
yaml_text = template.k8s_deployment_template(release_tag, app_name=name, replicas=2)
f.write(yaml_text)

rod_path = path.join(project_dir, 'rod')
if not path.exists(rod_path):
os.symlink(path.abspath(sys.argv[0]), rod_path)
Expand Down
6 changes: 6 additions & 0 deletions lib/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def dc_mariadb_config(db_host, app_name, dev_password='example'):
template = get_env().get_template('mariadb-config-template.yml')
return template.render(db_host=db_host, app_name=app_name, dev_password=dev_password)

# replicas, app_name, created_at
def k8s_deployment_template(image_tag, **kwargs):
kwargs['image_tag'] = image_tag
template = get_env().get_template('k8s-deployment-template.yml')
return template.render(**kwargs)

def dockerfile_pro_template(rails_base_tag, **kwargs):
kwargs['rails_base_tag'] = rails_base_tag
template = get_env().get_template('Dockerfile-pro-template')
Expand Down
16 changes: 15 additions & 1 deletion rod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ from lib.rails_cmds import rails_base_cmd, rake_base_cmd, rails_project_command,
from lib.rails_cmds import generate_controller_handler, generate_model_handler, generate_scaffold_handler, project_handler, new_project_handler, rails_command_handler, rails_command_server_handler, rails_command_console_handler
from lib.docker_cmds import docker_compose_run_cmd, docker_compose_exec_cmd, docker_base_cmd, docker_project_base_cmd, docker_compose_up_cmd, build_image_cmd

from lib import k8s
NAME="rod"

def signal_handler(sig, frame):
Expand Down Expand Up @@ -69,11 +70,24 @@ def deploy_commands_handler(args):
pass

def deploy_k8s_handler(args):
args_helper.set_production()
project_dir = os.getcwd()
tag, release_tag = config.get_project_tags()
# 1. Build pro image
# 2. Rails project will use a separate database, such as RDS,
# so that it will have the configuration in `production` section
# 3. Run db:create and db:migrate to initialise the database
# 4. Generate k8s configuration files: deployment and service files
# 5. Perform the deployment
cmd = docker_cmds.build_image_cmd(release_tag, project_dir, dockerfile='Dockerfile-pro')
if run_cmd(cmd).returncode == 0:
pass
print(f'{release_tag} built successfully')
args = args_helper.CustomArgs()
args.task = ['db:create', 'db:migrate']
# Rake tasks
task_handler(args)
# Generate k8s configuration
k8s.create_deployment('k8s-deployment.yml')

class BaseArgs(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def make_project_dirs(self):
self._config_path.mkdir(parents=True, exist_ok=True)

def remove_dirs(self, path=None):
return
path = path or self.project_path
for child in path.iterdir():
if child.is_dir():
Expand Down
7 changes: 7 additions & 0 deletions tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
class TestGeneratedFiles(helper.TestCase):
def test_files(self):
args_helper.set_global_arg('name', self._helper.project_name)

cwd = os.getcwd()
project_dir = os.path.join(cwd, rails_options[1])
tag, release_tag = config.generate_project_tags(options.tag, project_dir)
create_files_for_the_project(rails_base_tag, options.database, project_dir);
config.write_rod(rails_base_tag, tag, release_tag, 'web', project_dir)

rails_cmds.create_files_for_the_project('rails-tag:1.0','mysql', str(self._helper.project_path))
# Database config
f = open(self._helper.config_path.joinpath('database.yml'))
Expand Down

0 comments on commit 1cd247a

Please sign in to comment.