Skip to content

Instantly share code, notes, and snippets.

@threecourse
Last active March 2, 2016 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save threecourse/42b7e919d90cf03a7350 to your computer and use it in GitHub Desktop.
Save threecourse/42b7e919d90cf03a7350 to your computer and use it in GitHub Desktop.
request spot instance by python

spot_request.py

  • request spot instance
  • wait for running
  • print IP address

aws_run.py

  • rsync
  • nohup run command
  • s3 sync log
  • (shutdown)
import argparse
import subprocess
parser = argparse.ArgumentParser(description='stopper')
parser.add_argument('-i', '--ip', action='store', default="")
parser.add_argument('--cmd', action='store', default="")
args = parser.parse_args()
command = """
awsip0={}
fromdir0=~/code/
todir0=ec2-user@$awsip0:/home/ec2-user/code
keypath=$HOME/xxx/xxx.pem
rsync -av -e "ssh -o 'StrictHostKeyChecking=no' -i $keypath" $fromdir0 $todir0 --exclude '.git' --delete
ssh -o 'StrictHostKeyChecking=no' -t -t -i $keypath ec2-user@$awsip0 << EOF
cd code
nohup python -u aws/run_and_stop.py {} > out_v.log 2>&1 &
exit
EOF
""".format(args.ip, args.cmd)
print "-----------------------------------------------------------------"
print command
print "-----------------------------------------------------------------"
subprocess.call(command, shell=True)
import subprocess
import argparse
import os
import platform
import datetime
parser = argparse.ArgumentParser(description='stopper')
parser.add_argument('--shutdown', default=False, action='store_true')
parser.add_argument('-f', '--file', action='store', default="")
parser.add_argument('--post', action='store', default="")
parser.add_argument('--pre', action='store', default="")
args = parser.parse_args()
def sync_log():
# unique folder
if not os.path.exists("../others"):
os.makedirs("../others")
# create unique name
ydms = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
cname = platform.node()
# copy out_v.log
if os.path.exists("out_v.log"):
subprocess.call("cp -p out_v.log ../model/others/out_v_{}_{}.log".format(cname, ydms), shell=True)
# sync sync folder
subprocess.call("aws s3 sync ../model/others/ s3://bucket.xxx/others/", shell=True)
try:
if args.pre != "":
subprocess.call("python -u {}".format(args.pre), shell=True)
if args.file != "":
subprocess.call("python -u {}".format(args.file), shell=True)
if args.post != "":
subprocess.call("python -u {}".format(args.post), shell=True)
finally:
try:
sync_log()
finally:
if args.shutdown:
subprocess.call("sudo shutdown -h now", shell=True)
else:
print "finished!!!!"
{
"ImageId": "ami-xxxxxxx",
"KeyName": "xxxxxx",
"SecurityGroups": ["launch-xxx"],
"InstanceType": "c4.xlarge",
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sdf",
"Ebs": {
"VolumeSize": 32,
"DeleteOnTermination": true,
"VolumeType": "gp2",
"Encrypted": false
}
}
],
"EbsOptimized": true
}
"""
spot-request -> wait for running -> print ip address
"""
import subprocess
import json
import time
def spot_request():
cmd = 'aws ec2 request-spot-instances --spot-price "0.50" --type "one-time" --launch-specification file://aws/spec.json'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_data, stderr_data = p.communicate()
j = json.loads(stdout_data)
spot_request_id = j["SpotInstanceRequests"][0]["SpotInstanceRequestId"]
print "requested spot_request_id"
return spot_request_id
def describe_spot_requests(spot_req_id):
cmd = "aws ec2 describe-spot-instance-requests"
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_data, stderr_data = p.communicate()
j = json.loads(stdout_data)
j_spot_reqs = [jj for jj in j["SpotInstanceRequests"] if jj["SpotInstanceRequestId"] == spot_req_id]
j_spot_req = j_spot_reqs[0]
return j_spot_req
def instance_id_spot_requests(spot_req_id):
while True:
j_spot_req = describe_spot_requests(spot_request_id)
if j_spot_req.has_key("InstanceId"):
print "instance id", j_spot_req["InstanceId"]
return j_spot_req["InstanceId"]
time.sleep(5) # 5sec
print "instance id not assigned yet"
def describe_instance_status(instance_id):
cmd = "aws ec2 describe-instance-status"
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_data, stderr_data = p.communicate()
j = json.loads(stdout_data)
j_instance_statuses = [jj for jj in j["InstanceStatuses"] if jj["InstanceId"] == instance_id]
if len(j_instance_statuses) == 0:
return "noinfo"
else:
j_instance_status = j_instance_statuses[0]
return j_instance_status["InstanceState"]["Name"]
def wait_for_running(instance_id):
while True:
status = describe_instance_status(instance_id)
print status
if status == "running": break
time.sleep(5) # 5sec
def get_public_ip(instance_id):
cmd = "aws ec2 describe-instances --instance-ids {}".format(instance_id)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_data, stderr_data = p.communicate()
j = json.loads(stdout_data)
return j["Reservations"][0]["Instances"][0]["NetworkInterfaces"][0]["Association"]["PublicIp"]
spot_request_id = spot_request()
instance_id = instance_id_spot_requests(spot_request_id)
wait_for_running(instance_id)
public_ip = get_public_ip(instance_id)
print public_ip
print "awsip={}".format(public_ip)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment