2017-02-08 11 views
0

コマンドの実行を解析しない方法をrun_instancesしてインスタンスを作成し、私がログインすることができ、実行するアップデートなどUserDataのファイルが渡されないが、AWS Boto3 PythonはUserDataのパラメータに

ここでは私のスクリプトです:

#!/usr/bin/python 

import boto3 



def main(): 


    dev_server_ami_id = 'ami-0b33d91d' # This is currently the Amazon Linux base AMI. 
    dev_server_sec_group = 'xxxxxxxxxx' 
    dev_server_az = 'us-east-1a' 
    dev_server_subnet_id = 'xxxxxxxxxxx' 
    dev_server_name = 'test_server_name' 
    dev_instance_type = 't2.large' 
    slash_sites_size = 16 
    slash_scratch_size = 5 

    ec2client = boto3.client('ec2',aws_access_key_id='asdfasdfasdf',aws_secret_access_key='asdfasdfasdfasdf') 

    creation_response = ec2client.run_instances(DryRun=False,MinCount=1,ImageId=dev_server_ami_id, 
     MaxCount=1,KeyName='mcp_demo_dev',SecurityGroupIds=[dev_server_sec_group], 
     InstanceType=dev_instance_type,Placement={'AvailabilityZone': dev_server_az},SubnetId=dev_server_subnet_id,UserData="file://C:\\Users\\xxxxx\\Dev\\Site Where My Script Is\\base_server_bootstrap.sh", 
     BlockDeviceMappings=[{'DeviceName':'/dev/xvdb','Ebs':{'VolumeSize':slash_sites_size,'DeleteOnTermination':True}}, 
      {'DeviceName':'/dev/xvdc','Ebs':{'VolumeSize':slash_scratch_size,'DeleteOnTermination':True}}]) 
    instance_id = creation_response['Instances'][0]['InstanceId'] 
    ec2client.create_tags(Resources=[instance_id,],Tags=[{'Key':'Name','Value': dev_server_name,},],) 



if __name__ == "__main__": main() 

これはスクリプトに関する限りすべての作業ですが、pythonスクリプトとシェルスクリプトが同じであるため、ファイル名だけでファイルを渡すためのいくつかのオプションを試しましたディレクトリの前に "file://"というフルパスを付けずにシェルスクリプト名だけを渡し、フルパスとスクリプト名をnoその前に "file://"があります。

いずれのトリックでも、run_instancesメソッドはそのパラメータを無視しているようです。

ここでは、run_instance()メソッドに渡そうとしているシェルスクリプトを参照しています。それは呼ばれていない。

#!/bin/bash 

# 
# These variables will be used to create directories and name everything client specific. 
# All files that are pulled from S3 have to follow the naming convention and are client specific. 
# 

clientName="demo" 
# If both author and public are true then we are in DEV. 
magnoliaPublic=true 
magnoliaAuthor=true 
# Set this if the client is doing light-module development work. 
lightModule=true 
lightModuleFileName="one-pager-module.zip" 

# build the additional filesytems and mount points 
sudo mkfs -t ext4 /dev/xvdb 
sudo mkfs -t ext4 /dev/xvdc 
sudo mkdir /sites 
sudo mkdir /scratch 
sudo mount /dev/xvdb /sites 
sudo mount /dev/xvdc /scratch 

# add them to the fstab so that they will be there after a reboot 
sudo cat /etc/fstab > /home/ec2-user/fstab_temp 
sudo echo -e "/dev/xvdb\t/sites\text4\tdefaults,nofail\t0\t2" >> /home/ec2-user/fstab_temp 
sudo echo -e "/dev/xvdc\t/scratch\text4\tdefaults,nofail\t0\t2" >> /home/ec2-user/fstab_temp 
sudo cp /home/ec2-user/fstab_temp /etc/fstab 
sudo rm /home/ec2-user/fstab_temp 

# Set up of the base environment with Java and Tomcat. 

# Need to figure out how to set the specific version of the JDK that we install. Either copy it to S3 or direct it through yum. 
sudo yum -y install java-1.8.0-openjdk 
sudo groupadd tomcat 
sudo useradd -g tomcat tomcat 
sudo wget -O /home/tomcat/apache-tomcat-8.5.9.tar.gz http://mirror.stjschools.org/public/apache/tomcat/tomcat-8/v8.5.9/bin/apache-tomcat-8.5.9.tar.gz 
sudo tar -xf /home/tomcat/apache-tomcat-8.5.9.tar.gz -C /opt 
sudo rm /home/tomcat/apache-tomcat-8.5.9.tar.gz 
sudo chown -R tomcat:tomcat /opt/apache-tomcat-8.5.9/ 

# Create our individual JVM directory structure. 
sudo mkdir /sites/ 
sudo mkdir /sites/${clientName} 
sudo mkdir /sites/${clientName}/magnolia-base/ 
sudo mkdir /sites/${clientName}/light-module/ 
# If there is content to deploy to the light-module directory then grab it. 
if $lightModule; then 
    sudo aws s3 cp s3://mcp-${clientName}-light-module/${lightModuleFileName} /sites/${clientName}/light-module 
    sudo unzip /sites/${clientName}/light-module/${lightModuleFileName} -d /sites/${clientName}/light-module/ 
