1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::monitor::MonitorEntry;
use crate::objects::*;
use crate::registry::AppRegistry;
use juniper::FieldResult;
use kubos_service;
type Context = kubos_service::Context<AppRegistry>;
pub struct QueryRoot;
graphql_object!(QueryRoot : Context as "Query" |&self| {
field ping() -> FieldResult<String>
as "Test service query"
{
Ok(String::from("pong"))
}
field registered_apps(&executor,
name: Option<String>,
version: Option<String>,
active: Option<bool>)
-> FieldResult<Vec<KAppRegistryEntry>> as "Kubos Apps Query"
{
let entries = executor.context().subsystem().entries.lock()?;
let result = entries.iter().filter(|ref e| {
if name.is_some() && &e.app.name != name.as_ref().unwrap() {
return false;
}
if version.is_some() && &e.app.version != version.as_ref().unwrap() {
return false;
}
if active.is_some() && e.active_version != active.unwrap() {
return false;
}
true
}).map(|entry| KAppRegistryEntry(entry.clone())).collect();
Ok(result)
}
field app_status(&executor,
name: Option<String>,
version: Option<String>,
running: Option<bool>)
-> FieldResult<Vec<MonitorEntry>> as "App Status Query"
{
let entries = executor.context().subsystem().monitoring.lock()?;
let result = entries.iter().filter(|e| {
if name.is_some() && &e.name != name.as_ref().unwrap() {
return false;
}
if version.is_some() && &e.version != version.as_ref().unwrap() {
return false;
}
if running.is_some() && &e.running != running.as_ref().unwrap() {
return false;
}
true
}).map(|entry_ref| (*entry_ref).clone()).collect();
Ok(result)
}
});
pub struct MutationRoot;
graphql_object!(MutationRoot : Context as "Mutation" |&self| {
field register(&executor, path: String) -> FieldResult<RegisterResponse>
as "Register App"
{
let registry = executor.context().subsystem();
Ok(match registry.register(&path) {
Ok(app) => RegisterResponse { success: true, errors: "".to_owned(), entry: Some(KAppRegistryEntry(app))},
Err(error) => RegisterResponse {
success: false,
errors: error.to_string(),
entry: None
}
})
}
field uninstall(&executor, name: String, version: Option<String>) -> FieldResult<GenericResponse>
as "Uninstall App"
{
if let Some(val) = version {
Ok(match executor.context().subsystem().uninstall(&name, &val) {
Ok(v) => GenericResponse { success: true, errors: "".to_owned() },
Err(error) => GenericResponse { success: false, errors: error.to_string() },
})
} else {
Ok(match executor.context().subsystem().uninstall_all(&name) {
Ok(v) => GenericResponse { success: true, errors: "".to_owned() },
Err(error) => GenericResponse { success: false, errors: error.to_string() },
})
}
}
field set_version(&executor, name: String, version: String) -> FieldResult<GenericResponse>
as "Set App Active Version"
{
Ok(match executor.context().subsystem().set_version(&name, &version) {
Ok(v) => GenericResponse { success: true, errors: "".to_owned() },
Err(error) => GenericResponse { success: false, errors: error.to_string() },
})
}
field start_app(&executor, name: String, config: Option<String>, args: Option<Vec<String>>) -> FieldResult<StartResponse>
as "Start App"
{
Ok(match executor.context().subsystem().start_app(&name, config, args) {
Ok(pid) => StartResponse { success: true, errors: "".to_owned(), pid},
Err(error) => StartResponse { success: false, errors: error.to_string(), pid: None },
})
}
field kill_app(&executor, name: String, signal: Option<i32>) -> FieldResult<GenericResponse>
as "Kill Running App"
{
Ok(match executor.context().subsystem().kill_app(&name, signal) {
Ok(pid) => GenericResponse { success: true, errors: "".to_owned() },
Err(error) => GenericResponse { success: false, errors: error.to_string() },
})
}
});