Repository navigation
Expand file tree
/
Copy pathoracle-cloud.js
More file actions
102 lines (94 loc) · 3.01 KB
/
Copy pathoracle-cloud.js
File metadata and controls
102 lines (94 loc) · 3.01 KB
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
const domainID = "YOUR_DOMAIN_ID"; // "xUCN:AP-SEOUL-1-AD-1"
const imageID = "YOUR_IMAGE_ID"; // "ocid1.image.oc1.ap-seoul-1.xxxxxxxxxxxxxx"
const subnetID = "YOUR_SUBNET_ID"; // "ocid1.subnet.oc1.ap-seoul-1.xxxxxxxxxxxxxx"
const shape = "YOUR_SHAPE_ID"; // "VM.Standard.E2.1.Micro"
const sshKey = "YOUR_SSH_KEY"; // "ssh-rsa xxxxxxxxxxxxxx"
const compartmentID = "YOUR_COMPARTMENT_ID"; // "ocid1.tenancy.oc1..xxxxxxxxxxxxxx"
const name1 = "free-kr1";
const name2 = "free-kr2";
const sleepTime = 5000; // ms
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const fs = require("fs");
const readFile = fs.existsSync("./oracle-cloud.json") ? fs.readFileSync("./oracle-cloud.json") : `{"loopTime1":0,"loopTime2":0}`;
const save = JSON.parse(readFile.toString());
let loopTime1 = save.loopTime1;
let loopTime2 = save.loopTime2;
let isMachine1 = true;
let machine1Finished = false;
let machine2Finished = false;
async function execute(callback) {
return new Promise(async resolve => {
try {
if (isMachine1) {
if (machine1Finished) {
resolve(false);
return;
}
loopTime1++;
} else {
if (machine2Finished) {
resolve(false);
return;
}
loopTime2++;
}
const cmd = `oci compute instance launch --availability-domain ${domainID} --display-name ${
isMachine1 ? name1 : name2
} --image-id ${imageID} --subnet-id ${subnetID} --shape ${shape} --assign-public-ip true --metadata '{"ssh_authorized_keys": "${sshKey}"}' --compartment-id ${compartmentID}`
const result = await exec(cmd);
callback(result, resolve);
} catch (err) {
callback(err, resolve);
}
});
}
function sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
function callback(result, resolve) {
console.log(
`${name1}: ${loopTime1} ${
machine1Finished ? "Finished!" : "Creating"
} ${name2}: ${loopTime2} ${machine2Finished ? "Finished!" : "Creating"}`
);
if (result.stderr) {
console.log(`Err: ${isMachine1 ? name1 : name2}`);
const jsonGroup = result.stderr
.toString()
.match(/\{(?:[^{}]|(\{(?:[^{}]|(\{[^{}]*\}))*\}))*\}/);
try {
const json = JSON.parse(jsonGroup[0]);
console.log(json.status, json.message);
} catch (e) {
}
resolve(true);
} else if (result.stdout) {
if (isMachine1) {
machine1Finished = true;
} else {
machine2Finished = true;
}
console.log("Out");
resolve(true);
}
}
async function init() {
console.log("Started");
while (!machine1Finished || !machine2Finished) {
const isExecuted = await execute(callback);
isMachine1 = !isMachine1;
if (isExecuted) {
fs.writeFileSync(
"./oracle-cloud.json",
JSON.stringify({loopTime1: loopTime1, loopTime2: loopTime2})
);
await sleep(sleepTime);
}
}
}
init();
// By yyuueexxiinngg
// Link https://github.com/yyuueexxiinngg/some-scripts/blob/master/oracle/oracle-cloud.js