Skip to content

Commit a9d7b5b

Browse files
authored
Added flag for initialization scripts in yugabyted (yugabyte#5793)
- Added flag for initialization scripts in yugabyted - Added example for ycql to populate the data using file
1 parent ee13d1e commit a9d7b5b

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

bin/yugabyted

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,7 @@ class ControlScript(object):
11671167

11681168
self.configs.temp_data["daemon"] = args.daemon
11691169
self.configs.temp_data["ui"] = args.ui
1170+
self.configs.temp_data["initial_scripts_dir"] = args.initial_scripts_dir
11701171

11711172
if has_errors:
11721173
sys.exit(1)
@@ -1287,6 +1288,8 @@ class ControlScript(object):
12871288
cur_parser.add_argument(
12881289
"--ui", choices=BOOL_CHOICES, default="false", metavar="BOOL",
12891290
help="Toggle enabling or disabling webserver UI. Default false.")
1291+
cur_parser.add_argument(
1292+
"--initial_scripts_dir", help="Directory from where yugabyted reads initialization scripts")
12901293

12911294
# Hidden commands for development/advanced users
12921295
cur_parser.add_argument(
@@ -1359,6 +1362,34 @@ class ControlScript(object):
13591362
Output.log("Setting up custom credentials for YCQL...")
13601363
self.setup_env_init.setup_ycql_credentials(ycql_proxy)
13611364

1365+
if self.configs.temp_data.get("initial_scripts_dir"):
1366+
init_scripts = os.path.abspath(self.configs.temp_data.get("initial_scripts_dir"))
1367+
1368+
if os.path.exists(init_scripts):
1369+
Output.log("Initialization scripts from the {} directory".format(init_scripts))
1370+
1371+
sql_files = sorted([sql_file for sql_file in os.listdir(init_scripts) if (
1372+
sql_file.endswith('.sql'))])
1373+
cql_files = sorted([cql_file for cql_file in os.listdir(init_scripts) if (
1374+
cql_file.endswith('.cql'))])
1375+
1376+
ysql_proxy = YsqlProxy(ip=self.advertise_ip(),
1377+
port=self.configs.saved_data.get("ysql_port"))
1378+
if sql_files and retry_op(ysql_proxy.is_ysql_up):
1379+
self.load_init_scripts(ysql_proxy, init_scripts, sql_files)
1380+
1381+
ycql_proxy = YcqlProxy(ip=self.advertise_ip(),
1382+
port=self.configs.saved_data.get("ycql_port"))
1383+
if cql_files and retry_op(ycql_proxy.is_ycql_up):
1384+
self.load_init_scripts(ycql_proxy, init_scripts, cql_files)
1385+
1386+
def load_init_scripts(self, proxy_class, init_scripts_dir, files):
1387+
files_path = []
1388+
for name in files:
1389+
files_path.append(os.path.join(init_scripts_dir, name))
1390+
1391+
proxy_class.load_files(files_path)
1392+
13621393
class Configs(object):
13631394
def __init__(self, config_file, base_dir):
13641395
self.saved_data = {
@@ -1387,6 +1418,7 @@ class Configs(object):
13871418
"demo_db": DEFAULT_DEMO_DATABASE,
13881419
"daemon": True,
13891420
"ui": False,
1421+
"initial_scripts_dir": "",
13901422
}
13911423
self.config_file = config_file
13921424

@@ -1868,9 +1900,11 @@ class YsqlProxy(object):
18681900
env = self.env
18691901
if db:
18701902
env['PGDATABASE'] = db
1903+
else:
1904+
env['PGDATABASE'] = self.db
18711905
for path in filepaths:
18721906
cmd.extend(["-f", path])
1873-
run_process(cmd=cmd, log_cmd=False, env_vars=env)
1907+
run_process_checked(cmd=cmd, log_cmd=False, env_vars=env)
18741908

18751909
# Check user exists
18761910
# Note that this will return false if ysqlsh can't connect, even if user exists.
@@ -1974,6 +2008,19 @@ class YcqlProxy(object):
19742008
"CREATE KEYSPACE {};".format(keyspace)]
19752009
run_process_checked(cmd)
19762010

2011+
# Runs ycqlsh with specified files.
2012+
# Example:
2013+
# 1. bin/ycqlsh -f directory/a.ycql
2014+
# 2. If environment variables exists: bin/ycqlsh -u user -p password -f directory/b.ycql
2015+
def load_files(self, filepaths):
2016+
cmd = self.cmd
2017+
if self.setup_env_init.is_exists('YCQL_USER') or \
2018+
self.setup_env_init.is_exists('YCQL_PASSWORD'):
2019+
cmd.extend(["-u", self.username, "-p", self.password])
2020+
for path in filepaths:
2021+
cmd.extend(["-f", path])
2022+
run_process_checked(cmd=cmd, log_cmd=False)
2023+
19772024
# Check YCQL is UP
19782025
def is_ycql_up(self):
19792026
cmd = self.cmd + ["-u", self.username, "-p", self.password, "-e", "SHOW HOST"]

docs/content/latest/reference/configuration/yugabyted.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Usage: yugabyted start [-h] [--config CONFIG] [--data_dir DATA_DIR]
9090
[--webserver_port WEBSERVER_PORT]
9191
[--listen LISTEN] [--join JOIN]
9292
[--daemon BOOL] [--callhome BOOL] [--ui BOOL]
93+
[--initial_scripts_dir INITIAL_SCRIPTS_DIR]
9394
```
9495

9596
#### Flags
@@ -168,6 +169,12 @@ Enable or disable the "call home" feature that sends analytics data to Yugabyte.
168169

169170
Enable or disable the webserver UI. Default is `false`.
170171

172+
##### --initial_scripts_dir *initial-scripts-dir*
173+
174+
The directory from where yugabyted reads initialization scripts.
175+
The format will be: For YSQL - `.sql`, For YCQL - `.cql`.
176+
Initialization scripts will be executed in sorted name order.
177+
171178
-----
172179

173180
### stop

0 commit comments

Comments
 (0)