AOJ with Amazon EC2 의 연장선에서, 셋업 시도. 다 잘 되고 있는데, 파이썬 인터페이스인 boto 의 최신 버전에서도 아직 stop/resume instance 기능이 지원되지 않는 듯. ec2-api-tools 에서는 이게 지원되는데, 이제 와서 이쪽으로 fall back 해야 하나.. 너무 삽질 같은데... ㅠ.ㅠ
아니면 패치 만들어서 제출해볼까? -_-
(update) 소스코드 뒤져서 stop/resume 하는 법 알아냄.
Useful links
Tutorials links
Boto initial setup walkthrough
- AWS My Account; Security Credentials 로 가서 Access Key 를 만든다.
~/.bashrc고쳐서 다음 두 줄을 추가.export AWS_ACCESS_KEY_ID=access_keyexport AWS_SECRET_ACCESS_KEY=secret_key
. ~/.bashrc- Boto 트렁크를 다운받고 설치한다.
jongman@jmhome:/home/jongman/aws$ svn checkout http://boto.googlecode.com/svn/trunk/ boto-read-only
...
jongman@jmhome:/home/jongman/aws$ cd boto-read-only/
jongman@jmhome:/home/jongman/aws/boto-read-only$ ls
README bin boto cq.py dist docs setup.py
jongman@jmhome:/home/jongman/aws/boto-read-only$ sudo python setup.py install
python실행하고import boto해서 잘되면 일단 성공
Building an EBS-backed AMI
- 파이썬 띄우고 boto 임포트
>>> import boto
>>> c = boto.connect_ec2()
- 우선 ssh 접속에 쓸 key pair 를 만들고, 접속할 수 있도록 디폴트 security group 에 ssh 포트를 열어준다.
>> keypair = c.create_key_pair("testkey")
>>> keypair.save("/home/jongman/.cert")
True
>>> sg = c.get_all_security_groups()
>>> sg
[SecurityGroup:default]
>>> sg_default = sg[0]
>>> sg_default.authorize("tcp", 22, 22)
True
- Eric Hammond 가 만든 Jaunty 32비트 AMI 에서부터 시작한다. (Karmic 은 스킵하고 Lucid 부터 쓸 생각인데 잘하는 짓인지..)
>>> img = c.get_all_images(["ami-ccf615a5"])[0]
>>> res = img.run(key_name="testkey", instance_type="m1.small")
- 서버가 올라갈 때까지 기다리자.
>>> res.instances[0].state
u'pending'
>>> instance.update()
u'running'
>>> instance.dns_name
u'ec2-174-129-46-197.compute-1.amazonaws.com'
- 의외로 금방 열린다. 1분도 안걸림.. 이정도면 EBS 를 루트 디바이스로 쓸 필요 없지 않나 하는 생각이 굴뚝같지만 stop-resume 을 하면 좋기 때문에 기각.. ㅠ
- 새 터미널을 열고... 아까 만든 private key 로 접속 시도
jongman@jmhome:/home/jongman$ chmod 400 ~/.cert/testkey.pem
jongman@jmhome:/home/jongman$ [127] ssh -i /home/jongman/.cert/testkey.pem root@ec2-75-101-238-206.compute-1.amazonaws.com
The authenticity of host 'ec2-75-101-238-206.compute-1.amazonaws.com (75.101.238.206)' can't be established.
RSA key fingerprint is d0:25:0a:10:ec:83:58:1e:de:75:c4:74:6d:ec:d0:e7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'ec2-75-101-238-206.compute-1.amazonaws.com,75.101.238.206' (RSA) to the list of known hosts.
Linux ip-10-212-166-214 2.6.21.7-2.fc8xen-ec2-v1.0 #2 SMP Tue Sep 1 10:04:29 EDT 2009 i686
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
To access official Ubuntu documentation, please visit:
http://help.ubuntu.com/
Amazon EC2 Ubuntu 9.04 jaunty AMI built by Eric Hammond
http://alestic.com http://ec2ubuntu-group.notlong.com
root@ip-10-212-166-214:~#
- 호옹이 된당 ㅋㅋㅋ 테스트
root@ip-10-212-166-214:~# less /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 23
model name : Intel(R) Xeon(R) CPU E5430 @ 2.66GHz
...
root@ip-10-212-166-214:~# free -m
total used free shared buffers cached
Mem: 1706 62 1644 0 4 16
-/+ buffers/cache: 41 1665
Swap: 895 0 895
root@ip-10-212-166-214:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 9.9G 478M 8.9G 5% /
tmpfs 854M 0 854M 0% /lib/init/rw
varrun 854M 40K 854M 1% /var/run
varlock 854M 0 854M 0% /var/lock
udev 854M 76K 854M 1% /dev
tmpfs 854M 0 854M 0% /dev/shm
/dev/sda2 147G 188M 140G 1% /mnt
- small 인스턴스라 CPU 는 한개요, 램은 1.7gb, 하드는 10gb.. 뭐 그래도 이정도로도 일단은 충분하다. -_-;
- 일단 업데이트나 다 깔자
root@ip-10-212-166-214:~# apt-get update
...
root@ip-10-212-166-214:~# apt-get upgrade
...
- 의외로 깔 것이 별로 없다. 그럼 이제 AOJ 에서 1차적으로 지원하고 싶은 각종 언어들을 주르륵 깐다.
root@ip-10-212-166-214:~# apt-get install build-essential sun-java6-jdk python ruby1.8 ruby1.9 scala perl mono-devel
...
- EC2 자체에서 apt repository 미러를 유지하고 있어서 금방 깔린다. _
- 일단 여기에서 첫번째 스냅샷을 만들자. 파이썬으로 돌아가서..
>>> v = c.create_volume(10, inst.placement)
>>> v.attach(instance.id, "/dev/sdh")
u'attaching'
- 볼륨 포맷하고 마운팅, 그리고 디바이스를 쭉 복사한다
root@ip-10-212-166-214:~# mkfs -t ext3 /dev/sdh
...
root@ip-10-212-166-214:~# mount -t ext3 /dev/sdh /ebs
root@ip-10-212-166-214:~# tar cpS / | cpipe -vt -b 1024 | gzip -c | tar zxpS -C /ebs
...
root@ip-10-212-166-214:~# umount /ebs
- 그리고 파이썬으로 돌아가
>>> v.detach()
u'detaching'
>>> v.create_snapshot()
Snapshot:snap-3e482a57
>>> from boto.ec2.blockdevicemapping import EBSBlockDeviceType, BlockDeviceMapping
>>> ebs = EBSBlockDeviceType()
>>> ebs.snapshot_id = "snap-3e482a57"
>>> block_map = BlockDeviceMapping()
>>> block_map['/dev/sda1'] = ebs
>>> c.register_image('JMK-Ubuntu-9.04-EBS-20091213', 'AOJ Judging Daemon V1 - with basic compilers.',
architecture='i386', kernel_id=img.kernel_id, ramdisk_id=img.ramdisk_id, root_device_name='/dev/sda1',
block_device_map=block_map)
u'ami-a6799bcf'
- 이제 원래 인스턴스를 죽이고 새 AMI 로 띄워보자.
>>> r = c.run_instances("ami-a6799bcf", key_name="testkey", instance_type="m1.small")
>>> instance = r.instances[0]
>>> instance.update()
u'running'
>>> instance.dns_name
u'ec2-67-202-4-99.compute-1.amazonaws.com'
- 다시 ssh 로 접속해 보면
jongman@jmhome:/home/jongman$ [255] ssh -i /home/jongman/.cert/testkey.pem root@ec2-67-202-4-99.compute-1.amazonaws.com
- 잘된다. 핫핫핫 원하던 것 다 깔려 있음.
- 이 시점에서, EBS 볼륨에 대한 변화는 인스턴스를 다시 띄우면 없어진다. 따라서 서버가 pwn 당해도 그냥 죽이고 다시 띄우면 말끔한 새 인스턴스를 얻을 수 있음.
- EBS-backed instance 의 장점은 stop 했다가 다시 resume 할 수 있다는 것. 해보자.
>>> instance.stop()
>>> instance.update()
u'terminated'
- 헐 시발 이게 아니네 -_-; boto 에서 stop/start 어떻게 하지..
- 알아보니 connection 오브젝트에서 할 수 있다.
>>> c.stop_instances([instance.id])
[Instance:i-6f004807]
>>> instance.update()
u'stopping'
>>> instance.update()
u'stopped'
>>> c.start_instances([instance.id])
[Instance:i-6f004807]
>>> instance.update()
u'running'
command-line tutorials
command-line initial setup
mkdir .cert- X.509 certificate 생성하기/다운받기
- generate a certificate.
- download private key/certificate to
~/.cert
- Account number 기록
- 페이지 상단에 나오는 account number 를 기록해 둔다.
- Amazon EC2 API Tools 와 Amazon EC2 AMI Tools 를 받는다.
.bashrc고쳐서 다음 세 줄을 추가. 파일이름은 적절히 붙여넣는다.export EC2_PRIVATE_KEY=$HOME/.cert/pk-****.pemexport EC2_CERT=$HOME/.cert/cert-****.pemexport JAVA_HOME=/usr/lib/jvm/java-6-sun/
. .bashrcec2-describe-images -o self -o amazon해서 잘나오면 성공


