forked from vieter-v/vieter
				
			refactor: moved client code into own module
							parent
							
								
									f42d3fd8b0
								
							
						
					
					
						commit
						407b226955
					
				|  | @ -3,9 +3,9 @@ module build | ||||||
| import docker | import docker | ||||||
| import encoding.base64 | import encoding.base64 | ||||||
| import time | import time | ||||||
| import git |  | ||||||
| import os | import os | ||||||
| import db | import db | ||||||
|  | import client | ||||||
| 
 | 
 | ||||||
| const container_build_dir = '/build' | const container_build_dir = '/build' | ||||||
| 
 | 
 | ||||||
|  | @ -126,7 +126,7 @@ fn build(conf Config) ? { | ||||||
| 	build_arch := os.uname().machine | 	build_arch := os.uname().machine | ||||||
| 
 | 
 | ||||||
| 	// We get the repos map from the Vieter instance | 	// We get the repos map from the Vieter instance | ||||||
| 	repos := git.get_repos(conf.address, conf.api_key) ? | 	repos := client.new(conf.address, conf.api_key).get_git_repos() ? | ||||||
| 
 | 
 | ||||||
| 	// We filter out any repos that aren't allowed to be built on this | 	// We filter out any repos that aren't allowed to be built on this | ||||||
| 	// architecture | 	// architecture | ||||||
|  |  | ||||||
|  | @ -0,0 +1,40 @@ | ||||||
|  | module client | ||||||
|  | 
 | ||||||
|  | import net.http | ||||||
|  | import response { Response } | ||||||
|  | import json | ||||||
|  | 
 | ||||||
|  | pub struct Client { | ||||||
|  | pub: | ||||||
|  | 	address string | ||||||
|  | 	api_key string | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub fn new(address string, api_key string) Client { | ||||||
|  | 	return Client{ | ||||||
|  | 		address: address | ||||||
|  | 		api_key: api_key | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // send_request<T> is a convenience method for sending requests to the repos | ||||||
|  | // API. It mostly does string manipulation to create a query string containing | ||||||
|  | // the provided params. | ||||||
|  | fn (c &Client) send_request<T>(method http.Method, url string, params map[string]string) ?Response<T> { | ||||||
|  | 	mut full_url := '${c.address}$url' | ||||||
|  | 
 | ||||||
|  | 	if params.len > 0 { | ||||||
|  | 		params_str := params.keys().map('$it=${params[it]}').join('&') | ||||||
|  | 
 | ||||||
|  | 		full_url = '$full_url?$params_str' | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	mut req := http.new_request(method, full_url, '') ? | ||||||
|  | 	req.add_custom_header('X-API-Key', c.api_key) ? | ||||||
|  | 
 | ||||||
|  | 	res := req.do() ? | ||||||
|  | 	data := json.decode(Response<T>, res.text) ? | ||||||
|  | 
 | ||||||
|  | 	return data | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | @ -0,0 +1,51 @@ | ||||||
|  | module client | ||||||
|  | 
 | ||||||
|  | import db | ||||||
|  | import net.http | ||||||
|  | import response { Response } | ||||||
|  | 
 | ||||||
|  | // get_repos returns the current list of repos. | ||||||
|  | pub fn (c &Client) get_git_repos() ?[]db.GitRepo { | ||||||
|  | 	data := c.send_request<[]db.GitRepo>(http.Method.get, '/api/repos', {}) ? | ||||||
|  | 
 | ||||||
|  | 	return data.data | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // get_repo returns the repo for a specific ID. | ||||||
|  | pub fn (c &Client) get_git_repo(id int) ?db.GitRepo { | ||||||
|  | 	data := c.send_request<db.GitRepo>(http.Method.get, '/api/repos/$id', {}) ? | ||||||
|  | 
 | ||||||
|  | 	return data.data | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // add_repo adds a new repo to the server. | ||||||
|  | pub fn (c &Client) add_git_repo(url string, branch string, repo string, arch []string) ?Response<string> { | ||||||
|  | 	mut params := { | ||||||
|  | 		'url':    url | ||||||
|  | 		'branch': branch | ||||||
|  | 		'repo':   repo | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if arch.len > 0 { | ||||||
|  | 		params['arch'] = arch.join(',') | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	data := c.send_request<string>(http.Method.post, '/api/repos', params) ? | ||||||
|  | 
 | ||||||
|  | 	return data | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // remove_repo removes the repo with the given ID from the server. | ||||||
|  | pub fn (c &Client) remove_git_repo(id int) ?Response<string> { | ||||||
|  | 	data := c.send_request<string>(http.Method.delete, '/api/repos/$id', {}) ? | ||||||
|  | 
 | ||||||
|  | 	return data | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // patch_repo sends a PATCH request to the given repo with the params as | ||||||
|  | // payload. | ||||||
|  | pub fn (c &Client) patch_git_repo(id int, params map[string]string) ?Response<string> { | ||||||
|  | 	data := c.send_request<string>(http.Method.patch, '/api/repos/$id', params) ? | ||||||
|  | 
 | ||||||
|  | 	return data | ||||||
|  | } | ||||||
|  | @ -77,7 +77,7 @@ fn (mut d Daemon) run_build(build_index int, sb ScheduledBuild) { | ||||||
| 	// 0 means success, 1 means failure | 	// 0 means success, 1 means failure | ||||||
| 	mut status := 0 | 	mut status := 0 | ||||||
| 
 | 
 | ||||||
| 	build.build_repo(d.address, d.api_key, d.builder_images.last(), &sb.repo) or { | 	build.build_repo(d.client.address, d.client.api_key, d.builder_images.last(), &sb.repo) or { | ||||||
| 		d.ldebug('build_repo error: $err.msg()') | 		d.ldebug('build_repo error: $err.msg()') | ||||||
| 		status = 1 | 		status = 1 | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -1,6 +1,5 @@ | ||||||
| module daemon | module daemon | ||||||
| 
 | 
 | ||||||
| import git |  | ||||||
| import time | import time | ||||||
| import log | import log | ||||||
| import datatypes { MinHeap } | import datatypes { MinHeap } | ||||||
|  | @ -10,6 +9,7 @@ import build | ||||||
| import docker | import docker | ||||||
| import db | import db | ||||||
| import os | import os | ||||||
|  | import client | ||||||
| 
 | 
 | ||||||
| const ( | const ( | ||||||
| 	// How many seconds to wait before retrying to update API if failed | 	// How many seconds to wait before retrying to update API if failed | ||||||
|  | @ -31,8 +31,7 @@ fn (r1 ScheduledBuild) < (r2 ScheduledBuild) bool { | ||||||
| 
 | 
 | ||||||
| pub struct Daemon { | pub struct Daemon { | ||||||
| mut: | mut: | ||||||
| 	address                 string | 	client client.Client | ||||||
| 	api_key                 string |  | ||||||
| 	base_image              string | 	base_image              string | ||||||
| 	builder_images          []string | 	builder_images          []string | ||||||
| 	global_schedule         CronExpression | 	global_schedule         CronExpression | ||||||
|  | @ -56,8 +55,7 @@ mut: | ||||||
| // populates the build queue for the first time. | // populates the build queue for the first time. | ||||||
| pub fn init_daemon(logger log.Log, address string, api_key string, base_image string, global_schedule CronExpression, max_concurrent_builds int, api_update_frequency int, image_rebuild_frequency int) ?Daemon { | pub fn init_daemon(logger log.Log, address string, api_key string, base_image string, global_schedule CronExpression, max_concurrent_builds int, api_update_frequency int, image_rebuild_frequency int) ?Daemon { | ||||||
| 	mut d := Daemon{ | 	mut d := Daemon{ | ||||||
| 		address: address | 		client: client.new(address, api_key) | ||||||
| 		api_key: api_key |  | ||||||
| 		base_image: base_image | 		base_image: base_image | ||||||
| 		global_schedule: global_schedule | 		global_schedule: global_schedule | ||||||
| 		api_update_frequency: api_update_frequency | 		api_update_frequency: api_update_frequency | ||||||
|  | @ -180,7 +178,7 @@ fn (mut d Daemon) schedule_build(repo db.GitRepo) { | ||||||
| fn (mut d Daemon) renew_repos() { | fn (mut d Daemon) renew_repos() { | ||||||
| 	d.linfo('Renewing repos...') | 	d.linfo('Renewing repos...') | ||||||
| 
 | 
 | ||||||
| 	mut new_repos := git.get_repos(d.address, d.api_key) or { | 	mut new_repos := d.client.get_git_repos() or { | ||||||
| 		d.lerror('Failed to renew repos. Retrying in ${daemon.api_update_retry_timeout}s...') | 		d.lerror('Failed to renew repos. Retrying in ${daemon.api_update_retry_timeout}s...') | ||||||
| 		d.api_update_timestamp = time.now().add_seconds(daemon.api_update_retry_timeout) | 		d.api_update_timestamp = time.now().add_seconds(daemon.api_update_retry_timeout) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -3,6 +3,7 @@ module git | ||||||
| import cli | import cli | ||||||
| import env | import env | ||||||
| import cron.expression { parse_expression } | import cron.expression { parse_expression } | ||||||
|  | import client | ||||||
| 
 | 
 | ||||||
| struct Config { | struct Config { | ||||||
| 	address string [required] | 	address string [required] | ||||||
|  | @ -119,7 +120,8 @@ pub fn cmd() cli.Command { | ||||||
| 
 | 
 | ||||||
| // list prints out a list of all repositories. | // list prints out a list of all repositories. | ||||||
| fn list(conf Config) ? { | fn list(conf Config) ? { | ||||||
| 	repos := get_repos(conf.address, conf.api_key) ? | 	c := client.new(conf.address, conf.api_key) | ||||||
|  | 	repos := c.get_git_repos() ? | ||||||
| 
 | 
 | ||||||
| 	for repo in repos { | 	for repo in repos { | ||||||
| 		println('$repo.id\t$repo.url\t$repo.branch\t$repo.repo') | 		println('$repo.id\t$repo.url\t$repo.branch\t$repo.repo') | ||||||
|  | @ -128,7 +130,8 @@ fn list(conf Config) ? { | ||||||
| 
 | 
 | ||||||
| // add adds a new repository to the server's list. | // add adds a new repository to the server's list. | ||||||
| fn add(conf Config, url string, branch string, repo string) ? { | fn add(conf Config, url string, branch string, repo string) ? { | ||||||
| 	res := add_repo(conf.address, conf.api_key, url, branch, repo, []) ? | 	c := client.new(conf.address, conf.api_key) | ||||||
|  | 	res := c.add_git_repo(url, branch, repo, []) ? | ||||||
| 
 | 
 | ||||||
| 	println(res.message) | 	println(res.message) | ||||||
| } | } | ||||||
|  | @ -139,7 +142,8 @@ fn remove(conf Config, id string) ? { | ||||||
| 	id_int := id.int() | 	id_int := id.int() | ||||||
| 
 | 
 | ||||||
| 	if id_int != 0 { | 	if id_int != 0 { | ||||||
| 		res := remove_repo(conf.address, conf.api_key, id_int) ? | 		c := client.new(conf.address, conf.api_key) | ||||||
|  | 		res := c.remove_git_repo(id_int) ? | ||||||
| 		println(res.message) | 		println(res.message) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | @ -156,7 +160,9 @@ fn patch(conf Config, id string, params map[string]string) ? { | ||||||
| 
 | 
 | ||||||
| 	id_int := id.int() | 	id_int := id.int() | ||||||
| 	if id_int != 0 { | 	if id_int != 0 { | ||||||
| 		res := patch_repo(conf.address, conf.api_key, id_int, params) ? | 
 | ||||||
|  | 		c := client.new(conf.address, conf.api_key) | ||||||
|  | 		res := c.patch_git_repo(id_int, params) ? | ||||||
| 
 | 
 | ||||||
| 		println(res.message) | 		println(res.message) | ||||||
| 	} | 	} | ||||||
|  | @ -170,6 +176,7 @@ fn info(conf Config, id string) ? { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	repo := get_repo(conf.address, conf.api_key, id_int) ? | 		c := client.new(conf.address, conf.api_key) | ||||||
|  | 	repo := c.get_git_repo(id_int) ? | ||||||
| 	println(repo) | 	println(repo) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,77 +0,0 @@ | ||||||
| module git |  | ||||||
| 
 |  | ||||||
| import json |  | ||||||
| import response { Response } |  | ||||||
| import net.http |  | ||||||
| import db |  | ||||||
| 
 |  | ||||||
| // send_request<T> is a convenience method for sending requests to the repos |  | ||||||
| // API. It mostly does string manipulation to create a query string containing |  | ||||||
| // the provided params. |  | ||||||
| fn send_request<T>(method http.Method, address string, url string, api_key string, params map[string]string) ?Response<T> { |  | ||||||
| 	mut full_url := '$address$url' |  | ||||||
| 
 |  | ||||||
| 	if params.len > 0 { |  | ||||||
| 		params_str := params.keys().map('$it=${params[it]}').join('&') |  | ||||||
| 
 |  | ||||||
| 		full_url = '$full_url?$params_str' |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	mut req := http.new_request(method, full_url, '') ? |  | ||||||
| 	req.add_custom_header('X-API-Key', api_key) ? |  | ||||||
| 
 |  | ||||||
| 	res := req.do() ? |  | ||||||
| 	data := json.decode(Response<T>, res.text) ? |  | ||||||
| 
 |  | ||||||
| 	return data |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // get_repos returns the current list of repos. |  | ||||||
| pub fn get_repos(address string, api_key string) ?[]db.GitRepo { |  | ||||||
| 	data := send_request<[]db.GitRepo>(http.Method.get, address, '/api/repos', api_key, |  | ||||||
| 		{}) ? |  | ||||||
| 
 |  | ||||||
| 	return data.data |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // get_repo returns the repo for a specific ID. |  | ||||||
| pub fn get_repo(address string, api_key string, id int) ?db.GitRepo { |  | ||||||
| 	data := send_request<db.GitRepo>(http.Method.get, address, '/api/repos/$id', api_key, |  | ||||||
| 		{}) ? |  | ||||||
| 
 |  | ||||||
| 	return data.data |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // add_repo adds a new repo to the server. |  | ||||||
| pub fn add_repo(address string, api_key string, url string, branch string, repo string, arch []string) ?Response<string> { |  | ||||||
| 	mut params := { |  | ||||||
| 		'url':    url |  | ||||||
| 		'branch': branch |  | ||||||
| 		'repo':   repo |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	if arch.len > 0 { |  | ||||||
| 		params['arch'] = arch.join(',') |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	data := send_request<string>(http.Method.post, address, '/api/repos', api_key, params) ? |  | ||||||
| 
 |  | ||||||
| 	return data |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // remove_repo removes the repo with the given ID from the server. |  | ||||||
| pub fn remove_repo(address string, api_key string, id int) ?Response<string> { |  | ||||||
| 	data := send_request<string>(http.Method.delete, address, '/api/repos/$id', api_key, |  | ||||||
| 		{}) ? |  | ||||||
| 
 |  | ||||||
| 	return data |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // patch_repo sends a PATCH request to the given repo with the params as |  | ||||||
| // payload. |  | ||||||
| pub fn patch_repo(address string, api_key string, id int, params map[string]string) ?Response<string> { |  | ||||||
| 	data := send_request<string>(http.Method.patch, address, '/api/repos/$id', api_key, |  | ||||||
| 		params) ? |  | ||||||
| 
 |  | ||||||
| 	return data |  | ||||||
| } |  | ||||||
		Loading…
	
		Reference in New Issue