Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copy an index or migrate between clusters #25

Merged
merged 5 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions arlas/cli/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,31 @@ def describe(
print(tab)


@indices.command(help="Clone an index and set its name")
def clone(
index: str = typer.Argument(help="Source index name"),
Copy link
Contributor

@WilliGautier WilliGautier Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry i meant mainly the variable name, not only the help

name: str = typer.Argument(help="Target cloned index name")
):
config = variables["arlas"]
indices = Service.clone_index(config, index, name)
tab = PrettyTable(indices[0], sortby="name", align="l")
tab.add_rows(indices[1:])
print(tab)


@indices.command(help="Migrate an index on another arlas configuration, and set the target index name")
def migrate(
index: str = typer.Argument(help="Source index name"),
arlas_target: str = typer.Argument(help="Target ARLAS Configuration name"),
name: str = typer.Argument(help="Target migrated index name")
):
config = variables["arlas"]
indices = Service.migrate_index(config, index, arlas_target, name)
tab = PrettyTable(indices[0], sortby="name", align="l")
tab.add_rows(indices[1:])
print(tab)


@indices.command(help="Display a sample of an index")
def sample(
index: str = typer.Argument(help="index's name"),
Expand Down Expand Up @@ -87,6 +112,7 @@ def data(
Service.index_hits(config, index=index, file_path=file, bulk_size=bulk, count=count)
i = i + 1


@indices.command(help="Generate the mapping based on the data")
def mapping(
file: str = typer.Argument(help="Path to the file conaining the data. Format: NDJSON"),
Expand Down
52 changes: 42 additions & 10 deletions arlas/cli/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,17 @@ def list_collections(arlas: str) -> list[list[str]]:
])
return table

def list_indices(arlas: str) -> list[list[str]]:
def list_indices(arlas: str, keep_only: str = None) -> list[list[str]]:
data = json.loads(Service.__es__(arlas, "_cat/indices?format=json"))
table = [["name", "status", "count", "size"]]
for index in data:
table.append([
index.get("index"),
index.get("status"),
index.get("docs.count"),
index.get("store.size")
])
if keep_only is None or keep_only == index.get("index"):
table.append([
index.get("index"),
index.get("status"),
index.get("docs.count"),
index.get("store.size")
])
return table

def set_collection_visibility(arlas: str, collection: str, public: bool):
Expand Down Expand Up @@ -227,6 +228,35 @@ def describe_index(arlas: str, index: str) -> list[list[str]]:
table.extend(Service.__get_fields__([], description.get(index, {}).get("mappings", {}).get("properties", {})))
return table

def clone_index(arlas: str, index: str, name: str) -> list[list[str]]:
Service.__es__(arlas, "/".join([index, "_block", "write"]), put="")
Service.__es__(arlas, "/".join([index, "_clone", name]), put="")
Service.__es__(arlas, "/".join([index, "_settings"]), put='{"index.blocks.write": false}')
return Service.list_indices(arlas, keep_only=name)

def migrate_index(arlas: str, index: str, target_arlas: str, target_name: str) -> list[list[str]]:
source = Configuration.settings.arlas.get(arlas)
migration = {
"source": {
"remote": {
"host": source.elastic.location,
"username": source.elastic.login,
"password": source.elastic.password,
},
"index": index,
"query": {"match_all": {}},
},
"dest": {"index": target_name},
}
print("1/3: fetch mapping ...")
mapping = Service.__es__(arlas, "/".join([index, "_mapping"]))
mapping = json.dumps(json.loads(mapping).get(index))
print("2/3: copy mapping ...")
Service.__es__(target_arlas, "/".join([target_name]), put=mapping)
print("3/3: copy data ...")
print(Service.__es__(target_arlas, "/".join(["_reindex"]), post=json.dumps(migration)))
return Service.list_indices(target_arlas, keep_only=target_name)

def sample_collection(arlas: str, collection: str, pretty: bool, size: int) -> dict:
sample = Service.__arlas__(arlas, "/".join(["explore", collection, "_search"]) + "?size={}".format(size))
return sample
Expand Down Expand Up @@ -432,6 +462,7 @@ def __arlas__(arlas: str, suffix, post=None, put=None, patch=None, delete=None,
else:
print("Error: request {} failed with status {}: {}".format(method, str(r.status_code), str(r.reason)), file=sys.stderr)
print(" url: {}".format(url), file=sys.stderr)
print(r.content)
exit(1)
except Exception as e:
print("Error: request {} failed on {}".format(method, e), file=sys.stderr)
Expand All @@ -452,20 +483,21 @@ def __es__(arlas: str, suffix, post=None, put=None, delete=None, exit_on_failure
auth = (endpoint.elastic.login, endpoint.elastic.password) if endpoint.elastic.login else None
method = "GET"
data = None
if post:
if post is not None:
data = post
method = "POST"
if put:
if put is not None:
data = put
method = "PUT"
if delete:
if delete is not None:
method = "DELETE"
r: requests.Response = Service.__request__(url, method, data, __headers, auth)
if r.ok:
return r.content
elif exit_on_failure:
print("Error: request {} failed with status {}: {}".format(method, str(r.status_code), str(r.reason)), file=sys.stderr)
print(" url: {}".format(url), file=sys.stderr)
print(r.content)
sylvaingaudan marked this conversation as resolved.
Show resolved Hide resolved
exit(1)
else:
raise RequestException(r.status_code, r.content)
Expand Down
10 changes: 10 additions & 0 deletions scripts/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ else
exit 1
fi

# ----------------------------------------------------------
echo "TEST clone index"
if python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml indices --config tests clone courses courses2 | grep courses2 ; then
echo "OK: course2 found"
yes | python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml indices --config tests delete courses2
else
echo "ERROR: course2 not found"
exit 1
fi


# ----------------------------------------------------------
echo "TEST add collection"
Expand Down
Loading