Fixed: Fixed the issue of TOML editing.

This commit is contained in:
vorotamoroz
2023-11-07 01:07:58 +00:00
parent f7209e566c
commit 280d9e1dd9

View File

@@ -3,8 +3,8 @@
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "view-in-github", "colab_type": "text",
"colab_type": "text" "id": "view-in-github"
}, },
"source": [ "source": [
"<a href=\"https://colab.research.google.com/gist/vrtmrz/37c3efd7842e49947aaaa7f665e5020a/deploy_couchdb_to_flyio_v2_with_swap.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" "<a href=\"https://colab.research.google.com/gist/vrtmrz/37c3efd7842e49947aaaa7f665e5020a/deploy_couchdb_to_flyio_v2_with_swap.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
@@ -12,15 +12,16 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {
"id": "HiRV7G8Gk1Rs"
},
"source": [ "source": [
"History:\n", "History:\n",
"- 18, May, 2023: Initial.\n", "- 18, May, 2023: Initial.\n",
"- 19, Jun., 2023: Patched for enabling swap.\n", "- 19, Jun., 2023: Patched for enabling swap.\n",
"- 22, Aug., 2023: Generating Setup-URI implemented." "- 22, Aug., 2023: Generating Setup-URI implemented.\n",
], "- 7, Nov., 2023: Fixed the issue of TOML editing."
"metadata": { ]
"id": "HiRV7G8Gk1Rs"
}
}, },
{ {
"cell_type": "code", "cell_type": "code",
@@ -45,7 +46,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"# Delete once\n", "# Delete once (Do not care about `cannot remove './fly.toml': No such file or directory`)\n",
"!rm ./fly.toml" "!rm ./fly.toml"
] ]
}, },
@@ -78,15 +79,15 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "execution_count": null,
"# Check the toml once.\n",
"!cat fly.toml"
],
"metadata": { "metadata": {
"id": "2RSoO9o-i2TT" "id": "2RSoO9o-i2TT"
}, },
"execution_count": null, "outputs": [],
"outputs": [] "source": [
"# Check the toml once.\n",
"!cat fly.toml"
]
}, },
{ {
"cell_type": "code", "cell_type": "code",
@@ -96,52 +97,45 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"# Modify fly.toml\n", "# Modify the TOML and generate Dockerfile\n",
"## Port modification\n", "!pip install mergedeep\n",
"!sed -i 's/8080/5984/g' fly.toml\n", "from mergedeep import merge\n",
"## Add user into.\n", "import toml\n",
"!echo -e \"\\n[env]\\n COUCHDB_USER = \\\"${couchUser}\\\"\" >> ./fly.toml\n", "fly = toml.load('fly.toml')\n",
"## Set the location of an ini file which to save configurations persistently via erlang flags.\n", "override = {\n",
"!echo -e \"\\nERL_FLAGS=\\\"-couch_ini /opt/couchdb/etc/default.ini /opt/couchdb/etc/default.d/ /opt/couchdb/etc/local.d /opt/couchdb/etc/local.ini /opt/couchdb/data/persistence.ini\\\"\" >> ./fly.toml\n", " \"http_service\":{\n",
"## Mounting volumes to store data and ini file.\n", " \"internal_port\":5984\n",
"!echo -e \"\\n[mounts]\\n source=\\\"couchdata\\\"\\n destination=\\\"/opt/couchdb/data\\\"\" >> ./fly.toml\n", " },\n",
"!cat fly.toml" " \"build\":{\n",
] " \"dockerfile\":\"./Dockerfile\"\n",
}, " },\n",
{ " \"mounts\":{\n",
"cell_type": "code", " \"source\":\"couchdata\",\n",
"source": [ " \"destination\":\"/opt/couchdb/data\"\n",
" },\n",
" \"env\":{\n",
" \"COUCHDB_USER\":os.environ['couchUser'],\n",
" \"ERL_FLAGS\":\"-couch_ini /opt/couchdb/etc/default.ini /opt/couchdb/etc/default.d/ /opt/couchdb/etc/local.d /opt/couchdb/etc/local.ini /opt/couchdb/data/persistence.ini\",\n",
" }\n",
"}\n",
"out = merge(fly,override)\n",
"with open('fly.toml', 'wt') as fp:\n",
" toml.dump(out, fp)\n",
" fp.close()\n",
"\n",
"# Make the Dockerfile to modify the permission of the ini file. If you want to use a specific version, you should change `latest` here.\n", "# Make the Dockerfile to modify the permission of the ini file. If you want to use a specific version, you should change `latest` here.\n",
"!echo -e \"\\n[build]\\n dockerfile = \\\"./Dockerfile\\\"\" >> ./fly.toml" "dockerfile = '''FROM couchdb:latest\n",
], "RUN sed -i '2itouch /opt/couchdb/data/persistence.ini && chmod +w /opt/couchdb/data/persistence.ini && fallocate -l 512M /swapfile && chmod 0600 /swapfile && mkswap /swapfile && echo 10 > /proc/sys/vm/swappiness && swapon /swapfile && echo 1 > /proc/sys/vm/overcommit_memory' /docker-entrypoint.sh\n",
"metadata": { "'''\n",
"id": "LQPsZ_dYxkTu" "with open(\"./Dockerfile\",\"wt\") as fp:\n",
}, " fp.write(dockerfile)\n",
"execution_count": null, " fp.close()\n",
"outputs": [] "\n",
}, "!echo ------\n",
{ "!cat fly.toml\n",
"cell_type": "code", "!echo ------\n",
"source": [ "!cat Dockerfile"
"!echo -e \"FROM couchdb:latest\\nRUN sed -i '2itouch /opt/couchdb/data/persistence.ini && chmod +w /opt/couchdb/data/persistence.ini && fallocate -l 512M /swapfile && chmod 0600 /swapfile && mkswap /swapfile && echo 10 > /proc/sys/vm/swappiness && swapon /swapfile && echo 1 > /proc/sys/vm/overcommit_memory' /docker-entrypoint.sh\" > ./Dockerfile" ]
],
"metadata": {
"id": "44cBeGJ9on5i"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Check dockerfile\n",
"!cat ./Dockerfile"
],
"metadata": {
"id": "ai2R3BbpxRSe"
},
"execution_count": null,
"outputs": []
}, },
{ {
"cell_type": "code", "cell_type": "code",
@@ -189,20 +183,27 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": {
"id": "cGlSzVqlQG_z"
},
"outputs": [],
"source": [ "source": [
"# Finish setting up the CouchDB\n", "# Finish setting up the CouchDB\n",
"# Please repeat until the request is completed without error messages\n", "# Please repeat until the request is completed without error messages\n",
"# i.e., You have to redo this block while \"curl: (35) OpenSSL SSL_connect: Connection reset by peer in connection to xxxx\" is showing.\n", "# i.e., You have to redo this block while \"curl: (35) OpenSSL SSL_connect: Connection reset by peer in connection to xxxx\" is showing.\n",
"#\n",
"# Note: A few minutes might be required to be booted.\n",
"!curl -X POST \"${couchHost}/_cluster_setup\" -H \"Content-Type: application/json\" -d \"{\\\"action\\\":\\\"enable_single_node\\\",\\\"username\\\":\\\"${couchUser}\\\",\\\"password\\\":\\\"${couchPwd}\\\",\\\"bind_address\\\":\\\"0.0.0.0\\\",\\\"port\\\":5984,\\\"singlenode\\\":true}\" --user \"${couchUser}:${couchPwd}\"" "!curl -X POST \"${couchHost}/_cluster_setup\" -H \"Content-Type: application/json\" -d \"{\\\"action\\\":\\\"enable_single_node\\\",\\\"username\\\":\\\"${couchUser}\\\",\\\"password\\\":\\\"${couchPwd}\\\",\\\"bind_address\\\":\\\"0.0.0.0\\\",\\\"port\\\":5984,\\\"singlenode\\\":true}\" --user \"${couchUser}:${couchPwd}\""
], ]
"metadata": {
"id": "cGlSzVqlQG_z"
},
"execution_count": null,
"outputs": []
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": {
"id": "JePzrsHypY18"
},
"outputs": [],
"source": [ "source": [
"# Please repeat until all lines are completed without error messages\n", "# Please repeat until all lines are completed without error messages\n",
"!curl -X PUT \"${couchHost}/_node/nonode@nohost/_config/chttpd/require_valid_user\" -H \"Content-Type: application/json\" -d '\"true\"' --user \"${couchUser}:${couchPwd}\"\n", "!curl -X PUT \"${couchHost}/_node/nonode@nohost/_config/chttpd/require_valid_user\" -H \"Content-Type: application/json\" -d '\"true\"' --user \"${couchUser}:${couchPwd}\"\n",
@@ -214,28 +215,28 @@
"!curl -X PUT \"${couchHost}/_node/nonode@nohost/_config/couchdb/max_document_size\" -H \"Content-Type: application/json\" -d '\"50000000\"' --user \"${couchUser}:${couchPwd}\"\n", "!curl -X PUT \"${couchHost}/_node/nonode@nohost/_config/couchdb/max_document_size\" -H \"Content-Type: application/json\" -d '\"50000000\"' --user \"${couchUser}:${couchPwd}\"\n",
"!curl -X PUT \"${couchHost}/_node/nonode@nohost/_config/cors/credentials\" -H \"Content-Type: application/json\" -d '\"true\"' --user \"${couchUser}:${couchPwd}\"\n", "!curl -X PUT \"${couchHost}/_node/nonode@nohost/_config/cors/credentials\" -H \"Content-Type: application/json\" -d '\"true\"' --user \"${couchUser}:${couchPwd}\"\n",
"!curl -X PUT \"${couchHost}/_node/nonode@nohost/_config/cors/origins\" -H \"Content-Type: application/json\" -d '\"app://obsidian.md,capacitor://localhost,http://localhost\"' --user \"${couchUser}:${couchPwd}\"" "!curl -X PUT \"${couchHost}/_node/nonode@nohost/_config/cors/origins\" -H \"Content-Type: application/json\" -d '\"app://obsidian.md,capacitor://localhost,http://localhost\"' --user \"${couchUser}:${couchPwd}\""
], ]
"metadata": {
"id": "JePzrsHypY18"
},
"execution_count": null,
"outputs": []
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {
"id": "YfSOomsoXbGS"
},
"source": [ "source": [
"Now, our CouchDB has been surely installed and configured. Cheers!\n", "Now, our CouchDB has been surely installed and configured. Cheers!\n",
"\n", "\n",
"In the steps that follow, create a setup-URI.\n", "In the steps that follow, create a setup-URI.\n",
"\n", "\n",
"This URI could be imported directly into Self-hosted LiveSync, to configure the use of the CouchDB which we configured now." "This URI could be imported directly into Self-hosted LiveSync, to configure the use of the CouchDB which we configured now."
], ]
"metadata": {
"id": "YfSOomsoXbGS"
}
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": {
"id": "416YncOqXdNn"
},
"outputs": [],
"source": [ "source": [
"# Database config\n", "# Database config\n",
"import random, string\n", "import random, string\n",
@@ -250,39 +251,39 @@
"\n", "\n",
"print(\"Your database:\"+os.environ['database'])\n", "print(\"Your database:\"+os.environ['database'])\n",
"print(\"Your passphrase:\"+os.environ['passphrase'])" "print(\"Your passphrase:\"+os.environ['passphrase'])"
], ]
"metadata": {
"id": "416YncOqXdNn"
},
"execution_count": null,
"outputs": []
}, },
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "execution_count": null,
"# Install deno for make setup uri\n",
"!curl -fsSL https://deno.land/x/install/install.sh | sh"
],
"metadata": { "metadata": {
"id": "C4d7C0HAXgsr" "id": "C4d7C0HAXgsr"
}, },
"execution_count": null, "outputs": [],
"outputs": [] "source": [
"# Install deno for make setup uri\n",
"!curl -fsSL https://deno.land/x/install/install.sh | sh"
]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "execution_count": null,
"# Fetch module for encrypting a Setup URI\n",
"!curl -o encrypt.ts https://gist.githubusercontent.com/vrtmrz/f9d1d95ee2ca3afa1a924a2c6759b854/raw/d7a070d864a6f61403d8dc74208238d5741aeb5a/encrypt.ts"
],
"metadata": { "metadata": {
"id": "hQL_Dx-PXise" "id": "hQL_Dx-PXise"
}, },
"execution_count": null, "outputs": [],
"outputs": [] "source": [
"# Fetch module for encrypting a Setup URI\n",
"!curl -o encrypt.ts https://gist.githubusercontent.com/vrtmrz/f9d1d95ee2ca3afa1a924a2c6759b854/raw/d7a070d864a6f61403d8dc74208238d5741aeb5a/encrypt.ts"
]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": {
"id": "o0gX_thFXlIZ"
},
"outputs": [],
"source": [ "source": [
"# Make buttons!\n", "# Make buttons!\n",
"from IPython.display import HTML\n", "from IPython.display import HTML\n",
@@ -294,29 +295,24 @@
"else:\n", "else:\n",
" result = \"Failed to encrypt the setup URI\"\n", " result = \"Failed to encrypt the setup URI\"\n",
"result" "result"
], ]
"metadata": {
"id": "o0gX_thFXlIZ"
},
"execution_count": null,
"outputs": []
} }
], ],
"metadata": { "metadata": {
"colab": { "colab": {
"provenance": [], "include_colab_link": true,
"private_outputs": true, "private_outputs": true,
"include_colab_link": true "provenance": []
}, },
"gpuClass": "standard",
"kernelspec": { "kernelspec": {
"display_name": "Python 3", "display_name": "Python 3",
"name": "python3" "name": "python3"
}, },
"language_info": { "language_info": {
"name": "python" "name": "python"
}, }
"gpuClass": "standard"
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 0
} }