forked from vieter-v/vieter
				
			Merge pull request 'Add `vieter repos build` command; remove healthcheck' (#184) from Chewing_Bever/vieter:repos-build into dev
Reviewed-on: vieter/vieter#184main
						commit
						29e6d8d071
					
				| 
						 | 
				
			
			@ -13,6 +13,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 | 
			
		|||
* CLI commands to access build logs API
 | 
			
		||||
* Cron build logs are uploaded to above API
 | 
			
		||||
* Proper ASCII table output in CLI
 | 
			
		||||
* `vieter repos build id` command to run builds locally
 | 
			
		||||
 | 
			
		||||
### Removed
 | 
			
		||||
 | 
			
		||||
* `vieter build` command
 | 
			
		||||
    * This command was used alongside cron for periodic builds, but this has
 | 
			
		||||
      been replaced by `vieter cron`
 | 
			
		||||
 | 
			
		||||
### Changed
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -36,15 +36,8 @@ ENV PATH=/bin \
 | 
			
		|||
 | 
			
		||||
COPY --from=builder /app/dumb-init /app/vieter /bin/
 | 
			
		||||
 | 
			
		||||
HEALTHCHECK --interval=30s \
 | 
			
		||||
    --timeout=3s \
 | 
			
		||||
    --start-period=5s \
 | 
			
		||||
    CMD /bin/wget --spider http://localhost:8000/health || exit 1
 | 
			
		||||
 | 
			
		||||
RUN mkdir /data && \
 | 
			
		||||
    chown -R www-data:www-data /data && \
 | 
			
		||||
    mkdir -p '/var/spool/cron/crontabs' && \
 | 
			
		||||
    echo '0 3 * * * /bin/vieter build' | crontab - 
 | 
			
		||||
    chown -R www-data:www-data /data
 | 
			
		||||
 | 
			
		||||
WORKDIR /data
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -56,6 +56,11 @@ Vieter only supports uploading archives compressed using either gzip, zstd or
 | 
			
		|||
xz at the moment.
 | 
			
		||||
{{< /hint >}}
 | 
			
		||||
 | 
			
		||||
### `GET /health`
 | 
			
		||||
 | 
			
		||||
This endpoint's only use is to be used with healthchecks. It returns a JSON
 | 
			
		||||
response with the message "Healthy.".
 | 
			
		||||
 | 
			
		||||
## API
 | 
			
		||||
 | 
			
		||||
All API routes require the API key to provided using the `X-Api-Key` header.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,7 +5,6 @@ import encoding.base64
 | 
			
		|||
import time
 | 
			
		||||
import os
 | 
			
		||||
import db
 | 
			
		||||
import client
 | 
			
		||||
import strings
 | 
			
		||||
import util
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -155,31 +154,3 @@ pub fn build_repo(address string, api_key string, base_image_id string, repo &db
 | 
			
		|||
		logs: logs_builder.str()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// build builds every Git repo in the server's list.
 | 
			
		||||
fn build(conf Config, repo_id int) ? {
 | 
			
		||||
	c := client.new(conf.address, conf.api_key)
 | 
			
		||||
	repo := c.get_git_repo(repo_id)?
 | 
			
		||||
 | 
			
		||||
	build_arch := os.uname().machine
 | 
			
		||||
 | 
			
		||||
	println('Creating base image...')
 | 
			
		||||
	image_id := create_build_image(conf.base_image)?
 | 
			
		||||
 | 
			
		||||
	println('Running build...')
 | 
			
		||||
	res := build_repo(conf.address, conf.api_key, image_id, repo)?
 | 
			
		||||
 | 
			
		||||
	println('Removing build image...')
 | 
			
		||||
 | 
			
		||||
	mut dd := docker.new_conn()?
 | 
			
		||||
 | 
			
		||||
	defer {
 | 
			
		||||
		dd.close() or {}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dd.remove_image(image_id)?
 | 
			
		||||
 | 
			
		||||
	println('Uploading logs to Vieter...')
 | 
			
		||||
	c.add_build_log(repo.id, res.start_time, res.end_time, build_arch, res.exit_code,
 | 
			
		||||
		res.logs)?
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,29 +0,0 @@
 | 
			
		|||
module build
 | 
			
		||||
 | 
			
		||||
import cli
 | 
			
		||||
import env
 | 
			
		||||
 | 
			
		||||
pub struct Config {
 | 
			
		||||
pub:
 | 
			
		||||
	api_key    string
 | 
			
		||||
	address    string
 | 
			
		||||
	base_image string = 'archlinux:base-devel'
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// cmd returns the cli submodule that handles the build process
 | 
			
		||||
pub fn cmd() cli.Command {
 | 
			
		||||
	return cli.Command{
 | 
			
		||||
		name: 'build'
 | 
			
		||||
		required_args: 1
 | 
			
		||||
		usage: 'id'
 | 
			
		||||
		description: 'Build the repository with the given ID.'
 | 
			
		||||
		execute: fn (cmd cli.Command) ? {
 | 
			
		||||
			config_file := cmd.flags.get_string('config-file')?
 | 
			
		||||
			conf := env.load<Config>(config_file)?
 | 
			
		||||
 | 
			
		||||
			id := cmd.args[0].int()
 | 
			
		||||
 | 
			
		||||
			build(conf, id)?
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,34 @@
 | 
			
		|||
module git
 | 
			
		||||
 | 
			
		||||
import client
 | 
			
		||||
import docker
 | 
			
		||||
import os
 | 
			
		||||
import build
 | 
			
		||||
 | 
			
		||||
// build builds every Git repo in the server's list.
 | 
			
		||||
fn build(conf Config, repo_id int) ? {
 | 
			
		||||
	c := client.new(conf.address, conf.api_key)
 | 
			
		||||
	repo := c.get_git_repo(repo_id)?
 | 
			
		||||
 | 
			
		||||
	build_arch := os.uname().machine
 | 
			
		||||
 | 
			
		||||
	println('Creating base image...')
 | 
			
		||||
	image_id := build.create_build_image(conf.base_image)?
 | 
			
		||||
 | 
			
		||||
	println('Running build...')
 | 
			
		||||
	res := build.build_repo(conf.address, conf.api_key, image_id, repo)?
 | 
			
		||||
 | 
			
		||||
	println('Removing build image...')
 | 
			
		||||
 | 
			
		||||
	mut dd := docker.new_conn()?
 | 
			
		||||
 | 
			
		||||
	defer {
 | 
			
		||||
		dd.close() or {}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dd.remove_image(image_id)?
 | 
			
		||||
 | 
			
		||||
	println('Uploading logs to Vieter...')
 | 
			
		||||
	c.add_build_log(repo.id, res.start_time, res.end_time, build_arch, res.exit_code,
 | 
			
		||||
		res.logs)?
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -7,8 +7,9 @@ import client
 | 
			
		|||
import console
 | 
			
		||||
 | 
			
		||||
struct Config {
 | 
			
		||||
	address string [required]
 | 
			
		||||
	api_key string [required]
 | 
			
		||||
	address    string [required]
 | 
			
		||||
	api_key    string [required]
 | 
			
		||||
	base_image string = 'archlinux:base-devel'
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// cmd returns the cli submodule that handles the repos API interaction
 | 
			
		||||
| 
						 | 
				
			
			@ -112,6 +113,18 @@ pub fn cmd() cli.Command {
 | 
			
		|||
					patch(conf, cmd.args[0], params)?
 | 
			
		||||
				}
 | 
			
		||||
			},
 | 
			
		||||
			cli.Command{
 | 
			
		||||
				name: 'build'
 | 
			
		||||
				required_args: 1
 | 
			
		||||
				usage: 'id'
 | 
			
		||||
				description: 'Build the repo with the given id & publish it.'
 | 
			
		||||
				execute: fn (cmd cli.Command) ? {
 | 
			
		||||
					config_file := cmd.flags.get_string('config-file')?
 | 
			
		||||
					conf := env.load<Config>(config_file)?
 | 
			
		||||
 | 
			
		||||
					build(conf, cmd.args[0].int())?
 | 
			
		||||
				}
 | 
			
		||||
			},
 | 
			
		||||
		]
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,7 +3,6 @@ module main
 | 
			
		|||
import os
 | 
			
		||||
import server
 | 
			
		||||
import cli
 | 
			
		||||
import build
 | 
			
		||||
import console.git
 | 
			
		||||
import console.logs
 | 
			
		||||
import cron
 | 
			
		||||
| 
						 | 
				
			
			@ -25,7 +24,6 @@ fn main() {
 | 
			
		|||
		]
 | 
			
		||||
		commands: [
 | 
			
		||||
			server.cmd(),
 | 
			
		||||
			build.cmd(),
 | 
			
		||||
			git.cmd(),
 | 
			
		||||
			cron.cmd(),
 | 
			
		||||
			logs.cmd(),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue