-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
324 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/srv/hosty/ | ||
services/ | ||
db-foo/ | ||
compose.yaml | ||
... | ||
app-foo/ | ||
compose.yaml | ||
source.yaml | ||
Caddyfile | ||
... | ||
backups/ | ||
db-foo/ | ||
yyyy-mm-dd_hh-mm-ss.sql.gz | ||
... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Role } from './types.js' | ||
|
||
export function add_condition(role: Role, condition: string): Role { | ||
for (const item of [...role.tasks, ...role.handlers]) { | ||
if (item.when) item.when = `(${item.when}) and (${condition})` | ||
else item.when = condition | ||
} | ||
return role | ||
} | ||
|
||
export function merge(roles: Role[]): Role { | ||
return { | ||
tasks: roles.flatMap((x) => x.tasks), | ||
handlers: roles.flatMap((x) => x.handlers), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as YAML from 'yaml' | ||
import { Role } from '../types.js' | ||
import { builtin } from '../tasks/index.js' | ||
import path from 'path' | ||
|
||
type Config = { | ||
repo_url: string | ||
branch: string | ||
service_dir: string | ||
image_name: string | ||
} | ||
|
||
export function build_repo(config: Config): Role { | ||
const source_path = path.join(config.service_dir, 'source.yaml') | ||
const source_content = { repo: config.repo_url, branch: config.branch, commit: '{{commit_hash}}' } | ||
return { | ||
tasks: [ | ||
builtin.command(`Get last commit hash`, { cmd: `git ls-remote ${config.repo_url} ${config.branch}` }, { register: 'git_ls_remote' }), | ||
builtin.set_facts(`Set commit hash in a var`, { commit_hash: `{{git_ls_remote.stdout.split()[0]}}` }), | ||
builtin.copy(`Write the source info`, { content: YAML.stringify(source_content), dest: source_path }, { register: 'source_file' }), | ||
builtin.tempfile(`Create a temp dir to clone the repo`, { state: 'directory' }, { register: 'clone_dir', when: 'source_file.changed' }), | ||
builtin.git( | ||
`Clone the repo`, | ||
{ repo: config.repo_url, version: config.branch, accept_hostkey: true, dest: '{{clone_dir.path}}' }, | ||
{ when: 'source_file.changed' }, | ||
), | ||
builtin.command( | ||
`Build the app using nixpacks`, | ||
{ cmd: `nixpacks build {{clone_dir.path}} --name ${config.image_name}` }, | ||
{ when: 'source_file.changed' }, | ||
), | ||
builtin.file(`Delete clone dir`, { path: '{{clone_dir.path}}', state: 'absent' }, { when: 'source_file.changed' }), | ||
], | ||
handlers: [], | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
export * as assert from './assert.js' | ||
export * from './build_repo.js' | ||
export * from './create_service.js' | ||
export * from './create_hosty_directory.js' | ||
export * from './generate_ssh_key.js' | ||
export * from './install_caddy.js' | ||
export * from './install_docker.js' | ||
export * from './install_git.js' | ||
export * from './install_nixpacks.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Role } from '../types.js' | ||
import { builtin } from '../tasks/index.js' | ||
|
||
export function install_caddy(caddyfiles_pattern: string): Role { | ||
return { | ||
tasks: [ | ||
builtin.apt( | ||
`Install Caddy's dependencies`, | ||
{ name: ['debian-keyring', 'debian-archive-keyring', 'apt-transport-https', 'curl'], state: 'present' }, | ||
{ become: true }, | ||
), | ||
builtin.shell( | ||
`Add Caddy's official GPG key`, | ||
{ | ||
cmd: `curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --batch --yes --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg`, | ||
}, | ||
{ become: true }, | ||
), | ||
builtin.shell( | ||
`Add Caddy's apt repository`, | ||
{ | ||
cmd: `curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list`, | ||
creates: `/etc/apt/sources.list.d/caddy-stable.list`, | ||
}, | ||
{ become: true }, | ||
), | ||
builtin.apt(`Update apt cache`, { update_cache: true }, { become: true }), | ||
builtin.apt(`Install Caddy`, { name: 'caddy', state: 'present' }, { become: true }), | ||
builtin.copy( | ||
`Configure Caddy`, | ||
{ | ||
dest: '/etc/caddy/Caddyfile', | ||
content: `import ${caddyfiles_pattern}\n`, | ||
}, | ||
{ become: true }, | ||
), | ||
builtin.command(`Reload Caddy config`, { cmd: `sudo systemctl reload caddy` }, { become: true }), | ||
], | ||
handlers: [], | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import path from 'path' | ||
import { Role } from '../ansible/types.js' | ||
import { App, AppConfig, Server } from '../types.js' | ||
import { operations, roles } from '../ansible/index.js' | ||
|
||
export function app(config: AppConfig): App { | ||
return { | ||
...config, | ||
get_roles: (server) => get_roles(server, config), | ||
} | ||
} | ||
|
||
function get_roles(server: Server, config: AppConfig): Role[] { | ||
const service_dir = path.join(server.hosty_dir, 'services', config.name) | ||
const compose = config.compose || {} | ||
compose.image = config.name | ||
compose.environment = { ...(config.env || {}), ...(compose.environment || {}) } | ||
compose.expose ||= [] | ||
compose.expose.push('80') | ||
if (config.exposed_port) { | ||
compose.ports ||= [] | ||
compose.ports.push(`${config.exposed_port}:80`) | ||
} | ||
|
||
const steps = [ | ||
roles.build_repo({ repo_url: config.repo, branch: config.branch, service_dir, image_name: config.name }), | ||
operations.add_condition( | ||
roles.create_service({ name: config.name, compose, docker_network: server.docker_network, service_dir }), | ||
'source_file.changed', | ||
), | ||
] | ||
if (config.domain) { | ||
// ... | ||
} | ||
return [operations.merge(steps)] | ||
} |
Oops, something went wrong.