This is the 16th day article of Advent Calender on the 2019 code-server.
table of contents Local environment 1st day Online environment version 1st day Improve work environment
Online environment, day 2 Create a virtual network
Online environment 3rd day Launch an EC2 instance with Boto3
Online environment 4th day Try running Code-Server in the cloud
Online environment 5th day Launch code-server on Docker
Online environment, day 6 Let's automate
Online environment 7th day Deploy compute on git on EC2
... Online version .. Built with Coompose file
Online .. Try K8S
...
Demon remodeling
Up to the last time, I tried to launch Code-Server on EC Instance using Docker. This time, let's automate this work.
From the last continuation
$ git clone https://github.com/kyorohiro/advent-2019-code-server.git
$ cd advent-2019-code-server/remote_cs04/
$ docker-compose build
$ docker-compose up -d
In your browser, open http://127.0.0.1:8443/
.
On Terminal
Terminal
$ pip install -r requirements.txt
$ aws configure
..
..
Create EC2Instance
$ python main.py --create
Get EC2 information
$ python main.py --get
>>>> i-0d1e7775a07bbb326
>>>>
>>>> 3.112.18.33
>>>> ip-10-1-0-228.ap-northeast-1.compute.internal
>>>> 10.1.0.228
>>>> {'Code': 16, 'Name': 'running'}
initialize.py
import paramiko
def run_script(ip:str, rsa_key_path:str):
rsa_key: paramiko.rsakey.RSAKey = paramiko.rsakey.RSAKey.from_private_key_file(rsa_key_path)
client: paramiko.SSHClient = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, username="ubuntu", pkey=rsa_key)
stdin, stdout, stderr = client.exec_command("ls /")
d = stdout.read().decode('utf-8')
print(f"{d}")
client.close()
run_script("18.177.154.240", "/works/app/advent-code-server.pem")
I will create a Docker environment
On EC2
initialize.py
import paramiko
def run_command(client: paramiko.SSHClient, command: str):
print(f"run>{command}\n")
stdin, stdout, stderr = client.exec_command(command)
d = stdout.read().decode('utf-8')
print(f"stdout>\n{d}")
d = stderr.read().decode('utf-8')
print(f"stderr>\n{d}")
return stdin
def run_script(ip:str, rsa_key_path:str):
rsa_key: paramiko.rsakey.RSAKey = paramiko.rsakey.RSAKey.from_private_key_file(rsa_key_path)
client: paramiko.SSHClient = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, username="ubuntu", pkey=rsa_key)
run_command(client, "sudo apt-get update")
run_command(client, "sudo apt-get install -y docker.io")
client.close()
run_script("18.177.154.240", "/works/app/advent-code-server.pem")
initialize.py
import paramiko
def run_command(client: paramiko.SSHClient, command: str):
print(f"run>{command}\n")
stdin, stdout, stderr = client.exec_command(command)
d = stdout.read().decode('utf-8')
print(f"stdout>\n{d}")
d = stderr.read().decode('utf-8')
print(f"stderr>\n{d}")
return stdin
def run_script(ip:str, rsa_key_path:str):
rsa_key: paramiko.rsakey.RSAKey = paramiko.rsakey.RSAKey.from_private_key_file(rsa_key_path)
client: paramiko.SSHClient = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, username="ubuntu", pkey=rsa_key)
run_command(client, "sudo apt-get update")
run_command(client, "sudo apt-get install -y docker.io")
run_command(client, "sudo docker run hello-world")
client.close()
run_script("18.177.154.240", "/works/app/advent-code-server.pem")
atest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
initialize.py
import paramiko
def run_command(client: paramiko.SSHClient, command: str):
print(f"run>{command}\n")
stdin, stdout, stderr = client.exec_command(command)
d = stdout.read().decode('utf-8')
print(f"stdout>\n{d}")
d = stderr.read().decode('utf-8')
print(f"stderr>\n{d}")
return stdin
def run_script(ip:str, rsa_key_path:str):
rsa_key: paramiko.rsakey.RSAKey = paramiko.rsakey.RSAKey.from_private_key_file(rsa_key_path)
client: paramiko.SSHClient = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, username="ubuntu", pkey=rsa_key)
run_command(client, "sudo apt-get update")
run_command(client, "sudo apt-get install -y docker.io")
# run_command(client, "mkdir -p ${HOME}/.local/share/code-server/extensions")
run_command(client, "sudo docker run -p 0.0.0.0:8080:8080 -p0.0.0.0:8443:8443 codercom/code-server:v2 --cert")
client.close()
run_script("18.177.154.240", "/works/app/advent-code-server.pem")
..
..
info Server listening on https://0.0.0.0:8080
info - Password is 86821ed9f02ef11d83e980da
info - To use your own password, set the PASSWORD environment variable
info - To disable use `--auth none`
info - Using generated certificate and key for HTTPS
It's done !!
#logout from ec2 instance
$ exit
#local code-On the server
$ python main.py --delete
If you want to reuse it many times, please stop ec2 instance
Let's make it a web application and create a function to launch the specified Docker Image !! It's about time for the EC2 Instance edition, and that's it.
I tried to make it myself, but AWS and GCP can use existing ones, so I will use it.
https://github.com/kyorohiro/advent-2019-code-server/tree/master/remote_cs06
Recommended Posts