Compare commits

...

4 Commits

9 changed files with 46 additions and 215 deletions

View File

@ -1 +0,0 @@
export PATH="$PATH:$PWD/scripts/bin"

View File

@ -1,2 +0,0 @@
cd ./scripts
go build -o ./bin/inv-alias ./inv-alias.go

View File

@ -1 +1,2 @@
sudo apt-get -y install virtualbox vagrant ansible
#!/usr/bin/env bash
sudo apt-get -y install virtualbox vagrant ansible

View File

@ -1,6 +1,6 @@
---
# handlers file for nginx-webserver
- name: restart nginx
- name: reload nginx
ansible.builtin.service:
name: nginx
state: restarted

View File

@ -1,14 +1,35 @@
---
# tasks file for nginx-webserver
- name: Install nginx
- name: Install nginx and php
package:
name: nginx
name:
- nginx
- php7.4
- php7.4-fpm
- php7.4-cli
state: present
update_cache: yes
become: true
notify: restart nginx
- name: Copy over index.html
- name: Copy over nginx.conf
ansible.builtin.template:
src: ./templates/index.html.j2
dest: /var/www/html/index.html
src: ./templates/nginx.cfg.j2
dest: /etc/nginx/sites-available/nginx.cfg
become: true
notify: reload nginx
- name: Enable nginx.conf
file:
src: /etc/nginx/sites-available/nginx.cfg
dest: /etc/nginx/sites-enabled/default
state: link
become: true
notify: reload nginx
- name: Remove nginx default crap
file:
state: absent
path: /var/www/html/*
become: true
- name: Copy over index.php
ansible.builtin.template:
src: ./templates/index.php.j2
dest: /var/www/html/index.php
become: true

View File

@ -8,6 +8,7 @@
</head>
<body>
<h1><?php echo 'Hello, World!'; ?></h1>
<p>Hostname: {{ ansible_facts.nodename }}</p>
<p>OS: {{ ansible_facts.distribution }} {{ ansible_facts.distribution_version }}</p>
<p>Kernel: {{ ansible_facts.kernel }}</p>

View File

@ -0,0 +1,15 @@
server {
listen 80;
root /var/www/html;
index index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}

View File

@ -1,54 +0,0 @@
#!/usr/bin/env bash
VERSION="0.1.0"
function help() {
echo -e \
"Usage: $(basename $0) [OPTIONS] [COMMAND]\n\n" \
"Options:\n" \
" -i, --inv-file <path> Specify the Ansible inventory to add.\n" \
" -h, --help Show help.\n" \
" -v, --version Show version."
}
if [[ $# -eq 0 ]]; then
help
exit 1
fi
INVENTORY_FILE="$(pwd)/inventory"
while [[ $# -gt 0 ]]; do
case $1 in
-i|--inv-file)
INVENTORY_FILE="$2"
shift
shift
;;
-h|--help)
help
exit 1
;;
-v|--version)
echo $VERSION
exit 1
;;
-*|--*)
echo "hosto: unrecognized option '$1'"
help
exit 1
;;
*)
break
;;
esac
done
if [ -f $INVENTORY_FILE ]; then
sudo inv-alias add $INVENTORY_FILE
eval $@
sudo inv-alias rm $INVENTORY_FILE
else
echo "hosto: Could not find inventory file at $INVENTORY_FILE"
eval $@
fi

View File

@ -1,150 +0,0 @@
package main
import (
"bufio"
"fmt"
"io"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
type AliasMap map[string]string
const (
HostsFile string = "/etc/hosts"
)
func FixedSplit(s, sep string, parts int) []string {
n := make([]string, parts)
p := strings.SplitN(s, sep, parts)
copy(n, p)
return n
}
func IsLegalLine(line string) bool {
c := line[0]
return c != '[' && c != '#'
}
func BuildRegionString(regionName string, aliases AliasMap) string {
b := strings.Builder{}
b.WriteString("#region ")
b.WriteString(regionName)
b.WriteString("\n")
for ip, alias := range aliases {
b.WriteString(ip)
b.WriteString("\t")
b.WriteString(alias)
b.WriteString("\n")
}
b.WriteString("#endregion")
return b.String()
}
func BuildRegionRegexp(regionName string) *regexp.Regexp {
b := strings.Builder{}
b.WriteString("(?s)\n#region ")
b.WriteString(regexp.QuoteMeta(regionName))
b.WriteString(".*#endregion")
r := regexp.MustCompile(b.String())
return r
}
func ScanAliases(fileReader io.Reader) (AliasMap, error) {
aliasMap := AliasMap{}
scanner := bufio.NewScanner(fileReader)
for scanner.Scan() {
line := scanner.Text()
if IsLegalLine(line) {
s := FixedSplit(line, "#", 2)
ip, alias := strings.TrimSpace(s[0]), strings.TrimSpace(s[1])
if _, ok := aliasMap[ip]; !ok && alias != "" {
aliasMap[ip] = alias
}
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return aliasMap, nil
}
func AddAliases(fileName string) {
file, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
h, err := ScanAliases(file)
if err != nil {
log.Fatal(err)
}
file.Close()
r := BuildRegionRegexp(fileName)
s := BuildRegionString(fileName, h)
content, err := ioutil.ReadFile(HostsFile)
if err != nil {
log.Fatal(err)
}
c := r.ReplaceAllString(string(content), s)
if !r.MatchString(c) {
c += ("\n" + s)
}
err = os.WriteFile(HostsFile, []byte(c[:]), fs.FileMode(os.O_WRONLY|os.O_TRUNC))
if err != nil {
log.Fatal(err)
}
}
func RemoveAliases(fileName string) {
regionReg := BuildRegionRegexp(fileName)
content, err := ioutil.ReadFile(HostsFile)
if err != nil {
log.Fatal(err)
}
c := regionReg.ReplaceAll(content, []byte(""))
err = os.WriteFile(HostsFile, c, fs.FileMode(os.O_WRONLY|os.O_TRUNC))
if err != nil {
log.Fatal(err)
}
}
func main() {
u := fmt.Sprintf("Please use: %s <add|rm> <file:path>\n", os.Args[0])
if len(os.Args) < 3 {
fmt.Println(u)
os.Exit(1)
}
p, err := filepath.Abs(os.Args[2])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
switch os.Args[1] {
case "add":
AddAliases(p)
case "rm":
RemoveAliases(p)
default:
fmt.Println(u)
os.Exit(1)
}
}