Convert the error-step to a simple simple simple rust program

This commit is contained in:
R Tyler Croy 2020-10-28 10:28:25 -07:00
parent 05139a9fbe
commit 842248b29e
6 changed files with 41 additions and 65 deletions

8
Cargo.lock generated
View File

@ -595,6 +595,14 @@ dependencies = [
"termcolor 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "error-step"
version = "0.1.0"
dependencies = [
"otto-agent 0.1.0",
"serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "event-listener"
version = "2.5.1"

View File

@ -16,6 +16,7 @@ members = [
"osp",
"stdlib/dir",
"stdlib/error",
"stdlib/sh",
]

9
stdlib/error/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "error-step"
version = "0.1.0"
authors = ["R. Tyler Croy <rtyler@brokenco.de>"]
edition = "2018"
[dependencies]
otto-agent = { path = "../../agent" }
serde = {version = "~1.0.117", features = ["derive"]}

View File

@ -1,62 +0,0 @@
#!/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)

View File

@ -4,16 +4,17 @@ description: |
The `error` step is a simple step that exits the pipeline
includes:
- name: ./error-step
- name: target/release/error-step
flatten: true
- name: ./README.adoc
entrypoint:
path: error-step
multiarch: true
multiarch: false
parameters:
- name: message
required: true
type: string
description: |
The message to print when exiting the pipelin
The message to print when exiting the pipeline

19
stdlib/error/src/main.rs Normal file
View File

@ -0,0 +1,19 @@
/*
* The error step is really really really simple
*/
use ottoagent::step::*;
use serde::Deserialize;
#[derive(Clone, Debug, Deserialize)]
struct Parameters {
message: String,
}
fn main() {
let args = std::env::args().collect();
let invoke: Invocation<Parameters> = invocation_from_args(&args).unwrap();
println!("{}", invoke.parameters.message);
std::process::exit(1);
}