-
Notifications
You must be signed in to change notification settings - Fork 1
/
rod
executable file
·222 lines (175 loc) · 9.15 KB
/
rod
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python3
import sys, os, glob
import argparse
import signal
import time
from lib.config import RodConfig, rod_config
from lib import config, template, docker_cmds, rails_cmds, args_helper
from lib.cmd_helper import merge_cmds, run_cmd
from lib.rails_cmds import rake_tasks, 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, docker_compose_shell_cmd
from lib import k8s
NAME="rod"
def signal_handler(sig, frame):
print('\nWaiting for exiting...')
signal.signal(signal.SIGINT, signal_handler)
def project(args):
print(args)
print("project")
def build_image_handler(args):
tag = RodConfig.instance.image.tag
base = config.get_rails_base_tag()
tag_name = args.tag or tag
print(f"Create Rails project image with tag `{tag_name}` based on `{base}`")
project_dir = os.getcwd()
rails_cmds.build_image(tag_name, project_dir)
def image_handler(args):
print(args)
def shell_handler(args):
cmds = docker_compose_shell_cmd('sh', config.get_docker_compose_service())
run_cmd(*cmds)
def generate(args):
print(args)
def lock_gemfile(rails_base_tag, project_dir):
cmd = docker_project_base_cmd(rails_base_tag,project_dir) + ['bundle', 'lock', '--add-platform','ruby']
run_cmd(cmd)
def bundle_commands_handler(args):
print(args)
def bundle_install_handler(args):
print(args)
def tasks_handler(args):
print(args)
@rod_config
def task_handler(args):
rake_tasks(args)
def deploy_commands_handler(args):
pass
@rod_config
def deploy_k8s_handler(args):
args_helper.set_production()
if args.minikube:
args_helper.set_minikube()
project_dir = os.getcwd()
# 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
rails_cmds.build_production_image(args)
args = args_helper.CustomArgs()
args.task = ['db:create', 'db:migrate']
# Rake tasks
task_handler(args)
# Generate k8s configuration
k8s.create_deployment('k8s-deployment.yml')
if hasattr(args,'service') and args.service:
k8s.create_service('k8s-service.yml')
@rod_config
def deploy_service_handler(args):
if args.minikube:
args_helper.set_minikube()
k8s.create_service('k8s-service.yml')
class BaseArgs(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
super().__init__(option_strings, dest, nargs=0, **kwargs)
def __call__(self, parser, namespace, values, option_string):
setattr(namespace, self.dest, True)
if self.dest == 'dry_run':
args_helper.set_dry_run()
if self.dest == 'trace':
args_helper.set_trace()
if self.dest == 'production':
args_helper.set_production()
parser = argparse.ArgumentParser(prog=NAME)
subparsers = parser.add_subparsers(help='sub-command help')
base_subparser = argparse.ArgumentParser(add_help=False)
base_subparser.add_argument('--dry-run', nargs=0, action=BaseArgs)
base_subparser.add_argument('--trace', nargs=0, action=BaseArgs)
base_subparser.add_argument('--production', action=BaseArgs)
# Shell
parser_generate = subparsers.add_parser('Shell',aliases=['sh'], help='Attach to the shell')
parser_generate.set_defaults(func=shell_handler)
# Generator
parser_generate = subparsers.add_parser('generate',aliases=['g'], help='Generate controllers, models and scaffolds')
parser_generate.set_defaults(func=generate)
generate_subparsers = parser_generate.add_subparsers(help="Generate a controller")
## Generate controllers
controller_parser = generate_subparsers.add_parser('controller', help='Generate a controller')
controller_parser.add_argument('NAME', help='Controller name')
controller_parser.add_argument('actions', nargs='*', type=str, help='Controller name')
controller_parser.set_defaults(func=generate_controller_handler)
## Generate models
model_parser = generate_subparsers.add_parser('model', help='Generate a model', parents=[base_subparser])
model_parser.add_argument('NAME', help='Model name')
model_parser.add_argument('fields', nargs='*', type=str, help='Format: [field[:type][:index]')
model_parser.set_defaults(func=generate_model_handler)
## Generate scaffolds
scaffold_parser = generate_subparsers.add_parser('scaffold', help='Generate a scaffold')
scaffold_parser.add_argument('NAME', help='Scaffold name')
scaffold_parser.add_argument('fields', nargs='*', type=str, help='Format: [field[:type][:index]')
scaffold_parser.set_defaults(func=generate_scaffold_handler)
# Image
parser_image = subparsers.add_parser('image', help='Build Rails image')
## Build image for the Rails project
parser_image_subparsers = parser_image.add_subparsers(help="subcommands for Rails images")
parser_build = parser_image_subparsers.add_parser('build', help='Create a build Rails image')
parser_build.add_argument('-t', '--tag', dest="tag", action='store', help='Specify image tag')
parser_build.set_defaults(func=build_image_handler)
parser_image.set_defaults(func=image_handler)
# project
parser_project = subparsers.add_parser('project', help='create a new rails project')
parser_project_subparsers = parser_project.add_subparsers(help="subcommands for project")
parser_new = parser_project_subparsers.add_parser('new', help='create a new rails project', parents=[base_subparser])
parser_new.add_argument('name', help='project name')
parser_new.add_argument('-v', '--version', dest="version", action='store', help='specify rails version')
parser_new.add_argument('-t', '--tag', dest="tag", action='store', help='specify tag for Docker image')
parser_new.add_argument('-m', '--mysql', dest="database", action='store_const', const="mysql", help='using mysql')
parser_new.add_argument('-p', '--pg', dest="database", action='store_const', const="postgresql", help='using postgresql')
parser_new.add_argument('-s', '--sqlite3', dest="database", action='store_const', const="sqlite3", help='using sqlite3')
parser_new.add_argument('-b', '--skip-bundle', action='store_true', help='Don\'t run bundle install')
parser_new.set_defaults(func=new_project_handler)
parser_project.set_defaults(func=project_handler)
# Rails commands
parser_rails = subparsers.add_parser('rails', help='Rails commands', parents=[base_subparser])
parser_rails.set_defaults(func=rails_command_handler)
## Rails server
parser_rails_subparsers = parser_rails.add_subparsers(help="subcommands for Rails")
parser_server = parser_rails_subparsers.add_parser('server', aliases=['s'], help='Start a Rails server', parents=[base_subparser])
parser_server.add_argument('-p', '--port', dest="port", action='store', help='Specify the port for the server')
parser_server.set_defaults(func=rails_command_server_handler)
## Rails console
parser_console = parser_rails_subparsers.add_parser('console', help='Start a Rails console', parents=[base_subparser])
parser_console.set_defaults(func=rails_command_console_handler)
# Rake tasks
parser_tasks = subparsers.add_parser('tasks', aliases=['t'], help='Execute rake tasks', parents=[base_subparser])
parser_tasks.set_defaults(func=tasks_handler)
parser_tasks.add_argument('task', nargs='+', type=str, help='Rake tasks, such as db:migrate db:rollback')
parser_tasks.set_defaults(func=task_handler)
## Bundle
parser_bundle = subparsers.add_parser('bundle', help='Bundle commands')
parser_bundle.set_defaults(func=bundle_commands_handler)
## Bundle install
parser_bundle_subparsers = parser_bundle.add_subparsers(help="subcommands for bundle")
parser_install = parser_bundle_subparsers.add_parser('install', aliases=['s'], help='Bundle install')
parser_install.set_defaults(func=bundle_install_handler)
## Deploy
parser_deploy = subparsers.add_parser('deploy', help='Deploy commands')
parser_deploy.set_defaults(func=deploy_commands_handler)
## Deploy install
parser_deploy_subparsers = parser_deploy.add_subparsers(help="subcommands for deploy")
parser_k8s = parser_deploy_subparsers.add_parser('k8s', help='Deploy to k8s')
parser_k8s.add_argument('-m', '--minikube', action='store_true', default=True, help='Build image using minikube')
parser_k8s.add_argument('-s', '--service', action='store_true', default=False, help='Expose as a Kubernetes service')
parser_k8s.add_argument('--helm', action='store_true', default=False, help='Using Helm')
parser_k8s.add_argument('--istio', action='store_true', default=False, help='Add Istio')
parser_k8s.set_defaults(func=deploy_k8s_handler)
parser_service = parser_deploy_subparsers.add_parser('service', help='Deploy a service')
parser_service.add_argument('-m', '--minikube', action='store_true', default=True, help='Build image using minikube')
parser_service.set_defaults(func=deploy_service_handler)
if len(sys.argv) == 1:
parser.print_help()
exit(1)
# parse some argument lists
opts = parser.parse_args(sys.argv[1:])
opts.func(opts)