Cyberpunk TIL

Today I learned.

When I found CPU usage is higher than usual, I used to do two things:

  1. Use ps & grep to filter possible processes
1
ps aux | grep ruby
  1. Kill the processes
1
kill PROCESS_ID

However, if there are lots of processes to kill, it would be great if you merge these two steps and one more powerful tool awk:

1
kill $(ps aux | grep 'guard' | awk '{print $2}')

Nice and easy.

Kubernetes ConfigMap & Secrets are both really useful for managing environment variables and credentials.

You can create a cnofigmap from shell easily:

1
kubectl create configmap app-config --from-env-file ./.env.production

If you wanna change the configuration in configmap, you can just edit it from command line as well:

1
kubectl edit configmap admin-config

Very nice and easy.

Everytime I tried to visit websites with a .dev domain, I found the following error message:

1
DNS_PROBE_FINISHED_NXDOMAIN

However when I use dig to figure out why I can’t resolve .dev domain, everything goes well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ dig pptr.dev

; <<>> DiG 9.10.6 <<>> pptr.dev
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 7638
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;pptr.dev. IN A

;; ANSWER SECTION:
pptr.dev. 3599 IN A 185.199.108.153
pptr.dev. 3599 IN A 185.199.109.153
pptr.dev. 3599 IN A 185.199.110.153
pptr.dev. 3599 IN A 185.199.111.153

;; Query time: 224 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Thu Dec 27 11:05:52 CST 2018
;; MSG SIZE rcvd: 101

So I guess it’s related to my local settings about DNS. The solution is here: remove /etc/resolver/dev and done.

The reason why there was a etc/resolver/dev file is I used to develop websites with a .dev local domain for testing for years.

Now .dev gTLD domain is owned by Google, you should use .test, .local for local development.

When you got:

1
Makefile:2: *** missing separator.  Stop.

or

1
* missing separator (did you mean TAB instead of 8 spaces?). Stop.

It’s easy to fix it. Use Tab instead of space in your Makefile.

You can fix it by changing Spaces to Tab characters. Simply open your Makefile

1
$ vim Makefile

And then run the following command within:

1
:%s/^[ ]\+/^I/

This will substitute all the lines begin with one or more Spaces character with an actual Tab.

Why puma-dev

puma-dev[1] is a tool to manage rack apps in development with puma.

If you need the following features, you have to use puma-dev instead of pow:

  • Websockets or ActionCable in Rails 5
  • Zero-config HTTPs support to your development environment.

Installation

  • Remove your pow first $ curl get.pow.cx/uninstall.sh | sh
  • Install via Homebrew: brew install puma/puma/puma-dev
  • sudo puma-dev -setup to configure DNS settings as root.
  • Run puma-dev -install to configure puma-dev and run it in the background on ports both 80 and 443. This step requires root permission again to install certificates for HTTPs.

Pro tips

  • Just move or rename your old ~/.pow directory to ~/.puma-dev
  • Run puma-dev link in the rack app directory then it will generate the link automatically

  1. https://github.com/puma/puma-dev ↩︎

Use both ActiveRecord and Mongoid in a Rails project is easy. Add mongoid in Gemfile:

1
gem 'mongoid', '~> 6.1.0'

then bundle install

However, the mongoid gem would change your default ORM adapter from ActiveRecord to Mongoid:

If you’re not sure which default adapter is you can try: rails g model and check the output message

1
2
3
4
5
6
7
8
Usage:
rails generate model NAME [field[:type][:index] field[:type][:index]] [options]

Options:
[--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated applications)
[--force-plural], [--no-force-plural] # Forces the use of the given model name
-o, --orm=NAME # ORM to be invoked
# Default: mongoid

So if you wanna generate a new model inherited from ActiveRecord:

1
rails g model Manager --orm=active_record

Notice: it’s active_record instead of activerecord

It’s easy to setup a Hexo website, there are tons of tutorials on the internet.

Hexo: Basic Setup

It’s easy to setup a Hexo website, there are tons of tutorials on the internet.

1
2
3
4
5
npm install hexo-cli -g
hexo init blog
cd blog
npm install
hexo server

You’ll need several useful plugins to improve your Hexo website, such as to generate RSS feed and XML sitemap.

Push your Hexo website to GitHub

How to use GitHub Pages to host your website for free? Just create a new GitHub Repository. It’s free if the repository is public.

Then Install hexo-deployer-git plugin:

1
$ npm install hexo-deployer-git --save

Edit the setting:

1
2
3
4
5
deploy:
type: git
repo: <repository url>
branch: [branch]
message: [message]

Try to hexo deploy to deploy it to your repository.

Notice that GitHub Pages will build and serve website from one of the branch such as gh-pages or master. So you have to set source in repository settings.

Use HTTPS

Cloudflare provides free SSL to secure your website, all you have to do is:

  • Visit Crypto section in your domain name dashboard
  • In the first panel SSL, turn it on. The option should be “Flexible” or “Full”
  • Make sure your domain name is fully managed by Cloudflare

Auto Deployment using CircleCI

  • Add circle.yml in your repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
checkout:
post:
- git submodule sync
- git submodule update --init

deployment:
production:
branch: master
commands:
- git config --global user.name "Circle CI"
- git config --global user.email "YOUR EMAIL HERE"
- hexo clean
- hexo generate
- hexo deploy

test:
override:
- echo "Passed"
  • In CircleCI, add your project and let it try to build your website.
  • Remember to sync submodule if you use submodule to clone a theme into your repository.
  • You can use something like html-proofer to validate the HTML and find out dead links. However I choose to echo "Passed".
  • You have to generate a pair of SSH key and add it to CircleCI SSH Permission so that CircleCI will be able to access to your GitHub repository for both read and write.

Once the settings are done, the publishing process will be much easier: create a new post and commit it into your repository.

In a Crystal-based project, initialize your heroku app:

1
heroku create --buildpack https://github.com/crystal-lang/heroku-buildpack-crystal.git

If you want to assign the heroku app name:

1
heroku create YOUR_APP_NAME --buildpack https://github.com/crystal-lang/heroku-buildpack-crystal.git

Deploy to heroku:

1
git push heroku master

Open the website/application:

1
heroku open

sed(/usr/bin/sed) on macOS and GNU sed are different. How to install GNU sed:

1
brew install gnu-sed

GNU sed will be located in /usr/local/bin/sed. You can use gsed in terminal.

Or you can install GNU sed by the following command to use GNU sed by default.

1
brew install gnu-sed --with-default-names

There are many ways to remove YAML Front Matter, here is how I use sed to do so:

1
sed -i '1 { /^---/ { :a N; /\n---/! ba; d} }' INPUT_FILE

Explanation:

1
2
3
4
5
6
7
8
9
10
11
1 {              # in the first line
/^---/ { # if it starts with ---
:a # jump label for looping
N # fetch the next line, append to pattern space
/\n---/! ba; # if the result does not contain \n--- (that is, if the last
# fetched line does not begin with ---), go back to :a
d # then delete the whole thing.
}
}
# otherwise drop off the end here and do the default (print
# the line)

Furthermore, if you wanna keep some useful metadata in front matter like post title in blogging system, you can do so:

1
2
3
title=$(grep '^title\:\(.*\)$' $1 | sed -e 's/title\:\s//g')
sed -i -e "/---/a # `echo $title`" $1
sed -i '1 { /^---/ { :a N; /\n---/! ba; d} }' $1

Here is a workaround to insert $title in each --- then remove YAML Front Matter.

References:

0%