Skip to content

Commit 4bb7b7e

Browse files
committed
start project
0 parents  commit 4bb7b7e

4 files changed

Lines changed: 228 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idea.md
2+
slackNotification.py

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2009-2022 Robby Russell and contributors (https://github.com/ohmyzsh/ohmyzsh/contributors)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Switch Orchestrator
2+
3+
## About
4+
5+
I started with this project because I wanted to search job logs on Switch quickly and simply. If you work most part of the time coding and switching between GUI and Terminal you know how to bother is to go to Switch to look at the logs
6+
7+
### Requirements
8+
9+
Switch Orchestrator uses a few simple tools that you probably already have installed.
10+
A text editor (such as Vim or Emacs)
11+
A program to print files (like cat)
12+
Basic programs that should be included in almost all Linux/Unix distributions, including mkdir, touch, mv, rm, echo, printf
13+
But I order to optimize the developement two libraries are required, specially for deal with json files.
14+
JQ <https://github.com/stedolan/jq>
15+
JTBL <https://kellyjonbrazil.github.io/jtbl/>
16+
17+
### Usage
18+
19+
#### Before Start
20+
21+
Starting from the folder that swo.sh is located
22+
23+
```bash
24+
chmod +x swo.sh
25+
sudo cp swo.sh /usr/local/bin/swo
26+
```
27+
28+
Create a file `swo_config` on `$HOME/.config/switchOrchestrator`
29+
Please read the oficial manual of [Enfocus Switch API](https://www.enfocus.com/manuals/DeveloperGuide/WebServices/17/index.html#api-Authentication-LoginQuery) to learn how create a password hash
30+
31+
```bash
32+
USER="log"
33+
HASH_PASS="XXXXXXXXXXXXXXXX"
34+
SWITCH_IP="0.0.0.0"
35+
36+
```
37+
38+
#### Auth
39+
40+
This command will save a token used to make the API calls
41+
42+
```bash
43+
swo -a
44+
```
45+
46+
#### Search for Job
47+
48+
```bash
49+
swo -j EA-12-123123
50+
```
51+
52+
#### Options
53+
54+
| Options | Description |
55+
| ---------------------------- | --------------------- |
56+
| `-a --auth` | authentication |
57+
| `-j <string> --job <string>` | search a job |
58+
| `-h --help` | display help dialog |
59+
| `-i --install` | create config folders |
60+
61+
### How do I contribute to Switch Orchestrator?
62+
63+
I'm far from expert and suspect there are many ways to improve. If you have ideas on how to make this project better, don't hesitate to fork and send pull requests!
64+
65+
### Authors
66+
67+
```Bruno Bertolani``` [LinkedIn](https://www.linkedin.com/in/brunosbertolani/)
68+
69+
### Research used on this project
70+
71+
<https://stackoverflow.com/questions/39139107/how-to-format-a-json-string-as-a-table-using-jq>
72+
<https://www.makeareadme.com/>
73+
<https://www.enfocus.com/manuals/DeveloperGuide/WebServices/17/index.html#api-Authentication-LoginQuery>
74+
<https://makefiletutorial.com/#commands-and-execution>
75+
76+
### Next Features
77+
78+
- [X] Authentication
79+
- [X] Search by Jobs
80+
- [ ] Search with different parameters
81+
- [ ] Refresh Search
82+
- [ ] List Workflow
83+
- [ ] Start/Stop workflow
84+
- [ ] Multiple Switch
85+
- [ ] Environment ?
86+
- [ ] Sync multiple scripts between enviroments
87+
- [ ] Migrate to python ?

swo.sh

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/bin/bash
2+
3+
# shellcheck source="${XDG_CONFIG_HOME:-$HOME/.config}/switchOrchestrator/swo"
4+
# Set default configuration
5+
SWITCH_IP=""
6+
USER=""
7+
HASH_PASS=""
8+
TOKEN=""
9+
10+
# Overwrite default configs from noterc configuration file
11+
SWITCH_CF_FOLDER="${XDG_CONFIG_HOME:-$HOME/.config}/switchOrchestrator"
12+
SWITCH_CF_FILE="$SWITCH_CF_FOLDER/swo_config"
13+
if [ -f "$SWITCH_CF_FILE" ]; then source "$SWITCH_CF_FILE"; fi
14+
if [ -f "$SWITCH_CF_FOLDER/swo_$SWITCH_IP" ]; then TOKEN=$(cat $SWITCH_CF_FOLDER/swo_$SWITCH_IP); fi
15+
16+
SWITCH_ADR="http://"$SWITCH_IP":51088"
17+
18+
# Help
19+
Help() {
20+
printf "############################################################
21+
# Help #
22+
############################################################
23+
Switch Orchestrator
24+
Usage: swo [<args>]
25+
Arguments:
26+
-h | --help Display usage guide.
27+
-j | --job <JOBNUMBER> Search about a specific job.
28+
switchOrchestrator loads configuration variables from:
29+
\$HOME/.config/switchOrchestrator/swo"
30+
exit 0
31+
}
32+
33+
install() {
34+
exec mkdir $SWITCH_CF_FOLDER
35+
}
36+
37+
auth() {
38+
echo $SWITCH_ADR
39+
JSON=$(curl -s POST $SWITCH_ADR/login -H 'Content-Type: application/json' -d '{"username": "'$USER'", "password": "'$HASH_PASS'"}')
40+
41+
result=$(jq -r '.success' <<< $JSON)
42+
if [ "$result" == "false" ]; then
43+
echo "Login failed"
44+
exit 1
45+
fi
46+
TOKEN=$(jq -r '.token' <<< $JSON)
47+
SAVED_TOKEN=$SWITCH_CF_FOLDER/swo_$SWITCH_IP
48+
touch $SAVED_TOKEN
49+
truncate -s 0 $SAVED_TOKEN
50+
echo "$TOKEN" >> $SAVED_TOKEN
51+
echo "Login Sucessful | SWITCH: $SWITCH_IP"
52+
exit 0
53+
}
54+
55+
searchJob() {
56+
JSON=$(curl -s --location --request GET "$SWITCH_ADR/api/v1/messages?type=info&message=$JOB_NUMBER&limit=100" -H 'Authorization: Bearer '$TOKEN)
57+
status=$(jq '.status' <<< $JSON)
58+
if [ "$status" == "success" ]; then
59+
echo "Search failed"
60+
exit 1
61+
fi
62+
messages=$(jq '.messages' <<< $JSON)
63+
echo $messages | jq '[.[] | {timestamp,flow,job,element,message}] | sort_by(.timestamp)' | jtbl
64+
exit 0
65+
}
66+
67+
validateSearchJob() {
68+
if [ "$#" -ne 2 ]; then
69+
printf "Incorrect number of arguments.\n"
70+
printf "Usage: swo --job <JOBNUMBER>\n"
71+
exit 1
72+
fi
73+
JOB_NUMBER="$2"
74+
if [ -z "$JOB_NUMBER" ]; then
75+
printf "Expected additional argument <Job Number>.\n"
76+
exit 1
77+
fi
78+
searchJob $JOB_NUMBER
79+
}
80+
81+
############################################################
82+
# Main program #
83+
############################################################
84+
# Process the input options. Add options as needed. #
85+
############################################################
86+
# Get the options
87+
if (($# > 0)); then
88+
while [[ $# -gt 0 ]]; do
89+
key="$1"
90+
case $key in
91+
-j | --job)
92+
validateSearchJob "$@"
93+
;;
94+
-a | --auth)
95+
auth
96+
shift
97+
;;
98+
-i | --install)
99+
install
100+
;;
101+
-h | --help)
102+
Help
103+
;;
104+
*)
105+
printf "Unknown Argument \"%s\"\n" "$1"
106+
printf "Use \"swo --help\" to see usage information.\n"
107+
exit 1
108+
;;
109+
esac
110+
done
111+
else
112+
#no arguments/options
113+
printf "\n
114+
Switch: $SWITCH_ADR
115+
Config: $SWITCH_CF_FILE
116+
Use \"swo --help\" to see usage information.\n"
117+
118+
fi

0 commit comments

Comments
 (0)