Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
Mohamed Alibi
ansible-examples
Commits
4585ea1c
Unverified
Commit
4585ea1c
authored
Jun 24, 2018
by
Sviatoslav Sydorenko
Browse files
Add a binary rust example module
parent
bedca740
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
275 additions
and
0 deletions
+275
-0
rust-module-hello-world/Makefile
rust-module-hello-world/Makefile
+13
-0
rust-module-hello-world/library/.gitignore
rust-module-hello-world/library/.gitignore
+3
-0
rust-module-hello-world/module-src/Cargo.lock
rust-module-hello-world/module-src/Cargo.lock
+85
-0
rust-module-hello-world/module-src/Cargo.toml
rust-module-hello-world/module-src/Cargo.toml
+9
-0
rust-module-hello-world/module-src/src/main.rs
rust-module-hello-world/module-src/src/main.rs
+107
-0
rust-module-hello-world/module-src/target/.gitignore
rust-module-hello-world/module-src/target/.gitignore
+3
-0
rust-module-hello-world/rust.yml
rust-module-hello-world/rust.yml
+55
-0
No files found.
rust-module-hello-world/Makefile
0 → 100644
View file @
4585ea1c
.PHONY
:
all clean rust
all
:
rust
clean
:
rm
-f
library/rust_helloworld
cd
module-src
&&
\
cargo clean
rust
:
cd
module-src
&&
\
cargo build
&&
\
cp
-v
target/debug/helloworld ../library/rust_helloworld
rust-module-hello-world/library/.gitignore
0 → 100644
View file @
4585ea1c
!/.gitignore
/.*
/*
rust-module-hello-world/module-src/Cargo.lock
0 → 100644
View file @
4585ea1c
[[package]]
name = "dtoa"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "helloworld"
version = "0.1.0"
dependencies = [
"serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "itoa"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "proc-macro2"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quote"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde"
version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde_derive"
version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde_json"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-xid"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
"checksum itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c069bbec61e1ca5a596166e55dfe4773ff745c3d16b700013bcaff9a6df2c682"
"checksum proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "effdb53b25cdad54f8f48843d67398f7ef2e14f12c1b4cb4effc549a6462a4d6"
"checksum quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e44651a0dc4cdd99f71c83b561e221f714912d11af1a4dff0631f923d53af035"
"checksum serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "e9a2d9a9ac5120e0f768801ca2b58ad6eec929dc9d1d616c162f208869c2ce95"
"checksum serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "0a90213fa7e0f5eac3f7afe2d5ff6b088af515052cc7303bd68c7e3b91a3fb79"
"checksum serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)" = "fc97cccc2959f39984524026d760c08ef0dd5f0f5948c8d31797dbfae458c875"
"checksum syn 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c67da57e61ebc7b7b6fff56bb34440ca3a83db037320b0507af4c10368deda7d"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
rust-module-hello-world/module-src/Cargo.toml
0 → 100644
View file @
4585ea1c
[package]
name
=
"helloworld"
version
=
"0.1.0"
authors
=
[
"Sviatoslav Sydorenko <wk+ansible-github@sydorenko.org.ua>"
]
[dependencies]
serde
=
"1.0.66"
serde_derive
=
"1.0.66"
serde_json
=
"1.0.20"
rust-module-hello-world/module-src/src/main.rs
0 → 100644
View file @
4585ea1c
extern
crate
serde
;
extern
crate
serde_json
;
use
std
::
env
;
use
std
::
fs
::
File
;
use
std
::
io
::
prelude
::
*
;
use
std
::
process
;
#[macro_use]
extern
crate
serde_derive
;
use
serde_json
::
Error
;
fn
default_name_arg
()
->
String
{
String
::
from
(
"World"
)
}
#[derive(Serialize,
Deserialize)]
struct
ModuleArgs
{
#[serde(default
=
"default_name_arg"
)]
name
:
String
,
}
#[derive(Clone,
Serialize,
Deserialize)]
struct
Response
{
msg
:
String
,
changed
:
bool
,
failed
:
bool
,
}
fn
exit_json
(
response_body
:
Response
)
{
return_response
(
response_body
)
}
fn
fail_json
(
response_body
:
Response
)
{
let
failed_response
=
&
mut
response_body
.clone
();
failed_response
.failed
=
true
;
return_response
(
failed_response
.clone
())
}
fn
return_response
(
resp
:
Response
)
{
println!
(
"{}"
,
serde_json
::
to_string
(
&
resp
)
.unwrap
());
process
::
exit
(
resp
.failed
as
i32
);
}
fn
read_file_contents
(
file_name
:
&
str
)
->
Result
<
String
,
Box
<
std
::
io
::
Error
>>
{
let
mut
json_string
=
String
::
new
();
File
::
open
(
file_name
)
?
.read_to_string
(
&
mut
json_string
)
?
;
Ok
(
json_string
)
}
fn
parse_module_args
(
json_input
:
String
)
->
Result
<
ModuleArgs
,
Error
>
{
Ok
(
ModuleArgs
::
from
(
serde_json
::
from_str
(
json_input
.as_str
()
)
?
)
)
}
fn
main
()
{
let
args
:
Vec
<
String
>
=
env
::
args
()
.collect
();
let
program
=
&
args
[
0
];
let
input_file_name
=
match
args
.len
()
{
2
=>
&
args
[
1
],
_
=>
{
eprintln!
(
"module '{}' expects exactly one argument!"
,
program
);
fail_json
(
Response
{
msg
:
"No module arguments file provided"
.to_owned
(),
changed
:
false
,
failed
:
true
,
});
""
}
};
let
json_input
=
read_file_contents
(
input_file_name
)
.map_err
(|
err
|
{
eprintln!
(
"Could not read file '{}': {}"
,
input_file_name
,
err
);
fail_json
(
Response
{
msg
:
format!
(
"Could not read input JSON file '{}': {}"
,
input_file_name
,
err
),
changed
:
false
,
failed
:
true
,
})
})
.unwrap
();
let
module_args
=
parse_module_args
(
json_input
)
.map_err
(|
err
|
{
eprintln!
(
"Error during parsing JSON module arguments: {}"
,
err
);
fail_json
(
Response
{
msg
:
format!
(
"Malformed input JSON module arguments: {}"
,
err
),
changed
:
false
,
failed
:
true
,
})
})
.unwrap
();
exit_json
(
Response
{
msg
:
format!
(
"Hello, {}!"
,
module_args
.name
.as_str
()),
changed
:
true
,
failed
:
false
,
});
}
rust-module-hello-world/module-src/target/.gitignore
0 → 100644
View file @
4585ea1c
!/.gitignore
/.*
/*
rust-module-hello-world/rust.yml
0 → 100644
View file @
4585ea1c
---
-
hosts
:
localhost
tasks
:
-
debug
:
msg
:
Testing a binary module written in Rust
-
debug
:
var
:
ansible_system
-
name
:
ping
ping
:
-
name
:
Hello, World!
action
:
rust_helloworld
register
:
hello_world
-
assert
:
that
:
-
>
hello_world.msg == "Hello, World!"
-
name
:
Hello, Ansible!
action
:
rust_helloworld
args
:
name
:
Ansible
register
:
hello_ansible
-
assert
:
that
:
-
>
hello_ansible.msg == "Hello, Ansible!"
-
name
:
Async Hello, World!
action
:
rust_helloworld
async
:
10
poll
:
1
register
:
async_hello_world
-
assert
:
that
:
-
>
async_hello_world.msg == "Hello, World!"
-
name
:
Async Hello, Ansible!
action
:
rust_helloworld
args
:
name
:
Ansible
async
:
10
poll
:
1
register
:
async_hello_ansible
-
assert
:
that
:
-
>
async_hello_ansible.msg == "Hello, Ansible!"
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment