Implement the error step which will terminate the pipeline

This isn't yet changing the status of the pipeline however
This commit is contained in:
R Tyler Croy 2020-10-25 15:22:38 -07:00
parent a37d459e22
commit 1bf172490a
6 changed files with 109 additions and 3 deletions

View File

@ -29,7 +29,7 @@ async fn handle_request(mut req: tide::Request<State>) -> tide::Result {
pub async fn run(sender: Sender<Request>) -> tide::Result<()> {
info!("Starting the agent control server");
let sock = "agent.sock";
let sock = agent_socket();
let state = State { sender };
let mut app = tide::with_state(state);
@ -48,3 +48,22 @@ pub async fn run(sender: Sender<Request>) -> tide::Result<()> {
Ok(())
}
/**
* Return a string representing the absolute path of this agent's control socket
*/
pub fn agent_socket() -> String {
let path = std::env::current_dir().expect("Failed to get current directory");
path.join("agent.sock").to_string_lossy().into_owned()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_agent_sock() {
let buf = agent_socket();
assert!(buf.ends_with("agent.sock"));
}
}

View File

@ -161,6 +161,9 @@ pub fn run(
let mut file = NamedTempFile::new()?;
let mut step_args = HashMap::new();
// TODO: This is going to be wrong on nested steps
let sock = Value::String(control::agent_socket());
step_args.insert("control", &sock);
step_args.insert("parameters", &step.parameters);
serde_yaml::to_writer(&mut file, &step_args)

View File

@ -14,7 +14,7 @@ use ottoagent::*;
* If the number of pending messages exceeds this number, the requests to the
* control socket will block until the pending messages are cleared out
*/
const MAX_CONTROl_MSGS: usize = 64;
const MAX_CONTROL_MSGS: usize = 64;
#[async_std::main]
async fn main() -> std::io::Result<()> {
@ -27,7 +27,7 @@ async fn main() -> std::io::Result<()> {
}
let file = File::open(&args[1])?;
let (sender, receiver) = channel(MAX_CONTROl_MSGS);
let (sender, receiver) = channel(MAX_CONTROL_MSGS);
match serde_yaml::from_reader::<File, Pipeline>(file) {
Err(e) => {

3
stdlib/error/README.adoc Normal file
View File

@ -0,0 +1,3 @@
= error
The error step will simply abort the pipeline early

62
stdlib/error/error-step Executable file
View File

@ -0,0 +1,62 @@
#!/usr/bin/env ruby
require 'net/http'
require 'json'
require 'yaml'
data = YAML.load(File.read(ARGV.first))
control = data['control']
if control.nil?
puts "Did not get a control socket! `error` won't work without it!"
exit 1
end
# Originally sourced from https://github.com/puppetlabs/net_http_unix under the
# Apache 2.0 License
class HTTPUnix < Net::HTTP
BufferedIO = ::Net::BufferedIO
UNIX_REGEXP = %r{^unix://}i
def initialize(address, port=nil)
super(address, port)
case address
when UNIX_REGEXP
@socket_type = 'unix'
@socket_path = address.sub(UNIX_REGEXP, '')
# Address and port are set to localhost so the HTTP client constructs
# a HOST request header nginx will accept.
@address = 'localhost'
@port = 80
else
@socket_type = 'inet'
end
end
def connect
if @socket_type == 'unix'
connect_unix
else
super
end
end
##
# connect_unix is an alternative implementation of Net::HTTP#connect specific
# to the use case of using a Unix Domain Socket.
def connect_unix
D "opening connection to #{@socket_path}..."
s = Timeout.timeout(@open_timeout) { UNIXSocket.open(@socket_path) }
D "opened"
@socket = BufferedIO.new(s)
@socket.read_timeout = @read_timeout
@socket.continue_timeout = @continue_timeout
@socket.debug_output = @debug_output
on_connect
end
end
req = Net::HTTP::Post.new('/control', 'Content-Type' => 'application/json')
req.body = {:type => :Terminate}.to_json
client = HTTPUnix.new("unix://#{control}")
client.request(req)

19
stdlib/error/manifest.yml Normal file
View File

@ -0,0 +1,19 @@
---
symbol: error
description: |
The `error` step is a simple step that exits the pipeline
includes:
- name: ./error-step
- name: ./README.adoc
entrypoint:
path: error-step
multiarch: true
parameters:
- name: message
required: true
type: string
description: |
The message to print when exiting the pipelin