New Posts

This commit is contained in:
Brandon Rozek 2020-05-09 11:36:13 -04:00
parent 429bdef86c
commit 6e6033cd12
3 changed files with 292 additions and 0 deletions

View file

@ -0,0 +1,19 @@
---
title: "Show Applications using the Internet"
date: 2020-05-09T11:30:36-04:00
draft: false
tags: []
---
There's a great thread on [ask ubuntu](https://askubuntu.com/questions/104739/which-applications-are-using-internet) on seeing which applications are using the Internet. I'm going to add my own spin on the answers for future reference.
```bash
lsof -i | awk '{print $1}' | uniq | tail -n +2
```
Breaking it down...
- `lsof -i` shows us a lot of information pertaining to processes accessing the Internet.
- `awk '{print $1}'` filters the last output to only shown us the `COMMAND` column.
- `uniq` filters out multiple occurrences of a single application.
- `tail -n +2` removes the first `COMMAND` line.

View file

@ -0,0 +1,228 @@
---
title: "Auto-Deploy Docker Applications"
date: 2020-05-09T10:20:34-04:00
draft: false
tags: []
---
This post will combine that last three posts on [Packer](https://brandonrozek.com/blog/snapshotswithpacker/), [Terraform](https://brandonrozek.com/blog/autodeployterraform/), and their [configuration](https://brandonrozek.com/blog/sharedpackerterraformconfig/) to show an entire example of how to deploy a docker-compose application. We will specifically look at deploying a game called [`minetest`](https://www.minetest.net/) on DigitalOcean, but these principles can be adjusted to deploy your application as well. The entire setup is [documented on Github](https://github.com/Brandon-Rozek/minetest-deploy).
## Shared Config
We're going to use a shared configuration between Packer and Terraform. The template goes like this:
```
base_system_image = "ubuntu-20-04-x64"
region = "nyc3"
size = "512mb"
domain = "brandonrozek.com" # Replace
subdomain = "minetest"
# Secrets
do_token = "DO-TOKEN-HERE" # Replace
key_name = "SSH-NAME-ON-DO-HERE" # Replace
```
We'll also need to define the types of these variables in `variables.hcl`
```
variable "do_token" {
type = string
}
variable "base_system_image" {
type = string
}
variable "domain" {
type = string
}
variable "key_name" {
type = string
}
variable "subdomain" {
type = string
}
variable "region" {
type = string
}
variable "size" {
type = string
}
```
## Packer
Create a packer directory and setup some symbolic links to the share configuration
```bash
mkdir packer && cd packer
ln -s ../config variables.auto.pkrvars.hcl
ln -s ../variables.hcl variables.pkr.hcl
```
Now let's create a script named `setup.sh` that will run on top of our base image. This will install Docker and setup the firewall to allow SSH and Minetest traffic through.
```bash
#!/bin/bash
apt update
apt upgrade -y
apt install -y docker.io docker-compose
systemctl enable docker-compose
systemctl start docker-compose
ufw allow OpenSSH
# Add any firewall rules you need
# for your application here
ufw allow 30000/udp
ufw --force enable
```
The image that we'll use for Minetest comes from [linuxserverio](https://fleet.linuxserver.io/image?name=linuxserver/minetest). To configure docker-compose we'll need a file named `docker-compose.yml`. Its contents will be highly similar to what is listed on their [Github](https://github.com/linuxserver/docker-minetest/blob/466cde1f2fd38278fe69d33ea3b2f42df50e6b16/README.md).
```yml
version: "2.1"
services:
minetest:
image: linuxserver/minetest
container_name: minetest
environment:
- PUID=1000
- PGID=1000
- TZ=US/Eastern
volumes:
- /volumes/minetest/config/.minetest:/config/.minetest
ports:
- 30000:30000/udp
restart: unless-stopped
```
We'll need to create a `systemd` script called `docker-compose.service` for systemd to [enable docker-compose on startup](https://brandonrozek.com/blog/composesystemd/).
```ini
[Unit]
Description=Docker Compose Application Service
Requires=docker.service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/root
ExecStart=/usr/bin/docker-compose up -d
ExecStop=/usr/bin/docker-compose down
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
```
Finally we'll need to write a packer configuration file `do.pkr.hcl` to create our snapshot.
```
source "digitalocean" "web" {
api_token = var.do_token
image = var.base_system_image
region = var.region
size = var.size
ssh_username = "root"
snapshot_name = "packer-docker"
}
build {
sources = [
"source.digitalocean.web",
]
provisioner "file" {
source = "docker-compose.yml"
destination = "/root/docker-compose.yml"
}
provisioner "file" {
source = "docker-compose.service"
destination = "/etc/systemd/system/docker-compose.service"
}
provisioner "shell" {
scripts = [ "setup.sh" ]
}
}
```
To build our image we need to run `packer build .` in the directory with all these files.
## Terraform
Like before, we need to tell terraform where to look for its configuration
```bash
mkdir terraform && cd terraform
ln -s ../config terraform.tfvars
ln -s ../variables.hcl variables.tf
```
To deploy, we only need to create one additional file that will tell digital ocean to create a droplet and assign a subdomain to that droplet.
```
provider "digitalocean" {
token = var.do_token
}
data "digitalocean_ssh_key" laptop {
name = var.key_name
}
data "digitalocean_droplet_snapshot" "packer_snapshot" {
name = "packer-docker"
most_recent = true
}
# Create a droplet
resource "digitalocean_droplet" "web" {
name = "tf-1"
image = data.digitalocean_droplet_snapshot.packer_snapshot.id
region = var.region
size = var.size
ssh_keys = [data.digitalocean_ssh_key.laptop.id]
backups = false
}
# Attach a subdomain
resource "digitalocean_record" "www" {
domain = var.domain
type = "A"
name = var.subdomain
value = digitalocean_droplet.web.ipv4_address
}
output "ip" {
value = digitalocean_droplet.web.ipv4_address
}
output "domain" {
value = "${digitalocean_record.www.name}.${digitalocean_record.www.domain}"
}
```
To deploy run
```bash
terraform apply
```
To later take down the minetest server, run
```bash
terraform destroy
```
## Conclusion
This method can be easily configured to run whichever docker services you'd like. All you have to do is edit the `packer/docker.compose.yml` file and `packer/setup.sh` to setup the firewall rules.

View file

@ -0,0 +1,45 @@
---
title: "Git Line Endings"
date: 2020-05-09T11:01:21-04:00
draft: false
tags: ["git"]
---
if you have worked with a team that has a mix of Windows and Linux developers, you might have noticed pull requests where Git reports changes in a file that is not visible. One explanation is that the line endings might have changed.
Due to historical reasons Windows uses a line ending called CRLF which stands for carriage return + line feed. This dates back to type writers where once you hit the line feed button to push the paper up, you need to press the carriage return button to move back to the left side of the paper. [Wikipedia](https://en.wikipedia.org/wiki/Carriage_return) has more of a history if you want to learn more.
Meanwhile, Linux only uses LF or line feed for its ending. We can configure a repository with a `.gitattributes` file to automatically convert CRLF to LF line endings in order not to see these frivolous changes in our repository.
The `.gitattributes` file looks like this
```
# Automatically convert CRLF->LF on files git thinks are text
* text=auto
# Explicitly declare what are text files
*.py text
*.js text
# Denote files that are binary and should not be modified.
*.png binary
*.jpg binary
```
Make sure that you don't have any working changes that need to be committed as the following commands will convert all the line endings for text files in your repository to LF.
First let us commit the git attributes file
```bash
git add .gitattributes
git commit -m "Added gitattributes file"
```
Then let us convert all the line endings to LF
```bash
git add --renormalize .
git commit -m "Convert line endings to LF"
```
Then tell your coworkers to expect a large commit which only line ending changes....