From e964ec8f74b644d66ca166a7524adcc3a82709c9 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sat, 14 May 2022 19:56:06 +0200 Subject: feat: Move code to blog - Move most logic of the blog to blog.rs, making it easier to test. - Now the file contains the creation date of the blog post to be parsed. - Add chrono to parse datetime, so later we can order by date. - Refactor gitlab pipeline, move `before_script` to a proper place. --- .gitlab-ci.yml | 10 ++- Cargo.lock | 52 ++++++++++++++- Cargo.toml | 7 +- .../posts/2020-07-14Friz_box_turned_off_DHCP.html | 18 ++++++ content/posts/2021-12-26Enable_NFS_on_K3S.html | 60 ++++++++++++++++++ ...26K8S_private_gitlab_registry_using_podman.html | 39 ++++++++++++ content/posts/Enable_NFS_on_K3S.html | 60 ------------------ content/posts/Friz.box_turned_off_DHCP.html | 18 ------ .../K8S_private_gitlab_registry_using_podman.html | 39 ------------ src/blog.rs | 74 ++++++++++++++++++++++ src/lib.rs | 2 + src/main.rs | 64 ++----------------- tests/test_blog.rs | 36 +++++++++++ tests/test_router.rs | 2 +- 14 files changed, 294 insertions(+), 187 deletions(-) create mode 100644 content/posts/2020-07-14Friz_box_turned_off_DHCP.html create mode 100644 content/posts/2021-12-26Enable_NFS_on_K3S.html create mode 100644 content/posts/2021-12-26K8S_private_gitlab_registry_using_podman.html delete mode 100644 content/posts/Enable_NFS_on_K3S.html delete mode 100644 content/posts/Friz.box_turned_off_DHCP.html delete mode 100644 content/posts/K8S_private_gitlab_registry_using_podman.html create mode 100644 src/blog.rs create mode 100644 src/lib.rs create mode 100644 tests/test_blog.rs diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 12003e9..8cae0ac 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,14 +1,9 @@ -before_script: - - apt-get update -qy - - apt-get install -y ruby-dev - - gem install dpl - stages: - test - production test: - image: rust:latest + image: rust:alpine stage: test script: - cargo install cargo2junit @@ -25,6 +20,9 @@ production: needs: - test script: + - apt-get update -qy + - apt-get install -y ruby-dev + - gem install dpl - gem install faraday -v 1.8.0 - dpl --provider=heroku --app=$HEROKU_APP --api-key=$HEROKU_API_KEY only: diff --git a/Cargo.lock b/Cargo.lock index b687dc4..0b23a34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,6 +44,19 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "time", + "winapi", +] + [[package]] name = "cpufeatures" version = "0.2.2" @@ -287,6 +300,7 @@ dependencies = [ name = "macroblog" version = "0.1.0" dependencies = [ + "chrono", "hyper", "regex", "rust-embed", @@ -310,7 +324,7 @@ dependencies = [ "log", "miow", "ntapi", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "winapi", ] @@ -332,6 +346,25 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.13.1" @@ -601,6 +634,17 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "tokio" version = "1.18.1" @@ -738,6 +782,12 @@ dependencies = [ "try-lock", ] +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index de00d11..a241371 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,8 @@ sailfish = "0.4.0" hyper = { version = "0.14", features = ["full"] } tokio = { version = "1", features = ["full"] } regex = "1.5" -rust-embed="6.4.0" +rust-embed = "6.4.0" +chrono = "0.4" [profile.release] opt-level = 'z' @@ -18,5 +19,5 @@ panic = 'abort' strip = true [lib] -name="router" -path="src/router.rs" +name="macroblog" +path="src/lib.rs" diff --git a/content/posts/2020-07-14Friz_box_turned_off_DHCP.html b/content/posts/2020-07-14Friz_box_turned_off_DHCP.html new file mode 100644 index 0000000..569604b --- /dev/null +++ b/content/posts/2020-07-14Friz_box_turned_off_DHCP.html @@ -0,0 +1,18 @@ +
+

Friz.box turned off DHCP

+

+ If you turned off your DHCP server follow these steps to connect to FritzBox settings. +
+

+

+

+ On gnome turn the wired connection off on again to apply the settings. +

+

+ Note: why in the hell does FritzBox 7490 require a land-line telephone to be physically factory + reset? +

+
diff --git a/content/posts/2021-12-26Enable_NFS_on_K3S.html b/content/posts/2021-12-26Enable_NFS_on_K3S.html new file mode 100644 index 0000000..09f91e7 --- /dev/null +++ b/content/posts/2021-12-26Enable_NFS_on_K3S.html @@ -0,0 +1,60 @@ +
+

+ By default K3S comes only with local-path storage class, and if you are + running + with more than one node in your cluster you may want to use a more “distributed” + solution. For may case I opted for NFS. +

+

+ To check the current storage class you can run: +

+
k3s kubectl get storageclasses
+

+ And it will print something like: +

+
NAME                   PROVISIONER                                     RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
+local-path (default)   rancher.io/local-path                           Delete          WaitForFirstConsumer   false                  154d
+

+ To start adding First you need to install helm on your server. To do + so you may + run: +

+ +
curl -sSL https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
+

+ Be careful when running scripts directly into bash always check the source + Sometimes is also recommended to do not pipe directly to bash +

+

+ Once it is installed we need to add the NFS storage classes. It has two + providers, I have chose NFS Subdir + External Provisioner. +

+

+ Add the helm repo +

+ +
helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/
+

+ Then we need to actually install the provider +

+
+
helm install nfs-subdir-external-provisioner nfs-subdir-external-provisioner/nfs-subdir-external-provisioner
+--set nfs.server=x.x.x.x
+--set nfs.path=/exported/path
+
+

+ Set the nfs.server and nfs.path accordingly with your setup. +

+ +

+ After that if we run k3s kubectl get storageclasses it will now print another + NFS provider: +

+ +
NAME                   PROVISIONER                                     RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
+local-path (default)   rancher.io/local-path                           Delete          WaitForFirstConsumer   false                  154d
+nfs-client             cluster.local/nfs-subdir-external-provisioner   Delete          Immediate              true                   76m
+
diff --git a/content/posts/2021-12-26K8S_private_gitlab_registry_using_podman.html b/content/posts/2021-12-26K8S_private_gitlab_registry_using_podman.html new file mode 100644 index 0000000..43b8245 --- /dev/null +++ b/content/posts/2021-12-26K8S_private_gitlab_registry_using_podman.html @@ -0,0 +1,39 @@ +
+

K8S private gitlab registry using podman

+

+ This is based on Log in to + Docker Hub. It is just a bit different to use podman. +

+

+ First we should take a look at podman-login man page: +

man podman login
+

+

+ It will give some valueable information like the location of auth.json file. Now we can login using podman: +

podman login registry.gitlab.com
+

+

Then check the auth.json file located at ${XDG_RUNTIME_DIR}/containers/auth.json (as described + by the manual). It will contain your auth config: +

{
+	"auths": {
+		"registry.gitlab.com": {
+			"auth": "..."
+		}
+	}
+}
+

+

+ Now copy that file over to the server and register it in k8s with the following command: +


+kubectl create secret generic regcred \
+    --from-file=.dockerconfigjson=auth.json \
+    --type=kubernetes.io/dockerconfigjson
+

+

+ Once you have created you can list by kubectl get secret: +


+NAME     TYPE                                  DATA   AGE
+regcred  kubernetes.io/dockerconfigjson        1      53s
+

+
diff --git a/content/posts/Enable_NFS_on_K3S.html b/content/posts/Enable_NFS_on_K3S.html deleted file mode 100644 index 09f91e7..0000000 --- a/content/posts/Enable_NFS_on_K3S.html +++ /dev/null @@ -1,60 +0,0 @@ -
-

- By default K3S comes only with local-path storage class, and if you are - running - with more than one node in your cluster you may want to use a more “distributed” - solution. For may case I opted for NFS. -

-

- To check the current storage class you can run: -

-
k3s kubectl get storageclasses
-

- And it will print something like: -

-
NAME                   PROVISIONER                                     RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
-local-path (default)   rancher.io/local-path                           Delete          WaitForFirstConsumer   false                  154d
-

- To start adding First you need to install helm on your server. To do - so you may - run: -

- -
curl -sSL https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
-

- Be careful when running scripts directly into bash always check the source - Sometimes is also recommended to do not pipe directly to bash -

-

- Once it is installed we need to add the NFS storage classes. It has two - providers, I have chose NFS Subdir - External Provisioner. -

-

- Add the helm repo -

- -
helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/
-

- Then we need to actually install the provider -

-
-
helm install nfs-subdir-external-provisioner nfs-subdir-external-provisioner/nfs-subdir-external-provisioner
---set nfs.server=x.x.x.x
---set nfs.path=/exported/path
-
-

- Set the nfs.server and nfs.path accordingly with your setup. -

- -

- After that if we run k3s kubectl get storageclasses it will now print another - NFS provider: -

- -
NAME                   PROVISIONER                                     RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
-local-path (default)   rancher.io/local-path                           Delete          WaitForFirstConsumer   false                  154d
-nfs-client             cluster.local/nfs-subdir-external-provisioner   Delete          Immediate              true                   76m
-
diff --git a/content/posts/Friz.box_turned_off_DHCP.html b/content/posts/Friz.box_turned_off_DHCP.html deleted file mode 100644 index 569604b..0000000 --- a/content/posts/Friz.box_turned_off_DHCP.html +++ /dev/null @@ -1,18 +0,0 @@ -
-

Friz.box turned off DHCP

-

- If you turned off your DHCP server follow these steps to connect to FritzBox settings. -
-

-

-

- On gnome turn the wired connection off on again to apply the settings. -

-

- Note: why in the hell does FritzBox 7490 require a land-line telephone to be physically factory - reset? -

-
diff --git a/content/posts/K8S_private_gitlab_registry_using_podman.html b/content/posts/K8S_private_gitlab_registry_using_podman.html deleted file mode 100644 index 43b8245..0000000 --- a/content/posts/K8S_private_gitlab_registry_using_podman.html +++ /dev/null @@ -1,39 +0,0 @@ -
-

K8S private gitlab registry using podman

-

- This is based on Log in to - Docker Hub. It is just a bit different to use podman. -

-

- First we should take a look at podman-login man page: -

man podman login
-

-

- It will give some valueable information like the location of auth.json file. Now we can login using podman: -

podman login registry.gitlab.com
-

-

Then check the auth.json file located at ${XDG_RUNTIME_DIR}/containers/auth.json (as described - by the manual). It will contain your auth config: -

{
-	"auths": {
-		"registry.gitlab.com": {
-			"auth": "..."
-		}
-	}
-}
-

-

- Now copy that file over to the server and register it in k8s with the following command: -


-kubectl create secret generic regcred \
-    --from-file=.dockerconfigjson=auth.json \
-    --type=kubernetes.io/dockerconfigjson
-

-

- Once you have created you can list by kubectl get secret: -


-NAME     TYPE                                  DATA   AGE
-regcred  kubernetes.io/dockerconfigjson        1      53s
-

-
diff --git a/src/blog.rs b/src/blog.rs new file mode 100644 index 0000000..6c190a9 --- /dev/null +++ b/src/blog.rs @@ -0,0 +1,74 @@ +use rust_embed::RustEmbed; +use sailfish::TemplateOnce; +use chrono::NaiveDate; +use regex::{Regex}; +use std::str; + +const BLOG_REGEX: &str = r"(?P[\d]{4}-[\d]{2}-[\d]{2})(?P[a-zA-Z0-9_]*)"; + +#[derive(RustEmbed)] +#[folder = "content/posts/"] +struct PostAsset; + + +#[derive(TemplateOnce)] +#[template(path = "index.html")] +struct IndexTemplate { + posts: Vec<BlogEntry>, +} + +#[derive(TemplateOnce)] +#[template(path = "post.html")] +struct PostTemplate { + content: String, +} + +pub struct BlogEntry { + pub title: String, + pub datetime: NaiveDate, + pub file: String, +} + +impl BlogEntry { + pub fn new(path: &String) -> BlogEntry { + let re = Regex::new(BLOG_REGEX).unwrap(); + let caps = re.captures(path).unwrap(); + let date = &caps["date"]; + let title = str::replace(&caps["title"], "_", " "); + + BlogEntry { + title: String::from(title), + file: String::from(path), + datetime: NaiveDate::parse_from_str(date, "%Y-%m-%d").unwrap() + } + } + + pub fn read_assets() -> Vec<BlogEntry> { + PostAsset::iter() + .map(|e| format!("{}", e)) + .map(|e| BlogEntry::new(&e)) + .collect() + } +} + +fn get_file_content(path: &str) -> String { + let buffer = PostAsset::get(path) + .unwrap() + .data + .into_owned(); + + return String::from_utf8(buffer).unwrap(); +} + + +pub fn render_post_page(path: &String) -> String { + PostTemplate { content: get_file_content(path) } + .render_once() + .unwrap() +} + +pub fn render_index_page() -> String { + IndexTemplate { posts: BlogEntry::read_assets() } + .render_once() + .unwrap() +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..0c69888 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,2 @@ +pub mod blog; +pub mod router; diff --git a/src/main.rs b/src/main.rs index 63a5386..ed34713 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,60 +1,11 @@ -pub mod router; use std::convert::Infallible; -use rust_embed::RustEmbed; -use std::{env, str}; +use std::{env}; use std::net::SocketAddr; use hyper::{Body, Request, Response, Server}; use hyper::service::{make_service_fn, service_fn}; -use sailfish::TemplateOnce; -use ::router::Router; - -struct PostEntry { - title: String, - file: String, -} - -#[derive(TemplateOnce)] -#[template(path = "index.html")] -struct IndexTemplate { - posts: Vec<PostEntry>, -} - -#[derive(TemplateOnce)] -#[template(path = "post.html")] -struct PostTemplate { - content: String, -} - -#[derive(RustEmbed)] -#[folder = "content/posts/"] -struct PostAsset; - - -fn get_file_content(path: &str) -> String { - let buffer = PostAsset::get(path) - .unwrap() - .data - .into_owned(); - - return String::from_utf8(buffer).unwrap(); -} - -fn get_post_entry(path: &String) -> PostEntry { - let sub_title = str::replace(path, "_", " "); - let title = str::replace(sub_title.as_str(), ".html", ""); - PostEntry { - title: String::from(title), - file: String::from(path), - } -} - -fn get_post_title() -> Vec<PostEntry> { - PostAsset::iter() - .map(|e| format!("{}", e)) - .map(|e| get_post_entry(&e)) - .collect() -} +use macroblog::router::Router; +use macroblog::blog::{render_index_page, render_post_page}; async fn not_found() -> Result<Response<Body>, Infallible> { @@ -67,10 +18,7 @@ async fn not_found() -> Result<Response<Body>, Infallible> { async fn index() -> Result<Response<Body>, Infallible> { - let files = get_post_title(); - let body = IndexTemplate { posts: files } - .render_once() - .unwrap(); + let body = render_index_page(); let resp: Response<Body> = Response::builder() .status(200) @@ -83,9 +31,7 @@ async fn index() -> Result<Response<Body>, Infallible> { async fn post(path: &String) -> Result<Response<Body>, Infallible> { - let body = PostTemplate { content: get_file_content(path) } - .render_once() - .unwrap(); + let body = render_post_page(path); let resp: Response<Body> = Response::builder() .status(200) diff --git a/tests/test_blog.rs b/tests/test_blog.rs new file mode 100644 index 0000000..31d5725 --- /dev/null +++ b/tests/test_blog.rs @@ -0,0 +1,36 @@ +use macroblog::blog::*; + +use chrono::NaiveDate; + + +#[test] +fn test_create_blog_entry() { + let asset_filename = String::from("2021-12-03Enable_NFS_on_K3S.html"); + let post_date = NaiveDate::from_ymd(2021, 12, 03); + + let blog_entry = BlogEntry::new(&asset_filename); + + assert_eq!(blog_entry.title, "Enable NFS on K3S"); + assert_eq!(blog_entry.datetime, post_date); +} + +#[test] +fn test_read_assets() { + // This test meant to test if all files are parsed correctly + let assets = BlogEntry::read_assets(); + assert!(assets.iter().count() > 1) +} + +#[test] +fn test_render_post_page() { + let path = &String::from("2020-07-14Friz_box_turned_off_DHCP.html"); + let page = render_post_page(path); + assert!(!page.is_empty()); +} + + +#[test] +fn test_render_index_page() { + let page = render_index_page(); + assert!(!page.is_empty()); +} diff --git a/tests/test_router.rs b/tests/test_router.rs index 0158d78..7ebe019 100644 --- a/tests/test_router.rs +++ b/tests/test_router.rs @@ -1,4 +1,4 @@ -use router::{Router}; +use macroblog::router::{Router}; #[test] fn test_router_new_posts() { -- cgit v1.2.3