fi 
# Build the base tomcat directories for this client. 
sudo mkdir /sites/${clientName}/magnolia-base/common /sites/${clientName}/magnolia-base/conf /sites/${clientName}/magnolia-base/logs /sites/${clientName}/magnolia-base/server /sites/${clientName}/magnolia-base/shared /sites/${clientName}/magnolia-base/temp /sites/${clientName}/magnolia-base/work 
# Copy configs down from S3. mcp-demo-configs 
sudo aws s3 cp s3://mcp-${clientName}-configs/${clientName}_conf.zip /sites/${clientName}/magnolia-base/ 
sudo unzip /sites/${clientName}/magnolia-base/${clientName}_conf.zip -d /sites/${clientName}/magnolia-base/ 

# Set one or more appBase directories and copy our Magnolia WAR files in. 
if $magnoliaPublic; then 
    sudo mkdir /sites/${clientName}/magnolia-base/webapps_public 
    sudo aws s3 cp s3://mcp-${clientName}-magnolia-wars/demo-mcpLive-2.3.war /sites/${clientName}/magnolia-base/webapps_public 
fi 
if $magnoliaAuthor; then 
    sudo mkdir /sites/${clientName}/magnolia-base/webapps_author 
    sudo aws s3 cp s3://mcp-${clientName}-magnolia-wars/demo-mcpEdit-2.3.war /sites/${clientName}/magnolia-base/webapps_author 
fi 

sudo chown -R tomcat:tomcat /sites 

# From here on out everything is done as the tomcat user. 
sudo su - tomcat 

# Set up the environment variables. 
echo export "JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.121-0.b13.29.amzn1.x86_64" >> /home/tomcat/.bash_profile 
echo export JRE_HOME=\$JAVA_HOME/jre >> /home/tomcat/.bash_profile 

# Create our Tomcat setenv.sh file 
touch /opt/apache-tomcat-8.5.9/bin/setenv.sh 
echo export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxPermSize=256m -Xms64M -Xmx1024M -Djava.awt.headless=true" >> /opt/apache-tomcat-8.5.9/bin/setenv.sh 
echo export CATALINA_HOME=/opt/apache-tomcat-8.5.9 >> /opt/apache-tomcat-8.5.9/bin/setenv.sh 
echo export CATALINA_BASE=/sites/${clientName}/magnolia-base >> /opt/apache-tomcat-8.5.9/bin/setenv.sh 
chmod 755 /opt/apache-tomcat-8.5.9/bin/setenv.sh 

sudo -S -u tomcat -i /bin/bash -l -c '/opt/apache-tomcat-8.5.9/bin/startup.sh' 

ありがとうございます。

+0

そして、このスクリプト・ファイルは 'run_instances()'でインスタンスにコピーされますか?それが '/ var/lib/cloud/instance-id/user-data.txt'で利用可能かどうか確認してください。 – franklinsijo

+0

コメントありがとうございます。いいえ、そのファイルは空です。それは私の問題です。コマンドを書式設定してユーザーデータでファイルを渡す方法を理解できません。理論的には、コマンドラインから実行するときに "file://"指示文を使って行うことができます。私はどのようにPython内でそれを行うかを示すドキュメントは見つかりません。 –

+0

この方法で 'file:/// C:/ Users/xxxxx/Dev/Site%20Where%20My Script%20Is/base_server_bootstrap.sh'を渡しても動作しますか? – franklinsijo

答えて

0

ユーザーデータが正しく指定されていません。ユーザーデータの最初の行は、それがシェルスクリプトかクラウド初期スクリプトかを指定する必要があります。

For a bash script, the first line should be

#!/bin/bash 

ユーザーデータのシェルスクリプトは、#で始まる必要があります!文字と、スクリプトを読みたいインタプリタへのパス(通常は/ bin/bash)を指定します。シェルスクリプティングの素晴らしい紹介については、Linux Documentation Project(tldp.org)のBASH Programming HOW-TOを参照してください。

クラウドINITについて

#cloud-config 

構文が異なるがクラウドINITユーザ指令は、起動時にインスタンスにスクリプトが渡されたのと同じ方法を渡すことができます。 cloud-initの詳細については、http://cloudinit.readthedocs.org/en/latest/index.htmlを参照してください。

+0

コメントありがとうございます。質問に貼り付けたスクリプトはUserDataスクリプトではなく、 "run_instances()"を呼び出すPythonスクリプトです。実際にはシェルスクリプトであり、上に#!/ bin/bash行があるUserDataスクリプトを貼り付けていません。このスクリプトは、コンソールからインスタンスを起動すると完全に動作します。私はちょうど "run_instances()"を呼び出すPythonスクリプトをファイルに渡すことができません。 –

+0

実際には投稿されていないスクリプトについては、私たちがヘルプを提供することは不可能です。がんばろう! – 2ps

+0

皆さん、私が助けを求めているのではありません。私は今それを追加しますが、私はboto3ライブラリからのPythonコマンドに関する助けを求めています。 –

0

UserDataを

などの文字列としてスクリプトの内容を渡します。

ec2client.run_instances(,...,UserData=open("C:\\Users\\xxxxx\\Dev\\Site Where My Script Is\\base_server_bootstrap.sh").read(),...) 
関連する問題