[DOCKER] code-server online environment (6) Let's automate

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

Introduction

Up to the last time, I tried to launch Code-Server on EC Instance using Docker. This time, let's automate this work.

Create an EC2 Instance

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/.

Screen Shot 2019-12-24 at 0.39.23.png

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'}

Enter with SSH

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")

Install Docker

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")

Docker's Hello World

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.

Let's start Code-Server

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
Screen Shot 2019-12-24 at 1.11.08.png Screen Shot 2019-12-24 at 1.06.50.png Screen Shot 2019-12-24 at 1.12.23.png

It's done !!

Let's delete

#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

next time

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.

code

https://github.com/kyorohiro/advent-2019-code-server/tree/master/remote_cs06

Recommended Posts

code-server online environment (6) Let's automate
code-server online environment (1)
code-server online environment (5) Launch code-server on Docker
code-server online environment (4) Launch code-server on the EC2 instance
code-server online environment (7) Deploy compute on git on EC2
code-server Local environment (9) Let's prepare an MVC environment including DB (6)
code-server online environment (3) Launch an EC2 instance with Boto3
code-server Local environment (4) Let's prepare an MVC environment including DB (1)
code-server Online environment (2) Create a virtual network with Boto3
code-server Local environment (5) Let's prepare an MVC environment including DB (2)
Automate environment construction with Shell Script