Skip to content

Commit

Permalink
Merge pull request flyinghead#98 from inada-s/infra-fix-logging
Browse files Browse the repository at this point in the history
Infra fix logging
  • Loading branch information
inada-s authored Feb 5, 2021
2 parents 061f19f + 1486658 commit 7546b3b
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 132 deletions.
2 changes: 1 addition & 1 deletion gdxsv/lbs_mcsfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func McsFuncAlloc(region string) error {
return err
}

resp, err := client.Get(conf.McsFuncURL + fmt.Sprintf("/alloc?region=%s&version=%s", region, gdxsvVersion))
resp, err := client.Get(conf.McsFuncURL + fmt.Sprintf("/alloc?region=%s&version=%s", region, "latest"))
if err != nil {
return err
}
Expand Down
17 changes: 0 additions & 17 deletions infra/logfunc/.gcloudignore

This file was deleted.

2 changes: 0 additions & 2 deletions infra/logfunc/.gitignore

This file was deleted.

3 changes: 0 additions & 3 deletions infra/logfunc/env.yaml

This file was deleted.

64 changes: 0 additions & 64 deletions infra/logfunc/index.js

This file was deleted.

26 changes: 0 additions & 26 deletions infra/logfunc/package.json

This file was deleted.

2 changes: 1 addition & 1 deletion infra/mcsfunc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This allow us to allocate mcs (battle server) on demand in any region.
1. Create Cloud Function (Web/CUI)
1. Generate service account key (for Development) and Download it as json (Web/CUI)
1. Run `GOOGLE_APPLICATION_CREDENTIALS=/path/to/gcp-key.json npm start` to local development (CUI)
1. Run `gcloud functions deploy mcsfunc --region asia-northeast1 --entry-point cloudFunctionEntryPoint --trigger-http` to deploy (CUI)
1. Run `gcloud functions deploy mcsfunc --region asia-northeast1 --entry-point cloudFunctionEntryPoint --trigger-http --runtime nodejs12` to deploy (CUI)

## Access Control Memo
1. Generate service account (for function invoker) and its key then download it as json (Web/CUI)
Expand Down
22 changes: 10 additions & 12 deletions infra/mcsfunc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ function getStartupScript(version) {
#!/bin/bash
echo "startup-script"
su -c "echo 'deb http://packages.cloud.google.com/apt google-compute-engine-bionic-stable main' > /etc/apt/sources.list.d/google-compute-engine.list"
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
apt update
# installing google-osconfig-agent fail...
# apt -y install google-osconfig-agent jq wget curl
apt -y install jq wget curl
apt -y install google-osconfig-agent jq wget curl
if grep -xqFe 'ubuntu ALL=NOPASSWD: /sbin/shutdown' /etc/sudoers; then
echo 'ubuntu ALL=NOPASSWD: /sbin/shutdown' >> /etc/sudoers
fi
mkdir -p /etc/google-fluentd/config.d
cat << 'EOF' > /etc/google-fluentd/config.d/gdxsv.conf
<source>
@type tail
Expand Down Expand Up @@ -108,14 +109,14 @@ chown ubuntu:ubuntu /var/log/gdxsv-mcs.log
chmod +x /home/ubuntu/launch-mcs.sh
chmod +x /home/ubuntu/upload-battlelog.sh
systemctl restart google-fluentd
# systemctl restart google-fluentd
su ubuntu -c 'cd /home/ubuntu && nohup ./launch-mcs.sh &'
su ubuntu -c 'cd /home/ubuntu && nohup ./upload-battlelog.sh &'
echo "startup-script done"
`
}

function forResponse(vm) {
const forResponse = (vm) => {
const v = {};
try {
v.name = vm.name;
Expand Down Expand Up @@ -146,7 +147,6 @@ async function getList(req, res) {

res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(vmlist, null, " "));
return;
}

async function getDeleteAll(req, res) {
Expand Down Expand Up @@ -194,7 +194,7 @@ async function getAlloc(req, res) {

console.log("" + vms.length + "vms found.");

let vm = vms.find(vm => vm.metadata.status == "RUNNING");
let vm = vms.find(vm => vm.metadata.status === "RUNNING");
if (vm) {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(forResponse(vm), null, " "));
Expand All @@ -203,7 +203,7 @@ async function getAlloc(req, res) {

console.log("running vm not found");

for (let vm of vms.filter(vm => vm.metadata.status == "TERMINATED")) {
for (let vm of vms.filter(vm => vm.metadata.status === "TERMINATED")) {
try {
console.log("starting vm...", vm);
let [operation] = await vm.setMetadata({
Expand Down Expand Up @@ -234,7 +234,7 @@ async function getAlloc(req, res) {
console.log("trying to create new vm in", zoneName);
const zone = compute.zone(zoneName);
const [vm, operation] = await zone.createVM(vmName, {
os: "ubuntu",
os: "ubuntu-1804",
http: true,
tags: ["gdxsv-mcs"],
machineType: "e2-medium",
Expand All @@ -259,20 +259,18 @@ async function getAlloc(req, res) {
await operation.promise();
console.log("vm created");
const [metadata] = await vm.waitFor("RUNNING", {timeout: 30});
vm.metadata = metadata
vm.metadata = metadata;
console.log("new vm is running", vm);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(forResponse(vm), null, " "));
return;
} catch (e) {
console.log(e);
continue;
}
}

console.log('failed to allocate vm');
res.status(503).send('failed to allocate vm');
return;
}


Expand Down
6 changes: 0 additions & 6 deletions infra/mcsfunc/jsconfig.json

This file was deleted.

0 comments on commit 7546b3b

Please sign in to comment.