-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployinator.ino
130 lines (102 loc) · 3.27 KB
/
deployinator.ino
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
/* Deployinator
*
* Author: Casey Fulton <[email protected]>
* Reference: https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/HTTPSRequest/HTTPSRequest.ino
* License: https://opensource.org/licenses/MIT
*/
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include "credentials.h"
#define HOLD_LED_PIN D1
#define DEPLOY_LED_PIN D2
#define OK_LED_PIN D3
#define PWR_LED_PIN D4
#define KEY_PIN D5
#define HOLD_PIN D6
#define DEPLOY_PIN D7
void setup() {
Serial.begin(57600);
pinMode(HOLD_LED_PIN, OUTPUT);
pinMode(DEPLOY_LED_PIN, OUTPUT);
pinMode(OK_LED_PIN, OUTPUT);
pinMode(PWR_LED_PIN, OUTPUT);
pinMode(KEY_PIN, INPUT);
pinMode(HOLD_PIN, INPUT);
pinMode(DEPLOY_PIN, INPUT);
digitalWrite(PWR_LED_PIN, HIGH);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
digitalWrite(OK_LED_PIN, HIGH);
delay(250);
digitalWrite(OK_LED_PIN, LOW);
}
digitalWrite(OK_LED_PIN, HIGH);
Serial.print("WiFi connected, IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// If wifi is connected, turn ok LED on.
digitalWrite(OK_LED_PIN, WiFi.status() == WL_CONNECTED);
// If key is on, turn hold LED on.
digitalWrite(HOLD_LED_PIN, digitalRead(KEY_PIN));
// If both key and hold are on, turn deploy LED on.
digitalWrite(DEPLOY_LED_PIN, digitalRead(KEY_PIN) && digitalRead(HOLD_PIN));
// If all three, trigger deploy.
if (digitalRead(KEY_PIN) && digitalRead(HOLD_PIN) && digitalRead(DEPLOY_PIN)) {
// De-bounce. Wait for 5 ms and double check inputs.
delay(5);
if (digitalRead(KEY_PIN) && digitalRead(HOLD_PIN) && digitalRead(DEPLOY_PIN)) {
// Turn off all LEDs.
digitalWrite(OK_LED_PIN, LOW);
digitalWrite(HOLD_LED_PIN, LOW);
digitalWrite(DEPLOY_LED_PIN, LOW);
// Call deploy Jenkins job.
bool result = deploy();
// Turn on deploy (green) LED if deploy succeeded, or hold (red) LED if failed.
if (result) {
digitalWrite(DEPLOY_LED_PIN, HIGH);
} else {
digitalWrite(HOLD_LED_PIN, HIGH);
}
delay(5000);
digitalWrite(HOLD_LED_PIN, LOW);
digitalWrite(DEPLOY_LED_PIN, LOW);
delay(5000);
}
}
}
bool deploy() {
WiFiClientSecure client;
Serial.println("Beginning deploy...");
if (!client.connect(JENKINS_JOB_HOST, JENKINS_PORT)) {
Serial.println("Connection failed.");
return false;
}
if (!client.verify(JENKINS_SSL_FINGERPRINT, JENKINS_JOB_HOST)) {
Serial.println("Could not verify certificate.");
return false;
}
String request = String("POST ") + JENKINS_JOB_PATH + " HTTP/1.1\r\n" +
"Host: " + JENKINS_JOB_HOST + "\r\n" +
"Authorization: Basic " + JENKINS_AUTH + "\r\n" +
"User-Agent: SOAR Deployinator v2\r\n" +
"Connection: close\r\n\r\n";
Serial.print("Making request:\n" + request);
client.print(request);
Serial.println("Request sent.");
while (client.connected()) {
String line = client.readStringUntil('\n');
Serial.println(String("< ") + line);
if (line.indexOf("201 Created") > 0) {
return true;
}
if (line == "\r") {
break;
}
}
client.stop();
Serial.println("Something went wrong.");
return false;
}