Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for notebooks in Autoscale #3483

Merged
merged 17 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 19 additions & 10 deletions syft/grid/autoscale/gcloud.py
@@ -1,11 +1,11 @@
"""To autoscale pygrid workers on Google Cloud Platfrom"""
import os
import json
import shutil
import subprocess
import IPython
import terrascript
import terrascript.provider
import terrascript.resource
from utils.script import terraform_script
from utils.notebook import terraform_notebook


class GoogleCloud:
Expand All @@ -18,8 +18,7 @@ def __init__(self, credentials, project_id, region):
project_id: project_id of your project in GCP
region: region of your GCP project
"""
shutil.copy2(credentials, os.getcwd() + "/credentials.json")
self.credentials = "credentials.json"
self.credentials = credentials
self.project_id = project_id
self.region = region
self.config = terrascript.Terrascript()
Expand All @@ -28,7 +27,11 @@ def __init__(self, credentials, project_id, region):
)
with open("main.tf.json", "w") as main_config:
json.dump(self.config, main_config, indent=2, sort_keys=False)
subprocess.call("terraform init", shell=True)

if IPython.get_ipython():
terraform_notebook.init()
else:
terraform_script.init()

def compute_instance(self, name, machine_type, zone, image_family):
"""
Expand All @@ -48,12 +51,18 @@ def compute_instance(self, name, machine_type, zone, image_family):
)
with open("main.tf.json", "w") as main_config:
json.dump(self.config, main_config, indent=2, sort_keys=False)
# subprocess.call("terraform plan", shell=True)
subprocess.call("terraform apply", shell=True)

if IPython.get_ipython():
terraform_notebook.apply()
else:
terraform_script.apply()

def destroy(self):
"""
args:
"""
subprocess.call("terraform destroy", shell=True)
os.remove(self.credentials)
if IPython.get_ipython():
terraform_notebook.destroy()
else:
terraform_script.destroy()
del self.credentials
75 changes: 75 additions & 0 deletions syft/grid/autoscale/test.ipynb
@@ -0,0 +1,75 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from syft.grid.autoscale import gcloud"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"NEW = gcloud.GoogleCloud(\n",
" credentials=\"/home/usr/terraform.json\", project_id=\"project\", region=\"us-central1\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"NEW.compute_instance(\n",
" name=\"new-12345\",\n",
" machine_type=\"f1-micro\",\n",
" zone=\"us-central1-a\",\n",
" image_family=\"debian-9\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"NEW.destroy()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.7.6 64-bit ('syft': conda)",
"language": "python",
"name": "python37664bitsyftconda3821869f2cbc432c8ddeb2e882487673"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
4 changes: 2 additions & 2 deletions syft/grid/autoscale/test.py
@@ -1,9 +1,9 @@
"""To test the implementation of gcloud.py"""
import gcloud
from syft.grid.autoscale import gcloud


NEW = gcloud.GoogleCloud(
credentials="GCP Login/terraf.json", project_id="terraform", region="us-central1"
credentials="/usr/terraform.json", project_id="project", region="us-central1",
)

NEW.compute_instance(
Expand Down
94 changes: 94 additions & 0 deletions syft/grid/autoscale/utils/notebook/terraform_notebook.py
@@ -0,0 +1,94 @@
"""Helper methods to call terraform commands"""
import sys
import threading
import subprocess


def init():
"""
args:
"""
proc = subprocess.Popen(
"/bin/sh", stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT,
)

def outloop():
running = True
while running:
line = proc.stdout.readline().decode(sys.stdout.encoding)
print(line, end="")
running = "\n" in line
print("Exited")

threading.Thread(target=outloop).start()

commands = [b"terraform init\n", b"exit\n"]
i = 0
while proc.poll() is None and i < len(commands):
inp = commands[i]
if inp == "INPUT":
inp = bytearray(input("") + "\n", sys.stdin.encoding) # nosec
if proc.poll() is None:
proc.stdin.write(inp)
proc.stdin.flush()
i += 1


def apply():
"""
args:
"""
proc = subprocess.Popen(
"/bin/sh", stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT,
)

def outloop():
running = True
while running:
line = proc.stdout.readline().decode(sys.stdout.encoding)
print(line, end="")
running = "\n" in line
print("Exited")

threading.Thread(target=outloop).start()

commands = [b"terraform apply\n", "INPUT", b"exit\n"]
i = 0
while proc.poll() is None and i < len(commands):
inp = commands[i]
if inp == "INPUT":
inp = bytearray(input("") + "\n", sys.stdin.encoding) # nosec
if proc.poll() is None:
proc.stdin.write(inp)
proc.stdin.flush()
i += 1


def destroy():
"""
args:
"""
proc = subprocess.Popen(
"/bin/sh", stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT,
)

def outloop():
running = True
while running:
line = proc.stdout.readline().decode(sys.stdout.encoding)
print(line, end="")
running = "\n" in line
print("Exited")

threading.Thread(target=outloop).start()

commands = [b"terraform destroy\n", "INPUT", b"exit\n"]
i = 0
while proc.poll() is None and i < len(commands):
inp = commands[i]
if inp == "INPUT":
inp = bytearray(input("") + "\n", sys.stdin.encoding) # nosec
if proc.poll() is None:
proc.stdin.write(inp)
proc.stdin.flush()
i += 1
23 changes: 23 additions & 0 deletions syft/grid/autoscale/utils/script/terraform_script.py
@@ -0,0 +1,23 @@
"""Helper methods to call terraform commands"""
import subprocess


def init():
"""
args:
"""
subprocess.call("terraform init", shell=True)


def apply():
"""
args:
"""
subprocess.call("terraform apply", shell=True)


def destroy():
"""
args:
"""
subprocess.call("terraform destroy", shell=True)